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 plaincopy
to clipboardprint?·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······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 for
- * 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) 第一個參數為秒,第二個為納秒
- 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");
驅動的運行結果: