nrf5340之看门狗的添加

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查找到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值