1:内核定时器:
在内核中有系统自带的定时器,达到一定的时间就可以进行一个操作,这个和单片机中的定时器中断一样。
包含头文件是
Timer.h (\include\linux)
结构体是timer_list
struct timer_list{
/*
* All fields that change during normal runtime grouped to the
* same cacheline
*/
struct list_head entry;
unsigned long expires;
struct tvec_base *base;
void (*function)(unsigned long);
unsigned long data;
int slack;
#ifdef CONFIG_TIMER_STATS
int start_pid;
void *start_site;
char start_comm[16];
#endif
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
};
@1:unsigned long expires; 定时的时间
@2:void (*function)(unsigned long); 具体执行的函数
@3:unsigned long data; 作为参数 传入function函数
使用方法:
(1) 定义一个timer_list的对象 timer
(2)初始化timer中的data字段,function字段,expires的字段
(2) 调用init_timer(&timer)
或者是使用其他函数,这个用实例来讲,所属平台是amlogic中,驱动是key_pad
利用定时器,去不断轮询看那个键位按下
setup_timer(&kp->timer, kp_timer_sr, (unsigned int)kp) ;
mod_timer(&kp->timer, jiffies+msecs_to_jiffies(100));
其中setup_timer的定义是
#define setup_timer(timer, fn, data) \
__setup_timer((timer), (fn), (data), 0)
#define __setup_timer(_timer, _fn, _data, _flags) \
do { \
__init_timer((_timer), (_flags)); \
(_timer)->function = (_fn); \
(_timer)->data = (_data); \
} while (0)
实际上仍然调用的是init_timer,调用的是kp_timer_sr,传入的数据的data。
mod_timer(&kp->timer, jiffies+msecs_to_jiffies(100));
int mod_timer(struct timer_list *timer, unsigned long expires)
{
expires = apply_slack(timer, expires);
/*
* This is a common optimization triggered by the
* networking code - if the timer is re-modified
* to be the same thing then just return:
*/
if (timer_pending(timer) && timer->expires == expires)
return 1;
return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
}
这个函数的作用是修改timer的定时时间。
</