This is the documentation for the latest (main) development branch. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

WDT

Introduction

WDT (Watchdog Timer) is a timer circuit that typically features an input known as “kicking the dog” or “servicing the dog,” and an output connected to the RST terminal of the MCU. When the MCU is operating normally, it outputs a signal to the “kicking the dog” input at regular intervals to reset the WDT. If the WDT is not serviced within the specified time (usually when the program runs into an abnormal state), the WDT will timeout and send a reset signal to the MCU, causing it to reset and preventing it from freezing. The role of the watchdog timer is to prevent the program from entering an infinite loop or running abnormally.

Function List

  1. Initialize - Initialize the watchdog driver.

  2. Start WDT - Start the watchdog.

  3. Stop WDT - Stop the watchdog.

  4. Set WDT Period - Set the watchdog period.

  5. Get WDT Period - Get the watchdog period.

  6. Get WDT Remaining Time - Get the watchdog remaining time.

  7. Feed the Dog - Feed the watchdog.

Feature Overview

Setting and Getting Period Values: Set and get the watchdog period value,which takes effect immediately upon setting, clears the previous count value, and restarts.

Getting Remaining Time: Obtain the remaining time of the watchdog timer to observe its behavior.

High Precision: The watchdog timer possesses high-precision time control capabilities.

Interrupt: The timer supports interrupts.and When the watchdog is triggered, it sets the reboot reason and resets the system.

Main Features

Initialize the Watchdog Timer

Before using the timer, you need to call the wm_drv_wdt_init() function to allocate resources for the watchdog timer. The wm_device_t structure is used to receive the timer device identifier. Example:

wm_device_t *wdt_dev;
wdt_dev = wm_drv_wdt_init("wdt");

The first parameter specifies the device name.

Warning

After the watchdog is initialized, if wm_drv_wdt_deinit() is not called, calling wm_drv_wdt_init() again will return NULL.

Starting the Watchdog Timer

The function wm_drv_wdt_start() is called to start the watchdog timer. Example:

wm_device_t *wdt_dev;
wdt_dev = wm_drv_wdt_init("wdt");

wm_drv_wdt_start(wdt_dev);

The first parameter is a pointer to the timer device of type wm_device_t*.

Stopping the Watchdog Timer

The function wm_drv_wdt_stop() is called to stop the watchdog timer . Example:

wm_device_t *wdt_dev;
wdt_dev = wm_drv_wdt_init("wdt");

wm_drv_wdt_stop(wdt_dev);

The first parameter is a pointer to the timer device of type wm_device_t*.

Setting the Watchdog Timer Period

The function wm_drv_wdt_set_counter_value() is called to set the watchdog timer period in microseconds. Example:

wm_device_t *wdt_dev;
wdt_dev = wm_drv_wdt_init("wdt");

wm_drv_wdt_set_counter_value(wdt_dev, 1000 * 1000 * 10);

The first parameter is a pointer to the timer device of type wm_device_t*.

The second parameter is an unsigned 32-bit integer representing the watchdog timer period in microseconds. In this example, the watchdog period is set to 10 seconds.

Getting the Watchdog Timer Period

The function wm_drv_wdt_get_counter_value() is called to get the watchdog timer period value. Example:

wm_device_t *wdt_dev;
int counter_value = 0;

wdt_dev = wm_drv_wdt_init("wdt");
wm_drv_wdt_get_counter_value(wdt_dev, &counter_value);

The first parameter is a pointer to the watchdog timer device of type wm_device_t*.

The second parameter is the watchdog period value to be retrieved, of type int.

Getting the Watchdog Timer Remaining Time

The function wm_drv_wdt_get_remaining_time() is called to get the watchdog timer’s remaining time value. Example:

wm_device_t *wdt_dev;
int remain = 0;

wdt_dev = wm_drv_wdt_init("wdt");
wm_drv_wdt_get_remaining_time(wdt_dev, &remain);

The first parameter is a pointer to the watchdog timer device of type wm_device_t*.

The second parameter is the remaining time value to be retrieved, of type int.

Feeding the Watchdog Timer

The function wm_drv_wdt_feed() is called to “feed” the watchdog timer. Example:

wm_device_t *wdt_dev;
wdt_dev = wm_drv_wdt_init("wdt");

wm_drv_wdt_feed(wdt_dev);

The first parameter is a pointer to the timer device of type wm_device_t*.

Application Example

For a basic example of using the WDT, please refer to: examples/peripheral/wdt

API Reference

For WDT-related API references, please refer to:

WDT API Reference