Single Pin GPIO (General Purpose Input Output)
A single pin GPIO has only two states, 0
and 1
, which can be either driven by the computer associated with the GPIO pin, or driven by an external device connected to the pin. The timing of driving changes between the two states can be arbitary up to the resolution of the microcontroller that controls the GPIO.
The gpiod
library
For interfacing with the GPIO pins, we use the library libgpiod
, which stands for:
lib
_rary for g
_eneral p
_urpose i
_nput/o
_utput d
_evices
For an Ubuntu operating system, this library can be is installed via:
sudo apt install gpiod
The most official page of the libgpiod
library can be found here:
The following provide some helpful discussion about libgpiod
:
Recorded presentation: https://youtu.be/BK6gOLVRKuU
Other libraries considered were sysfs
, WiringPi
, and jetson-gpio
:
Using gpiod
in C++ code
The following two websites both provide the most comprehensive list of all the C++ functions offered by the gpiod
library for monitoring a GPIO pin:
The following compiler flag is necessary for any code using the gpiod
library functions:
-lgpiodcxx
for C++ code
-lgpiod
for C code
Command line tools from gpiod
The command line tools provided by libgpiod
are:
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
For the Jetson TX2 Developer Kit, the command:
sudo gpiodetect
should display the following:
gpiochip0 [tegra-gpio] (192 lines)
gpiochip1 [tegra-gpio-aon] (64 lines)
gpiochip2 [tca9539] (16 lines)
gpiochip3 [tca9539] (16 lines)
gpiochip4 [max77620-gpio] (8 lines)
All the GPIO line of the J21 40-pin expansion header are connected into the tegra-gpio
, i.e., into gpiochip0
.
To display the details of the 192 tegra-gpio line, use either of the following commands:
`
sudo gpioinfo gpiochip0
sudo gpioinfo tegra-gpio
`
This should list most of the lines as unnamed unused input active-high.
Test that a GPIO input is working
The gpioget
and gpiomon
command line tools can be used to perform a quick test that a input to a GPIO pin is properly connected and functioning as expected.
To read the value, {0,1}
, from the GPIO pin, used the command:
sudo gpioget <gpio_chip_name> <line_number>
For example, if the input is connected to pin 18 of the J21 expansion header, then according to the table above pin 18 connects to line 161 of the tegra-gpio
chip. Hence you can read the value of pin 18 with the
sudo gpioget tegra-gpio 161
To monitor a line for multiple of a particular event, use the following:
sudo gpiomon --num-events=3 --rising-edge tegra-gpio 161