HAL WWDG How to Use ¶
- group WWDG_How_To_Use
-
The system window watchdog (WWDG) is used to detect the occurrence of a software fault, usually generated by external interference or by unforeseen logical conditions, which causes the application program to abandon its normal sequence.
The WWDG clock is prescaled from the APB clock and has a configurable time-window that can be programmed to detect abnormally late or early application behavior.
The WWDG is best suited for applications requiring the watchdog to react within an accurate timing window.
¶
Main features ¶
-
The WWDG can be started by either software or hardware (configurable through option byte).
-
Once enabled the WWDG generates a system reset on expiry of a programmed time period, unless the program refreshes the counter (T[6;0] downcounter) before reaching 0x3F value (i.e. a reset is generated when the counter value rolls down from 0x40 to 0x3F).
-
A 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.
-
Once enabled the WWDG cannot be disabled except by a system reset.
-
If required by application, an Early Wakeup Interrupt can be triggered in order to be warned before WWDG expiration. The Early Wakeup Interrupt (EWI) can be used if specific safety operations or data logging must be performed before the actual reset is generated. When the downcounter reaches 0x40, interrupt occurs. This mechanism requires WWDG interrupt line to be enabled in NVIC. Once enabled, EWI interrupt cannot be disabled except by a system reset.
-
The WWDG counter input clock is derived from the APB clock divided by a programmable prescaler.
-
WWDG clock (Hz) = PCLK1 / (4096 * Prescaler)
-
WWDG timeout (ms) = 1000 * (T[5;0] + 1) / WWDG clock (Hz) where T[5;0] are the lowest 6 bits of Counter.
-
WWDG Counter refresh is allowed between the following limits:
-
min time (ms) = 1000 * (Counter - Window) / WWDG clock (The min time represents the minimum time before refresh is allowed)
-
max time (ms) = 1000 * (Counter - 0x40) / WWDG clock (The max time represents the maximum time before reset)
-
-
Typical values:
-
Counter min (T[5;0] = 0x00) at 56MHz (PCLK1) with one prescaler:
-
WWDG step: approximately 73.14us (The WWDG step represents the WWDG counter period)
-
max timeout before reset: 4.681ms
-
-
Counter max (T[5;0] = 0x3F) at 56MHz (PCLK1) with prescaler dividing by 128:
-
max timeout before reset: approximately 599.18ms
-
-
¶
How to use ¶
The WWDG HAL driver can be used as follows:
-
Enable the WWDG interface clock, if USE_HAL_WWDG_CLK_ENABLE_MODEL = HAL_CLK_ENABLE_NO, otherwise it is enabled in HAL_WWDG_Init() .
-
Configure the allowed refresh period (minimum and maximum time values) and early interrupt status using HAL_WWDG_Start() function. The WWDG is automatically enabled and its downcounter is started.
-
HAL_WWDG_Start() computes and initializes prescaler, reload and window registers to values corresponding to the nearest achievable minimum and maximum times inputs.
-
HAL_WWDG_GetMaxTime() and HAL_WWDG_GetMinTime() functions permits to retrieve the times actually set. HAL_WWDG_GetStep_us() and HAL_WWDG_SetMinTime() permit to tune the refresh time.
-
Care must be taken to provide a maximum time value greater than 0 to prevent generation of immediate reset.
-
If the Early Wakeup Interrupt (EWI) feature is enabled, an interrupt is generated when the counter reaches 0x40. When HAL_WWDG_IRQHandler() is triggered by the interrupt service routine, Early Wakeup flag is automatically cleared and HAL_WWDG_EarlyWakeupCallback() user callback is executed. User can add his own code by customization of callback HAL_WWDG_EarlyWakeupCallback() .
-
-
Then the application program must refresh the WWDG counter at regular intervals during normal operation to prevent an MCU reset, using HAL_WWDG_Refresh() function. This operation must occur only when the counter is lower than the refresh window value already programmed.
Callback registration: ¶
-
The compilation flag USE_HAL_WWDG_REGISTER_CALLBACKS allows the user to configure dynamically the driver callbacks.
-
Use HAL_WWDG_RegisterEarlyWakeupCallback() function to register WWDG Early Wakeup callback.
-
This function takes as parameters the HAL peripheral handle and a pointer to the user callback function.
-
When calling HAL_WWDG_Init() function, callbacks are reset to the corresponding legacy weak function: HAL_WWDG_EarlyWakeupCallback() only if it has not been registered before.
-
When compilation define USE_HAL_WWDG_REGISTER_CALLBACKS is set to 0 or not defined, the callback registering feature is not available and weak callbacks are used.
¶
Allowed Maximum time ranges: ¶
-
The selection of prescaler is done as follows: as long as the requested maximum time value is lower than the maximum time of a time range n, algorithm keeps the same prescaler n, once it exceeds the maximum time of the range n, algorithm switches to the prescaler of the range n+1.
-
The next table describes the possible maximum time ranges for each prescaler at a high frequency (160 MHz):
Note:
-
The theoretical floating point values presented in the following tables are rounded to the nearest integer values as only integers are used as arguments and return values for WWDG APIs.
-
For “Not supported” value in seconds, the user must switch to the milliseconds or microseconds unit. Same, for “Not supported” values in microseconds, the user must switch to the milliseconds or seconds unit.
-
In order to cover all the ranges, time unit static configuration has been introduced and can be expressed in microseconds, milliseconds or second.
Prescaler
Step(us)
Max(us)
Max(ms)
Max(s)
1
25.6
1638.4
1.6384
Not supported
2
51.2
3276.8
3.2768
Not supported
4
102.4
6553.6
6.5536
Not supported
8
204.8
13107.2
13.1072
Not supported
16
409.6
26214.4
26.2144
Not supported
32
819.2
52428.8
52.4288
Not supported
64
1638.4
104857.6
104.8576
Not supported
128
3276.8
209715.2
209.715
Not supported
And the possible maximum time ranges at a low frequency (6kHz), are described in next table:
Prescaler
Step(us)
Max(us)
Max(ms)
Max(s)
1
682666.7
43690666.67
43690.7
43.6907
2
1365333
87381333.33
87381.3
87.3813
4
2730667
174762666.7
174763
174.763
8
5461333
349525333.3
349525
349.525
16
10922667
699050666.7
699051
699.051
32
21845333
1398101333
1398101
1398.1
64
43690667
2796202667
2796203
2796.2
128
87381333
Not supported
5592405
5592.41
-