GPIO LIBGPIOD DRAFT: Difference between revisions

From Variscite Wiki
(Created page with "<!-- Set release according to "release" parameter in URL and use RELEASE_SUMO_V1.0_DART-MX8M as default --> {{INIT_RELEASE_PARAM|RELEASE_SUMO_V1.0_DART-MX8M}}<!-- --> {{#lst:Y...")
 
(Blanked the page)
Tag: Blanking
 
Line 1: Line 1:
<!-- Set release according to "release" parameter in URL and use RELEASE_SUMO_V1.0_DART-MX8M as default
--> {{INIT_RELEASE_PARAM|RELEASE_SUMO_V1.0_DART-MX8M}}<!--
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:B2QT_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:Android_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:Debian_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#vardefine: SD_CD_GPIO | 44}} <!--
--> {{PageHeader|{{#var:HARDWARE_NAME}} GPIO}} {{DocImage|category1=Yocto|category2=Android}}[[Category:Debian]][[Category:{{#var:HARDWARE_NAME}}]] __toc__


= GPIO state =
The current state of the system's GPIOs can be obtained in user space, as shown in the following example:
{{#switch: {{#var:HARDWARE_NAME}} | DART-MX8M=
<pre>
# gpioinfo
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
gpio-8  (                    |eth_phy_pwr        ) out hi   
gpio-9  (                    |phy-reset          ) out hi   
gpio-10  (                    |connect            ) in  hi IRQ
gpiochip1: GPIOs 32-63, parent: platform/30210000.gpio, 30210000.gpio:
gpio-44  (                    |cd                  ) in  lo IRQ
gpio-51  (                    |VSD_3V3            ) out hi   
gpiochip2: GPIOs 64-95, parent: platform/30220000.gpio, 30220000.gpio:
gpiochip3: GPIOs 96-127, parent: platform/30230000.gpio, 30230000.gpio:
gpio-102 (                    |Back                ) in  hi IRQ
gpio-109 (                    |Home                ) in  hi IRQ
gpio-111 (                    |Down                ) in  hi IRQ
gpio-113 (                    |?                  ) out lo   
gpio-114 (                    |Up                  ) in  hi IRQ
</pre>
| DART-MX8M-MINI=
<pre>
# gpioinfo
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
gpio-7  (                    |eth_phy_pwr        ) out lo   
gpio-9  (                    |phy-reset          ) out hi   
gpiochip1: GPIOs 32-63, parent: platform/30210000.gpio, 30210000.gpio:
gpio-38  (                    |sysfs              ) out hi   
gpio-39  (                    |sysfs              ) out hi   
gpio-42  (                    |sysfs              ) out hi   
gpio-43  (                    |enable              ) out hi   
gpio-44  (                    |cd                  ) in  lo IRQ
gpio-51  (                    |VSD_3V3            ) out hi   
gpio-52  (                    |sysfs              ) out lo   
gpiochip2: GPIOs 64-95, parent: platform/30220000.gpio, 30220000.gpio:
gpiochip3: GPIOs 96-127, parent: platform/30230000.gpio, 30230000.gpio:
gpio-102 (                    |Back                ) in  hi IRQ
gpio-104 (                    |ov5640_mipi_pwdn    ) out lo   
gpio-109 (                    |Home                ) in  hi IRQ
gpio-111 (                    |Down                ) in  hi IRQ
gpio-113 (                    |eMMC                ) out lo   
gpio-114 (                    |Up                  ) in  hi IRQ
gpiochip4: GPIOs 128-159, parent: platform/30240000.gpio, 30240000.gpio:
gpio-133 (                    |sysfs              ) out lo   
gpio-137 (                    |spi_imx            ) in  lo   
gpio-156 (                    |ov5640_mipi_reset  ) out hi
</pre>
| VAR-SOM-MX8M-NANO=
<pre>
# gpioinfo
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
gpio-0  (                    |spi_imx            ) out hi   
gpio-9  (                    |phy-reset          ) out hi   
gpio-10  (                    |connect            ) in  hi IRQ
gpio-13  (                    |ov5640_mipi_reset  ) out hi   
gpio-14  (                    |spi_imx            ) in  lo   
gpiochip1: GPIOs 32-63, parent: platform/30210000.gpio, 30210000.gpio:
gpio-38  (                    |sysfs              ) out hi   
gpio-39  (                    |sysfs              ) out hi   
gpio-41  (                    |eth_phy_pwr        ) out hi   
gpio-42  (                    |sysfs              ) out hi   
gpio-43  (                    |enable              ) out hi   
gpio-44  (                    |cd                  ) in  lo IRQ
gpio-51  (                    |VSD_3V3            ) out hi   
gpio-52  (                    |sysfs              ) out hi   
gpiochip2: GPIOs 64-95, parent: platform/30220000.gpio, 30220000.gpio:
gpiochip3: GPIOs 96-127, parent: platform/30230000.gpio, 30230000.gpio:
gpiochip4: GPIOs 128-159, parent: platform/30240000.gpio, 30240000.gpio:
gpio-133 (                    |sysfs              ) out lo   
gpio-140 (                    |ov5640_mipi_pwdn    ) out lo   
gpiochip6: GPIOs 502-503, parent: spi/spi0.1, spi0.1, can sleep:
gpiochip5: GPIOs 504-511, parent: i2c/1-0020, pca9534, can sleep:
gpio-504 (                    |Heartbeat          ) out lo   
gpio-505 (                    |Back                ) in  hi IRQ
gpio-506 (                    |Home                ) in  hi IRQ
gpio-507 (                    |Menu                ) in  hi IRQ
gpio-508 (                    |usb3_sel            ) out hi   
gpio-509 (                    |enet_rst            ) out lo   
gpio-510 (                    |som_vsel            ) out lo   
gpio-511 (                    |enet_sel            ) out lo   
</pre>
| DART-MX8M-PLUS=
<pre>
# gpioinfo
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
gpio-5  (                    |hdmi_on            ) out lo
gpio-7  (                    |ads7846_pendown    ) in  hi IRQ
gpio-10  (                    |int                ) in  hi IRQ
gpio-12  (                    |spi_imx            ) out hi
gpiochip1: GPIOs 32-63, parent: platform/30210000.gpio, 30210000.gpio:
gpio-38  (                    |sysfs              ) out hi
gpio-39  (                    |sysfs              ) out hi
gpio-40  (                    |sysfs              ) out hi
gpio-41  (                    |sysfs              ) out lo
gpio-42  (                    |spi_imx            ) out hi
gpio-44  (                    |cd                  ) in  lo IRQ ACTIVE LOW
gpio-51  (                    |regulator-usdhc2-vmm) out hi
gpio-52  (                    |regulator-eqos-phy-p) out hi
gpiochip2: GPIOs 64-95, parent: platform/30220000.gpio, 30220000.gpio:
gpiochip3: GPIOs 96-127, parent: platform/30230000.gpio, 30230000.gpio:
gpio-101 (                    |reset              ) out hi ACTIVE LOW
gpio-102 (                    |Back                ) in  hi IRQ ACTIVE LOW
gpio-104 (                    |powerdown          ) out hi
gpio-105 (                    |powerdown          ) out hi
gpio-108 (                    |reset              ) out hi ACTIVE LOW
gpio-109 (                    |Home                ) in  hi IRQ ACTIVE LOW
gpio-111 (                    |Down                ) in  hi IRQ ACTIVE LOW
gpio-113 (                    |eMMC                ) out lo
gpio-114 (                    |Up                  ) in  hi IRQ ACTIVE LOW
gpiochip4: GPIOs 128-159, parent: platform/30240000.gpio, 30240000.gpio:
gpio-137 (                    |spi_imx            ) out hi
gpio-142 (                    |scl                ) out lo
gpio-143 (                    |sda                ) in  lo
gpio-144 (                    |scl                ) out lo
gpio-145 (                    |sda                ) in  lo
gpio-146 (                    |scl                ) out lo
gpio-147 (                    |sda                ) in  lo
gpio-148 (                    |scl                ) out lo
gpio-149 (                    |sda                ) in  lo
gpio-156 (                    |reset              ) out hi ACTIVE LOW
</pre>
| VAR-SOM-MX93=
<pre>
# gpioinfo
gpiochip3: GPIOs 0-31, parent: platform/47400080.gpio, 47400080.gpio:
gpio-0  (                    |spi0 CS0            ) out hi ACTIVE LOW
gpio-1  (                    |sda                ) in  lo
gpio-7  (                    |regulator-eqos-phy  ) out hi
gpiochip0: GPIOs 32-63, parent: platform/43810080.gpio, 43810080.gpio:
gpio-43  (                    |powerdown          ) out hi
gpio-44  (                    |spi1 CS0            ) out hi ACTIVE LOW
gpio-49  (                    |cam_buf_dis        ) in  lo
gpio-50  (                    |regulator-usdhc2    ) out hi
gpio-54  (                    |sda                ) in  lo
gpio-55  (                    |scl                ) out lo
gpio-60  (                    |sda                ) in  lo
gpio-61  (                    |scl                ) out lo
gpiochip1: GPIOs 64-95, parent: platform/43820080.gpio, 43820080.gpio:
gpio-64  (                    |cd                  ) in  lo IRQ ACTIVE LOW
gpio-71  (                    |regulator-usdhc3    ) out hi
gpio-91  (                    |reset              ) out hi ACTIVE LOW
gpiochip2: GPIOs 96-127, parent: platform/43830080.gpio, 43830080.gpio:
gpio-110 (                    |regulator-wl-on    ) out hi
gpiochip4: GPIOs 504-511, parent: i2c/4-0020, 4-0020, can sleep:
gpio-505 (                    |Back                ) in  hi IRQ ACTIVE LOW
gpio-506 (                    |Home                ) in  hi IRQ ACTIVE LOW
gpio-507 (                    |Menu                ) in  hi IRQ ACTIVE LOW
gpio-509 (                    |regulator-fec-phy-rs) out hi
gpio-510 (                    |regulator-fec-phy-vs) out hi
gpio-511 (                    |regulator-fec-phy  ) out hi
</pre>
{{#vardefine: SD_CD_GPIO | 64}} }}
Each GPIO is defined as in or out and the state is shown as lo or hi.<br>
For example, pin {{#var: SD_CD_GPIO}} is the SD card card-detect.
When an SD card is plugged in, the state will be:
gpio-{{#var: SD_CD_GPIO}}  (                    |cd                  ) in  lo IRQ
When the SD card is removed, the state will be:
gpio-{{#var: SD_CD_GPIO}}  (                    |cd                  ) in  hi IRQ
= Manipulating GPIO using libgpiod =
{{#vardefine:LIBGPIOD_URL|https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/}}<!--
-->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.
== libgpiod via command line ==
[{{#var:LIBGPIOD_URL}} libgpiod] provides command line utilities for GPIO:
{| class="wikitable"
|-
| '''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 the values of the specified GPIO lines (not valid if the line is already requested). The line will be then configured as 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 <code>/dev/gpiochip<bank index></code>. The <code>gpiodetect</code> utility can be used to inspect the available gpiochip character devices:
# gpiodetect
gpiochip0 [30200000.gpio] (32 lines)
gpiochip1 [30210000.gpio] (32 lines)
...
The <code>gpioinfo</code> 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 <code>gpioset</code> and <code>gpioget</code> utilities can be used to manipulate GPIO from the command line.<br>
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 21
The <code>gpiomon</code> utility is useful for polling the lines to expect incoming input events.<br>
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]
== libgpiod C Application ==
[{{#var:LIBGPIOD_URL}} libgpiod] provides bindings for C/C++ applications. C++ examples are available in the libgpiod [{{#var:LIBGPIOD_URL}}/tree/bindings/cxx/examples /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'''
<pre>
#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;
}
</pre>
== libgpiod Python Application ==
libgpiod provides bindings for python applications:
# pip3 install gpiod
Python examples are available in the libgpiod [{{#var:LIBGPIOD_URL}}/tree/bindings/python/examples /tree/bindings/python/examples ] directory.

Latest revision as of 09:00, 8 May 2023