文章目录
1 内核定时器介绍
2 定时器数据结构及函数
3 外部看门狗驱动使用定时器函数
1 内核定时器
Linux 内核中有大量的函数需要时间管理,比如周期性的调度程序、延时程序等。硬件定时器
提供时钟源,时钟源的频率可以设置, 设置好以后就周期性的产生定时中断,系统使用定时中断
来计时。
Linux内核定时器采用系统时钟来实现,内核中配置:
-> Kernel Features
-> Timer frequency (<choice> [=y])
高节拍率和低节拍率的优缺点:
①、高节拍率会提高系统时间精度,如果采用 100Hz 的节拍率,时间精度就是 10ms,采用
1000Hz 的话时间精度就是 1ms,精度提高了 10 倍。高精度时钟对于那些对时间要求严
格的函数来说,能够以更高的精度运行,时间测量也更加准确。
②、高节拍率会导致中断的产生更加频繁,频繁的中断会加剧系统的负担, 1000Hz 和
100Hz的 系统节拍率相比,系统要花费 10 倍的资源去处理中断。Linux 内核使用全局变
量 jiffies 来记录系统从启动以来的系统节拍数,系统启动的时候会将 jiffies 初始化为 0
2 定时器数据结构
struct timer_list {
struct list_head entry;
unsigned long expires; /* 定时器超时时间,单位是节拍数 */
struct tvec_base *base;
void (*function)(unsigned long); /* 定时处理函数 */
unsigned long data; /* 要传递给 function 函数的参数 */
int slack;
};
3 定时器函数
含义 | 函数 |
初始化函数 | void init_timer(struct timer_list *timer) |
向 Linux 内核注册定时器 | void add_timer(struct timer_list *timer) |
修改定时值 | int mod_timer(struct timer_list *timer, unsigned long expires) |
删除一个定时器 | int del_timer(struct timer_list * timer) |
等待其他处理器使用完定时器再删除 | int del_timer_sync(struct timer_list *timer) |
4 一般驱动程序框架
/* timer设备结构体 */
struct timer_dev{
struct timer_list timer;/* 定义一个定时器*/
...
};
struct timer_dev timerdev; /* timer设备 */
static int timer_open(struct inode *inode, struct file *filp)
{
timerdev.timeperiod = 1000; /* 默认周期为1s */
}
/* 定时器回调函数 */
void timer_function(unsigned long arg)
{
//重新设置定时器,设置超时时间
mod_timer(&dev->timertest, jiffies + msecs_to_jiffies(2000));
}
static int __init timer_init(void)
{
/* 初始化timer,设置定时器处理函数,还未设置周期,所有不会激活定时器 */
init_timer(&timerdev.timer);
timerdev.timer.function = timer_function;//回调函数
timerdev.timer.data = (unsigned long)&timerdev;
return 0;
}
static void __exit timer_exit(void)
{
del_timer_sync(&timerdev.timer); /* 删除timer */
}
5 外部看门狗程序示例
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/of_gpio.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/rfkill.h>
#include <linux/regulator/consumer.h>
#include <linux/platform_device.h>
#include <linux/sys_config.h>
#include <linux/if_ether.h>
#include <linux/etherdevice.h>
#include <linux/crypto.h>
#include <linux/err.h>
#include <linux/scatterlist.h>
#define WDT_DEV_NAME "hardware-wdt"
// 喂狗控制
#define WDT_CTL 1
// 函数声明
static int wdt_gpio_init(void);
static void timeout_handle(unsigned long val);
// 内核定时器控制
static int wdtCtlTimer = 1;
// 引脚状态控制
static int wdtCtlStat = 0;
// 引脚编号
static int wdtEn = 0;
static int wdtCtl = 0;
/
static struct timer_list wdt_timer;
static void timeout_handle(unsigned long val)
{
struct timeval tv;
// 产生电平变化喂狗
if(wdtCtlTimer)
{
wdtCtlStat = !wdtCtlStat;
gpio_direction_output(wdtCtl, wdtCtlStat);
}
// 重新开始计时
do_gettimeofday(&tv);
wdt_timer.expires = jiffies+1*HZ;
add_timer(&wdt_timer);
}
/*
* @description : 写入设备数据
* @param - filp :
* @return : 0 成功;其他 失败
*/
static long wdt_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
void __user *argp = (void __user *)arg;
cmd &= 0xFF;
switch(cmd)
{
case WDT_CTL:// 取反喂狗引脚电平喂狗
wdtCtlStat = !wdtCtlStat;
gpio_direction_output(wdtCtl, wdtCtlStat);
break;
default:
return -EINVAL;
}
return 0;
}
/*
* @description : 写入设备数据
* @param - filp :
* @return : 0 成功;其他 失败
*/
static ssize_t wdt_write(struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
return 0;
}
/*
* @description : 打开设备
* @param - inode :
* @param - filp :
* @return : 0 成功; 其他 失败
*/
static int wdt_open(struct inode *inode, struct file *filp)
{
// 打开内核定时器喂狗
wdtCtlTimer = 0;
return 0;
}
/*
* @description : 关闭设备
* @param - inode :
* @param - filp :
* @return : 0 成功; 其他 失败
*/
static int wdt_release(struct inode *inode, struct file *filp)
{
// 关闭内核定时器喂狗
wdtCtlTimer = 1;
return 0;
}
/*
* @description : wdt_ops操作函数
* @param - filp :
* @return :
*/
static const struct file_operations wdt_ops = {
.owner = THIS_MODULE,
.open = wdt_open,
.release = wdt_release,
.write = wdt_write,
.unlocked_ioctl = wdt_ioctl,
};
/*
* @description : wdt_miscdev 杂项设备数据结构
* @param - filp :
* @return :
*/
static struct miscdevice wdt_miscdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = WDT_DEV_NAME,
.fops = &wdt_ops,
};
static int wdt_probe(struct platform_device *pdev)
{
int ret = 0;
char pin_name[32] = {0};
unsigned long pin_config = 0;
/* 1.获取设备节点 */
struct device *dev = &pdev->dev;
struct device_node *wdtNd = pdev->dev.of_node;
/* 2.初始化外部看门狗芯片相关引脚 */
// 找到wdt_ctl引脚
wdtCtl = of_get_named_gpio(wdtNd, "wdt_ctl", 0);
if(!gpio_is_valid(wdtCtl)) {
printk("get wdt_ctl gpio failed !\r\n");
return -EINVAL;
} else {
ret = devm_gpio_request(dev, wdtCtl, "wdt_ctl");
if(ret < 0) {
printk("request gpio wdt_ctl failed !\r\n");
return ret;
}
// 设置wdt_ctl引脚方向
ret = gpio_direction_output(wdtCtl, wdtCtlStat);
if (ret < 0) {
printk("can't request output direction wdtCtl gpio %d\n", wdtCtl);
return ret;
} else {
printk("wdt_ctl gpio_direction_output ok !\r\n");
}
}
// 找到wdt_en引脚
wdtEn = of_get_named_gpio(wdtNd, "wdt_en", 0);
if(!gpio_is_valid(wdtEn)) {
printk("get wdt_en gpio failed !\r\n");
return -EINVAL;
} else {
printk("get wdt_en = %d gpio ok !\r\n", wdtEn);
ret = devm_gpio_request(dev, wdtEn, "wdt_en");
if(ret < 0) {
printk("request gpio wdt_en failed !\r\n");
return ret;
}
// 设置wdt_en引脚方向(拉低使能喂狗芯片)
ret = gpio_direction_output(wdtEn, 0);
if (ret < 0) {
printk("can't request output direction wdtEn gpio %d\n", wdtEn);
return ret;
} else {
printk("wdt_en gpio_direction_output ok !\r\n");
}
}
/* 添加杂项设备 */
ret = misc_register(&wdt_miscdev);
if (ret) {
printk("can't misc_register wdt_miscdev~!\n");
return ret;
}
/* 初始化内核定时器 */
init_timer(&wdt_timer);
wdt_timer.expires = jiffies + (1*HZ);
wdt_timer.data = (unsigned long)"hello";
wdt_timer.function = timeout_handle;
add_timer(&wdt_timer);
return 0;
}
static int wdt_remove(struct platform_device *pdev)
{
// 先关闭喂狗芯片
gpio_direction_output(wdtEn, 1);
/* 删除控制引脚 */
if(wdtEn > 0)
{
gpio_free(wdtEn);
}
if(wdtCtl > 0)
{
gpio_free(wdtCtl);
}
/* 删除内核定时器 */
del_timer(&wdt_timer);
/* 删除设备驱动 */
misc_deregister(&wdt_miscdev);
return 0;
}
// 此处与设备树中名字相匹配
static const struct of_device_id wdt_ids[] = {
{ .compatible = "hardware-wdt" },
{ /* Sentinel */ }
};
static struct platform_driver wdt_drv = {
.probe = wdt_probe,
.remove = wdt_remove,
.driver = {
.owner = THIS_MODULE,
.name = WDT_DEV_NAME,
.of_match_table = wdt_ids,
},
};
/*
* @description : 驱动入口函数
* @param : 无
* @return : 无
*/
static int __init wdt_init(void)
{
return platform_driver_register(&wdt_drv);
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static void __exit wdt_exit(void)
{
platform_driver_unregister(&wdt_drv);
}
module_init(wdt_init);
module_exit(wdt_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("generic hw watchdog driver");
MODULE_AUTHOR("AiTeHao");