This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

am335x 按键中断



写了一个按键中断驱动,应用程序里读按键值然后点灯

遇到的问题是:应用程序读函数执行后,调用驱动读函数开始休眠,然后在按键中断里打开休眠,通过无打印信息发现,按下按键后无法进入中断函数

后来把申请中断request_irq和free_irq分别放到加载init和卸载exit中后发现,加载驱动后按下按键,能够进入中断函数,但执行应用程序后就无法再进入中断了,是不是休眠会影响中断的发生?

部分驱动程序:

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;

中断处理函数

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
     printk(KERN_INFO "IRQ = %d\n",irq);
     ev_press = 1;
     wake_up_interruptible(&button_waitq);
     return IRQ_RETVAL(IRQ_HANDLED);
}

打开函数

static int gpio_open(struct inode *inode, struct file *filp)
{
request_irq(gpio_to_irq(GPIO_TO_PIN(1,16)), buttons_irq, IRQF_TRIGGER_FALLING, "Key1", NULL);
request_irq(gpio_to_irq(GPIO_TO_PIN(1,31)), buttons_irq, IRQF_TRIGGER_FALLING, "Key2", NULL);
}

关闭函数

static int gpio_close(struct inode *inode, struct file *filp)
{
free_irq(gpio_to_irq(GPIO_TO_PIN(1,16)), NULL);

free_irq(gpio_to_irq(GPIO_TO_PIN(1,31)), NULL);*/

}

读函数

static ssize_t gpio_read(struct file *filp, char __user *readBuf, size_t port, loff_t *offp)
{

wait_event_interruptible(button_waitq, ev_press);
ev_press = 0;

}

应用程序部分代码:

int main(int argc, char * argv)
{
int fd;
char key1,key2;
fd = open("/dev/am335x_gpio", O_RDWR); //打开设备
ioctl(fd, 1, GPIO_TO_PIN(1,21)); //设置gpio1-21为输出
ioctl(fd, 1, GPIO_TO_PIN(1,22)); //设置gpio1-22为输出

ioctl(fd, 0, GPIO_TO_PIN(1,16)); //设置gpio1-16为输入
ioctl(fd, 0, GPIO_TO_PIN(1,31)); //设置gpio1-31为输入
while (1)
{
     read(fd,&key1,GPIO_TO_PIN(1,16));
     read(fd,&key2,GPIO_TO_PIN(1,31));
     if(key1 == '0')
     write(fd,"0",GPIO_TO_PIN(1,21));
     else
     write(fd,"1",GPIO_TO_PIN(1,21));
     if(key2 == '0')
     write(fd,"0",GPIO_TO_PIN(1,22));
      else
       write(fd,"1",GPIO_TO_PIN(1,22));

}
}