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

驱动的运行结果:

最新文章

  1. hao916,hao123,2345.com浏览器劫持-分析与清除
  2. redis命令String
  3. linq lamada
  4. Python入门笔记(5):对象
  5. 译文 对无障碍网页应用(ARIA)的选择
  6. java中运算符的解析和计算
  7. Claim-based-security for ASP.NET Web APIs using DotNetOpenAuth
  8. 网络防火墙实战-基于pfsense(1)
  9. Unity3D——窗体介绍
  10. [Code] 中缀式转后缀式
  11. 2017 国庆湖南 Day5
  12. 小程序+node+mysql做的小项目
  13. 阿里云-免费SSL证书申请及验证步骤
  14. NOIP2016解题报告
  15. geopandas overlay 函数报错问题解决方案
  16. sqlserver 空 处理
  17. Node入门教程(6)第五章:node 模块化(上)模块化演进
  18. GC知识随笔
  19. java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl.&lt;init&gt;(Ljava/util/LinkedHashSet;Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)
  20. java String标准格式转换Date(yyyy-MM-dd HH:mm:ss)

热门文章

  1. pgbouncer+pg(fdw)+pg(datanode)分表方案
  2. JAVA JDBC连接 SQLServer2012
  3. RMQ求区间最值 nlog(n)
  4. AJAX简单的数据增删改与分页应用
  5. extjs 4.2 日期控件 选择时分秒功能
  6. java的I/O操作:文件的路径
  7. hdu5390 tree
  8. JS 实现中英文翻译
  9. ECharts切换主题
  10. 夺命雷公狗---node.js---17之项目的构建在node+express+mongo的博客项目2之一,二级路由