linux 嵌入式看门狗使用

Posted chenchangxiong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 嵌入式看门狗使用相关的知识,希望对你有一定的参考价值。

linux看门狗使用很简单,在应用层使用,只需要ioctl设置一下溢出时间,既可以使用看门狗,定时喂狗即可

 

前提是kernel支持看门狗

Device Drivers ─>

  [*] Watchdog Timer Support  --->

 技术图片

 

 

配置好好后,即可make

应用层的使用,直接上代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <sys/signal.h>

//watchdog 
#define WATCHDOG_IOCTL_BASE     ‘W‘

struct watchdog_info 
    unsigned int options;          /* Options the card/driver supports */
    unsigned int firmware_version; /* Firmware version of the card */
    char identity[32];     /* Identity of the board */
;

#define WDIOC_GETSUPPORT        _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)
#define WDIOC_GETSTATUS         _IOR(WATCHDOG_IOCTL_BASE, 1, int)
#define WDIOC_GETBOOTSTATUS     _IOR(WATCHDOG_IOCTL_BASE, 2, int)
#define WDIOC_GETTEMP           _IOR(WATCHDOG_IOCTL_BASE, 3, int)
#define WDIOC_SETOPTIONS        _IOR(WATCHDOG_IOCTL_BASE, 4, int)
#define WDIOC_KEEPALIVE         _IOR(WATCHDOG_IOCTL_BASE, 5, int)
#define WDIOC_SETTIMEOUT        _IOWR(WATCHDOG_IOCTL_BASE, 6, int)
#define WDIOC_GETTIMEOUT        _IOR(WATCHDOG_IOCTL_BASE, 7, int)
#define WDIOC_SETPRETIMEOUT     _IOWR(WATCHDOG_IOCTL_BASE, 8, int)
#define WDIOC_GETPRETIMEOUT     _IOR(WATCHDOG_IOCTL_BASE, 9, int)
#define WDIOC_GETTIMELEFT       _IOR(WATCHDOG_IOCTL_BASE, 10, int)

#define WDIOF_OVERHEAT          0x0001  /* Reset due to CPU overheat */
#define WDIOF_FANFAULT          0x0002  /* Fan failed */
#define WDIOF_EXTERN1           0x0004  /* External relay 1 */
#define WDIOF_EXTERN2           0x0008  /* External relay 2 */
#define WDIOF_POWERUNDER        0x0010  /* Power bad/power fault */
#define WDIOF_CARDRESET         0x0020  /* Card previously reset the CPU */
#define WDIOF_POWEROVER         0x0040  /* Power over voltage */
#define WDIOF_SETTIMEOUT        0x0080  /* Set timeout (in seconds) */
#define WDIOF_MAGICCLOSE        0x0100  /* Supports magic close char */
#define WDIOF_PRETIMEOUT        0x0200  /* Pretimeout (in seconds), get/set */
#define WDIOF_KEEPALIVEPING     0x8000  /* Keep alive ping reply */

#define WDios_DISABLECARD       0x0001  /* Turn off the watchdog timer */
#define WDIOS_ENABLECARD        0x0002  /* Turn on the watchdog timer */
#define WDIOS_TEMPPANIC         0x0004  /* Kernel panic on temperature trip */

int wdt_fd;
int time_out = 5;

void stop_signal()

    int i = 0 , ret = 0 ;

    i = WDIOS_DISABLECARD ;
    ioctl(wdt_fd, WDIOC_SETOPTIONS, &i) ;

    printf("===watchdow will be closed===\\n") ;
    close(wdt_fd) ;

    exit(0) ;



int main(int argc, char *argv[])

    int new_time;    
    int i;
    int ret, count = 10;
    struct watchdog_info wdt_info;

    signal(SIGINT, stop_signal) ;

    wdt_fd = open("/dev/watchdog", O_RDWR);
    if(wdt_fd == -1)
        perror("Open Watchdog ERROR!\\n");

    //get watchdog infomation struct
    ioctl(wdt_fd, WDIOC_GETSUPPORT, &wdt_info);
    //printf("options=%d,id=%s\\n", wdt_info.options, wdt_info.identity);

    ioctl(wdt_fd, WDIOC_SETTIMEOUT, &time_out);
    
    while(1)
    
        ioctl(wdt_fd,WDIOC_KEEPALIVE,NULL);
        sleep(atoi(argv[1]));

        

    close(wdt_fd);
    return 0;

 

以上是关于linux 嵌入式看门狗使用的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统看门狗应用编程

嵌入式软件设计(看门狗)

嵌入式 02 STM32 09 独立/窗口看门狗实验

MCU独立看门狗 vs 窗口看门狗

看门狗定时器原理介绍

嵌入式喂狗