首页 技术 正文
技术 2022年11月8日
0 收藏 485 点赞 1,287 浏览 2918 个字

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工作队列的使用

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848