簡(jiǎn)介
? ? ? ? 嵌入式系統(tǒng)或設(shè)備中凡纳,如果發(fā)生故障(宕機(jī))疏虫,無法及時(shí)對(duì)故障作出反應(yīng)。為了使系統(tǒng)在異常情況下能自動(dòng)恢復(fù)特石,一般都會(huì)引入看門狗盅蝗。
? ? ? ? 看門狗電路是一種電子計(jì)時(shí)器,其用于檢測(cè)和恢復(fù)設(shè)備故障姆蘸。當(dāng)看門狗啟動(dòng)后墩莫,計(jì)數(shù)器開始自動(dòng)計(jì)數(shù),經(jīng)過一定時(shí)間計(jì)數(shù)器溢出就會(huì)對(duì)CPU產(chǎn)生一個(gè)復(fù)位信號(hào)使系統(tǒng)重啟逞敷。系統(tǒng)正常運(yùn)行時(shí)狂秦,需要在看門狗允許的時(shí)間間隔內(nèi)對(duì)看門狗計(jì)數(shù)器清零也即喂狗,不讓復(fù)位信號(hào)產(chǎn)生推捐。
? ? ? ? 看門狗是 Linux 系統(tǒng)一個(gè)重要的機(jī)制裂问,其目的是監(jiān)測(cè)系統(tǒng)運(yùn)行的情況,一旦出現(xiàn)鎖死牛柒,死機(jī)的情況堪簿,能及時(shí)重啟設(shè)備。
開發(fā)環(huán)境介紹
- 主機(jī)操作系統(tǒng):Ubuntu14.04 64位
- 目標(biāo)平臺(tái):A40I (ARM Cortex-A7)
- 交叉工具鏈:arm-linux-gnueabi皮壁,gcc5.3.1
- 內(nèi)核版本:3.10
使能看門狗驅(qū)動(dòng)
Step1. start the Linux Kernel Configuration tool:
make ARCH=arm menuconfig
Step2. Select Device Drivers from the main menu.
...
Device Drivers --->
File systems --->
...
Step3. Select Watchdog Timer Support as shown here:
...
[*] Watchdog Timer Support --->
Sonics Silicon Backplane --->
...
Step4. Select SUNXI Watchdog as shown here:
--- Watchdog Timer Support
[*] WatchDog Timer Driver Core
[*] Disable watchdog shutdown on close
< > Software watchdog
<*> SUNXI Watchdog
[Disable watchdog shutdown on close]選項(xiàng):當(dāng)編譯內(nèi)核的時(shí)候這個(gè)選項(xiàng)被設(shè)置為Y椭更,則一旦watchdog被啟動(dòng),則將沒有辦法能夠停止蛾魄。這樣虑瀑,則當(dāng)watchdog守護(hù)進(jìn)程崩潰的時(shí)候,系統(tǒng)仍將在超時(shí)后重啟畏腕。
Step5. 添加設(shè)備樹節(jié)點(diǎn)
wdt: watchdog@01c20ca0 {
compatible = "allwinner,sun4i-wdt";
reg = <0x0 0x01c20c90 0 0x18>;
};
compatible屬性需要根據(jù)驅(qū)動(dòng)中來:
static const struct of_device_id sunxi_wdt_dt_ids[] = {
{ .compatible = "allwinner,sun4i-wdt", .data = &sun4i_wdt_reg },
{ .compatible = "allwinner,sun6i-wdt", .data = &sun6i_wdt_reg },
{ .compatible = "allwinner,sun8i-wdt", .data = &sun6i_wdt_reg },
{ .compatible = "allwinner,sun50i-wdt", .data = &sun6i_wdt_reg },
{ /* sentinel */ }
};
應(yīng)用測(cè)試
? ? ? ? 通常一個(gè)用戶空間守護(hù)進(jìn)程會(huì)在正常的時(shí)間間隔內(nèi)通過/dev/watchdog特殊設(shè)備文件來通知內(nèi)核的watchdog驅(qū)動(dòng)缴川,用戶空間仍然正常。當(dāng)這樣的一個(gè)通知發(fā)生時(shí)描馅,驅(qū)動(dòng)通常會(huì)告訴硬件watchdog一切正常把夸,然后watchdog應(yīng)該再等待一段時(shí)間來復(fù)位系統(tǒng)。如果用戶空間出問題(RAM錯(cuò)誤铭污,內(nèi)核bug等)恋日,則通知將會(huì)停止,然后硬件watchdog將在超時(shí)后復(fù)位系統(tǒng)嘹狞。
? ? ? ? 所有的設(shè)備驅(qū)動(dòng)都支持的基本的操作模式岂膳,一旦/dev/watchdog被打開,則watchdog激活磅网,并且除非喂狗谈截,否則將在一段時(shí)間之后重啟,這個(gè)時(shí)間被稱為timeout或margin。最簡(jiǎn)單的喂狗方法就是寫一些數(shù)據(jù)到設(shè)備簸喂。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/watchdog.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#define WATCHDOGDEV "/dev/watchdog0"
#define MIN_TIMEOUT 1 //(in secs)
#define MAX_TIMEOUT 16 //(in secs)
#define MIN_SLEEP 1 //(in secs)
#define IN_RANGE(val, max, min) \
((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
void print_usage(FILE * stream, const char *app_name, int exit_code);
int main(int argc, const char *argv[])
{
int fd = -1; /* File handler for watchdog */
int timeout; /* Watchdog timeout interval (in secs) */
int sleep_sec; /* Feed the dog time(in secs) */
int mode = 0; /* Feed the dog way */
if (argc < 3) {
print_usage(stdout, argv[0], EXIT_FAILURE);
return 1;
}
timeout = IN_RANGE(atoi(argv[1]), MAX_TIMEOUT, MIN_TIMEOUT);
sleep_sec = IN_RANGE(atoi(argv[2]), INT_MAX, MIN_SLEEP);
mode = atoi(argv[3]);
printf("Starting wdt_driver (timeout: %d, sleep: %d, mode: %s)\n",
timeout, sleep_sec, (mode == 0) ? "ioctl" : "write");
/*打開設(shè)備文件*/
fd = open(WATCHDOGDEV, O_WRONLY);
if (fd == -1) {
perror("watchdog");
exit(EXIT_FAILURE);
}
printf("Trying to set timeout value=%d seconds\n", timeout);
ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
printf("The actual timeout was set to %d seconds\n", timeout);
ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
printf("Now reading back -- The timeout is %d seconds\n", timeout);
while (1)
{
if ( 0==mode ) {
//使用ioctl()方法喂狗
ioctl(fd, WDIOC_KEEPALIVE, 0);
} else {
//使用write()方法喂狗
write(fd, "\0", 1);
}
sleep(sleep_sec);
}
return 0;
}
void print_usage(FILE * stream, const char *app_name, int exit_code)
{
fprintf(stream, "Usage: %s <timeout> <sleep> <mode> \n", app_name);
fprintf(stream,
" timeout: value in seconds to cause wdt timeout/reset.\n"
" sleep : value in seconds to service the wdt.\n"
" mode : 0 - Service wdt with ioctl(), 1 - with write() \n"
" The default device file is '/dev/watchdog0'\n");
exit(exit_code);
}
備注:應(yīng)用層上毙死,dev/watchdog 下會(huì)有watchdog 和 watchdog0;代表的是同一個(gè)硬件喻鳄,即使他們的字符設(shè)備的major和minor 不一樣扼倘。(之所以要注冊(cè)/dev/watchog 是為來兼容老的接口)
參考:
https://processors.wiki.ti.com/index.php/AM335x_PSP_WDT_Driver_User_Guide
https://embeddedfreak.wordpress.com/2010/08/23/howto-use-linux-watchdog/