21. System window watchdog (WWDG)
21.1 Introduction
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 watchdog circuit generates an MCU reset on expiry of a programmed time period, unless the program refreshes the contents of the down-counter before the T6 bit becomes cleared. An MCU reset is also generated if the 7-bit down-counter value (in the control register) is refreshed before the down-counter has reached the window register value. This implies that the counter must be refreshed in a limited window.
The WWDG clock is prescaled from the APB1 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 which require the watchdog to react within an accurate timing window.
21.2 WWDG main features
- • Programmable free-running down-counter
- • Conditional reset
- – Reset (if watchdog activated) when the down-counter value becomes lower than 0x40
- – Reset (if watchdog activated) if the down-counter is reloaded outside the window (see Figure 180 )
- • Early wakeup interrupt (EWI): triggered (if enabled and the watchdog activated) when the down-counter is equal to 0x40.
21.3 WWDG functional description
If the watchdog is activated (the WDGA bit is set in the WWDG_CR register) and when the 7-bit down-counter (T[6:0] bits) is decremented from 0x40 to 0x3F (T6 becomes cleared), it initiates a reset. If the software reloads the counter while the counter is greater than the value stored in the window register, then a reset is generated.
The application program must write in the WWDG_CR register at regular intervals during normal operation to prevent an MCU reset. This operation must occur only when the counter value is lower than the window register value and higher than 0x3F. The value to be stored in the WWDG_CR register must be between 0xFF and 0xC0.
Refer to Figure 179 for the WWDG block diagram.
21.3.1 WWDG block diagram
Figure 179. Watchdog block diagram
![Figure 179. Watchdog block diagram. The diagram shows the internal architecture of the WWDG. On the left, an APB bus connects to a Register interface containing WWDG_CFR, WWDG_SR, and WWDG_CR registers. The WWDG_CFR register outputs W[6:0] to a CMP block. The WWDG_CR register has a readback of T[6:0] and a preload input to a 7-bit DownCounter (CNT). The CNT is clocked by pclk divided by 4096 and then by 2^WDGTB. The CNT outputs cnt_out to the CMP block. The CMP block outputs CMP = 1 when T[6:0] > W[6:0]. This output is ANDed with the T6 bit from the WWDG_CR register. The result is ORed with the WDGA bit from the WWDG_CR register to produce the wwdg_out_rst output. The CNT also outputs to a Logic block along with EWI and EWIF signals to produce the wwdg_it output. A write to WWDG_CR is also shown.](/RM0377-STM32L0x1/22f129958642affe06cd9285c2a16dde_img.jpg)
21.3.2 Enabling the watchdog
The watchdog is always disabled after a reset. It is enabled by setting the WDGA bit in the WWDG_CR register, then it cannot be disabled again except by a reset.
21.3.3 Controlling the down-counter
This down-counter is free-running, counting down even if the watchdog is disabled. When the watchdog is enabled, the T6 bit must be set to prevent generating an immediate reset.
The T[5:0] bits contain the number of increments that represent the time delay before the watchdog produces a reset. The timing varies between a minimum and a maximum value due to the unknown status of the prescaler when writing to the WWDG_CR register (see Figure 180 ). The WWDG configuration register (WWDG_CFR) contains the high limit of the window: to prevent a reset, the down-counter must be reloaded when its value is lower than the window register value and greater than 0x3F. Figure 180 describes the window watchdog process.
Note: The T6 bit can be used to generate a software reset (the WDGA bit is set and the T6 bit is cleared).
21.3.4 How to program the watchdog timeout
Use the formula in Figure 180 to calculate the WWDG timeout.
Warning: When writing to the WWDG_CR register, always write 1 in the T6 bit to avoid generating an immediate reset.
Figure 180. Window watchdog timing diagram
![Figure 180. Window watchdog timing diagram. The diagram shows the timing relationship between the CNT DownCounter, WWDG signals, and the T6 bit. The CNT DownCounter (T[6:0]) starts at 0x3F and counts down. The WWDG window (W[6:0]) is shown as a line that starts at 0x3F and decreases. The diagram is divided into two regions: 'Refresh not allowed' and 'Refresh allowed'. The 'Refresh not allowed' region ends when the counter reaches the window value. The 'Refresh allowed' region ends when the counter reaches 0x3F. A zoomed-in view shows the counter values 0x41, 0x40, and 0x3F. The wwdg_ewit signal is shown as a pulse that goes high when the counter reaches 0x3F. The wwdg_rst signal is shown as a pulse that goes high when the counter reaches 0x3F. The T6 bit is shown as a pulse that goes high when the counter reaches 0x3F. The formula for the timeout value is given as t_WWDG = t_PCLK1 x 4096 x 2^WDGTB[1:0] x (T[5:0] + 1) (ms).](/RM0377-STM32L0x1/23e6531954db1e8cb51490d1878b992c_img.jpg)
The diagram illustrates the timing of the Window Watchdog (WWDG) system. The top section shows the CNT DownCounter (T[6:0]) and the WWDG window (W[6:0]) over time. The counter starts at 0x3F and counts down. The window is a line that starts at 0x3F and decreases. The diagram is divided into two regions: 'Refresh not allowed' and 'Refresh allowed'. The 'Refresh not allowed' region ends when the counter reaches the window value. The 'Refresh allowed' region ends when the counter reaches 0x3F. A zoomed-in view shows the counter values 0x41, 0x40, and 0x3F. The wwdg_ewit signal is shown as a pulse that goes high when the counter reaches 0x3F. The wwdg_rst signal is shown as a pulse that goes high when the counter reaches 0x3F. The T6 bit is shown as a pulse that goes high when the counter reaches 0x3F. The formula for the timeout value is given as \( t_{WWDG} = t_{PCLK1} \times 4096 \times 2^{WDGTB[1:0]} \times (T[5:0] + 1) \) (ms).
The formula to calculate the timeout value is given by:
where:
\( t_{WWDG} \) : WWDG timeout
\( t_{PCLK1} \) : APB1 clock period measured in ms
4096: value corresponding to internal divider
As an example, if APB1 frequency is 32 MHz, WDGTB[1:0] is set to 3 and T[5:0] is set to 63:
Refer to the datasheet for the minimum and maximum values of \( t_{WWDG} \) .
For code example, refer to A.12.1: WWDG configuration code example .
21.3.5 Debug mode
When the device enters debug mode (processor halted), the WWDG counter either continues to work normally or stops, depending on the configuration bit in DBG module. For more details refer to Section 27.9.2: Debug support for timers, watchdog and I 2 C .
21.4 WWDG interrupts
The early wakeup interrupt (EWI) can be used if specific safety operations or data logging must be performed before the actual reset is generated. The EWI interrupt is enabled by setting the EWI bit in the WWDG_CFR register. When the down-counter reaches the value 0x40, an EWI interrupt is generated and the corresponding interrupt service routine (ISR) can be used to trigger specific actions (such as communications or data logging) before resetting the device.
In some applications the EWI interrupt can be used to manage a software system check and/or system recovery/graceful degradation, without generating a WWDG reset. In this case the corresponding ISR has to reload the WWDG counter to avoid the WWDG reset, then trigger the required actions.
The EWI interrupt is cleared by writing '0' to the EWIF bit in the WWDG_SR register.
Note: When the EWI interrupt cannot be served (e.g. due to a system lock in a higher priority task) the WWDG reset is eventually generated.
21.5 WWDG registers
Refer to Section 1.2 on page 45 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by halfwords (16-bit) or words (32-bit).
21.5.1 WWDG control register (WWDG_CR)
Address offset: 0x000
Reset value: 0x0000 007F
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | WDGA | T[6:0] | ||||||
| rs | rw | rw | rw | rw | rw | rw | rw | ||||||||
Bits 31:8 Reserved, must be kept at reset value.
Bit 7 WDGA : Activation bit
This bit is set by software and only cleared by hardware after a reset. When WDGA = 1, the watchdog can generate a reset.
0: Watchdog disabled
1: Watchdog enabled
Bits 6:0 T[6:0] : 7-bit counter (MSB to LSB)
These bits contain the value of the watchdog counter, decremented every \( (4096 \times 2^{\text{WDGTB}[1:0]}) \) PCLK cycles. A reset is produced when it is decremented from 0x40 to 0x3F (T6 becomes cleared).
21.5.2 WWDG configuration register (WWDG_CFR)
Address offset: 0x004
Reset value: 0x0000 007F
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | EWI | WDGTB[1:0] | W[6:0] | |||||||
| rs | rw | rw | |||||||||||||
Bits 31:10 Reserved, must be kept at reset value.
Bit 9 EWI : Early wakeup interrupt
When set, an interrupt occurs whenever the counter reaches the value 0x40. This interrupt is only cleared by hardware after a reset.
Bits 8:7 WDGTB[1:0] : Timer base
The time base of the prescaler can be modified as follows:
00: CK counter clock (PCLK div 4096) div 1
01: CK counter clock (PCLK div 4096) div 2
10: CK counter clock (PCLK div 4096) div 4
11: CK counter clock (PCLK div 4096) div 8
Bits 6:0 W[6:0] : 7-bit window value
These bits contain the window value to be compared with the down-counter.
21.5.3 WWDG status register (WWDG_SR)
Address offset: 0x008
Reset value: 0x0000 0000
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. |
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | EWIF |
| rc_w0 |
Bits 31:1 Reserved, must be kept at reset value.
Bit 0 EWIF : Early wakeup interrupt flag
This bit is set by hardware when the counter has reached the value 0x40. It must be cleared by software by writing 0. Writing 1 has no effect. This bit is also set if the interrupt is not enabled.
21.5.4 WWDG register map
The following table gives the WWDG register map and reset values.
Table 93. WWDG register map and reset values
| Offset | Register | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x000 | WWDG_CR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | WDGA | T[6:0] | ||||||
| Reset value | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | |||||||||||||||||||||||||
| 0x004 | WWDG_CFR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | EWI | WDGTB1 | W[6:0] | |||||||
| Reset value | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | ||||||||||||||||||||||||
| 0x008 | WWDG_SR | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | Res. | EWIF |
| Reset value | 0 | ||||||||||||||||||||||||||||||||
Refer to Section 2.2 on page 51 for the register boundary addresses.