AM33 GPIO

From Variscite Wiki
Revision as of 18:13, 29 September 2023 by Pierluigi (talk | contribs) (→‎libgpiod via command line)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Warning: This page is designed to be used with a 'release' URL parameter.

This page is using the default release RELEASE_SUMO_V1.0_DART-MX8M.
To view this page for a specific Variscite SoM and software release, please follow these steps:

  1. Visit variwiki.com
  2. Select your SoM
  3. Select the software release
DART-MX8M GPIO

GPIO state

The current state of the system's GPIOs can be obtained in user-mode, as shown in the following example:

# cat /sys/kernel/debug/gpio
gpiochip1: GPIOs 0-31, parent: platform/44e07000.gpio, gpio-0-31:

gpiochip3: GPIOs 32-63, parent: platform/4804c000.gpio, gpio-32-63:
 gpio-60  (                    |cd                  ) in  hi IRQ ACTIVE LOW

gpiochip4: GPIOs 64-95, parent: platform/481ac000.gpio, gpio-64-95:
 gpio-82  (                    |wp                  ) in  hi

gpiochip5: GPIOs 96-127, parent: platform/481ae000.gpio, gpio-96-127:
 gpio-100 (                    |button0             ) in  hi IRQ ACTIVE LOW
 gpio-105 (                    |enable              ) out hi
 gpio-117 (                    |fixedregulator1     ) out hi

gpiochip2: GPIOs 504-509, parent: platform/tps65910-gpio, tps65910, can sleep:

gpiochip0: GPIOs 510-511, parent: platform/50000000.gpmc, omap-gpmc:
 gpio-510 (                    |rb                  ) in  hi

Each GPIO is defined as in or out and the state is shown as lo or hi.
For example pin 304 is the Back button.
When the Back button is not pressed, the state will be:

gpio-100 (                    |button0             ) in  hi IRQ ACTIVE LOW

When the Back button is pressed, the state will be:

gpio-100 (                    |button0             ) in  lo IRQ ACTIVE LOW

Manipulating GPIO using libgpiod

libgpiod provides bindings and utilities for for manipulating GPIO via user space.

libgpiod via command line

[ libgpiod] provides command line utilities for GPIO:

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 values of specified GPIO lines
gpioset Set values of specified GPIO lines, potentially keep the lines exported and wait until timeout, user input or signal
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

The AM625 GPIOs exist in two domains, the MAIN domain and the MCU domain.

The MAIN domain includes two General Purpose Input/Output (GPIO) modules that provide dedicated general-purpose pins that can be configured as either inputs or outputs. The GPIO modules main features include:

  • Support of 9 banks x 16 GPIO pins
  • Support of up to 9 banks of interrupt capable GPIOs
  • Interrupts can be triggered by rising and/or falling edge, specified for each interrupt capable GPIO pin
  • Set/clear functionality per individual GPIO pin

The MCU domain includes one General Purpose Input/Output (MCU_GPIO) module provides dedicated general-purpose pins that can be configured as either inputs or outputs. the MCU_GPIO module includes thesemain features:

  • Support of 9 banks x 16 GPIO pins
  • Support of up to 9 banks of interrupt capable GPIOs
  • Interrupts can be triggered by rising and/or falling edge, specified for each interrupt capable GPIO pin
  • Set/clear functionality per individual GPIO pin

Each module corresponds to a character device /dev/gpiochip<bank index>. The gpiodetect utility can be used to inspect the available gpiochip character devices:

# gpiodetect
gpiochip0 [omap-gpmc] (2 lines)
gpiochip1 [gpio-0-31] (32 lines)
gpiochip2 [tps65910] (6 lines)
gpiochip3 [gpio-32-63] (32 lines)
gpiochip4 [gpio-64-95] (32 lines)
gpiochip5 [gpio-96-127] (32 lines)

These can be mapped to the device tree:

gpiochip devicetree address
gpiochip0 gpmc 0x50000000
gpiochip1 gpio0 0x44e07000
gpiochip2 tps65910-gpio
gpiochip3 gpio1 0x4804c000
gpiochip4 gpio2 0x481ac000
gpiochip5 gpio3 0x481ae000

The gpioinfo utility can be used to inspect the lines for a given gpiochip:

# gpioinfo gpiochip2
gpiochip2 - 87 lines:                                                                                                                                               
       line   0:      unnamed       unused   input  active-high 
       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 GPIO1_4 (gpiochip3 and main_gpio1 in the device tree) is configured as a GPIO in your device tree:

Set GPIO1_4 high:

gpioset gpiochip3 4=1

Set GPIO1_4 low:

gpioset gpiochip3 4=0

Read GPIO1_4:

gpioget gpiochip3 4

libgpiod C Application

[ libgpiod] provides bindings for C/C++ applications. C++ examples are available in the libgpiod [/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

#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 /tree/bindings/python/examples ] directory.

Using a C application

All of the command line operations above can be translated to C code:
Reserve (export) the GPIO:

#define IMX_GPIO_NR(port, index)    ((((port)-1)*32)+((index)&31))

int fd;
char buf[MAX_BUF]; 
int gpio = IMX_GPIO_NR(4, 21); /* Just an example */

fd = open("/sys/class/gpio/export", O_WRONLY);

sprintf(buf, "%d", gpio); 

write(fd, buf, strlen(buf));

close(fd);

Set the GPIO direction:

sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);

fd = open(buf, O_WRONLY);

/* Set out direction */
write(fd, "out", 3); 
/* Set in direction */
write(fd, "in", 2); 

close(fd);

In case of out direction set the GPIO value:

sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

fd = open(buf, O_WRONLY);

/* Set GPIO high status */
write(fd, "1", 1); 
/* Set GPIO low status */
write(fd, "0", 1); 

close(fd);

In case of in direction get the current GPIO value:

char value;

sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

fd = open(buf, O_RDONLY);

read(fd, &value, 1);

if (value == '0') { 
     /* Current GPIO status low */
} else {
     /* Current GPIO status high */
}

close(fd);

Once finished, free (unexport) the GPIO:

fd = open("/sys/class/gpio/unexport", O_WRONLY);

sprintf(buf, "%d", gpio);

write(fd, buf, strlen(buf));

close(fd);

Important notes:

  • Remember that after the first read operation the file pointer will move to the next position in the file, so to get a correct value for each read operation you simply have to set the file pointer at the beginning of the file before read by using the following command:
lseek(fd, 0, SEEK_SET);
  • This is only a short example. If you want to use it in your code remember add error handling to it.

Kernel Device Tree GPIO configuration

SysConfig Device Tree Generation

TI's SysConfig provides an intuitive graphical user interface for configuring pins, including pin mux and pad settings: https://www.ti.com/tool/SYSCONFIG.

As shown in the image below, SysConfig enables you to select the GPIO pin(s) for your design and will then generate the device tree code:

Ti-sysconfig-gpio-markup.png

Define a pin as GPIO in the kernel Device Tree

The device tree code generated by SysConfig can be copied directly to your device tree file. Generally, GPIO will be used by another device node and you will provide it the mygpio1_pins_default handle. However, if the GPIO is not used by another device and you wish to control it using libgpiod, you need to initialize the pin configuration like shown below:

&main_pmx0 {
	pinctrl-names = "default";
	pinctrl-0 = <&mygpio1_pins_default>;
	…
	mygpio1_pins_default: mygpio1-pins-default {
		pinctrl-single,pins = <
			AM62X_IOPAD(0x0188, PIN_INPUT, 7) /* (AB20) RGMII2_RD1.GPIO1_4 */
		>;
	};
	…
};