Linux设备驱动编程之定时器
2010-03-09 22:04
507 查看
Linux设备驱动编程之定时器
2006-10-26 13:34作者:
出处:Linux
责任编辑:方舟
相关专题:
Linux设备驱动程序开发入门
Linux内核中定义了一个timer_list结构,我们在驱动程序中可以利用之:
struct timer_list { struct list_head list; unsigned long expires; //定时器到期时间 unsigned long data; //作为参数被传入定时器处理函数 void (*function)(unsigned long); }; |
增加定时器
void add_timer(struct timer_list * timer); |
int del_timer(struct timer_list * timer); |
修改定时器的expire
int mod_timer(struct timer_list *timer, unsigned long expires); |
(1)timer、编写function;
(2)为timer的expires、data、function赋值;
(3)调用add_timer将timer加入列表;
(4)在定时器到期时,function被执行;
(5)在程序中涉及timer控制的地方适当地调用del_timer、mod_timer删除timer或修改timer的expires。
我们可以参考drivers/char/keyboard.c中键盘的驱动中关于timer的部分:
…
#include <linux/timer.h>
…
static struct timer_list key_autorepeat_timer =
{
function: key_callback
};
static void
kbd_processkeycode(unsigned char keycode, char up_flag, int autorepeat)
{
char raw_mode = (kbd->kbdmode == VC_RAW);
if (up_flag) {
rep = 0;
if(!test_and_clear_bit(keycode, key_down))
up_flag = kbd_unexpected_up(keycode);
} else {
rep = test_and_set_bit(keycode, key_down);
/* If the keyboard autorepeated for us, ignore it.
* We do our own autorepeat processing.
*/
if (rep && !autorepeat)
return;
}
if (kbd_repeatkeycode == keycode || !up_flag || raw_mode) {
kbd_repeatkeycode = -1;
del_timer(&key_autorepeat_timer);
}
…
/*
* Calculate the next time when we have to do some autorepeat
* processing. Note that we do not do autorepeat processing
* while in raw mode but we do do autorepeat processing in
* medium raw mode.
*/
if (!up_flag && !raw_mode) {
kbd_repeatkeycode = keycode;
if (vc_kbd_mode(kbd, VC_REPEAT)) {
if (rep)
key_autorepeat_timer.expires = jiffies + kbd_repeatinterval;
else
key_autorepeat_timer.expires = jiffies + kbd_repeattimeout;
add_timer(&key_autorepeat_timer);
}
}
…
}
相关文章推荐
- Linux设备驱动之timer定时器与延时
- linux驱动编程--设备模型3--平台设备解析
- linux字符设备驱动-定时器按键去抖笔记
- Linux驱动编程 step-by-step (四) 字符设备的注册与设备节点的自动创建
- linux网络设备应用与驱动编程学习4——模板与实例(A)
- Linux设备驱动——内核定时器
- linux 设备驱动编程
- Linux设备驱动编程之中断处理
- Linux字符设备驱动之定时器去抖动按键驱动
- Linux驱动编程 step-by-step (四) 字符设备的注册与设备节点的自动创建
- linux设备驱动编程-初探(1)--驱动程序概念
- Linux网络设备驱动编程
- Linux终端tty设备驱动编程
- Linux设备驱动编程之内存与I/O操作
- Linux设备驱动编程之内存与I/O操作…
- Linux网络设备驱动编程
- Linux设备驱动归纳总结(七):2.内核定时器
- Linux设备驱动编程之阻塞与非阻塞 2
- linux 设备驱动编程
- linux驱动开发之字符设备驱动编程步骤简述