Library 20- Independent watchdog timer on STM32F4
Sometimes you need watchdog timer to look at your system if it gets stuck. I made a little library to work with it.
Watchdog in STM32F4xx device has it’s own clock which is independent from main system clock. You have to constantly reset it’s counter value or it will elapse and reset MCU.
Watchdog Library
Features
- Detect if system was reset by watchdog
- Supports different timeouts
- Reset watchdog
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx IWDG
- TM
- defines.h
- defines.h
Timeout structure
I made a structure with different timings for watchdog reset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * Watchdog timeout settings */ typedef enum { TM_WATCHDOG_Timeout_5ms, //System reset called every 5ms TM_WATCHDOG_Timeout_10ms, //System reset called every 10ms TM_WATCHDOG_Timeout_15ms, //System reset called every 15ms TM_WATCHDOG_Timeout_30ms, //System reset called every 30ms TM_WATCHDOG_Timeout_60ms, //System reset called every 60ms TM_WATCHDOG_Timeout_120ms, //System reset called every 120ms TM_WATCHDOG_Timeout_250ms, //System reset called every 250ms TM_WATCHDOG_Timeout_500ms, //System reset called every 500ms TM_WATCHDOG_Timeout_1s, //System reset called every 1s TM_WATCHDOG_Timeout_2s, //System reset called every 2s TM_WATCHDOG_Timeout_4s //System reset called every 4s } TM_WATCHDOG_Timeout_t; |
Initialize watchdog timer
Before you can use it, you have to initialize it. Function below initializes watchdog timer.
As parameter you have to pass one of members of watchdog structure.
This function returns 1 if system was reset by watchdog, otherwise 0.
1 2 3 4 5 6 7 8 9 10 |
/** * Initialize Watchdog timer * * Parameters: * - TM_WATCHDOG_Timeout_t timeout: * Select time when watchdog reset accur if Watchdog timer is not reset before * * Returns 1 if system was reset by Watchdog, otherwise 0 */ uint8_t TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_t timeout); |
Reset watchdog timer
You want system reset by watchdog only if system stuck somewhere. So you don’t want that watchdog reset your system all the time. For this, you have to reset timer’s counter periodically faster than he can count to it’s top value.
Reset timer with
1 2 3 4 5 6 |
/** * Reset Watchdog timer * * */ void TM_WATCHDOG_Reset(void); |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/** * Keil project * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ #include "defines.h" #include "stm32f4xx.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_watchdog.h" int main(void) { //Initialize system SystemInit(); //Initialize delay TM_DELAY_Init(); //Initialize leds on board TM_DISCO_LedInit(); //Initialize button TM_DISCO_ButtonInit(); //Initialize watchdog timer //Set timeout to 1s if (TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_1s)) { //System was reset by watchdog TM_DISCO_LedOn(LED_RED); } else { //System was not reset by watchdog TM_DISCO_LedOn(LED_GREEN); } while (1) { //if button is pressed, do nothing and system will be reset after 1 second while (TM_DISCO_ButtonPressed()); //Reset watchdog TM_WATCHDOG_Reset(); } } |
Project available onĀ Github, download library below.
Internal Watchdog on STM32F4xx
Recent comments