(a40i)嵌入式Linux 看门狗使用

2020-04-07  本文已影响0人  JalynFang

简介

        嵌入式系统或设备中,如果发生故障(宕机),无法及时对故障作出反应。为了使系统在异常情况下能自动恢复,一般都会引入看门狗。
        看门狗电路是一种电子计时器,其用于检测和恢复设备故障。当看门狗启动后,计数器开始自动计数,经过一定时间计数器溢出就会对CPU产生一个复位信号使系统重启。系统正常运行时,需要在看门狗允许的时间间隔内对看门狗计数器清零也即喂狗,不让复位信号产生。
        看门狗是 Linux 系统一个重要的机制,其目的是监测系统运行的情况,一旦出现锁死,死机的情况,能及时重启设备。

开发环境介绍

使能看门狗驱动

Step1. start the Linux Kernel Configuration tool:
make ARCH=arm menuconfig
Step2. Select Device Drivers from the main menu.
...                                                                                        
 Device Drivers  --->                                                          
 File systems  --->                                                           
...
Step3. Select Watchdog Timer Support as shown here:
...
 [*] Watchdog Timer Support  --->                                                  
         Sonics Silicon Backplane  --->  
...
Step4. Select SUNXI Watchdog as shown here:
--- Watchdog Timer Support 
[*]   WatchDog Timer Driver Core
[*]   Disable watchdog shutdown on close
< >   Software watchdog 
<*>   SUNXI Watchdog

[Disable watchdog shutdown on close]选项:当编译内核的时候这个选项被设置为Y,则一旦watchdog被启动,则将没有办法能够停止。这样,则当watchdog守护进程崩溃的时候,系统仍将在超时后重启。

Step5. 添加设备树节点
    wdt: watchdog@01c20ca0 {
         compatible = "allwinner,sun4i-wdt";
         reg = <0x0 0x01c20c90 0 0x18>;
     };

compatible属性需要根据驱动中来:

static const struct of_device_id sunxi_wdt_dt_ids[] = {
    { .compatible = "allwinner,sun4i-wdt", .data = &sun4i_wdt_reg },
    { .compatible = "allwinner,sun6i-wdt", .data = &sun6i_wdt_reg },
    { .compatible = "allwinner,sun8i-wdt", .data = &sun6i_wdt_reg },
    { .compatible = "allwinner,sun50i-wdt", .data = &sun6i_wdt_reg },
    { /* sentinel */ }
};

应用测试

        通常一个用户空间守护进程会在正常的时间间隔内通过/dev/watchdog特殊设备文件来通知内核的watchdog驱动,用户空间仍然正常。当这样的一个通知发生时,驱动通常会告诉硬件watchdog一切正常,然后watchdog应该再等待一段时间来复位系统。如果用户空间出问题(RAM错误,内核bug等),则通知将会停止,然后硬件watchdog将在超时后复位系统。

        所有的设备驱动都支持的基本的操作模式,一旦/dev/watchdog被打开,则watchdog激活,并且除非喂狗,否则将在一段时间之后重启,这个时间被称为timeout或margin。最简单的喂狗方法就是写一些数据到设备。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/watchdog.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#define WATCHDOGDEV "/dev/watchdog0"
#define MIN_TIMEOUT 1   //(in secs)
#define MAX_TIMEOUT 16  //(in secs)
#define MIN_SLEEP   1   //(in secs)

#define IN_RANGE(val, max, min) \
    ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))

void print_usage(FILE * stream, const char *app_name, int exit_code);

int main(int argc, const char *argv[])
{
    int fd = -1;    /* File handler for watchdog */
    int timeout;    /* Watchdog timeout interval (in secs) */
    int sleep_sec;  /* Feed the dog time(in secs) */
    int mode = 0;       /* Feed the dog way */
    
    if (argc < 3) {
        print_usage(stdout, argv[0], EXIT_FAILURE);
        return 1;
    }
    
    timeout = IN_RANGE(atoi(argv[1]), MAX_TIMEOUT, MIN_TIMEOUT);
    sleep_sec = IN_RANGE(atoi(argv[2]), INT_MAX, MIN_SLEEP);
    mode = atoi(argv[3]);
    printf("Starting wdt_driver (timeout: %d, sleep: %d, mode: %s)\n",
           timeout, sleep_sec, (mode == 0) ? "ioctl" : "write");
    
    /*打开设备文件*/
    fd = open(WATCHDOGDEV, O_WRONLY);
    if (fd == -1) {
        perror("watchdog");
        exit(EXIT_FAILURE);
    }
    
    printf("Trying to set timeout value=%d seconds\n", timeout);
    ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
    printf("The actual timeout was set to %d seconds\n", timeout);
    ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
    printf("Now reading back -- The timeout is %d seconds\n", timeout);
    
    while (1) 
    {
        if ( 0==mode ) {
            //使用ioctl()方法喂狗
            ioctl(fd, WDIOC_KEEPALIVE, 0); 
        } else {
            //使用write()方法喂狗
            write(fd, "\0", 1);           
        }
        
        sleep(sleep_sec);
    }
    return 0;
}

void print_usage(FILE * stream, const char *app_name, int exit_code)
{
   fprintf(stream, "Usage: %s <timeout> <sleep> <mode> \n", app_name);
   fprintf(stream,
      "     timeout:    value in seconds to cause wdt timeout/reset.\n"
      "     sleep  :    value in seconds to service the wdt.\n"
      "     mode   :    0 - Service wdt with ioctl(), 1 - with write() \n"
      "  The default device file is '/dev/watchdog0'\n");
 
   exit(exit_code);
}

备注:应用层上,dev/watchdog 下会有watchdog 和 watchdog0;代表的是同一个硬件,即使他们的字符设备的major和minor 不一样。(之所以要注册/dev/watchog 是为来兼容老的接口)



参考:
https://processors.wiki.ti.com/index.php/AM335x_PSP_WDT_Driver_User_Guide
https://embeddedfreak.wordpress.com/2010/08/23/howto-use-linux-watchdog/

上一篇下一篇

猜你喜欢

热点阅读