How to configure and use GPIO interrupts in embedded Linux userspace?

Q: How to configure and use GPIO interrupts in embedded Linux userspace?

Answer

Userspace GPIO access via libgpiod (Linux 4.8+): use gpiochip device files in /dev. Command-line: gpioset gpiochip0 23=1 to set gpioget gpiochip0 23 to read. For interrupts use poll() on /dev/gpiochip with GPIOEVENT_REQUEST_BOTH_EDGES. C example: open /dev/gpiochipX request event with GPIOHANDLE_REQUEST_INPUT | GPIOHANDLE_REQUEST_EVENTS then poll(gpio_fd …) in a loop. For better performance in latency-critical code use the kernel GPIO driver directly (struct gpio_desc + gpiod_to_irq() + request_threaded_irq). Avoid polling in a busy loop. Use kernel character device (/dev/gpiochipN) rather than sysfs which is deprecated. For high-frequency interrupts (>10 kHz) implement the handler in kernel space as a threaded IRQ.

Filed under: FAQ

Leave a Reply

Your email address will not be published. Required fields are marked *