dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_wdg.c
Go to the documentation of this file.
1 
20 /*
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation; either version 3 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful, but
27  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29  * for more details.
30  *
31  * You should have received a copy of the GNU General Public License along
32  * with this program; if not, see <http://www.gnu.org/licenses/>
33  */
34 
35 /*
36  * @todo This is virtually identical to the F1xx code and should be merged.
37  */
38 
39 #include "pios.h"
40 #if defined(STM32F40_41xxx) || defined(STM32F446xx) /* F4 */
41 #include "stm32f4xx_iwdg.h"
42 #include "stm32f4xx_dbgmcu.h"
43 #include "stm32f4xx_rtc.h"
44 #elif defined(STM32F30X) /* F3 */
45 #include "stm32f30x_iwdg.h"
46 #include "stm32f30x_dbgmcu.h"
47 #include "stm32f30x_rtc.h"
48 #elif defined(STM32F10X_MD) /* F1 */
49 #include "stm32f10x_iwdg.h"
50 #include "stm32f10x_dbgmcu.h"
51 
52 #define RTC_ReadBackupRegister BKP_ReadBackupRegister
53 #define RTC_WriteBackupRegister BKP_WriteBackupRegister
54 #elif defined(STM32F0XX) /* F0 */
55 #include "stm32f0xx_iwdg.h"
56 #include "stm32f0xx_dbgmcu.h"
57 #else
58 #error Attempted to build on unsupported target
59 #endif
60 
61 #if defined(PIOS_WDG_REGISTER)
62 static struct wdg_configuration {
63  uint16_t used_flags;
64  uint16_t bootup_flags;
65 } wdg_configuration;
66 #endif
67 
84 uint16_t PIOS_WDG_Init()
85 {
86  uint16_t delay = ((uint32_t) PIOS_WATCHDOG_TIMEOUT * 60) / 16;
87  if (delay > 0x0fff)
88  delay = 0x0fff;
89 #if defined(PIOS_INCLUDE_WDG)
90  DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE); // make the watchdog stop counting in debug mode
91  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
92  IWDG_SetPrescaler(IWDG_Prescaler_16);
93  IWDG_SetReload(delay);
94  IWDG_ReloadCounter();
95  IWDG_Enable();
96 
97  // PIOS_WDG_REGISTER is used to choose whether the flag-mode is available
98  // for watchdog.. for maximal compatability with existing targets
99 #ifdef PIOS_WDG_REGISTER
100  // watchdog flags now stored in backup registers
101  PWR_BackupAccessCmd(ENABLE);
102 
103  wdg_configuration.bootup_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
104 
105  /*
106  * Start from an empty set of registered flags so previous boots
107  * can't influence the current one
108  */
109  RTC_WriteBackupRegister(PIOS_WDG_REGISTER, 0);
110 #endif // PIOS_WDG_REGISTER
111 #endif
112  return delay;
113 }
114 
115 #ifdef PIOS_WDG_REGISTER
116 
129 bool PIOS_WDG_RegisterFlag(uint16_t flag_requested)
130 {
131 
132  /* Fail if flag already registered */
133  if(wdg_configuration.used_flags & flag_requested)
134  return false;
135 
136  // FIXME: Protect with semaphore
137  wdg_configuration.used_flags |= flag_requested;
138 
139  return true;
140 }
141 
152 bool PIOS_WDG_UpdateFlag(uint16_t flag)
153 {
154  // Validate that the flag has been registered. If not, halt the program.
155  if ((wdg_configuration.used_flags & flag) == 0) {
156  PIOS_Assert(0);
157  }
158 
159  // we can probably avoid using a semaphore here which will be good for
160  // efficiency and not blocking critical tasks. race condition could
161  // overwrite their flag update, but unlikely to block _all_ of them
162  // for the timeout window
163  uint16_t cur_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
164 
165  if((cur_flags | flag) == wdg_configuration.used_flags) {
166  PIOS_WDG_Clear();
167  RTC_WriteBackupRegister(PIOS_WDG_REGISTER, flag);
168  return true;
169  } else {
170  RTC_WriteBackupRegister(PIOS_WDG_REGISTER, cur_flags | flag);
171  return false;
172  }
173 }
174 
183 uint16_t PIOS_WDG_GetBootupFlags()
184 {
185  return wdg_configuration.bootup_flags;
186 }
187 
195 uint16_t PIOS_WDG_GetActiveFlags()
196 {
197  return RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
198 }
199 #endif //PIOS_WDG_REGISTER
200 
206 void PIOS_WDG_Clear(void)
207 {
208 #if defined(PIOS_INCLUDE_WDG)
209  IWDG_ReloadCounter();
210 #endif
211 }
Main PiOS header to include all the compiled in PiOS options.
#define PIOS_WATCHDOG_TIMEOUT
Definition: pios_board.h:92
bool PIOS_WDG_RegisterFlag(uint16_t flag_requested)
Register a module against the watchdog.
Definition: pios_wdg.c:86
bool PIOS_WDG_UpdateFlag(uint16_t flag)
Function called by modules to indicate they are still running.
Definition: pios_wdg.c:102
uint16_t PIOS_WDG_GetBootupFlags()
Returns the flags that were set at bootup.
Definition: pios_wdg.c:125
uint16_t PIOS_WDG_GetActiveFlags()
Returns the currently active flags.
Definition: pios_wdg.c:137
void PIOS_WDG_Clear(void)
Clear the watchdog timer.
Definition: pios_wdg.c:147
#define PIOS_WDG_REGISTER
Definition: pios_board.h:93
uint16_t PIOS_WDG_Init()
Initialize the watchdog timer for a specified timeout.
Definition: pios_wdg.c:63
uint32_t flag
Definition: msp_messages.h:99
#define PIOS_Assert(test)
Definition: pios_debug.h:52