STM32 Watch Dog Timer – IWDG
STM32 Watch Dog Timer – IWDG

Anything that can go wrong, will go wrong especially for embedded systems. Apart from hardware faults, which can also impact on the software, even the most careful design may have some unexpected conditions that lead to abnormal behaviour of our device. And this may have important costs, especially if the device is designed to work in dangerous and critical contexts.

A watchdog timer is a specialized timer module that helps a microprocessor to recover from malfunctions. A watchdog timer is based on a counter that counts down from some initial value to zero. If a watchdog timer reaches the end of its counting period, it resets the entire processor system. In order to prevent this, a processor must perform some type of specific action that resets the watchdog. Thus, a watchdog timer can be configured such that it will reach the end of its counting period only if a processor failure has occurred, and by forcing a system reset, the watchdog timer helps the processor to escape from the failure mode and continue normal operation.

In STM32 microcontrollers the independent watchdog (IWDG) is a 12-bit down-counter timer clocked by its own dedicated low-speed clock (LSI) and thus stays active even if the main clock fails.

IWDG initialization

IWDG_HandleTypeDef hiwdg;
  
  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
  hiwdg.Init.Reload = 4095;
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  {
    Error_Handler();
  }
  • Instance – Pointer to IWDG descriptor
  • Init.Prescaler – prescaler value, and it can assume all powers of two ranging from 2² up to 2⁸. For example:
    1. If prescaler is set to IWDG_PRESCALER_4 and reload value is set to 4096 then timeout period is about 512ms.
    2. If prescaler is set to IWDG_PRESCALER_64 and reload value is set to 4096 then timeout period is about 8192ms approx. 8 sec.
  • Reload – specifies the timer period, that is the auto-reload value when the timer is refreshed. It can range from 0x0 up to 0xFFF

To refresh IDWG timer before it reaches zero, HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg); function has to be used.

Independent watchdog is started by writing the value 0xCCCC in the Key register (IWDG_KR), the counter starts counting down from the reset value of 0xFFF. Whenever the key value 0xAAAA is written in the IWDG_KR register, the IWDG_RLR value is reloaded in the counter and the watchdog reset is prevented. This functionality is carried out internally by HAL drivers.

Block Diagram

Simple application

IWDG_HandleTypeDef hiwdg;
UART_HandleTypeDef huart2;

int main(void)
{
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
	
  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
  hiwdg.Init.Reload = 4095;
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  {
    Error_Handler();
  }

	uint8_t msg[100] = "Welcome to embeddeddiaries.com!!";
	uint8_t msg1[100] = "Refreshing the IWDG timer\r\n";
	uint8_t msg2[100] = "Control got stuck\r\n";

	HAL_UART_Transmit(&huart2,msg,sizeof(msg),1000);
	for(int i = 0; i < 5; i++)
	{
                //Normal microcontroller operation
		HAL_UART_Transmit(&huart2,msg1,sizeof(msg1),1000);
		HAL_Delay(1000);
		HAL_IWDG_Refresh(&hiwdg);
	}
                //Assume while(1) means microcontroller got stuck
		HAL_UART_Transmit(&huart2,msg2,sizeof(msg2),1000);
		while(1);	
}

///////////////////////main.c////////////////

In this project the IWDG timer is set to 32 seconds if the microcontroller did not refresh the IWDG timer within 32 seconds, assuming that the fault has occured or control got stuck somewhere, the automatic reset will be triggered.