FriendlyThings for RK3399

From FriendlyARM WiKi
Revision as of 02:24, 28 December 2018 by Yftan (Talk | contribs)

Jump to: navigation, search


Note: the steps and methods presented here apply to all FriendlyElec's RK3399 based boards. For steps and methods that apply to other platforms refer to FriendlyThings

1 Introduction

FriendlyThings is an Android SDK developed by FriendlyElec to access hardware. Users can use it to access various hardware resources Uart, SPI, I2C, GPIO etc on a FriendlyElec ARM board under Android. This SDK is based on Android-NDK. Users can use it to develop popular IoT applications without directly interacting with drivers.

2 Android Versions

The Android BSPs provided by FriendlyElec already have FriendlyThings SDK( and currently have two version:

  • Android 7.1.2-rk3399

BSP source code download link:
BSP source code location on the network disk: sources/rk3399-android-7.git-YYYYMMDD.tgz
Latest ROM download link:

  • Android 8.1-rk3399

BSP source code download link:
BSP source code location on the network disk: sources/rk3399-android-8.1.git-YYYYMMDD.tgz
Latest ROM download link:

3 List of Applicable Boards

FriendlyThings SDK( works with the following FriendlyElec RK3399 based boards:

  • NanoPC-T4
  • NanoPi M4 (an external eMMC module is needed)
  • NanoPi NEO4 (an external eMMC module is needed)

FriendlyThings might also work with other FriendlyElec boards such as Samsung S5P4418/S5P6818, Samsung S5PV210, Allwinner H3/H5 etc. For more details refer to FriendlyThings

4 Quick Start

4.1 Step 1: Include in APP

Clone the following library locally:

git clone

Copy all the files under the libs directory to your working directory and create a "com/friendlyarm" directory in your Android project's src directory, copy the whole "java/FriendlyThings" to your newly created "com/friendlyarm" directory. The whole project directory will look like this(Note:AndroidStudio's project may be a little bit different):

├── AndroidManifest.xml
├── libs
│   ├── arm64-v8a
│   │   └──
│   └── armeabi
│       └──
├── src
│   └── com
│       └── friendlyarm
│           ├── FriendlyThings
│           │   ├──
│           │   ├──
│           │   ├──
│           │   ├──
│           │   ├──
│           │   ├──
│           │   └──

Import the following components and the major APIs are included in the file:

import com.friendlyarm.FriendlyThings.HardwareControler;
import com.friendlyarm.FriendlyThings.SPIEnum;
import com.friendlyarm.FriendlyThings.GPIOEnum;
import com.friendlyarm.FriendlyThings.FileCtlEnum;
import com.friendlyarm.FriendlyThings.BoardType;

4.2 Step 2: Give APP System Right

Your app needs the system right to access hardware resources;
Give your app the system right by making changes in the AndroidManifest.xml file and the file;
It is better to include your app in your Android source code and compile them together. If your app is not compiled together with your Android source code you have to go through tedious steps to compile your app and sign your app to give it the system right.

4.2.1 Modify AndroidManifest.xml

Add the following line in the manifest node in the AndroidManifest.xml file:


4.2.2 Modify

Create an file(the simplest way is to copy a sample file), modify the file by adding a line LOCAL_CERTIFICATE := platform :

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_CFLAGS := -lfriendlyarm-hardware
include $(BUILD_PACKAGE)

4.3 Final Step: Compile Your APP Together with Android Source Code

Go to Android source code's root directory and run "" to export environmental variables, enter your app's directory and run "mm" to compile:
For example: compile the GPIO_LED_Demo on RK3399:

cd rk3399-android-8.1
cd vendor/friendlyelec/apps/GPIO_LED_Demo

5 APIs in Library

Refer to this wiki site:FriendlyThings APIs

6 Access RK3399 Based Boards' Hardware under Android

6.1 Serial Port

Currently the only available serial port for users is UART4 and its device name is "/dev/ttyS4". Other serial ports are already taken. Here is a list of the serial ports and their functions. If you need more serial ports you can convert USB ports to serial ports:

Serial Function
UART0 used for Bluetooth
UART1 used for Gbps Ethernet
UART2 used for serial debug port
UART3 used for Gbps Ethernet
UART4 available for users and its device name is "/dev/ttyS4" (Note: it is only available in ROMs generated on June 18,2018 or after)

6.1.1 APIs for Accessing Serial Ports

HardwareControler.openSerialPortEx //opens a serial port.   //polls a serial port's status and checks if it has data to be read or if data can be written to it.     //reads data from a serial port.
HardwareControler.write    //writes data to a serial port.
HardwareControler.close    //closes a serial port.

For more details refer to :FriendlyThings APIs

6.2 GPIO

You can access GPIO by calling sysfs APIs. You need to access the "/sys/class/gpio" directory, write a GPIO index number you want to access to the export file, and set the GPIO's direction and value.

Here is a list of GPIOs FriendlyElec's RK3399 boards support:

  • NanoPC T4
Physical Index GPIO Index
Pin11 33
Pin12 50
Pin15 36
Pin16 54
Pin18 55
Pin22 56
Pin37 96
Pin38 125
Pin40 126
  • NanoPi M4和NanoPi NEO4
Physical Index Linux Index
Pin11 33
Pin12 50
Pin15 36
Pin16 54
Pin18 55
Pin22 56

6.2.1 APIs for Accessing GPIO

HardwareControler.exportGPIOPin      //exports a GPIO.
HardwareControler.setGPIODirection   //sets a GPIO's direction.
HardwareControler.getGPIODirection   //gets a GPIO's direction.
HardwareControler.setGPIOValue    //sets a GPIO's value.
HardwareControler.getGPIOValue    //gets a GPIO's value
HardwareControler.unexportGPIOPin //unexports a GPIO.

For more details refer to:FriendlyThings APIs

6.2.2 Testing GPIO

You can use a FriendlyElec's LED module to test GPIOs. Set a HIGH to turn on the LED and a LOW to turn off the LED. GPIO

6.3 ADC

RK3399 populates three ADC channels 0, 2 and 3 and here is a list of the channels and their corresponding nodes:

Channel Node
Channel 0 /sys/devices/platform/ff100000.saradc/iio:device0/in_voltage0_raw
Channel 2 /sys/devices/platform/ff100000.saradc/iio:device0/in_voltage2_raw
Channel 3 /sys/devices/platform/ff100000.saradc/iio:device0/in_voltage3_raw

You can access GPIOs like accessing files under Android.

6.4 PWM

You can access PWMs by calling sysfs APIs. You can access the nodes under the "/sys/class/pwm/pwmchip1" directory. Here is code sample to control a PWM fan:

6.4.1 APIs for Accessing PWM

  • Export PWM0 to users
echo 0 > /sys/class/pwm/pwmchip1/export
  • Control a PWM fan's speed by setting the PWM's period and duty_cycle.
echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable
echo 50000 > /sys/class/pwm/pwmchip1/pwm0/period
echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
echo 45000 > /sys/class/pwm/pwmchip1/pwm0/duty_cycle

6.4.2 Testing PWM

Connect a PWM fan(3 pins) to a NanoPC-T4's fan port to test it. PWM

6.5 I2C

To test I2C we connected a FriendlyElec's LCD1602 module to a NanoPC-T4 and ran the I2C demo program:

Physical Index I2C Functions
Pin3 I2C2_SDA(3V)
Pin5 I2C2_SCL(3V)
Pin6 GND

Here is a hardware setting:

6.6 RTC

You can access RTC by calling APIs under the "/sys/class/rtc/rtc0/" directory. For instance you can check the current RTC time by running the following commands:

cat /sys/class/rtc/rtc0/date
# 2018-10-20                                                              
cat /sys/class/rtc/rtc0/time                                            
# 08:20:14

Set power-on time. For instance power on in 120 seconds:

#Power on in 120 seconds
echo +120 >  /sys/class/rtc/rtc0/wakealarm


6.7 Watch dog

It is quite straightforward to access the watch dog. You can simply open the "/dev/watchdog" device and write characters to it.If for any reason no characters can be written to the device the system will reboot in a moment:

mWatchDogFD ="/dev/watchdog", FileCtlEnum.O_WRONLY);
HardwareControler.write(mWatchDogFD, "a".getBytes());


