Configuring GPIO Pin as Power On Feedback on VIM2 for Android

Hello Khadas Community,

I hope this message finds you well.

I am currently working on a project with the Khadas VIM2, and I need to configure a GPIO pin to serve as a power-on feedback signal, where the pin goes high when the board is powered on and low when it is off.

I am aware that this is possible, as I have successfully implemented this on a Linux environment in the past by modifying the Device Tree. However, I am now working with Android, and I need guidance on how to achieve the same functionality in this environment.

Could anyone provide insights or point me in the right direction on how to properly configure this for Android? Any help or reference to relevant documentation would be greatly appreciated.

Thank you in advance for your assistance.

Best regards!

Hello @Rafael_Morato_Mangua

@william.lin will help you later.

Hello, you can use GPOH_5 as a gpio to achieve your goals

Here are the methods for API calls, please refer to the following

Process mProcess = Runtime.getRuntime().exec("su");

apply for GPIO:
DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
os.writeBytes("echo " + 176 + " > /sys/class/gpio/export\n");

Set Output Mode:
os.writeBytes("echo out > /sys/class/gpio/gpio" + 176 + "/direction\n");

set up GPIO:
os.writeBytes("echo 1 > /sys/class/gpio/gpio" + 176 + "/value\n"); 

read GPIO:
Runtime runtime = Runtime.getRuntime(); 
Process process = runtime.exec("cat " + "/sys/class/gpio/gpio" + 176 + "/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()); 
} 

release GPIO:
os.writeBytes("echo " + 176 + " > /sys/class/gpio/unexport\n");

Thank you very much for your assistance; I appreciate your effort to help. However, the code provided feels like an isolated Java snippet without much context, which makes it difficult for me to proceed effectively.

If I understand correctly, you’re suggesting that this code be incorporated into an app or something similar. Unfortunately, I’m not experienced with Java programming, and I don’t see why you would assume that, as I never mentioned any intention to develop an app.

To clarify my goal: all I want is for the GPIO to go HIGH when Android starts. I’m not looking to develop a full-fledged app—just a simple solution. Perhaps this could be achieved by creating a service that runs on Android at startup or by executing some commands via ADB.

Any guidance on achieving this in a straightforward manner would be greatly appreciated.

The adb command is as follows

su

apply for GPIO:
echo 176 > /sys/class/gpio/export

Set Output Mode:
# echo out > /sys/class/gpio/gpio176/direction

set up GPIO:
echo 1 >  /sys/class/gpio/gpio176/value

read GPIO:
cat /sys/class/gpio/gpio176/value

release GPIO:
echo 176 > /sys/class/gpio/unexport

It seems like there’s been a misunderstanding, and unfortunately, you still haven’t grasped the purpose of my request. Perhaps I wasn’t clear enough, or maybe you didn’t fully read my original message.

The board is being used in a commercial application, and the purpose of this feature is to have the GPIO signal activate the peripherals of the equipment where the board is embedded. While the ADB commands you suggested do work, they cannot be executed manually every time the board is powered on. I’m not looking to develop an app or manually send commands via ADB each time. The solution needs to be automatic and enabled by default on the board.

Additionally, you’ve only repeated information that can already be found in the documentation, which isn’t helpful at all. I’m looking for a practical solution to automate this process. Is there any way to create a script or automate this so that it runs automatically at startup?

Refer to this post to add a startup auto execution script. The script content is the adb command above

Thank you for your earlier suggestions, but despite my best efforts, I haven’t been able to get the GPIO to function as a Power On feedback on my Khadas VIM2 running Android. I created a script to set the GPIO pin, which works fine when executed manually. I tried to automate this by modifying the init.rc file to import a custom .rc file that would run the script during boot. Even after successfully remounting the filesystem as read-write and editing init.rc, the script didn’t execute on boot.

I also looked into using init.d scripts, but the /system/etc/init.d/ directory wasn’t present, and I didn’t have the necessary permissions to create it. I made sure that SELinux was set to permissive mode and verified that the script and service had the correct permissions. I even checked system logs and attempted to start the service manually. While the script works manually, it still fails to execute during boot.

At this point, I’m at a bit of a loss. If you have any other suggestions or insights on how to resolve this, I’d really appreciate your input.

Thank you again for your help.

After trying numerous strategies without success, I will summarize what I have accomplished so far in an attempt to clarify the process and see if we can move forward from here.

  1. I created the script file in /vendor/etc/configure_gpio.sh with the following content:
#!/system/bin/sh

echo "Script started at $(date)" > /data/local/tmp/gpio_log.txt

echo "Exporting GPIO 470 at $(date)" >> /data/local/tmp/gpio_log.txt
echo 470 > /sys/class/gpio/export
if [ $? -eq 0 ]; then
    echo "GPIO 470 exported successfully at $(date)" >> /data/local/tmp/gpio_log.txt
else
    echo "Failed to export GPIO 470 at $(date)" >> /data/local/tmp/gpio_log.txt
fi

echo "Setting GPIO 470 direction to out at $(date)" >> /data/local/tmp/gpio_log.txt
echo out > /sys/class/gpio/gpio470/direction
if [ $? -eq 0 ]; then
    echo "GPIO 470 direction set successfully at $(date)" >> /data/local/tmp/gpio_log.txt
else
    echo "Failed to set GPIO 470 direction at $(date)" >> /data/local/tmp/gpio_log.txt
fi

echo "Setting GPIO 470 value to 1 at $(date)" >> /data/local/tmp/gpio_log.txt
echo 1 > /sys/class/gpio/gpio470/value
if [ $? -eq 0 ]; then
    echo "GPIO 470 value set to 1 successfully at $(date)" >> /data/local/tmp/gpio_log.txt
else
    echo "Failed to set GPIO 470 value at $(date)" >> /data/local/tmp/gpio_log.txt
fi

echo "Script completed at $(date)" >> /data/local/tmp/gpio_log.txt
  1. I created the service in /vendor/etc/init/configure_gpio.rc with the following content:
service configure_gpio /system/bin/sh /vendor/etc/configure_gpio.sh
    class core
    user root
    group root
    oneshot
  1. I imported the service in init.rc in the root directory: import /vendor/etc/init/configure_gpio.rc

When I run the script manually in administrator mode, everything works perfectly, but the script is still not being executed during boot. I have checked all the permissions, and they seem to be correct.

Can you identify any errors in my approach? Do you have any other ideas on where we can go from here?

I also tryed to execute the script directly from /vendor/etc/init/hw/init.amlogic.board.rc with the line: exec /system/bin/sh /vendor/etc/configure_gpio.sh

But still no success

I forgot to mention, maybe you noticed I used the GPIO 470, that’s why I tried the one you sugested and it did not work. In fack, none of the Android number on the VIM2 docs worked, but some of the Linux ones had, why is that?

@Rafael_Morato_Mangua Your problem should be that you did not add the corresponding permissions, which caused the service to fail to start. You can refer to the following submission to add the corresponding configure_gpio.te file:

Thank you so much, look what I did:

Create the file at /etc/selinux/gpio.te

type gpio, domain;

allow gpio self:capability { net_admin sys_rawio };
allow gpio sysfs_gpio:file { read write open getattr };
allow gpio gpio_exec:file { read execute open };

allow gpio vendor_file_type:file { read write open };

Added the line to /etc/selinux/plat_file_contexts

/vendor/etc/gpio.sh   u:object_r:gpio_exec:s0

Reboot and nothing. The service doesn’t apear at the service list and is not started.
Did I forget something?

@Rafael_Morato_Mangua Is there any startup code added like this?

Is the content of init.rc complete below