Difference between revisions of "FriendlyThings for Rockchip"

From FriendlyELEC WiKi
Jump to: navigation, search
Line 171: Line 171:
  
 
===Watch dog===
 
===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 while:
+
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:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
mWatchDogFD = HardwareControler.open("/dev/watchdog", FileCtlEnum.O_WRONLY);
 
mWatchDogFD = HardwareControler.open("/dev/watchdog", FileCtlEnum.O_WRONLY);
Line 179: Line 179:
  
 
===SPI===
 
===SPI===
====启用SPI====
+
====Enable SPI====
SPI由于与UART4共用引脚,所以需要修改内核DTS文件,修改方法如下: <br />
+
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:<br />
编辑内核目录下的dts文件 arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi:
+
Edit the DTS file: arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
cd ANDROID_SOURCE/kernel
 
cd ANDROID_SOURCE/kernel
 
vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi
 
vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi
 
</syntaxhighlight>
 
</syntaxhighlight>
其中,ANDROID_SOURCE请替换为真实的 Android7或Android8源代码目录。
+
You need to replace "ANDROID_SOURCE" to your real source. In our system it is either Android7's or Android8's source code directory.
  
首先找到 spi1 的定义:
+
Locate spi1's definition:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
&spi1 {
 
&spi1 {
     status = "disabled";  // 将其中的 disabled 改为 okay
+
     status = "disabled";  // change "disabled" to "okay"
 
</syntaxhighlight>
 
</syntaxhighlight>
然后再在 rk3399-nanopi4-common.dtsi 文件到找到 uart4 的定义:
+
Locate uart4's definition in the rk3399-nanopi4-common.dtsi file:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
&uart4 {
 
&uart4 {
     status = "okay";  // 将其中的 okay 改为 disabled
+
     status = "okay";  // change "okay" to "disabled"
 
</syntaxhighlight>
 
</syntaxhighlight>
 
最后重新编译内核:
 
最后重新编译内核:

Revision as of 02:24, 28 December 2018

查看中文

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(libfriendlyarm-things.so) and currently have two version:

  • Android 7.1.2-rk3399

BSP source code download link: https://gitlab.com/friendlyelec/rk3399-nougat
BSP source code location on the network disk: sources/rk3399-android-7.git-YYYYMMDD.tgz
Latest ROM download link: http://download.friendlyelec.com/NanoPC-T4

  • Android 8.1-rk3399

BSP source code download link: https://gitlab.com/friendlyelec/rk3399-android-8.1
BSP source code location on the network disk: sources/rk3399-android-8.1.git-YYYYMMDD.tgz
Latest ROM download link: http://download.friendlyelec.com/NanoPC-T4

3 List of Applicable Boards

FriendlyThings SDK(libfriendlyarm-things.so) works with the following FriendlyElec RK3399/RK3568/RK3588 based boards:

  • NanoPC-T6
  • NanoPi-R6S/R6C
  • NanoPi-R5S/R5C
  • 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 libfriendlyarm-things.so in APP

Clone the following library locally:

git clone https://github.com/friendlyarm/friendlythings-sdk

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):

YourProject/
├── AndroidManifest.xml
├── libs
│   ├── arm64-v8a
│   │   └── libfriendlyarm-things.so
│   └── armeabi
│       └── libfriendlyarm-things.so
├── src
│   └── com
│       └── friendlyarm
│           ├── FriendlyThings
│           │   ├── BoardType.java
│           │   ├── FileCtlEnum.java
│           │   ├── GPIOEnum.java
│           │   ├── HardwareControler.java
│           │   ├── SPIEnum.java
│           │   ├── SPI.java
│           │   └── WatchDogEnum.java

Import the following components and the major APIs are included in the HardwareControler.java 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 Android.mk 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:

android:sharedUserId="android.uid.system"

4.2.2 Modify Android.mk

Create an Android.mk file(the simplest way is to copy a sample Android.mk file), modify the Android.mk 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_PACKAGE_NAME := Project Name
 
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_TAGS := optional
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 "setenv.sh" 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
. setenv.sh
cd vendor/friendlyelec/apps/GPIO_LED_Demo
mm

4.4 Sample project in Android Studio

https://github.com/friendlyarm/AndroidStudio-GPIODemo

5 APIs in libfriendlyarm-things.so 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.
HardwareControler.select   //polls a serial port's status and checks if it has data to be read or if data can be written to it.
HardwareControler.read     //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)
Pin4 VCC5V0_SYS
Pin5 I2C2_SCL(3V)
Pin6 GND

Here is a hardware setting:
NanoPC-T4+OLED.jpg

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

RTC

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 = HardwareControler.open("/dev/watchdog", FileCtlEnum.O_WRONLY);
HardwareControler.write(mWatchDogFD, "a".getBytes());

WatchDog

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:

cd ANDROID_SOURCE/kernel
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"

最后重新编译内核:

cd ANDROID_SOURCE/
./build-nanopc-t4.sh -K -M

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

6.8.2 SPI示例

SPI示例默认没有预装,需要手动编译,方法是修改Android源代码的以下文件:
vendor/friendlyelec/apps# vi device-partial.mk
去掉前面的注释:

# PRODUCT_PACKAGES += SPI-OLED

然后重新编译 Android源代码。

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

屏幕上有7个引脚,与开发板用母对母杜邦线连接,接法如下表:

OLED引脚 功能 NanoPC-T4引脚编号 备注
GND Pin6 GND
VCC Pin2 VCC5V
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

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/SerialPortDemo

GPIO

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/GPIO_LED_Demo

ADC

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/ADCDemo

PWM

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/PWMDemo

I2C

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/I2C_LCD1602_Demo

RTC

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/RTC_Demo-RK3399

Watch dog

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/WatchDogDemo-RK3399

SPI

https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/SPI_OLED_Demo

7.2 Android7.1.2

7.2.1 Applicable Boards

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

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/SerialPortDemo

GPIO

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/GPIO_LED_Demo

ADC

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/ADCDemo

PWM

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/PWMDemo

I2C

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/I2C_LCD1602_Demo

RTC

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/RTC_Demo-RK3399

Watch dog

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/WatchDogDemo-RK3399

SPI

https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/SPI_OLED_Demo