19. Window watchdog (WWDG)

19.1 WWDG introduction

The window watchdog 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 downcounter before the T6 bit becomes cleared. An MCU reset is also generated if the 7-bit downcounter value (in the control register) is refreshed before the downcounter has reached the window register value. This implies that the counter must be refreshed in a limited window.

19.2 WWDG main features

19.3 WWDG functional description

If the watchdog is activated (the WDGA bit is set in the WWDG_CR register) and when the 7-bit downcounter (T[6:0] bits) rolls over 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.

Figure 173. Watchdog block diagram

Figure 173. Watchdog block diagram. The diagram shows the internal logic of the WWDG. A 7-bit downcounter (CNT) is clocked by PCLK1 (from RCC clock controller) through a WDG prescaler (WDGTB) with a /4096 divider. The counter value is compared with the Watchdog configuration register (WWDG_CFR) which contains bits W6:0. The comparator output is 1 when T6:0 > W6:0. This output is ANDed with the 'Write WWDG_CR' signal. The result is ORed with the RESET signal. The Watchdog control register (WWDG_CR) contains bits WDGA, T6, T5, T4, T3, T2, T1, T0. The T6 bit is used to prevent an immediate reset. The diagram also shows the MSV37226V2 identifier.

The diagram illustrates the internal architecture of the Window Watchdog (WWDG). At the top, the Watchdog configuration register (WWDG_CFR) is shown with bits W6, W5, W4, W3, W2, W1, W0 . Below it, the Watchdog control register (WWDG_CR) is shown with bits WDGA, T6, T5, T4, T3, T2, T1, T0 . A 7-bit downcounter (CNT) is connected to the WWDG_CR register. The counter is clocked by PCLK1 (from RCC clock controller) through a WDG prescaler (WDGTB) with a /4096 divider. A comparator compares the counter value (T6:0) with the WWDG_CFR value (W6:0). The comparator output is 1 when T6:0 > W6:0 . This output is ANDed with the Write WWDG_CR signal. The result is ORed with the RESET signal. The diagram also shows the MSV37226V2 identifier.

Figure 173. Watchdog block diagram. The diagram shows the internal logic of the WWDG. A 7-bit downcounter (CNT) is clocked by PCLK1 (from RCC clock controller) through a WDG prescaler (WDGTB) with a /4096 divider. The counter value is compared with the Watchdog configuration register (WWDG_CFR) which contains bits W6:0. The comparator output is 1 when T6:0 > W6:0. This output is ANDed with the 'Write WWDG_CR' signal. The result is ORed with the RESET signal. The Watchdog control register (WWDG_CR) contains bits WDGA, T6, T5, T4, T3, T2, T1, T0. The T6 bit is used to prevent an immediate reset. The diagram also shows the MSV37226V2 identifier.

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. The value to be stored in the WWDG_CR register must be between 0xFF and 0xC0.

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.

Controlling the downcounter

This downcounter 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 which represents 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 174 ). The Configuration register (WWDG_CFR) contains the high limit of the window: To prevent a reset, the downcounter must be reloaded when its value is lower than the window register value and greater than 0x3F. Figure 174 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).

Advanced watchdog interrupt feature

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 downcounter 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 interrupt service routine (ISR) should 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 will eventually be generated.

19.4 How to program the watchdog timeout

The formula in Figure 174 must be used 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 174. Window watchdog timing diagram

Timing diagram showing the relationship between the T[6:0] CNT downcounter, the window register W[6:0], the T6 bit, and the RESET signal over time. The counter starts at 0x3F and decreases. The window register is set to a value W[6:0]. The diagram is divided into 'Refresh not allowed' and 'Refresh allowed' periods. The T6 bit is shown as a high signal, and the RESET signal is shown as a low pulse when the counter reaches 0x00.

The diagram illustrates the timing of the Window Watchdog (WWDG). The top trace shows the T[6:0] CNT downcounter starting at 0x3F and decreasing linearly over time. The W[6:0] register is set to a value W[6:0]. The counter value is compared with the window register value. The diagram is divided into two regions: 'Refresh not allowed' (where the counter is below the window value) and 'Refresh allowed' (where the counter is above the window value). The T6 bit is shown as a high signal. The RESET signal is shown as a low pulse when the counter reaches 0x00.

Timing diagram showing the relationship between the T[6:0] CNT downcounter, the window register W[6:0], the T6 bit, and the RESET signal over time. The counter starts at 0x3F and decreases. The window register is set to a value W[6:0]. The diagram is divided into 'Refresh not allowed' and 'Refresh allowed' periods. The T6 bit is shown as a high signal, and the RESET signal is shown as a low pulse when the counter reaches 0x00.

The formula to calculate the timeout value is given by:

\[ t_{WWDG} = t_{PCLK1} \times 4096 \times 2^{WDGTB[1:0]} \times (T5:0] + 1) \quad (\text{ms}) \]

where:

As an example, let us assume APB1 frequency is equal to 24 MHz, WDGTB[1:0] is set to 3 and T[5:0] is set to 63:

\[ t_{\text{WWDG}} = 1 / 24000 \times 4096 \times 2^3 \times (63 + 1) = 21.85 \text{ ms} \]

Refer to the datasheets for the minimum and maximum values of the \( t_{\text{WWDG}} \) .

19.5 Debug mode

When the microcontroller enters debug mode (Cortex ® -M4 with FPU core halted), the WWDG counter either continues to work normally or stops, depending on DBG_WWDG_STOP configuration bit in DBGMCU module. For more details, refer to Section 26.16.4: Debug MCU APB1 freeze register (DBGMCU_APB1_FZ) .

19.6 WWDG registers

Refer to Section 1.2 on page 35 for a list of abbreviations used in register descriptions.

The peripheral registers have to be accessed by half-words (16 bits) or words (32 bits).

19.6.1 Control register (WWDG_CR)

Address offset: 0x00

Reset value: 0x0000 007F

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.WDGAT[6:0]
rsrw

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. It is decremented every \( (4096 \times 2^{\text{WDGTB}[1:0]}) \) PCLK1 cycles. A reset is produced when it rolls over from 0x40 to 0x3F (T6 becomes cleared).

19.6.2 Configuration register (WWDG_CFR)

Address offset: 0x04

Reset value: 0x0000 007F

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.Res.Res.Res.EWIWDGTB[1:0]W[6:0]
rsrwrw

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 (PCLK1 div 4096) div 1

01: CK Counter Clock (PCLK1 div 4096) div 2

10: CK Counter Clock (PCLK1 div 4096) div 4

11: CK Counter Clock (PCLK1 div 4096) div 8

Bits 6:0 W[6:0] : 7-bit window value

These bits contain the window value to be compared to the downcounter.

19.6.3 Status register (WWDG_SR)

Address offset: 0x08

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
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'. A write of '1' has no effect. This bit is also set if the interrupt is not enabled.

19.6.4 WWDG register map

The following table gives the WWDG register map and reset values.

Table 79. WWDG register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00WWDG_CRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.WDGAT[6:0]
Reset value01111111
0x04WWDG_CFRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.EWIWDGTB1WDGTB0W[6:0]
Reset value000111111
0x08WWDG_SRRes.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 value0

Refer to Section 2.2 on page 39 for the register boundary addresses.