CERA2

Datasheetcera2 · embedded

Watchdog Timer: How It Works and How to Use It Correctly

A watchdog timer is an electronic or software counter that gives an embedded system a way to recover when its software stops making progress. Firmware must periodically refresh the watchdog timer; if it does not, the counter expires, asserts a reset signal, and restarts the processor. That makes the watchdog a last line of defense against hangs caused by software faults, EMI, or bit errors that send execution off course.

How a watchdog timer detects a fault and forces recovery

A watchdog timer (WDT) is built around a counter register driven by a clock source. Depending on the design, it counts down to zero or up to an overflow. Embedded software loads an initial value and periodically restarts it before the deadline. Those refreshes are also called feeding, kicking, or petting the watchdog.

If the expected refresh never arrives, a watchdog timeout occurs. The watchdog logic asserts a reset signal tied to the processor reset pin, so the system reboots as if power had been cycled. Most watchdog inputs are edge-triggered: a rising or falling edge on a WDI pin clears the counter.

A soft memory failure or EMI discharge can flip a bit; if the processor then starts executing operands as if they were opcodes, it stops servicing the watchdog and a reset follows. Remote sensors, satellites, and space probes cannot rely on an operator to press reset, which is why autonomous recovery is essential.

Some WDTs issue a non-maskable interrupt instead of a reset. However, a CPU in an arbitrary dysfunctional state may not respond to an interrupt; a hard reset reliably restores operation.

Choose an internal, external, or window watchdog for the failure you need to catch

An internal watchdog timer is built into the MCU. It saves board space and cost, but it can be affected by the runaway code it is supposed to catch if a crash accidentally disables it. An external watchdog is a separate IC; a crashed program cannot disable its functionality, and external devices also assert reset during power-up.

Type What it monitors well Important limitation or behavior
Internal watchdog Firmware liveness with little board overhead May share failure modes with the MCU and its code
External watchdog MCU failure and power-up supervision Consumes additional board space
Independent watchdog Hangs that coincide with a main-clock failure Catches late refreshes only, not early ones
Window watchdog Refreshes that are too early as well as too late On STM32 it stops when the system clock stops

Using an internal watchdog and an external watchdog together creates redundancy. For example, a supervisor IC can monitor both a supply rail and a digital pulse from the MCU. The external device can still recover the system if the MCU’s internal watchdog fails.

STM32 MCUs illustrate two modes. The Independent Watchdog (IWDG) uses a 12-bit down-counter, an 8-bit prescaler, and an independent 32 kHz internal RC oscillator, so it remains active if the main clock fails. The Window Watchdog (WWDG) uses a 7-bit down-counter clocked from APB1; if that system clock stops, the WWDG stops too. The IWDG can be refreshed any time before zero, while the WWDG resets on a refresh outside its valid time window. Its Early Wakeup Interrupt can fire one count before the reset threshold, giving firmware a chance to save diagnostic data. Once enabled, neither the IWDG nor the WWDG can be disabled except by a system reset, so runaway code cannot switch them off. Running both together covers more failure modes: the IWDG survives clock failures, and the WWDG validates execution timing.

Set the timeout from measured work, startup, and clock tolerance

A timeout is the maximum permitted gap between successful watchdog refreshes. It must be longer than the longest legitimate gap, evaluated at the watchdog clock’s fastest tolerance: t_gap,max < t_timeout,min. A shorter interval recovers sooner, but an interval shorter than worst-case execution creates nuisance resets.

  1. Identify the longest legitimate path between completed health checks, including slow loop iterations or scheduled work.
  2. Add startup time. A device that needs five seconds to boot but begins with a three-second watchdog will enter an endless reset cycle.
  3. Account for watchdog clock tolerance, then test the selected value under the slowest expected execution conditions.
  4. Decide what expiry does: trigger a reset, or save diagnostic data before reset where supported.

For a typical main-loop application, a 5–30 second timeout is a useful starting rule. At the other extreme, the Space Shuttle main engine controller used an 18 ms watchdog timeout, shorter than a major control cycle so a backup computer could take over before irreversible engine action.

On STM32L476 devices, the IWDG has a programmable range from 125 µs to 32.7 s, while the WWDG range is 51.2 µs to 28.2 ms. The IWDG timeout follows (RL × P) / f_LSI, where RL is reload, P is prescaler, and f_LSI is the internal oscillator frequency.

/* STM32 IWDG setup: prescaler 256, reload 2000, about 16 s at 32 kHz */
IWDG->KR  = 0xCCCC;      /* start the watchdog */
IWDG->KR  = 0x5555;      /* unlock PR and RLR */
IWDG->PR  = 0x06;        /* prescaler divide by 256 */
IWDG->RLR = 2000;        /* reload value */
while (IWDG->SR != 0) { }  /* wait until the registers are updated */
IWDG->KR  = 0xAAAA;      /* first refresh */

Refresh the watchdog only after the system proves it is healthy

The right moment to refresh is after the work that demonstrates liveness, not whenever an interrupt happens to run. A robust main-loop design checks conditions such as stack depth, buffer allocation, and completion of every critical task, then feeds the watchdog only when those checks pass.

