全部文档
OneOS简介 硬件支持 编译构造工具 API参考文档 高级语言 用户编程手册 应用笔记 FAQ

Watchdog设备


简介

Watchdog又称为看门狗,硬件看门狗的实质是一个定时器,其输出连接到电路的复位端。使用看门狗可以使系统出现异常时能够自动复位。

开启看门狗后,定时器开始计数,一旦定时器溢出就会产生一个复位信号使系统复位。程序正常运行时,会定期清零定时器(俗称“喂狗”),避免定时器溢出。程序出现故障时,不能够按时进行喂狗,于是定时器溢出产生复位信号重启系统。

API 列表

应用程序通过系统提供的IO设备模型接口来访问Watchdog设备,接口如下:

函数 说明
os_device_find 根据Watchdog设备名称查找设备指针
os_device_init 初始化Watchdog设备
os_device_open 打开Watchdog设备
os_device_control 控制Watchdog设备
os_device_close 关闭Watchdog设备

os_device_find

该函数用于查找系统中已经注册的Watchdog设备,函数原型如下:

os_device_t *os_device_find(const char *name);
参数 说明
name Watchdog设备名称
返回 说明
设备指针 名为name的设备指针
OS_NULL 没有找到设备

os_device_init

该函数用于初始化Watchdog设备,函数原型如下:

os_err_t os_device_init(os_device_t *dev);
参数 说明
dev Watchdog设备指针
返回 说明
OS_EOK 设备初始化成功
OS_ENOSYS 初始化失败,dev为空
其他错误码 设备初始化失败

os_device_open

该函数并未做任何操作,仅用于配合os_device_close配对调用,函数原型如下:

os_err_t os_device_open(os_device_t *dev, os_uint16_t oflag)
参数 说明
dev 设备指针
oflag 模式标志(未使用)
返回值 说明
OS_EOK 设备打开成功

os_device_control

该函数发送控制命令以控制Watchdog设备,函数原型如下:

os_err_t os_device_control(os_device_t *dev, int cmd, void *arg)
参数 说明
dev 设备指针
cmd 控制命令
arg 可变参数
返回值 说明
OS_EOK 函数执行成功
OS_ENOSYS 函数执行失败,dev为空
其他错误码 函数执行失败

cmd控制命令取值如下

#define OS_DEVICE_CTRL_WDT_GET_TIMEOUT    (1) /* 获取溢出时间(秒) */
#define OS_DEVICE_CTRL_WDT_SET_TIMEOUT    (2) /* 设置溢出时间(秒) */
#define OS_DEVICE_CTRL_WDT_GET_TIMELEFT   (3) /* 获取剩余时间(秒) */
#define OS_DEVICE_CTRL_WDT_KEEPALIVE      (4) /* 喂狗 */
#define OS_DEVICE_CTRL_WDT_START          (5) /* 开启 */
#define OS_DEVICE_CTRL_WDT_STOP           (6) /* 关闭 */

os_device_close

该函数用于关闭Watchdog设备,需要同os_device_open配对使用,函数原型如下:

os_err_t os_device_close(os_device_t *dev)
参数 说明
dev 设备指针
返回值 说明
OS_EOK 设备关闭成功
OS_EBUSY 设备已经关闭,不允许重复操作
其他错误码 设备关闭失败

使用示例

#include <os_task.h>
#include <os_device.h>
#include <os_errno.h>
#include <os_idle.h>
#include <watchdog/watchdog.h>
#include <string.h>

#ifdef OS_USING_WDT

#ifdef OS_USING_SHELL
#include <shell.h>
#endif

#define WDT_DEVICE_NAME "wdt" 
static os_device_t *wdg_dev; 

static int wdt_test(int argc, char *argv[])
{
    os_err_t ret = OS_EOK;
    os_uint32_t timeout = 10; 
    char device_name[OS_NAME_MAX];
    int count = 0;
    if (argc == 2)
    {
        strncpy(device_name, argv[1], OS_NAME_MAX);
    }
    else
    {
        strncpy(device_name, WDT_DEVICE_NAME, OS_NAME_MAX);
    }

    /* Find Watchdog device */
    wdg_dev = os_device_find(device_name);
    if (!wdg_dev)
    {
        os_kprintf("find %s failed!\n", device_name);
        return OS_ERROR;
    }
    /* Initialise Watchdog device */
    ret = os_device_init(wdg_dev);
    if (ret != OS_EOK)
    {
        os_kprintf("initialize %s failed!\n", device_name);
        return OS_ERROR;
    }
    /* Set timeout */
    ret = os_device_control(wdg_dev, OS_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
    if (ret != OS_EOK)
    {
        os_kprintf("set %s timeout failed!\n", device_name);
        return OS_ERROR;
    }
    /* Start Watchdog */
    ret = os_device_control(wdg_dev, OS_DEVICE_CTRL_WDT_START, OS_NULL);
    if (ret != OS_EOK)
    {
        os_kprintf("start %s failed!\n", device_name);
        return OS_ERROR;
    }
    /* Keep Watchdog alive */
    os_device_control(wdg_dev, OS_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
    while(count < 11)
    {
        os_task_mdelay(1000);
        count++;
        os_kprintf("watch dog keep alive for :%ds\n",count);
        os_device_control(wdg_dev, OS_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
    }
    /* Stop Watchdog */
    count = 0;
    os_kprintf("\n");
    while(1)
    {
        os_kprintf("watch dog stop feed for :%d.%ds\n",count/10,count%10);
        os_task_mdelay(100);
        count++;
    }

    return ret;
}
SH_CMD_EXPORT(wdt_test,wdt_test, "test Independent watchdog!");
#endif /* OS_USING_RTC */

运行结果如下:

sh>wdt_test
watch dog keep alive for :1s
watch dog keep alive for :2s
watch dog keep alive for :3s
watch dog keep alive for :4s
watch dog keep alive for :5s
watch dog keep alive for :6s
watch dog keep alive for :7s
watch dog keep alive for :8s
watch dog keep alive for :9s
watch dog keep alive for :10s
watch dog keep alive for :11s

watch dog stop feed for :0.0s
watch dog stop feed for :0.1s
watch dog stop feed for :0.2s
watch dog stop feed for :0.3s
watch dog stop feed for :0.4s
.... ...
watch dog stop feed for :10.0s

results matching ""

    No results matching ""

    返回顶部