参考文档: Documentation/watchdog/
内核提供了一套通用的看门狗驱动框架,这样用户层的操作就比较统一了。
三星通用的看门狗驱动为: drivers/watchdog/s3c2410_wdt.c
CONFIG_S3C2410_WATCHDOG_ATBOOT 为 0 表示不会自启动,需要用户编程启动看门狗
CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME 定义了默认的超时时间为 15s
在 mach-Louis210.c 中已经添加了看门狗设备
Louis210_devices 设备列表, 只需配置内核
Device Drivers ---> [*] Watchdog Timer Support ---> [*] Disable watchdog shutdown on close (NEW) //如果选中, 用户一旦 open 看门狗设备,用户就没法关闭看门狗 <*> S3C2410 Watchdog |
编译内核,运行测试
内核提供了一个简单的看门狗喂狗程序: Documentation/watchdog/src/watchdog-simple.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int fd = open("/dev/watchdog", O_WRONLY);
int ret = 0;
if (fd == -1) {
perror("watchdog");
exit(EXIT_FAILURE);
}
while (1) {
ret = write(fd, "\0", 1);
if (ret != 1) {
ret = -1;
break;
}
sleep(10);
}
close(fd);
return ret;
}
对看门狗写入任何值,都能实现喂狗。内核还提供了一个稍微复杂点的看门狗测试程序: Documentation/watchdog/src/watchdog-test.c
/*
* Watchdog Driver Test Program
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/watchdog.h>
int fd;
/*
* This function simply sends an IOCTL to the driver, which in turn ticks
* the PC Watchdog card to reset its internal timer so it doesn't trigger
* a computer reset.
*/
static void keep_alive(void)
{
int dummy;
ioctl(fd, WDIOC_KEEPALIVE, &dummy);
}
/*
* The main program. Run the program with "-d" to disable the card,
* or "-e" to enable the card.
*/
int main(int argc, char *argv[])
{
int flags;
fd = open("/dev/watchdog", O_WRONLY);
if (fd == -1) {
fprintf(stderr, "Watchdog device not enabled.\n");
fflush(stderr);
exit(-1);
}
if (argc > 1) {
if (!strncasecmp(argv[1], "-d", 2)) {
flags = WDIOS_DISABLECARD;
ioctl(fd, WDIOC_SETOPTIONS, &flags);
fprintf(stderr, "Watchdog card disabled.\n");
fflush(stderr);
exit(0);
} else if (!strncasecmp(argv[1], "-e", 2)) {
flags = WDIOS_ENABLECARD;
ioctl(fd, WDIOC_SETOPTIONS, &flags);
fprintf(stderr, "Watchdog card enabled.\n");
fflush(stderr);
exit(0);
} else {
fprintf(stderr, "-d to disable, -e to enable.\n");
fprintf(stderr, "run by itself to tick the card.\n");
fflush(stderr);
exit(0);
}
} else {
fprintf(stderr, "Watchdog Ticking Away!\n");
fflush(stderr);
}
while(1) {
keep_alive();
sleep(1);
}
}
下面是对 watchdog-simple.c 的测试
运行程序后,然后按 Ctrl+C 终止程序,系统会自动关闭所有打开的文件描述符,即会对看门狗调用close,这里的提示是从内核打印出来的,一会儿系统就自动重启了。
可以向看门狗写入字符’v’,这样在对其调用 close 时,看门狗就会停止,下面是修改后的程序