参考文章:http://www.ibm.com/developerworks/cn/linux/l-timers-list/
1.标准计时器:
使用方法:
(1)定义一个定时器结构体:
struct timer_list my_timer
(2)初始化并设置回调函数
setup_timer( &my_timer, my_timer_callback, 0 );
(3)实现回调函数
void my_timer_callback( unsigned long data )
{
printk( "my_timer_callback called (%ld).\n", jiffies );
mod_timer( &my_timer, jiffies + msecs_to_jiffies(200) );
}
(4) 启动、停止定时器
start: mod_timer( &my_timer, jiffies + msecs_to_jiffies(200) );
stop: del_timer(&my_timer);
2.高精度定时器:
使用方法:
(1)定义一个高精度结构体:
struct hrtimer hr_timer;
(2)初始化并设置回调函数
hrtimer_init( &hr_timer , CLOCK_MONOTONIC, HRTIMER_MODE_REL );
ihr_timer .function=hrtimer_callback;
(3)实现回调函数
static enum hrtimer_restart hrtimer_callback ( struct hrtimer *timer )
{
printk("hrtimer callback running !\n");
//如果加上hrtimer_start这句,就回反复回调
//ktime_set( 0, 1* 1000 * 1000) 括号里前面一个参数表示秒,后面一个参数表示微妙,此时我们设定的时间是1ms
hrtimer_start( &hr_timer , ktime_set( 0, 1* 1000 * 1000), HRTIMER_MODE_REL );
return HRTIMER_NORESTART;
}
(4) 启动、停止定时器
start: hrtimer_start( &hr_timer , ktime_set( 0, 1* 1000 * 1000), HRTIMER_MODE_REL );
stop: hrtimer_cancel(&infrared_data->hr_infrared_timer);
阅读(2726) | 评论(0) | 转发(0) |