Skip to content

Contributed by 爱笑, organized by Ai-Thinker

[Ai-WB2 Getting Started] Watchdog Usage

A watchdog, also called a watchdog timer, is a hardware timing device. When some error occurs in the main program of the system and the count value of the watchdog timer is not cleared in time, the watchdog timer sends a reset signal to the system, restoring it from a stalled state to normal operation. This article describes in detail how to use the watchdog timer module of the Ai-WB2. 1. Watchdog Timer (WDG) Introduction The BL602 chip contains a set of watchdog counters. Unpredictable software or hardware behavior may cause the application to malfunction, and the watchdog timer helps the system recover from such conditions. If the current stage exceeds the preset time without feeding the watchdog or disabling the watchdog timer, an interrupt or a system reset can be triggered as configured. Its functional block diagram is as follows:

The watchdog timer clock has 3 choices:

Click to expand full code
c
**·** Fclk-系统主时钟
**·** 32K-32K时钟
**·** Xtal-外部晶振

Each counter has its own 8-bit divider. The selected clock can be divided by 1-256 through the APB. Specifically, setting it to 0 means no division, setting it to 1 means division by 2, and so on, with a maximum division factor of 256. The counter uses the divided clock as the counting cycle unit and increments by 1 every counting cycle. How the Watchdog Timer Works The watchdog timer contains a counter and a comparator. The counter counts up from 0; if the counter is reset (feed the watchdog), it starts counting up from 0 again. When the counter value matches the comparator value, a compare interrupt signal or a system reset signal can be generated, and the user can choose one of them as needed. The watchdog counter increments by 1 in each counting cycle unit, and software can clear the watchdog counter to zero at any time through the APB. Taking the comparator value as 6, the working sequence of the watchdog is shown in the figure below:

Watchdog Timer Alarm Flow Each counter can be configured with a set of compare values. When the software fails to clear the watchdog counter to zero in time due to a system error, causing the watchdog counter to exceed the compare value, a watchdog alarm is triggered. There are two alarm methods: the first is to notify the software through an interrupt so that necessary measures can be taken, and the second is to enter the system watchdog reset. When the watchdog reset is triggered, the system reset controller is notified and preparations are made before the system reset; when everything is ready, the system watchdog reset is entered. It is worth noting that software can read the WSR register via the APB to know whether a watchdog system reset has occurred.

2. Watchdog Timer Driver API Introduction The HOSAL layer driver API for the WDG is in the file components/platform/hosal/include/hosal_wdg.h. The commonly used API functions are as follows: · int hosal_wdg_init(hosal_wdg_dev_t*wdg): initializes the watchdog timer. Its parameters are described as follows: · wdg: the watchdog timer device object, defined as follows:

Click to expand full code
c
typedef struct {
uint8_t       port;   /**< wdg 端口 */
hosal_wdg_config_t  config; /**< wdg 配置 */
void         *priv;   /**< 用户自定义数据 */
} hosal_wdg_dev_t;

hosal_wdg_config_t is defined as follows:

Click to expand full code
c
typedef struct {
uint32_t timeout; /*!<看门狗定时器超时,以毫秒为单位*/
} hosal_wdg_config_t;
Click to expand full code
c
**·** 返回值:成功时返回0;否则返回非零值
**·** void hosal_wdg_reload(hosal_wdg_dev_t *wdg):看门狗定时器重载,即喂狗。参数说明如下:
**·** wdg:看门狗定时器设备对象
**·** 返回值:无
**·** int hosal_wdg_finalize(hosal_wdg_dev_t *wdg):看门狗定时器销毁,释放相关资源。参数说明如下:
**·** wdg:看门狗定时器设备对象
**·** 返回值:成功时返回0;否则返回非零值
**三:看门狗定时器使用实例**

The example code is as follows:

Click to expand full code
c
#include <stdio.h>
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
#include <stdbool.h>
#include <hosal_wdg.h>
#include <blog.h>
#include "bl602_dma.h"
#define TAG "wdg_demo"
hosal_wdg_dev_t wdg;
void wdg_init(void){
/* wdg 端口设置 */
wdg.port = 0;
/* 最大超时时间 65532/16 ms */
wdg.config.timeout = 3000;
/* 初始化WDG */
hosal_wdg_init(&wdg);
}
void wdg_task(void){
printf("wdg task start...\r\n");
while(true){
// 喂狗
hosal_wdg_reload(&wdg);
printf("feed wdg\r\n");
vTaskDelay(2000);
}
}
void counter_task(void){
size_t counter = 0;
printf("counter task start...\r\n");
while(true){
printf("counter task:counter = %d\r\n",counter++);
vTaskDelay(500);
}
}
void main(void) {
wdg_init();
xTaskCreate(counter_task, "counter_task", 1024, NULL, 15, NULL);
xTaskCreate(wdg_task, "wdg_task", 1024, NULL, 15, NULL);
}

Released under the MIT License. Build Time 2026-09-11 14:52:23