| 1 |
.. |
| 2 |
SPDX-License-Identifier: CC-BY-SA-4.0 |
| 3 |
SPDX-FileCopyrightText: 2025 Bartosz Golaszewski <[email protected]> |
| 4 |
|
| 5 |
.. |
| 6 |
This file is part of libgpiod. |
| 7 |
|
| 8 |
GPIO tools documentation |
| 9 |
|
| 10 |
Command-line tools |
| 11 |
================== |
| 12 |
|
| 13 |
Overview |
| 14 |
-------- |
| 15 |
|
| 16 |
The **libgpiod** project includes a suite of **command-line tools** to |
| 17 |
facilitate GPIO manipulation from console and shell scripts. |
| 18 |
|
| 19 |
There are currently six command-line tools available: |
| 20 |
|
| 21 |
* **gpiodetect**: list all gpiochips present on the system, their names, labels |
| 22 |
and number of GPIO lines |
| 23 |
* **gpioinfo**: list lines, their gpiochip, offset, name, and direction, and if |
| 24 |
in use then the consumer name and any other configured attributes, such as |
| 25 |
active state, bias, drive, edge detection and debounce period |
| 26 |
* **gpioget**: read values of specified GPIO lines |
| 27 |
* **gpioset**: set values of specified GPIO lines, holding the lines until the |
| 28 |
process is killed or otherwise exits |
| 29 |
* **gpiomon**: wait for edge events on GPIO lines, specify which edges to watch |
| 30 |
for, how many events to process before exiting, or if the events should be |
| 31 |
reported to the console |
| 32 |
* **gpionotify**: wait for changed to the info for GPIO lines, specify which |
| 33 |
changes to watch for, how many events to process before exiting, or if the |
| 34 |
events should be reported to the console |
| 35 |
|
| 36 |
.. toctree:: |
| 37 |
:maxdepth: 1 |
| 38 |
:caption: Manual entries |
| 39 |
|
| 40 |
gpiodetect<gpiodetect> |
| 41 |
gpioinfo<gpioinfo> |
| 42 |
gpioget<gpioget> |
| 43 |
gpioset<gpioset> |
| 44 |
gpiomon<gpiomon> |
| 45 |
gpionotify<gpionotify> |
| 46 |
|
| 47 |
Examples |
| 48 |
-------- |
| 49 |
|
| 50 |
.. note:: |
| 51 |
The following examples where creating using a Raspberry Pi 4B. The pins |
| 52 |
will be different on other board. |
| 53 |
|
| 54 |
Detect the available gpiochips: |
| 55 |
|
| 56 |
.. code-block:: none |
| 57 |
|
| 58 |
$ gpiodetect |
| 59 |
gpiochip0 [pinctrl-bcm2711] (58 lines) |
| 60 |
gpiochip1 [raspberrypi-exp-gpio] (8 lines) |
| 61 |
|
| 62 |
Read the info for all the lines on a gpiochip: |
| 63 |
|
| 64 |
.. code-block:: none |
| 65 |
|
| 66 |
$ gpioinfo -c 1 |
| 67 |
gpiochip1 - 8 lines: |
| 68 |
line 0: "BT_ON" output |
| 69 |
line 1: "WL_ON" output |
| 70 |
line 2: "PWR_LED_OFF" output active-low consumer="led1" |
| 71 |
line 3: "GLOBAL_RESET" output |
| 72 |
line 4: "VDD_SD_IO_SEL" output consumer="vdd-sd-io" |
| 73 |
line 5: "CAM_GPIO" output consumer="cam1_regulator" |
| 74 |
line 6: "SD_PWR_ON" output consumer="sd_vcc_reg" |
| 75 |
line 7: "SD_OC_N" input |
| 76 |
|
| 77 |
Read the info for particular lines: |
| 78 |
|
| 79 |
.. code-block:: none |
| 80 |
|
| 81 |
$ ./gpioinfo PWR_LED_OFF STATUS_LED_G_CLK GLOBAL_RESET |
| 82 |
gpiochip0 42 "STATUS_LED_G_CLK" output consumer="led0" |
| 83 |
gpiochip1 2 "PWR_LED_OFF" output active-low consumer="led1" |
| 84 |
gpiochip1 3 "GLOBAL_RESET" output |
| 85 |
|
| 86 |
Read the value of a single GPIO line by name: |
| 87 |
|
| 88 |
.. code-block:: none |
| 89 |
|
| 90 |
$ gpioget RXD1 |
| 91 |
"RXD1"=active |
| 92 |
|
| 93 |
Read the value of a single GPIO line by chip and offset: |
| 94 |
|
| 95 |
.. code-block:: none |
| 96 |
|
| 97 |
$ gpioget -c 0 15 |
| 98 |
"15"=active |
| 99 |
|
| 100 |
Read the value of a single GPIO line as a numeric value: |
| 101 |
|
| 102 |
.. code-block:: none |
| 103 |
|
| 104 |
$ gpioget --numeric RXD1 |
| 105 |
1 |
| 106 |
|
| 107 |
Read two values at the same time, set the active state of the lines to low and |
| 108 |
without quoted names: |
| 109 |
|
| 110 |
.. code-block:: none |
| 111 |
|
| 112 |
$ gpioget --active-low --unquoted GPIO23 GPIO24 |
| 113 |
GPIO23=active GPIO24=active |
| 114 |
|
| 115 |
Set the value of a line and hold the line until killed: |
| 116 |
|
| 117 |
.. code-block:: none |
| 118 |
|
| 119 |
$ gpioset GPIO23=1 |
| 120 |
|
| 121 |
Set values of two lines, then daemonize and hold the lines: |
| 122 |
|
| 123 |
.. code-block:: none |
| 124 |
|
| 125 |
$ gpioset --daemonize GPIO23=1 GPIO24=0 |
| 126 |
|
| 127 |
Set the value of a single line, hold it for 20ms, then exit: |
| 128 |
|
| 129 |
.. code-block:: none |
| 130 |
|
| 131 |
$ gpioset --hold-period 20ms -t0 GPIO23=1 |
| 132 |
|
| 133 |
Blink an LED on GPIO22 at 1Hz: |
| 134 |
|
| 135 |
.. code-block:: none |
| 136 |
|
| 137 |
$ gpioset -t500ms GPIO22=1 |
| 138 |
|
| 139 |
Blink an LED on GPIO22 at 1Hz with a 20% duty cycle: |
| 140 |
|
| 141 |
.. code-block:: none |
| 142 |
|
| 143 |
$ gpioset -t200ms,800ms GPIO22=1 |
| 144 |
|
| 145 |
Set some lines interactively (requires ``--enable-gpioset-interactive``): |
| 146 |
|
| 147 |
.. code-block:: none |
| 148 |
|
| 149 |
$ gpioset --interactive --unquoted GPIO23=inactive GPIO24=active |
| 150 |
gpioset> get |
| 151 |
GPIO23=inactive GPIO24=active |
| 152 |
gpioset> toggle |
| 153 |
gpioset> get |
| 154 |
GPIO23=active GPIO24=inactive |
| 155 |
gpioset> set GPIO24=1 |
| 156 |
gpioset> get |
| 157 |
GPIO23=active GPIO24=active |
| 158 |
gpioset> toggle |
| 159 |
gpioset> get |
| 160 |
GPIO23=inactive GPIO24=inactive |
| 161 |
gpioset> toggle GPIO23 |
| 162 |
gpioset> get |
| 163 |
GPIO23=active GPIO24=inactive |
| 164 |
gpioset> exit |
| 165 |
|
| 166 |
Wait for three rising edge events on a single GPIO line, then exit: |
| 167 |
|
| 168 |
.. code-block:: none |
| 169 |
|
| 170 |
$ gpiomon --num-events=3 --edges=rising GPIO22 |
| 171 |
10002.907638045 rising "GPIO22" |
| 172 |
10037.132562259 rising "GPIO22" |
| 173 |
10047.179790748 rising "GPIO22" |
| 174 |
|
| 175 |
Wait for three edge events on a single GPIO line, with time in local time and |
| 176 |
with unquoted line name, then exit: |
| 177 |
|
| 178 |
.. code-block:: none |
| 179 |
|
| 180 |
$ gpiomon --num-events=3 --edges=both --localtime --unquoted GPIO22 |
| 181 |
2022-11-15T10:36:59.109615508 rising GPIO22 |
| 182 |
2022-11-15T10:36:59.129681898 falling GPIO22 |
| 183 |
2022-11-15T10:36:59.698971886 rising GPIO22 |
| 184 |
|
| 185 |
Wait for falling edge events with a custom output format: |
| 186 |
|
| 187 |
.. code-block:: none |
| 188 |
|
| 189 |
$ gpiomon --format="%e %c %o %l %S" --edges=falling -c gpiochip0 22 |
| 190 |
2 gpiochip0 22 GPIO22 10946.693481859 |
| 191 |
2 gpiochip0 22 GPIO22 10947.025347604 |
| 192 |
2 gpiochip0 22 GPIO22 10947.283716669 |
| 193 |
2 gpiochip0 22 GPIO22 10947.570109430 |
| 194 |
... |
| 195 |
|
| 196 |
Block until an edge event occurs, don't print anything: |
| 197 |
|
| 198 |
.. code-block:: none |
| 199 |
|
| 200 |
$ gpiomon --num-events=1 --quiet GPIO22 |
| 201 |
|
| 202 |
Monitor multiple lines, exit after the first edge event: |
| 203 |
|
| 204 |
.. code-block:: none |
| 205 |
|
| 206 |
$ gpiomon --quiet --num-events=1 GPIO5 GPIO6 GPIO12 GPIO17 |
| 207 |
|
| 208 |
Monitor a line for changes to info: |
| 209 |
|
| 210 |
.. code-block:: none |
| 211 |
|
| 212 |
$ gpionotify GPIO23 |
| 213 |
11571.816473718 requested "GPIO23" |
| 214 |
11571.816535124 released "GPIO23" |
| 215 |
11572.722894029 requested "GPIO23" |
| 216 |
11572.722932843 released "GPIO23" |
| 217 |
11573.222998598 requested "GPIO23" |
| 218 |
... |
| 219 |
|
| 220 |
Monitor a line for requests, reporting UTC time and unquoted line name: |
| 221 |
|
| 222 |
.. code-block:: none |
| 223 |
|
| 224 |
$ gpionotify --utc --unquoted GPIO23 |
| 225 |
2022-11-15T03:05:23.807090687Z requested GPIO23 |
| 226 |
2022-11-15T03:05:23.807151390Z released GPIO23 |
| 227 |
2022-11-15T03:05:24.784984280Z requested GPIO23 |
| 228 |
2022-11-15T03:05:24.785023873Z released GPIO23 |
| 229 |
... |
| 230 |
|
| 231 |
Monitor multiple lines, exit after the first is requested: |
| 232 |
|
| 233 |
.. code-block:: none |
| 234 |
|
| 235 |
$ gpionotify --quiet --num-events=1 --event=requested GPIO5 GPIO6 GPIO12 GPIO17 |
| 236 |
|
| 237 |
Block until a line is released: |
| 238 |
|
| 239 |
.. code-block:: none |
| 240 |
|
| 241 |
$ gpionotify --quiet --num-events=1 --event=released GPIO6 |