当前位置: 首页 > news >正文

hrtimer和work工作队列的使用

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

 

 

view plain copy to clipboard print ?
 
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  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.   
  12. #include <linux/module.h>     /* MODULE_LICENSE     */  
  13. #include <linux/kernel.h>     /* printk,pr_info     */  
  14. #include <linux/errno.h>      /* EINVAL,EAGAIN,etc. */  
  15. #include <linux/err.h>            /* IS_ERR             */  
  16. #include <linux/fb.h>         /* FB header file     */  
  17. #include <linux/init.h>           /* module_init        */  
  18. #include <linux/semaphore.h>  /* init_MUTEX APIs    */  
  19. #include <linux/mm.h>         /* vm_area_struct     */  
  20. #include <linux/dma-mapping.h>  /* DMA APIs             */  
  21. #include <linux/delay.h>      /* mdelay,msleep      */  
  22. #include <linux/hrtimer.h>   
  23. #include <linux/time.h>           /* struct timespec    */  
  24.   
  25. #define KER_PRINT(fmt, ...) printk("<ker-driver>"fmt, ##__VA_ARGS__);  
  26. static struct hrtimer vibe_timer;  
  27. static struct work_struct vibe_work;  
  28.   
  29. static void vibe_work_func(struct work_struct *work)  
  30. {  
  31.     KER_PRINT("vibe_work_func:msleep(50)/n");  
  32.     msleep(50); /* CPU sleep */  
  33. }  
  34.   
  35. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)   
  36. {             
  37.     struct timespec uptime;  
  38.       
  39.     do_posix_clock_monotonic_gettime(&uptime);  
  40.     KER_PRINT("Time:%lu.%02lu/n",  
  41.             (unsigned long) uptime.tv_sec,  
  42.             (uptime.tv_nsec / (NSEC_PER_SEC / 100)));  
  43.       
  44.     KER_PRINT("vibrator_timer_func/n");   
  45.     schedule_work(&vibe_work);  
  46.     return HRTIMER_NORESTART;  
  47. }  
  48.   
  49. static int __init ker_driver_init(void)  
  50. {  
  51.   
  52.     int value = 2000;   /* Time out setting,2 seconds */  
  53.     struct timespec uptime;  
  54.       
  55.     KER_PRINT("ker_driver_init/n");  
  56.     hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);  
  57.     vibe_timer.function = vibrator_timer_func;  
  58.     hrtimer_start(&vibe_timer,  
  59.         ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);  
  60.       //static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)  第一个参数为秒,第二个为纳秒
  61.     do_posix_clock_monotonic_gettime(&uptime);  
  62.     KER_PRINT("Time:%lu.%02lu/n",  
  63.             (unsigned long) uptime.tv_sec,  
  64.             (uptime.tv_nsec / (NSEC_PER_SEC / 100)));  
  65.   
  66.     INIT_WORK(&vibe_work, vibe_work_func);  /* Intialize the work queue */  
  67.     return 0;  
  68.   
  69. }  
  70.   
  71. static void __exit ker_driver_exit(void)  
  72. {  
  73.     hrtimer_cancel(&vibe_timer);  
  74. }  
  75.   
  76. module_init(ker_driver_init);  
  77. module_exit(ker_driver_exit);  
  78.   
  79. MODULE_AUTHOR("Woodpecker <Pecker.hu@gmail.com>");  
  80. MODULE_DESCRIPTION("Kernel driver");  
  81. MODULE_LICENSE("GPL");  

 

 

 

 

驱动的运行结果:

转载于:https://www.cnblogs.com/Ph-one/p/5891462.html

相关文章:

  • nautilus-open-terminal很有用的插件--鼠标右键打开终端
  • userdebug版本开机串口log打开
  • no branch 问题
  • 网页撤销后ubuntu本地撤销
  • 电子类网站
  • ubuntu查看内存占用和查看cpu使用情况的简单方法(ubuntu内存管理)
  • 文件映射mmap
  • Linux的bg和fg命令简单介绍
  • ubuntu下查看cpu信息
  • ubuntu安装和查看已安装
  • ISO C90 forbids mixed declarations and code 警告
  • Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
  • Linux下c++中的atoi、atol、atoll、atof函数调用实例
  • 图像处理的基本概念
  • c++中函数empty()怎么使用
  • 【EOS】Cleos基础
  • 【mysql】环境安装、服务启动、密码设置
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • express + mock 让前后台并行开发
  • happypack两次报错的问题
  • Iterator 和 for...of 循环
  • Java 最常见的 200+ 面试题:面试必备
  • Js基础——数据类型之Null和Undefined
  • Python进阶细节
  • React16时代,该用什么姿势写 React ?
  • Redis 中的布隆过滤器
  • 闭包,sync使用细节
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 当SetTimeout遇到了字符串
  • 分布式任务队列Celery
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 前嗅ForeSpider中数据浏览界面介绍
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 首页查询功能的一次实现过程
  • 我看到的前端
  • 小程序开发之路(一)
  • 协程
  • ​低代码平台的核心价值与优势
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #Spring-boot高级
  • #我与Java虚拟机的故事#连载15:完整阅读的第一本技术书籍
  • ${factoryList }后面有空格不影响
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (4.10~4.16)
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (二)linux使用docker容器运行mysql
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (离散数学)逻辑连接词
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (十八)三元表达式和列表解析
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。