Project

General

Profile

Example - User-level GPIO Access

Objective

The objective of this example is to show how to work with an unused GPIO with the user-level sysfs facilities available in Linux.

Prerequisites

Steps

Picking a gpio to use

The schematic shows a connector named P3 which contains a number of lines that are accessible. Most of these lines have a number of possible functions. The name on the line usually corresponds to the first function name in the list of possible functions. The actual function of the line is determined by how the pinmux is set for a particular pin.

The first line on the P3 connector is the line on pin 3 of the connector. It is labeled GPMC_AD0_BM0. The alternate PinMux functions include GPIO0_15. So let's use this line for this example.

Looking at the connector where GPMC_AD0_BM0 comes from, it can be seen that this line corresponds to the M25 pin which will be set in the pinmux.

On Page 14 of the schematic:

On Page 3 of the schematic:

Verifying that the pinmux is set correctly

Now that we know the pin (M25) and the desired function (GPIO0_15), we can look at the pinmux settings to see if the pinmux needs to be changed or not.

In the kernel code, in the file arch/arm4/boot/dts/ti/k3-am62x-mitysom-devkit.dts, we can search for M25 and see the following near line 442:

    main_gpio_p3_pins_default: gpio-p3-pins-default {
        pinctrl-single,pins = <
            /* P3 Connector */
            AM62X_IOPAD(0x003c, PIN_INPUT_PULLDOWN, 7) /* (M25) GPMC0_AD0.GPIO0_15 */
            AM62X_IOPAD(0x0040, PIN_INPUT_PULLDOWN, 7) /* (N23) GPMC0_AD1.GPIO0_16 */
            AM62X_IOPAD(0x0044, PIN_INPUT_PULLDOWN, 7) /* (N24) GPMC0_AD2.GPIO0_17 */
            AM62X_IOPAD(0x0048, PIN_INPUT_PULLDOWN, 7) /* (N25) GPMC0_AD3.GPIO0_18 */
            AM62X_IOPAD(0x004c, PIN_INPUT_PULLDOWN, 7) /* (P24) GPMC0_AD4.GPIO0_19 */

The comments indicate that the line is referred to as GPMC0_AD0 and is currently set for the function GPIO0_15. So the function is already set as desired.

If the function was set to something else, you can follow the instructions in https://support.criticallink.com/redmine/projects/mitysom_am62x/wiki/Linux_Kernel_Setup_Pinmux to set the line for GPIO0_15.

Determine which gpiochip contains the desired gpio

We need to determine the Linux GPIO number of the gpio we selected. (GPIO0_15) This will take a couple of steps.

The first step is to determine which gpiochip contains the gpio.

On the target console, type the following command:

gpioinfo

If you look for GPIO0_15, you will see that the gpio is line 15 in gpiochip2.

gpiochip2 - 92 lines:
        line   0:    "GPIO0_0"       unused   input  active-high
        line   1:    "GPIO0_1"       unused   input  active-high
        line   2:    "GPIO0_2"       unused   input  active-high
        line   3:    "GPIO0_3"       unused   input  active-high
        line   4:    "GPIO0_4"       unused   input  active-high
        line   5:    "GPIO0_5"       unused   input  active-high
        line   6:    "GPIO0_6"       unused   input  active-high
        line   7:    "GPIO0_7"       unused   input  active-high
        line   8:    "GPIO0_8"       unused   input  active-high
        line   9:    "GPIO0_9"       unused   input  active-high
        line  10:   "GPIO0_10"       unused   input  active-high
        line  11:   "GPIO0_11"       unused   input  active-high
        line  12:   "GPIO0_12"       unused   input  active-high
        line  13:   "GPIO0_13"       unused   input  active-high
        line  14:   "GPIO0_14"       unused   input  active-high
        line  15:   "GPIO0_15"       unused   input  active-high
        line  16:   "GPIO0_16"       unused   input  active-high
        line  17:   "GPIO0_17"       unused   input  active-high
        line  18:   "GPIO0_18"       unused   input  active-high

Get information about the gpiochip

Now we need a "label" for gpiochip2 so we can correlate it to information in the /sys/class/gpio directory.

Enter the following command:

root@mitysom-am62x:~# gpiodetect
gpiochip0 [omap-gpmc] (2 lines)
gpiochip1 [4201000.gpio] (24 lines)
gpiochip2 [600000.gpio] (92 lines)
gpiochip3 [601000.gpio] (52 lines)
gpiochip4 [tps65219-gpio] (3 lines)
gpiochip5 [1-0020] (8 lines)
root@mitysom-am62x:~#

The label for gpiochip2 is 600000.gpio.

Find the gpio in the /sys/class/gpio

To find the corresponding entry in /sys/class/gpio, we do the following:

root@mitysom-am62x:~# tail /sys/class/gpio/*/label
==> /sys/class/gpio/gpiochip331/label <==
1-0020

==> /sys/class/gpio/gpiochip339/label <==
tps65219-gpio

==> /sys/class/gpio/gpiochip342/label <==
601000.gpio

==> /sys/class/gpio/gpiochip394/label <==
600000.gpio

==> /sys/class/gpio/gpiochip486/label <==
4201000.gpio

==> /sys/class/gpio/gpiochip510/label <==
omap-gpmc
root@mitysom-am62x:~#

With this output, we see that gpiochip2 corresponds to gpiochip394 since it has the same label. This means that the gpios in gpiochip2 start with the gpio number base number 394

Get the gpio number

Since GPIO0_15 is the 15th line in gpiochip2 which has a base number of 394, then the gpio number for GPIO0_15 is simply 394 + 15 = 409

Export the gpio

The "sysfs" system has a mechanism to make an unused gpio accessible to user-level commands. The next few steps will walk you through the basics of this.

The first step is to "export" the gpio.

Enter the command:

echo 409 >/sys/class/gpio/export

After this is done, a new entry in /sys/class/gpio will appear for gpio409.

root@mitysom-am62x:~# ls /sys/class/gpio
export  gpio409  gpiochip331  gpiochip339  gpiochip342  gpiochip394  gpiochip486  gpiochip510  unexport
root@mitysom-am62x:~#

If you repeat the gpioinfo command from earlier, you will also see that the status of GPIO0_15 has changed to indicate that it is in use by "sysfs".

root@mitysom-am62x:~# gpioinfo | grep -w GPIO0_15
        line  15:   "GPIO0_15"      "sysfs"   input  active-high [used]
root@mitysom-am62x:~#

The status also indicates that the current direction of the gpio is "input".

Set the direction of the gpio

In order to test the gpio, we need the "direction" of the gpio to be an output.

echo out >/sys/class/gpio/gpio409/direction

Set the value of the gpio

To change the value of the gpio, try the following:

echo 0 >/sys/class/gpio/gpio409/value
echo 1 >/sys/class/gpio/gpio409/value

Verify the value of the gpio with a multimeter

A multimeter can be used to verify that the level on the gpio line is changing as the value is changing.

  • Stick a leader wire in hole 3 of the P3 connector. The odd number holes are on the outside of the connector so hole 3 is the hole next to hole 1.
  • Use a multimeter with the ground touching a ground test point on the development board (e.g. TP19) and the other connector on the lead wire.

Location of hole 3 on the P3 connector:

Multimeter showing level when value of gpio is 1:

Unexport the gpio

To release the gpio from being used by sysfs, enter the following command:

echo 409 >/sys/class/gpio/unexport

Summary

This example has shown how to:

  • Identify a gpio to use
  • Determine the linux gpio number for the gpio
  • Make the gpio available as part of the /sys/class/gpio directory
  • Use user-level commands to set the direction and value of the gpio
  • Remove the gpio from the /sys/class/gpio directory

Go to top
Add picture from clipboard (Maximum size: 1 GB)