Independent watchdog and window watchdog of stm32f10 series

In a microcomputer system composed of a single-chip microcomputer, the work of the single-chip microcomputer is often interfered with by external electromagnetic fields, causing the program to run away and 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. , will cause the entire system to stagnate and produce unpredictable consequences. Therefore, out of the consideration of real-time monitoring of the running status of the single-chip microcomputer, a module or chip specifically used to monitor the running status of the single-chip microcomputer program has been produced, commonly known as “watching”. “watchdog”.

Let me first talk about the idea of this article. This article will be divided into 3 parts. The first part is written based on the manual and stm32cube to introduce the use of the independent watchdog, and then the code is written to realize the dog feeding through hardware; the second part is written based on the manual and stm32cube to introduce the use of the window dog, and then the code is written to realize the dog feeding through the software; The third part is a comparison of two dogs.

Independent watchdog

1. Introduction and essence

1 Introduction

The independent watchdog (IWDG) is driven by a dedicated low-speed clock (LSI), which remains active even if the main clock fails. IWDG is most suitable for applications where the watchdog needs to be able to work completely independently from the main program as a separate watchdog.

What does this introduction mean? Still don’t understand, it doesn’t matter, I’ll use two pictures to explain to you what it means.

We know that many peripherals of microcontrollers require the participation of internal clocks when working, otherwise they will not work accurately. In the picture below, I turned off both the internal high-speed and low-speed clocks. Let’s see what happens.

Found ? When the internal clocks are turned off, the external LS RC clock can still provide a 40HKz clock for the independent watchdog to use. How about it, it is independent enough. It does not rely on the internal clock at all and has exclusive It is an external clock, so the word “independent” in its name is well deserved.

2.Essence

It is essentially a 12-bit down counter. When the counter value decreases from a certain value to 0, the system will generate a reset signal, i.e. IWDG_RESET. If the counter value is refreshed before the count reaches 0, then the reset signal will not be generated. This action is what we often call feeding the dog.

In layman’s terms, you need to refresh its counter to the original maximum value manually through an external trigger before the independent watchdog countdown ends, so that your program can still You cancontinue. If you don’t interfere and wait until itscountdown ends, it will also reset itself and refresh its counter to its original maximum value, but your program will strong>Executed from scratch. Just like those loopy movies you watch, the protagonist is obviously dead today, but he is alive again the next day, and then the same scene starts over and over again. If you don’t feed the dog within the specified time, the dog will “bite the microcontroller to death” (that is, restart the microcontroller) as soon as the specified time comes. Then comes the “rebirth after death” of the microcontroller. As long as you don’t feed the dog within the specified time, the microcontroller will also fall into the cycle of “being bitten to death and reborn”.

2. Related registers of concern

1. Prescaler register (IWDG_PR)

I don’t know what everyone thinks. What is the use of prescaler? To put it bluntly, prescaler is used to reduce the frequency of recording a number. Think about it if we don’t divide the frequency, the original frequency of counting once is 40Hz, and now the prescaler factor = 4, then the frequency of counting once is 40/4=10Hz. The greater the frequency, the shorter the time to count a number; the smaller the frequency, the longer the time to count a number. Our prescaler factor is determined by the last three digits of IWDG_PR, and there are eight cases in total.

2. Reload register (IWDG_RLR)

Compared with the reload register in the timer, the function of the reload register here is relatively simple. To put it bluntly, you need to set an upper limit of the count. This upper limit does not exceed the 12-bit RL[11:0], that is, 4096 is enough. The manual tells us that the lower limit of the count is 0. After we configure it, every time a reset occurs, the value in the reload register will become the maximum value we set.

3. Key register (IWDG_KR)

What we use here is an independent watchdog, not a hardware watchdog. Don’t pay too much attention to the writing register operation here. It is actually used when we configure stm32cube and HAL library functions.

4. Overflow time calculation formula

The overflow time here is the same as the algorithm of the timer. They both use “time to count a number * count number”, and finally the total time is obtained, which is the overflow time.

3. The configuration of stm32cube is as shown below

SYS

RCC

iWDG

4. Code part

We need to turn on the independent watchdog, the overflow time is 1 second, and use button 2 to feed the dog.

#include "main.h"
#include "iwdg.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"
void SystemClock_Config(void);
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration------------------------------------------------- ----------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

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

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_IWDG_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
HAL_UART_Transmit( & amp;huart1, "Remember to feed the dog\r\\
", strlen("Remember to feed the dog\r\\
"), 100);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
    HAL_IWDG_Refresh( & amp;hiwdg);//Dog feeding function, change the reload register to the set 625
    HAL_Delay(50);
  }
  /* USER CODE END 3 */
}

