看门狗实例 以及设置进空闲线程
#include "iwdg.h"
rt_device_t WdgDev;
static void idle_hook(void)
{
rt_device_control(WdgDev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
}
rt_int8_t WdgInit(void)
{
rt_err_t ret = RT_EOK;
rt_uint32_t timeout = WDT_DEV_TIMEOUT;
WdgDev = rt_device_find(WDT_DEV_NAME);
if(!WdgDev)
{
rt_kprintf("find %s failed!\n", WDT_DEV_NAME);
return RT_ERROR;
}
ret = rt_device_control(WdgDev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
if (ret != RT_EOK)
{
rt_kprintf("set %s timeout failed!\n", WDT_DEV_NAME);
return RT_ERROR;
}
ret = rt_device_control(WdgDev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
if (ret != RT_EOK)
{
rt_kprintf("start %s failed!\n", WDT_DEV_NAME);
return RT_ERROR;
}
rt_thread_idle_sethook(idle_hook);
return ret;
}
```c
#ifndef IWDG_H
#define IWDG_H
#include <rtthread.h>
#include <rtdevice.h>
#define WDT_DEV_NAME "wdt"
#define WDT_DEV_TIMEOUT 2
extern rt_device_t WdgDev;
rt_int8_t WdgInit(void);
#endif
rt_thread_idle_sethook(idle_hook); 设置空闲线程
static void idle_hook(void)
{
/* 在空闲线程的回调函数里喂狗 */
rt_device_control(WdgDev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
// rt_kprintf("feed the dog!\n ");
}
空闲线程具体实现