Manage the GPIO lines from command line 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

Detect the group of GPIO lines available

Roadrunner board

sudo gpiodetect

gpiochip0 [fc038000.pinctrl] (128 lines)
gpiochip1 [fc040000.secumod] (8 lines)

Acqua board

sudo gpiodetect

gpiochip0 [fffff200.gpio] (32 lines)
gpiochip1 [fffff400.gpio] (32 lines)
gpiochip2 [fffff600.gpio] (32 lines)
gpiochip3 [fffff800.gpio] (32 lines)
gpiochip4 [fffffa00.gpio] (32 lines)

Read the state of each GPIO line

Roadrunner board

sudo gpioinfo fc038000.pinctrl

gpiochip0 - 128 lines:
    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 

sudo gpioinfo fc040000.secumod

gpiochip1 - 8 lines:
    line   0:      unnamed       unused  output  active-high 
    line   1:      unnamed       unused   input  active-high 
    line   2:      unnamed       unused   input  active-high 
    line   3:      unnamed       unused   input  active-high 
    line   4:      unnamed       unused   input  active-high 
    line   5:      unnamed       unused   input  active-high 
    line   6:      unnamed       unused   input  active-high 
    line   7:      unnamed       unused   input  active-high 

Acqua board

sudo gpioinfo fffff200.gpio

gpiochip0 - 32 lines:
    line   0:      "pioA0"       unused   input  active-high 
    line   1:      "pioA1"       unused   input  active-high 
    line   2:      "pioA2"       unused   input  active-high 
    line   3:      "pioA3"       unused   input  active-high 
    line   4:      "pioA4"       unused   input  active-high 
    line   5:      "pioA5"       unused   input  active-high 
    line   6:      "pioA6"       unused   input  active-high 
    line   7:      "pioA7"       unused   input  active-high 
    line   8:      "pioA8"       unused   input  active-high 
    line   9:      "pioA9"       unused   input  active-high 
    line  10:     "pioA10"       unused   input  active-high 
    line  11:     "pioA11"       unused   input  active-high 
    line  12:     "pioA12"       unused   input  active-high 
    line  13:     "pioA13"       unused   input  active-high 
    line  14:     "pioA14"       unused   input  active-high 
    line  15:     "pioA15"       unused   input  active-high 
    line  16:     "pioA16"       unused   input  active-high 
    line  17:     "pioA17"       unused   input  active-high 
    line  18:     "pioA18"       unused   input  active-high 
    line  19:     "pioA19"       unused   input  active-high 
    line  20:     "pioA20"       unused   input  active-high 
    line  21:     "pioA21"       unused   input  active-high 
    line  22:     "pioA22"       unused   input  active-high 
    line  23:     "pioA23"       unused   input  active-high 
    line  24:     "pioA24"       unused   input  active-high 
    line  25:     "pioA25"       unused   input  active-high 
    line  26:     "pioA26"       unused   input  active-high 
    line  27:     "pioA27"       unused   input  active-high 
    line  28:     "pioA28"       unused   input  active-high 
    line  29:     "pioA29"       unused   input  active-high 
    line  30:     "pioA30"       unused   input  active-high 
    line  31:     "pioA31"       unused   input  active-high    

Read the value of a single GPIO line.

Roadrunner board

sudo gpiofind "PA24"

    gpiochip0 24

sudo gpioget gpiochip0 24

    1

wire PA24 to GND and try again:

sudo gpioget gpiochip0 24

    0

Acqua board

sudo gpiofind "pioA0"

    gpiochip0 0

sudo gpioget gpiochip0 0

    1

wire PA24 to GND and try again:

sudo gpioget gpiochip0 0

    0

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

sudo gpioget --active-low gpiochip0 24 25

    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

    event:  RISING EDGE offset: 3 timestamp: [    1151.814356387]
    event:  RISING EDGE offset: 3 timestamp: [    1151.815449803]
    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

    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

Check the GPIO status

sudo cat /sys/kernel/debug/pinctrl/ahb:apb:pinctrl@fffff200/pinmux-pins

Links

Sergio Tanzilli
Systems designer, webmaster of www.acmesystems.it and founder of Acme Systems srl

Personal email: tanzilli@acmesystems.it
Web pages: https://www.acmesystems.it --- https://www.acmestudio.it
Github repositories: https://github.com/tanzilli --- https://github.com/acmesystems
Telegram group dedicated to the Acme Systems boards: https://t.me/acmesystemssrl