How to write GPIO interrupt handler in Linux device driver?

3,547 views
Skip to first unread message

TheInventor1993

unread,
Jan 25, 2015, 9:54:39 AM1/25/15
to beagl...@googlegroups.com
I am writing GPIO Device Driver for linux 3.14 on Beagleboard XM (RevB). I have done with simple ON/OFF LED blinking. But stuck at interrupt handler implementation. BB XM has GPIO_7 pin connected to user button. Following is code sample I have implemented. But it is not working. I am able to insert module using insmod. Interrupt is also registered in /proc/interrupts. But handler is not getting invoked.

Am I on right track?

#include <linux/gpio.h>
#include <linux/interrupt.h>

#define GPIO 7

int var;

static irqreturn_t irq_handler(int irq, void *dev_id)
{
 
var++;
  printk
("In the Interrupt handler, Count: %d\n", var);
 
return IRQ_HANDLED;
}

static int __init hello_init(void)
{
 
int errno, irq_line;
 
if((errno = gpio_direction_input(GPIO)) !=0)
 
{
    printk
(KERN_INFO "Can't set GPIO direction, error %i\n", errno);
    gpio_free
(GPIO);
   
return -EINVAL;
 
}
  irq_line
= gpio_to_irq(GPIO);
  printk
("IRQ Line is %d \n",irq_line);

  errno
= request_irq( irq_line, (irq_handler_t)irq_handler, IRQF_TRIGGER_FALLING, "Interrupt123", NULL );
 
if(errno<0)
 
{
    printk
(KERN_INFO "Problem requesting IRQ, error %i\n", errno);
 
}
 
return 0;
}

static void __exit hello_exit(void)
{
 
int irq_line;
  printk
("Unloading my module.\n");
  irq_line
= gpio_to_irq(GPIO);
  free_irq
(irq_line,NULL);
  printk
("Hello Example Exit\n");
 
return;
}

module_init
(hello_init);
module_exit
(hello_exit);

MODULE_AUTHOR
("TheInventor");
MODULE_DESCRIPTION
("Interrupt Module");
MODULE_LICENSE
("GPL");


Do Minh Quoc

unread,
Sep 28, 2015, 11:35:36 AM9/28/15
to BeagleBoard
Hi,
Could you help me know where to store the linux/gpio.h file in beagleboard xM?

santhos...@gmail.com

unread,
Oct 30, 2018, 10:10:41 AM10/30/18
to BeagleBoard
It is working thank you

Dave Chapman

unread,
Oct 30, 2018, 10:26:13 AM10/30/18
to beagl...@googlegroups.com

What does dmesg look like?

 

I would have made the variable var look like this

“static   int   var;”

 

Also, I would have added a proc to display the current value of var.

 

Finally, I would have added something this to hello_init():

printk(“Entered hello_init().\n”);

 

None of these things are strictly necessary, but that’s how I do it. . .

 

Regards,

 

Dave Chapman

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/830130c8-0310-4d3c-b0f8-d08caef6ccdf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

santhos...@gmail.com

unread,
Oct 30, 2018, 5:27:54 PM10/30/18
to BeagleBoard

I have used this driver on dragon 410c platform.
Not on Beagleboard.
Reply all
Reply to author
Forward
0 new messages