Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4242652
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: LINUX

2011-12-10 19:45:54

驱动编写实现在虚拟机U-10.10上,使用PC主机,环境搭建在章节 构建内核树:分享 ubuntu10.10构建内核树   中

1. 在linux内核中,timer_list结构体对应一个定时器,2.6.35内核中,定义incude/linux/timer.h

  1. struct timer_list {
  2.   13 /*
  3.   14 * All fields that change during normal runtime grouped to the
  4.   15 * same cacheline
  5.   16 */
  6.   17 struct list_head entry
  7.   18 unsigned long expires;//定时器到期时间
  8.   19 struct
  9.   20
  10.   21 void (*function)(unsigned long);
  11.   22 unsigned long data; 作为参数传入functiong 函数中
  12.   23
  13.   24 int slack;
  14.   25
  15.   26#ifdef CONFIG_TIMER_STATS
  16.   27 void *start_site;
  17.   28 char start_comm[16];
  18.   29 int start_pid;
  19.   30#endif
  20.   31#ifdef CONFIG_LOCKDEP
  21.   32 struct lockdep_map lockdep_map;
  22.   33#endif
  23.   34};
  1. //定时器列表 一个双列表include/linux/list.h
  2.                 struct list_head { struct list_head *next, *prev;};
  1. 75struct tvec_base {
  2.   76 spinlock_t lock;
  3.   77 struct timer_list *running_timer;
  4.   78 unsigned long timer_jiffies;
  5.   79 unsigned long next_timer;
  6.   80 struct tvec_root tv1;
  7.   81 struct tvec tv2;
  8.   82 struct tvec tv3;
  9.   83 struct tvec tv4;
  10.   84 struct tvec tv5;
  11.   85} ____cacheline_aligned;
 使用timer步骤

1. 定义定时器
   struct timer_list my_timer;
2. 初始化定时器
  1. init_timer(&my_timer);
  2. my_timer.function=my_timer_func该设置设置定时器触发时处理的函数
  3. my_timer.data=0;   初始化处理函数的参数值,若处理函数没有参数则可简单设置为0或任意其他数值
  4. my_timer.expires=jiffies+TT初始化定时器的到期节拍数
3. 增加定时器
 add_timer(struct timer_list *timer) 
  2.6.35定义/kernel/timer.c 
  1. 849void add_timer(struct timer_list *timer)
  2.  850{
  3.  851 BUG_ON(timer_pending(timer));
  4.  852 mod_timer(timer, timer->expires);
  5.  853}
 4. 删除定时器
 del_timer(struct timer_list *timer)
  
 注: del_timer_sync()和del_timer()的同步版,主要在多处理系统中使用,如果编译内核时不支持SMP,del_timer_sync()和del_timer()等价

5.修改定时器的expire
  mod_timer(struct time_list *timer,unsigned long expires)

程序一:
 my_timer定时器每个1s 打印1 和 second_timer定时器每隔2s打印2.附件:  timer1.rar    将.rar修改为.tar.bz2后解压
内核定时器并不是周期运行,它在超时后自动销毁。因此,如果要实现周期轮询,就需要在定时器执行函数返回前再次激活定时器。add_timer()重新激活

jiffies是Linux内核中的一个全局变量,用来记录自系统启动以来产生的节拍的总数。启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值。

HZ是内核定义的宏,在i386体系结构中定义为:
#define HZ 1000
2.6内核的时钟中断频率是1000,也就是说,在1秒里jiffies会被增加1000。
因此jiffies + 2 * HZ表示推后2秒钟。有时,需要更改已经激活的定时器,采用如下函数:
mod_timer(&my_timer, jiffies + new_delay);

