在嵌入式工控领域,为保障程序的持续运行,使用看门狗是必须的。
调狐上网,发现详细介绍s3c2440 Watchdog的资料真不多。于是就对着linux kernel里的代码自己研究吧。
偶写的测试例程如下所示:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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: %dn", 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 Dogn");
ret = ioctl(wt_fd, WDIOC_SETTIMEOUT, &timeout);
if(EINVAL == ret)
{
printf("EINVAL Returnedn");
}
else if(EFAULT == ret)
{
printf("EFAULT Returnedn");
}
else if(0 == ret)
{
printf("Set timeout %d secs successn", timeout);
}
else
{
printf("Ret %dn", ret);
}
}
alarm(3);
while(1);
write(wt_fd, &flag, 1);
printf("Turned off Watch Dogn");
close(wt_fd);
return 0;
}
本代码实现了启用看门狗,设置看门狗超时时间,定时喂狗,关闭看门狗还没有修正。
经实践发现,42秒是s3c2440最长的超时时间。