看 门狗的驱动一般来说比较简单,只要做寄存器的设置实现开启、关闭、喂狗功能。本项目中我们使用的是at91sam9g45处理器,带有看门狗定时器。这个 看门狗的驱动却比较复杂,应用层想用它的话,将涉及到boot引导设置,uboot配置及驱动,改写驱动程序。下面将逐步说明。

1、boot引导(bootstrap-v1.14)

由于该看门狗的MR寄存器只能写一次(Only a processor reset resets it.),而默认情况下看门狗在boot引导程序中被关闭了,所以在boot引导程序中我们要开启看门狗。在at91sam9g45ekes.c文件的硬 件初始化函数hw_init中注释掉下面的配置即可开启看门狗:

/* writel(AT91C_WDTC_WDDIS, AT91C_BASE_WDTC + WDTC_WDMR); */

为了功能设置:我们配置如下:

writel(AT91C_WDTC_WDV | AT91C_WDTC_WDD | AT91C_WDTC_WDRSTEN | AT91C_WDTC_WDFIEN, AT91C_BASE_WDTC + WDTC_WDMR);

2、uboot配置及驱动(uboot-v1.3.4):

默认情况下,看门狗在uboot中没有配置,需要手动添加配置,在文件include/configs/at91sam9m10g45ek.h中添加如下配置

#define CONFIG_HW_WATCHDOG 1

#define CONFIG_AT91SAM9_WATCHDOG 1

此时编译uboot,会提示你找不到hw_watchdog_reset复位函数,这是因为虽然我们配置看门狗,但看门狗的uboot驱动并不存在,下面就来添加uboot下的看门狗驱动。

1)添加 include/asm-arm/arch-at91sam9/at91_wdt.h,内容如下

/*

* [origin: Linux kernel arch/arm/mach-at91/include/mach/at91_wdt.h]

*

* Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>

* Copyright (C) 2007 Andrew Victor

* Copyright (C) 2007 Atmel Corporation.

*

* Watchdog Timer (WDT) - System peripherals regsters.

* Based on AT91SAM9261 datasheet revision D.

*

* This program is free software; you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation; either version 2 of the License, or

* (at your option) any later version.

*/

#ifndef AT91_WDT_H

#define AT91_WDT_H

#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 */

#define AT91_WDT_MR (AT91_WDT + 0x04) /* Watchdog Mode Register */

#define AT91_WDT_WDV (0xfff << 0) /* Counter Value */

#define AT91_WDT_WDFIEN (1     << 12) /* Fault Interrupt Enable */

#define AT91_WDT_WDRSTEN (1     << 13) /* Reset Processor */

#define AT91_WDT_WDRPROC (1     << 14) /* Timer Restart */

#define AT91_WDT_WDDIS (1     << 15) /* Watchdog Disable */

#define AT91_WDT_WDD (0xfff << 16) /* Delta Value */

#define AT91_WDT_WDDBGHLT (1     << 28) /* Debug Halt */

#define AT91_WDT_WDIDLEHLT (1     << 29) /* Idle Halt */

#define AT91_WDT_SR (AT91_WDT + 0x08) /* Watchdog Status Register */

#define AT91_WDT_WDUNF (1 << 0) /* Watchdog Underflow */

#define AT91_WDT_WDERR (1 << 1) /* Watchdog Error */

#endif

2)添加drivers/watchdog/at91sam9_wdt.c,内容如下

/*

* [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c]

*

* Watchdog driver for Atmel AT91SAM9x processors.

*

* Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>

* Copyright (C) 2008 Renaud CERRATO r.cerrato at til-technologies.fr

*

* This program is free software; you can redistribute it and/or

* modify it under the terms of the GNU General Public License

* as published by the Free Software Foundation; either version

* 2 of the License, or (at your option) any later version.

*/

/*

* The Watchdog Timer Mode Register can be only written to once. If the

* timeout need to be set from U-Boot, be sure that the bootstrap doesn't

* write to this register. Inform Linux to it too

*/

#include <common.h>

#include <watchdog.h>

#include <asm/arch/hardware.h>

#include <asm/arch/io.h>

#include <asm/arch/at91_wdt.h>

/*

* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,

* use this to convert a watchdog

* value from/to milliseconds.

*/

#define ms_to_ticks(t) (((t << 8) / 1000) - 1)

#define ticks_to_ms(t) (((t + 1) * 1000) >> 8)

/* Hardware timeout in seconds */

#define WDT_HW_TIMEOUT 2

/*

* Set the watchdog time interval in 1/256Hz (write-once)

* Counter is 12 bit.

*/

static int at91_wdt_settimeout(unsigned int timeout)

