This guide is about how to access the GPIO using Android and Ubuntu.
Preconditions
The ROM must satisfy the following conditions:
* Android M >= V170603 |
How to Get the GPIO Number
You can get the GPIO number from GPIO Banks or Pins. Different versions of kernel will be different.
- Linux 3.14 (Android M, N and Ubuntu)
Banks:
# cat /sys/kernel/debug/pinctrl/c1109880.pinmux/gpio-ranges |
Pins:
# cat /sys/kernel/debug/pinctrl/c1109880.pinmux/pins |
For example, get the number of GPIOH_4
, GPIOH_5
and GPIOAO_6
.
Number(GPIOH_5) = bank + pin = 155 - 10 + 31 = 176 |
- Linux 4.9 (Android O and Ubuntu)
aobus-banks
:
Banks:
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff800014/gpio-ranges |
Pins:
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff800014/pins |
For example, get the number of GPIOAO_6
:
Number(GPIOAO_6) = bank + pin = 496 + 6 = 502
periphs-banks
:
Banks:
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff634480/gpio-ranges |
Pins:
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff634480/pins |
For example, get the number of GPIOH_5
:
Number(GPIOH_5) = bank + pin = 410 + 22 = 432
On Android
** GPIO List **
PIN GPIO Number |
There are two ways to access the GPIO:
- ADB Command
- Third-Party Applications
** ADB command **
Connect the VIMs with a wifi adb:
$ adb connect IP_ADDR |
Login to the VIMs:
$ adb shell |
Get root permision
$ su |
Request the gpio(GPIOH5)
$ echo 432 > /sys/class/gpio/export |
Config the gpio(GPIOH5) as output
$ echo out > /sys/class/gpio/gpio432/direction |
Config the gpio(GPIOH5) as high level output
$ echo 1 > /sys/class/gpio/gpio432/value |
Config the gpio(GPIOH5) as low level output
$ echo 0 > /sys/class/gpio/gpio432/value |
Config the gpio(GPIOH5) as input
$ echo in > /sys/class/gpio/gpio432/direction |
Get the gpio(GPIOH5) level
$ cat /sys/class/gpio/gpio432/value |
Release the gpio(GPIOH5)
$ echo 432 > /sys/class/gpio/unexport |
** Third-Party Applications **
Get root permision
Process mProcess = Runtime.getRuntime().exec("su"); |
Request the gpio(GPIOH5)
DataOutputStream os = new DataOutputStream(mProcess.getOutputStream()); |
Config the gpio(GPIOH5) as high level output
os.writeBytes("echo out > /sys/class/gpio/gpio" + 432 + "/direction\n"); |
Config the gpio(GPIOH5) as input
os.writeBytes("echo in > /sys/class/gpio/gpio" + 432 + "/direction\n"); |
Get the gpio(GPIOH5) level
Runtime runtime = Runtime.getRuntime(); |
Release the gpio(GPIOH5)
os.writeBytes("echo " + 432 + " > /sys/class/gpio/unexport\n"); |
On Ubuntu
** GPIO List **
- Linux-3.14
PIN GPIO Number
PIN37 GPIOH5 176
PIN33 GPIOAO6 151 - Linux-4.9.40
PIN GPIO Number
PIN37 GPIOH5 432
PIN33 GPIOAO6 502 - How to access GPIO on Terminal **
[Example on linux-4.9]
Request the gpio(GPIOH5)
$ echo 432 > /sys/class/gpio/export |
Config the gpio(GPIOH5) as output
$ echo out > /sys/class/gpio/gpio432/direction |
Config the gpio(GPIOH5) as high level output
$ echo 1 > /sys/class/gpio/gpio432/value |
Config the gpio(GPIOH5) as low level output
$ echo 0 > /sys/class/gpio/gpio432/value |
Config the gpio(GPIOH5) as input
$ echo in > /sys/class/gpio/gpio432/direction |
Get the level of gpio(GPIOH5)
$ cat /sys/class/gpio/gpio432/value |
Release the gpio(GPIOH5)
$ echo 432 > /sys/class/gpio/unexport |