Hotwheel Technology has developed a rk3588s motherboard, youyeetoo R1, which provides 40pin expansion pins, of which 7 pins can be used as gpio pins. The pin distribution is shown in the figure below:
If you want to use shell commands in the Android system, you need to enter the Android command line window through ADB. For the tutorial on how to use ADB, please refer to the chapter adb debugging . After opening the Android system command line through adb, you need to export the corresponding gpio device to the user space to operate gpio. See the table below for the numbers needed for exporting. The following takes the operation of the GPIO1_A4
pin as an example.
Pin Name | GPIO Number |
---|---|
GPIO2_A6 | 70 |
GPIO1_A4 | 36 |
GPIO1_A7 | 39 |
GPIO1_B1_D | 41 |
GPIO1_D5 | 61 |
GPIO0_B0 | 8 |
GPIO0_A0 | 0 |
adb shell setprop persist.sys.root_access 3
adb root
adb remount
adb shell
cat >> /system/bin/setup.sh << EOF
echo 36 > /sys/class/gpio/export
chmod 777 /sys/class/gpio/gpio36/direction
chmod 777 /sys/class/gpio/gpio36/value
EOF
The permission management of the Android system is very strict. Using c++ or java to operate files or resources in the Android root file system requires corresponding permissions. The ndk program written here is written based on the system app as a template. For system app, please refer to the chapter Creating system app.
Write the following code in the native-lib.cpp file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <jni.h>
#include <string>
#define DIRECTION "sys/class/gpio/gpio36/direction"
#define VALUE "sys/class/gpio/gpio36/value"
extern "C" JNIEXPORT jstring JNICALL
Java_com_youyeetoo_r1_1gpio_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
int fd_dir;
int fd_val;
char val[2];
fd_dir = open( DIRECTION, O_WRONLY);//Open file
if (fd_dir < 0) {
return env->NewStringUTF("direction open fail");
}
fd_val = open( VALUE, O_RDONLY);//Open file
if (fd_val < 0) {
return env->NewStringUTF("value open fail");
}
write(fd_dir, "in", 3);
read(fd_val, val, 1);
close(fd_dir);
close(fd_val);
return env->NewStringUTF(val);
}