zoukankan      html  css  js  c++  java
  • 看门狗watchdog

       看门狗在嵌入式系统开发中占据重要的地位,管理系统的工作状态。在这里本人muge0913在参考别人的基础上,实现了mini6410看门狗的移植本文章仅供技术交流请勿商用,转载请标明地址:

    http://blog.csdn.net/muge0913/article/details/7063001

       在mini6410中看门狗驱动文件为linux2.6.38/drivers/watchdog/s3c2410_wdt.c

       在mini6410中linux系统默认看门狗是不开机启动,但是我们可以向/dev/watchdog写入数据来启动或关闭看门狗。

    如:echo 0 >/dev/watchdog

       echo这个命令启动的作用是先打开文件,再写入内容,然后关闭。也就是open->write->release。

    运行效果:

    一段时间后系统会自动重启。

    如果执行:

      echo 0 >/dev/watchdog

      echo V >/dev/watchdog

    系统侧不会重启。

    原因分析:

    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);
    }

    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;
    }

    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;
    }

      看门狗只能被一个进程打开,打开函数中先判断了一下,然后启动了看门狗;再看write函数,写入的如果是V则允许关闭看门狗,如果不是V仅仅喂狗一次;最后是release函数,如果允许关闭则关闭看门狗,如果不允许关闭,打印"Unexpectedclose, not stoppingwatchdog",喂狗一次。此时看门狗并没有关闭,所以系统会复位的,如果输入V则看门狗被关闭,这样系统就不复位了。

     

     

    看门狗在mini6410上的移植过程:

    首先配置:make menuconfig


       在drivers/watchdog/s3c2410_wdt.c中进行修改:

    #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)//复位时间
    

       设置成系统启动就启动看门狗,并且看门狗到期时间为15s。这样系统复位后每15s系统就会复位一次,所以我们在用户空间进行喂狗,驱动中的那个中断函数是当看门狗作为定时器时用的,所以没有实现喂狗,所以只能在用户程序中喂狗,下面是源码:

    #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

       把wdt拷贝到root-2.6.30.4/sbin/下,并修改root-2.6.38/etc/init.d/rcS文件,添加wdt&这么一句,让系统启动后这个应用程序在后台运行。

    #! /bin/sh
    
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:
    runlevel=S
    prevlevel=N
    umask 022
    export PATH runlevel prevlevel
    
    #
    #	Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
    #
    trap ":" INT QUIT TSTP
    /bin/hostname FriendlyARM
    
    [ -e /proc/1 ]    || /bin/mount -n -t proc  none /proc
    [ -e /sys/class ] || /bin/mount -n -t sysfs none /sys
    [ -e /dev/tty ]   || /bin/mount    -t ramfs none /dev
    /bin/mount -n -t usbfs none /proc/bus/usb
    
    echo /sbin/mdev > /proc/sys/kernel/hotplug
    /sbin/mdev -s
    /bin/hotplug
    # mounting file system specified in /etc/fstab
    mkdir -p /dev/pts
    mkdir -p /dev/shm
    /bin/mount -n -t devpts none /dev/pts -o mode=0622
    /bin/mount -n -t tmpfs tmpfs /dev/shm
    /bin/mount -n -t ramfs none /tmp
    /bin/mount -n -t ramfs none /var
    mkdir -p /var/empty
    mkdir -p /var/log
    mkdir -p /var/lock
    mkdir -p /var/run
    mkdir -p /var/tmp
    
    /sbin/hwclock -s
    
    syslogd
    /etc/rc.d/init.d/netd start
    echo "                        " > /dev/tty1
    echo "Starting networking..." > /dev/tty1
    sleep 1
    /etc/rc.d/init.d/httpd start
    echo "                        " > /dev/tty1
    echo "Starting web server..." > /dev/tty1
    sleep 1
    /etc/rc.d/init.d/leds start
    echo "                        " > /dev/tty1
    echo "Starting leds service..." > /dev/tty1
    echo "                        "
    sleep 1
    
    echo "                        " > /dev/tty1
    /etc/rc.d/init.d/alsaconf start
    echo "Loading sound card config..." > /dev/tty1
    echo "                        "
    
    /sbin/ifconfig lo 127.0.0.1
    /etc/init.d/ifconfig-eth0
    
    /sbin/wdt&
    
    /bin/qtopia &
    echo "                                  " > /dev/tty1
    echo "Starting Qtopia, please waiting..." > /dev/tty1
    
    



    http://www.linuxso.com/linuxbiancheng/12702.html



    某CSDN帖子

    /dev/watchdog是一个字符设备节点,简单点可以理解为linux下的一个文件,在程序中使用看门狗的过程大致分为以下几步:
    1.打开年看门狗“文件”;
    2.设置超时时间;
    3,周期性向这个文件写入字符(喂狗);
    若程序出现异常退出,无法在喂狗周期内写入字符,则系统将自动复位重启。我写的一个例子代码如下:

    C/C++ code
    #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/watchdog.h> #define WDT "/dev/watchdog" int main() { int wdt_fd = -1;//看门狗文件标识 int timeout = 10;//超时时间为10s wdt_fd = open(WDT,O_WRONLY); if(wdt_fd == -1) { printf("----------Fail to open "WDT"!\n"); } ioctl(wdt_fd,WDIOC_SETTIMEOUT,&timeout); ioctl(wdt_fd,WDIOC_GETTIMEOUT,&timeout); while(1) { write(wdt_fd,"\0",1); printf("feet dog***********\n"); sleep(9); } return 0; }





  • 相关阅读:
    canvas性能优化——离屏渲染
    event.target 和 event.currentTarget 的区别
    Electron 主进程和渲染进程互相通信
    谈谈 JS 垃圾回收机制
    【Vue】Vue中render函数用到的with(this)中with的用法及其优缺点
    Java递归读取文件路径下所有文件名称并保存为Txt文档
    Java读取Excel指定列的数据详细教程和注意事项
    Sybase ASE无响应的又一个情况
    AWR报告导出的过程报ORA-06550异常
    如何借助浏览器Console使用Js进行定位和操作元素
  • 原文地址:https://www.cnblogs.com/zhiweiyouzhishenghuo/p/5005512.html
Copyright © 2011-2022 走看看