update credits
[librepilot.git] / flight / pios / stm32f30x / pios_wdg.c
blob9cf3c35be4b59828642d9cb79d81e72cc6218006
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_WDG Watchdog Functions
6 * @brief PIOS Comamnds to initialize and clear watchdog timer
7 * @{
9 * @file pios_wdg.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * Parts by Thorsten Klose (tk@midibox.org) (tk@midibox.org)
12 * @brief Hardware Abstraction Layer for SPI ports of STM32
13 * @see The GNU Public License (GPL) Version 3
14 * @notes
16 * The PIOS Watchdog provides a HAL to initialize a watchdog
18 *****************************************************************************/
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful, but
26 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 * for more details.
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 * @todo This is virtually identical to the F1xx code and should be merged.
39 #include "pios.h"
41 #ifdef PIOS_INCLUDE_WDG
43 #include "stm32f30x_iwdg.h"
44 #include "stm32f30x_dbgmcu.h"
45 #include "stm32f30x_rtc.h"
47 static struct wdg_configuration {
48 uint16_t used_flags;
49 uint16_t bootup_flags;
50 } wdg_configuration;
52 /**
53 * @brief Initialize the watchdog timer for a specified timeout
55 * It is important to note that this function returns the achieved timeout
56 * for this hardware. For hardware independence this should be checked when
57 * scheduling updates. Other hardware dependent details may need to be
58 * considered such as a window time which sets a minimum update time,
59 * and this function should return a recommended delay for clearing.
61 * For the STM32 nominal clock rate is 32 khz, but for the maximum clock rate of
62 * 60 khz and a prescaler of 4 yields a clock rate of 15 khz. The delay that is
63 * set in the watchdog assumes the nominal clock rate, but the delay for FreeRTOS
64 * to use is 75% of the minimal delay.
66 * @returns Maximum recommended delay between updates based on PIOS_WATCHDOG_TIMEOUT constant
68 uint16_t PIOS_WDG_Init()
70 uint16_t delay = ((uint32_t)PIOS_WATCHDOG_TIMEOUT * 60) / 16;
72 if (delay > 0x0fff) {
73 delay = 0x0fff;
75 #if defined(PIOS_INCLUDE_WDG)
76 DBGMCU_APB1PeriphConfig(DBGMCU_IWDG_STOP, ENABLE); // OP-1272 : write in APB1 register
77 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
78 IWDG_SetPrescaler(IWDG_Prescaler_16);
79 IWDG_SetReload(delay);
80 IWDG_ReloadCounter();
81 IWDG_Enable();
83 // watchdog flags now stored in backup registers
84 PWR_BackupAccessCmd(ENABLE);
86 wdg_configuration.bootup_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
87 #endif
88 return delay;
91 /**
92 * @brief Register a module against the watchdog
94 * There are two ways to use PIOS WDG: this is for when
95 * multiple modules must be monitored. In this case they
96 * must first register against the watchdog system and
97 * only when all of the modules have been updated with the
98 * watchdog be cleared. Each module must have its own
99 * bit in the 16 bit
101 * @param[in] flag the bit this module wants to use
102 * @returns True if that bit is unregistered
104 bool PIOS_WDG_RegisterFlag(uint16_t flag_requested)
106 // flag are being registered so we are in module initialization phase
107 // clear the WDG to prevent timeout while initializing modules. (OP-815)
108 PIOS_WDG_Clear();
110 /* Fail if flag already registered */
111 if (wdg_configuration.used_flags & flag_requested) {
112 return false;
115 // FIXME: Protect with semaphore
116 wdg_configuration.used_flags |= flag_requested;
118 return true;
122 * @brief Function called by modules to indicate they are still running
124 * This function will set this flag in the active flags register (which is
125 * a backup regsiter) and if all the registered flags are set will clear
126 * the watchdog and set only this flag in the backup register
128 * @param[in] flag the flag to set
129 * @return true if the watchdog cleared, false if flags are pending
131 bool PIOS_WDG_UpdateFlag(uint16_t flag)
133 // we can probably avoid using a semaphore here which will be good for
134 // efficiency and not blocking critical tasks. race condition could
135 // overwrite their flag update, but unlikely to block _all_ of them
136 // for the timeout window
137 uint16_t cur_flags = RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
139 if ((cur_flags | flag) == wdg_configuration.used_flags) {
140 PIOS_WDG_Clear();
141 RTC_WriteBackupRegister(PIOS_WDG_REGISTER, flag);
142 return true;
143 } else {
144 RTC_WriteBackupRegister(PIOS_WDG_REGISTER, cur_flags | flag);
145 return false;
150 * @brief Returns the flags that were set at bootup
152 * This is used for diagnostics, if only one flag not set this
153 * was likely the module that wasn't running before reset
155 * @return The active flags register from bootup
157 uint16_t PIOS_WDG_GetBootupFlags()
159 return wdg_configuration.bootup_flags;
163 * @brief Returns the currently active flags
165 * For external monitoring
167 * @return The active flags register
169 uint16_t PIOS_WDG_GetActiveFlags()
171 return RTC_ReadBackupRegister(PIOS_WDG_REGISTER);
175 * @brief Clear the watchdog timer
177 * This function must be called at the appropriate delay to prevent a reset event occuring
179 void PIOS_WDG_Clear(void)
181 #if defined(PIOS_INCLUDE_WDG)
182 IWDG_ReloadCounter();
183 #endif
186 #endif /* PIOS_INCLUDE_WDG */