GPIO

General Purpose Input Output (GPIO) is most commonly used hardware peripherals. To understand GPIO as Hardware prospective, visit GPIO

GPIO access in Linux follows a producer/consumer model. Kernel drivers act as producers by exposing physical chips and pins to the system. Userspace applications act as consumers, safely acquiring and controlling those lines using the standard Linux GPIO character device API. for more on how linux handled it, visit TODO.

How userspace GPIO access works

The important point is that your userspace C application normally does not access GPIO registers directly. It asks the kernel to operate the pin. The kernel driver handles the hardware-specific register operations.

Using sysfs interface (old way)

Until Linux version 4.7, sysfs was used to manage and control GPIO. This interface is deprecated in Linux, and it may not be enabled in your kernel.

steps using sysfs

  • Check whether sysfs GPIO is available
  • Identify the GPIO Number
  • Export the GPIO writing its number to /sys/class/gpio/export.
  • Configure the GPIO line as input/output writing in/out to /sys/class/gpio/gpioX/direction.
  • Set the GPIO high/low by writing 1/0 to /sys/class/gpio/gpioX/value or read the GPIO value by reading /sys/class/gpio/gpioX/value

example using command


example using c


#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
    int fd = open("/sys/class/gpio/gpio17/value", O_WRONLY);

    if (fd < 0) {
        perror("open");
        return 1;
    }

    for (int i = 0; i < 10; i++) {
        if (write(fd, "1", 1) != 1) {
            perror("write HIGH");
            close(fd);
            return 1;
        }
        sleep(1);

        if (write(fd, "0", 1) != 1) {
            perror("write LOW");
            close(fd);
            return 1;
        }
        sleep(1);
    }

    close(fd);
    return 0;
}

Major drawbacks of sysfs GPIO

  • Deprecated interface: The legacy GPIO sysfs ABI is deprecated; new applications should use the character-device API.
  • No atomic multi-GPIO operations: Changing multiple GPIO lines together is difficult and may produce timing differences between pins.
  • Limited event handling: It supports basic edge notifications, but lacks the richer event and request model of the character-device API.
  • Global GPIO numbers: Legacy global GPIO numbers can be confusing across multiple controllers and are not a reliable hardware-independent identifier.
  • Export/unexport management: Applications must manage GPIO exports and handle conflicts when another process has already exported a line.
  • Poorer request management: The interface does not provide the same explicit line-request ownership model as the character-device API.
  • Not suitable for precise timing: Userspace scheduling and file operations introduce latency and jitter, so sysfs is unsuitable for precise bit-banging or real-time signaling.

Since Linux version 4.8 the GPIO sysfs interface is deprecated, and now we have a new API based on character devices to access GPIO lines from user space.

Every GPIO controller (gpiochip) will have a character device file in /dev (created by linux gpio driver) and we can use file operations (open(), read(), write(), ioctl(), poll(), close()) to manage and interact with GPIO lines:


©2023-2024 rculock.com