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 generally has one input, called kicking the dog/service the dog, and one output to the MCU’s RST pin. When the MCU is working normally, it periodically sends a signal to the watchdog input to reset the WDT. If the MCU fails to reset the WDT within the specified time (typically when the program crashes), the WDT timer overflows and sends a reset signal to the MCU, causing it to reset. This prevents the MCU from freezing. The watchdog’s purpose is to prevent the program from entering an infinite loop or crashing.

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. The setting takes effect immediately, clearing the previous count value and starting over.

Getting Remaining Time: Get the watchdog remaining time to monitor the watchdog’s actions.

High Precision: The watchdog has high precision time control capability.

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

Main Features

Initialize Watchdog

Before using the timer, you need to call the wm_drv_wdt_init() function to allocate resources for the watchdog, using the wm_device_t structure 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.

Start Watchdog

Call the function wm_drv_wdt_start() to start the watchdog. 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*.

Stop Watchdog

Call the function wm_drv_wdt_stop() to stop the watchdog. 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*.

Set Watchdog Period

Call the function wm_drv_wdt_set_counter_value() to set the watchdog 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 period, in microseconds. In this example, the watchdog period is set to 10 seconds.

Get Watchdog Period

Call the function wm_drv_wdt_get_counter_value() to get the watchdog 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 device of type wm_device_t*.

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

Get Watchdog Remaining Time

Call the function wm_drv_wdt_get_remaining_time() to get the watchdog 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 device of type wm_device_t*.

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

Feed the Dog

Call the function wm_drv_wdt_feed() to feed the dog. 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 Examples

For basic usage examples of WDT, please refer to: examples/peripheral/wdt

API Reference

For WDT related APIs, please refer to:

WDT API Reference