stm32 watchdog timer example

STM32 Watchdog Timer Example: A Comprehensive Guide

By Andrew Lopez
AD

The STM32 watchdog timer, a crucial component of STM32 microcontrollers, serves as a fail-safe mechanism, automatically resetting the microcontroller if it hangs or freezes due to software or hardware issues. Understanding and effectively utilizing this feature is paramount for building reliable and robust embedded systems. This comprehensive guide will walk you through an STM32 watchdog timer example, ensuring you grasp the concept and implement it in your projects.

Before diving into the examples, let's briefly understand the purpose and operation of the watchdog timer. The watchdog timer continuously counts down from a pre-set value; if the microcontroller's software doesn't reset it periodically (typically before it times out), it'll trigger a system reset. This behavior ensures that the system doesn't remain in a faulty state indefinitely, enhancing overall system reliability.

INITIALIZING AND CONFIGURING THE WATCHDOG TIMER

To initialize and configure the watchdog timer, you'll need to include the appropriate header file (e.g., `stm32f1xx_hal.h` for STM32F1xx series) and initialize the watchdog timer using the HAL (Hardware Abstraction Layer) library's functions. Here's a basic setup:

AD
Arduino Watchdog Timer Tutorial with Example
Arduino Watchdog Timer Tutorial with Example
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
Program sequence monitoring using watchdog timers
Program sequence monitoring using watchdog timers

INITIALIZING THE WATCHDOG TIMER STRUCTURE

The first step is initializing the `wtd_typeDef` structure, which holds the watchdog timer's configuration parameters. Here's an example:

```c WWDG_HandleTypeDef hwwdg; WWDG_InitTypeDef sWWDGInit; ```

Set the wakeup counter register value, counter value, and window register value according to your required timeout period. The timeout period (in milliseconds) can be calculated using the following formula: `Timeout (ms) = (ReloadValue + 1) * ms_per_counter * prescaler`.

Simple Real-Time Clock With Watchdog Timer
Simple Real-Time Clock With Watchdog Timer
PWM with STM32 - The Engineering Projects
PWM with STM32 - The Engineering Projects
Introduction to Watchdog Timer | Electronics For You
Introduction to Watchdog Timer | Electronics For You

INITIALIZING AND ACTIVATING THE WATCHDOG TIMER

After configuring the watchdog timer parameters, initialize and activate the watchdog timer using the HAL's `WWDG_Init()` and `WWDG_Start()` functions:

```c if (WWDG_Init(&hwwdg, &sWWDGInit) != HAL_OK) { Error_Handler(); } if (WWDG_Start(&hwwdg) != HAL_OK) { Error_Handler(); } ```

FEEDING THE WATCHDOG TIMER

Periodically feeding the watchdog timer is crucial to prevent it from triggering a system reset. This operation should ideally happen at regular intervals, corresponding to the watchdog timer's timeout period. Here's how you can feed the watchdog timer using the HAL's `WWDG_Refresh()` function:

AD

FEEDING THE WATCHDOG TIMER WITHIN THE INTERVAL

Include the watchdog timer feeding code within a timer interrupt service routine (ISR) or a regular interval-based task. Below is an example of feeding the watchdog timer within a function called at a 1ms interval:

Tutorial: Adding a WatchDog Timer to Your Project - SwitchDoc Labs Blog
Tutorial: Adding a WatchDog Timer to Your Project - SwitchDoc Labs Blog
How to Create a Watchdog Timer for Computers!
How to Create a Watchdog Timer for Computers!
Digital Stop Watch and Digital Timer Circuit
Digital Stop Watch and Digital Timer Circuit
STM32 Blue Pill Timer in Counter Mode with STM32Cube IDE
STM32 Blue Pill Timer in Counter Mode with STM32Cube IDE
Tutorial: WatchDog Timer - Adding a Status Bit - SwitchDoc Labs Blog
Tutorial: WatchDog Timer - Adding a Status Bit - SwitchDoc Labs Blog
Getting started with STM: STM32F429 Discovery
Getting started with STM: STM32F429 Discovery
```c void WWDGsier(void) { if (WWDG_Refresh(&hwwdg) != HAL_OK) { Error_Handler(); } } ```

FEEDING THE WATCHDOG TIMER BEFORE THE PRE-SCALER EXPIRATION

Remember to feed the watchdog timer before the pre-scaler expires. In the example above, `WWDGsier()` should be called at an interval that ensures the watchdog timer gets refreshed before the pre-scaler expires.

HANDLING SYSTEM RES Reset BY THE WATCHDOG TIMER

When the watchdog timer triggers a system reset, it's essential to execute specific actions, such as saving data or enabling debug features. To achieve this, you can customize the `HardFault_Handler()` function in your project's CMSIS (Cortex Microcontroller Software Interface Standard) file.

AD

CUSTOMIZING THE HARD FAULT HANDLER

Within the `HardFault_Handler()` function, include the necessary code for system failure handling. If you want to enter a Debug mode instead of resetting the system, you can set the `DBGMCU encerr` bit. Here's an example:

Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Bare Metal STM32: The Various Real Time Clock Flavors
Bare Metal STM32: The Various Real Time Clock Flavors
10 Minute External Watchdog for MCU (eg. ESP32 / ESP8266)
10 Minute External Watchdog for MCU (eg. ESP32 / ESP8266)
Free PDF: Practical 555 Timer Projects Guide | Circuits & Schematics
Free PDF: Practical 555 Timer Projects Guide | Circuits & Schematics
Прикольные часы на базе STM32
Прикольные часы на базе STM32
Programmable digital timer switch using a PIC Microcontroller
Programmable digital timer switch using a PIC Microcontroller
Reliable Projects 5: External WatchDog Timers for Raspberry Pi/Arduino Systems - SwitchDoc Labs Blog
Reliable Projects 5: External WatchDog Timers for Raspberry Pi/Arduino Systems - SwitchDoc Labs Blog
Arduino Controlled Versatile Timer/controller
Arduino Controlled Versatile Timer/controller
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
Tutorial - Guest Blog - Using SunControl WatchDog Timer on OurWeather - SwitchDoc Labs Blog
Tutorial - Guest Blog - Using SunControl WatchDog Timer on OurWeather - SwitchDoc Labs Blog
Using DAC with STM32 - The Engineering Projects
Using DAC with STM32 - The Engineering Projects
How to Create a Clock Using Arduino , DS3231 RTC Module and OLED Display
How to Create a Clock Using Arduino , DS3231 RTC Module and OLED Display
Reliable Projects 3: Using the Internal WatchDog Timer for the Arduino - SwitchDoc Labs Blog
Reliable Projects 3: Using the Internal WatchDog Timer for the Arduino - SwitchDoc Labs Blog
```c void HardFault_Handler(void) { if ( (SCB->ICSR & 0xFFFFFFF0U) != 0U ) { SCB->cier = 0xFFFFFFFFU; } // Put a break-point here while (1) { } } ```

To conclude this STM32 watchdog timer example, always ensure you thoroughly test your system's reset behavior, including the watchdog timer's functionality. Understanding and properly utilizing the watchdog timer will greatly enhance the reliability and robustness of your STM32-based systems.

AD