hrtimer高精度定时器的简单使用【学习笔记】

时间:2021-08-10 08:55:04
 1 #include <linux/module.h>
 2 
 3 #include <linux/kernel.h>
 4 #include <linux/hrtimer.h>
 5 #include <linux/jiffies.h>
 6 
 7 
 8 static struct hrtimer timer;
 9 ktime_t kt;
10 
11 static enum hrtimer_restart        hrtimer_hander(struct hrtimer *timer)
12 {
13     printk("zbzhuang:I am in hrtimer hander\r\n");
14 
15     hrtimer_forward(timer,timer->base->get_time(),kt);
16 
17     return HRTIMER_RESTART;
18         
19 
20 }
21 
22 static int __init test_init(void)
23 {
24     printk("zbzhuang:---------%s-----------\r\n",__func__);
25 
26     kt = ktime_set(1,10);
27     hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
28     hrtimer_start(&timer,kt,HRTIMER_MODE_REL);
29     timer.function = hrtimer_hander;
30 
31     return 0;
32 }
33 
34 static void __exit test_exit(void)
35 {
36     hrtimer_cancel(&timer);
37     printk("zbzhuang------------test over---------------\r\n");
38 }
39 
40 
41 module_init(test_init);
42 module_exit(test_exit);
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("zbzhuang@qq.com");