如果需要在定时器超时前停止定时器,可以使用del_timer()函数:
处理器的情况下使用:
del_timer_sync(&polling_timer);


  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>

  4. #include <linux/time.h>
  5. #include <linux/timer.h>

  6. #define TT HZ/1

  7. struct timer_list my_timer; //设备要使用的定时器
  8. struct timer_list second_timer;

  9. static void my_timer_func(unsigned long p)//定时器处理函数
  10. {
  11.     printk("%d\n",1);
  12.     my_timer.expires=jiffies+TT;
  13.     //内核定时器并不是周期运行,它在超时后自动销毁
  14.     add_timer(&my_timer);
  15. }

  16. static void second_timer_func(unsigned long p)
  17. {
  18.     printk("%d\n",2);
  19.     second_timer.expires=jiffies+2*TT;
  20.     add_timer(&second_timer);    
  21. }

  22. static int __init my_timer_init(void)
  23. {
  24.     init_timer(&my_timer);
  25.     my_timer.function=my_timer_func;
  26.     my_timer.data=0;
  27.     my_timer.expires=jiffies+TT;
  28.     add_timer(&my_timer);

  29.     init_timer(&second_timer);
  30.     second_timer.function=second_timer_func;
  31.     second_timer.data=0;
  32.     second_timer.expires=jiffies+2*TT;
  33.     add_timer(&second_timer);

  34.     printk("welcom\n");
  35.     return 0;
  36. }

  37. static void __exit my_timer_exit(void)
  38. {
  39.     del_timer(&my_timer);
  40.     del_timer(&second_timer);
  41.     printk("bye\n");
  42. }

  43. module_init(my_timer_init);
  44. module_exit(my_timer_exit);

  45. MODULE_LICENSE("Dual BSD/GPL");
  46. MODULE_AUTHOR("yuweixian4230.blog.chinaunx.net");
Makefile

  1. obj-m:=timer.o
  2. KERNELDIR := /lib/modules/2.6.35-31-generic/build
  3. PWD :=$(shell pwd)

  4. modules:
  5.         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

  6. modules_install:
  7.         $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

  8. clean:
  9.         rm -rf *.o *.ko *.mod.c *.order *.symvers

dmesg | tail -15 或
cat /var/log/syslog | tail -15 
   

   
2.程序二
  功能:简单延迟当前进程执行的程序,延迟是通过定时器来实现的;
  附件: timer2.rar    将rar修改为tar.bz2
 
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>

  4. #include <linux/sched.h>
  5. #include <linux/timer.h>

  6. struct timer_list my_timer;// defination timer
  7. int timeout=10*HZ;

  8. static void time_handler(unsigned long data)
  9. {
  10.     struct task_struct *p=(struct task_struct *)data;//定时器处理函数,执行该函数获取挂起进程的pid,唤醒该进程
  11.     wake_up_process(p);///唤醒进程
  12.     printk("current jiffies is %ld\n",jiffies);
  13. }

  14. static int __init timer_init(void)
  15. {
  16.     printk("my module worked\n");
  17.     init_timer(&my_timer);
  18.     my_timer.data=(unsigned long )current;////将当前进程的pid作为参数传递
  19.     my_timer.expires=jiffies+timeout//a
  20.     my_timer.function=time_handler;
  21.     add_timer(&my_timer);
  22.     printk("current jiffies is %ld\n",jiffies);
  23.     set_current_state(TASK_INTERRUPTIBLE);// 进程睡眠 
  24.     schedule();////挂起该进程 放弃CPU
  25.     del_timer(&my_timer);
  26.     return 0;
  27. }

  28. static void __exit timer_exit(void)
  29. {
  30.     printk("unloading my module.\n");
  31. }

  32. module_init(timer_init);
  33. module_exit(timer_exit);

  34. MODULE_AUTHOR("yuweixian4230.blog.chinaunx.net");
  35. MODULE_LICENSE("GPL");
  1. ywx@ywx:~/Desktop/module/timer/timer2$ dmesg | tail -5
  2. [ 578.542874] bye
  3. [13945.374106] my module worked
  4. [13945.374176] current jiffies is 3411343
  5. [13955.392720] current jiffies is 3413848
  6. [13962.993754] unloading my module.
  7. ywx@ywx:~/Desktop/module/timer/timer2$





阅读(1287) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~