idea6410看门狗watchdog移植及喂狗
来源:互联网 发布:淘宝售后纠纷案例分析 编辑:程序博客网 时间:2024/05/08 09:09
//--------------------------------------------------------------------------------------------
// 作者:longtian635241(longtian_huang@urbetter.com)
// 论坛ID:idea6410
// 版权:idea6410
// 平台:友坚idea6410开发板
// 发布日期:2012-11-22
// 最后修改:2012-11-22
//
//----------------------------------------------------------------------------------------------
linux-3.6.6其实已经有看门狗驱动了,即H:\VM share\linux-3.6.6\drivers\watchdog\s3c2410_wdt.c;只需配置就可用了!
【1】在内核中配置看门狗驱动
在内核源代码目录执行:make menuconfig,进入内核配置主菜单,依次选择进入如下子菜单:
Device Drivers --->
[*] Watchdog Timer Support --->
<*> S3C2410 Watchdog
/////////////////////////////////////////////////
在drivers/watchdog/s3c2410_wdt.c中进行修改:
- #define CONFIG_S3C2410_WATCHDOG_ATBOOT (1)
- #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
#define CONFIG_S3C2410_WATCHDOG_ATBOOT(1)#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME(15)
注:
- #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)//系统启动时不开启看门狗
- #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)//复位时间
///////////////////////////////////////////////////////////////可以不必修改!
启动显示:
s3c2410-wdt s3c2410-wdt: watchdog inactive, reset disabled, irq enabled
【2】关于打开和关闭看门狗
在看门狗驱动程序中,我们注意到有这样一个函数,注意其中的蓝色粗体部分字体:
#define PFX "s3c2410-wdt: "
#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
//这里表明看门狗的默认时间是15 秒,如果超过此时间系统将自行重启
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
size_t len, loff_t *ppos)
{
/*
* Refresh the timer.
*/
if (len) {
if (!nowayout) {
size_t i;
/* In case it was set long ago */
expect_close = 0;
for (i = 0; i != len; i++) {
char c;
if (get_user(c, data + i))
return -EFAULT;
if (c == 'V')
expect_close = 42;
}
}
s3c2410wdt_keepalive();
}
return len;
}
根据此代码,我们判定可以在打开看门狗设备(/dev/watchdog)之后不断的向看门狗随便写入写入一些数据以实现喂狗操作,但是,当写入“V“时,就可以关闭看门狗了。
原因分析:
open函数:
- static int s3c2410wdt_open(struct inode *inode, struct file *file)
- {
- if (test_and_set_bit(0, &open_lock))
- return -EBUSY;
- if (nowayout)
- __module_get(THIS_MODULE);
- expect_close = 0;
- /* start the timer */
- s3c2410wdt_start();
- return nonseekable_open(inode, file);
- }
static int s3c2410wdt_open(struct inode *inode, struct file *file){if (test_and_set_bit(0, &open_lock))return -EBUSY;if (nowayout)__module_get(THIS_MODULE);expect_close = 0;/* start the timer */s3c2410wdt_start();return nonseekable_open(inode, file);}
release函数:
- static int s3c2410wdt_release(struct inode *inode, struct file *file)
- {
- /*
- * Shut off the timer.
- * Lock it in if it's a module and we set nowayout
- */
- if (expect_close == 42)
- s3c2410wdt_stop();
- else {
- dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
- s3c2410wdt_keepalive();
- }
- expect_close = 0;
- clear_bit(0, &open_lock);
- return 0;
- }
static int s3c2410wdt_release(struct inode *inode, struct file *file){/* *Shut off the timer. * Lock it in if it's a module and we set nowayout */if (expect_close == 42)s3c2410wdt_stop();else {dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");s3c2410wdt_keepalive();}expect_close = 0;clear_bit(0, &open_lock);return 0;}
write函数:
- static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
- size_t len, loff_t *ppos)
- {
- /*
- * Refresh the timer.
- */
- if (len) {
- if (!nowayout) {
- size_t i;
- /* In case it was set long ago */
- expect_close = 0;
- for (i = 0; i != len; i++) {
- char c;
- if (get_user(c, data + i))
- return -EFAULT;
- if (c == 'V')
- expect_close = 42;
- }
- }
- s3c2410wdt_keepalive();
- }
- return len;
- }
static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,size_t len, loff_t *ppos){/* *Refresh the timer. */if (len) {if (!nowayout) {size_t i;/* In case it was set long ago */expect_close = 0;for (i = 0; i != len; i++) {char c;if (get_user(c, data + i))return -EFAULT;if (c == 'V')expect_close = 42;}}s3c2410wdt_keepalive();}return len;}
看门狗只能被一个进程打开,打开函数中先判断了一下,然后启动了看门狗;再看write函数,写入的如果是V则允许关闭看门狗,如果不是V仅仅喂狗一次;最后是release函数,如果允许关闭则关闭看门狗,如果不允许关闭,打印"Unexpectedclose, not stopping watchdog",喂狗一次。此时看门狗并没有关闭,所以系统会复位的,如果输入V则看门狗被关闭,这样系统就不复位了。
【3】测试看门狗
根据上面的分析,我们可以使用 echo 命令向/dev/watchdog 设备随便写入一些数据即可开启看门狗,比如:echo 0 > /dev/watchdog,如下:
[root@zxj /]#echo 0 > /dev/watchdog
s3c2410-wdt s3c2410-wdt: Unexpected close, not stopping watchdog
[root@zxj /]#
此时,如果静等 15 秒钟,系统将会自动重启,这样就证实了看门狗已经被开启了。如果 15 秒之内,我们不停地重复“喂狗”操作,也就是不停的使用echo 命令向看门狗写入数据,那么系统就不会重启。那么,如何停止看门狗呢?根据上面的分析,只要写入“V”就可以了。需要知道的是,但我们使用echo 命令向/dev/watchdog 写入数据的时候,同时也把“回车”给送进去了,因此可以这样操作:echo -n V >/dev/watchdog这里的“-n”意思是“去掉回车”,为了测试,你可以先输入:
echo 0 > /dev/watchdog
接着再输入:
echo –n V > /dev/watchdog
然后接着静等,过了好久,系统依然在正常运行,这就证明了看门狗已经被关闭了。
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <linux/watchdog.h>
- int main(int argc, char **argv){
- int fd;
- fd = open("/dev/watchdog",O_RDONLY);
- if(fd < 0){
- perror("/dev/watchdog");
- return -1;
- }
- for(;;){
- ioctl(fd, WDIOC_KEEPALIVE);
- sleep(3);
- }
- close(fd);
- return 0;
- }
#include <stdio.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <stdlib.h> #include <errno.h> #include <linux/watchdog.h> int main(int argc, char **argv){ int fd; fd = open("/dev/watchdog",O_RDONLY); if(fd < 0){ perror("/dev/watchdog"); return -1; } for(;;){ ioctl(fd, WDIOC_KEEPALIVE); sleep(3); } close(fd); return 0; }
编译:
- arm-linux-gcc wdt.c -o wdt
arm-linux-gcc -o Feed-watchdog Feed-watchdog.c
把Feed-watchdog拷贝到/bin/下,并修改root-2.6.38/etc/init.d/rcS文件,添加Feed-watchdog&这么一句,让系统启动后这个应用程序在后台运行。
- idea6410看门狗watchdog移植及喂狗
- 看门狗watchdog
- idea6410 tslib&qt移植
- 看门狗watchdog的理解
- 什么是看门狗(watchdog)
- 什么是看门狗(watchdog)
- 什么是看门狗(watchdog)
- watchdog(看门狗)
- s3c2410 watchdog (看门狗)转
- android -- WatchDog看门狗分析
- Linux 软件看门狗 watchdog
- android -- WatchDog看门狗分析
- android Watchdog 看门狗
- android -- WatchDog看门狗分析
- WatchDog Timer看门狗
- watchdog(看门狗)
- Linux 软件看门狗 watchdog
- WatchDog Timer看门狗
- 用Python实现队列
- ~备份 /etc/profile
- 游戏开发笔记三十五 站在巨人的肩膀上:游戏引擎导论
- 网上书店 第三天 jsp初步
- Cocos2d-x的多分辨率解决方案
- idea6410看门狗watchdog移植及喂狗
- java使用POI实现Excel文件的读取
- 康托展开
- C#中通过XmlDocument操作Xml文档(新建XML、新增节点、删除节点、显示文档内容)
- 如何灵活利用免费开源图标字体-IcoMoon篇
- QPushButton 控制
- MySql主从同步配置详解(1)
- <为知更新>java里的throws 和 try-catch 的用法
- 最简单的web server程序