这篇文章主要介绍如何在Android系统下控制GPIO。
获取GPIO数值
想要控制GPIO,必须知道其对应的GPIO数值。
计算方法
GPIO数值计算方法为:Number = Range Base + Pin Index
。
Range Base
表示Range基数。Pin Index
表示GPIO管脚相对于Range的偏移。
GPIO数值计算举例
- 获取
Range Base
:
1 | # cat /sys/kernel/debug/pinctrl/fe000000.apb4\:pinctrl\@4000-pinctrl-meson/gpio-ranges |
- 获取
Pin Index
:
1 | # cat /sys/kernel/debug/pinctrl/fe000000.apb4\:pinctrl\@4000-pinctrl-meson/pins |
- 获取GPIO数值:
以GPIOT_19
为例
GPIOT_19
= Range Base
+ Pin Index
= 355 + 110 = 465
GPIO用法
在获取到GPIO数值后就可以通过如下的步骤来控制GPIO,以GPIOT_19(Number 465)为例:
ADB命令使用方法
申请GPIO(
GPIOT_19
)1
# echo 465 > /sys/class/gpio/export
配置GPIO(
GPIOT_19
)为输出模式1
# echo out > /sys/class/gpio/gpio465/direction
设置GPIO(
GPIOT_19
)输出为高电平1
# echo 1 > /sys/class/gpio/gpio465/value
设置GPIO(
GPIOT_19
)输出为低电平1
# echo 0 > /sys/class/gpio/gpio465/value
配置GPIO(
GPIOT_19
)为输入模式1
# echo in > /sys/class/gpio/gpio465/direction
读取GPIO(
GPIOT_19
)电平1
# cat /sys/class/gpio/gpio465/value
释放GPIO(
GPIOT_19
)1
# echo 465 > /sys/class/gpio/unexport
JAVA应用使用方法
获取root权限
1
Process mProcess = Runtime.getRuntime().exec("su");
申请GPIO(
GPIOT_19
)1
2DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes("echo " + 465 + " > /sys/class/gpio/export\n");配置GPIO(
GPIOT_19
)为输出模式1
os.writeBytes("echo out > /sys/class/gpio/gpio" + 465 + "/direction\n");
设置GPIO(
GPIOT_19
)输出为高电平1
os.writeBytes("echo 1 > /sys/class/gpio/gpio" + 465 + "/value\n");
配置GPIO(
GPIOT_19
)为输入模式1
os.writeBytes("echo in > /sys/class/gpio/gpio" + 465 + "/direction\n");
读取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());
}释放GPIO(
GPIOT_19
)1
os.writeBytes("echo " + 465 + " > /sys/class/gpio/unexport\n");