深入浅出Linux设备驱动编程--定时器

简介:
定时器
Linux内核中定义了一个timer_list结构,我们在驱动程序中可以利用之:
struct timer_list {
       struct list_head list;
       unsigned long expires; // 定时器到期时间
       unsigned long data; // 作为参数被传入定时器处理函数
       void (*function)(unsigned long);
};
下面是关于timer的API函数:
增加定时器
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);
              }
       }
}


Trackback: [url]http://tb.donews.net/TrackBack.aspx?PostId=1082425[/url]



 本文转自 21cnbao 51CTO博客,原文链接:http://blog.51cto.com/21cnbao/120092,如需转载请自行联系原作者


相关文章
|
13天前
|
Linux
【Linux系统编程】基础指令(二)(下)
【Linux系统编程】基础指令(二)
|
13天前
|
Linux C语言
【Linux系统编程】基础指令(二)(上)
【Linux系统编程】基础指令(二)
|
2天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
5天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
12 0
|
5天前
|
Linux
linux驱动层输出dev_dbg打印信息
linux驱动层输出dev_dbg打印信息
10 0
|
13天前
|
存储 Unix Linux
【Linux系统编程】基础指令(三)
【Linux系统编程】基础指令(三)
|
13天前
|
Linux
【Linux系统编程】基础指令(一)(下)
【Linux系统编程】基础指令(一)
|
13天前
|
人工智能 Unix Linux
【Linux系统编程】基础指令(一)(上)
【Linux系统编程】基础指令(一)
|
13天前
|
Unix 大数据 Linux
【Linux系统编程】Linux背景知识
【Linux系统编程】Linux背景知识