转自:http://blog.csdn.net/waverider2012/article/details/38305785

hrtimer高精度定时器的interval由ktime_set(const long secs, const unsigned long nsecs)决定,可做到ns级。此处的例子为5ms interval:

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/hrtimer.h>
  4. #include <linux/ktime.h>
  5. MODULE_LICENSE("GPL");
  6. static struct hrtimer hr_timer;
  7. static struct work_struct wq_hrtimer;
  8. static ktime_t ktime;
  9. static unsigned int interval=5000; /* unit: us */
  10. struct timespec uptimeLast;
  11. static unsigned int count=0;
  12. #define COUNT_INTERVAL 4000
  13. unsigned long long diff_tv(struct timespec start, struct timespec end) {
  14. return (end.tv_sec-start.tv_sec)*1000000000+(end.tv_nsec-start.tv_nsec);
  15. }
  16. enum hrtimer_restart my_hrtimer_callback( struct hrtimer *timer )
  17. {
  18. schedule_work(&wq_hrtimer);
  19. return HRTIMER_NORESTART;
  20. }
  21. static void wq_func_hrtimer(struct work_struct *work)
  22. {
  23. struct timespec uptime;
  24. hr_timer.function = my_hrtimer_callback;
  25. ktime = ktime_set( interval/1000000, (interval%1000000)*1000 );
  26. hrtimer_start(&hr_timer, ktime, HRTIMER_MODE_REL );
  27. /* print time every COUNT_INTERVAL*interval second*/
  28. if(count%COUNT_INTERVAL==0)
  29. {
  30. do_posix_clock_monotonic_gettime(&uptime);
  31. printk(KERN_INFO"hrtimer:%9lu sec, %9lu ns, interval_delay=%lu ns\n",
  32. (unsigned long) uptime.tv_sec, uptime.tv_nsec,
  33. (unsigned long)(diff_tv(uptimeLast, uptime)-interval*1000*COUNT_INTERVAL) \
  34. /COUNT_INTERVAL);
  35. uptimeLast=uptime;
  36. }
  37. count++;
  38. }
  39. static int __init module_hrtimer_init( void )
  40. {
  41. struct timespec uptime;
  42. printk(KERN_INFO"HR Timer module installing\n");
  43. hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
  44. ktime = ktime_set( interval/1000000, (interval%1000000)*1000 );
  45. hr_timer.function = my_hrtimer_callback;
  46. hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
  47. do_posix_clock_monotonic_gettime(&uptime);
  48. uptimeLast = uptime;
  49. printk(KERN_INFO"hrtimer:%9lu sec, %9lu ns\n", (unsigned long) uptime.tv_sec,
  50. uptime.tv_nsec );
  51. INIT_WORK(&wq_hrtimer, wq_func_hrtimer);
  52. return 0;
  53. }
  54. static void __exit module_hrtimer_exit( void )
  55. {
  56. int ret;
  57. ret = hrtimer_cancel( &hr_timer );
  58. if (ret)
  59. printk("The timer was still in use...\n");
  60. printk("HR Timer module uninstalling\n");
  61. return;
  62. }
  63. module_init(module_hrtimer_init);
  64. module_exit(module_hrtimer_exit);

如果在my_hrtimer_callback()里面直接返回HRTIMER_RESTART会导致立即重新进入my_hrtimer_callback()。这时shell对输入没有任何响应。

所以为了解决这个问题,创建了一个work queue,由my_hrtimer_callback() enqueue这个工作队列。在work queue的处理函数里面重启hrtimer。

但是这样带来的负面影响是进入hrtimer_callback和wq_func被调用之间有Linux系统调度引入的延迟,导致interval出现误差。经过实测,在ZC706缺省配置下,这个延迟大约是17.5us (hrtimer interval为5ms,每20秒计算一次interval误差)。

    1. root@zynq:~/nfs/hrtimer# insmod hrtimer.ko
    2. HR Timer module installing
    3. hrtimer:    2900 sec, 993366078 ns
    4. hrtimer:    2900 sec, 998395278 ns, interval_delay=369966 ns
    5. hrtimer:    2921 sec,  69525447 ns, interval_delay=17782 ns
    6. hrtimer:    2941 sec, 139764655 ns, interval_delay=17559 ns
    7. hrtimer:    2961 sec, 210029519 ns, interval_delay=17566 ns
    8. hrtimer:    2981 sec, 280465631 ns, interval_delay=17609 ns
    9. hrtimer:    3001 sec, 350677038 ns, interval_delay=17552 ns
    10. hrtimer:    3021 sec, 420625114 ns, interval_delay=17487 ns
    11. hrtimer:    3041 sec, 490744847 ns, interval_delay=17529 ns

Linux下的hrtimer高精度定时器【转】的更多相关文章

  1. linux下jiffies定时器和hrtimer高精度定时器【转】

    本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...

  2. 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】

    关键词:Android  linux hrtimer 蜂鸣器  等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418  作 ...

  3. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  4. hrtimer高精度定时器的简单使用【学习笔记】

    #include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...

  5. 高精度定时器实现 z

    1背景Permalink .NET Framework 提供了四种定时器,然而其精度都不高(一般情况下 15ms 左右),难以满足一些场景下的需求. 在进行媒体播放.绘制动画.性能分析以及和硬件交互时 ...

  6. Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现

    转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...

  7. Linux下的定时器:alarm()与setitimer()

    Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...

  8. Linux下的定时器

    以下摘自linux下的man文件:(man  getitimer) #include  <sys/time.h> int  getitimer(int which,  struct iti ...

  9. Linux下定时器

    http://unix8.net/linux%E4%B8%8B%E5%AE%9A%E6%97%B6%E5%99%A8.html 一. 基础知识 1.时间类型.Linux下常用的时间类型有4个:time ...

随机推荐

  1. python3 安装win32clipboard 和 win32con 报No matching distribution found for win32con错误

    win32con.win32clipboad不能用pip install 安装,也不能够查找到这个包,原来,这个是pypiwin32的一部分,直接安装pypiwin32就可以了 pip install ...

  2. [P2387魔法森林

    题面 题意: 给出一个图,边权有两维,a与b. 求1到n的一条路径使得路径经过的边的最大的a与b的和最小,输出最小之和. \(Solution:\) 如果做过这题,那么就显得很简单了很好想了. 又是想 ...

  3. ubuntu中tensorflow安装

    首先加入了CUDA_HOM环境变量,然后下载了一个分析工具库,结果E: 无法定位软件包 libcupti-dev,在etc/apt 的sources.list 添加镜像源 deb http://arc ...

  4. Python 学习笔记之—— PIL 库

    PIL,全称 Python Imaging Library,是 Python 平台一个功能非常强大而且简单易用的图像处理库.但是,由于 PIL 仅支持到Python 2.7,加上年久失修,于是一群志愿 ...

  5. 机器视觉必知-GenICam相机通用接口标准

    机器视觉必知-GenICam相机通用接口标准 GenICam(相机通用接口): 一种通用软件接口 通用相机接口标准 目前机器视觉行业所使用的相机几乎均以相同方式来进行配置,即:---通过在注册表中的读 ...

  6. 延迟加载(Lazyload)三种实现方式

    定义:延迟加载也称为惰性加载,即在长网页中延迟加载图像.用户滚动到它们之前,视口外的图像不会加载.这与图像预加载相反,在长网页上使用延迟加载将使网页加载更快.在某些情况下,它还可以帮助减少服务器负载. ...

  7. [Leetcode] add binary 二进制加法

    Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...

  8. linux管理(二)---网络使用情况的监控

    我们经常在监控服务器或者排查程序性能瓶颈时需要知道  网络带宽的使用情况,看看带宽是不是瓶颈. linux系统中监控网络的工具和命令很多. 但其实主要分2种,一种是实时监控带宽情况(速度如何),一种是 ...

  9. 树剖模板by fcdalao

    #include<bits/stdc++.h> using namespace std; ; *MX]; *MX]; int n,Index,fir[MX],fa[MX],dfn[MX], ...

  10. BZOJ_day5

    32题...今天颓了不想再写了