一 前言 在谈linux中断框架时,我们先来简单回顾一下单片机中断系统的设计方式,一般地,比如gpio中断,cpu检测到发生中断,先保存被中断的现场,然后跳转到异常向量入口函数,执行中断处理函数,执行完之后,恢复之前被中断的现场,继续执行。单片机程序中,首先分辨是哪个中断,然后调用处理函数,在中断函数中清中断(一般地,有些中断需要软件清除,有些中断在响应中断服务程序的时候会自动清除)。本篇文章中,主要是以使用目的为主,没有仔细分析很多linux内核封装的函数,前期先学会使用这些内核函数即可。最近忙着加班,个人技术公众号的更新又停止了一段时间,这可不是好苗头,写技术公众号的目的主要是两个:1. 将自己学习过程中的认识分享出来便于大家一起交流;2. 做好相关记录,嵌入式软件行业需要的知识实在太多了,以便后期自己需要的时候能够快速查找到。另外,本文章还是韦东山老师linux课程的学习笔记,有想买韦老师教学课程的同学请扫描下方二维码,学习过程中遇到的问题,欢迎前来咨询, 二 what知道单片机中断处理流程之后,我们来看看linux中断框架,linux系统只是将中断处理的流程封装成一套处理流程api,对于用户程序来说,只需要做注册的动作即可。linux内核中断流程:
linux中断框架如下: a. 进入异常模式 b vector_irq + offset -----------> entry-armv.s b. __irq_usr -----------> entry-armv.s c. asm_do_irq -----------> Irq.c d. irq_desc[irq] ----> handle_irq -----------> Irq.h e. handle_edge_irq 三 why字符设备驱动程序为什么需要中断的方式呢?比如我们应用程序需要查询某个电平的状态,如果没有中断方式,用户程序通过read一直读的话,会导致应用程序一直占据cpu,其他程序无法执行(当然我们可以在用户程序中通过别的方式来避免出现这种情况);如果我们有中断方式,那么我们用户程序在read的时候,就不用关心通过其他方式来避免出现用户程序一直占据cpu的情况了。四 how介绍完了基础知识之后,我们来看看字符设备驱动的中断实现方式,与前面字符设备驱动相同的是,open,read的实现是相同的,但是在中断实现方式中多了一个release的操作,即释放中断,或者说不使能中断A. open函数 我们在open函数中注册中断,调用linux内核提供的request_irq函数来实现注册中断,将中断服务程序添加到链表中,并且使能中断 static int third_drv_open(struct inode *inode, struct file *file) { /* 配置GPF0,2、GPG3,11为输出引脚 */ request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]); request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]); request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]); request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return0; } request_irq函数原型如下,它接受五个参数 int request_irq(unsignedint irq, irq_handler_t handler, unsignedlong irqflags, constchar *devname, void *dev_id) 第一个参数 irq为中断号 第二个参数 handle为中断回调函数 第三个参数 irqflags为中断类型,比如对于gpio中断来说,可以是上升沿中断、下降沿中断、双边沿中断、低电平中断、高电平中断 第四个参数 devname为名称,可以任意指定 第五个参数 dev_id为注册时传递给中断服务程序的参数 pins_desc定义如下 structpin_desc { unsignedint pin; unsignedint key_val; };
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */ /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */ structpin_descpins_desc[4] = { {S3C2410_GPF0, 0x01}, {S3C2410_GPF2, 0x02}, {S3C2410_GPG3, 0x03}, {S3C2410_GPG11, 0x04} }; 注册的中断回调函数为buttons_irq,也就是中断发生时,cpu会跳转进入执行的驱动入口函数,这个里面的内容可以由中断驱动实现,我们先通过系统实现的s3c2410_gpio_getpin获取引脚电平状态的函数获取对应引脚的电平,并在中断中通过wake_up_interruptible唤醒cpu,通知等待中断的地方继续执行static irqreturn_t buttons_irq(int irq, void *dev_id) { structpin_desc *pindesc = (structpin_desc *)dev_id; unsignedint pinval; //printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin); /* 获取电平状态 */ if (pinval) { /* 松开 */ key_val = 0x80 | pindesc->key_val; } else { /* 按下 */ key_val = pindesc->key_val; }
ev_press = 1; /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_HANDLED; } B. read函数 我们使用wait_event_interruptible等待中断,如果一直没有中断,那么read函数中这条语句的后面部分就不会被执行,不会引发cpu线程调度;如果发生了中断,read函数就会紧接着执行后面的内容,因为key_val是属于kernel驱动的,用户空间无法访问,所以要使用copy_to_usr拷贝到用户态 staticintthird_drv_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { /* 返回4个引脚电平状态 */ if (size != 1) return -EINVAL;
/* 如果没有按键动作, 休眠 */ wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */ copy_to_user(buf, &key_val, 1); ev_press = 0;
return (sizeof(key_val)); } C. release函数 我们调用free_irq禁止对应中断,大致的实现过程就是将前面使能的中断函数从链表中删除,并且禁止中断,可参考源码分析free_irq的过程 int third_drv_close(struct inode *inode, struct file *file) { free_irq(IRQ_EINT0, &pins_desc[0]); free_irq(IRQ_EINT2, &pins_desc[1]); free_irq(IRQ_EINT11, &pins_desc[2]); free_irq(IRQ_EINT19, &pins_desc[3]);
return0; } 完整代码如下 #include<linux/module.h> #include<linux/kernel.h> #include<linux/fs.h> #include<linux/init.h> #include<linux/delay.h> #include<linux/irq.h> #include<asm/uaccess.h> #include<asm/irq.h> #include<asm/io.h> #include<asm/arch/regs-gpio.h> #include<asm/hardware.h> #include<linux/poll.h>
staticstructclass *thirddrv_class; staticstructclass_device *thirddrv_class_dev; volatileunsignedlong *gpfcon; volatileunsignedlong *gpgcon; volatileunsignedlong *gpfdat; volatileunsignedlong *gpgdat;
staticDECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */ staticvolatileint ev_press = 0;
staticunsignedchar key_val;
structpin_desc { unsignedint pin; unsignedint key_val; };
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */ /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */ structpin_descpins_desc[4] = { {S3C2410_GPF0, 0x01}, {S3C2410_GPF2, 0x02}, {S3C2410_GPG3, 0x03}, {S3C2410_GPG11, 0x04} };
static irqreturn_t buttons_irq(int irq, void *dev_id) { structpin_desc *pindesc = (structpin_desc *)dev_id; unsignedint pinval; //printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(pindesc->pin); /* 获取电平状态 */ if (pinval) { /* 松开 */ key_val = 0x80 | pindesc->key_val; } else { /* 按下 */ key_val = pindesc->key_val; }
ev_press = 1; /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_HANDLED; }
staticintthird_drv_open(struct inode *inode, struct file *file) { /* 配置GPF0,2、GPG3,11为输出引脚 */ request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]); request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]); request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]); request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return0; }
staticintthird_drv_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { /* 返回4个引脚电平状态 */ if (size != 1) return -EINVAL;
/* 如果没有按键动作, 休眠 */ wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */ copy_to_user(buf, &key_val, 1); ev_press = 0;
return (sizeof(key_val)); }
intthird_drv_close(struct inode *inode, struct file *file) { free_irq(IRQ_EINT0, &pins_desc[0]); free_irq(IRQ_EINT2, &pins_desc[1]); free_irq(IRQ_EINT11, &pins_desc[2]); free_irq(IRQ_EINT19, &pins_desc[3]);
return0; }
staticstructfile_operationsthird_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = third_drv_open, .read = third_drv_read, .release = third_drv_close, };
staticint major;
staticintthird_drv_init(void) { major = register_chrdev(0, "my_third_drv", &third_drv_fops);
thirddrv_class = class_create(THIS_MODULE, "mythirddrv"); thirddrv_class_dev = class_device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatileunsignedlong *)ioremap(0x56000050, 16); gpfdat = gpfcon + 1;
gpgcon = (volatileunsignedlong *)ioremap(0x56000060, 16); gpgdat = gpgcon + 1;
return0; }
staticvoidthird_drv_exit(void) { unregister_chrdev(major, "my_third_drv"); class_device_unregister(thirddrv_class_dev); class_destroy(thirddrv_class); iounmap(gpfcon); iounmap(gpgcon); return0; }
module_init(third_drv_init); module_exit(third_drv_exit);
MODULE_LICENSE("GPL"); 五 test
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h>
/* thirddrvtest */ intmain(int argc, char **argv) { int fd; unsignedchar key_val;
fd = open("/dev/buttons", O_RDWR); if (fd < 0) { printf("can't open!\n"); }
while (1) { read(fd, &key_val, 1); printf("key_val = 0x%x\n", key_val); }
return0; }
---------------------------------------------------------------------------------------------------------------------- 我们尊重原创,也注重分享,文章来源于微信公众号:张笑生的地盘,建议关注公众号查看原文。如若侵权请联系qter@qter.org。 ---------------------------------------------------------------------------------------------------------------------- |