1、中断定义
中断是指cpu在执行过程中,出现了某些突发事件时cpu必须暂停执行当前的程序,转去处理突发事件,处理完毕后cpu又返回原程序被中断的位置并继续执行。
2、中断分类
3、Linux中断处理程序结构
a.在Linux系统中,中断处理程序分解为两个半部:顶半部(TopHalf)和底半部(BottomHalt)。
b.顶半部:完成尽可能少的比较紧急的功能,往往只是简单的读取寄存器中的中断状态并清除中断标志后就进行“登记中断”的工作,也就是将底半部处理程序挂到该设备的底半部执行队列中去,该过程不可中断。
c.底半部:它将完成中断时间的绝大多数任务,该部分任务不是非常紧急,并且相对比较耗时,该部分可以被新的中断打断。
4、中断编程
实现在内核源码kernel/irq/manage.c中
申请中断
int request_irq(unsigned int irq, irq_handler_t handler, unsigned long irqflags, const char *name, void *dev_id); irq:待申请的中断号 handler:待注册的中断处理函数 irqflags:中断标志 name:中断设备的名称 dev_id:传递给中断处理函数的指针,通常用于共享中断时传递设备结构体指针 返回:成功返回0, 失败返回负值 -EINVAL:表示申请的中断号无效火种中断处理函数指针为空 -EBUSY:表示中断已经被占用并且不能共享
中断申请常见中断标志
IRQF_SHAREED:表示多个设备共享中断
IRQF_SAMPLE_RANDOM:用于随机数种子的随机采样
IRQF_TRIGGER_RISING:上升沿触发中断
IRQF_TRIGGER_FALLING:下降沿触发中断
IRQF_TRIGGER_HIGH:高电平触发中断
IRQF_TRIGGER_LOW:低电平触发中断
中断处理函数
irqreturn_t (*irq_handler_t)(int irq, void *dev_id); irq:中断号 dev_id:通常传递设备结构体指针 返回:IRQ_NONE:未做处理、IRQ_HANDLED:正常处理后应该返回该值
中断释放
void free_irq(unsigned int irq, void *dev_id): irq:待释放的IRQ号 dev_id:传递给中断处理函数的指针
/** *Copyright (c) 2013.TianYuan *All rights reserved. * *文件名称: irq.c *文件标识: 按键1的中断,测试的时候需要裁剪掉内核中自带的按键驱动 *make menuconfig--- device drivers *input keyboards ---s3c gpio keypad supports #cat /proc/interrupts : 32(中断号) :intertupt_demo * *当前版本:1.0 *作者:wuyq * *取代版本:xxx *原作者:xxx *完成日期:2014-02-14 */ #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_quue_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, }; irqreturn_t cdd_isr(int irq, void *dev_id) { printk("occur up key press or release!\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);
时间: 2024-12-23 09:35:36