2、内核定时器使用模板
//设备结构体 struct xxx_dev{ struct cdev cdev; ... struct timer_list xxx_timer; //定义定时器 } //驱动中某函数 xxx_funcl(...) { struct xxx_dev *dev = filp->private_data; ... //初始化定时器 init_timer(&dev->xxx_time); dev->xxx_timer.function = &xxx_do_timer; //定义定时器处理函数 dev->xxx_timer.data = (unsigned long)dev; //设备结构体指针作为定时器处理参数 dev->xxx_timer.expires = jiffies + delay; //定义到期时间 add_timer(&dev->xxx_timer); //注册定时器 ... } //驱动中某函数 xxx_func2(...) { ... //删除中断 del_timer(&dev->xxx_timer); ... } //定时器处理函数 static void xxx_do_timer(unsigned long arg) { struct xxx_dev *dev = filp->private_data; ... dev->xxx_timer.expires = jiffies + delay; //重新设置定时时间 add_timer(&dev->xxx_timer); ... } |
HZ表示延时1s
3、实例–秒字符设备second_drv.c ,它在被打开时将初始化的定时器加到内核定时器链表中,每秒输出一次当前的jiffes,代码如下:
#include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/mm.h> #include <linux/sched.h> #include <linux/init.h> #include <linux/cdev.h> #include <asm/io.h> #include <asm/system.h> #include <asm/uaccess.h> #include <linux/slab.h> #define SECOND_MAJOR 248 static int second_major = SECOND_MAJOR; struct second_dev { struct cdev cdev; atomic_t counter; struct timer_list s_timer; }; struct second_dev *second_devp; static void second_timer_handle (unsigned long arg) { mod_timer (&second_devp->s_timer, jiffies + HZ); atomic_inc (&second_devp->counter); printk (KERN_NOTICE "current jiffies is %ld\n", jiffies); } int second_open (struct inode *inode, struct file *filp) { init_timer (&second_devp->s_timer); second_devp->s_timer.function = &second_timer_handle; second_devp->s_timer.expires = jiffies + HZ; add_timer (&second_devp->s_timer); atomic_set (&second_devp->counter, 0); return 0; } int second_release (struct inode *inode, struct file *filp) { del_timer (&second_devp->s_timer); return 0; } static ssize_t second_read (struct file *filp, char __user *buf, size_t count, loff_t *ppos) { int counter; counter = atomic_read (&second_devp->counter); if (put_user (counter, (int *)buf)) return -EFAULT; else return sizeof (unsigned int); } static const struct file_operations second_fops = { .owner = THIS_MODULE, .open = second_open, .release = second_release, .read = second_read, }; static void second_setup_cdev (struct second_dev *dev, int index) { int err, devno = MKDEV (second_major, index); cdev_init (&dev->cdev, &second_fops); dev->cdev.owner = THIS_MODULE; err = cdev_add (&dev->cdev, devno, 1); if (err) printk (KERN_NOTICE "Error %d adding CDEV %d", err, index); } int second_init (void) { int ret; dev_t devno = MKDEV (second_major, 0); if (second_major) ret = register_chrdev_region (devno, 1, "second"); else { return alloc_chrdev_region (&devno, 0, 1, "second"); second_major = MAJOR (devno); } if (ret < 0) return ret; second_devp = kmalloc (sizeof (struct second_dev), GFP_KERNEL); if (!second_devp) { ret = -ENOMEM; goto fail_malloc; } memset (second_devp, 0, sizeof (struct second_dev)); second_setup_cdev (second_devp, 0); return 0; fail_malloc: unregister_chrdev_region (devno, 1); return ret; } void second_exit (void) { cdev_del (&second_devp->cdev); kfree (second_devp); unregister_chrdev_region (MKDEV (second_major, 0), 1); } MODULE_AUTHOR ("Ljia-----Ljia"); MODULE_LICENSE ("Dual BSD/GPL"); module_param (second_major, int, S_IRUGO); module_init (second_init); module_exit (second_exit); |
在second的open()函数中,将启动定时器,此后每秒会再次运行定时器处理函数,且在release()函数中删除,编译驱动,加载并创建“/dev/second”设备文件节点之后,用以下程序打开,second_test会不断读取来自“/dev/second”设备文件以来经历的秒数。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> int main (void) { int fd; int counter = 0; int old_counter = 0; fd = open ("/dev/second", O_RDONLY); if (fd != -1) { while (1) { read (fd, &counter, sizeof (unsigned int)); if (counter != old_counter) { printf ("seconds after open /dev/second: %d\n", counter); old_counter = counter; } } } else { printf ("Device open failure\n"); } return 0; } |
运行second_test后,不断输出jiffes的值,如下
current jiffes is 17216
current jiffes is 17316
current jiffes is 17416
current jiffes is 17516
current jiffes is 17616
current jiffes is 17716
current jiffes is 17816
current jiffes is 17916
current jiffes is 17016
current jiffes is 17116
current jiffes is 17216
current jiffes is 17316
而应用程序将不断输出来自打开的“/dev/second”如下:
seconds after open /dev/second :1
seconds after open /dev/second :2
seconds after open /dev/second :3
seconds after open /dev/second :4
seconds after open /dev/second :5
seconds after open /dev/second :6
seconds after open /dev/second :7
seconds after open /dev/second :8
seconds after open /dev/second :9
seconds after open /dev/second :10
三、总结
Linux中断处理分为两个半部,上述都讲得很清楚了,这里强调以下,为了充分利用CPU资源,在对延时使用不是很精确的情况下,睡眠等待值得推荐。对于上述的几个例子,需要大家自己在Linux的操作中敲出来,并且编译,看输出的结果才能完全理解~