STM32 Watchdog Example Code
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.
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:
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:
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.
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.
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.
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!