Use of hrtimer and Work work queue

Source: Internet
Author: User

 

1. hrtimers-high-resolution kernel timer, which can be used as a timeout or periodic Timer

1). hrtimer_init initializes the timer working mode.

Hrtimer_init (& vibe_timer, clock_monotonic, hrtimer_mode_rel );
Vibe_timer.function = vibrator_timer_func;

/* Set the timer callback function, which will be called by the timer */

Static Enum hrtimer_restart vibrator_timer_func (struct hrtimer * timer)

Note: This callback function is an atomic operation and cannot be interrupted.

 

2) The second parameter of hrtimer_start is used to set the timeout parameter.
Hrtimer_start (& vibe_timer,
Ktime_set (value/1000, (Value % 1000) * 1000000), hrtimer_mode_rel );

 

3). init_work initializes the work queue.

Init_work (& vibe_work, vibe_work_func );

Static void vibe_work_func (struct work_struct * Work)

 

4). schedule_work calls the work queue.

Schedule_work (& vibe_work );

 

View plaincopy
To clipboardprint? ··· · 50 ······· · 90 ····· · 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
  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) the first parameter is second, and the second parameter is Nanosecond
  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 ");

 

Driver running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.