GPIO access in Android via NDK


#1

Hi everyone!
I would like to write a Java-App that controls GPIO-pins (on the DragonBoard). First I tried to do it via Java directly (executing shell-commands and accessing the sysfs-files via the File libraries). It seems like the App never got enough permissions, so I tried it via the NDK and the libsoc library as described in http://www.96boards.org/blog/access-gpio-pins-programmatically/).
Unfortunately, this doesn’t work either, the open(path, flags) function with path="/sys/class/gpio/export" and flags=1052673 in file.c on line 9 got me fd=-1;

Could someone please help me with that?


GPIO access in Android Environment
#2

Hello Raphael, no need for Android NDK to change the GPIOs. First you need to give the permissions and then use the Android SDK. I used the library made by San Diego University net.calit2.mooc.iot_db410c:db410c_gpiolib
But easier, goto-> https://www.coursera.org/learn/internet-of-things-sensing-actuation/lecture/WxAPz/getting-ready-for-module-2
You will have a full tutorial on how to build a simple app to control the GPIOs and set the permission.
You should be able to see the video if you follow the link above.
Hope this will help.


Using GPIO in Android L
#3

Thank you for your response! The course looks exactly like what I’ve been searching for.
Unfortunately, it doesn’t work for me. I changed the bootscript as proposed, but after a reboot, the /sys/class/gpio folder wasn’t any different :frowning:

Do you have any idea what the problem might be?


#4

Mh?! Several possibilities, at least 2.

  1. You mistype some line of script.
  2. You did not change the script as you thought or did not upload it back correctly.

Before checking these 2 options check if you can change the GPIO by the terminal.
To help you can also use this Coursera video https://www.coursera.org/learn/internet-of-things-sensing-actuation/lecture/pKDxG/gpio-access-with-android.

If this works I would search in these 2 options above.

Let me know :slight_smile:


#5

It works now, but not with the code of the online course. I just used

chmod 222 /sys/class/gpio/export
chmod 222 /sys/class/gpio/unexport

echo 915 > /sys/class/gpio/export
chmod 777 /sys/class/gpio/gpio915
chmod 777 /sys/class/gpio/gpio915/value
chmod 777 /sys/class/gpio/gpio915/direction

My idea was to be able to export the pins I needed in java if I set the permissions of export, but that doesn’t work.
With this code I can only control pin 915.

The problem seems to be the loop. I tried to enter it in the console directly (in android studio) but it wouldn’t accept the curly brackets. It isn’t a problem of the keyboard, because it also rejected them when pasting the code into the console.
Anyway, I will just set the permissions of every pin I need manually for now.

Thank you for your help!


#6

Hi @fradera ,
I used your code in file init.qcom.post_boot.sh file and then pushed into /etc/init.qcom.post_boot.sh. After Reboot, when I checked /sys/class/gpio directory, it didnot show any exported gpio.
I did without reboot also but it shown same result.


#7

Hi @fradera ,
I have figured out the problem but I don’t know how to fix it. Actually init.qcom.post_boot.sh does not run after boot up.
Even if I append script with chmod 777 /sys/class/gpio/export it does not work. After bootup permissions remain same as before. Tell me any other script that runs after bootup.