1.hrtimers - 为高分辨率kernel定时器,可作为超时或周期性定时器使用

1). hrtimer_init初始化定时器工作模式。

hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 vibe_timer.function = vibrator_timer_func;

/* 设置定时器的回调函数,定时器到时该函数将被调用 */

static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)

 注:该回调函数为原子操作不能被中断

2). hrtimer_start的第二个参数用于设置超时参数。
  hrtimer_start(&vibe_timer,
  ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);

3). INIT_WORK初始化工作队列。

INIT_WORK(&vibe_work, vibe_work_func);

static void vibe_work_func(struct work_struct *work)

4). schedule_work调用工作队列。

schedule_work(&vibe_work);

  1. /* linux/drivers/ker-driver.c
  2. * Author: Woodpecker <Pecker.hu@gmail.com>
  3. *
  4. * kernel-driver
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License.  See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. */
  11. #include <linux/module.h>     /* MODULE_LICENSE     */
  12. #include <linux/kernel.h>     /* printk,pr_info     */
  13. #include <linux/errno.h>      /* EINVAL,EAGAIN,etc. */
  14. #include <linux/err.h>            /* IS_ERR             */
  15. #include <linux/fb.h>         /* FB header file     */
  16. #include <linux/init.h>           /* module_init        */
  17. #include <linux/semaphore.h>  /* init_MUTEX APIs    */
  18. #include <linux/mm.h>         /* vm_area_struct     */
  19. #include <linux/dma-mapping.h>  /* DMA APIs             */
  20. #include <linux/delay.h>      /* mdelay,msleep      */
  21. #include <linux/hrtimer.h>
  22. #include <linux/time.h>           /* struct timespec    */
  23. #define KER_PRINT(fmt, ...) printk("<ker-driver>"fmt, ##__VA_ARGS__);
  24. static struct hrtimer vibe_timer;
  25. static struct work_struct vibe_work;
  26. static void vibe_work_func(struct work_struct *work)
  27. {
  28. KER_PRINT("vibe_work_func:msleep(50)/n");
  29. msleep(50); /* CPU sleep */
  30. }
  31. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  32. {
  33. struct timespec uptime;
  34. do_posix_clock_monotonic_gettime(&uptime);
  35. KER_PRINT("Time:%lu.%02lu/n",
  36. (unsigned long) uptime.tv_sec,
  37. (uptime.tv_nsec / (NSEC_PER_SEC / 100)));
  38. KER_PRINT("vibrator_timer_func/n");
  39. schedule_work(&vibe_work);
  40. return HRTIMER_NORESTART;
  41. }
  42. static int __init ker_driver_init(void)
  43. {
  44. int value = 2000;   /* Time out setting,2 seconds */
  45. struct timespec uptime;
  46. KER_PRINT("ker_driver_init/n");
  47. hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  48. vibe_timer.function = vibrator_timer_func;
  49. hrtimer_start(&vibe_timer,
  50. ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
  51. //static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)  第一个参数为秒,第二个为纳秒
  52. do_posix_clock_monotonic_gettime(&uptime);
  53. KER_PRINT("Time:%lu.%02lu/n",
  54. (unsigned long) uptime.tv_sec,
  55. (uptime.tv_nsec / (NSEC_PER_SEC / 100)));
  56. INIT_WORK(&vibe_work, vibe_work_func);  /* Intialize the work queue */
  57. return 0;
  58. }
  59. static void __exit ker_driver_exit(void)
  60. {
  61. hrtimer_cancel(&vibe_timer);
  62. }
  63. module_init(ker_driver_init);
  64. module_exit(ker_driver_exit);
  65. MODULE_AUTHOR("Woodpecker <Pecker.hu@gmail.com>");
  66. MODULE_DESCRIPTION("Kernel driver");
  67. MODULE_LICENSE("GPL");

驱动的运行结果:

hrtimer和work工作队列的使用的更多相关文章

  1. Linux下的hrtimer高精度定时器【转】

    转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...

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

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

  3. 工作队列(workqueue) create_workqueue/schedule_work/queue_work

    --- 项目需要,在驱动模块里用内核计时器timer_list实现了一个状态机.郁闷的是,运行时总报错"Scheduling while atomic",网上搜了一下:" ...

  4. 第3.3 案例2: 工作队列 job queue

    第2个案例就是工作队列,典型的点对点的消息,一个Producer发送一个工作消息到队列去,具有Listener类的Consumer能够从工作队列中获得一个工作情况的消息,这个消息被这个消费者消费掉之后 ...

  5. 【译】RabbitMQ:工作队列(Work Queue)

    在第一篇我们写了两个程序通过一个命名的队列分别发送和接收消息.在这一篇,我们将创建一个工作队列在多个工作线程间分发耗时的工作任务. 工作队列的核心思想是避免立刻处理资源密集型任务导致必须等待其执行完成 ...

  6. rabbitmq消息队列——"工作队列"

    二."工作队列" 在第一节中我们发送接收消息直接从队列中进行.这节中我们会创建一个工作队列来分发处理多个工作者中的耗时性任务. 工作队列主要是为了避免进行一些必须同步等待的资源密集 ...

  7. RabbitMQ入门教程——工作队列

    什么是工作队列 工作队列是为了避免等待一些占用大量资源或时间操作的一种处理方式.我们把任务封装为消息发送到队列中,消费者在后台不停的取出任务并且执行.当运行了多个消费者工作进程时,队列中的任务将会在每 ...

  8. RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)

    工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...

  9. RabbitMQ 工作队列

    创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进 ...

随机推荐

  1. Leetcode: Palindrome Pairs

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  2. SWF类标准开头Tag

    [SWF(width="800", height="600", backgroundColor="#ffffff", frameRate=& ...

  3. TPageControl组件

    TPageControl的功能是创建多个Dialog页,而这些重叠的每一个页Dialog就是通过TTabSheet对象实现的

  4. poj 算法 分类

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 最近AC题:2528   更新时间:2011.09.22  ...

  5. 搜集好的java技术帖子,持续更新,java程序员的要求

    1.Java NIO 系列教程 2.Java实现 二叉搜索树算法(BST) 3. Java 并发工具包 java.util.concurrent 用户指南 4.架构师之路系列:http://blog. ...

  6. C++之路进阶——codevs2366(朋友圈)

    2366 朋友圈 2012年省队选拔赛河北  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 在很久很久以前,曾经 ...

  7. Tomcat8.5

    说明:Tomcat服务器上一个符合J2EE标准的Web服务器,在tomcat中无法运行EJB程序,如果要运行可以选择能够运行EJB程序的容器WebLogic,WebSphere,Jboss等Tomca ...

  8. 在linux中的virtualbox无法挂载usb设备的解决方法

    方法来源于网络. 在安装完virtualbox之后,virtualbox会建立一个名为 vboxusers 的组,将你的用户名加入到该组即可. 命令参考: #usermod -a -G vboxuse ...

  9. C语言初学者代码中的常见错误与瑕疵(2)

    问题: 另一种阶乘 大家都知道阶乘这个概念,举个简单的例子:5!=1*2*3*4*5. 现在我们引入一种新的阶乘概念,将原来的每个数相乘变为i不大于n的所有奇数相乘 例如:5!!=1*3*5.现在明白 ...

  10. HTML5动画软件工具编辑器 HTML5动画分类 工具推荐

    接下来介绍几款制作HTML5动画的工具,它们可以分为几类: 1.导出canvas动画: Flash CC(13.1).Animation.Radi 2.导出DIV+CSS3动画: HTML5 Make ...