2. GPIO control¶
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 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 rk3588 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.2. Use GPIO sysfs interface to control IO¶
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.
Pin |
Controller |
Port |
Index |
Result |
---|---|---|---|---|
GPIO1_C4 |
1 |
C |
4 |
52 (32 x 1 + 8 x 2 + 4) |
GPIO3_B2 |
3 |
B |
2 |
106 (32 x 3 + 8 x 1 + 2) |
GPIO0_D6 |
0 |
D |
6 |
30 (32 x 0 + 8 x 3 + 6) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #All the following operations require administrator rights to be used
#Enable pin GPIO1_C4
echo 52 > /sys/class/gpio/export
#Set the pin to input mode
echo in > /sys/class/gpio/gpio52/direction
#Read the value of the pin
cat /sys/class/gpio/gpio52/value
#Set the pin to output mode
echo out > /sys/class/gpio/gpio52/direction
#Set the pin low
echo 0 > /sys/class/gpio/gpio52/value
#Set the pin high
echo 1 > /sys/class/gpio/gpio52/value
#Reset pin
echo 52 > /sys/class/gpio/unexport
|
2.3. 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.
1 2 | #Install libgpiod library and header files
sudo apt install gpiod
|
The method of using the gpiod tool is different from that of the sysfs interface. gpiod is based on the controller, and then details the port number and index number. That is, gpiod uses two data to determine the pin.
Pin |
Controller |
Port |
Index |
Result |
---|---|---|---|---|
GPIO1_C4 |
1 |
C |
4 |
1 20(8 x 2 + 4) |
GPIO3_B2 |
3 |
B |
2 |
3 10(8 x 1 + 2) |
GPIO0_D6 |
0 |
D |
6 |
0 30(8 x 3 + 6) |
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 20=0 |
Set the first group of controller number 20 pins to low |
gpioget |
Get gpio pin status |
gpioget 1 20 |
Get the pin status of the first group controller number 20 |
gpiomon |
Monitor the status of gpio |
gpiomon 1 20 |
Monitor the pin status of the first group controller number 20 |
重要
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.
2.4. FAQs¶
- Q1:
gpioset: error setting the GPIO line values: Device or resource busy
or-bash: echo: write error: Device or resource busy
appears when using GPIO. A1: It means that the GPIO is occupied. The reason for the occupation may be that the pin is used as a gpio or other multiplexing function in the device tree.