s3c2440 看门狗设置使用例程


在嵌入式工控领域,为保障程序的持续运行,使用看门狗是必须的。
调狐上网,发现详细介绍s3c2440 Watchdog的资料真不多。于是就对着linux kernel里的代码自己研究吧。

偶写的测试例程如下所示:

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

time_t  backTime;
struct tm *pBackTime;
int wt_fd;

static void sigAlarm(int sig)
{
    char flag = '0';
    (void)time(&backTime);
    pBackTime= localtime(&backTime);
    printf("day: %d; hour: %d; min: %d; sec: %d\n", pBackTime->tm_mday, pBackTime->tm_hour, pBackTime->tm_min, pBackTime->tm_sec);

    write(wt_fd, &flag, 1); //Reset Watchdog
    alarm(2);
    return;
}


int main()
{
       char flag = 'V';
       int ret;
       int timeout = 42;
      
       if(SIG_ERR == signal(SIGALRM, sigAlarm))
       {
           perror("signal (SIGALARM) error");
       }

       wt_fd = open("/dev/watchdog", O_RDWR);
       if(wt_fd <= 0)
       {
           printf("Fail to open watchdog  device!\n");
       }
       else
       {
           write(wt_fd,NULL,0);
           printf("Turn on Watch Dog\n");
           ret = ioctl(wt_fd, WDIOC_SETTIMEOUT, &timeout);
           if(EINVAL == ret)
           {
                printf("EINVAL Returned\n");
           }
           else if(EFAULT == ret)
           {
                printf("EFAULT Returned\n");
           }
           else if(0 == ret)
           {
                printf("Set timeout %d secs success\n", timeout);
           }
           else
           {
                printf("Ret %d\n", ret);
           }
       }

       alarm(3);

       while(1);

       write(wt_fd, &flag, 1);
       printf("Turned off Watch Dog\n");
       close(wt_fd);
       return 0;
}


本代码实现了启用看门狗,设置看门狗超时时间,定时喂狗,关闭看门狗还没有修正。
经实践发现,42秒是s3c2440最长的超时时间。

相关内容