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
- /* Linux/Drivers/ker-driver.c
- * Author: woodpecker <Pecker.hu@gmail.com>
- *
- * Kernel-driver
- *
- * This file is subject to the terms and conditions of the GNU General Public
- * License. See the file copying in the main directory of this archive
- * More details.
- *
- */
- # Include <Linux/module. h>/* module_license */
- # Include <Linux/kernel. h>/* printk, pr_info */
- # Include <Linux/errno. h>/* einval, eagain, etc .*/
- # Include <Linux/err. h>/* is_err */
- # Include <Linux/FB. h>/* FB header file */
- # Include <Linux/init. h>/* module_init */
- # Include <Linux/semaphore. h>/* init_mutex APIs */
- # Include <Linux/mm. h>/* vm_area_struct */
- # Include <Linux/dma-mapping.h>/* DMA APIs */
- # Include <Linux/delay. h>/* mdelay, msleep */
- # Include <Linux/hrtimer. h>
- # Include <Linux/time. h>/* struct timespec */
- # Define ker_print (FMT,...) printk ("<Ker-driver>" FMT, ##__ va_args __);
- Static struct hrtimer vibe_timer;
- Static struct work_struct vibe_work;
- Static void vibe_work_func (struct work_struct * Work)
- {
- Ker_print ("vibe_work_func: msleep (50)/n ");
- Msleep (50);/* CPU sleep */
- }
- Static Enum hrtimer_restart vibrator_timer_func (struct hrtimer * timer)
- {
- Struct timespec uptime;
- Do_posix_clock_monotonic_gettime (& uptime );
- Ker_print ("Time: % lu. % 02lu/N ",
- (Unsigned long) uptime. TV _sec,
- (Uptime. TV _nsec/(nsec_per_sec/100 )));
- Ker_print ("vibrator_timer_func/N ");
- Schedule_work (& vibe_work );
- Return hrtimer_norestart;
- }
- Static int _ init ker_driver_init (void)
- {
- Int value = 2000;/* Time Out setting, 2 seconds */
- Struct timespec uptime;
- Ker_print ("ker_driver_init/N ");
- Hrtimer_init (& vibe_timer, clock_monotonic, hrtimer_mode_rel );
- Vibe_timer.function = vibrator_timer_func;
- Hrtimer_start (& vibe_timer,
- Ktime_set (value/1000, (Value % 1000) * 1000000), hrtimer_mode_rel );
- // Static inline ktime_t ktime_set (const long secs, const unsigned long nsecs) the first parameter is second, and the second parameter is Nanosecond
- Do_posix_clock_monotonic_gettime (& uptime );
- Ker_print ("Time: % lu. % 02lu/N ",
- (Unsigned long) uptime. TV _sec,
- (Uptime. TV _nsec/(nsec_per_sec/100 )));
- Init_work (& vibe_work, vibe_work_func);/* intialize the work queue */
- Return 0;
- }
- Static void _ exit ker_driver_exit (void)
- {
- Hrtimer_cancel (& vibe_timer );
- }
- Module_init (ker_driver_init );
- Module_exit (ker_driver_exit );
- Module_author ("woodpecker <Pecker.hu@gmail.com> ");
- Module_description ("kernel driver ");
- Module_license ("GPL ");
Driver running result: