Manage GPIO lines with gpiod

gpiod is a set of tools for interacting with the linux GPIO character device that uses libgpiod library. Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use the character device instead. libgpiod encapsulates the ioctl calls and data structures behind a straightforward API.

If you are using Debian Buster 10 install gpiod by typing these commands:

sudo apt update
sudo apt install gpiod

Six command-line tools are available:

* gpiodetect - list all gpiochips present on the system, their names, labels
               and number of GPIO lines

* gpioinfo   - list all lines of specified gpiochips, their names, consumers,
               direction, active state and additional flags

* gpioget    - read values of specified GPIO lines

* gpioset    - set values of specified GPIO lines, potentially keep the lines
               exported and wait until timeout, user input or signal

* gpiofind   - find the gpiochip name and line offset given the line name

* gpiomon    - wait for events on GPIO lines, specify which events to watch,
               how many events to process before exiting or if the events
               should be reported to the console

Practical examples

Some examples on how to use the gpiod tools on a RoadRunner board

Detect the GPIO lines available

sudo gpiodetect
gpiochip0 [fc038000.pinctrl] (128 lines)

Read the state of each GPIO line

sudo gpioinfo fc038000.pinctrl
line   0:        "PA0"       unused   input  active-high 
line   1:        "PA1"       unused   input  active-high 
line   2:        "PA2"       unused   input  active-high 
line   3:        "PA3"       unused   input  active-high 
line   4:        "PA4"       unused   input  active-high 
...
line 123:       "PD27"       unused   input  active-high 
line 124:       "PD28"       unused   input  active-high 
line 125:       "PD29"       unused   input  active-high 
line 126:       "PD30"       unused   input  active-high 
line 127:       "PD31"       unused   input  active-high 

Read the value of a single GPIO line.

sudo gpiofind "PA24"
    reply -> gpiochip0 24

sudo gpioget gpiochip0 24
    reply -> 1

wire PA24 to GND and try again:

sudo gpioget gpiochip0 24
    reply -> 0

Read two values at the same time. Set the active state of the lines to low.

sudo gpioget --active-low gpiochip0 24 25
    reply -> 1 1

Set the value of a single line, then exit immediately. This is useful for floating pins.

sudo gpioset gpiochip0 24=1

Toggle a GPIO by name, then wait for the user to press ENTER.

sudo gpioset --mode=wait `gpiofind "PA24"`=1

Toggle PA24 high for 1 second.

sudo gpioset --mode=time --sec=1 gpiochip0 34=0

Wait for three rising edge events on a single GPIO line, then exit.

sudo gpiomon --num-events=3 --rising-edge gpiochip0 24
    reply --> event:  RISING EDGE offset: 3 timestamp: [    1151.814356387]
    reply --> event:  RISING EDGE offset: 3 timestamp: [    1151.815449803]
    reply --> event:  RISING EDGE offset: 3 timestamp: [    1152.091556803]

Wait for a single falling edge event. Specify a custom output format.

sudo gpiomon --format="%e %o %s %n" --falling-edge gpiochip0 24
    reply --> 0 25 1156 615459801

Pause execution until a single event of any type occurs. Don't print anything. Find the line by name.

sudo gpiomon --num-events=1 --silent `gpiofind "PA24"`

Monitor multiple lines, exit after the first event.

sudo gpiomon --silent --num-events=1 gpiochip0 24 25 31

Links

Sergio Tanzilli
System designer, webmaster and Acme Systems co-founder
Personal email: tanzilli@acmesystems.it
Webpages: https://www.acmesystems.it - https://www.tanzolab.it
Github repositories: https://github.com/tanzilli and https://github.com/acmesystems
Telegram group dedicated to the Acme Systems boards: https://t.me/acmesystemssrl

Bruna Mandolino