基本概念:
Linux操作系统下同裸机程序一样,需要利用中断机制来处理硬件的异步事件
但用户态不允许中断事件,因此中断必须由设备驱动程序来接收与处理
如果CPU接收到一个中断,它会停止一切工作,调用中断处理函数,因为进程调度依赖中断,此时进程调度也会停止
所以就要求我们的中断处理一定要快
中断处理程序运行在中断上下文
中断处理程序运行在中断上下文中:
上下文:内核运行环境,包括寄存器值、堆栈等数据
进程上下文:一个进程在内核态执行时,CPU的所有寄存器中的值、进程的状态以及堆栈中的内容等。
中断上下文:中断服务程序执行时所处的内核环境,CPU的所有寄存器的值、中断栈中的内容等。
中断上下文注意事项:
1.不能使用导致睡眠的处理机制(信号量、等待队列等)
2.不能与用户空间直接交互数据
3.中断处理函数执行时间尽可能短
中断相关函数介绍:
#include <linux/interrupt.h> //头文件
申请中断:
int request_irq(unsigned int irq,
irqreturn_t(*handler)(int,void*),
unsigned long irqflag,
const char *devname,
void *dev_id);
参数1:中断号,所申请的中断向量,比如EXIT0中断等定义在mach/irqs.h
参数2:中断服务程序(函数指针)
参数3:中断属性设置,定义在linux/interrupt.h
IRQF_DISABLED, 用于保证中断不被打断和嵌套
IRQF_SHARED, 申请子中断时,共享中断源
IRQF_SAMPLE_RANDOM, 中断可能被用来产生随机数
参数4:中断名字,cat /proc/interrupts 可察看系统中断申请与使用情况
参数5:中断参数,作为共享中断时用于区别不同中断的参数
设置中断触发方式(外部中断):
int set_irq_type(int irq, int edge);
参数1:中断号
参数2:外部中断触发方式定义在linux/irq.h
IRQ_TYPE_LEVEL_LOW,
IRQ_TYPE_LEVEL_HIGH,
IRQ_TYPE_EDGE_FALLING,
IRQ_TYPE_EDGE_RISING,
IRQ_TYPE_EDGE_BOTH
释放中断:
void free_irq(unsigned int irq, void *dev_id);
参数1:所要清除中断服务程序的中断号
参数2:中断参数,同申请时的值一致
使能中断:
void enable_irq(unsigned int irq);
关闭中断,并等待中断处理完成后返回:
void disable_irq(unsigned int irq);
注意:当用户在中断处理函数中使用该函数会形成死锁
关闭中断,立即返回:
void disable_irq_nosync(unsigned int irq);
中断服务程序:
irqreturn_t handler(int irq,void *dev_id) {
...... // 中断处理
return IRQ_HANDLED;
}参数1:中断号,不同中断共用一个中断服务程序时区分各中断参数2:中断参数,不同设备分享同一中断时,区分中断来源返回值:IRQ_HANDLED在linux/irqreturn.h中定义
函数汇总表:
实例代码:
驱动端:
#include <linux/device.h>
#include <linux/interrupt.h>
#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 <mach/gpio.h>
#include <mach/regs-gpio.h> /*S5PV210_GPH3_BASE*/
#define EINT_DEVICE_ID 1
#define EINT_DEVICE_ID_CLONE 2
#define DRIVER_NAME "key1_eint"
#define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
#define __debug(fmt, arg...) printk(KERN_WARNING fmt, ##arg)
#define GPH3CON (unsigned long)(S5PV210_GPH3_BASE+ 0x00)
#define GPH3DAT (unsigned long)(S5PV210_GPH3_BASE + 0x04)
#define GPH2UP (unsigned long)(S5PV210_GPH2_BASE + 0x08)
static int major = 0; /* Driver Major Number */
static int minor = 0; /* Driver Minor Number */
struct class *key_class;
static struct device *key_device;
static unsigned char key;
irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
if((int)(dev_id) != EINT_DEVICE_ID)
return IRQ_NONE;
key = (unsigned int)dev_id;
__debug("in eint function...\n");
return IRQ_HANDLED;
}
/* 共享中断实例,去掉注释即可验证
irqreturn_t buttons_interrupt_clone(int irq, void *dev_id)
{
if((int)(dev_id) != EINT_DEVICE_ID_CLONE)
return IRQ_NONE;
__debug("in eint function clone...\n");
return IRQ_HANDLED;
}
*/
static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
int key_num;
int cpy_len;
int retval;
key_num = key; //读取键值
key = 0; //清除键值
cpy_len = min(sizeof(key_num), count);
retval = copy_to_user(buf, &key_num, cpy_len);
return (cpy_len - retval);
}
/* Driver Operation structure */
static struct file_operations key_fops = {
.owner = THIS_MODULE,
.read = key_read,
};
static void key_io_port_init(void)
{
unsigned long reg_val;
reg_val = readl(GPH3CON);
reg_val &= ~((0x0f<<0) | (0x0f<<4));
reg_val |= ((0x01<<0) | (0x01<<4));
writel(reg_val, GPH3CON);
reg_val = readl(GPH3DAT);
reg_val &= ~((0x01<<0) | (0x01<<1));
writel(reg_val, GPH3DAT);
reg_val = readl(GPH2UP);
reg_val &= ~(0x03<<8);
reg_val |= 0x02<<8;
writel(reg_val, GPH2UP);
}
static int __init key_eint_init(void)
{
int retval;
key_io_port_init();
// 设置中断的触发类型,我们也可以在request_irq函数中
// 直接设置中断触发类
// retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING);
// if(retval){
// err("IRQ_EINT20 set irq type failed");
// goto error;
// }
retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_SHARED|IRQ_TYPE_EDGE_FALLING,
"KEY1", (void *)EINT_DEVICE_ID);
if(retval){
err("request eint20 failed");
goto error;
}
/* 共享中断实例,去掉注释即可验证
retval = request_irq(IRQ_EINT(20), buttons_interrupt_clone, IRQF_SHARED|IRQ_TYPE_EDGE_FALLING,
"KEY1_CLONE", (void *)EINT_DEVICE_ID_CLONE);
if(retval){
err("request eint20 failed");
goto error;
}
*/
/* Driver register */
major = register_chrdev(major, DRIVER_NAME, &key_fops);
if(major < 0){
err("register char device fail");
retval = major;
goto error_register;
}
key_class=class_create(THIS_MODULE,DRIVER_NAME);
if(IS_ERR(key_class)){
err("class create failed!");
retval = PTR_ERR(key_class);
goto error_class;
}
key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
if(IS_ERR(key_device)){
err("device create failed!");
retval = PTR_ERR(key_device);
goto error_device;
}
__debug("register myDriver OK! Major = %d\n", major);
return 0;
error_device:
class_destroy(key_class);
error_class:
unregister_chrdev(major, DRIVER_NAME);
error_register:
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
error:
return retval;
}
static void __exit key_eint_exit(void)
{
//__debug("in key_eint_exit\n");
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
/* 共享中断实例,去掉注释即可验证
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID_CLONE);
*/
unregister_chrdev(major, DRIVER_NAME);
device_destroy(key_class,MKDEV(major, minor));
class_destroy(key_class);
return;
}
module_init(key_eint_init);
module_exit(key_eint_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eric");
应用层:
#include <linux/device.h>
#include <linux/interrupt.h>
#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 <mach/gpio.h>
#include <mach/regs-gpio.h> /*S5PV210_GPH3_BASE*/
#define EINT_DEVICE_ID 1
#define EINT_DEVICE_ID_CLONE 2
#define DRIVER_NAME "key1_eint"
#define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
#define __debug(fmt, arg...) printk(KERN_WARNING fmt, ##arg)
#define GPH3CON (unsigned long)(S5PV210_GPH3_BASE+ 0x00)
#define GPH3DAT (unsigned long)(S5PV210_GPH3_BASE + 0x04)
#define GPH2UP (unsigned long)(S5PV210_GPH2_BASE + 0x08)
static int major = 0; /* Driver Major Number */
static int minor = 0; /* Driver Minor Number */
struct class *key_class;
static struct device *key_device;
static unsigned char key;
irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
if((int)(dev_id) != EINT_DEVICE_ID)
return IRQ_NONE;
key = (unsigned int)dev_id;
__debug("in eint function...\n");
return IRQ_HANDLED;
}
/* 共享中断实例,去掉注释即可验证
irqreturn_t buttons_interrupt_clone(int irq, void *dev_id)
{
if((int)(dev_id) != EINT_DEVICE_ID_CLONE)
return IRQ_NONE;
__debug("in eint function clone...\n");
return IRQ_HANDLED;
}
*/
static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
int key_num;
int cpy_len;
int retval;
key_num = key; //读取键值
key = 0; //清除键值
cpy_len = min(sizeof(key_num), count);
retval = copy_to_user(buf, &key_num, cpy_len);
return (cpy_len - retval);
}
/* Driver Operation structure */
static struct file_operations key_fops = {
.owner = THIS_MODULE,
.read = key_read,
};
static void key_io_port_init(void)
{
unsigned long reg_val;
reg_val = readl(GPH3CON);
reg_val &= ~((0x0f<<0) | (0x0f<<4));
reg_val |= ((0x01<<0) | (0x01<<4));
writel(reg_val, GPH3CON);
reg_val = readl(GPH3DAT);
reg_val &= ~((0x01<<0) | (0x01<<1));
writel(reg_val, GPH3DAT);
reg_val = readl(GPH2UP);
reg_val &= ~(0x03<<8);
reg_val |= 0x02<<8;
writel(reg_val, GPH2UP);
}
static int __init key_eint_init(void)
{
int retval;
key_io_port_init();
// 设置中断的触发类型,我们也可以在request_irq函数中
// 直接设置中断触发类
// retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING);
// if(retval){
// err("IRQ_EINT20 set irq type failed");
// goto error;
// }
retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_SHARED|IRQ_TYPE_EDGE_FALLING,
"KEY1", (void *)EINT_DEVICE_ID);
if(retval){
err("request eint20 failed");
goto error;
}
/* 共享中断实例,去掉注释即可验证
retval = request_irq(IRQ_EINT(20), buttons_interrupt_clone, IRQF_SHARED|IRQ_TYPE_EDGE_FALLING,
"KEY1_CLONE", (void *)EINT_DEVICE_ID_CLONE);
if(retval){
err("request eint20 failed");
goto error;
}
*/
/* Driver register */
major = register_chrdev(major, DRIVER_NAME, &key_fops);
if(major < 0){
err("register char device fail");
retval = major;
goto error_register;
}
key_class=class_create(THIS_MODULE,DRIVER_NAME);
if(IS_ERR(key_class)){
err("class create failed!");
retval = PTR_ERR(key_class);
goto error_class;
}
key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
if(IS_ERR(key_device)){
err("device create failed!");
retval = PTR_ERR(key_device);
goto error_device;
}
__debug("register myDriver OK! Major = %d\n", major);
return 0;
error_device:
class_destroy(key_class);
error_class:
unregister_chrdev(major, DRIVER_NAME);
error_register:
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
error:
return retval;
}
static void __exit key_eint_exit(void)
{
//__debug("in key_eint_exit\n");
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
/* 共享中断实例,去掉注释即可验证
free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID_CLONE);
*/
unregister_chrdev(major, DRIVER_NAME);
device_destroy(key_class,MKDEV(major, minor));
class_destroy(key_class);
return;
}
module_init(key_eint_init);
module_exit(key_eint_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eric");
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char *devname = "/dev/key1_eint";
int fd;
unsigned char key;
fd = open(devname, O_RDWR);
while(1)
{
read(fd, &key, sizeof(key));
if(key)
printf("the key = %d\n",key);
usleep(100*1000);
}
close(fd);
}