测试驱动之前需要执行make menuconfig,去掉内核自带看门狗驱动(位于Device Drivers -> Character devices -> Watchdog Timer Support),重新编译烧写内核。
将驱动编译为wdt_drv.ko文件,insmod加载驱动,如图所示,可以看到probe函数中的提示信息:
再编写一个简单的应用程序测试驱动功能,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd;
char c;
int i;
fd = open("/dev/watchdog", O_RDWR);
if(fd == -1) {
printf("no device\n");
exit(1);
}
c = argv[1][0];
for(i = 0; i < 15; i++) {
printf("%d\n", i);
sleep(1);
write(fd, &c, 1);
}
close(fd);
exit(0);
}
在应用程序的for循环中每隔1s输出一次计数,共15s, 并将程序命令行参数写入看门狗设备文件。看门狗定时器在驱动程序中设置为10s超时,如果命令行参数为1,将字符‘1’写入设备文件,驱动write函数执行喂狗操作,for循环15s系统不会重启,程序正常