Template:GPIO LIBGPIOD DRAFT

From Variscite Wiki
Revision as of 15:06, 8 May 2023 by Alifer (talk | contribs)


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]

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.