| 62 | | [=#gpiolib] |
| 63 | | == gpiolib and gpio class == |
| | 62 | [=#gpiochardev] |
| | 63 | == chardev GPIO |
| | 64 | The GPIO character device ABI (Application Binary Interface) was introduced in Linux 4.8 (which subsequently marked the User-mode sysfsgpio method deprecated). This is a 'descriptor-based' character device exposed as {{{/dev/gpiochipN}}} or {{{/sys/bus/gpiochipN}}} where N is the chip number. |
| | 65 | |
| | 66 | The main feature of this new interface is a discovery mechanism which aids in having to figure out where a particular GPIO was mapped. It also supports open-drain I/O support and the ability to read and set multiple I/O lines at once. The downside to this interface is that it prevents manipulating GPIO with standard command line tools such as echo and cat. |
| | 67 | |
| | 68 | Fortunately the kernel is distributed with three basic user-mode tools written for testing the interface which also show examples of the API. These can be found in the kernel source [https://elixir.bootlin.com/linux/latest/source/tools/gpio tools/gpio]: |
| | 69 | * lsgpio - example showing how to list the GPIO lines on a system |
| | 70 | * gpio-event-mon - example showing how to monitor GPIO line events from userspace |
| | 71 | * gpio-hammer - example swiss army knife to wiggle GPIO lines |
| | 72 | |
| | 73 | Additionally [https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/ libgpiod] provides both API calls for use in your own programs and the following user-mode apps to manipulate GPIO lines: |
| | 74 | - **gpiodetect** - list all gpiochips present on the system, their names, labels and number of GPIO lines |
| | 75 | - **gpioinfo** - list all lines of specified gpiochips, their names, consumers, direction, active state and additional flags |
| | 76 | - **gpioget** - read values of specified GPIO lines |
| | 77 | - **gpioset** - set values of specified GPIO lines, potentially keep the lines exported and wait until timeout, user input or signal |
| | 78 | - **gpiofind** - find the gpiochip name and line offset given the line name |
| | 79 | - **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 |
| | 80 | |
| | 81 | On Ubuntu {{{libgpiod}}} and its apps can be isntalled via the {{{gpiod}}} package: |
| | 82 | {{{#!bash |
| | 83 | apt install gpiod |
| | 84 | }}} |
| | 85 | |
| | 86 | Examples: |
| | 87 | - Newport |
| | 88 | {{{#!bash |
| | 89 | root@bionic-newport:~# apt install gpiod |
| | 90 | ... |
| | 91 | root@bionic-newport:~# gpiodetect |
| | 92 | gpiochip0 [gpio_thunderx] (48 lines) |
| | 93 | gpiochip1 [pca9555] (16 lines) |
| | 94 | root@bionic-newport:~# gpioinfo gpio_thunderx |
| | 95 | gpiochip0 - 48 lines: |
| | 96 | line 0: unnamed unused input active-high |
| | 97 | line 1: unnamed unused input active-high |
| | 98 | line 2: unnamed unused input active-high |
| | 99 | line 3: unnamed unused input active-high |
| | 100 | line 4: unnamed "interrupt" input active-high [used] |
| | 101 | line 5: unnamed unused input active-high |
| | 102 | line 6: unnamed unused input active-high |
| | 103 | line 7: unnamed unused input active-high |
| | 104 | line 8: unnamed "mmc_supply_3v3" output active-high [used] |
| | 105 | line 9: unnamed unused input active-high |
| | 106 | line 10: unnamed unused output active-high |
| | 107 | line 11: unnamed unused input active-high |
| | 108 | line 12: unnamed unused input active-high |
| | 109 | line 13: unnamed unused input active-high |
| | 110 | line 14: unnamed "user2" output active-high [used] |
| | 111 | line 15: unnamed unused output active-high |
| | 112 | line 16: unnamed unused output active-high |
| | 113 | line 17: unnamed unused output active-high |
| | 114 | line 18: unnamed unused output active-high |
| | 115 | line 19: unnamed unused output active-high |
| | 116 | line 20: unnamed unused input active-high |
| | 117 | line 21: unnamed unused input active-high |
| | 118 | line 22: unnamed unused input active-high |
| | 119 | line 23: unnamed unused output active-high |
| | 120 | line 24: unnamed unused input active-high |
| | 121 | line 25: unnamed unused input active-high |
| | 122 | line 26: unnamed unused input active-high |
| | 123 | line 27: unnamed unused input active-high |
| | 124 | line 28: unnamed unused output active-high |
| | 125 | line 29: unnamed unused input active-high |
| | 126 | line 30: unnamed "pps-gpio" input active-high [used] |
| | 127 | line 31: unnamed "user1" output active-high [used] |
| | 128 | line 32: unnamed unused input active-high |
| | 129 | line 33: unnamed unused input active-high |
| | 130 | line 34: unnamed unused input active-high |
| | 131 | line 35: unnamed unused input active-high |
| | 132 | line 36: unnamed unused input active-high |
| | 133 | line 37: unnamed unused input active-high |
| | 134 | line 38: unnamed unused input active-high |
| | 135 | line 39: unnamed unused input active-high |
| | 136 | line 40: unnamed unused input active-high |
| | 137 | line 41: unnamed unused input active-high |
| | 138 | line 42: unnamed unused input active-high |
| | 139 | line 43: unnamed unused input active-high |
| | 140 | line 44: unnamed unused input active-high |
| | 141 | line 45: unnamed unused input active-high |
| | 142 | line 46: unnamed unused input active-high |
| | 143 | line 47: unnamed unused input active-high |
| | 144 | root@bionic-newport:~# gpioget gpio_thunderx 24 # read gpio24 (DIO0) |
| | 145 | 1 |
| | 146 | root@bionic-newport:~# gpioset gpio_thunderx 24=0 # drive low gpio24 (DIO0) |
| | 147 | }}} |
| | 148 | |
| | 149 | References: |
| | 150 | * https://elinux.org/images/9/9b/GPIO_for_Engineers_and_Makers.pdf |
| | 151 | |
| | 152 | [=#sysfsgpio] |
| | 153 | == User-mode GPIO class |