RK3399 watch dog

简介: RK3399 watch dog

内核选中红框部分,确保有/dev/watchdog

include <errno.h>

include <fcntl.h>

include <stdlib.h>

include <string.h>

include <unistd.h>

include "stdio.h"

include <unistd.h> / System V /

include <sys/ioctl.h> / BSD and Linux /

include <stropts.h> / XSI STREAMS /

include <linux/watchdog.h>

define DEV_NAME "/dev/watchdog"

int fd;

/*

  • @brief 初始化看门狗
  • @param interval 喂狗时间
  • @param margin 喂狗间隔保留时间
  • @return int
  • @create at 2019.09.04 by Eric.Xi

*/
int watch_dog_init(int interval,int margin)
{

 printf("started (interval %d, margin %d)!\n", interval, margin);

fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
if (fd == -1) {
    printf("Failed to open %s: %s\n", DEV_NAME, strerror(errno));
    return 1;
}

int timeout = interval + margin;
int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
if (ret) {
    printf("Failed to set timeout to %d: %s\n", timeout, strerror(errno));
    ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
    if (ret) {
        printf("Failed to get timeout: %s\n", strerror(errno));
    } else {
        if (timeout > margin) {
            interval = timeout - margin;
        } else {
            interval = 1;
        }
        printf("Adjusted interval to timeout returned by driver:"
                " timeout %d, interval %d, margin %d\n",
                timeout, interval, margin);
    }
}
return 0;

}

/*

  • @brief 喂看门狗
  • @param interval 喂狗时间
  • @param fd 看门狗句柄
  • @return int
  • @create at 2019.09.04 by Eric.Xi

*/
int watch_dog_feed(int fd, int interval)
{

write(fd, "", 1);
sleep(interval);
return 0;

}

/*

  • @brief 看门狗使用demo
  • @return int
  • @create at 2019.09.04 by Eric.Xi

*/
int main(int argc, char **argv)
{

int interval = 1;
int margin =2;
int ret;
ret = watch_dog_init(interval,margin);
if(ret<0){
    return -1;
}


    while (true) {
        watch_dog_feed(fd ,interval);
     printf("------feed--------\n");
    
    }

}

相关文章
|
7月前
|
设计模式 缓存
proxy与watch的关系【了解】
proxy与watch的关系【了解】
26 0
|
7月前
|
资源调度 前端开发
Error_ watch path ENOSPC
Error_ watch path ENOSPC
43 0
报错AttributeError: Can‘t pickle local object ‘Worker.__init__.<locals>.<lambda>‘解决办法
报错AttributeError: Can‘t pickle local object ‘Worker.__init__.<locals>.<lambda>‘解决办法
520 0
|
Arthas 缓存 监控
Watch 命令的参数介绍 | 学习笔记
快速学习 Watch 命令的参数介绍
Watch 命令的参数介绍 | 学习笔记
|
Scala
Scala Lazy 惰性加载变量 你知道吗?
Scala Lazy 惰性加载 原写法;
122 0
|
前端开发
Python----魔法函数__getattr__/__setattr__/__delattr__/__getattribute__的用法
Python----魔法函数__getattr__/__setattr__/__delattr__/__getattribute__的用法
141 0
computed和watch的用法和区别
computed和watch的用法和区别