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

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

    驱动的运行结果:

  • 相关阅读:
    学习之路总结
    一个怀旧的人
    struts2+ibatis+spring框架整合(一)
    大雪来的不知所措
    struts2+ibatis+spring框架整合(二)
    20110610上午java考试复数题
    直到永远……
    2012年10月份考试后感
    Use sp_MSForEachDB instead of your own loop
    Execute TSQL using OpenRowSet
  • 原文地址:https://www.cnblogs.com/Ph-one/p/5891462.html
Copyright © 2020-2023  润新知