Detailed Explanation of MCU STM32 Watchdog (Embedded Learning)

what is watchdog

The watchdog (Watchdog) of the microcontroller STM32 is a hardware timer used to monitor the operating status of the system and take measures to restore normal operation in the event of a failure or deadlock. The main function of the watchdog is to periodically check whether the system is running normally and trigger a reset operation if there is a problem with the system.

STM32 series microcontrollers are usually equipped with a built-in watchdog timer (commonly known as independent watchdog timer, IWDG) and window watchdog timer (WWDG) to provide more reliable system protection.
insert image description here

The independent watchdog (IWDG) is a commonly used watchdog in STM32. It is an independent hardware module that can run independently within the system. By configuring the counter and prescaler of the IWDG timer, the timing time of the watchdog can be set. When the watchdog timer counter reaches the preset value, a watchdog timeout event will be generated to trigger a system reset.

A windowed watchdog (WWDG) is another type of watchdog that allows the counter value to be updated within a specific time window to avoid triggering a reset. WWDG can be configured by setting the window and counter value, and every time the counter is updated, it ensures that the counter value is within the set window range. If the counter exceeds the window range, a watchdog reset will be triggered.

By using the watchdog function of STM32, the normality of system operation can be monitored to prevent the system from deadlock or unresponsive state caused by unexpected failures. The watchdog can provide a safety mechanism to restore the normal operation of the system in the event of system software programming error, power disturbance or other abnormal conditions.

Why do you need a watchdog?

A watchdog plays an important role in an embedded system, and here are some reasons why a watchdog is needed:

  1. Preventing system deadlocks: Under certain circumstances, an embedded system may get stuck in an infinite loop or deadlock state, causing the system to stop responding. The watchdog can monitor the running status of the system. If the system fails to update the watchdog timer within the predetermined time, a reset operation will be triggered to restart the system and resume normal operation.

  2. Dealing with software failures: When developing embedded software, problems such as programming errors, memory overflow, and task priority errors may occur, resulting in abnormal system operation. The watchdog can monitor whether the system continues to run, and reset when the software fails to restore the system to the original state.

  3. Response to external interference: Embedded systems may be subject to external interference, such as power fluctuations, electromagnetic interference, etc. These disturbances may cause the system to crash or operate incorrectly. A watchdog can detect these anomalies and take action when the system becomes unresponsive to ensure system stability and reliability.

  4. Improve system reliability: By introducing a watchdog, the system can automatically recover when a fault occurs, reducing system downtime. This is very important for applications that require continuous operation and high reliability, such as industrial control, automotive electronics, medical equipment and other fields.

In conclusion, the watchdog provides a monitoring and protection mechanism to ensure the reliability and stability of embedded systems. It prevents system deadlocks, handles software failures, defends against external interference, and increases system availability and reliability.

STM32CubeMX configuration and application

insert image description here
insert image description here

When using STM32CubeMX to configure and apply the watchdog, you can follow the steps below:

  1. Open the STM32CubeMX software and create or open your project.

  2. In the "Pinout & Configuration" tab, select your target STM32 microcontroller model.

  3. In the "Peripherals" panel, find and expand the "System Core" option.

  4. Select "Independent Watchdog" (independent watchdog) or "Window Watchdog" (window watchdog), and select the corresponding watchdog type according to your needs.

  5. Configure the parameters of the watchdog, including counter value, prescaler, window value, etc., and set them according to your application requirements. You can configure it in the "Configuration" panel.

  6. After the configuration is complete, click the "Project" menu, select "Settings", and then enable watchdog code generation in the "Code Generator" option.

  7. Click the "Project" menu, select "Generate Code", generate the code and export it to your project directory.

  8. In your code, you can use the related watchdog functions to initialize and operate the watchdog.

For example, if an independent watchdog (IWDG) is used, the following functions can be used for initialization and operation:

  • HAL_IWDG_Init(): Initialize independent watchdog, set parameters such as counter and prescaler.
  • HAL_IWDG_Start(): Start the independent watchdog counter.
  • HAL_IWDG_Refresh(): Refresh the independent watchdog counter to prevent the watchdog from resetting the system when it times out.
  • HAL_IWDG_GetState(): Obtain the status of the independent watchdog and judge whether the watchdog timeout occurs.

For the window watchdog (WWDG), similar functions can be used for configuration and operation.

Note that the above steps only cover the basic process of configuring and applying the watchdog. The specific steps and function names may vary depending on the STM32 series and CubeMX version you are using. It is recommended to refer to the official documentation and the corresponding reference manual for more detailed information and guidance.

example

Independent Watchdog (IWDG)

