Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1861623
  • 博文数量: 152
  • 博客积分: 3730
  • 博客等级: 上尉
  • 技术积分: 3710
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:36
个人简介

减肥,运动,学习,进步...

文章分类

全部博文(152)

文章存档

2016年(14)

2015年(17)

2014年(16)

2013年(4)

2012年(66)

2011年(35)

分类: LINUX

2011-12-25 13:33:29

我的环境:
Fedora 14 内核版本为2.6.38.1
开发板:ARM9  TQ2440
移植内核版本:linux-2.6.30.4

定时器在linux内核中主要是采用一个结构体实现的。但是需要注意定时器是一个只运行一次的对象,也就是当一个定时器结束以后,还需要重现添加定时器。但是可以采用mod_timer()函数动态的改变定时器到达时间。
这个驱动主要实现内核定时器的基本操作。内核定时器主要是是通过下面的结构体struct timer_list实现。需要的头文件包括#include,但是在实际开发过程中不需要包含该头文件,因为在sched.h中包含了该头文件。
  1. struct timer_list {
  2.     struct list_head entry;
  3.     unsigned long expires;

  4.     void (*function)(unsigned long);
  5.     unsigned long data;

  6.     struct tvec_base *base;
  7. #ifdef CONFIG_TIMER_STATS
  8.     void *start_site;
  9.     char start_comm[16];
  10.     int start_pid;
  11. #endif
  12. #ifdef CONFIG_LOCKDEP
  13.     struct lockdep_map lockdep_map;
  14. #endif
  15. };
定时器的实现主要是该结构体的填充和部分函数的配合即可完成。其中红色的部分是最主要的几个元素,1、expires主要是用来定义定时器到期的时间,通常采用jiffies这个全局变量和HZ这个全局变量配合设置该元素的值。比如expires = jiffies + n*HZ,其中jiffies是自启动以来的滴答数,HZ是一秒种的滴答数。
2、function可以知道是一个函数指针,该函数就是定时器的处理函数,类似我们在中断中的中断函数,其实定时器和中断有很大的相似性。定时器处理函数是自己定义的函数。
3、data通常是实现参数的传递,从function的参数类型可以知道,data可以作为定时器处理函数的参数。
其他的元素可以通过内核的函数来初始化。

初始化函数为:
init_timer(struct timer_list * timer);
或者直接DEFINE_TIMER宏实现定义和初始化操作。
  1. #define DEFINE_TIMER(_name, _function, _expires, _data)        \
  2.     struct timer_list _name =                \
  3.         TIMER_INITIALIZER(_function, _expires, _data)
添加定时器到内核的函数:
  1. void add_timer(struct timer_list *timer)
  2. {
  3.     BUG_ON(timer_pending(timer));
  4.     mod_timer(timer, timer->expires);
  5. }
删除定时器函数,如果定时器的定时时间还没有到达,那么才可以删除定时器:
int del_timer(struct timer_list *timer)