Burn it into the microcontroller, open the serial port assistant, and you can see that “Remember to feed the dog” is sent every second. When I feed the dog within one second or keep feeding the dog (keep pressing KEY2), it will not Restart to jump out of the loop, but keep calling the dog feeding function to change the reload register to the set 625.

Window watchdog

1. Introduction and essence

1. Introduction in the manual

Window watchdogs are usually used to monitor software failures caused by external interference or unforeseen logic conditions that cause the application to deviate from the normal running sequence. Unless the down counter value is refreshed before the T6 bit becomes 0, the watchdog circuit will generate an MCU reset when the preset time period is reached. If the 7-bit down counter value (in the control register) is refreshed before the down counter reaches the window register value, an MCU reset will also be generated. This indicates that the down counter needs to be refreshed within a limited time window. It’s obvious that I’m confused by what I’m seeing. It doesn’t matter, just keep reading and analyze.

2.Essence

It is a 6-bit counter that can generate system reset signals and early wake-up interrupts.

Generating a reset condition: Reset when the down counter value decreases from 0x40 to 0x3F (that is, the T6 bit jumps to 0 (Program starts over))

When the counter value is greater than the W[6:0] value, feeding the dog will be reset (The program starts over).

Generating interrupt conditions: An early wake-up interrupt (EWI) can be generated when the down counter equals 0x40.

Reload the counter value within the window period to prevent reset (the program will not start over), which is the so-called feeding the dog.

Look at the picture and then the text to understand

If nothing else, you have almost understood the above statement combined with the picture, and you also understand why the name of this dog contains the word “window”, but I guess you are confused about the vertical axis count value in the picture. , it doesn’t matter, keep reading.

2. Related registers of concern

1. Control register (WWDG_CR)

We can see that the seventh bit is to control the opening of the window door dog. We can configure this in stm32cube, followed by bits 0 to 6, which is 7 bits. What does the manual mean? This means that the maximum counting limit of the counter is 2^7, which is 127, because there are 7 bits from 0 to 6; the lower counting limit of the counter is fixed at 0x3F, because once it reaches 0x3F, a reset will occur, and the counter value will become the maximum again. value.

To put it bluntly, it means that the control register determines whether the window door dog is turned on and the maximum value of the vertical axis count value in the picture.

2. Configuration register (WWDG_CFR)

There are a total of 10 bits configurable here. The ninth bit is configured through stm32cube. The eighth and seventh bits are used to configure the prescaler. Bits 0~6 are used to set a value when the counter value is equal to it, as the window. The dividing point between window and non-window, and this value must be less than or equal to the maximum value of the counter and greater than 0x3F.

Okay, after introducing these, let’s go back to the vertical axis of the count value that just made you confused. I will create a new two-dimensional coordinate system for you to see, and you will understand.

The value of T[6:0] is used as the maximum value of the counter, and the value of W[6:0] is used as the window judgment value. I’m not so confused now.

3. Timeout calculation

Tout is the WWDG timeout time (the dog is not fed)

Fwwdg is the clock source frequency of WWDG (maximum 36M)

4096 is the fixed prescaler coefficient of WWDG 2^WDGTB is the prescaler coefficient value set by the WWDG_CFR register

T[5:0] is the low 6 bits of WWDG counter, up to 63

3. The configuration of stm32cube is as shown below

Turn on the window watchdog, set the counter value to 0X7F, the window value to 0X5F, and the prescaler coefficient to 8. LED1 lights up when the program starts, turns off after 300ms, and then flips LED1 state every three seconds. . In the early wake-up interrupt service function, feed the dog and flip the LED2 state at the same time.

SYS

RCC

WWDG

GPIO (both B89 and B9 are configured high by default)

4. Code part

#include "main.h"
#include "wwdg.h"
#include "gpio.h"
void SystemClock_Config(void);
void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
{
HAL_WWDG_Refresh(hwwdg);//The count value is equal to 0x40 to enter the interrupt, and then feed the dog to prevent reset
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);

}
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration------------------------------------------------- ----------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

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

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  HAL_Delay(300);
  MX_WWDG_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
    HAL_Delay(3000);

  }
  /* USER CODE END 3 */
}

Comparison between independent watchdog and window watchdog