The following is a code example for writing an independent watchdog (IWDG) using the STM32F103C8T6 microcontroller:

#include "stm32f1xx_hal.h"

IWDG_HandleTypeDef hiwdg;

void SystemClock_Config(void);

int main(void)
{
    
    
  HAL_Init();
  SystemClock_Config();

  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_4;  // 设置预分频器,产生IWDG时钟频率
  hiwdg.Init.Reload = 4095;  // 设置计数器重装载值,用于设定看门狗超时时间

  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  while (1)
  {
    
    
    HAL_IWDG_Refresh(&hiwdg);  // 刷新看门狗计数器,防止看门狗超时复位系统
    // 执行其他任务和代码
  }
}

void SystemClock_Config(void)
{
    
    
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

In the above code example, the main steps are as follows:

  1. Initialize the HAL library and system clock.

  2. Defines IWDG_HandleTypeDefa variable of type hiwdgused to configure and operate a standalone watchdog.

  3. In main()the function, use HAL_IWDG_Init()the function to initialize the independent watchdog, set the prescaler and counter reload value.

  4. In the main loop, use HAL_IWDG_Refresh()the function to refresh the watchdog counter to prevent the watchdog from resetting the system when it times out. You can add other tasks and code here.

Note that the clock configuration in this code example is based on the basic configuration of HSI (Internal High Clock) and PLL (Phase Locked Loop).

Window Watchdog (WWDG)

The following is a code example for writing window watchdog (WWDG) using STM32F103C8T6 microcontroller:

#include "stm32f1xx_hal.h"

WWDG_HandleTypeDef hwwdg;

void SystemClock_Config(void);

int main(void)
{
    
    
  HAL_Init();
  SystemClock_Config();

  hwwdg.Instance = WWDG;
  hwwdg.Init.Prescaler = WWDG_PRESCALER_8;  // 设置预分频器,产生WWDG时钟频率
  hwwdg.Init.Window = 127;  // 设置窗口值
  hwwdg.Init.Counter = 127;  // 设置计数器重装载值

  if (HAL_WWDG_Init(&hwwdg) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  while (1)
  {
    
    
    HAL_WWDG_Refresh(&hwwdg, 127);  // 刷新看门狗计数器,并设置窗口值
    // 执行其他任务和代码
  }
}

void SystemClock_Config(void)
{
    
    
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

In the above code example, the main steps are as follows:

  1. Initialize the HAL library and system clock.

  2. Defines WWDG_HandleTypeDefa variable of type hwwdgused to configure and operate the window watchdog.

  3. In main()the function, use HAL_WWDG_Init()the function to initialize the window watchdog, set the prescaler, window value and counter reload value.

  4. In the main loop, use HAL_WWDG_Refresh()the function to refresh the watchdog counter and set the window value. You can add other tasks and code here.

Precautions

When using the watchdog, there are several aspects that need special attention:

  1. Configuration of the watchdog timer: Make sure to configure the watchdog counter value, prescaler and other parameters correctly to meet your application needs and system stability requirements. A counter value that is too small may cause the system to reset frequently, while a counter value that is too large may cause the watchdog to fail to reset the system in time.

  2. Regularly refresh the watchdog: In the main loop or critical tasks, ensure that the watchdog is refreshed in time to prevent the watchdog from resetting the system when it times out. The operation of refreshing the watchdog should be completed within the predetermined time frame, otherwise the watchdog will consider the system to be faulty.

  3. Start and Stop of Watchdog: Make sure to start and stop watchdog at proper timing. Usually, the watchdog is started after the system initialization is completed, and it is stopped when the system is shut down or an abnormal situation occurs.

  4. System stability and testing: Before using the watchdog, ensure the stability and reliability of the system. Perform comprehensive system testing, including normal operation, abnormal conditions, boundary conditions, etc., to ensure that the watchdog works as expected and protects the system from failures.

  5. Be aware of watchdog limitations: Know the limitations and characteristics of the specific watchdog you are using. Different microcontrollers and watchdog modules may have different features and limitations, such as maximum counter value, accuracy, reset time, etc. Make sure to comply with relevant specifications and requirements during use.

  6. Handle watchdog timeout: When watchdog timeout occurs, the system will be reset. If a watchdog timeout reset occurs, please carefully check the system code and hardware design to find potential problems, such as task priority, infinite loop, resource competition, etc.

In short, the use of the watchdog needs to ensure correct configuration and timely refresh, and at the same time conduct system stability testing and troubleshooting to ensure the reliability and stability of the system.

Guess you like

Origin blog.csdn.net/m0_56694518/article/details/131391513