From 39e6dce60286ab3b5dec9a4ae2da571868e55a59 Mon Sep 17 00:00:00 2001 From: leeboby Date: Fri, 28 Aug 2020 20:47:16 +0800 Subject: [PATCH] Add watchdog.c to examples --- examples/Makefile | 7 ++- examples/watchdog.c | 115 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100755 examples/watchdog.c diff --git a/examples/Makefile b/examples/Makefile index 97ee0ee..923a8e7 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -49,7 +49,8 @@ SRC = blink.c blink8.c blink12.c \ lowPower.c \ max31855.c \ rht03.c \ - w25q64_test.c oled_demo.c spiSpeed.c + w25q64_test.c oled_demo.c spiSpeed.c \ + watchdog.c OBJ = $(SRC:.c=.o) @@ -158,6 +159,10 @@ w25q64_test: w25q64_test.o $Q echo [link] $Q $(CC) -o $@ w25q64_test.o $(LDFLAGS) $(LDLIBS) +watchdog: watchdog.o + $Q echo [link] + $Q $(CC) -o $@ watchdog.o $(LDFLAGS) $(LDLIBS) + oled_demo: oled_demo.o $Q echo [link] $Q $(CC) -o $@ oled_demo.o $(LDFLAGS) $(LDLIBS) diff --git a/examples/watchdog.c b/examples/watchdog.c new file mode 100755 index 0000000..c8e4a2a --- /dev/null +++ b/examples/watchdog.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct watchdog_info{ + unsigned int options; //options the card/driver supprots 19 + unsigned int firmware_version; //firmcard version of the card + unsigned char identity[32]; //identity of the board 21 + }; + +#define WATCHDOG_IOCTL_BASE 'W' +#define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info) +#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int) +#define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int) //27 +#define WDIOS_DISABLECARD 0x0001 +#define WDIOS_ENABLECARD 0x0002 +#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int) +#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int) + +int Getch (void) //无回显的从屏幕输入字符,来达到喂狗的目的 + +{ + + int ch; + struct termios oldt, newt; //终端设备结构体 + tcgetattr(STDIN_FILENO, &oldt); //获得终端属性 + newt = oldt; + newt.c_lflag &= ~(ECHO|ICANON); //设置无回显属性 + tcsetattr(STDIN_FILENO, TCSANOW, &newt); //设置新的终端属性 + ch = getchar(); //从键盘输入一个数据 + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); //恢复终端设备初始设置 + return ch; + +} + //suspend some seconds +int zsleep(int millisecond) + +{ + unsigned long usec; + usec=1000*millisecond; + usleep(usec); //usleep(1)睡眠一微秒(10E-6),这里也就是0.1s +} +int Init() +{ + int fd; + //open device file + fd = open("/dev/watchdog",O_RDWR); //打开看门狗设备 + if(fd < 0) + { + printf("device open fail\n"); + return -1; + } + printf("open success\n"); + return fd; +} + +int main(int argc,char **argv) +{ + int fd,ch; + int i,j; + char c; + struct watchdog_info wi; + if(argc != 2){ + printf("Usage : ./watchdog 10\n"); + return -1; + } + fd=Init(); //打开终端看门狗设备 + ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD); + //读板卡信息,但不常用 + ioctl(fd,WDIOC_GETSUPPORT,&wi); + printf("options is %d,identity is %s\n",wi.options,wi.identity); + //读看门狗溢出时间 + + printf("put_usr return,if 0,success:%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i)); + + printf("The old reset time is: %d\n", i); + //关闭 + i=WDIOS_DISABLECARD;//WDIOC_SETOPTIONS=0X0001 + printf("return ENOTTY,if -1,success:%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i)); + //打开 + i=WDIOS_ENABLECARD;//WDIOS_ENABLECARD 0x0002 + printf("return ENOTTY,if -1,success:%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i)); + i=atoi(argv[1]); + printf("put_user return,if 0,success:%d\n",ioctl(fd,WDIOC_SETTIMEOUT,&i)); + //读新的设置时间 + + printf("put_usr return,if 0,success:%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i)); + + + while(1) + { + zsleep(100); + if((c=Getch())!=27){ + //输入如果不是ESC,就喂狗,否则不喂狗,到时间后系统重启 + printf("keep alive \n"); + ioctl(fd,WDIOC_KEEPALIVE,NULL); + //write(fd,NULL,1); //同样是喂狗 + + } + } + close(fd); //关闭设备 + return 0; +}