How to access GPIO on Android


#1

The post is about how to access GPIO on Android
Firstly, You need to flash the new rom(Vim_Nougat_Root_170421)
Please follow the below steps to access GPIO

Steps

####1). Need to know the GPIO port number

	 GPIOH5:     176
	 GPIOH6:     177
	 GPIOH7:     178
	 GPIOH8:     179
	 GPIOH9:     180
    GPIOAO6:     151

####2). How to access GPIO

  • Get the root permission

mProcess = Runtime.getRuntime().exec(“su”);

  • Request the GPIOH5

DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes(“echo " + 176 + " > /sys/class/gpio/export\n”);

  • Config the GPIOH5 as high level output

DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes(“echo out > /sys/class/gpio/gpio” + 176 + “/direction\n”);
os.writeBytes(“echo 1 > /sys/class/gpio/gpio” + 176 + “/value\n”);

  • Config the GPIOH5 as low level output

DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes(“echo out > /sys/class/gpio/gpio” + 176 + “/direction\n”);
os.writeBytes(“echo 0 > /sys/class/gpio/gpio” + 176 + “/value\n”);

  • Config the GPIOH5 as input

DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes(“echo in > /sys/class/gpio/gpio” + 176 + “/direction\n”);

  • Release the GPIOH5

DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes(“echo " + 176 + " > /sys/class/gpio/unexport\n”);

###Notice

It is necessary to write the node like ‘/sys/class/gpio/*’ when we access the GPIO on 3rd application , but it haven’t the permission to write the node. In order to solve this problem, we have several kinds of methods.

<1> Let the 3rd application get the root permission.
<2> Let the 3rd application as system process
<3> Open the permission for 3rd application on system

We only use the first methods to access the GPIO
We can discuss together If you want to know the another two methods.
About more information for it , you can download the source code of demo and to have a test.

This is the link for demo apk

How to download the source code of demo
$ git clone https://github.com/terry2droid/Wiringpi


#2

Hi, terry!

How to get access GPIO on sh script? I need to create the fan control output.

This script worked on radxa board.

#!/bin/sh

enable the gpio 176 -> fan output

echo 176 > /sys/class/gpio/export

set the direction to output

echo “out” > /sys/class/gpio/gpio176/direction
while true;
do
echo 0 > /sys/class/gpio/gpio176/value #fan on
sleep 1
echo 1 > /sys/class/gpio/gpio176/value #fan off
sleep 1
done


#3

I think it also work on VIM.
What’s the result? How did you connect the fan?


#4

Hello All,

I would like know, how I can control PWM_AO_A and PWM_F signals. In my android build I see GPIO number 145,155. The takeover controll echo 145 >/sys/class/gpio/export hangs somehow
gpio 155 looks in works, but don’t what kind of is. (using bash scripting under su)

note: I2S is enabled (acording info here), so GPIOH_x signals are gone. Any way how to get GPIOH_5 signal back? And keep I2S bus working


#5

Hi, TomasFilip:
Are you a developer?

You can try to set the register up to Amlogic S905X Datasheet.
If you has question about it , I will provide some advices for you.

You can copy your bash scripting into the path ‘/system/etc/init.d/’ when you on Vim_Marshmallow_Root_170603

It is impossible and only one function will be used on the same time.


#6

Hi Terry,

I’m believe I’m developer, but not very experienced with android build.

Questions are about the vHAT1 (ask Kenny or Guowa for schematic/board), I’m using GPIOH_5 as INT signals from devices, PWM_F is now general gpio and PWM_AO_A/PWM_D is used fan control.

so I’m guess I need update kvim.dts file for gpio except GPIOH_5, right?
So so far I have own android 7 build, where I can play with some settings. Any advice should be helpful.

any chance to get support of GPIOH_5 like split the port control, because has nothing with I2S bus, or?


How to request GPIOH_5 as INT mode
#7

Hello Terry,

So if I’m understood well, based on doc you provide me is possible only Have in one time of uboot

  1. HDMI Data/SCL/HPD
    or
  2. JTAG
    or
  3. I2S sound

so no posibility to have HDMI resolutions data + I2S together right ? (that’if is true, that is pitty… btw)
So this means, no posibility to detect a HDMI monitor, choose a right resolutions and have change to play music throught I2S, right?

anyway from that table is GPIOH_5 universal, without any functions so can be used as GPIO anytime, right ?


#8

The GPIOH_5 has nothing with HDMI and I2S. It can be only used as GPIO.


#9

yes, that what I’m about GPIOH_5 thought…
so I have enabled I2S under Android 7, how make GPIOH_5 accesible, can you please give me a advice?
Maybe just indications what I should focus should be for first run enough.


#10

Hi, terry.
I’m racking my brains over the clock on max7219. I would like to use a third-party library like this https://github.com/sharetop/max7219-java
After studying your example, I could not find a solution. Do I understand correctly: if I want to send 1 byte to my max7219 I should call setLevel method 8 times? Could you help me with advice?


#11

Yes, you are right.
But you need to connect your max7219 with three GPIO

  • DIN
  • CS
  • CLK

#12

Okay, I understood. So first of all I should implement driver which will know how and when send data to DIN, CS and CLK using exportGPIO and setLevel . Then I will able to call method like this led.setText(…).
All right?


#13

Yes, all right.
You can have a try. :smile:


#14

I am very sorry for such a late reply.
I will release the docs about how to request the gpio as interrupted function in next week.
Thanks.


#15

Could you provide a low level api? I mean native calls.


#16

You maybe need to have a try and google for it.
It is an example about how to control the serial by JNI
Thanks.


#17

Sorry for the silly questions. I have a little experience in the Android development.
Thanks for the advice. I’ll try in the coming days.


#18

A post was split to a new topic: How to request GPIOH_5 as INT mode


#19

The pinout which you list is not include the two leds,can you show me how to control the two leds by echo cmd please?


#20

The PWR_LED is controled by below chip and not CPU.


You can control the SYS_LED following below method

//IP_ADDR is the IP Adreess of your VIMs
# adb connect IP_ADDR
# adb shell 
//turn off led 
# echo off > /sys/class/leds/led-sys/trigger
//turn on led
# echo default-on > /sys/class/leds/led-sys/trigger