linux驱动之内核定时器驱动设计-- timer

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 09:29   458   0
定时器在linux内核中主要是采用一个结构体实现的。但是需要注意定时器是一个只运行一次的对象,也就是当一个定时器结束以后,还需要重现添加定时器。但是可以采用mod_timer()函数动态的改变定时器到达时间。
这个驱动主要实现内核定时器的基本操作。内核定时器主要是是通过下面的结构体struct timer_list实现。需要的头文件包括#include<linux/timer.h>,但是在实际开发过程中不需要包含该头文件,因为在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. struct class *myclass;
  30. };

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

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

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

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

  60. return 0;
  61. }

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

  67. return 0;
  68. }

  69. /*read函数的实现*/
  70. static ssize_t second_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos)
  71. {
  72. int counter;

  73. /*读当前的值*/
  74. counter = atomic_read(&second_devp->counter);

  75. /*
  76. 采用put_user实现数值的传送
  77. put_user函数存在对指针变量的检查,
  78. 因此不需要检测指针是否正确
  79. */
  80. if(put_user(counter,(int *)buf))
  81. return -EFAULT;
  82. else
  83. /*返回数据大小*/
  84. return sizeof(unsigned int);
  85. }

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

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

  101. /*静态申请设备号*/
  102. if(second_major)
  103. {
  104. ret = register_chrdev_region(devno,1,"second");
  105. }
  106. /*动态申请设备号*/
  107. else
  108. {
  109. ret = alloc_chrdev_region(&devno,0,1,"second");
  110. second_major = MAJOR(devno);
  111. }
  112. if(ret < 0)
  113. {
  114. return ret;
  115. }

  116. /*分配设备结构体的地址空间*/
  117. second_devp = kmalloc(sizeof(struct second_dev),GFP_KERNEL);
  118. /*检查是否分配正确*/
  119. if(!second_devp)
  120. {
  121. ret = -ENOMEM;
  122. goto fail_malloc;
  123. }
  124. /*清零分配的空间*/
  125. memset(second_devp,0,sizeof(struct second_dev));
  126. /*创建设备类,用于自动创建设备文件*/
  127. second_devp->myclass = class_create(THIS_MODULE,"second_timer_class");

  128. /*字符设备初始化,绑定相关操作到设备*/
  129. cdev_init(&second_devp->cdev,&second_fops);
  130. /*设备的拥有者*/
  131. second_devp->cdev.owner = THIS_MODULE,
  132. /*添加设备到内核*/
  133. ret = cdev_add(&second_devp->cdev,devno,1);

  134. /*错误处理*/
  135. if(ret)
  136. {
  137. printk(KERN_NOTICE "ERROR %d\n ",ret);
  138. goto fail_malloc;
  139. }
  140. /*依据以前创建的设备类,创建设备*/
  141. device_create(second_devp->myclass,NULL,devno,NULL,"second%d",0);
  142. return 0;

  143. /*错误操作*/
  144. fail_malloc:
  145. unregister_chrdev_region(devno,1);
  146. return ret;
  147. }

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

  162. /*卸载和加载*/
  163. module_init(second_init);
  164. module_exit(second_exit);

  165. /*LICENSE和作者信息*/
  166. MODULE_LICENSE("GPL");
  167. MODULE_AUTHOR("GP-<gp19861112@yahoo.com.cn>");
应用程序:
  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. fd = open("/dev/second0",O_RDONLY);

  14. if(fd != -1)
  15. {
  16. while(1)
  17. {
  18. read(fd,&counter,sizeof(unsigned int));
  19. if(counter != old_counter)
  20. {
  21. printf("second after open /dev/second0 : %d\n",counter);
  22. old_counter = counter;
  23. }
  24. }
  25. }
  26. else
  27. {
  28. printf("Device open failure\n");
  29. exit(1);
  30. }
  31. exit(0);
  32. }
实验效果:
[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。

转载于:https://www.cnblogs.com/liuchengchuxiao/p/4118582.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP