日期:2014-05-16 浏览次数:20968 次
按钮中断例程
/************************************* NAME:EmbedSky_hello.c COPYRIGHT:www.embedsky.net *************************************/ #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/poll.h> #include <linux/irq.h> #include <asm/irq.h> #include <linux/interrupt.h> #include <asm/uaccess.h> #include <mach/regs-gpio.h> #include <mach/hardware.h> #include <linux/platform_device.h> #include <linux/cdev.h> #include <linux/miscdevice.h> #define DEVICE_NAME "IRQ-Test" //设备名称 struct button_irq_desc { int irq;//中断号码 int pin;//引脚 int pin_setting;//引脚功能 int number;//编号 char *name; //设备名称 }; //定义两套按钮 #if !defined (CONFIG_SKY2440_IRQ_TEST) static struct button_irq_desc button_irqs [] = { {IRQ_EINT1, S3C2410_GPF1, S3C2410_GPF1_EINT1, 0, "KEY1"}, /* K1 */ {IRQ_EINT4, S3C2410_GPF4, S3C2410_GPF4_EINT4, 1, "KEY2"}, /* K2 */ {IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 2, "KEY3"}, /* K3 */ {IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 3, "KEY4"}, /* K4 */ }; #else static struct button_irq_desc button_irqs [] = { {IRQ_EINT9, S3C2410_GPG1, S3C2410_GPG1_EINT9, 0, "KEY1"}, /* K1 */ {IRQ_EINT11, S3C2410_GPG3, S3C2410_GPG3_EINT11, 1, "KEY2"}, /* K2 */ {IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 2, "KEY3"}, /* K3 */ {IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 3, "KEY4"}, /* K4 */ }; #endif static volatile char key_values [] = {'0', '0', '0', '0'}; static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//生成一个等待队列 static volatile int ev_press = 0; static irqreturn_t irq_interrupt(int irq, void *dev_id) { struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id; int down; down = !s3c2410_gpio_getpin(button_irqs->pin); if (down != (key_values[button_irqs->number] & 1))//检测是否一样,并保存到key_values中 { key_values[button_irqs->number] = '0' + down; ev_press = 1; wake_up_interruptible(&button_waitq); } return IRQ_RETVAL(IRQ_HANDLED);//返回值,标准格式 } static int tq2440_irq_open(struct inode *inode, struct file *file) { int i; int err = 0; for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) { if (button_irqs[i].irq < 0)//检查完毕后跳出 continue; err = request_irq(button_irqs[i].irq, irq_interrupt, IRQ_TYPE_EDGE_BOTH, button_irqs[i].name, (void *)&button_irqs[i]); //共用一个中断处理程序。 if (err) break; } if (err) { i--; for (; i >= 0; i--) { if (button_irqs[i].irq < 0) continue; disable_irq(button_irqs[i].irq); free_irq(button_irqs[i].irq, (void *)&button_irqs[i]); } return -EBUSY; } ev_press = 1; return 0; } static int tq2440_irq_close(struct inode *inode, struct file *file) { int i; for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) { if (button_irqs[i].irq < 0)//没有设置过,就不用打开。 continue; free_irq(button_irqs[i].irq, (void *)&button_irqs[i]); } return 0; } static int tq2440_irq_read(struct file *filp, char __user *buff, size_t count, loff_t *offp) { unsigned long err; if (!ev_press) { if (filp->f_flags & O_NONBLOCK) return -EAGAIN; else wait_event_interruptible(button_waitq, ev_press); } ev_press = 0; err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count)); return err ? -EFAULT : min(sizeof(key_values), count); } static unsigned int tq2440_irq_poll( struct file *file, struct poll_table_struct *wait) { unsigned int mask = 0; poll_wait(file, &button_waitq, wait); if (ev_press) mask |= POLLIN | POLLRDNORM; return mask; } // static struct file_operations dev_fops = { .owner = THIS_MODULE, .op