您的位置:首页 > 其它

字符设备驱动--外部中断

2016-03-27 02:18 176 查看
使用头文件

#include <linux/irq.h>

#include <asm/irq.h>

//定义一个file_operations结构体

static struct file_operations secdrv_fops =

{

        .owner = THIS_MODULE,

        .open  = secdrv_open,

        .read  = secdrv_read,

        .release = secdrv_close,  //用于释放中断

};

//初始化中断

static int secdrv_open(struct inode *inode ,struct file *file)

{

        request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"s1",&pins_desc[0]);

        return 0;

}

//释放中断

int secdrv_close(struct inode *inode,struct file *file)

{

        free_irq(IRQ_EINT0, &pins_desc[0]);

}

//中断入口函数

static irqreturn_t buttons_irq(int irq, void *dev_id)

{

        return IRQ_RETVAL(IRQ_HANDLED);  //中断函数返回值

}

/*在Linux 2.6内核中,request_irq() 函数是注册中断服务函数>:函数的原型如下:

int request_irq (unsigned int irq, void (*handler)

                        (int, void *, struct pt_regs *),

                        unsigned long frags,

                        const char *device,

                        void *dev_id);

5个参数的含义如下:

第一个参数irq:申请的硬件中断号;

第二个参数handler:是一个函数指针,向系统登记的中断处理函>数,是一个回调函数,当中断发生时,系统调用      这                             个函数,传入的参数包括中断设备 id,寄存器值。

第三个参数flags:指定了快速中断或中断共享等中断处理属性。

第四个参数devices:指定设备驱动程序的名称。

第五个参数dev_id:传入中断处理程序的参数,可以为NULL,在注

                                册共享中断时,此参数不能为NULL,作为共享中断时的中断区别参

数,并且用于释放中断的标志。

返回值:

函数运行正常时返回 0 ,否则返回对应错误的负值。

Linux可以让多个设备共享一个中断号,而且共享同一中断的中断>处理程序形成一个链表,内核对每个中断处理程序都要执行

*/

flags : IRQT_RISING,IRQT_FALLING,IRQT_BOTHEDGE,IRQT_LOW ,IRQT_HIGH

/*

DECLARE_WAIT_QUEUE_HEAD(name);//生成一个等待队列头

wake_up_interruptible(&name);  //唤醒休眠

wait_event_interruptible(name, ev_press);//ev_press  0:休眠 1:唤醒

参考 //http://blog.chinaunix.net/uid-27717694-id-3895960.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: