免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 722 | 回复: 0
打印 上一主题 下一主题

Linux设备驱动编程之定时器(八) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-29 10:01 |只看该作者 |倒序浏览
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

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);
  }
 }
 …
}

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/52475/showart_432625.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP