2. GPIO control¶
Video introduction to this chapter:

“29-GPIO usage”
https://www.bilibili.com/video/BV1uo4y1T7cR/
GPIO is the abbreviation of General Purpose I/O, that is, general-purpose input and output ports, which are simply MCU/CPU controllable pins. These pins usually have multiple functions, the most basic ones are high and low level input detection and output, and some pins are also bound to the on-chip peripherals of the master controller. Such as communication pins for serial port, I2C, network, and voltage detection.
Linux provides a GPIO subsystem driver framework, which can be used to flexibly control the GPIO on the board.
2.1. GPIO performance¶
Sink current and source current are configurable from 0-20mA
2.2. GPIO naming¶
The ID of Rockchip Pin is composed of controller (bank) + prot + index number (pin).
The number of controller and GPIO controller are the same
Ports are fixed A, B, C and D, each port has only 8 pin (a=0,b=1,c=2,d=3)
Index numbers are fixed at 0, 1, 2, 3, 4, 5, 6, 7
The rk356x has 5 GPIO controllers, and each controller can control 32 IOs. When used as a GPIO function, the port behavior is configured by the GPIO controller register.
GPIO1_A4 means the first group of controllers, the port number is A, and the index number is 4. The formula for calculating this pin number is : 32 x 1 + 0 x 8 + 4 = 36
2.3. Method 1: Use GPIO sysfs interface to control IO¶
2.3.1. Use the command line¶
In Linux, the most common way to read and write GPIO is to use GPIO sysfs interface, By operating export, unexport, gpio{N}/direction under the /sys/class/gpio directory, gpio{N} /value (replace {N} with the actual pin number) and other files, often appear in shell scripts. Starting from kernel 4.8, the support of libgpiod has been added; the original access method based on sysfs will be gradually abandoned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #All the following operations need to open the administrator permission to use
#Enable pin
echo 36 > /sys/class/gpio/export
#Set the pin to input mode
echo in > /sys/class/gpio/gpio36/direction
#Read the value of the pin
cat /sys/class/gpio/gpio36/value
#Set the pin to output mode
echo out > /sys/class/gpio/gpio36/direction
#Set the pin low
echo 0 > /sys/class/gpio/gpio36/value
#Set the pin high
echo 1 > /sys/class/gpio/gpio36/value
#Reset pin
echo 36 > /sys/class/gpio/unexport
|
2.4. Method 2: Use libgpiod to control IO¶
libgpiod is a character device interface, and GPIO access control is implemented by operating character device files (such as /dev/gpiodchip0 ), and provides some command tools, c library and python package through libgpiod.
To use libgpiod, the libgpiod library needs to be installed on the board.
1 2 3 4 5 | #Install libgpiod library and header files
sudo apt install libgpiod-dev vim
#Install the gpiod command-line tool
sudo apt install gpiod
|
2.4.1. Command line control¶
Commonly used command lines are as follows, you can use -h to view the instructions corresponding to the command (take GPIO1_A4 as an example)
Command |
Usage |
Example |
Illustrate |
---|---|---|---|
gpiodetect |
List all GPIO controllers |
gpiodetect(no parameters) |
List all GPIO controllers |
gpioinfo |
List the pins of the gpio controller |
gpioinfo 1 |
List the first group of controller pin groups |
gpioset |
Set gpio |
gpioset 1 4=0 |
Set the first group of controller number 4 pins to low |
gpioget |
Get gpio pin status |
gpioget 1 4 |
Get the pin status of the first group controller number 4 |
gpiomon |
Monitor the status of gpio |
gpiomon 1 4 |
Monitor the pin status of the first group controller number 4 |
重要
The ID of Rockchip Pin is composed of controller (bank) + port (port) + index number (pin). Among them, the port number and index number will be combined into a value and passed to gpiod. Not all pins can be controlled by libgpiod, such as led and some pins that have been used.