修改定时器的到达时间,该函数的特点是,不管定时器是否到达时间,都会重现添加一个定时器到内核。所以可以在定时处理函数中可以调用该函数修改需要重新定义的到达时间。
int mode_timer(struct timer_list *timer,unsigned long expires)

  1. int mod_timer(struct timer_list *timer, unsigned long expires)
  2. {
  3.     /*
  4.      * This is a common optimization triggered by the
  5.      * networking code - if the timer is re-modified
  6.      * to be the same thing then just return:
  7.      */
  8.     if (timer->expires == expires && timer_pending(timer))
  9.         return 1;

  10.     /*注意调用的条件,也就是说明当前的定时器为链表的最后一个*/
  11.     return __mod_timer(timer, expires, false);
  12. }

  13. static inline int
  14. __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
  15. {
  16.     struct tvec_base *base, *new_base;
  17.     unsigned long flags;
  18.     int ret;

  19.     ret = 0;

  20.     timer_stats_timer_set_start_info(timer);
  21.     BUG_ON(!timer->function);

  22.     base = lock_timer_base(timer, &flags);

  23.     if (timer_pending(timer)) {
  24.         detach_timer(timer, 0);
  25.         ret = 1;
  26.     } else {
  27.         if (pending_only)
  28.             goto out_unlock;
  29.     }

  30.     debug_timer_activate(timer);

  31.     new_base = __get_cpu_var(tvec_bases);

  32.     if (base != new_base) {
  33.         /*
  34.          * We are trying to schedule the timer on the local CPU.
  35.          * However we can't change timer's base while it is running,
  36.          * otherwise del_timer_sync() can't detect that the timer's
  37.          * handler yet has not finished. This also guarantees that
  38.          * the timer is serialized wrt itself.
  39.          */
  40.         if (likely(base->running_timer != timer)) {
  41.             /* See the comment in lock_timer_base() */
  42.             timer_set_base(timer, NULL);
  43.             spin_unlock(&base->lock);
  44.             base = new_base;
  45.             spin_lock(&base->lock);
  46.             timer_set_base(timer, base);
  47.         }
  48.     }

  49.     timer->expires = expires;
  50.     internal_add_timer(base, timer);

  51. out_unlock:
  52.     spin_unlock_irqrestore(&base->lock, flags);

  53.     return ret;
  54. }

  55. static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
  56. {
  57.     unsigned long expires = timer->expires;
  58.     unsigned long idx = expires - base->timer_jiffies;
  59.     struct list_head *vec;

  60.     if (idx < TVR_SIZE) {
  61.         int i = expires & TVR_MASK;
  62.         vec = base->tv1.vec + i;
  63.     } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  64.         int i = (expires >> TVR_BITS) & TVN_MASK;
  65.         vec = base->tv2.vec + i;
  66.     } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  67.         int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  68.         vec = base->tv3.vec + i;
  69.     } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  70.         int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  71.         vec = base->tv4.vec + i;
  72.     } else if ((signed long) idx < 0) {
  73.         /*
  74.          * Can happen if you add a timer with expires == jiffies,
  75.          * or you set a timer to go off in the past
  76.          */
  77.         vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  78.     } else {
  79.         int i;
  80.         /* If the timeout is larger than 0xffffffff on 64-bit
  81.          * architectures then we use the maximum timeout:
  82.          */
  83.         if (idx > 0xffffffffUL) {
  84.             idx = 0xffffffffUL;
  85.             expires = idx + base->timer_jiffies;
  86.         }
  87.         i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  88.         vec = base->tv5.vec + i;
  89.     }
  90.     /*
  91.      * Timers are FIFO:
  92.      */
  93.     /*添加到链表的最后,这说明mod_timer实现了重新注册一个定时器的操作*/
  94.     list_add_tail(&timer->entry, vec);
  95. }
