纪念第一个驱动程序:at91sam9g45核心板的看门狗驱动

看门狗的驱动一般来说比较简单,只要做寄存器的设置实现开启、关闭、喂狗功能。本项目中我们使用的是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;

.....

至此,全部结束,虽然该驱动并不涉及复杂的时序操作、中断处理,连寄存器就仅有3个(控制、模式、状态各一个),但是这个驱动涉及了uboot+linux启动的各个过程,对于第一个驱动能有这样的机会,对于系统的理解有莫大的好处。

第一个驱动程序:at91sam9g45核心板的看门狗驱动相关推荐

  1. linux设备驱动归纳总结(十一):简单的看门狗驱动

    http://blog.chinaunix.net/uid-25014876-id-112879.html 设备驱动的归纳已经差不多了,趁着知识点还没有遗忘,写点代码巩固一下,来个简单的看门狗驱动-- ...

  2. Amlogic A311D 驱动分析系列(一)-看门狗驱动分析

    本系列文章基于Amlogic A311D SDK中的驱动,将我之前阅读代码的一些收获进行总结,算是学习笔记吧. 1.dts配置 先看dts调用的是看门狗的哪个驱动 wdt: watchdog@0xff ...

  3. Linux看门狗驱动程序设计(三) 测试验证

    测试驱动之前需要执行make menuconfig,去掉内核自带看门狗驱动(位于Device Drivers -> Character devices -> Watchdog Timer ...

  4. linux内核看门狗关闭方法,详解linux 看门狗驱动编写

    看门狗是linux驱动的一个重要环节.某些特殊的设备,有时候需要放在一些环境恶劣的地方,比如电信设备.但是,任何软件都不可能100%没有bug.如何保证软件在遇到严重bug.死机的时候也能正常运行呢, ...

  5. linux 多线程看门狗,X86平台的看门狗驱动,在内核中开线程喂狗

    1.X86平台的看门狗驱动: 相关文件为:drivers/watchdog/iTCO_wdt.c 相关配置选项为: Device Drivers  ---> [*] Watchdog Timer ...

  6. 正点原子 核心板IMX6ULL IIC RTC驱动 PCF8563

    目录 前言 IIC RTC PCF8563硬件使用 IIC设备地址 配置 menuconfig 自带PCF8563驱动 修改设备树dtb 编写应用App 测试 前言 此篇基于学完[正点原子]I.MX6 ...

  7. 高通看门狗驱动-MSM Watchdog

    源码中的文档: 不论是软狗还是硬狗,大体思路都差不多: 准备一个定时器,定时10s,超时之后启动地球毁灭程序~~~.为了防止地球毁灭,需要时不时的将计时器的计数值清零,称为喂狗.一般看门狗定时器是无论 ...

  8. RT-Thread GD32F4xx 看门狗驱动

    目录 1.WDT 2.WDT驱动 2.1 创建WDT设备 2.2 实现WDT设备操作方法 2.3 注册WDT设备 2.4 添加配置 3.应用测试 1.WDT   WDT(Watch Dog Timer ...

  9. RK3399驱动开发 | 02 - 使用Linux自带的硬件看门狗驱动(SGM706B)

    一.SGM706 SGM706是一种集成微处理器的监控装置. 与使用单个集成电路或分立元件的设计相比,这种集成设计具有提高系统稳定性和准确性的优点. SGM706可以在上电.下电甚至降压断电的情况下进 ...

最新文章

  1. android异步任务详解 AsynTask[转 杨瓦瓦]
  2. linux苹果开发者p12,苹果testflight发布流程
  3. 一个程序员的日常,简直太欢乐了!
  4. postgresql表中间加列_PostgreSQL ALTER TABLE命令
  5. Victor and World(spfa+状态压缩dp)
  6. 一步一步写算法(之内存)
  7. px 与 dp, sp换算公式?(转)
  8. 篮球计分器软件_SPSS 26 综合性数学统计分析工具
  9. Unity的渲染管线
  10. 机房收费系统——可行性研究报告
  11. 视频+案例 | 钟南山院士谈5G医疗
  12. 浅谈融云即时通讯服务「日志优化」
  13. 深度学习-感知机模型---思路图解+python代码
  14. 程序员面试被问,有没有别家的offer?这个问题怎么回答?
  15. matlab系统频域分析,基于MATLAB的系统频域分析的实现
  16. 图像算法工程师面试考点集锦
  17. CUDA C 编程指导(二):CUDA编程模型详解
  18. Salvage Robot[agc-004E]
  19. createjs开发教程
  20. 计算机没考好的检讨书300百以上,考试反思检讨书300字(精选10篇)

热门文章

  1. 矩形波如何傅立叶展开_COMSOL 软件建模教程:如何模拟自由液面 (二)
  2. 我的世界java版记分板_我的世界计分板怎么用 我的世界计分板指令一览
  3. ABAP QA11检验批决策的创建
  4. C语言课程设计——仓库物资管理系统
  5. 四川万合通盈:拼多多产品如何下架
  6. Activiti基础
  7. JavaScript的基础学习(一)
  8. P3983 赛斯石(赛后强化版)-动态规划
  9. 巧用Rundll32命令安装和卸载Windows程序
  10. 平平无奇的“小插座”,堆出千亿市值的公牛集团