Watchdog使用参考


1. 概述

Watchdog采用标准的linux 框架提供驱动,上层应用可以设定time out时间,自己来keep alive。

Watchdog默认是关闭的,客户可自行决定是否开启。如果开启建议在主线程中操作,如果在其他线程中操作,Watchdog会随着线程的关闭而关闭。

2. WATCHDOG寄存器

Watchdog Register (Bank = 30)
Index (Absolute) Mnemonic Bit Description
00h (003000h) REG003000 7:0 Default : 0x00 Access : W
- 7:1 Reserved.
WDT_CLR 0 set: to restart Watchdog
02h (003004h) REG003004 7:0 Default : 0x00 Access : R/W
- 7:1 Reserved.
WDT_RST_FLAG 0 assert: Watchdog reset has been occurred.
set: to clear
02h (003005h) REG003005 7:0 Default : 0x09 Access : R/W
WDT_RST_LEN 7:0 length of Watchdog reset.
03h REG003006 15:0 Default : 0xFFFF Access : R/W
WDT_INT 15:0 Watchdog interrupt period; interrupt asserts when "Watchdog counter [31:16]" is equal to WDT_INT and "Watchdog counter[15:0]" is equal to 0x0000.
04h REG003008 15:0 Default : 0xFFFF Access : R/W
WDT_MAX[15:0] 15:0 Watchdog period maximum value bit[15:0].
05h REG00300A 15:0 Default : 0xFFFF Access : R/W
WDT_MAX[32:16] 15:0 Watchdog period maximum value bit[31:16].

3. WATCHDOG控制

3.1. 打开WATCHDOG

  1. kernel增加配置

    Device Drivers --->
        [*] SStar SoC platform drivers --->
            <*>  watchdog driver
    
  2. infinity7.dtsi确认有watchdog节点

    1.  watchdog: watchdog {  
    2.      compatible = "sstar, infinity-wdt";  
    3.      reg = <0x0 0x1F006000 0x0 0x40>;  
    4.      status="ok";  
    5.  };
    
  3. 保证以上两点后如何确认在kernel中有打开watchdog的支持?

    进入shell后,在/dev路径下会有watchdog设备节点:/dev/watchdog。

    打开/dev/watchdog设备,watchdog将被启动。

    参考代码如下:

    1.  int wdt_fd = -1;
    2.  wdt_fd = open("/dev/watchdog", O_WRONLY);
    3.  if (wdt_fd == -1)
    4.  {
    5.      // fail to open watchdog device
    6.  }
    

3.2. 关闭WATCHDOG

参考代码如下:

1.  #define WDIOS_DISABLECARD 0x0001  
2.  int option = WDIOS_DISABLECARD;   
3.  ioctl(wdt_fd, WDIOC_SETOPTIONS, &option);   
4.  if (wdt_fd != -1)  
5.  {  
6.      close(wdt_fd);  
7.      wdt_fd = -1;    
8.  }

3.3. 设定TIMEOUT

通过标准的IOCTL命令WDIOC_SETTIMEOUT,来设定timeout,单位是second,timeout的时间建议大于5s,参考代码如下:

1.  #define WATCHDOG_IOCTL_BASE 'W'  
2.  #define WDIOC_SETTIMEOUT_IOWR(WATCHDOG_IOCTL_BASE, 6, int)  
3.  int timeout = 20;  //secs.
4.  ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);

3.4. KEEP ALIVE

通过标准的IOCTL命令WDIOC_KEEPALIVE来喂狗,喂狗时间按照设定的timeout来决定,喂狗时间应该比timeout小,参考代码如下:

1.  #define WATCHDOG_IOCTL_BASE 'W'  
2.  #define WDIOC_KEEPALIVE_IOR(WATCHDOG_IOCTL_BASE, 5, int)  
3.  ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);