HAL Library 17- IWDG for STM32Fxxx
Sometimes you need watchdog timer to look at your system if it gets stuck. I made a little library to work with it and to be compatible with HAL based drivers.
IWDG timer in STM32Fxxx device has its 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.
Library
Features
- Detects if system was reset by watchdog
- Supports different timeouts
- Resets watchdog
Dependencies
- HAL
- TM
- STm32Fxxx HAL
- defines.h
Functions and enumerations
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/** * @defgroup TM_IWDG_Typedefs * @brief Library Typedefs * @{ */ /** * @brief Watchdog timeout settings */ typedef enum { TM_IWDG_Timeout_5ms = 0x00, /*!< System reset called every 5ms */ TM_IWDG_Timeout_10ms = 0x01, /*!< System reset called every 10ms */ TM_IWDG_Timeout_15ms = 0x02, /*!< System reset called every 15ms */ TM_IWDG_Timeout_30ms = 0x03, /*!< System reset called every 30ms */ TM_IWDG_Timeout_60ms = 0x04, /*!< System reset called every 60ms */ TM_IWDG_Timeout_120ms = 0x05, /*!< System reset called every 120ms */ TM_IWDG_Timeout_250ms = 0x06, /*!< System reset called every 250ms */ TM_IWDG_Timeout_500ms = 0x07, /*!< System reset called every 500ms */ TM_IWDG_Timeout_1s = 0x08, /*!< System reset called every 1s */ TM_IWDG_Timeout_2s = 0x09, /*!< System reset called every 2s */ TM_IWDG_Timeout_4s = 0x0A, /*!< System reset called every 4s */ TM_IWDG_Timeout_8s = 0x0B, /*!< System reset called every 8s */ TM_IWDG_Timeout_16s = 0x0C, /*!< System reset called every 16s */ TM_IWDG_Timeout_32s = 0x0D /*!< System reset called every 32s. This is maximum value allowed with IWDG timer */ } TM_IWDG_Timeout_t; /** * @} */ /** * @defgroup TM_IWDG_Functions * @brief Library Functions * @{ */ /** * @brief Initialize Watchdog timer * @note After you initialize it, you can't disable it unless reset occur. * @param timeout. Timeout value when watchdog reset happen if not reset by user. * This parameter can be a value of @ref TM_IWDG_Timeout_t enumeration * @retval Value if system was reset because of watchdog timer * - 1: Reset happen because of watchdog * - 0: Otherwise */ uint8_t TM_IWDG_Init(TM_IWDG_Timeout_t timeout); /** * @brief Reset IWDG timer * @note This function have to be called periodically to reset watchdog timer, or your system will reset * @param None * @retval None * @note Defined as macro for faster execution */ #define TM_IWDG_Reset() (IWDG->KR = 0xAAAA) /** * @} */ |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * Keil project example for IWDG - Independent Watchdog timer * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_iwdg.h" int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Check if system reset because of IWDG */ if (TM_IWDG_Init(TM_IWDG_Timeout_4s)) { /* System reset was done because of IWDG timer */ TM_DISCO_LedOn(LED_RED); } else { /* No IWDG */ TM_DISCO_LedOn(LED_GREEN); } /* If there is no LED active (F7-Discovery, Nucleo boards), */ /* then system reset occurred because of IWDG */ while (1) { /* Check for button */ if (TM_DISCO_ButtonPressed()) { /* Wait till pressed */ /* If pressed more than 4 seconds in a row, system will reset because of IWDG timer */ while (TM_DISCO_ButtonPressed()); } /* Reset watchdog */ TM_IWDG_Reset(); } } |
Project is available on Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments