prj.conf文件的修改
CONFIG_WATCHDOG=y
main.c文件
#include <drivers/watchdog.h>
static void wdt_callback(const struct device *wdt_dev, int channel_id)
{
static bool handled_event;
if (handled_event) {
return;
}
wdt_feed(wdt_dev, channel_id);
LOG_INF("Handled things..ready to reset\n");
handled_event = true;
}
static const struct device *wdt;
static int wdt_channel_id;
static void watdog_init(void)
{
int err;
struct wdt_timeout_cfg wdt_config;
LOG_INF("Watchdog sample application\r\n");
wdt = device_get_binding("WDT");这个参数可以根据devicetree_unfixed.h文件找到
if (!wdt) {
LOG_INF("Cannot get WDT device\r\n");
return;
}
/* Reset SoC when watchdog timer expires. */
wdt_config.flags = WDT_FLAG_RESET_SOC;
/* Expire watchdog after 1000 milliseconds. */
wdt_config.window.min = 0U;
wdt_config.window.max = 10*1000U;//设置看门狗的最大喂狗时间,超过了就重启
/* Set up watchdog callback. Jump into it when watchdog expired. */
wdt_config.callback = wdt_callback;
wdt_channel_id = wdt_install_timeout(wdt, &wdt_config);
if (wdt_channel_id == -ENOTSUP) {
wdt_config.callback = NULL;
wdt_channel_id = wdt_install_timeout(wdt, &wdt_config);
}
if (wdt_channel_id < 0) {
LOG_INF("Watchdog install error\r\n");
return;
}
err = wdt_setup(wdt, 0);
if (err < 0) {
LOG_INF("Watchdog setup error\r\n");
return;
}
wdt_feed(wdt, wdt_channel_id);
}
void watdog_feed(void)
{
wdt_feed(wdt, wdt_channel_id);
}
int main()
{
watdog_init();
for(;;)
{
watdog_feed();
k_msleep(1000);
}
}
中间遇到的问题就是device_get_binding函数的参数问题,可以根据devicetree_unfixed.h中watchdog的labal查找到