Hot Wheels Technology has developed a rk3588s motherboard youyeetoo R1, which provides 40pin expansion pins, including 1 pwm pin. 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 tutorials on using ADB, please refer to the adb debugging chapter. After opening the Android system command line through adb, you need to export the pwm device to user space first to operate the pwm pins.
adb shell setprop persist.sys.root_access 3
adb root
adb remount
adb shell
cat >> /system/bin/setup.sh << EOF
echo 0 > /sys/class/pwm/pwmchip0/export
chmod 666 /sys/class/pwm/pwmchip0/pwm0/*
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.
#include <jni.h>
#include <string>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
extern "C" JNIEXPORT jstring
#define PERIOD "/sys/class/pwm/pwmchip0/pwm0/period"
#define DUTY_CYCLE "/sys/class/pwm/pwmchip0/pwm0/duty_cycle"
#define ENABLE "/sys/class/pwm/pwmchip0/pwm0/enable"
JNICALL
Java_com_youyeetoo_r1_1pwm_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
int fd_period = open(PERIOD, O_WRONLY);
int fd_dutyPeriod = open(DUTY_CYCLE, O_WRONLY);
int fd_enable = open(ENABLE, O_WRONLY);
if (fd_period < 0 || fd_dutyPeriod < 0 || fd_enable < 0)
{
return env->NewStringUTF("period open fail OR fd_dutyPeriod open fail OR fd_enable open fail");
}
write(fd_period, "10000", 6);
write(fd_dutyPeriod, "5000", 5);
write(fd_enable, "1", 2);
close(fd_period);
close(fd_dutyPeriod);
close(fd_enable);
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
Use an oscilloscope after running the program. If you can see the square wave, it is successful!