版權所有,轉載必須說明轉自 http://my.csdn.net/weiqing1981127
對於看門狗驅動,在我們內核中把這些代碼放在/driver/watchdog文件夾下面,但是watchdog並沒有子系統,每個驅動都是獨立的,需要我們根據自己的平臺去編寫看門狗驅動,比如mini2440看門狗驅動代碼就是/driver/watchdog/S3c2410_wdt.c,這個代碼比較簡單,我不想多分析,這裏主要講下移植和應用層使用的方法。另外對於Watchdog設備。內核中的說明文檔在/Document/wdt.txt中
一般我們移植一個驅動,先在內核中找到驅動文件,這個就需要我們知道常見的一些設備的驅動分佈在我們內核的哪個子系統,哪個目錄下。當找到了這個驅動文件後,一般先瀏覽驅動文件的順序是從下往上看,先看模塊加載函數,一般會定義一個平臺驅動,比如
static struct platform_driver s3c2410wdt_driver = {
.probe = s3c2410wdt_probe,
.remove = __devexit_p(s3c2410wdt_remove),
.shutdown = s3c2410wdt_shutdown,
.suspend = s3c2410wdt_suspend,
.resume = s3c2410wdt_resume,
.driver = {
.owner = THIS_MODULE,
.name = "s3c2410-wdt", //驅動名
},
};
然後我們在Source Insight中查找"s3c2410-wdt"相關所有文件,這樣會在Devs.c中找到如下代碼
static struct resource s3c_wdt_resource[] = {
[0] = {
.start = S3C24XX_PA_WATCHDOG,
.end = S3C24XX_PA_WATCHDOG + S3C24XX_SZ_WATCHDOG - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_WDT,
.end = IRQ_WDT,
.flags = IORESOURCE_IRQ,
}
};
struct platform_device s3c_device_wdt = {
.name = "s3c2410-wdt",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_wdt_resource),
.resource = s3c_wdt_resource,
};
需要注意的是如果Devs.c中沒有定義,而在其他文件中找到設備名,那麼需要在BSP中添加平臺設備。好了,下面打開我們的BSP文件mach-mini2440.c,在mach-mini2440.c這個mini2440開發板的BSP中把這個s3c_device_rtc加入到mini2440_devices數組
static struct platform_device *mini2440_devices[] __initdata = {
……
& s3c_device_wdt , //添加
};
這樣配置完後,進行make zImage生成zImage內核鏡像。
Watchdog驅動測試
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/fcntl.h>
#include <linux/watchdog.h>
#define DEV_NAME "/dev/watchdog"
int main(int argc, char *argv[])
{
int fd = -1;
if ((fd = open(DEV_NAME, O_RDONLY)) < 0)
{
perror("open error");
exit(1);
}
for(;;)
{
ioctl(fd, WDIOC_KEEPALIVE); //餵狗
sleep(5);
}
close(fd);
return 0;
}
上面是應用層餵狗程序,當加載內核後移,必須每隔15秒執行一次該文件,如果你不餵狗,那麼系統會在15秒後重啓。