{

unsigned int reg;

unsigned int mr;

/* Check if disabled */

mr = at91_sys_read(AT91_WDT_MR);

if (mr & AT91_WDT_WDDIS) {

printf("sorry, watchdog is disabled/n");

return -1;

}

/*

* All counting occurs at SLOW_CLOCK / 128 = 256 Hz

*

* Since WDV is a 12-bit counter, the maximum period is

* 4096 / 256 = 16 seconds.

*/

reg = AT91_WDT_WDRSTEN /* causes watchdog reset */

/* | AT91_WDT_WDRPROC causes processor reset only */

| AT91_WDT_WDDBGHLT /* disabled in debug mode */

| AT91_WDT_WDD /* restart at any time */

| (timeout & AT91_WDT_WDV); /* timer value */

at91_sys_write(AT91_WDT_MR, reg);

return 0;

}

void hw_watchdog_reset(void)

{

at91_sys_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);

}

void hw_watchdog_init(void)

{

/* 16 seconds timer, resets enabled */

at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000));

}

3)添加drivers/watchdog/Makefile

#

# (C) Copyright 2008

# Wolfgang Denk, DENX Software Engineering, wd at denx.de.

#

# See file CREDITS for list of people who contributed to this

# project.

#

# This program is free software; you can redistribute it and/or

# modify it under the terms of the GNU General Public License as

# published by the Free Software Foundation; either version 2 of

# the License, or (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 59 Temple Place, Suite 330, Boston,

# MA 02111-1307 USA

#

include $(TOPDIR)/config.mk

LIB := $(obj)libwatchdog.a

COBJS-$(CONFIG_AT91SAM9_WATCHDOG) += at91sam9_wdt.o

COBJS := $(COBJS-y)

SRCS := $(COBJS:.o=.c)

OBJS := $(addprefix $(obj),$(COBJS))

all: $(LIB)

$(LIB): $(obj).depend $(OBJS)

$(AR) $(ARFLAGS) $@ $(OBJS)

#########################################################################

# defines $(obj).depend target

include $(SRCTREE)/rules.mk

sinclude $(obj).depend

#########################################################################

4)修改uboot的Makefile,主要是把watchdog编辑到工程里

修改1:

LIBS += drivers/video/libvideo.a

+LIBS += drivers/watchdog/libwatchdog.a(添加)

LIBS += common/libcommon.a

修改2:

TAG_SUBDIRS += drivers/usb

TAG_SUBDIRS += drivers/video

+TAG_SUBDIRS += drivers/watchdog (添加)

5)修改串口驱动drivers/serial/atmel_usart.c,主要是在串口读的时候复位看门狗,防止系统重启

int serial_getc(void)

{

- while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) ; (删除)

+ while (!(usart3_readl(CSR) & USART3_BIT(RXRDY))) (添加)

+ WATCHDOG_RESET(); (添加)

return usart3_readl(RHR);

}

3、改写驱动程序(linux2.6.30内核)

1、配置内核

在默认情况,系统并不加载看门狗驱动,需要配置内核:make menuconfig

Device drivers-->Watchdog Timer Support-->AT91SAM9 watchdog

注:在笔者的at91sam9g45处理器,编译后配置编译后还是不能加载,后花了很久时间才找出原因,看门狗配置项如下:

CONFIG_AT91SAM9X_WATCHDOG=y (.config文件)

而平台设备加载中(arch/arm/mach-at91/at91sam9g45_devices.c文件)

#if defined(CONFIG_AT91SAM9_WATCHDOG) || defined(CONFIG_AT91SAM9_WATCHDOG_MODULE)

static struct platform_device at91sam9g45_wdt_device = {

.name = "at91_wdt",

.id = -1,

.num_resources = 0,

};

static void __init at91_add_device_watchdog(void)

{

platform_device_register(&at91sam9g45_wdt_device);

}

#else

static void __init at91_add_device_watchdog(void) {}

#endif

宏定义和配置项不符,难怪加载不进去,修改CONFIG_AT91SAM9_WATCHDOG为CONFIG_AT91SAM9X_WATCHDOG

正常启动,系统/dev目录下有watchdog设备

2、修改驱动

由于,该系统看门狗在处理器复位时才可以设置模式寄存器(MR),所以看门狗一旦开启,就不能关闭。

查看看门狗驱动(drivers/watchdog/at91sam9_wdt.c),我们发现驱动在平台驱动注册的时候at91wdt_driver函数中调用了:

setup_timer(&at91wdt_private.timer, at91_ping, 0);

mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);

调用内核定时器定时复位看门狗。当应用程序要使用看门狗时,可以从内核定时器列表中删除看门狗定时器,然后手动定时复位看门狗;当应用程序不使用时,再将看门狗定时器添加到内核定时器中。

这些操作我们放在IOCTL中,代码如下(at91_wdt_ioctl函数):

switch(cmd) {

case WDIOC_SETON: //"开启"看门狗自动定时复位

setup_timer(&at91wdt_private.timer, at91_ping, 0);

mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);

return 0;

case WDIOC_SETOFF: //"关闭"看门狗自动定时复位,变位手动复位

del_timer(&at91wdt_private.timer);

return 0;

case WDIOC_KEEPALIVE:

//at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;

at91_wdt_reset();

return 0;

.....