从上面的分析可以看出,mod_timer的实现过程比较复杂,但是基本上说明了mod_timer函数重新注册定时器的操作过程。
一般而言定时器的基本操作主要是上面的几个函数。
我的基于内核定时器的驱动函数如下,参考了宋宝华的Linux设备驱动开发详解(第二版)。
驱动程序:
  1. #include<linux/module.h>
  2. #include<linux/types.h>
  3. #include<linux/fs.h>
  4. #include<linux/errno.h>
  5. #include<linux/mm.h>
  6. #include<linux/sched.h>
  7. #include<linux/init.h>
  8. #include<linux/cdev.h>
  9. #include<asm/io.h>
  10. #include<asm/uaccess.h>
  11. #include<linux/device.h>

  12. /*采用宏定义设置设备的主设备号*/
  13. #define SECOND_MAJOR    0
  14. /*静态的分别保存静态主设备号的变量*/
  15. static int second_major = SECOND_MAJOR;

  16. /*设备结构体,通常在设备中包含需要的设备,比如字符、块等类型*/
  17. struct second_dev{
  18.     /*添加设备类型,
  19.     我认为可以采用一个联合体,
  20.     包含块设备或者字符设备,类似inode的实现方法,
  21.     这样可以提高结构体的通用性
  22.     */
  23.     struct cdev cdev;
  24.     /*原子变量,用来统计*/
  25.     atomic_t counter;
  26.     /*添加内核定时器结构体变量*/
  27.     struct timer_list s_timer;
  28.     
  29.     /*用于动态创建设备文件的设备类*/
  30.     struct class *myclass;
  31. };

  32. /*结构体指针或者采用全局变量直接定义结构都可以*/
  33. struct second_dev *second_devp;

  34. /*如果定时时间到了,定时器的处理函数*/
  35. static void second_timer_handler(unsigned long arg)
  36. {
  37.     /*
  38.     修改定时器中的到期时间,增加时间为1s,
  39.     需要注意的是mod_timer函数是重新注册定时器到内核
  40.     而不管定时器是否被运行过
  41.     */
  42.     mod_timer(&second_devp->s_timer,jiffies + HZ);
  43.     /*原子变量的增加*/
  44.     atomic_inc(&second_devp->counter);
  45.     /*输出jiffies值*/
  46.     printk(KERN_NOTICE "Current jiffies is %d\n",jiffies);
  47. }

  48. /*open函数实现*/
  49. static int second_open(struct inode *inode,struct file *filp)
  50. {
  51.     /*初始化定义的内核定时器*/
  52.     init_timer(&second_devp->s_timer);
  53.     /*指定内核定时器的处理函数是上面定义好的函数*/
  54.     second_devp->s_timer.function = second_timer_handler;
  55.     /*指定定时间隔是1s*/
  56.     second_devp->s_timer.expires = jiffies + HZ;

  57.     /*将定时器添加到内核*/
  58.     add_timer(&second_devp->s_timer);
  59.     /*同时设备相关的统计值为0*/
  60.     atomic_set(&second_devp->counter,0);

  61.     return 0;
  62. }

  63. /*release函数的实现*/
  64. static int second_release(struct inode *inode,struct file *filp)
  65. {
  66.     /*如果没有到时间就关闭设备,直接删除定时器*/
  67.     del_timer(&second_devp->s_timer);

  68.     return 0;
  69. }

  70. /*read函数的实现*/
  71. static ssize_t second_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos)
  72. {
  73.     int counter;
  74.     
  75.     /*读当前的值*/
  76.     counter = atomic_read(&second_devp->counter);
  77.     
  78.      /*
  79.      采用put_user实现数值的传送
  80.      put_user函数存在对指针变量的检查,
  81.      因此不需要检测指针是否正确
  82.      */
  83.     if(put_user(counter,(int *)buf))
  84.         return -EFAULT;
  85.     else
  86.         /*返回数据大小*/
  87.         return sizeof(unsigned int);
  88. }

  89. /*具体的文件操作集合*/
  90. static const struct file_operations second_fops =
  91. {
  92.     /*这是拥有者*/
  93.     .owner = THIS_MODULE,
  94.     .open = second_open,
  95.     .release = second_release,
  96.     .read = second_read,
  97. };

  98. /*初始化函数*/
  99. static int __init second_init(void)
  100. {
  101.     int ret;
  102.     /*设备号的申请,创建*/
  103.     dev_t devno = MKDEV(second_major,0);

  104.     /*静态申请设备号*/
  105.     if(second_major)
  106.     {
  107.         ret = register_chrdev_region(devno,1,"second");
  108.     }
  109.     /*动态申请设备号*/
  110.     else
  111.     {
  112.         ret = alloc_chrdev_region(&devno,0,1,"second");
  113.         second_major = MAJOR(devno);
  114.     }
  115.     if(ret < 0)
  116.     {
  117.         return ret;
  118.     }
  119.     
  120.     /*分配设备结构体的地址空间*/
  121.     second_devp = kmalloc(sizeof(struct second_dev),GFP_KERNEL);
  122.     /*检查是否分配正确*/
  123.     if(!second_devp)
  124.     {
  125.         ret = -ENOMEM;
  126.         goto fail_malloc;
  127.     }
  128.     /*清零分配的空间*/
  129.     memset(second_devp,0,sizeof(struct second_dev));
  130.     /*创建设备类,用于自动创建设备文件*/
  131.     second_devp->myclass = class_create(THIS_MODULE,"second_timer_class");
  132.     
  133.     /*字符设备初始化,绑定相关操作到设备*/
  134.     cdev_init(&second_devp->cdev,&second_fops);
  135.     /*设备的拥有者*/
  136.     second_devp->cdev.owner = THIS_MODULE,
  137.     /*添加设备到内核*/
  138.     ret = cdev_add(&second_devp->cdev,devno,1);
  139.     
  140.     /*错误处理*/
  141.     if(ret)
  142.     {
  143.         printk(KERN_NOTICE "ERROR %d\n ",ret);
  144.         goto fail_malloc;
  145.     }
  146.     /*依据以前创建的设备类,创建设备*/
  147.     device_create(second_devp->myclass,NULL,devno,NULL,"second%d",0);
  148.     return 0;

  149. /*错误操作*/
  150. fail_malloc:
  151.     unregister_chrdev_region(devno,1);
  152.     return ret;
  153. }

  154. /*退出函数*/
  155. static void __exit second_exit(void)
  156. {
  157.     /*释放设备*/
  158.     device_destroy(second_devp->myclass,MKDEV(second_major,0));
  159.     /*删除字符设备*/
  160.     cdev_del(&second_devp->cdev);
  161.     /*释放设备类*/
  162.     class_destroy(second_devp->myclass);
  163.     /*释放分配的内存空间大小*/    
  164.     kfree(second_devp);
  165.     /*释放设备号*/
  166.     unregister_chrdev_region(MKDEV(second_major,0),1);
  167. }

  168. /*卸载和加载*/
  169. module_init(second_init);
  170. module_exit(second_exit);

  171. /*LICENSE和作者信息*/
  172. MODULE_LICENSE("GPL");
  173. MODULE_AUTHOR("GP-");
