GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_iwdg.c
Go to the documentation of this file.
1 /*!***************************************************************************
2  @file stm32f103cb_iwdg.c
3  @brief Source file for stm32f103cb WATCHDOG
4  @author Stuart Ianna
5  @version 0.1
6  @date June 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug
10 
11  @details
12 
13  @par Compilers
14  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
15 ******************************************************************************/
16 
17 #include "stm32f103cb_iwdg.h"
18 
19 mcu_error IWDG_setup(uint32_t timeoutms){
20 
21  //Watchdog runs on an independant 40khz oscillator
22  //Prescaler rnges from 4-256 in powers of 2
23 
24  const float watchdog_LSI_period = 1/40.0f; //This is /40 as timeout is in ms
25  uint16_t reloadValue;
26  uint8_t prescaler; //These are the bits written to the register, not the value itself
27 
28 
29  //Select the correct prescaler and calculate reload
30  if(timeoutms < 400){
31 
32  //Prescaler = 4
33  prescaler = 0;
34  reloadValue = timeoutms/(watchdog_LSI_period * 4) -1; //Subtract 1 as counter counts to 0
35  }
36  else if(timeoutms < 800){
37 
38  //Prescaler = 8
39  prescaler = 1;
40  reloadValue = timeoutms/(watchdog_LSI_period * 8) -1; //Subtract 1 as counter counts to 0
41  }
42  else if(timeoutms < 1600){
43 
44  //Prescaler = 16
45  prescaler = 2;
46  reloadValue = timeoutms/(watchdog_LSI_period * 16) -1; //Subtract 1 as counter counts to 0
47  }
48  else if(timeoutms < 3200){
49 
50  //Prescaler = 32
51  prescaler = 3;
52  reloadValue = timeoutms/(watchdog_LSI_period * 32) -1; //Subtract 1 as counter counts to 0
53  }
54  else if(timeoutms < 6500){
55 
56  //Prescaler = 64
57  prescaler = 4;
58  reloadValue = timeoutms/(watchdog_LSI_period * 64) -1; //Subtract 1 as counter counts to 0
59  }
60  else if(timeoutms < 13000){
61 
62  //Prescaler = 128
63  prescaler = 5;
64  reloadValue = timeoutms/(watchdog_LSI_period * 128) -1; //Subtract 1 as counter counts to 0
65  }
66  else if(timeoutms < 26000){
67 
68  //Prescaler = 256
69  prescaler = 6;
70  reloadValue = timeoutms/(watchdog_LSI_period * 256) -1; //Subtract 1 as counter counts to 0
71  }
72  else{
73 
74  //Shouldn't be this long
75  if(MCU_debugIsEnabled()){
76 
78  }
79  return E_IWDG_PERIOD;
80  }
81 
82 
83  //Write 0x5555 to enable access to the prescaler and reload registers
84  IWDG_KR = 0x5555;
85 
86  //Write the preload value
87  IWDG_PR = prescaler;
88 
89  //Write the reload value
90  IWDG_RLR = reloadValue;
91 
92  //Write 0xCCCC to the key register to enable the watchdog timer
93  IWDG_KR = 0xCCCC;
94 
95  return E_IWDG_NOERROR;
96 }
97 
98 bool IWDG_resetCheck(void){
99 
100  if(RCC_CSR & 0x20000000){
101 
102  //Reset the flag
103  RCC_CSR |= 0x1000000;
104  return true;
105  }
106 
107  return false;
108 }
109 
110 void IWDG_feed(void){
111 
112  //Write 0xAAAA to the key register to reload the watchdog timer
113  IWDG_KR = 0xAAAA;
114 }
115 
Header file for stm32f103cb independant watchdog timer.
Error IWDG: Period too long.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
void IWDG_feed(void)
Reload the watchdog timer.
mcu_error IWDG_setup(uint32_t timeoutms)
Initialize the independant watchdog timer.
bool IWDG_resetCheck(void)
Check if reset occured from watchdog timeout.
Error IWDG: No error.
mcu_error
Error enumerators for the Debug peripheral.