at91 看门狗的更多相关文章

  1. STM32之看门狗(独立与窗口)

    广大的互联网网友们,大家早上中午晚上好,我是某某某..对于狗..看过<忠犬八公>的我.无不深深的被狗的义气与灵气所震撼..我也觉得在所有mcu中用看门狗来形容让系统复位的功能是很恰当的.也 ...

  2. zigbee学习之路(十一):看门狗

    一.前言 今天,我们要通过实验学习和认识一下看门狗的使用,看门狗是为了防止防止程序跑飞的,通过不断的喂狗,使看门狗能持续监管程序的运行状态,当程序跑飞时,能及时把程序拽回来. 二.原理与分析 在CPU ...

  3. S3C2416 看门狗

    原理:看门狗自己有个硬件计数器,看门狗开启后,计数器就开始计数,当计数为0时触发,触发事件有两个:系统复位和中断,可设置屏蔽. 在计数器计数到0之前,程序可以重新设置计数器中的数值,称之喟狗.计数器的 ...

  4. STM32之独立看门狗与窗口看门狗总结

    一.独立看门狗 STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动,即使主时钟发生故障,它也仍然有效. 看门狗的原理:单片机系统在外界的干扰下会出现程序跑飞的现象导致出现死循环,看门狗电路 ...

  5. [ZigBee] 12、ZigBee之看门狗定时器——饿了就咬人的GOOD DOG

    引言:硬件中的看门狗,不是门卫的意思,而是一只很凶的狗!如果你不按时喂它,它就会让系统重启!这反而是我们想要的功能~ 1.看门狗概述 看门狗定时器(WDT,Watch Dog Timer)是单片机的一 ...

  6. 5、CC2541芯片中级教程-OSAL操作系统(PWM+看门狗)

    本文根据一周CC2541笔记汇总得来—— 适合概览和知识快速索引—— 全部链接: 中级教程-OSAL操作系统\OSAL操作系统-实验01 OSAL初探 [插入]SourceInsight-工程建立方法 ...

  7. 给NIOS II CPU增加看门狗定时器并使用

    给NIOS II CPU增加看门狗定时器并使用   配置看门狗定时器: 设置计时溢出时间为1秒 计数器位宽为32位 勾选No Start/Stop control bits 勾选Fixed perio ...

  8. Cubieboard2裸机开发之(五)看门狗操作

    前言 说到看门狗,应该不会陌生,看门狗说白了就是一个定时器,但是它有一个非常重要的功能就是复位系统.在A20里,看门狗的操作非常简单,只有两个寄存器,不需要操作时钟相关的东西,系统起来后可以直接使用, ...

  9. CC2540开发板学习笔记(八)—— 看门狗

    一.功能: 防止程序跑飞,使得系统重新进入工作状态,即一种复位操作吧. 但需要不停地喂狗= =(就是不断的执行某种操作),假如说你的程序跑到了不知道哪里去的错误时,即不会实现喂狗,就会发生复位,程序就 ...

随机推荐

  1. Bootstrap VS Semantic VS Foundation

    相信多数科技公司都没有自己动手设计UI的能力! 看到这个开头,预计非常多程序员非常不服气,我们那个美工可NB了,各种切图,各种特效! 假设你跟我讲这些,就说明你还没有明确UI详细指什么.通常我们所讲的 ...

  2. kafka 集群的部署安装

    这里我们罗列一下我们的环境 10.19.18.88 zk1 10.19.16.84 zk2 10.19.11.44 zk3 这里公司需要接入kafka用于zipkin来定位调用链 kafka 的地址是 ...

  3. 复习下C 链表操作(双向链表)

    双向链表 创建.删除.反转.插入 //struct #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  4. HTML常用模板:用DIV实现网站首页、后台管理首页(整理)

    1. 说明 网上下载的模板,用DIV实现页面模块之间的分隔(不是用frameset/frame).可以选择有无header/menu/footer,主体如何等等.删除了几个我认为重复的.重新命名而已. ...

  5. ARC指南 strong和weak指针

    一.简介 ARC是自iOS 5之后增加的新特性,完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autorelease语句.你不再需要担心内存管理,因为编 ...

  6. 关于iPhone音频的那些事

    音频文件(Audio File) 1.有两个概念(1).文件格式(File Format or Audio Containers)——描述文件本身的格式,里面的音频数据可以用不同的编码格式.例如:ca ...

  7. CCAction、CCFiniteTimeAction、CCSpeed、CCFollow

    /**************************************************************************** Copyright (c) 2010-201 ...

  8. Debugging the Java HotSpot VM

    Open Heart Surgery: Analyzing and Debugging the Java HotSpot VM at the OS Level https://www.youtube. ...

  9. https://jzh.12333sh.gov.cn/jzh/

    https://jzh.12333sh.gov.cn/jzh/ https://superuser.com/questions/171917/force-a-program-to-run-withou ...

  10. numpy数组-标准化数据

    标准化数据的公式: (数据值 - 平均数) / 标准差 import numpy as np employment = np.array([ 55.70000076, 51.40000153, 50. ...