Welcome to YoulianTrade !
loading
    YoulianTrade > News & Blog > Electromechanical >  Structure of the Watchdog Timer (WDT)

    Structure of the Watchdog Timer (WDT)

    Release Time:2025-08-10 16:16:07   Edit:isky  Views:388   【Large】【Medium】【Small】

    A watchdog, or watchdog timer (WDT), is a safety mechanism used in embedded systems to monitor for system failures (software errors and hardware failures). During program execution, system crashes or abnormal states are inevitable, resulting in prolonged system freezes or unrecoverable states. To prevent these, a watchdog is used to reset the system. Based on their implementation and functionality, watchdogs are categorized into two types: software watchdogs and hardware watchdogs.


    A software watchdog is a watchdog function designed in software. Its operating principle is as follows: After a program runs, the CPU's built-in watchdog timer begins counting and periodically calls a reset function to reset the watchdog counter. If a program or system crashes, the watchdog timer exceeds a threshold, triggering a reset through a timer interrupt handler, forcing the system to recover.


    The basic structure of a software watchdog program:


    <1> Initialization: Initializes the hardware abstraction layer, system clock configuration, and hardware peripherals;


    <2> Periodic watchdog feeding: The program periodically calls the watchdog feeding function through the Delay function to reset the count;


    <3> Timeout check: If the watchdog is not fed in time, the timer will reach a timeout state;


    <4> Timeout processing: When the counter exceeds the set time, the timeout processing is executed and the system is reset.


    A hardware watchdog function is implemented in hardware circuitry. It operates independently of the main processor and has its own independent clock and timer. Even if the main processor fails, the hardware watchdog can still function. The hardware watchdog typically waits for a preset timer period for a watchdog feeding signal from the main program. If no watchdog feeding signal is received within this timer period, the hardware watchdog resets the system by triggering the reset pin.


    The basic structure of the hardware watchdog program:


    <1> Initializing the hardware watchdog: Remove the IWDG write protection, allowing writes to the IWDG register. This allows for setting the prescaler (affecting the watchdog timer period), the reload value (determining the timer timeout period), and the reload counter (reloading the IWDG timer counter to its initial value to ensure the watchdog timer does not time out immediately upon initialization. 


    Re-enable write protection via the IWDG configuration register (IWDG->KR = 0xAAAA) to prevent writes to the IWDG configuration register). Start the hardware watchdog (IWDG->KR = 0xCCCC to enable the IWDG timer).


    <2> Periodically feeding the watchdog (periodically resetting the watchdog): In the system's main loop, periodically feeding the watchdog resets the watchdog timer to prevent watchdog timeouts.


    <3> Using the hardware watchdog in the main loop: If a program exception occurs and the watchdog timer cannot be fed normally, a system reboot is triggered.


    <4> Reset processing.