The idea: a counter that resets the chip unless the firmware keeps it going
A device in the field has no one to press its reset button. If its program gets stuck, for example waiting for a sensor that never answers, it stays stuck until the power is cut. A watchdog timer fixes that in hardware. It is a down-counter with its own clock. When it reaches its end value, it resets the whole chip. The program prevents this by writing a special value to the watchdog now and then, which loads the counter again. That write is called kicking (or feeding, or refreshing) the watchdog. A program that works keeps kicking. A program that has hung stops, and the reset brings the device back.
The page uses the two watchdogs of an STM32F4 and its real register names. Clock assumptions: the LSI oscillator runs at 32 kHz (its nominal value on the F4; F1 parts use 40 kHz), and the APB1 bus clock PCLK1 is 16 MHz (the internal HSI oscillator, the reset default).
The independent watchdog: IWDG_KR, IWDG_PR, IWDG_RLR
The IWDG is called independent because it runs from the LSI, a small RC oscillator of its own. It keeps counting even if the main clock fails. Three registers set it up:
| register | what it does |
|---|---|
IWDG_KR | the key register: 0xCCCC starts the watchdog, 0x5555 unlocks PR and RLR for writing, 0xAAAA loads the counter with RLR (the kick) |
IWDG_PR | prescaler: 0 = ÷4, 1 = ÷8, 2 = ÷16, 3 = ÷32, 4 = ÷64, 5 = ÷128, 6 = ÷256 |
IWDG_RLR | reload value, 12 bits (0 to 0xFFF = 4095) |
Magic values instead of a plain enable bit protect the watchdog from a program that has gone wrong: a random write is very unlikely to be exactly 0xAAAA, and a stray write cannot change the timeout unless 0x5555 came first. Once started with 0xCCCC, the IWDG cannot be stopped by software. Only a reset stops it.
One count takes prescaler / fLSI, so the timeout is prescaler × RLR / 32 000 Hz. With PR = 3 (÷32) the counter ticks at 1 kHz and RLR = 1000 gives 1000 ms (Demo: boot, then the loop kicks every 200 ms: the counter is down to 800 before each of the 5 kicks at 200, 400, 600, 800 and 1000 ms, far from 0).
PR | divider | one count | timeout, RLR = 1000 | longest, RLR = 0xFFF |
|---|---|---|---|---|
| 0 | ÷4 | 0.125 ms | 125 ms | about 0.5 s |
| 3 | ÷32 | 1 ms | 1000 ms | about 4.1 s |
| 6 | ÷256 | 8 ms | 8 s | about 32.8 s |
Pick the wrong prescaler and the dog bites a working program. With PR = 0 the same RLR = 1000 lasts only 125 ms, but the loop kicks every 200 ms. The chip resets at 125, 250 and 375 ms and never reaches its first kick: a boot loop (Demo: PR ÷4 makes the timeout 125 ms).
A hang, a reset, and RCC_CSR on the way back up
In Demo: Sensor_Read hangs, the watchdog resets the chip the sensor stops answering. Sensor_Read() starts at 610 ms and spins in while (!(I2C1->SR1 & I2C_SR1_RXNE)); forever, because that loop has no timeout. The last kick was at 600 ms, so the counter reaches 0 at 1600 ms and the IWDG resets the chip. A reset restarts the CPU and every peripheral, including the stuck I2C controller (how a bus gets stuck is on the I2C bus). After the reboot the loop runs again and kicks at 1800 ms.
A watchdog reset should never pass silently. The reset and clock controller records why the chip restarted in RCC_CSR: IWDGRSTF for the independent watchdog, WWDGRSTF for the window watchdog, PORRSTF for power-on, PINRSTF for the reset pin (it is also set by every internal reset, because the chip drives its own NRST pin). The first lines of main() read the flags, log a watchdog reset (count it, store it in flash or a backup register, report it), then clear them by writing RMVF, so the next boot sees only its own cause. Unlike the exceptions on MIPS exceptions or Cortex-M exceptions, nothing returns: the CPU starts again from the reset vector, as on MCU start-up and the memory map.
If the fault is permanent, the watchdog turns a hang into a reset every second. Firmware that counts its watchdog resets can stop after a few and fall back to a safe mode instead of rebooting forever.
Where the kick goes
The kick must mean "the work got done". A tempting shortcut is to kick from a timer interrupt, because it always runs on time. That is the classic mistake. In Demo: kicking from a timer ISR hides the hang ✗, TIM2_IRQHandler writes 0xAAAA every 100 ms. The main loop hangs at 610 ms, but the interrupt keeps firing (an interrupt does not care that the loop is stuck; see interrupt priorities and nesting). The counter never drops below 900. At 3000 ms the loop has been stuck for 2390 ms and there were 0 resets: the device looks alive and does nothing.
The fix is a check-in. The loop sets alive = 1 at the end of each pass, after the real work. The interrupt kicks only if alive is set, and clears it. In Demo: the fix, the ISR kicks only after the loop checks in, the kicks come at 250, 450 and 650 ms. Then the loop is stuck, every interrupt sees alive = 0, and the IWDG resets the chip at 1650 ms. With several tasks, each one sets its own bit and the kick happens only when all bits are set; that fits a time-triggered scheduler, a cyclic executive or an RTOS (program shapes are compared on embedded software architectures).
The window watchdog: too late and too early
The WWDG runs from PCLK1 ÷ 4096 ÷ 2WDGTB. With WDGTB = 2 one count takes 4096 × 4 / 16 MHz = 1.024 ms. Its 7-bit counter T in WWDG_CR starts at 0x7F (127). It resets the chip when bit 6 clears, that is when T goes from 0x40 to 0x3F: 64 counts, 65.536 ms after a kick. It also resets the chip if the program kicks while T is still above the window value W in WWDG_CFR. With W = 0x50 (80) a kick is allowed only from 47 counts on, 48.128 ms after the last one.
- Demo: window watchdog, every kick inside the window: the loop kicks every 50 ms, each time with
T= 0x4F (79) ≤ 0x50. No reset. - Demo: a kick that comes too early resets the chip: a loop that kicks every 30 ms finds
T= 0x62 (98) > 0x50, and the WWDG resets the chip at 30 ms, and again at 60 ms after the reboot. - Demo: a loop pass that is too slow misses the window: a 70 ms pass never gets to kick; the counter reaches 0x3F at 65.536 ms.
Why would kicking early be wrong? A loop that suddenly runs much faster than designed is skipping work, or a bug has put the kick inside a tight loop. The window catches both. The price is that the window watchdog needs a loop with a known, steady period.
| IWDG | WWDG | |
|---|---|---|
| clock | LSI, about 32 kHz, its own | PCLK1 ÷ 4096 ÷ 2WDGTB |
| resets when | counter reaches 0 | T goes 0x40 → 0x3F, or a kick while T > W |
| kick | IWDG_KR = 0xAAAA | WWDG_CR = T (e.g. 0x7F) |
| catches | a hang, a stopped main clock | a hang, and timing that is too fast or too slow |
| timeouts | 0.125 ms to about 32 s | milliseconds to about a hundred milliseconds |
Choosing the timeout
- The timeout must be longer than the longest normal gap between kicks, including slow paths such as a flash erase, with a margin. The LSI is only accurate to tens of percent: a nominal 1 s timeout is about 680 ms if the LSI runs at 47 kHz. A 200 ms kick period stays far inside it.
- It must be short enough that the device recovers before harm is done: a motor controller cannot wait 30 s, a data logger can.
- Put the kick in one place, at the end of the work, never inside a delay or a wait loop. A control loop that reads an ADC and sets a PWM duty (ADC and PWM) is exactly the work it should prove.
See also the cyclic executive, where a job that runs past its frame causes a frame overrun that the executive must detect and handle.
See also MCU startup from power-on to main(), where a warm reset keeps SRAM and hides a missing .data copy.
What the page leaves out
Boot takes no time here, and the IWDG write sequence is simplified: on the real chip, writes to PR and RLR take a few LSI cycles to land (the PVU and RVU bits of IWDG_SR), and the reference manual counts RLR + 1 periods, so RLR = 1000 gives 1.001 s. Also left out: the hardware option bit that starts the IWDG at reset with no code at all, the debug freeze bits (DBGMCU_APB1_FZ) that stop both watchdogs at a breakpoint, the WWDG early wakeup interrupt (EWI, at T = 0x40) that gives a last chance to save state, the IWDG window register of newer STM32 families, HAL calls such as HAL_IWDG_Refresh, the real LSI drift, and external watchdog chips.