Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071644
  • 博文数量: 71
  • 博客积分: 3078
  • 博客等级: 少校
  • 技术积分: 945
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-16 20:10
个人简介

此博客已停用 个人博客: Mangogeek.com

文章分类
文章存档

2016年(1)

2015年(32)

2014年(25)

2011年(13)

分类: LINUX

2014-08-20 17:42:47

1.linux内核定时器基本结构和函数

1)struct timer_list 一个struct timer_list对应了一个定时器。
#include
以下列出常用的接口:
struct timer_list
  {
   /*....*/
   unsigned long expires;//定时器服务函数开始执行时间
   void (*function)(unsigned long);//定义一个指向定时器服务函数的指针function,服务函数有一个 unsigned long的参数,并且返回void
   unsigned long data;//定时时间到时,data参数会传入服务函数
  }
 
void init_timer(struct timer_list* timer)//初始化一个定时器

-----------使用定时器的步骤--------------
struct timer_list  my_timer_list;//定义一个定时器,可以把它放在你的设备结构中
init_timer(&my_timer_list);//初始化一个定时器
my_timer_list.expire=jiffies+HZ;//定时器1s后运行服务程序
my_timer_list.function=timer_function;//定时器服务函数
add_timer(&my_timer_list);//添加定时器
void timer_function(unsigned long)//写定时器服务函数
del_timer(&my_timer_list);//当定时器不再需要时删除定时器
del_timer_sync(&my_timer_list);//基本和del_timer一样,比较适合在多核处理器使用,一般推荐使用del_timer_sync
-------------------------------------------------
2.以下是一个定时1s的驱动程序,直接上代码

点击(此处)折叠或打开

  1. #include <linux/miscdevice.h>
  2. #include <linux/delay.h>
  3. #include <asm/irq.h>
  4. //#include <mach/regs-gpio.h>
  5. //#include <mach/hardware.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/mm.h>
  10. #include <linux/fs.h>
  11. #include <linux/types.h>
  12. #include <linux/delay.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/slab.h>
  15. #include <linux/errno.h>
  16. #include <linux/ioctl.h>
  17. #include <linux/cdev.h>
  18. #include <linux/string.h>
  19. #include <linux/list.h>
  20. #include <linux/pci.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/atomic.h>
  23. #include <asm/unistd.h>
  24. #include <asm/io.h>
  25. #include <asm/system.h>
  26. #include <asm/uaccess.h>
  27. #define TIMER_MAJOR 300
  28. #define TIMER_MINOR 0
  29. dev_t timer_dev_t;//设备号
  30. dev_t timer_dev_major=TIMER_MAJOR;
  31. dev_t timer_dev_minor=TIMER_MINOR;
  32. struct TIMER_DEV
  33. {
  34.   struct cdev cdev;
  35.   atomic_t count;
  36.   struct timer_list timer_list;

  37. };
  38. struct TIMER_DEV* timer_dev;
  39. //---------timer interrupt function----------------
  40. static void timer_function(unsigned long data)
  41. {
  42. mod_timer(&(timer_dev->timer_list),jiffies+HZ);//重新设置时间
  43. printk("current jiffies is %ld,count=%d\n",jiffies,timer_dev->count);
  44. //(timer_dev->count)++;
  45. atomic_inc(&(timer_dev->count));
  46. }
  47. //--------timer release function--------------------
  48. static int timer_release(struct inode* inode, struct file* filp)
  49. {
  50. del_timer_sync(&(timer_dev->timer_list));
  51. return 0;
  52. }

  53. //----------------file open function-----------------
  54. static int timer_open(struct inode* inode,struct file* filp)
  55. {
  56. init_timer(&(timer_dev->timer_list));//初始化定时器
  57. timer_dev->timer_list.function=timer_function;//设置定时器处理函数
  58. timer_dev->timer_list.expires=jiffies+HZ;//处理函数1s后运行
  59. add_timer(&timer_dev->timer_list);//添加定时器
  60. atomic_set(&(timer_dev->count),0);
  61. return 0;
  62. }
  63. //--------------------------------------

  64. //----------------timer_read function---------------
  65. static int timer_read(struct file* filp,char __user *buf,size_t count,loff_t* f_pos)
  66. {

  67.  unsigned int counter=atomic_read(&(timer_dev->count));
  68. if(copy_to_user(buf,(unsigned int*)&counter,sizeof(unsigned int)))
  69.   {
  70.    printk("copy to user error\n");
  71.    goto out;
  72.   }
  73. return (sizeof(unsigned int));
  74. out:
  75. return (-EFAULT);

  76. }

  77. struct file_operations timer_ops={
  78. .owner=THIS_MODULE,
  79. .open=timer_open,
  80. .read=timer_read,
  81. .release=timer_release,
  82. };
  83. static int __init timer_init(void)
  84. {
  85.  int ret;
  86.  
  87.  if(TIMER_MAJOR)//主设备号大于0,静态申请设备号
  88.     {
  89.     timer_dev_t=MKDEV(TIMER_MAJOR,TIMER_MINOR);
  90.     ret=register_chrdev_region(TIMER_MAJOR,1,"timer_dev");//first,count,name
  91.     }
  92.  else
  93.     {
  94.     ret=alloc_chrdev_region(&timer_dev_t,0,1,"time_dev");
  95.     timer_dev_major=MAJOR(timer_dev_t);
  96.     }
  97.  if(ret<0)
  98.     {
  99.     printk("can't get major %d\n",timer_dev_major);
  100.     return ret;
  101.     }
  102. //-----------------------------------------------------------
  103.  timer_dev=kmalloc(sizeof(struct TIMER_DEV),GFP_KERNEL);&nbsp;
  104.     memset(timer_dev,0,sizeof(struct TIMER_DEV));
  105. &nbsp;cdev_init(&(timer_dev->cdev),&timer_ops);
  106. &nbsp;cdev_add(&(timer_dev->cdev),timer_dev_t,1);
  107. &nbsp;printk("init timer_dev success\n");
  108. return 0;


  109. }
  110. static void __exit timer_exit(void)
  111. {
  112. kfree(timer_dev);
  113. cdev_del(&(timer_dev->cdev));
  114. unregister_chrdev_region(MKDEV(TIMER_MAJOR,0),1);
  115. }
  116. module_init(timer_init);
  117. module_exit(timer_exit);

阅读(2265) | 评论(0) | 转发(0) |
0

上一篇:C语言符号优先级

下一篇:linux 等待队列

给主人留下些什么吧!~~