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 "stm32f10x_iwdg.h"
40 #include "stm32f10x_dbgmcu.h"
42 static struct wdg_configuration
{
44 uint16_t bootup_flags
;
48 * @brief Initialize the watchdog timer for a specified timeout
50 * It is important to note that this function returns the achieved timeout
51 * for this hardware. For hardware indendence this should be checked when
52 * scheduling updates. Other hardware dependent details may need to be
53 * considered such as a window time which sets a minimum update time,
54 * and this function should return a recommended delay for clearing.
56 * For the STM32 nominal clock rate is 32 khz, but for the maximum clock rate of
57 * 60 khz and a prescalar of 4 yields a clock rate of 15 khz. The delay that is
58 * set in the watchdog assumes the nominal clock rate, but the delay for FreeRTOS
59 * to use is 75% of the minimal delay.
61 * @returns Maximum recommended delay between updates based on PIOS_WATCHDOG_TIMEOUT constant
63 uint16_t PIOS_WDG_Init()
65 uint16_t delay
= ((uint32_t)PIOS_WATCHDOG_TIMEOUT
* 60) / 16;
70 #if defined(PIOS_INCLUDE_WDG)
71 DBGMCU_Config(DBGMCU_IWDG_STOP
, ENABLE
); // make the watchdog stop counting in debug mode
72 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable
);
73 IWDG_SetPrescaler(IWDG_Prescaler_16
);
74 IWDG_SetReload(delay
);
78 // watchdog flags now stored in backup registers
79 PWR_BackupAccessCmd(ENABLE
);
81 BKP_WriteBackupRegister(PIOS_WDG_REGISTER
, 0x0);
82 wdg_configuration
.bootup_flags
= BKP_ReadBackupRegister(PIOS_WDG_REGISTER
);
88 * @brief Register a module against the watchdog
90 * There are two ways to use PIOS WDG: this is for when
91 * multiple modules must be monitored. In this case they
92 * must first register against the watchdog system and
93 * only when all of the modules have been updated with the
94 * watchdog be cleared. Each module must have its own
97 * @param[in] flag the bit this module wants to use
98 * @returns True if that bit is unregistered
100 bool PIOS_WDG_RegisterFlag(uint16_t flag_requested
)
102 // flag are being registered so we are in module initialization phase
103 // clear the WDG to prevent timeout while initializing modules. (OP-815)
106 /* Fail if flag already registered */
107 if (wdg_configuration
.used_flags
& flag_requested
) {
111 // FIXME: Protect with semaphore
112 wdg_configuration
.used_flags
|= flag_requested
;
118 * @brief Function called by modules to indicate they are still running
120 * This function will set this flag in the active flags register (which is
121 * a backup regsiter) and if all the registered flags are set will clear
122 * the watchdog and set only this flag in the backup register
124 * @param[in] flag the flag to set
125 * @return true if the watchdog cleared, false if flags are pending
127 bool PIOS_WDG_UpdateFlag(uint16_t flag
)
129 // we can probably avoid using a semaphore here which will be good for
130 // efficiency and not blocking critical tasks. race condition could
131 // overwrite their flag update, but unlikely to block _all_ of them
132 // for the timeout window
133 uint16_t cur_flags
= BKP_ReadBackupRegister(PIOS_WDG_REGISTER
);
135 if ((cur_flags
| flag
) == wdg_configuration
.used_flags
) {
137 BKP_WriteBackupRegister(PIOS_WDG_REGISTER
, flag
);
140 BKP_WriteBackupRegister(PIOS_WDG_REGISTER
, cur_flags
| flag
);
146 * @brief Returns the flags that were set at bootup
148 * This is used for diagnostics, if only one flag not set this
149 * was likely the module that wasn't running before reset
151 * @return The active flags register from bootup
153 uint16_t PIOS_WDG_GetBootupFlags()
155 return wdg_configuration
.bootup_flags
;
159 * @brief Returns the currently active flags
161 * For external monitoring
163 * @return The active flags register
165 uint16_t PIOS_WDG_GetActiveFlags()
167 return BKP_ReadBackupRegister(PIOS_WDG_REGISTER
);
171 * @brief Clear the watchdog timer
173 * This function must be called at the appropriate delay to prevent a reset event occuring
175 void PIOS_WDG_Clear(void)
177 #if defined(PIOS_INCLUDE_WDG)
178 IWDG_ReloadCounter();
182 #endif /* PIOS_INCLUDE_WDG */