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 32 kHz LSI clock goes through a divide-by-32 prescaler set by IWDG_PR, giving 1 kHz, into a 12-bit down-counter loaded from IWDG_RLR = 1000. When the counter reaches 0 it drives the chip's reset line. The main loop (Sensor_Read, Control_Update, kick) writes 0xAAAA to IWDG_KR at the end of every pass, which loads the counter with RLR again
The counter only ever goes down; each kick from a working loop puts it back to 1000, and a loop that stops kicking lets it reach 0 and reset the chip.

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:

registerwhat it does
IWDG_KRthe key register: 0xCCCC starts the watchdog, 0x5555 unlocks PR and RLR for writing, 0xAAAA loads the counter with RLR (the kick)
IWDG_PRprescaler: 0 = ÷4, 1 = ÷8, 2 = ÷16, 3 = ÷32, 4 = ÷64, 5 = ÷128, 6 = ÷256
IWDG_RLRreload 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).

PRdividerone counttimeout, RLR = 1000longest, RLR = 0xFFF
0÷40.125 ms125 msabout 0.5 s
3÷321 ms1000 msabout 4.1 s
6÷2568 ms8 sabout 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).

Two timelines from 0 to 2000 ms with the main loop stuck from 610 ms. Top, TIM2 ISR kicks every 100 ms: the kicks go on after 610 ms, the counter never goes below 900 and there is no reset. Bottom, check-in: the ISR kicks at 250, 450 and 650 ms, then sees alive = 0 at every interrupt, the counter falls to 0 and the chip resets at 1650 ms
A kick from a timer interrupt only proves that the interrupt runs; the check-in makes the kick depend on the loop finishing its work.

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.

WWDG counter from 0x7F after a kick, one count every 1.024 ms. From 0 to 48.128 ms the counter is above W = 0x50 and a kick resets the chip (red band, too early). From 48.128 to 65.536 ms the kick is allowed (green band). At 65.536 ms the counter goes from 0x40 to 0x3F and the chip resets (too late). Kicks at 30 ms (reset), 50 ms (OK) and 70 ms (too late) are marked
The window watchdog accepts a kick only between 48.128 and 65.536 ms after the last one; earlier or later both reset the chip.
IWDGWWDG
clockLSI, about 32 kHz, its ownPCLK1 ÷ 4096 ÷ 2WDGTB
resets whencounter reaches 0T goes 0x40 → 0x3F, or a kick while T > W
kickIWDG_KR = 0xAAAAWWDG_CR = T (e.g. 0x7F)
catchesa hang, a stopped main clocka hang, and timing that is too fast or too slow
timeouts0.125 ms to about 32 smilliseconds 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.

On the mind map

Open the Operating Systems mind mapOperatingSystemsBoot and program start: from power on to main()Boot and program startProcesses and the kernel: how user code talks to the kernelProcesses and the kernelInterrupts and devices: hardware interrupts the CPUInterrupts and devicesScheduling and deadlock: who runs next, who gets whatScheduling and deadlockVirtual memory: every address is translatedVirtual memoryPhysical memory: who gets which bytes of RAMPhysical memoryFiles and storage: from open() down to the diskFiles and storageDisks and RAID: blocks down to platter or flashDisks and RAIDIsolation and security: limit what a process can doIsolation and securityInterrupt-driven I/OInterrupt-driven I/OIRQ, IDT, handler, then iretInterrupt priority nestingInterrupt priority nestingOnly a more urgent level cuts inWatchdog timerKick only after real work, never from an ISRCortex-M exceptionsCortex-M exceptionsHardware stacks 8 words; tail-chain in 6Kinds of interruptsKinds of interruptsIRQ, timer, NMI, fault, syscall: one IDTDMADMAdevice writes RAM itself, then one IRQHW/SW co-design acceleratorHW/SW co-design acceleratorAmdahl: 2,226 → 478 cycles, capped at 12.5×Packet receive pathPacket receive pathone IRQ, then NAPI polls in a softirqOpen the Operating Systems mind mapOperating Systemshardware interrupts the CPUInterrupts and devicesInterrupt-driven I/OInterrupt-driven I/OIRQ, IDT, handler, then iretInterrupt priority nestingInterrupt priority nestingOnly a more urgent level cuts inWatchdog timerKick only after real work, never from an ISRCortex-M exceptionsCortex-M exceptionsHardware stacks 8 words; tail-chain in 6Kinds of interruptsKinds of interruptsIRQ, timer, NMI, fault, syscall: one IDTDMADMAdevice writes RAM itself, then one IRQHW/SW co-design acceleratorHW/SW co-design acceleratorAmdahl: 2,226 → 478 cycles, capped at 12.5×Packet receive pathPacket receive pathone IRQ, then NAPI polls in a softirq
This page on the Operating Systems mind map · 78 pages in 9 branches · open it to see the whole map and test yourself in recall mode.