Search by Tags

Basic GPIO usage - Aster Carrier Board - Colibri T30

 

Your progress

 

Overview

In this lesson, you will learn the basics of GPIO usage on Linux, being able to read or write to a GPIO pin from command-line, as well as by developing a minimal C application.

In this lesson you will:

  • Understand how to translate the hardware pin names to the correspondent Linux sysfs interface numbers.
  • Assemble the additional hardware - an LED and a switch.
  • Use the Toradex GPIO Tool to validate the hardware setup.
  • Configure and use GPIO pins through the Linux sysfs.
  • Debug GPIO configuration.
  • Write minimal sample applications in C.

The information provided in this guide is based in Toradex's knowledge base article GPIO (Linux), as well as other knowledge sources such as kernel documentation and the Linux man-pages project.

Materials (Optional)

Since Aster Carrier Board doesn't have buttons and LEDs available for debbuging purposes, we present two alternatives to test GPIOs: the first connects two GPIOs configured as input and output respectively; whereas the second alternative uses an external button and LED to achieve a better user experience, however, the following items are required:

  • 1x LED
  • 1x Button
  • 1x Transistor BC548
  • 2x 2k2Ω Resistor
  • 1x 470Ω Resistor
  • 1x Breadboard
  • Jumper wires

Note: On this getting started we are going to use 2x Resistor 2k2Ω, but you can use anyone between 1kΩ and 10kΩ. For Resistor 470Ω, you can use anyone between 100Ω and 1kΩ. For the transistor BC548, you can use any switch component you want, as MOSFET, just change the circuit according to the component.

Step 1

To find out which GPIO number to use in the Linux sysfs interface, you have to know the correspondence between available pins in the carrier board, number of the correspondent pins on the SODIMM connector of the Colibri computer on module and number of the pins on Linux.

Download or open in a web browser the Colibri Evaluation Board and the Colibri T30 datasheets from the respective products pages of the developer website:

Aster Carrier Board datasheet

Colibri T30 datasheet

Step 2

For this introduction guide, some pins configured by default as GPIO in the Toradex BSP were chosen. The choice of pins was made based on their availability on all the carrier boards covered by the getting-started guide. This module will not go through the configuration of other pins as GPIO, although it is possible.

First of all, you need to find the correspondence between the SODIMM and the connectors exposed for the developer on the Aster Carrier Board. Consult the Aster Carrier Board datasheet and fill the table below based in the example provided:

Note: The notation CONNECTOR.PIN will be employed in this lesson, e.g. X12.5 means pin 5 of the X12 connector.

Note: For pin 85, please verify Parallel Camera Interface chapter in Aster's datasheet.

Aster Carrier Board (connector.pin) SODIMM pins
X20.37 79
85
97
98
101
103
133
Table 1 filled

Step 3

Have a look at the table available in the "List Functions" chapter of the Colibri T30 datasheet. It provides a list of most of the Tegra pins available on the SODIMM connector.

The SODIMM pins we are interested at are connected to the Tegra SoC and have names defined by the Tegra Pin Name function. Each pin is multiplexed to have a specific function - among them GPIO, therefore the GPIO function is the column that we are interested at.

Having a look at the GPIO Alphanumeric to GPIO Numeric Assignment article, the correspondence between GPIO and the Linux numeric representation of the GPIO pins is provided as a table. To find it from the GPIO it is possible to use the formula below:

GPIO-(Character).Digit
Linux numeric representation = 8 x (Character - 'A') + Digit

Either by consulting the table from the article pointed above or calculating it, the previous table with the correspondence between Aster Carrier Board pins and SODIMM pins can be extended to have the Tegra pin name (GPIO), formed by GPIO character and digit, as example GPIO-T.02, and the Linux numeric representation. Fill the table below based in the example provided:

Aster Carrier Board (connector.pin) SODIMM pins Tegra pin name (GPIO) Linux GPIO number
X20.37 79 GPIO-Y.05 197
X20.26 85
X20.13 97
X20.16 98
X20.22 101
X20.18 103
Table 2 filled

Step 4

Choose two of the GPIO pins from the list above to make a loopback test. This lesson will use the following pins (Linux GPIO number):

  • 201 as Input
  • 196 as Output

