STM32 Guide: Watchdog Timer

Prerequisites

This is tutorial is not intended to be a guide for learning C language or about the STM32 platform. It’s primary target is to provide developers a concise guide about integrating peripheral modules and features into active applications.

If you are a beginner, I would recommend you look into an STM32 Project Setup guide like this one.

https://medium.com/vicara-hardware-university/smt32-project-setup-with-cubeide-947974baf713

Watchdog Timer

A watchdog timer is a simple countdown timer which is used to reset a microprocessor after a specific interval of time. In a properly operating system, software will periodically “pet” or restart the watchdog timer. After being restarted, the watchdog will begin timing another predetermined interval.

Image for post

Implement Watchdog Timer in STM32

To implement a watchdog timer, we need to configure a timer that generates an interrupt at a fixed interval. This can be done easily by writing and configuring the Timer object in STM32.

In STM32Cube IDE, under the System Core tab, we have the WWDG option. We need to activate it and set the clock features in the section below.

Image for post

When you generate code from here you should find the following definition around the top of your main.c file.

/* WWDG handler declaration */
WWDG_HandleTypeDef   WwdgHandle;

This is the watchdog timer handle which will be passed to all functions that need to operate or work from watchdog timer operations.

Setup WWDG Config

/*##-1- Check if the system has resumed from WWDG reset ####################*/  
if(__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET)  {
    /* WWDGRST flag set: Turn LED1 on */
   BSP_LED_On(LED1);
    /* Clear reset flags */
   __HAL_RCC_CLEAR_RESET_FLAGS();
 }  else  {
   /* WWDGRST flag is not set: Turn LED1 off */
   BSP_LED_Off(LED1);  
}    
/*##-2- Configure the WWDG peripheral ######################################*/  
/* WWDG clock counter = (PCLK1 (42MHz)/4096)/8) = 1281 Hz (~780 us)      WWDG Window value = 80 means that the WWDG counter should be refreshed only      when the counter is below 80 (and greater than 64) otherwise a reset will      be generated.      WWDG Counter value = 127, WWDG timeout = ~780 us * 64 = 49.9 ms */WwdgHandle.Instance = WWDG;  
WwdgHandle.Init.Prescaler = WWDG_PRESCALER_8;
WwdgHandle.Init.Window    = 80;  
WwdgHandle.Init.Counter   = 127;  
if(HAL_WWDG_Init(&WwdgHandle) != HAL_OK)  {
   /* Initialization Error */
   Error_Handler();
 }
/*##-5- Start the WWDG #####################################################*/   if(HAL_WWDG_Start(&WwdgHandle) != HAL_OK)  {
   Error_Handler();
 }

Start WWDG in Execution Loop

/* Refresh WWDG: update counter value to 127, the refresh window is:     ~780 * (127-80) = 36.6ms < refresh window < ~780 * 64 = 49.9ms */          if(HAL_WWDG_Refresh(&WwdgHandle, 127) != HAL_OK)
   {
     Error_Handler();
   }

Conclusion

In this tutorial, we discussed the methods for using WWDG API from CMSIS-HAL library for implementing Watchdog Timers.

Sources