STM32CubeMX series|window watchdog

Window watchdog

1. Introduction to Window Watchdog

Window watchdog (WWDG) is usually used to monitor software failures caused by external disturbances or unforeseen logic conditions that the application deviates from the normal operating sequence; unless the value of the down counter is refreshed before the T6 bit becomes 0 When the watchdog circuit reaches the preset time period, it will generate an MCU reset. Before the down counter reaches the window register value, if the 7-bit down counter value is refreshed, an MCU reset will also be generated. Therefore, the down counter needs to be refreshed in a limited time window. Its main features are:

  • Programmable free running decrement calculator
  • Conditional reset: When the value of the down counter is less than 0x40, a reset occurs; when the down counter is reloaded outside the window, a reset occurs
  • If the watchdog is enabled and interrupts are enabled, an early wake-up interrupt (EWI) is generated when the down counter is equal to 0x40, which can be used to reload the counter to avoid WWDG reset

Watchdog block diagram

Insert picture description here
Window watchdog timing diagram

Insert picture description here
In the above figure, T[6:0] is the counter of the window watchdog, W[6:0] is the upper window of the window watchdog, and the lower window is a fixed value (0x40)
. The frequency of the WWDG counter is: PCLK1/( 4096 * counter prescaler value), because the PCLK1 clock frequency is too high, divide by 4096
WWDG timeout time: (4096 * counter prescaler value) * (T[5:0]+1)/PCLK1, here T[ 5:0]+1 is T[6:0]-0x3F

2. Hardware design

Use USART1 to print debugging information, PC0 to indicate whether the program is reset

Insert picture description here

3. Software design

3.1 STM32CubeMX settings
  • RCC set external HSE, clock is set to 72M; WWDG clock is mounted on APB1
  • PC0 is set to GPIO push-pull output mode, pull-up, high-speed, and the default output level is high
  • USART1 is selected as the asynchronous communication mode, the baud rate is set to 115200Bits/s, the transmission data length is 8Bit, no parity, 1 stop bit

Insert picture description here

  • Activate WWDG, the counter prescaler value is set to 8, the window register is set to W[6:0] = 0x5A, the decrement counter refresh value is set to T[6:0] = 0x7F; according to the formula, the timeout time is (4096 * 8 )*(127-63)/ 36MHz = 53.8ms; enable EWI interrupt, open window watchdog interrupt in NVIC settings

Insert picture description here

  • Enter the project name, select the project path (no Chinese), select MDK-ARM V5; check Generated periphera initialization as a pair of'.c/.h' files per IP; click GENERATE CODE to generate the project code
3.2 MDK-ARM programming
  • You can see the initialization function of the window watchdog in the wwdg.c file
void MX_WWDG_Init(void){
    
    
  hwwdg.Instance = WWDG;
  hwwdg.Init.Prescaler = WWDG_PRESCALER_8;
  hwwdg.Init.Window = 0x5a;
  hwwdg.Init.Counter = 0x7f;
  hwwdg.Init.EWIMode = WWDG_EWI_ENABLE;
  if (HAL_WWDG_Init(&hwwdg) != HAL_OK){
    
    
    Error_Handler();
  }
}

Find the prototype of the weak symbol early wakeup interrupt function, and customize the callback function in wwdg.c
__weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)

void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg){
    
    
	HAL_WWDG_Refresh(hwwdg);	//在早期唤醒中断函数中喂狗
}
  • Write the code in the main function. If the dog is successfully fed in the early wake-up interrupt function, it will be printed every 1s in the while loop
int main(void){
    
     
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_WWDG_Init();
  /* USER CODE BEGIN 2 */
  printf("\r\n***** AndyXi WWDG test program *****\r\n");
  HAL_Delay(10);
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,GPIO_PIN_RESET);
  /* USER CODE END 2 */
  while (1){
    
    
 	printf("\r\nHello WWDG...\r\n");
	HAL_Delay(10);
  }
}

4. Download verification

  • If the dog is successfully fed in the early wake-up interrupt function, the program will not be reset. The message will be printed every 1s in the while loop, and LED1 will always be on.

Insert picture description here

  • If the feed dog function in the early wake-up interrupt function is commented out, the program will always be reset and LED1 flashes

Insert picture description here

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108583716