6.8 SPI

6.8.1 Enable SPI

The SPI and UART4 share the same pins. You need to modify the kernel's DTS file to enable the SPI by running the following commands:
Edit the DTS file: arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi:

vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi

You need to replace "ANDROID_SOURCE" to your real source. In our system it is either Android7's or Android8's source code directory.

Locate spi1's definition:

&spi1 {
    status = "disabled";  // change "disabled" to "okay"

Locate uart4's definition in the rk3399-nanopi4-common.dtsi file:

&uart4 {
    status = "okay";   // change "okay" to "disabled"


./ -K -M

更新 rockdev/Image-nanopc_t4/resource.img 到开发板即可,可以将resource.img拷贝到安装有 eflasher 系统的TF卡中,位置是FriendlyARM分区的androidX 目录,替换里面的resource.img文件,然后用TF卡重刷系统。

6.8.2 SPI示例

vendor/friendlyelec/apps# vi


然后重新编译 Android源代码。

运行SPI示例需要在NanoPC-T4上连接一个 SPI接口的OLED,规格为0.96寸的128x64屏。


OLED引脚 功能 NanoPC-T4引脚编号 备注
DO SCLK Pin23 (SPI1_CLK(3V) 时钟信号,由主设备产生
DI MOSI Pin19 (SPI1_TXD) 主设备数据输出,从设备数据输入
RES Pin16 (GPIO1_C6(3V))
D/C Pin12 (GPIO1_C2)
CS CS0 Pin24 (SPI1_CSn0) 从设备使能信号,由主设备控制

6.8.3 SPI接口说明

请参考:FriendlyThings APIs

7 示例程序下载地址

所有硬件访问的示例程序均已集成到 Android 的源代码当中,位于 Android7.1.2 和 Android8.1 源代码的以下目录:vendor/friendlyelec/apps,也可以单独网上下载,下表中列出各个 Demo 的源代码地址:

7.1 Android8.1

7.1.1 Applicable Boards

  • NanoPC-T4/NanoPi-M4/NanoPi-NEO4
Android8.1 Demos
Serial Port






Watch dog


7.2 Android7.1.2

7.2.1 Applicable Boards

  • NanoPC-T4/NanoPi-M4/NanoPi-NEO4
Android7.1.2 Demos
Serial Port






Watch dog