1. Use environment of watchdog
In the industrial working environment, the equipment often faces some harsh environments, such as ultra-high temperature and ultra-low temperature strong electromagnetic interference. These environments will affect the normal operation of the equipment, cause the equipment to crash, and the programs running in the equipment will fly directly
Therefore, the watchdog can be used to help the programmer detect whether the device crashes or the program flies. Once the device crashes or the program flies, the watchdog can help the programmer restart the system
2. Introduction to watchdog
The watchdog is a timer resource. Since the watchdog is a timer, the timing length of the watchdog is determined by the clock frequency and count value. Unlike ordinary timers, ordinary timers can handle the things specified by the programmer when the timing arrives, but different from the watchdog, when the timing of the watchdog arrives, restart the system directly (press reset), If the device itself does not crash or the program does not fly, there is no need to restart
3. The watchdog feeds the dog
Use the watchdog to monitor the equipment system. If the equipment does not crash and the program does not fly, it does not need to be restarted
It is necessary to feed the dog at regular intervals (re assign the count value to the watchdog counter)
4. Watchdog peripheral resources
Some chips have integrated watchdog peripherals - > which can be used directly
Some chips have no watchdog peripherals - > hardware engineers need to connect a watchdog on the development board
5. Introduce the watchdog in STM32 chip
There are two watchdog in STM32F103RBT6 chip: independent watchdog (IWDG) and window watchdog (WWDG).
Note: Generally speaking, there is only one watchdog in the high-end chip, which is similar to the independent watchdog
Similarities:
- Both IWDG and WWDG are peripheral resources provided by STM32 chip
- Both IWDG and WWDG restart the system because the device crashes or the program flies
- Both IWDG and WWDG can adjust the dog feeding time by themselves
difference:
- Different clock sources: IWDG uses an independent internal RC(LSI 40kHz) and does not need to enable the clock through the bus,
WWDG uses the clock provided by APB1 bus and needs to enable the clock through the bus - Different interrupt generation: IWDG has no interrupt. As long as IWDG is triggered, the system will be restarted directly
WWDG has an interrupt, which can be triggered when the counter is timed out - Different counting methods: IWDG uses a 12bit counter (0 ~ 4095)
WWDG uses a 7-bit counter - Different dog feeding time ranges: IWDG can feed dogs at any time before the counter counts to 0
There will be an upper threshold and a lower threshold in the WWDG
Learn more about the independent watchdog: open the < stm32rbt6 \ Datasheet \ STM32 > directory, < stm32f10x Chinese reference manual. PDF > p276 P280
IWDG_KR: accept specific values and perform specific operations, [0-15] is valid and [16-31] is reserved
IWDG_PR: prescaler register, [0-2] valid, [3-31] reserved
IWDG_PLR: reload the load value register, [0 ~ 11] is valid, [12-31] is reserved, and the count value range is [0 - 4095]
/* For driving independent watchdog The timing length is determined by the clock frequency and count value */ #include "stm32f10x_conf.h" extern void iwdg_init(int ms);//Initialization of independent watchdog /* IWDG_KR 0xAAAA Feed dog 0x5555 turn off write protection 0xCCCC turn on watchdog IWDG_PR Set Prescaler IWDG_RLR Set count value */ extern void iwdg_feed_dog(void);//Dog feeding operation
#include "iwdg.h" void iwdg_init(int ms)//Initialization of independent watchdog { IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//Turn on IWDG_PR and iwdg_ Write access to RLR register IWDG_SetPrescaler(IWDG_Prescaler_64);//Set the prescaled value to 64, clock frequency = 40KHz / 64 = 625Hz IWDG_SetReload(ms);//Set reload value IWDG_ReloadCounter();//feed a dog IWDG_Enable();//Open the watchdog } void iwdg_feed_dog(void)//Dog feeding operation { IWDG_ReloadCounter();//feed a dog }
test
int flag = 0;//Defines a global variable for recording flags void h0(void)//Press the KEY0 key to handle the matter { led_on(1); flag = 1;//Change the state of the global variable flag } void h1(void)//KEY1 press the key to handle the matter { led_on(2); } void h2(void)//KEY2 press the key to handle the matter { led_off(1); led_off(2); } int main(void) { led_init();//Call the LED lamp initialization function buzzer_init();//Call the buzzer initialization function button_init();//Call the function initialized by the function key delay_init();//Call the function initialized by the system timer eint_init();//Call the function of key interrupt initialization set_eint_handler(h0, h1, h2);//Set callback function of key interrupt buzzer_on();//Turn on the buzzer iwdg_init(625 * 6);//Call the function initialized by the independent watchdog and set the count value (feed the dog within 6s) delay_ms(4000);//Delay 4s buzzer_off();//Turn off the buzzer while(!flag)//You can let the CPU do other things in an endless loop { iwdg_feed_dog();//feed a dog led_on(0); delay_ms(500); led_off(0); delay_ms(500); } }