|
沙发

楼主 |
发表于 2009-9-29 16:30:43
|
只看该作者
struct timer_list 变量的初始化
为了把将要运行的函数注册到内核定时器目录上,需要使用包含函数处理信息的 struct timer_list 类型变量。定义了 #include <linux/timer> 后就可以使用 struct timer_list 结构体。 struct timer_list 结构体包含了多个域,但是多数用在内核内部。
下面是 struct timer_list 结构体的定义:
struct timer_list {
struct list_head entry;
unsigned long expires;
void (*function)(unsigned long);
unsigned long data;
struct tvec_base *base;
#ifdef CONFIG_TIMER_STATS
void *start_site;
char start_comm[16];
int start_pid;
#endif
}; 其中,与设备驱动相关的几个域为 unsigned long expires; unsigned long data; void (*function) (unsigned long); 。
1、unsigned long expires
是函数运行的起点,即设定内核定时器的结束时间。2.4 内核中 expires 值 expires 值与 jiffies 进行比较,当大小相同时启动函数。同样地,2.6 内核中则与 jiffies_64 比较,因此,expires 值为注册内核定时器起点的 jiffies 值 (或 jiffies_64) 与以 Hz 单位计时的延迟时间的和。下面是 2.4 内核和 2.6 内核中经过 0.3s 后运行的方法。
struct timer_list kerneltimer;
kerneltime.expires = jiffies + (3*HZ/10);
kerneltime.expires = get_jiffies_64() + (3*HZ/10);
2、unsigned long data;
内核定时器中运行的函数在定时器中断点运行,因此要获取与函数处理相关的数据参考地址。此时,主要指定可参考数据的起始地址,其值由函数的第一个变量传送。
3、void (*function) (unsigned long);
expires 值为注册在内核定时器上的结构体时间域,该值的大小与 jiffies 值或 jiffies_64 相同时,运行 function 域中的定义的函数。该函数没有返回值,unsigned long 型变量上传送上面 2 中的 data 域。
为了初始化 struct timer_list 结构体,使用 init_timer() 函数,关于 init_timer() 函数见:
http://www.groad.net/bbs/read.php?tid-1227.html
该函数为了处理 list ,初始化 timer 指向的结构体变量,接着再具体初始化 timer 结构体中设备驱动程序需要的 expires, data, function 等域。 下面示例:
char mng_data [128];
struct timer_list timer;
void kerneltimer_handler (unsigned long arg)
{
...
}
xxx_timer_init (...)
{
init_timer (&timer);
timer.expires = get_jiffies_64() + (3*Hz/10);
timer.data = (unsigned long) &mng_data[0];
timer.function = kerneltimer_handler;
} |
|