Template:GPIO LIBGPIOD DRAFT: Difference between revisions
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
| {{Note|'''NOTE:'''<br> | | {{Note|'''NOTE:'''<br> | ||
The Linux GPIO sysfs interface is being deprecated. Moving forward, user space should use the character device <code>/dev/gpiochip*</code> instead. libgpiod provides bindings and utilities for manipulating GPIO via user space.<br> | The Linux GPIO sysfs interface is being deprecated. Moving forward, user space should use the character device <code>/dev/gpiochip*</code> instead. libgpiod provides bindings and utilities for manipulating GPIO via user space.<br> | ||
The GPIO sysfs interface is no longer included in the Linux kernel by default. To include it, the following lines should be added to the Linux kernel defconfig:< | The GPIO sysfs interface is no longer included in the Linux kernel by default. To include it, the following lines should be added to the Linux kernel defconfig: | ||
<pre> | |||
CONFIG_EXPERT=y | |||
CONFIG_GPIO_SYSFS=y | |||
</pre> | |||
For a guide on how to manipulate GPIO via sysfs interface see: [[GPIO_SYSFS_DRAFT|Manipulating a single GPIO via /sys/class/gpio]] | For a guide on how to manipulate GPIO via sysfs interface see: [[GPIO_SYSFS_DRAFT|Manipulating a single GPIO via /sys/class/gpio]] | ||
| warning }} | | warning }} | ||
Line 72: | Line 74: | ||
event: RISING EDGE offset: 3 timestamp: [ 1152.091556803] | event: RISING EDGE offset: 3 timestamp: [ 1152.091556803] | ||
== Porting scripts using sysfs GPIO interface to libgpiod == | |||
Migrating from sysfs GPIO interface to libgpiod can be accomplished using the libgpiod package's command-line tools. | |||
For bash scripts, instead of writing or reading from /sys/class/gpio, you'll use these tools. | |||
<br> | |||
Here are a few examples of how you'd read and write in a GPIO using sysfs interface and the equivalent operations through libgpiod: | |||
{{Note|'''NOTE:'''<br> | |||
GPIOs in i.MX SoCs are grouped in groups of 32 pins. That is relevant when converting the GPIO absolute number used by sysfs GPIO interface.<br> | |||
For example:<br> | |||
GPIO 17 belongs to gpiochip0, pin 17: <code>32 * 0 + 17 equals 17</code>.<br> | |||
GPIO 83 belongs to gpiochip2, pin 19: <code>32 * 2 + 19 equals 83</code>.<br> | |||
GPIO 149 belongs to gpiochip4, pin 21: <code>32 * 4 + 21 equals 149</code>. | |||
| info }} | |||
<pre> | |||
# writing in a GPIO using sysfs GPIO interface | |||
echo 149 > /sys/class/gpio/export | |||
echo out > /sys/class/gpio/gpio149/direction | |||
echo 1 > /sys/class/gpio/gpio149/value | |||
# writing in a GPIO using libgpiod | |||
gpioset gpiochip4 21=1 | |||
</pre> | |||
<pre> | |||
# reading a GPIO using sysfs GPIO interface | |||
echo 149 > /sys/class/gpio/export | |||
echo in > /sys/class/gpio/gpio149/direction | |||
cat /sys/class/gpio/gpio149/value | |||
# reading a GPIO using libgpiod | |||
gpioget gpiochip4 21 | |||
</pre> | |||
Remember, it is also important to check the return status of these commands in your scripts to ensure they are working correctly. If a command fails, it will return a non-zero status which can be checked in the '''$?''' variable, this variable holds the exit status of the last command that was executed. For example: | |||
<pre> | |||
gpioget gpiochip4 21 | |||
if [ $? -ne 0 ]; then | |||
echo "Failed to get GPIO value" | |||
exit 1 | |||
fi | |||
</pre> | |||
== libgpiod C Application == | == libgpiod C Application == | ||
Latest revision as of 09:25, 15 May 2023
Manipulating GPIO using libgpiod
Expression error: Unexpected >= operator.
libgpiod via command line
libgpiod provides command line utilities for GPIO:
gpiodetect | List all gpiochips present on the system, their names, labels, and the number of GPIO lines. |
gpioinfo | List all lines of specified gpiochips, their names, consumers, direction, active state, and additional flags. |
gpioget | Read the values of the specified GPIO lines (not valid if the line is already requested). The line will be then configured as an input. |
gpioset | Set the values of the specified GPIO lines, potentially keeping the lines exported, and wait until timeout, user input, or signal (not valid if the line is already requested). The line will be then configured as output. |
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. |
i.MX GPIOs are organized in banks of 32 pins. Each bank corresponds to a character device /dev/gpiochip<bank index>
. The gpiodetect
utility can be used to inspect the available gpiochip character devices:
# gpiodetect gpiochip0 [30200000.gpio] (32 lines) gpiochip1 [30210000.gpio] (32 lines) ...
The gpioinfo
utility can be used to inspect the lines for a given gpiochip:
# gpioinfo gpiochip0 gpiochip0 - 32 lines: line 0: unnamed "spi_imx" output active-high [used] line 1: unnamed unused input active-high line 2: unnamed unused input active-high ...
The gpioset
and gpioget
utilities can be used to manipulate GPIO from the command line.
For example, assuming GPIO4_21 is configured as a GPIO in your device tree:
Set GPIO4_21 high:
gpioset gpiochip3 21=1
Set GPIO4_21 low:
gpioset gpiochip3 21=0
Read GPIO4_21:
gpioget gpiochip3 21GPIO_SYSFS_DRAFT
The gpiomon
utility is useful for polling the lines to expect incoming input events.
For example, wait for three rising edge events on a given GPIO line:
gpiomon --num-events=3 --rising-edge gpiochip3 21 event: RISING EDGE offset: 3 timestamp: [ 1151.814356387] event: RISING EDGE offset: 3 timestamp: [ 1151.815449803] event: RISING EDGE offset: 3 timestamp: [ 1152.091556803]
Porting scripts using sysfs GPIO interface to libgpiod
Migrating from sysfs GPIO interface to libgpiod can be accomplished using the libgpiod package's command-line tools.
For bash scripts, instead of writing or reading from /sys/class/gpio, you'll use these tools.
Here are a few examples of how you'd read and write in a GPIO using sysfs interface and the equivalent operations through libgpiod:
GPIOs in i.MX SoCs are grouped in groups of 32 pins. That is relevant when converting the GPIO absolute number used by sysfs GPIO interface.
For example:
GPIO 17 belongs to gpiochip0, pin 17: 32 * 0 + 17 equals 17
.
GPIO 83 belongs to gpiochip2, pin 19: 32 * 2 + 19 equals 83
.
GPIO 149 belongs to gpiochip4, pin 21: 32 * 4 + 21 equals 149
.
# writing in a GPIO using sysfs GPIO interface echo 149 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio149/direction echo 1 > /sys/class/gpio/gpio149/value # writing in a GPIO using libgpiod gpioset gpiochip4 21=1
# reading a GPIO using sysfs GPIO interface echo 149 > /sys/class/gpio/export echo in > /sys/class/gpio/gpio149/direction cat /sys/class/gpio/gpio149/value # reading a GPIO using libgpiod gpioget gpiochip4 21
Remember, it is also important to check the return status of these commands in your scripts to ensure they are working correctly. If a command fails, it will return a non-zero status which can be checked in the $? variable, this variable holds the exit status of the last command that was executed. For example:
gpioget gpiochip4 21 if [ $? -ne 0 ]; then echo "Failed to get GPIO value" exit 1 fi
libgpiod C Application
libgpiod provides bindings for C/C++ applications. C++ examples are available in the libgpiod /tree/bindings/cxx/examples directory.
Below is a simple C application demonstrating how to use the bindings with GPIO4_IO21:
Makefile:
all: main.cpp $(CC) $(CCFLAGS) -Og -lgpiod main.c -g -o hello.bin clean: rm -f hello.bin
main.c
#include <gpiod.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #define CONSUMER "Variscite Demo" int main(int argc, char **argv) { unsigned int i, ret, val; struct gpiod_chip *chip; struct gpiod_line *line; const char * chipname = "gpiochip3"; const unsigned int line_num = 21; chip = gpiod_chip_open_by_name(chipname); if (!chip) { perror("Open chip failed\n"); goto end; } line = gpiod_chip_get_line(chip, line_num); if (!line) { perror("Get line failed\n"); goto close_chip; } ret = gpiod_line_request_output(line, CONSUMER, 0); if (ret < 0) { perror("Request line as output failed\n"); goto release_line; } /* Blink 5 times */ val = 0; for (i = 0; i < 5; i++) { ret = gpiod_line_set_value(line, val); if (ret < 0) { perror("Set line output failed\n"); goto release_line; } printf("Output %u on line #%u\n", val, line_num); sleep(1); val = !val; } release_line: gpiod_line_release(line); close_chip: gpiod_chip_close(chip); end: return 0; }
libgpiod Python Application
libgpiod provides bindings for python applications:
# pip3 install gpiod
Python examples are available in the libgpiod /tree/bindings/python/examples directory.