红联Linux门户
Linux帮助

linux3.4.2内核定时器time_list的使用

发布时间:2017-02-04 10:01:13来源:blog.csdn.net/wang__rongwei作者:JiffyKernel
在有些驱动程序中,我们需要用定时的功能去扫描一个硬件,或者说成去执行一个函数,那么我们可以采用内核动态定时器:
 
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.定义一个内核定时器
 
2.初始化内核结构体
init_timer(&xxx_timer);
xxx_timer.function = xxx_timer_function;
add_timer(&buttons_timer);
xxx_timer.function = xxx_timer_function这里就是指定定时时间到时,去执行的函数,其实干脆就叫定时器中断得了。
 
3.修改定时器的值:
mod_timer(&xxx_timer, jiffies+HZ/100);
将定时器的值设置为当前的内核计时器值+HZ/100,意思就是10ms以后执行xxx_timer_function函数,干脆叫定时器中断服务程序。
jiffies是内核的一个时钟节拍,研究过操作系统的都知道,相当于是这个CPU只要在工作,这个值就在++。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28125.html