应用程序:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/types.h>
  4. #include<sys/stat.h>
  5. #include<linux/fs.h>
  6. #include<unistd.h>
  7. #include<fcntl.h>

  8. int main()
  9. {
  10.     int fd;
  11.     int counter = 0;
  12.     int old_counter = 0;
  13.     
  14.     fd = open("/dev/second0",O_RDONLY);

  15.     if(fd != -1)
  16.     {
  17.         while(1)
  18.         {
  19.             read(fd,&counter,sizeof(unsigned int));
  20.             if(counter != old_counter)
  21.             {
  22.                 printf("second after open /dev/second0 : %d\n",counter);
  23.                 old_counter = counter;
  24.             }
  25.         }
  26.     }
  27.     else
  28.     {
  29.         printf("Device open failure\n");
  30.         exit(1);
  31.     }
  32.     exit(0);
  33. }
实验效果:
[root@EmbedSky Test]# ./app-timer                                               
Current jiffies is 2137721                                                      
second after open /dev/second0 : 1                                              
Current jiffies is 2137921                                                      
second after open /dev/second0 : 2                                              
Current jiffies is 2138121                                                      
second after open /dev/second0 : 3                                              
Current jiffies is 2138321                                                      
second after open /dev/second0 : 4                                              
Current jiffies is 2138521                                                      
second after open /dev/second0 : 5                                              
Current jiffies is 2138721                                                      
second after open /dev/second0 : 6    

以上的结果表明内核定时器基本实现了效果,但从实验结果看好像为每两秒实现一次显示。具体的原因还有待于再次分析,因为arm中的HZ应该为100,而不是200。           
阅读(7986) | 评论(0) | 转发(5) |
给主人留下些什么吧!~~