Use jumper wires to connect GPIO 201 to GPIO 196.

Step 5

The Toradex Linux pre-built image comes with a tool named Toradex GPIO tool meant for debugging pins configuration. It can also be used to determine the correspondences found in the previous step. We will use it to test the hardware connections.

Note: You need a display and a mouse connected to the system in order to use the GPIO tool. Please go to the beginning of the getting-started guide for more information about assembling the peripherals.

Run the GPIO tool from the target Linux desktop:


  • Starting the GPIO tool

    Starting the GPIO tool


  • GPIO tool initial screen

    GPIO tool initial screen

Step 6

Locate the pins 201 and 196 in the table. Right click the direction of each of them and configure pin 201 as INPUT and pin 196 as OUTPUT. See the changes reflected in the application.


  • Configuring SODIMM pin

    Configuring SODIMM pin

Step 7

Click the Logic checkbox of pin 196 and see the logic checkbox of pin 201 switch on/off.


  • Logic checkbox toggled from GPIO tool

    Logic checkbox toggled from GPIO tool

Step 8

The Linux sysfs interface provides an abstraction to access the GPIO, as well as many other hardware features, from the Linux user-space.

The pin has to be exported first, which guarantees that it is not being used by other kernel drivers nor allow other drivers to use it. It also has to be configured as input or output.

From the Linux terminal, export the pins 201 and 196:

echo 201 > /sys/class/gpio/export
echo 196 > /sys/class/gpio/export

Configure the pins as input and output, respectively:

echo "in" > /sys/class/gpio/gpio201/direction
echo "out" > /sys/class/gpio/gpio196/direction

Step 9

Read the INPUT value as you toggle OUTPUT value:

echo 1 > /sys/class/gpio/gpio196/value
cat /sys/class/gpio/gpio201/value
echo 0 > /sys/class/gpio/gpio196/value
cat /sys/class/gpio/gpio201/value

Steps 10 to 17 (Optional)

The following steps are meant for readers that have the optional items listed in the beginning of this lesson. If you want to go through them, click the dropdown link below:

[COLLAPSE=Steps 10 to 17]

Step 10

The steps below will use the following pins (Linux GPIO number) to toggle an LED and read the value of a switch:

  • 201 for SW
  • 196 for LED

Use jumper wires to connect GPIO 196 to 2k2Ω resistor, connect the resistor to transistor base pin, connect pin X16.12 (5V) from iris to 470Ω resistor, connect the resistor to one LED and connect to transistor collector pin, connect the transistor emitter pin to pin X16.2 (GND) on Iris Carrier Board. Use jumper wires to connect pin X16.33 (3,3V) to 2k2Ω resistor, connect the resistor to one switch and to GPIO 201, and connect the node to pin X16.21 (GND) on Iris Carrier Board.


  • Connecting the SODIMM pins to the LED and switch

    Connecting the SODIMM pins to the LED and switch


  • Breadboard connection

    Breadboard connection


  • Schematic

    Schematic

Step 11

Read the switch value as you toggle it:

cat /sys/class/gpio/gpio201/value

Note: If the module was rebooted the GPIO must be configured again as explained in Step 8.

Step 12

Toggle the LED GPIO:

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

Note: If the module was rebooted the GPIO must be configured again as explained in Step 8.

Step 13

There is a debug interface provided by the kernel debugfs for GPIO, which holds information about GPIO pins already reserved for drivers, as well as pin configuration and state. See the example below for the Colibri T30, and try it yourself:

