Beginner-Friendly Guide on Watchdog Timer (WDT) and Configuration on STM32F407VG

What is a Watchdog Timer (WDT)?

A Watchdog Timer (WDT) is a hardware timer that helps in ensuring your system or microcontroller doesn’t get stuck in an infinite loop or remain unresponsive. It’s a safety mechanism that resets the system in case it becomes unresponsive. If the software fails to reset the watchdog timer (indicating the system is stuck), the WDT will automatically generate a system reset.

Why Use a Watchdog Timer?

The Watchdog Timer is crucial for embedded systems, especially in critical applications like medical devices, automotive systems, and industrial control systems. If a fault occurs, such as the software entering an infinite loop or not responding to external events, the WDT ensures the system resets and recovers, thus preventing further malfunction.

Working of Watchdog Timer

  1. Initialization: The WDT is set up to start counting.
  2. Periodic Reset: Your software (application code) must periodically reset the WDT by writing a value to it. This is called “feeding” the watchdog.
  3. Timeout: If the WDT is not reset within a specified timeout period, the watchdog will trigger a system reset to recover from a fault.

How to Configure WDT on STM32F407VG

We’ll go through how to configure and use the Watchdog Timer (WDT) in STM32F407VG using the HAL (Hardware Abstraction Layer).

Step 1: Enable Watchdog Timer Clock

To use the WDT on STM32F407VG, we first need to enable its clock. The STM32 microcontroller uses the Independent Watchdog (IWDG) peripheral. Let’s enable its clock through the RCC (Reset and Clock Control) register.

// Enable IWDG clock
RCC->CSR |= RCC_CSR_IWDGRSTF;  // Reset IWDG if it's active
RCC->CSR |= RCC_CSR_IWDGEN;    // Enable IWDG clock
Step 2: Configure the IWDG (Independent Watchdog)

The IWDG requires a configuration of three key parameters:

  1. Prescaler: The prescaler divides the clock to provide the timeout period.
  2. Reload Value: The reload value sets the timeout duration.
  3. Enable the Watchdog: Finally, enable the IWDG.

Here’s how you can configure it:

// IWDG setup
IWDG->KR = 0x5555;  // Unlock the IWDG configuration
IWDG->PR = IWDG_PR_PR_4;  // Set prescaler (divides clock by 128)
IWDG->RLR = 0xFFF;  // Set reload value (Max timeout)
IWDG->KR = 0xAAAA;  // Start the watchdog timer
  • Prescaler: In the example above, we divide the clock by 128.
  • Reload Value: The reload value is set to its maximum value (0xFFF), so the timer will trigger a reset after a long timeout period.
Step 3: Feeding the Watchdog Timer (Resetting the WDT)

Once the WDT is configured and running, your application must periodically reset the timer to avoid a reset. If the WDT is not reset within the timeout period, it will automatically generate a system reset.

// Reset the watchdog timer (feeding the WDT)
IWDG->KR = 0xAAAA;

This command resets the WDT and starts counting down again.

Step 4: Wait for Watchdog Timeout (Optional)

For testing purposes, you can wait for the watchdog timer to trigger a system reset by intentionally avoiding feeding the WDT:

while(1) {
    // Your application code here
    // Don't feed the watchdog to cause a reset
}

If the WDT is not reset within the specified timeout, the system will automatically reset, allowing the system to recover from a potential fault.

Key Points to Remember:

  • Timeout Configuration: The timeout period is determined by the prescaler and reload value.
  • Watchdog Reset: Always ensure that the watchdog timer is fed (reset) periodically to prevent an automatic system reset.
  • IWDG vs WWDG: STM32 offers two watchdog timers: IWDG (Independent Watchdog) and WWDG (Window Watchdog). The IWDG is more common, and in this guide, we used it.
  • System Recovery: The watchdog timer helps in automatically recovering from software issues, especially in embedded applications that need high reliability.

Leave a Reply

Your email address will not be published. Required fields are marked *