Linux内核高精度定时器hrtimer 使用实例

                   Linux内核高精度定时器hrtimer 使用实例

 

一、内核为高精度定时器重新设计了一套软件架构,它可以为我们提供纳秒级的定时精度,以满足对精确时间有迫切需求的应用程序或内核驱动,以下学习使用hrtimer(high resolution timer)高精度定时器。

 

二、hrtimer_init函数初始化定时器工作模式。which_clock可以是CLOCK_REALTIME、CLOCK_MONOTONIC、CLOCK_BOOTTIME中的一种,mode则可以是相对时间HRTIMER_MODE_REL,也可以是绝对时间HRTIMER_MODE_ABS。

void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
			 enum hrtimer_mode mode);

 三、设定超时回调函数。

timer.function = hr_callback;

四、使用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_start(struct hrtimer *timer, ktime_t tim,
			 const enum hrtimer_mode mode);

五、使用hrtimer_cancel取消一个hrtimer。

int hrtimer_cancel(struct hrtimer *timer);

六、定时器一旦到期,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);
 
	return HRTIMER_RESTART;
}

 

八、测试代码

#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");  

九、运行结果

  • 11
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
hrtimerLinux内核中的一个重要功能,可提供高精度定时器功能。它可以在纳秒级别的精度下进行定时,主要用于实时任务或需要高精度定时的应用。 使用hrtimer高精度的方法如下: 1. 定义hrtimer使用hrtimer之前,首先需要定义一个hrtimer对象。可以通过声明struct hrtimer类型的变量来完成,例如: struct hrtimer my_hrtimer; 2. 初始化hrtimer: 初始化hrtimer对象可以使用hrtimer_init函数,该函数有三个参数:hrtimer对象,时钟源和是相对还是绝对时间。根据具体需要,可以选择不同的时钟源,如CLOCK_MONOTONIC或CLOCK_REALTIME。 3. 设置并启动hrtimer: 设置hrtimer对象的定时器周期和回调函数,然后使用hrtimer_start函数启动hrtimer。设置定时器周期可以使用hrtimer_set_periodic或hrtimer_set_expires函数。回调函数会在定时器到期时被调用。 4. 处理hrtimer到期: 当hrtimer到期时,会触发之前设置的回调函数,我们可以在回调函数中执行相应的操作。 5. 停止hrtimer: 如果需要停止hrtimer,可以使用hrtimer_cancel函数。 综上所述,通过使用Linux内核hrtimer模块,我们可以实现高精度的定时功能。这对于实时任务或需要高精度定时的应用至关重要。通过定义、初始化、设置和启动hrtimer对象,可以实现定时器的定期触发和相应的操作。同时,可以根据需要选择不同的时钟源来满足特定的应用需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值