一、timer 定时器
工作流程:
1)创建timer,编写定时器处理函数;
2)为timer的expires、data、function赋值;
3)调用add_timer将timer加入列表;
4)定时器到期时,function被执行。
接口介绍:
#include<linux/timer.h> //头文件
struct timer_list
{
struct list_head list; //linux中对所有定时器实行链表管理,此为链表头结点
unsigned long expires; //定时器到期时间
unsigned long data; //此参数最后传入定时处理函数,类似于线程中的arg
void (*function)(unsigned long); //定时器超时执行函数
};
void init_timer(struct timer_list * timer); // 初始化定时器
void add_timer(struct timer_list * timer);//添加定时器
int del_timer(struct timer_list * timer);//删除定时器
int del_timer_sync( struct timer_list *timer ) //删除定时器;
//检测CPU上是否还有其他地方在使用这个将要被删除的内核定时器,如果有的话,等待这个内核定时器被用完然后在删除;
int timer_pending( struct timer_list *timer );
//判断内核定时器实在激活状态,还是在非激活状态
int mod_timer(struct timer_list * timer,unsigned long expires);//修改定时器到期时间
说明:
mod_timer() 等价于:
del_timer(timer);
timer->expires=expires;
add_timer(timer);
HZ介绍:
#include <linux/jiffies.h> //jiffies头文件
HZ:1秒钟内,时钟中断的次数,即1秒钟内,系统时钟的节拍次数。
jiffies:全局变量,用来记录系统自启动以来产生的节拍总数
系统运行时间(以秒为单位):system_time=(jiffies)/HZ。
例如:
jiffies定时器,HZ=1000,精度只能达到1ms。,HZ=100,精度只能达到10ms,
jiffies+msecs_to_jiffies(xx ms);可做到ms级,
示例:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>//jiffies在此头文件中定义
#include <linux/init.h>
#include <linux/timer.h>
struct timer_list timer;//定义一个定时器
void timer_function(unsigned long arg)
{
printk("hello world\n");
printk("read data from timer: %d\n",arg);
//mod_timer(&timer, jiffies + (5*HZ));//重新设置定时器,每隔5秒执行一次
}
static int __init hello_init (void)
{
printk("hello,world\n");
init_timer(&timer); //初始化定时器
timer.expires = jiffies+(200*HZ);//设定超时时间,200秒(未来的中断数)
timer.data = 100; //传递给定时器超时函数的值
timer.function = timer_function;//设置定时器超时函数
add_timer(&timer); //添加定时器,定时器开始生效
return 0;
}
static void __exit hello_exit (void)
{
del_timer(&timer);//卸载模块时,删除定时器
printk("Hello module exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("zx");
MODULE_LICENSE("Dual BSD/GPL");
二、hrtimer定时器
void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
enum hrtimer_mode mode);// 函数初始化定时器工作模式
//which_clock可以是CLOCK_REALTIME、CLOCK_MONOTONIC、CLOCK_BOOTTIME中的一种,
//mode则可以是相对时间HRTIMER_MODE_REL,也可以是绝对时间HRTIMER_MODE_ABS。
int hrtimer_start(struct hrtimer *timer, ktime_t tim,
const enum hrtimer_mode mode);//hrtimer_start激活该定时器
//根据time和mode参数的值计算hrtimer的超时时间,并设置到timer->expire域。
//expire设置的是绝对时间,所以如果参数mode的值为HRTIMER_MODE_REL(即参数tim的值为相对时间),
//那么需要将tim的值修正为绝对时间:expire = tim + timer->base->get_time(),
//调用enqueue_hrtimer,将hrtimer加入到红黑树中。
int hrtimer_cancel(struct hrtimer *timer);//使用hrtimer_cancel取消一个hrtimer
定时器一旦到期,function字段指定的回调函数会被调用,该函数的返回值为一个枚举值,它决定了该hrtimer是否需要被重新激活。
enum hrtimer_restart {
HRTIMER_NORESTART, /* Timer is not restarted /
HRTIMER_RESTART, / Timer must be restarted */
};
把hrtimer的到期时间推进一个tick周期,返回HRTIMER_RESTART表明该hrtimer需要再次启动,以便产生下一个tick事件。
hrtimer_forward(timer, now, tick_period);
#include <linux/input.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/hrtimer.h>
#include <linux/of_gpio.h>
#include <linux/io.h>
#include <linux/regulator/consumer.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/of.h>
#include <linux/i2c.h>
#include <linux/i2c/mms114.h>
#include <linux/input/mt.h>
#include <linux/interrupt.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
unsigned int timer_count=0;
struct hrtimer hrtimer_test_timer;
ktime_t m_kt;
int value=2000;
static enum hrtimer_restart hrtimer_test_timer_poll(struct hrtimer *timer)
{
printk("================timer_count=%d ==============\n",timer_count++);
hrtimer_forward(timer, timer->base->get_time(), m_kt);//hrtimer_forward(timer, now, tick_period);
//return HRTIMER_NORESTART;
return HRTIMER_RESTART;
}
int hrtimer_test_ioctl(struct inode *n, struct file *f, unsigned int cmd, unsigned long value){
}
ssize_t hrtimer_test_write(struct file *f, const char __user *buf, size_t t, loff_t *len){
int i;
int ret = -1;
printk("hrtimer Debug buf:%x size:%d\n",*buf,t);
if(*buf == '0'){
printk("hrtimer_start\n");
m_kt=ktime_set(value / 1000, (value % 1000) * 1000000);
hrtimer_start(&hrtimer_test_timer,m_kt, HRTIMER_MODE_REL);
}
else if(*buf == '1'){
printk("hrtimer_cancel\n");
hrtimer_cancel(&hrtimer_test_timer);
}
return t;
}
static const struct file_operations hrtimer_test_fops =
{
.owner = THIS_MODULE,
.write = hrtimer_test_write,
.unlocked_ioctl = hrtimer_test_ioctl,
};
struct miscdevice hrtimer_test_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "hrtimer_test",
.fops = &hrtimer_test_fops,
};
static int __init hrtimer_test_init(void)
{
int ret;
ret = misc_register(&hrtimer_test_dev);
hrtimer_init(&hrtimer_test_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
hrtimer_test_timer.function = hrtimer_test_timer_poll;
return 0;
}
static void __exit hrtimer_test_exit(void)
{
misc_deregister(&hrtimer_test_dev);
}
module_init(hrtimer_test_init);
module_exit(hrtimer_test_exit);
MODULE_AUTHOR("hrtimer<hrtimer@hrtimer.cc>");
MODULE_LICENSE("GPL");