Embedded development--STM32 on-chip peripherals--independent watchdog IWDG

Embedded Development – ​​STM32 On-Chip Peripherals – Independent Watchdog IWDG

watchdog

In a microcomputer system composed of a single-chip microcomputer, because the work of the single-chip microcomputer is often disturbed by an external electromagnetic field, the data of various registers and memory is confused, which will cause the program pointer to be wrong, not in the program area, and take out wrong program instructions, etc. It is possible to fall into an infinite loop, the normal operation of the program is interrupted, and the system controlled by the single-chip microcomputer cannot continue to work normally, resulting in the stagnation of the entire system and unpredictable consequences.

Watchdog, also called watchdog, is essentially a timer circuit, generally has an input and an output, where the input is called feed dog, and the output is generally connected to the reset terminal of another part, usually connected to a single-chip microcomputer. The function of the watchdog is to regularly check the internal situation of the chip, and send a restart signal to the chip once an error occurs. The watchdog command has the highest priority among program interrupts.

The above is the copied content. To put it bluntly, the MCU may work abnormally due to interference, or the program may run away due to software programming errors. For example, instructions should be fetched in the program area, but due to abnormal pointers, they ran to the data area to fetch instructions. Or the other way around. It is conceivable that program execution will have unexpected consequences.

So we set up a hardware mechanism that must send a signal to a certain hardware circuit within a fixed time interval. If this time interval is exceeded, the hardware circuit will reset the microcontroller to make the program re-execute. This increases the reliability of the system. This hardware mechanism is called watchdog. Send a signal to feed the dog. Once the timeout dog will bark, the MCU will be reset.

Watchdog for STM32

All STM32 series have watchdogs, and both independent watchdogs (IWDG) and window watchdogs (WWDG). Today is about the independent watchdog.
As the name suggests, this kind of watchdog runs independently, and it uses the low-speed internal clock LSI as the clock source. In different product series, the frequency of LSI is 32 or 40KHz. Due to the large frequency error of LSI, it may reach More than 2%, this should be considered when programming. As shown in the figure below, the frequency error is 1K, that is, 3%
insert image description here
insert image description here

Use of Independent Watchdog

Very simple, just 2 steps. Initialize and feed the dog.

initialization

insert image description here
Click Activated to start the independent watchdog.
The lower right corner is the initialization parameter, taking the 40KHz chip as an example, the configuration shown in the figure is 40000/4/4095=2.5 times/second, that is, a reset occurs every 400ms. The dog must be fed within 400ms, and the interval between any two feedings should not exceed 400ms. Once the timeout expires, the dog will bark and the MCU will restart immediately.

feed the dog

Include this reference in the file that needs to feed the dog

#include "iwdg.h"		//在需要喂狗的文件中加入此引用

The next line is to feed the dog.

HAL_IWDG_Refresh(&hiwdg);	//喂狗

Tips for feeding dogs

1 Feed the dog in the main loop, this is the most basic way
2 Feed the dog in the flashing function, this is easy to ignore
3 Feed the dog in the function function, if a function execution function takes a long time to execute, pay attention to the dog barking
4 in the delay Feed the dog in the time function.
When there is no watchdog, the delay can be written as follows

HAL_Delay(1000);	//延时1秒

When the watchdog is enabled, the above-mentioned dog will bark and the system will reset. It should be written as follows:

//延时1秒
for(i=0; i<10; i++)
{
    
    
	HAL_Delay(100);				//每次延时0.1秒
	HAL_IWDG_Refresh(&hiwdg);	//喂狗
}

5 It is not recommended to open a timer interrupt to feed the dog specifically

Guess you like

Origin blog.csdn.net/13011803189/article/details/130042669