Watchdog
The phyCORE-RT1170 module includes an internal watchdog timer capable of resetting the system. Once started, the watchdog must be regularly refreshed (or “fed”) to prevent it from expiring and triggering a system reset.
Task Watchdog
The init_watchdog() function initializes the Zephyr Task Watchdog subsystem using a watchdog device defined in the devicetree under the watchdog0 alias. This prepares the system to allow threads to register themselves with the Task Watchdog for timeout supervision.
CONFIG_TASK_WDT_MIN_TIMEOUT
The CONFIG_TASK_WDT_MIN_TIMEOUT
configuration option sets the minimum allowed timeout (in milliseconds) for any thread registered with the Task Watchdog. If a thread attempts to register with a shorter timeout, the registration will fail. The default value in Zephyr is too low and must be explicitly set to at least 500
in the application’s prj.conf
file.
Example configuration:
CONFIG_TASK_WDT_MIN_TIMEOUT=500
Use of Task Watchdog API
The following Zephyr Task Watchdog API function is used in this context:
task_wdt_init(const struct device *dev)
This function initializes the Task Watchdog framework using the specified watchdog device. In this example, the device is retrieved from the devicetree via thewatchdog0
alias usingDEVICE_DT_GET()
. This step is required before any threads can be registered for monitoring. If initialization fails, an error is logged and the error code is returned; otherwise, a debug message confirms successful setup.
The devicetree alias mechanism ensures portability across boards. If the alias watchdog0
is missing or not enabled, compilation will fail with a clear error message, enforcing correct devicetree configuration.
Source Code
#include <zephyr/kernel.h>
#include <zephyr/task_wdt/task_wdt.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(watchdog, CONFIG_APP_LOG_LEVEL);
/*
* Get watchdog configuration from the devicetree sw0 alias. This is mandatory.
*/
#define WDOG0_NODE DT_ALIAS(watchdog0)
#if !DT_NODE_HAS_STATUS_OKAY(WDOG0_NODE)
#error "Unsupported board: watchdog0 devicetree alias is not defined"
#endif
static const struct device *const wdt = DEVICE_DT_GET(WDOG0_NODE);
int init_watchdog(void)
{
int ret;
ret = task_wdt_init(wdt);
if (ret < 0) {
LOG_ERR("Failed to init task watchdog: %d", ret);
return ret;
}
LOG_DBG("Watchdog initialized");
return 0;
}