root@colibri-t30:~# cat /sys/kernel/debug/gpio
GPIOs 0-255, tegra-gpio:
gpio-10 (SODIMM pin 154 ) in lo
gpio-17 (SODIMM pin 81 ) in lo
gpio-23 (sdhci_cd ) in lo
gpio-26 (THERMD_ALERT ) in hi
gpio-29 (sysfs ) out hi
gpio-67 (SODIMM pin 130 ) in lo
gpio-70 (SODIMM pin 132 ) in lo
gpio-85 (USBC_DET ) in lo
gpio-86 (KEY_MENU ) in hi
gpio-90 (sysfs ) out lo
gpio-91 (sysfs ) out hi
gpio-94 (sysfs ) out hi
gpio-110 (SODIMM pin 162 ) in lo
gpio-111 (hdmi_hpd ) in lo
gpio-144 (SODIMM pin 73 ) in lo
gpio-153 (EN_MIC_GND ) out hi
gpio-157 (KEY_BACK ) in hi
gpio-158 (KEY_HOME ) in hi
gpio-168 (TOUCH_PEN_INT ) in hi
gpio-169 (KEY_POWER ) in lo
gpio-170 (BL_ON ) out hi
gpio-171 (SODI-85, Iris X16-18) out lo
gpio-178 (usb_host_vbus ) out lo
gpio-181 (SODIMM pin 75 ) in lo
gpio-188 (SODIMM pin 134 ) in lo
gpio-190 (102, I X13 ForceOFF#) in lo
gpio-191 (104, I X14 ForceOFF#) in lo
gpio-196 (sysfs ) out lo
gpio-197 (sysfs ) out lo
gpio-198 (sysfs ) out lo
gpio-199 (sysfs ) out hi
gpio-201 (sysfs ) out lo
gpio-220 (SODIMM pin 166 ) in lo
gpio-221 (SODIMM pin 168 ) in lo
gpio-222 (SODIMM pin 170 ) in lo
gpio-223 (SODIMM pin 172 ) in lo
gpio-226 (KEY_FIND ) in lo
gpio-230 (KEY_VOLUMEDOWN ) in hi
gpio-231 (SODIMM pin 94 ) in lo
gpio-232 (LAN_RESET ) out hi
gpio-234 (LAN_V_BUS ) out hi
gpio-237 (SODIMM pin 69 ) in hi
gpio-238 (SODIMM pin 65 ) in hi
gpio-239 (KEY_VOLUMEUP ) in hi
GPIOs 256-264, i2c/4-002d, tps6591x, can sleep:
gpio-262 (fixed_reg_en_hdmi ) out lo

See that the pins 201 and 196, configured as input and output in the previous steps, are the only ones taken by sysfs and are correctly configured as in and out respectively.

Export, unexport, configure and toggle the GPIO pins as you read the debugfs information to see the changes.

Note: If you want additional pin configuration debug, explore the */sys/kernel/debug/tegra_pinctrl/ directory.

Step 14

Write a small C application that toggles the LED GPIO. A small source-code is given below for reference and you may copy-paste it to your previously configured Eclipse environment.

Warning: The source-codes provided in this guide are distributed under the 3-clause BSD license terms. See below:

License
Source code 1

Step 15

Write a small C application that reads the switch GPIO. A small source-code is given below for reference and you may copy-paste it to your previously configured Eclipse environment.

Source code 2

Step 16

The GPIO sysfs interface enables the use of interrupts from user space, as long as it is supported by the underlying hardware. Read the sysfs GPIO documentation, have a look at the poll system call and try to implement an application that handles the interrupt. Have a look at the source code below for reference:

Source code 3

To prevent the main loop from blocking, run the GPIO IRQ handling in a separate thread. Try to implement it.

Step 17

Improve the code from the previous steps to handle errors and add other functionality you want. The code below implements a frequency meter by measuring the elapsed time between GPIO interruptions:

Source code 4
[/COLLAPSE]

FAQ

This lesson only covers the basics of GPIO usage on Linux. Since there are other important topics that were not discussed, this FAQ section is meant as an information complement.

What is the sysfs interface
Where can I find more information regarding GPIO in the Toradex documentation
Should I always use the sysfs interface when I need to use a GPIO
How can I use pins that are not configured as GPIO by default
How can I use more GPIOs than available for a specific module
I dont want to reconfigure pins as GPIO due to Toradex pin compatibility How can I work around this
Linux has a kernel driver for controlling LEDs How to use it
Can I control backlight brightness using GPIO
Is it possible to use a GPIO to shutdown the system
Is it possible to use a GPIO to suspend resume the system
Can I toggle a GPIO earlier than Linux boots
Can I bypass sysfs to have direct access to GPIO
What is the initial state of a GPIO pin
How to set the initial state of a GPIO pin
What happens when a GPIO pin is unexported