You can access the GPIO pins in two ways:
- ADB shell
- Java application
Get GPIO Number
To use a GPIO pin, you need to know its PIN number.
Calculation
GPIO PIN number calculation method: Number = Range Base + Pin Index
Range Base
refers to the base value of GPIO ranges.Pin Index
refers to the sorting of the GPIO pins you need to calculate in the corresponding ranges.
Example
- Get
Range Base
:
1 | # cat /sys/kernel/debug/pinctrl/fe000000.apb4\:pinctrl\@4000-pinctrl-meson/gpio-ranges |
- Get
Pin Index
:
1 | # cat /sys/kernel/debug/pinctrl/fe000000.apb4\:pinctrl\@4000-pinctrl-meson/pins |
- Calculate
Number
:
Here is an example for GPIOT_19
:
GPIOT_19 = Range Base + Pin Index = 355 + 110 = 465
GPIO Usage
When you get the GPIO number, you can follow the steps below to control it.
Here will take GPIO number 465 as a example.
ADB Shell Commands
Request GPIO(
GPIOT_19
)1
# echo 465 > /sys/class/gpio/export
Configure GPIO(
GPIOT_19
) as output mode1
# echo out > /sys/class/gpio/gpio465/direction
Set GPIO(
GPIOT_19
) to high level output1
# echo 1 > /sys/class/gpio/gpio465/value
Set GPIO(
GPIOT_19
) to low level output1
# echo 0 > /sys/class/gpio/gpio465/value
Configure GPIO(
GPIOT_19
) as input mode1
# echo in > /sys/class/gpio/gpio465/direction
Read the level of GPIO(
GPIOT_19
)1
# cat /sys/class/gpio/gpio465/value
Release GPIO(
GPIOT_19
)1
# echo 465 > /sys/class/gpio/unexport
JAVA Application
Get root privileges
1
Process mProcess = Runtime.getRuntime().exec("su");
Request GPIO(
GPIOT_19
)1
2DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes("echo " + 465 + " > /sys/class/gpio/export\n");Configure GPIO(
GPIOT_19
) as output mode1
os.writeBytes("echo out > /sys/class/gpio/gpio" + 465 + "/direction\n");
Set GPIO(
GPIOT_19
) to high level output1
os.writeBytes("echo 1 > /sys/class/gpio/gpio" + 465 + "/value\n");
Configure GPIO(
GPIOT_19
) as input mode1
os.writeBytes("echo in > /sys/class/gpio/gpio" + 465 + "/direction\n");
Read the level of GPIO(
GPIOT_19
)1
2
3
4
5
6
7
8
9Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cat " + "/sys/class/gpio/gpio" + 465 + "/value");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line ;
while (null != (line = br.readLine())) {
return Integer.parseInt(line.trim());
}Release GPIO(
GPIOT_19
)1
os.writeBytes("echo " + 465 + " > /sys/class/gpio/unexport\n");