驱动开发笔记--hrtimer高精度定时器

参考

linux下jiffies定时器和hrtimer高精度定时器


背景

jiffies定时器,HZ=100,精度只能达到10ms。
hrtimer高精度定时器,可做到ns级,此处做到毫秒如下例:

历程
#include <linux/dma-mapping.h> //DO-->hrtimer包含以下三个头文件 /* DMA APIs             */   
#include <linux/hrtimer.h>  

#include <linux/time.h>           /* struct timespec    */

#define KER_PRINT(fmt, ...) printk("<ker-driver>"fmt, ##__VA_ARGS__);   
static struct hrtimer vibe_timer; 

static struct work_struct vibe_work;  
static int value = 2000;   /*注:以毫秒ms为单位 Time out setting,2 seconds */

static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)  //DO-->回调函数,中断时调用 
{    

    struct timespec uptime;

    do_posix_clock_monotonic_gettime(&uptime);  
    KER_PRINT("Time:%lu.%02lu\n", 

            (unsigned long) uptime.tv_sec,  
            (uptime.tv_nsec / (NSEC_PER_SEC / 1000))); 

    KER_PRINT("vibrator_timer_func\n");   
    schedule_work(&vibe_work);  
    return HRTIMER_NORESTART; 

}  
static void vibe_work_func(struct work_struct *work)  //DO-->工作队列函数
{ 

    KER_PRINT("'vibe_work_func'-->work\n");  
   // msleep(50); /* CPU sleep */

    vibe_timer.function = vibrator_timer_func;  
    hrtimer_start(&vibe_timer, 

        ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);  
} 

static void ker_driver_init(void)                        //DO-->hrtimer高精度定时器初始化函数
{ 

    struct timespec uptime; 

    KER_PRINT("ker_driver_init\n");  
    hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);  //DO-->hrtimer定时器初始化

    vibe_timer.function = vibrator_timer_func;                     //DO-->hrtimer定时器回调函数
    hrtimer_start(&vibe_timer,  

        ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);  //DO-->hrtimer定时器时间初始化,其中ktime_set(秒,纳秒)

    do_posix_clock_monotonic_gettime(&uptime);    //线程建立时间,用于比较看(定时器)此时时间
    KER_PRINT("Time:%lu.%02lu\n", 

            (unsigned long) uptime.tv_sec,  
            (uptime.tv_nsec / (NSEC_PER_SEC / 1000))); 

    INIT_WORK(&vibe_work, vibe_work_func);  /* Intialize the work queue */  //初始化工作队列

} 

 

static int32_t xxxx_init(void)

{

    ker_driver_init();

    ....

}
————————————————
版权声明:本文为CSDN博主「dosculler」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dosculler/article/details/7932315
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值