1、Bootstrap中启用看门狗
注释掉源码\Bootstrap-v1.15\board\at91sam9260ek\at91sam9260ek.c中hw_init()函数里的以下红色语句:
void hw_init(void){
.........................................................
.........................................................
/* Disable watchdog */
//writel(AT91C_WDTC_WDDIS, AT91C_BASE_WDTC + WDTC_WDMR);
/* At this stage the main oscillator is supposed to be enabled
* PCK = MCK = MOSC */
.........................................................
.........................................................
}
2、Uboot中喂狗
(1)在源码/u-boot-1.3.4/include/configs/at91sam9260ek.h中添加使用看门狗宏定义:
#CONFIG_HW_WATCHDOG 1
(2)在源码/u-boot-1.3.4/board/atmel/at91sam9260ek.c中添加看门狗相关宏定义和喂狗函数hw_watchdog_reset(),如以下代码所示:
#define AT91_WDT (0xfffffd40 - AT91_BASE_SYS)
#define AT91_WDT_CR (AT91_WDT + 0x00) /* Watchdog Control Register */
#define AT91_WDT_WDRSTT (1 << 0) /* Restart */
#define AT91_WDT_KEY (0xa5 << 24) /* KEY Password */
#ifdef CONFIG_HW_WATCHDOG
#include <watchdog.h>
void hw_watchdog_reset(void)
{
writel(AT91_WDT_KEY | AT91_WDT_WDRSTT, AT91_BASE_SYS + AT91_WDT_CR);
}
#endif
(3)添加喂狗处理:
第1处更改
在\u-boot-1.3.4\common\main.c的main_loop ()函数,for();;中输入字符处理前后加入以下看门狗喂狗处理:
//add 2010 for WDT
#ifdef CONFIG_HW_WATCHDOG //加看门狗
#include <watchdog.h>
WATCHDOG_RESET();
#endif
len = readline (CFG_PROMPT);
//add 2010 for WDT
#ifdef CONFIG_HW_WATCHDOG
WATCHDOG_RESET();
#endif
第2处更改
run_command()函数中加入两处处理:
if (strlen(cmd) >= CFG_CBSIZE) {
puts ("## Command too long!\n");
return -1;
}
//add 2010 for WDT
#ifdef CONFIG_HW_WATCHDOG
#include <watchdog.h>
WATCHDOG_RESET();
#endif
strcpy (cmdbuf, cmd);
/* Extract arguments */
if ((argc = parse_line (finaltoken, argv)) == 0) {
rc = -1; /* no command at all */
continue;
}
//add 2010 for WDT
#ifdef CONFIG_HW_WATCHDOG
#include <watchdog.h>
WATCHDOG_RESET();
#endif
第3处更改
在\u-boot-1.3.4\drivers/serial/atmel_usart.c串口输出/输出字符函数中增加看门狗函数:
//add watchdog 必须加,否则等待命令有问题
int serial_getc(void)
{
while (!(usart3_readl(CSR) & USART3_BIT(RXRDY)))
{
#ifdef CONFIG_HW_WATCHDOG
#include <watchdog.h>
WATCHDOG_RESET();
#endif
}
return usart3_readl(RHR); //读一字节
}
3、内核配置
配置看门狗驱动,在#make menuconfig 配置内核时选上AT 91SAM9 watchdog选项:
Device Drivers --->
Watchdog Timer Support --->
[*]AT 91SAM9 watchdog
对内核中/linux9260/drivers/watchdog/at91sam9_wdt.c 看门狗驱动程序做以下几处修改:
- at91_ping()函数中添加心跳包设置语句
- at91_wdt_open()函数中添加at91_wdt_reset();语句
- at91_wdt_ioctl()函数中注释掉心跳包设置语句
4、应用程序中喂狗
系统初始化完成后启动看门狗喂狗进程,使用看门狗设备如以下步骤:
打开看门狗设备:
int fd;
daemon(1,1);
fd = open("/dev/watchdog", O_WRONLY);
if (fd == -1) {
fprintf(stderr, "Watchdog device not enabled.\n");
fflush(stderr);
exit(-1);
}
使用喂狗函数喂狗:
int dummy;
ioctl(fd, WDIOC_KEEPALIVE, &dummy);
硬件支持:看门狗芯片MAX823TEUK、延时电路
CPU通过所带的GPIO口(PC4和PC9)来控制看门狗芯片的开关及喂狗程序。因看门狗芯片喂狗时间为1.6秒,超过时间则会给主芯片一个复位信号,而运行linux内核启动时间远大于1.6秒,又不便于在内核中做喂狗操作,因此设计了延时电路,中断看门狗芯片对主芯片约45秒左右的复位信号,此时间内核启动已完毕,运行至应用程序时接着对看门狗芯片喂狗即可。
延时电路对系统硬件重启和软件重启都有效。
使用步骤如下:
1、bootstrap中启用延时电路,给延时芯片一个高电平。
给延时芯片一个高电平,在\Bootstrap-v1.15\board\at91sam9260ek\at91sam9260ek.C的hw_init()函数中hw_pio[] 结构添加:
{"TEST_OUT3", AT91C_PIN_PC(4), 1, PIO_DEFAULT, PIO_OUTPUT},
2、使用GPIO在应用程序中喂外置硬件看门狗,每隔一秒对管脚给个高或低电平,如以下代码所示:
define WATCHDOE_PH AT91_PIN_PC9
define GPIO_SET_HIGH 1
define GPIO_SET_LOW 0
int start_hwatchdog_main(int argc, char **argv)
{
int fd;
daemon(1,1);
fd = open("/dev/gpio", 0);
if (fd == -1) {
fprintf(stderr, "Watchdog device not enabled.\n");
fflush(stderr);
exit(-1);
}
while(1) {
if(watchdog_flag==0){
ioctl(fd, GPIO_SET_HIGH,WATCHDOE_PH);
watchdog_flag=1;
}else{
ioctl(fd, GPIO_SET_LOW,WATCHDOE_PH);
watchdog_flag=0;
}
sleep(1);
}
close(fd);
return 0;
}

本文详细介绍了如何在AT91SAM9260路由器上启用和使用硬件看门狗,包括在Bootstrap、Uboot、内核以及应用程序中的配置和喂狗操作。同时提到了外置硬件看门狗的延时电路设计,确保系统稳定运行。
6003

被折叠的 条评论
为什么被折叠?



