Skip to content

Test: Linux GPIO from user space (libgpiod and pigpio)

Linux GPIO

Linux GPIO from User Space: libgpiod and pigpio

Who owns the pins: the kernel or your program

On Linux the kernel owns the GPIO hardware. A program asks for lines through the GPIO character device (/dev/gpiochip0 and its neighbours), using libgpiod or the gpio tools built on it. The kernel records who holds each line (the consumer), refuses a second request for a line in use, and releases a program’s lines when it exits or crashes. pigpio and pinctrl work differently: they map the GPIO registers into their own memory and write them directly, and the kernel is not told. The older sysfs interface (/sys/class/gpio) goes through the kernel but is deprecated. Start fan-ctl, which holds GPIO17 to run the fan, then let a second program try to switch the fan off.

Challenge: Show the kernel refusing a second owner, then switch the fan off without going through the kernel.

Character device

/dev/gpiochipN. A program requests lines by offset; the kernel records the consumer and allows one owner per line.

libgpiod, gpioset, gpioget, gpiomon, lgpio

Direct register access

Map the GPIO registers through /dev/mem or /dev/gpiomem and write them. The kernel is not involved and is not told.

pigpio, pinctrl, RPi.GPIO

sysfs (deprecated)

/sys/class/gpio: through the kernel, but global numbers, no bias or debounce, and lines stay exported after a crash.

GPIO17 = gpio529 here

fan-ctl (libgpiod)not runningsecond programgpioset (libgpiod)Linux kernel: gpiolib and the GPIO character device/dev/gpiochip0 [pinctrl-bcm2711]kernel’s record for line 17unusedGPIO registers in the BCM2711 (0xFE200000)request, ioctl()driverGPIO17 = 0fan off
$ gpioinfo GPIO17
gpiochip0 17	"GPIO17"        	input
← Start here
Second program

Nothing has happened yet.

Owner (kernel)

none

GPIO17

0

Records agree?

yes

libgpiod v2’s gpioset keeps running to hold its line, because a released line’s level is not guaranteed. pinctrl is a debugging tool; it is useful for exactly this reason, and a hazard for the same one.