2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_WDG Watchdog Functions
6 * @brief PIOS Comamnds to initialize and clear watchdog timer
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
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
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
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
37 #ifdef PIOS_INCLUDE_WDG
39 #include <stm32f0xx_iwdg.h>
40 #include <stm32f0xx_rcc.h>
41 #include <stm32f0xx_dbgmcu.h>
44 static struct wdg_configuration
{
46 uint16_t bootup_flags
;
48 #define LSI_FREQ 50000
50 * @brief Initialize the watchdog timer for a specified timeout
52 * It is important to note that this function returns the achieved timeout
53 * for this hardware. For hardware indendence this should be checked when
54 * scheduling updates. Other hardware dependent details may need to be
55 * considered such as a window time which sets a minimum update time,
56 * and this function should return a recommended delay for clearing.
58 * For the STM32 nominal clock rate is 32 khz, but for the maximum clock rate of
59 * 60 khz and a prescalar of 4 yields a clock rate of 15 khz. The delay that is
60 * set in the watchdog assumes the nominal clock rate, but the delay for FreeRTOS
61 * to use is 75% of the minimal delay.
63 * @returns Maximum recommended delay between updates based on PIOS_WATCHDOG_TIMEOUT constant
65 uint16_t PIOS_WDG_Init()
67 uint16_t delay
= (((uint32_t)PIOS_WATCHDOG_TIMEOUT
* LSI_FREQ
) / (1000 * 32));
72 #if defined(PIOS_INCLUDE_WDG)
73 DBGMCU_APB1PeriphConfig(DBGMCU_IWDG_STOP
, ENABLE
); // make the watchdog stop counting in debug mode
74 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable
);
75 IWDG_SetPrescaler(IWDG_Prescaler_32
);
76 IWDG_SetReload(delay
);
80 // watchdog flags now stored in backup registers
81 PWR_BackupAccessCmd(ENABLE
);
83 PIOS_BKP_WriteRegister(PIOS_WDG_REGISTER
, 0x0);
84 wdg_configuration
.bootup_flags
= PIOS_BKP_ReadRegister(PIOS_WDG_REGISTER
);
90 * @brief Register a module against the watchdog
92 * There are two ways to use PIOS WDG: this is for when
93 * multiple modules must be monitored. In this case they
94 * must first register against the watchdog system and
95 * only when all of the modules have been updated with the
96 * watchdog be cleared. Each module must have its own
99 * @param[in] flag the bit this module wants to use
100 * @returns True if that bit is unregistered
102 bool PIOS_WDG_RegisterFlag(uint16_t flag_requested
)
104 // flag are being registered so we are in module initialization phase
105 // clear the WDG to prevent timeout while initializing modules. (OP-815)
108 /* Fail if flag already registered */
109 if (wdg_configuration
.used_flags
& flag_requested
) {
113 // FIXME: Protect with semaphore
114 wdg_configuration
.used_flags
|= flag_requested
;
120 * @brief Function called by modules to indicate they are still running
122 * This function will set this flag in the active flags register (which is
123 * a backup regsiter) and if all the registered flags are set will clear
124 * the watchdog and set only this flag in the backup register
126 * @param[in] flag the flag to set
127 * @return true if the watchdog cleared, false if flags are pending
129 bool PIOS_WDG_UpdateFlag(uint16_t flag
)
131 // we can probably avoid using a semaphore here which will be good for
132 // efficiency and not blocking critical tasks. race condition could
133 // overwrite their flag update, but unlikely to block _all_ of them
134 // for the timeout window
135 uint16_t cur_flags
= PIOS_BKP_ReadRegister(PIOS_WDG_REGISTER
);
137 if ((cur_flags
| flag
) == wdg_configuration
.used_flags
) {
139 PIOS_BKP_WriteRegister(PIOS_WDG_REGISTER
, flag
);
142 PIOS_BKP_WriteRegister(PIOS_WDG_REGISTER
, cur_flags
| flag
);
148 * @brief Returns the flags that were set at bootup
150 * This is used for diagnostics, if only one flag not set this
151 * was likely the module that wasn't running before reset
153 * @return The active flags register from bootup
155 uint16_t PIOS_WDG_GetBootupFlags()
157 return wdg_configuration
.bootup_flags
;
161 * @brief Returns the currently active flags
163 * For external monitoring
165 * @return The active flags register
167 uint16_t PIOS_WDG_GetActiveFlags()
169 return PIOS_BKP_ReadRegister(PIOS_WDG_REGISTER
);
173 * @brief Clear the watchdog timer
175 * This function must be called at the appropriate delay to prevent a reset event occuring
177 void PIOS_WDG_Clear(void)
179 #if defined(PIOS_INCLUDE_WDG)
180 IWDG_ReloadCounter();
184 #endif /* PIOS_INCLUDE_WDG */