Skip to main content

GPIO

Many of the available GPIO pins are available on the expansion connectors. Check exactly which pins are available on the board used, because they all differ in number of available GPIO pins.

Note that a pin must be declared as a GPIO before it can be used and manipulated as a GPIO. Error messages are not always given if a declaration is missing in the device tree file.

Also note that if a pin is already declared and used in the device tree file, then it is not possible to use it as a general GPIO.

U-Boot

The U-Boot has a command to interact with gpio pins:

gpio
gpio <input|set|clear|toggle> <pin>
- input/set/clear/toggle the specified pin
gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs

Using the gpio status command:

gpio status
Bank gpio@21_:
gpio@21_0: output: 1 [x] M2E_WL_REG_ON
gpio@21_3: output: 1 [x] M2E_BT_REG_ON
gpio@21_7: output: 1 [x] M2E_PERST
gpio@21_10: output: 1 [x] M2E_PWR_EN
gpio@21_15: output: 1 [x] M2M_PERST
gpio@21_17: output: 1 [x] M2BM_PWR_EN

Bank gpio-21:
gpio-216: func M1_PCIE_RST_N_1V8
gpio-217: func M1_PWR_EN_1V8

This example is for toggling the BLUE RGB LED on the SOM Carrier Board. Looking at the schematics, page 5, it is available as pin I2C_GPIO1_IO2_0-RGB_LED_BLUE on the GPIO expander at address 0x23.

If we list the status of all pins.

gpio status -a
...
Bank gpio@23_: <-- the gpio@23_ matches the i2c address 0x23
gpio@23_0: input: 0 [ ]
gpio@23_1: input: 0 [ ]
gpio@23_2: input: 0 [ ]
gpio@23_3: input: 0 [ ]
gpio@23_4: input: 0 [ ]
gpio@23_5: input: 0 [ ]
gpio@23_6: input: 0 [ ]
gpio@23_7: input: 0 [ ]
gpio@23_8: input: 0 [ ]
gpio@23_9: input: 0 [ ]
gpio@23_10: input: 1 [ ]
gpio@23_11: input: 0 [ ]
gpio@23_12: input: 0 [ ]
gpio@23_13: input: 0 [ ]
gpio@23_14: input: 0 [ ]
gpio@23_15: input: 1 [ ]
gpio@23_16: input: 1 [ ] <-- this is the one we are looking for
gpio@23_17: input: 0 [ ]
gpio@23_18: input: 1 [ ]
gpio@23_19: input: 1 [ ]
gpio@23_20: input: 1 [ ]
gpio@23_21: input: 1 [ ]
gpio@23_22: input: 1 [ ]
gpio@23_23: input: 1 [ ]
...

To toggle the pin:

gpio toggle gpio@23_16
gpio: pin gpio@23_16 (gpio 40) value is 0

Each time toggle is called the pin will change value and the LED will turn off (if value is 1) or on (if value is 0)

Linux - libgpiod

The libgpiod project is a C library and tools for interacting with GPIO character devices. It has been added to ea-image-base builds from 2021-01-22.

The gpiodetect command can be used to list all GPIO chips that are available. In the example below GPIO chip 0 is built-in to the processor while GPIO chips 1 and 2 are a GPIO expanders attached to the I2C bus on SOM Carrier Board. GPIO chip 2 is a GPIO expander mounted on the RZ/G3E SOM.

gpiodetect
gpiochip0 [10410000.pinctrl] (232 lines)
gpiochip1 [8-0021] (24 lines)
gpiochip2 [8-0023] (24 lines)
gpiochip3 [8-0022] (24 lines)

The gpioinfo command can be used to get more information about a GPIO chip. The command will list all available GPIO lines (pins), the name of the line, how it is configured and if it is already used. In the example below info about GPIO chip 2 is listed.

gpioinfo -c 2
        line   0:       "CSI0_RST-DATA"         output
line 1: "CSI0_PWR-CLK" input
line 2: "CSI1_RST-DATA" input
line 3: "CSI1_PWR-CLK" input
...
line 15: "RGB_LED_RED" input
line 16: "RGB_LED_BLUE" input
line 17: "RGB_LED_GREEN" input
line 18: "USER_LED_RED" input
line 19: "USER_LED_GREEN" input
line 20: "USER_LED_YELLOW" input
line 21: "USER_BUTTON1" input
line 22: "USER_BUTTON2" input
line 23: "USER_BUTTON3" input

The gpioset command can be used to set a GPIO line. In the example below USER_LED_GREEN will turn on.

gpioset -c 2 USER_LED_GREEN=0

Note that the command does not return after setting the LED. To end it, use Ctrl-C.

To issue the command and immediately exit use this instead:

gpioset -c 2 -t 0 USER_LED_GREEN=0

The gpioget command can be used to read the value of a GPIO line. In the example below the value of USER_BUTTON1 will be read. Note that we use the numerical value of the pin here but we could just as well have used the pin name "USER_BUTTON1".

gpioget -c 2 21
"21"=active       <-- will say inactive if the button is pressed

More information about libgpiod can be found on this link: https://embeddedbits.org/new-linux-kernel-gpio-user-space-interface/