1、在Linux 中实现中断底半部的机制主要是:
tasklet、工作队列、软中断
2、tasklet
定义在<linux/interrupt.h>中
tasklet定义
DECLARE_TASKLET(taskletname, tasklet_func, data);
taskletname:待定义的tasklet名字
tasklet_func:tasklet处理函数
data:待传入tasklet处理函数的参数
tasklet处理函数
void tasklet_func(unsigned long data);
tasklet调用
在中断处理函数中弟阿勇tasklet_schedule()函数
void tasklet_schedule(struct tasklet_struct *taskletname);
3、软中断
软中断使用软件方式模拟硬件中断,目的是实现异步执行。
tasklet即基于软中断实现。
软终端和tasklet仍然运行于中断上下文,工作队列运行于进程上下文。
/** *Copyright (c) 2013.TianYuan *All rights reserved. * *文件名称: irq.c *文件标识: tasklet实现,登记底半部 *make menuconfig--- device drivers *input keyboards ---s3c gpio keypad supports #cat /proc/interrupts : 32(中断号) :intertupt_demo * *当前版本:1.0 *作者:wuyq * *取代版本:xxx *原作者:xxx *完成日期:2014-03-05 */ #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/slab.h> #include <asm/uaccess.h> #include <asm/gpio.h> #include <plat/gpio-cfg.h> #include <linux/spinlock_types.h> #include <linux/sched.h> #include <linux/poll.h> #include <linux/interrupt.h> MODULE_LICENSE("GPL"); #define CDD_MAJOR 200//cat /proc/devices找一个尚未使用的 #define CDD_MINOR 0 #define CDD_COUNT 10 dev_t dev = 0; u32 cdd_major = 0; u32 cdd_minor = 0; struct class *dev_class = NULL; #define BUF_SIZE 100 struct cdd_cdev{ struct cdev cdev; struct device *dev_device; u8 led; char kbuf[BUF_SIZE]; u32 data_len;//记录缓冲区中已经写入数据的长度 //定义等待队列头 wait_queue_head_t wqh; }; struct cdd_cdev *cdd_cdevp = NULL; unsigned long led_gpio_table[2] = { S5PV210_GPC1(3),//数字 S5PV210_GPC1(4), }; int cdd_open(struct inode* inode, struct file *filp) { struct cdd_cdev *pcdevp = NULL; printk("enter cdd_open!\n"); pcdevp = container_of(inode->i_cdev, struct cdd_cdev, cdev); printk("led = %d\n", pcdevp->led); /*获取信号量*/ //down获取信号量不成功,会导致进程睡眠(第3个进程的时候) //down(&pcdevp->sem_open); if(down_interruptible(&pcdevp->sem_open)<0){ return -1; } filp->private_data = pcdevp; //申请gpio管脚 gpio_request(led_gpio_table[0], "GPC1_3"); gpio_request(led_gpio_table[1], "GPC1_4"); return 0; } int cdd_read(struct file *filp, char __user *buf, size_t count, loff_t *offset) { int ret = 0; u32 pos = *offset; u32 cnt = count; struct cdd_cdev *cdevp = filp->private_data; #if 0 //定义并初始化一个等待队列 DECLARE_WAITQUEUE(wq, current); //将等待队列添加到wqh指向的链表 add_wait_queue(&pcdevp->wqh, &wq); //判断设备有没有数据供用户空间读,假设led不为0,表示有数据供用户空间读取 if(pcdevp->led == 0){ printk("no data for reading! sleep...\n"); //设置当前线程为睡眠状态 set_current_state(TASK_INTERRUPTIBLE); schedule();//内核调度cpu的算法 printk("have data for reading!\n"); } //从指定的链表中删除等待队列 remove_wait_queue(&pcdevp->wqh, &wq); #endif wait_event_interruptible(pcdevp->wqh, pcdevp->led != 0); //printk("enter cdd_read!\n"); if(cnt > (cdevp->data_len-pos) ){ cnt = cdevp->data_len - pos; } ret = copy_to_user(buf, cdevp->kbuf+pos, cnt); //printk("kernel kbuf content:%s\n", cdevp->kbuf); *offset += cnt; pcdevp->led = 0; return ret; } int cdd_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset) { int ret = 0; struct cdd_cdev *cdevp = filp->private_data; u32 pos = *offset; u32 cnt = count; //printk("enter cdd_write!\n"); if(cnt > (BUF_SIZE - pos) ){ cnt = BUF_SIZE - pos; } ret = copy_from_user(cdevp->kbuf+pos, buf, cnt); *offset += cnt; if(*offset > cdevp->data_len){ cdevp->data_len = *offset; } pcdevp->led = 1; //唤醒等待队列头中的一个等待队列 wake_up_interruptible(&pcdevp->wqh); return ret; } int cdd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long data) { //printk("enter cdd_ioctl!\n"); switch(cmd){ case 1://点亮灯 //设置管脚为输出功能 //参数:1.要设置的管脚编号2.默认的输出值 0低电平1高电平 gpio_direction_output(led_gpio_table[data], 0); //禁止内部上拉 s3c_gpio_setpull(led_gpio_table[data], SEC_GPIO_PULL_NONE); //设置输出值 gpio_set_value(led_gpio_table[data], 1); break; case 0://熄灭灯 //设置管脚为输出功能 //参数:1.要设置的管脚编号2.默认的输出值 0低电平1高电平 gpio_direction_output(led_gpio_table[data], 0); //禁止内部上拉 s3c_gpio_setpull(led_gpio_table[data], SEC_GPIO_PULL_NONE); //设置输出值 gpio_set_value(led_gpio_table[data], 0); break; default: return -EINVAL; } return 0; } int cdd_release(struct inode *inode, struct file *filp) { struct cdd_cdev *pcdevp = filp->private_data; printk("enter cdd_release!\n"); gpio_free(led_gpio_table[0]); gpio_free(led_gpio_table[1]); up(&pcdevp->sem_open); return 0; } loff_t cdd_llseek(struct file *filp, loff_t offset, int whence) { struct cdd_cdev *pcdevp = filp->private_data; loff_t newpos = 0; switch(whence){ case SEEK_SET: newpos = offset; break; case SEEK_CUR: newpos = filp->f_pos + offset; break; case SEEK_END: newpos = pcdevp->data_len + offset; break; default: return -EINVAL;//无效的参数 } if( newpos<0 || newpos>= BUF_SIZE ){ return -EINVAL; } filp->f_pos = newpos; return newpos; } unsigned int cdd_poll(struct file *filp, struct poll_table_struct *wait) { unsigned int mask = 0; struct cdd_cdev pcdevp = filp->private_data; printk("enter cdd_poll!\n"); poll_wait(filp, &pcdep->wqh, wait); //led不为0即可读 if(pcdevp->led){ mask = POLLIN | POLLRDNORM; } return mask; } struct file_operations cdd_fops = { .owner = THIS_MODULE, .open = cdd_open, .read = cdd_read, .write = cdd_write, .ioctl = cdd_ioctl, .release = cdd_release, .llseek = cdd_llseek, .poll = cdd_poll, }; void cdd_func(unsigned long data) { int i = 10000000; printk("enter cdd_func!\n"); while(i--);//耗时 printk("exit cdd_func!\n"); } //定义tasklet DECLARE_TASKLET(cdd_tasklet, cdd_func, 0); irqreturn_t cdd_isr(int irq, void *dev_id) { printk("occur up key press or release!\n"); //登记底半部 tasklet_schedule(&cdd_tasklet); ` printk("exit cdd_isr!\n"); return IRQ_HANDLED; } int __init cdd_init(void) { int ret = 0; int i = 0; if(cdd_major){ dev = MKDEV(CDD_MAJOR, CDD_MINOR);//生成设备号 //注册设备号;1、要注册的起始设备号2、连续注册的设备号个数3、名字 ret = register_chrdev_region(dev, CDD_COUNT, "cdd_demo"); }else{ // 动态分配设备号 ret = alloc_chrdev_region(&dev, cdd_minor, CDD_COUNT, "cdd_demo02"); } if(ret < 0){ printk("register_chrdev_region failed!\n"); goto failure_register_chrdev; } //获取主设备号 cdd_major = MAJOR(dev); printk("cdd_major = %d\n", cdd_major); cdd_cdevp = kzalloc(sizeof(struct cdd_cdev)*CDD_COUNT, GFP_KERNEL); if(IS_ERR(cdd_cdevp)){ printk("kzalloc failed!\n"); goto failure_kzalloc; } /*创建设备类*/ dev_class = class_create(THIS_MODULE, "cdd_class"); if(IS_ERR(dev_class)){ printk("class_create failed!\n"); goto failure_dev_class; } for(i=0; i<CDD_COUNT; i++){ /*初始化cdev*/ cdev_init(&(cdd_cdevp[i].cdev), &cdd_fops); /*添加cdev到内核*/ cdev_add(&(cdd_cdevp[i].cdev), dev+i, 1); /* “/dev/xxx” */ device_create(dev_class, NULL, dev+i, NULL, "cdd%d", i); cdd_cdevp[i].led = i; //初始化等待队列头 init_waitqueue_head(&cdd_cdevp[i].wqh); } //注册中断 request_irq(IRQ_EINT0, cdd_isr, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "interrupt_demo", NULL); return 0; failure_dev_class: kfree(cdd_cdevp); failure_kzalloc: unregister_chrdev_region(dev, CDD_COUNT); failure_register_chrdev: return ret; } void __exit cdd_exit(void) { /*逆序消除*/ int i = 0; for(; i < CDD_COUNT; i++){ device_destroy(dev_class, dev+i); cdev_del(&(cdd_cdevp[i].cdev)); //cdev_del(&((cdd_cdevp+i)->cdev)); } class_destroy(dev_class); kfree(cdd_cdevp); unregister_chrdev_region(dev, CDD_COUNT); free_irq(IRQ_EINT0, NULL); } module_init(cdd_init); module_exit(cdd_exit);