HAL IWDG How to Use ¶
- group IWDG_How_To_Use
-
¶
Main features ¶
The IWDG can be started by either software or hardware (configurable through option byte).
Note: If the user has chosen to start the IWDG in hardware mode, set the USE_HAL_IWDG_HARDWARE_START directive to enable the APIs associated with hardware mode.
The IWDG is clocked by the Low-Speed Internal clock (LSI) and thus stays active even if the main clock fails.
Once the IWDG is started, the LSI is forced ON and neither the IWDG nor the LSI can be disabled except by a system reset.
Once enabled, the IWDG generates a system reset on expiration of a programmed time period, unless the program refreshes the downcounter before reaching 0x000 value (i.e. a reset is generated when the counter value rolls down from 0x001 to 0x000).
An MCU reset is also generated if the counter value is refreshed before the counter has reached the refresh window value. This implies that the counter must be refreshed in a limited window.
If required by the application, configure an Early Wakeup Interrupt time to receive an interrupt before IWDG expiration. Use the Early Wakeup Interrupt (EWI) if specific safety operations or data logging must be performed before the actual reset is generated. Enable the IWDG interrupt line in the NVIC. Once enabled, the EWI interrupt cannot be disabled except by a system reset.
The IWDG is implemented in the VDD voltage domain, which remains functional in STOP and STANDBY modes (an IWDG reset can wake up the CPU from STANDBY).
The IWDG counter input clock is derived from LSI clock divided by a programmable prescaler.
IWDG clock (Hz) = LSI_clock / (4 * Prescaler)
IWDG timeout (ms) = 1000 * (RL[11:0]) / IWDG clock (Hz) where RL[11:0] is the counter reload value.
IWDG Counter refresh is allowed between the following limits:
min time (ms) = 1000 * (Counter - Window) / IWDG clock (The min time represents the minimum time before refresh is allowed)
max time (ms) = 1000 * (Counter) / IWDG clock (The max time represents the maximum time before reset)
Typical values @32kHz (LSI)
Step range: [125us ; 8ms] (The IWDG step represents the IWDG counter period)
Timeout range (with RL[11:0] in [2 ; 4096]): [250us ; ~131s]
LSI management
The IWDG timeout might vary due to LSI clock frequency dispersion. STM32C5xx devices provide the capability to measure the LSI clock frequency (LSI clock is internally connected to TIM16 CH1 input capture). Use the measured value to achieve an IWDG timeout with acceptable accuracy.
Default: Constant LSI_VALUE is defined based on the nominal LSI clock frequency. As this frequency is subject to variations as mentioned above, the default timeout has been specifically adjusted to accommodate the LSI startup time.
The IWDG HAL driver allows calculation of a custom LSI frequency value for later use.
Debug mode: When the microcontroller enters debug mode (core halted), the IWDG counter either continues to work normally or stops, depending on the DBG_IWDG_STOP configuration bit in the DBG module. Refer to the DBGMCU module services to freeze or unfreeze the IWDG during system low power modes.
¶
How to use ¶
Use the IWDG HAL driver as follows:
Select the LSI frequency by setting USE_HAL_IWDG_LSI_FREQ. The choice is either static or dynamic depending on this define.
Configure the allowed refresh period (minimum and maximum time values) and early interrupt time using HAL_IWDG_Start() function. The IWDG is automatically enabled and its downcounter is started.
HAL_IWDG_Start() computes and initializes prescaler, reload, window and early wake-up registers to values corresponding to the nearest achievable minimum, maximum and early interrupt times inputs.
Call HAL_IWDG_GetMaxTime(), HAL_IWDG_GetMinTime(), and HAL_IWDG_GetEarlyWakeupInterruptTime() to retrieve the times actually set.
Call HAL_IWDG_GetStep_us() and HAL_IWDG_SetMinTime() to tune the refresh time.
Call HAL_IWDG_SetEarlyWakeupInterruptTime() to tune the early interrupt time.
Provide a maximum time value greater than 0 to prevent generation of an immediate reset.
If the Early Wakeup Interrupt (EWI) feature is enabled (early interrupt time not equal to 0), an interrupt is generated when the early wakeup time is reached. When HAL_IWDG_IRQHandler() is triggered by the interrupt service routine, Early Wakeup flag is automatically cleared and HAL_IWDG_EarlyWakeupCallback() callback is executed. Add user code by customizing the HAL_IWDG_EarlyWakeupCallback().
After IWDG first initialization, call HAL_IWDG_SetLSIFrequency() to set a more accurate LSI value. Call HAL_IWDG_Start() again to re-configure the IWDG. Call HAL_IWDG_GetLSIFrequency() to retrieve the LSI value used by the IWDG driver.
Refresh the IWDG counter at regular intervals during normal operation to prevent an MCU reset, using HAL_IWDG_Refresh() function.
Callback registration: ¶
The compilation flag USE_HAL_IWDG_REGISTER_CALLBACKS allows dynamic configuration of the driver callbacks.
Use HAL_IWDG_RegisterEarlyWakeupCallback() function to register IWDG Early Wakeup callback.
This function takes as parameters the HAL peripheral handle and a pointer to the user callback function.