Wednesday, September 21, 2011

GPIO Interrupts on the i.MX53


So I had been scouring the web for some information on how to use the GPIO pins as kernel interrupts. There is plenty of reference material on how to use sysfs interface to poll the pins manually but I couldn't figure out the interrupts, with some perserverance and  some digging through the source tree I came up with this.

Bear in mind my platform is the imx53 based DIMM-MX53 from Emtrion but the theory should still apply across the board including the IMXQSB.

static irqreturn_t button_interrupt(int irq, unsigned long data)
{
printk(KERN_INFO "Hello World\n");
tasklet_schedule(&my_tasklet);
return IRQ_HANDLED;
}

static int __init hello_start(void)
{
int ret;
 
printk(KERN_INFO "Loaded hello module....\n");
ret = request_irq(gpio_to_irq(42), button_interrupt,
IRQF_DISABLED | IRQF_TRIGGER_FALLING,
"hello", NULL);
if(ret){
printk(KERN_ERR "Failed to request IRQ\n");
return ret;
}
return 0;
}
The macro gpio_to_irq(x) changes the GPIO address to an IRQ, in my case I'm using the second 32 bit GPIO port and bit 10 (GPIO2-10). It is also worth noting that it is probably bad practice to request the IRQ during the initialisation but rather when you want to use the device, this way you will not hog any system resource.

This should be enough to get you going, enjoy.

8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. i muste copy and paste this on my mx53_loco.c?

    ReplyDelete
    Replies
    1. This was part of kernel module I created to test the interrupts. I would use modprobe to load the module, then by using a button connected to the GPIO I could trigger the interrupt.

      Delete
  3. do you have an email or a chat? pls?

    ReplyDelete
    Replies
    1. I'd rather try an answer questions here so that others could benefit. :)

      Delete
  4. i have a board with imx53.. i have on CSI_DAT17 a gpio6_3. Iconnect here a button and it work when i press it, infact i view "value" cat /sys/class/gpio/gpio163/value:

    0<-pressed
    1<-not pressed

    now i want connect this gpio to an interrupt like you and when i press gpio i view in console: "GPIO6_3 is pressed"

    is possible?
    if you give me your email i send you my mx53_loco.c

    ReplyDelete
  5. MX53_PAD_CSI0_DAT17__GPIO6_3,

    ReplyDelete
  6. what is this tasklet_schedule(&my_tasklet);???

    how i must modified in my module?

    thanks

    ReplyDelete