How to access GPIO on Android

您好,关于gpio的number号,是如何计算的?为什么GPIOH5: 176

Question:

I understand how to get to root with su for the GPIO.

For the other options, how do you do it ?

2 Let the 3rd application as system process

Is this one just putting the app in priv-app in the rom ?

3 Open the permission for 3rd application on system

This one i don’t know how. Can you point me in the right direction ?

Thanks

You also need a system signature for the app.

You need to modify source code of the direction system/sepolicy

1 Like

Thank you, I will try the 2nd way.

Hello Terry ,
My understanding is that the gpio path in linux systems is “/sys/class/gpio/”.

I would like to know is it the same for even android systems?

Yes, it’s the same. :grinning:

Thank you very much Terry.

On the vim1 board, can I only control GPIO GPIOH5 and GPIOAO6?
I can not control GPIOH_6, there is always 1.5 volt power supply

Yes, you are right.

By default, the GPIOH_6 is for I2S. And you can’t control the GPIO.
You need to modify the source code if you want to control the GPIOH_6.

Hi @Terry in VIM 1 :
i change the
led_gpio = <&gpio_ao GPIOAO_9 GPIO_ACTIVE_HIGH>;
to
led_gpio = <&gpio GPIODV_24 GPIO_ACTIVE_HIGH>;

sysled {
compatible = “amlogic, sysled”;
dev_name = “sysled”;
status = “okay”;
led_gpio = <&gpio GPIODV_24 GPIO_ACTIVE_HIGH>;
led_active_low = <1>;
};

and do the command under su :
echo 1 > /sys/class/leds/sys_led/brightness

but the sys_led can’t controll

I check, cat /sys/kernel/debug/pinctrl/pinctrl@4b0/pinmux-pins

pin 73 (GPIODV_24): c1108500.i2c (GPIO UNCLAIMED) function i2c_a group i2c_sda_a

GPIODV_24 used in i2c_sda_a

what’s the version of your system? Android Pie or Android Nougat?

hi @Terry in Android pie. thank you

Why did you want to change the led from GPIOAO_9 to GPIODV_24? Did you make a new PCB?

i want to use GPIODV_24 to control LED,
but after boot logo LED change to ACTIVE_LOW

from schematic GPIODV_24 used : (UART_TX_B // I2C_SDA_A // DMIC_IN)

How to disable UART_TX_B / I2C_SDA_A / DMIC_IN @Terry?

@Terry @goenjoy
I have try access GPIOH_5 from Android application as your post above (my pin no. is 422), but it doesn’t work.
It’s worked only when I try in CLI.

$ adb shell
1|kvim:/ # su
kvim:/ # echo 422 > /sys/class/gpio/export
kvim:/ # ls -a /sys/class/gpio/
. .. export gpio422 gpiochip401 gpiochip501 unexport 

After I run echo 422 > /sys/class/gpio/export VIM OS will generate file /sys/class/gpio/gpio422
But if I run from Android application /sys/class/gpio/gpio422 is not generated.

Could you please help.
Thank you

PS:

  1. Android image : VIM1_Pie_V211022
  2. I get GPIOH_5 pin number from this instructions https://khadas.github.io/android/vim1/AccessGpio.html
cat /sys/kernel/debug/pinctrl/pinctrl@4b0/gpio-ranges 
0: periphs-banks GPIOS [401 - 500] PINS [0 - 99]
cat /sys/kernel/debug/pinctrl/pinctrl@4b0/pins 
pin 0 (GPIOZ_0)  pinctrl@4b0
pin 1 (GPIOZ_1)  pinctrl@4b0
pin 2 (GPIOZ_2)  pinctrl@4b0
pin 3 (GPIOZ_3)  pinctrl@4b0
...
pin 20 (GPIOH_4)  pinctrl@4b0
pin 21 (GPIOH_5)  pinctrl@4b0
...

GPIOH_5 = bank + pin = 401+21 = 422

  1. My android app source code (Kotlin)
val pin = 422
val mProcess: Process = Runtime.getRuntime().exec("su")
val os = DataOutputStream(mProcess.outputStream)
//Request the gpio(GPIOH5)
os.writeBytes("echo $pin > /sys/class/gpio/export\n")
//Config the gpio(GPIOH5) as output
os.writeBytes("echo out > /sys/class/gpio/gpio$pin/direction\n")
// Send as high level output
os.writeBytes("echo 1 > /sys/class/gpio/gpio$pin/value\n")

Hi,@pinij. You should add “os.flush()” to flush the outPutStream after you call “os.writeBytes(str)”.Please have a try.

example:
os.writeBytes(str1);
os.flush();
os.writeBytes(str2);
os.flush();
2 Likes

Blockquote
Hi,@pinij. You should add “os.flush()” to flush the outPutStream after you call “os.writeBytes(str)”.Please have a try.

Yes, it works!!!
Thank you @PeterLin, you save my life.

Did you find out how to disable them on Android/AOSP?