stm32 watchdog example code

STM32 Watchdog Example Code

By Kevin Wilson
AD

In the realm of embedded systems, the STM32 microcontroller series from ST Microelectronics stand out due to their versatility and performance. One of the safety features offered by these microcontrollers is the watchdog timer. The watchdog timer, or IWDG (Independent Watchdog Timer), ensures that the system does not hang or get stuck in an infinite loop by restarting it if needed. In this article, we delve into the world of STM32 watchdog timers, with a hands-on focus on example code.

Before we dive into the details, let's ensure we understand why watchdog timers are essential. In a nutshell, watchdog timers are like lifeguards in a pool – they keep an eye on the system, making sure it's behaved. If the system misbehaves or stops feeding the watchdog, it triggers a reset, much like how a lifeguard would respond to a swimmer in distress. Now that we've established their importance let's explore STM32's watchdog implementation.

Understanding STM32's IWDG

The Independent Watchdog Timer (IWDG) in STM32 microcontrollers is an independent peripheral, meaning it is not controlled by the CPU. This independence ensures that it can operate even when the software or CPU is malfunctioning. It is powered directly by the VDD supply and has its clock source, making it robust and reliable.

AD
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...
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
Getting started with STM: STM32F429 Discovery
Getting started with STM: STM32F429 Discovery

STM32's IWDG consists of three primary registers: the Prescaler Register (IWDG_PR), the Reload Register (IWDG_RLR), and the Status Register (IWDG_SR). The IWDG_PR sets the prescaler value, the IWDG_RLR sets the initial counter value (or reload value), and the IWDG_SR indicates the state of the IWDG.

Setting Up the IWDG

To initialize the IWDG, we start by enabling its clock and resetting it. We then set the prescaler and reload values. Here's an example of how to enable and configure the IWDG in STM32:

Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Canaduino STM32 boards with MicroPython
Canaduino STM32 boards with MicroPython
PWM with STM32 - The Engineering Projects
PWM with STM32 - The Engineering Projects
void IWDG_Setup(uint16_t counter){
  // Enable IWDG clock
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_IWDG, ENABLE);

  // Reset IWDG
  IWDG_ResetCounter();

  // Set prescaler and reload values (counter is in ms)
  IWDG_SetPrescaler(100);
  IWDG_SetReload(counter);
}

Here, we've enabled the IWDG clock, reset the counter, set the prescaler (division factor of the APB1 clock), and set the reload value (the initial counter value).

Feeding the IWDG

To avoid a system reset, the IWDG must be "fed" regularly. This is done by writing to the IWDG_KEYOgesetzt Register. Here's an example:

AD
void IWDG_Feed(void){
  IWDG_ReloadCounter();
}

In this function, we call IWDG_ReloadCounter() to update the IWDG counter, effectively "feeding" it and resetting the timeout period.

STM32. Интерфейс SWD. Создание собственного программатора - отладчика.
STM32. Интерфейс SWD. Создание собственного программатора - отладчика.
STM32 Serial Communication in Polling Mode
STM32 Serial Communication in Polling Mode
How to use SPI in STM32F103C8T6? STM32 SPI Tutorial
How to use SPI in STM32F103C8T6? STM32 SPI Tutorial
STM32 Serial Communication With PC (STM32 UART To USB)
STM32 Serial Communication With PC (STM32 UART To USB)
Переходим на STM32 | Практическая электроника
Переходим на STM32 | Практическая электроника
Нестандартный способ подружиться с STM32: не Ардуино и не Discovery
Нестандартный способ подружиться с STM32: не Ардуино и не Discovery

Example Code: Blinking LED while Resetting with IWDG

Let's put the IWDG to use in a simple example: blinking an LED and triggering a system reset if the process stops. Here's a straightforward example using the IWDG with STM32's GPIO and SysTick peripherals:

Initializing the IWDG and SysTick

First, we initialize the IWDG and SysTick. SysTick is used to delay the blinking of the LED and to feed the IWDG periodically.

AD
void SysTick_Init(void) {
  // Initialization code...
}

void IWDG_Init(void) {
  // IWDG setup code...
  // Set the reload value for a 1s timeout
  IWDG_SetReload(1000); // Assuming a 8MHz APB1 clock and a 1000ms timeout
}

Blinking the LED and Feeding the IWDG

Next, we create a function to blink the LED and feed the IWDG. We use a infinite loop in this case, but in a real-world application, you'd want to use higher-level constructs like tasks or interrupts.

STM32 Tutorials. ARM Programming – STM32 Course - DeepBlueMbedded
STM32 Tutorials. ARM Programming – STM32 Course - DeepBlueMbedded
STM32 Starting Small
STM32 Starting Small
STM32 Keil uVision 5 Debug
STM32 Keil uVision 5 Debug
STM32F103C8T6 Blue Pill: high resolution pinout and specs - Mischianti
STM32F103C8T6 Blue Pill: high resolution pinout and specs - Mischianti
Using DAC with STM32 - The Engineering Projects
Using DAC with STM32 - The Engineering Projects
War-Walker/images/finished.jpg at master · stevemcquaid/War-Walker
War-Walker/images/finished.jpg at master · stevemcquaid/War-Walker
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
the circuit diagram for an analog clock with lm388 opp - amp
the circuit diagram for an analog clock with lm388 opp - amp
ESP32 Data Logging with DHT22 and Real-Time NTP Timestamps
ESP32 Data Logging with DHT22 and Real-Time NTP Timestamps
Interfacing I2C LCD with STM32F103C8T6 | STM32 I2C LCD Tutorial
Interfacing I2C LCD with STM32F103C8T6 | STM32 I2C LCD Tutorial
How to create stm32 project in stm32cubeide with example code
How to create stm32 project in stm32cubeide with example code
Build a Custom STM32 PCB with KiCad
Build a Custom STM32 PCB with KiCad
STM 32 Nucleo – Supporto Arduino Core
STM 32 Nucleo – Supporto Arduino Core
void LED_Blink(void) {
  while(1) {
    GPIO_ToggleBits(GPIOA, GPIO_Pin_8); // producir un 'pulsito' de 1ms
    delay(1000); // esperamos 1 segundo
    IWDG_Feed(); // feed the dog
  }
}

The IWDG_SetReload() function sets a 1 second timeout. If the watchdog isn't fed (i.e., IWDG_WriteAccess() isn't called) within that time, the system will reset.

The final touch is to call IWDG_Init() and LED_Blink() in the main() function. And there you have it – a simple example of using the IWDG with STM32. You've learned how to initialize and use the IWDG, and how to feed it to avoid a reset. With this knowledge, you can now build upon this foundation to implement more robust and reliable systems. Happy coding!

AD
AD