linux下硬件看门狗驱动

看门狗工作原理

在产品化的嵌入式系统中,为了使系统在异常情况下能自动复位,一般都需要引入看门狗。

看门狗其实就是一个可以在一定时间内被复位的计数器。当看门狗启动后,计数器开始自动计数,经过一定时间,如果没有被复位,计数器溢出就会对CPU产生一个复位信号使系统重启。系统正常运行时,需要在看门狗允许的时间间隔内对看门狗计数器清零,不让复位信号产生。如果系统不出问题,程序按时“喂狗”,一旦程序跑飞,没有“喂狗”,系统复位。

软硬件看门狗

在现在的嵌入式系统中主要可以分为两种类型的看门狗:
    1、CPU内部自带的看门狗:此类看门狗一般是将一个芯片中的定时器来作为看门狗,通过程序的初始化,写入初值,设定溢出时间,并启动定时器。程序按时对定时器赋初值。这种看门狗是可以被禁用的(只要停止这个定时器即可)。大部分CPU都内置看门狗,硬件原理可参考各芯片数据手册。
   优点:可以通过程序改变溢出时间;可以随时禁用
   缺点:需要初始化;如果程序在初始化、启动完成前跑飞或在禁用后跑飞,看门狗就无法复位系统,这样看门狗的作用就没有了,系统恢复能力降低。

   2、独立的看门狗芯片:这种看门狗主要有一个用于喂狗的引脚(一般与CPU的GPIO相连)和一个复位引脚(与系统的RESET引脚相连),如果没有在一定时间内改变喂狗脚的电平,复位引脚就会改变状态复位CPU。此类看门狗一上电就开始工作,无法禁用。现在常用的芯片有:CAT705/CAT706、IMP706等等,溢出时间在1.6秒左右。
  优点:无须配置,上电即用。无法禁用,系统必须按时喂狗,系统恢复能力高。
  缺点:无法灵活配置溢出时间,无法禁用,灵活性降低。

Linux下看门狗驱动

  1. 硬件watchdog必须有硬件电路支持, 设备节点/dev/watchdog对应着真实的物理设备, 不同类型的硬件watchdog设备由相应的硬件驱动管理。软件watchdog由一内核模块softdog.ko 通过定时器机制实现,/dev/watchdog并不对应着真实的物理设备,只是为应用提供了一个与操作硬件watchdog相同的接口。

对于应用程序而言, 操作软件、硬件watchdog的方式基本相同:打开设备/dev/watchdog, 在重启时间间隔内对/dev/watchdog执行写操作。即软件、硬件watchdog对应用程序而言基本是透明的。

驱动实现

思路

  • 1、硬件看门狗对应引脚保持为高阻状态时,看门狗功能未启用。Linux系统加载到对应驱动之前(引脚未被使用的情况下),GPIO默认为高阻状态,故在Boot阶段中不需要加入喂狗操作。
  • 2、在应用层使用之前,需一直持续喂狗,故在驱动中需周期性喂狗,使用软件定时器来实现喂狗操作。
  • 3、应用层打开驱动时,应关闭驱动中的定时喂狗操作,将任务转移到应用层中去。

注意:此方案中,若系统在boot阶段就出问题的话,硬件看门狗的功能就没有起到复位系统的作用了,更完善的方案是在boot阶段中也加入喂狗操作。因为对boot阶段代码不熟悉,暂不讨论。

使用的硬件资源

一个设置为输出模式的普通IO。

软件源码如下

//#define DEBUG

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/sys_config.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/timer.h>


struct timer_list mytimer;

static void timeout_handle(unsigned long arg);

static void wdt_release(struct device *dev)
{

}

static struct platform_device wdt_dev = {
	.name	= "hw_wdt",
	.id	= 1,
	.dev	= {
		.release = wdt_release,
	},
};

static int enable = 1;//驱动加载时,默认开启喂狗功能,待应用层打开时关闭

static int wdt_dev_init(void)
{
	platform_device_register(&wdt_dev);
	return 0;
}

static void wdt_dev_exit(void)
{
	platform_device_unregister(&wdt_dev);
}

module_init(wdt_dev_init);
module_exit(wdt_dev_exit);

static int wdt_remove(struct platform_device *wdt_dev);
static int wdt_probe(struct platform_device *wdt_dev);

struct platform_driver wdt_drv = {
	.probe	= wdt_probe,
	.remove = wdt_remove,
	.driver	= {
		.name = "hw_wdt",
	},
};

#define HW_WDI_PIN	34//喂狗引脚号,根据具体硬件原理图修改


static int wdt_open(struct inode *inode, struct file *file)
{
	enable = 0;//关闭定时器喂狗功能
	gpio_direction_output(HW_WDI_PIN,0);
	return 0;
}

static unsigned int wdt_status=0x00;//记录引脚状态,每调用一次write时进行"非"操作一次

static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
{
	wdt_status = !wdt_status;
	__gpio_set_value(HW_WDI_PIN,wdt_status);
	return 0;
}

static void  timeout_handle(unsigned long arg)
{
	static unsigned int flag=0;

	//printk("feed the hw_wdt\n");
	if(enable == 1){
    //定时器软中断仅触发一次,故需要周期中断的情况下,需每次重新设置超时时间
	mytimer.expires = jiffies + (1*HZ);
	add_timer(&mytimer);		
	}
        flag = !flag;
	__gpio_set_value(HW_WDI_PIN,flag);
}


static struct file_operations wdt_fops={
	.owner = THIS_MODULE,
	.open  = wdt_open,
	.write = wdt_write,
};

static int major=0;
static struct class *drv_class;

static int wdt_probe(struct platform_device *pdev)
{
	
	printk("hw_wdt enter probe\n");
	init_timer(&mytimer);
	mytimer.expires = jiffies + (1*HZ);
	mytimer.data = 5;
	mytimer.function = timeout_handle;
	add_timer(&mytimer);		
	//
	major = register_chrdev(0,"hw_wdt",&wdt_fops);
	drv_class = class_create(THIS_MODULE,"hw_wdt");
	device_create(drv_class,NULL,MKDEV(major,0),NULL,"hw_watchdog");
	return 0;
}

static int wdt_remove(struct platform_device *pdev)
{
	printk("enter remove\n");
	device_destroy(drv_class,MKDEV(major,0));
	class_destroy(drv_class);
	unregister_chrdev(major,"hw_wdt");
	del_timer(&mytimer);
	return 0;
}

static int wdt_drv_init(void)
{
	platform_driver_register(&wdt_drv);
	return 0;
}

static void wdt_drv_exit(void)
{
	platform_driver_unregister(&wdt_drv);
}

module_init(wdt_drv_init);
module_exit(wdt_drv_exit);

MODULE_LICENSE("GPL");

 

前文介绍的内容引用了以下两篇博客:

使用 watchdog 构建高可用性的 Linux 系统及应用

嵌入式系统中看门狗的使用总结

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值