if (all_critical_tasks_reported() && stack_is_safe() && buffers_are_available()) {
    IWDG->KR = 0xAAAA;  /* refresh only after health checks */
}

Heartbeat flags make that rule explicit. Each critical task sets its flag only after completing its own work. A watchdog monitor verifies every flag, clears them for the next cycle, and refreshes the hardware watchdog. If one task stalls, its flag is missing and the monitor intentionally lets the WDT expire.

Do not refresh from a periodic timer interrupt. The interrupt can continue to run while the main application is deadlocked, making the watchdog ineffective. For the same reason, do not call feed() from inside a long blocking network request or sensor read; that operation may be exactly the hang the watchdog should catch.

Use a hardware watchdog beneath RTOS task monitoring

In an RTOS, a hardware watchdog protects the scheduler and the system as a whole, while software watchdog channels supervise individual threads. A common task-watchdog pattern assigns each thread its own timeout; the hardware WDT remains the fallback if the scheduler or task-watchdog fails.

The idle task is one practical place to update the hardware watchdog because it runs only when higher-priority tasks are blocked or sleeping. If a higher-priority task monopolizes the CPU, the idle task starves and the watchdog expires. A dedicated watchdog task can instead collect heartbeat flags, provided it cannot merely certify itself while the rest of the application is stuck.

After a watchdog reset, read the reset-cause register before normal initialization erases the evidence. On STM32, RCC->CSR includes flags for power-on, reset-pin, and IWDG reset causes. Firmware can log a watchdog cause, count consecutive failures in non-volatile memory, and enter a safe mode after repeated resets. Clear that crash counter only after sustained normal operation, not at startup.

Common watchdog mistakes that leave a system unprotected

The most basic mistake is not using a watchdog timer at all. Other frequent ones are enabling it before a long boot sequence has finished, which causes an endless reset loop, and scattering refresh instructions across the code, which makes it impossible to tell when the timer can still expire. Four further mistakes start as plausible assumptions.

“Any periodic refresh proves the software is alive.”

A timer interrupt can feed the watchdog while the application is frozen. Tie refresh to completed critical work instead.

“The shortest timeout is always safer.”

A too-short setting causes nuisance resets during valid worst-case work and makes debugging harder. The timeout must exceed the longest legitimate refresh gap and startup path.

“A watchdog reset is enough information.”

Without checking the reset cause, firmware cannot distinguish a watchdog reset from a normal power-on or external reset, leaving recurring failures undiagnosed.

“Once it is configured, it does not need testing.”

Deliberately allow the watchdog to expire during startup testing and verify the reset. This catches silicon defects or configuration errors that would otherwise leave recovery silently disabled.

What the Dell watchdog timer does in PCs and servers

Dell uses the term for several platform-specific recovery features rather than one universal embedded WDT. On an OptiPlex XE, the Watchdog Timer software utility monitors programs listed in Windows Task Manager and can terminate unresponsive programs or restart the computer according to user-defined actions. It monitors the power-on stage, operating-system loading, and the period after the OS loads, tracking up to five programs.

On PowerEdge servers, the OS Watchdog Timer is a BIOS option intended to help recovery when the system stops responding; it is disabled by default. Dell Embedded Box PCs use a BIOS setting for watchdog support during POST and the transition from BIOS to the operating system. Dell iDRAC can also pair a BMC watchdog with systemd through the ipmi_watchdog kernel module: systemd refreshes the watchdog, and a hardware reset follows if systemd becomes non-operational.

Frequently Asked Questions

What does a watchdog timer do?

It expects regular refreshes from functioning software. If they stop, its counter expires and asserts a reset signal so the processor can restart.

Should I enable a watchdog timer?

For embedded systems that cannot be manually reset, such as remote sensors, satellites, or medical devices, it is essential. It also provides automatic recovery from hardware-induced faults such as EMI and bit errors that well-written firmware cannot prevent.

What are the different types and modes of watchdog timers?

Watchdogs are either internal to the MCU or external ICs. By mode, a simple (independent) watchdog only requires a refresh before the timeout, a window watchdog also rejects refreshes that come too early, and a question-and-answer watchdog requires the controller to return a computed answer to a changing question.

What does watchdog timeout mean?

It means the configured period ended before the watchdog was refreshed. The resulting action is usually a system reset, though some designs issue a non-maskable interrupt instead of a reset.

How do you choose and configure a watchdog timer timeout?

Base it on the longest valid gap between health-confirming refreshes, startup duration, and clock tolerance. Set it long enough for worst-case legitimate execution, then test both normal operation and deliberate expiry.

When should you refresh a watchdog timer in software?

Refresh it only after all critical work and health checks have succeeded. Avoid timer-interrupt refreshes and refreshes inside blocking operations, because either can mask a failed application.

What are the advantages of internal and external watchdog timers?

An internal watchdog minimizes cost and board space. An external watchdog cannot be disabled by a crashed program and may add power-up supervision; using both broadens failure coverage.

Related embedded topics

  1. QSPI Explained: Quad SPI Signals, Modes and Flash Memory
  2. Bit Stuffing: What It Is and How CAN, USB, and HDLC Use It
  3. Firmware vs Software: The Differences That Matter
  4. NOR vs NAND Flash: Differences, Speeds and Uses

Index