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:
Since Iris 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:
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.
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 Iris Carrier Board and the Colibri iMX6ULL datasheets from the respective products pages of the developer website:
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 Iris Carrier Board. Consult the Iris 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.
|Iris Carrier Board (connector.pin)||SODIMM pins|
Have a look at the table available in the "List Functions" chapter of the Colibri iMX6ULL datasheet. It provides a list of most of the iMX6ULL pins available on the SODIMM connector.
The SODIMM pins we are interested at are connected to the iMX6ULL SoC and have names defined by the iMX6ULL Ball Name function. Each pin is multiplexed to have a specific function - among them GPIO, therefore the ALT5 function is the column that we are interested at.
Having a look at the GPIO Alphanumeric to GPIO Numeric Assignment article, the correspondence between ALT5 and the Linux numeric representation of the GPIO pins is provided as a table. To find it from the ALT5 it is possible to use the formula below:
PORTx[y]Linux numeric representation = [(x-1)*32]+y
Either by consulting the table from the article pointed above or calculating it, the previous table with the correspondence between Iris Carrier Board pins and SODIMM pins can be extended to have the iMX6ULL pin name (ALT5), formed by GPIO controller, as example GPIO07, plus pin at SoC level, as example IO09, and the Linux numeric representation. Fill the table below based in the example provided:
|Iris Carrier Board (connector.pin)||SODIMM pins||iMX6ULL name (ALT5)||Linux GPIO number|
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):
Use jumper wires to connect GPIO 123 to GPIO 122.
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:
Locate the pins 123 and 122 in the table. Right click the direction of each of them and configure pin 123 as INPUT and pin 122 as OUTPUT. See the changes reflected in the application.
Click the Logic checkbox of pin 122 and see the logic checkbox of pin 123 switch on/off.
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 123 and 122:
echo 123 > /sys/class/gpio/exportecho 122 > /sys/class/gpio/export
Configure the pins as input and output, respectively:
echo "in" > /sys/class/gpio/gpio123/directionecho "out" > /sys/class/gpio/gpio122/direction
Read the INPUT value as you toggle OUTPUT value:
echo 1 > /sys/class/gpio/gpio122/valuecat /sys/class/gpio/gpio123/valueecho 0 > /sys/class/gpio/gpio122/valuecat /sys/class/gpio/gpio123/value
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:
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.