Difference between revisions of "APITestPage"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
(updated by API)
 
(202 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Android Hardware Programming for RK3399|English]]
 
  
==简介==
+
{{RockchipUnbrick|NanoPC-T4}}
友善电子开发了一个名为libfriendlyarm-hardware.so的函数库,用于Android应用程序访问开发板上的硬件资源,该函数库基于Android-NDK技术开发,提供便利的硬件访问接口,开发者无需掌握过多的嵌入式知识便可使用,有效提高开发进度。<br /><br />
+
本篇文章仅适用于RK3399开发板,包括 NanoPi-NEO4, NanoPi-M4 和 NanoPC-T4。<br />
+
其他平台也有相应的支持,请移步这里查看:[[Android Hardware Access APIs/zh]]
+
 
+
==支持如下RK3399官方Android BSP==
+
* Android 7.1.2
+
* Android 8.1
+
<br />
+
为方便测试,已集成了各个硬件资源的开源示例程序:
+
[[File:Android8-hw-demos.png|frameless|450px]]
+
 
+
==支持如下RK3399硬件资源==
+
* Serial Port
+
* GPIO
+
* ADC
+
* PWM
+
* I2C
+
* RTC
+
* Watch dog
+
* SPI
+
 
+
==支持的开发板==
+
* NanoPi-NEO4
+
* NanoPi-M4
+
* NanoPC-T4
+
* 其他开发板请移步这里查看:[[Android Hardware Access APIs/zh]]
+
 
+
==示例程序下载地址==
+
所有硬件访问的示例程序均已集成到 Android 的源代码当中,位于 Android7.1.2 和 Android8.1 源代码的以下目录:vendor/friendlyelec/apps,也可以单独网上下载,下表中列出各个 Demo 的源代码地址:
+
{{libfriendlyarm-hardware.so Android8 Demos}}
+
{{libfriendlyarm-hardware.so Android7 Demos}}
+
{{libfriendlyarm-hardware.so Installation Guide/zh}}
+
 
+
==RK3399平台Android硬件资源的程序访问==
+
===Serial Port===
+
目前可直接使用的串口是UART4,设备名称为/dev/ttyS4,其他串口资源已被分配给其它功能,如下表所示,你也可以使用USB转串口来扩展:
+
::{| class="wikitable"
+
|-
+
|串口设备 || 串口资源占用情况
+
|-
+
|UART0  || 已被蓝牙占用
+
|-
+
|UART1  ||  已被千兆以太网占用 
+
|-
+
|UART2  ||  已被作为调试串口
+
|-
+
|UART3  ||  已被千兆以太网占用
+
|-
+
|UART4  ||  空闲,设备名称为 /dev/ttyS4 (注:需使用20180618之后的ROM)
+
|}
+
 
+
====串口读写的接口说明====
+
<syntaxhighlight lang="c">
+
HardwareControler.openSerialPortEx //打开串口设备
+
HardwareControler.select  //轮询串口是否有数据可写或可读
+
HardwareControler.read    //读取串口数据
+
HardwareControler.write    //写数据到串口
+
HardwareControler.close    //关闭设备
+
</syntaxhighlight>
+
接口说明请参考:[[Android Hardware Access APIs/zh]]
+
 
+
 
+
===GPIO===
+
 
+
通过 sysfs 方式控制来 GPIO,是先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO编号,使得该 GPIO 的操作接口从内核空间暴露到用户空间,然后就可以操作引脚的 direction 和 value 等,direction 控制 GPIO 方向,而 value 可控制 GPIO 输出或获得 GPIO 输入。<br />
+
<br />
+
下面中列出RK3399可直接操作的GPIO编号:
+
* NanoPC T4
+
::{| class="wikitable"
+
|-
+
| style="background: PaleTurquoise; color: black" | '''物理序号'''
+
| style="background: PaleTurquoise; color: black" |'''GPIO编号'''
+
|-
+
|Pin11||33
+
|-
+
|Pin12||50
+
|-
+
|Pin15||36
+
|-
+
|Pin16||54
+
|-
+
|Pin18||55
+
|-
+
|Pin22||56
+
|-
+
|Pin37||96
+
|-
+
|Pin38||125
+
|-
+
|Pin40||126
+
|}
+
* NanoPi M4和NanoPi NEO4
+
::{| class="wikitable"
+
|-
+
| style="background: PaleTurquoise; color: black" | '''物理序号'''
+
| style="background: PaleTurquoise; color: black" |'''Linux索引号'''
+
|-
+
|Pin11||33
+
|-
+
|Pin12||50
+
|-
+
|Pin15||36
+
|-
+
|Pin16||54
+
|-
+
|Pin18||55
+
|-
+
|Pin22||56
+
|}
+
 
+
====GPIO接口说明====
+
<syntaxhighlight lang="c">
+
HardwareControler.exportGPIOPin      //导出GPIO
+
HardwareControler.setGPIODirection  //改变GPIO的方向
+
HardwareControler.getGPIODirection  //获得当前GPIO的方向
+
HardwareControler.setGPIOValue    //向引脚输出高电平或低电平
+
HardwareControler.getGPIOValue    //获得引脚的输入(当前的值)
+
HardwareControler.unexportGPIOPin //取消导出
+
</syntaxhighlight>
+
接口说明请参考:[[Android Hardware Access APIs/zh]]
+
 
+
====GPIO示例说明====
+
可以用LED配件来测试GPIO示例,在界面上对引脚输出高电平时,LED亮,低电平时,LCD灭。
+
 
+
===ADC===
+
 
+
RK3399引出了三路ADC,channel为0, 2, 3,对应的文件节点为:
+
::{| class="wikitable"
+
|-
+
| style="background: PaleTurquoise; color: black" | '''频道'''
+
| style="background: PaleTurquoise; color: black" |'''文件节点'''
+
|-
+
|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
+
|}
+
 
+
在 Android 下用标准的文件读取接口就可以读取,你可以将一个可调电阻配件连接到开发板上进行测试。
+
 
+
===PWM===
+
RK3399开发板也是通过sysfs方式来控制PWM,直接操作 /sys/class/pwm/pwmchip1 下的文件节点即可,下面以在命令行操作PWM风扇风速作为示例:
+
 
+
====PWM接口说明====
+
* 将PWM0暴露到用户空间
+
<syntaxhighlight lang="bash">
+
echo 0 > /sys/class/pwm/pwmchip1/export
+
</syntaxhighlight>
+
* 通过设置PWM的period和duty_cycle来控制风速
+
<syntaxhighlight lang="bash">
+
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
+
</syntaxhighlight>
+
====PWM示例说明====
+
可以连接一个PWM风扇(3针)到NanoPC-T4的风扇接口来测试PWM示例。
+
 
+
===I2C===
+
 
+
运行I2C Demo需要将一个LCD1602的模块连接到NanoPC-T4的以下引脚:
+
::{| class="wikitable"
+
|-
+
| style="background: PaleTurquoise; color: black" | '''物理序号'''
+
| style="background: PaleTurquoise; color: black" |'''I2C功能'''
+
|-
+
|Pin3||I2C2_SDA(3V)
+
|-
+
|Pin4||VCC5V0_SYS
+
|-
+
|Pin5||I2C2_SCL(3V)
+
|-
+
|Pin6||GND
+
|}
+
 
+
如下图所示:
+
[[File:NanoPC-T4+OLED.jpg|frameless|450px]]
+
 
+
===RTC===
+
通过 /sys/class/rtc/rtc0/下面的接口来操作RTC,比如查看当前RTC的日期和时间:<br />
+
<syntaxhighlight lang="bash">
+
cat /sys/class/rtc/rtc0/date
+
# 2018-10-20                                                             
+
cat /sys/class/rtc/rtc0/time                                           
+
# 08:20:14
+
</syntaxhighlight>
+
设置开机时间,如设置120秒后开机:
+
<syntaxhighlight lang="bash">
+
#120秒后定时开机
+
echo +120 >  /sys/class/rtc/rtc0/wakealarm
+
</syntaxhighlight>
+
 
+
===Watch dog===
+
看门狗的操作比较简单,打开设备 /dev/watchdog 并定时写入字符即可,如果系统出现问题导致没有写入字符,设备会过一段时间自动重启:
+
<syntaxhighlight lang="java">
+
mWatchDogFD = HardwareControler.open("/dev/watchdog", FileCtlEnum.O_WRONLY);
+
HardwareControler.write(mWatchDogFD, "a".getBytes());
+
</syntaxhighlight>
+
===SPI===
+
SPI由于与UART4共用引脚,所以需要修改内核DTS文件,先禁用UART4方可使用SPI功能。<br />
+
运行SPI示例需要在NanoPC-T4上连接一个 SPI接口的OLED,规格为0.96寸的128x64屏。
+
<br />
+
SPI接口说明请参考:[[Android Hardware Access APIs/zh]]
+
 
+
 
+
==函数库(libfriendlyarm-hardware.so)接口说明==
+
请参考这份WiKi文档:[[Android Hardware Access APIs/zh]]
+

Latest revision as of 09:23, 4 June 2024

1 Unbricking Method

If the ROM is not installed correctly, causing the development board to become bricked, and you might not have the opportunity to reinstall the ROM via an SD card, you need to enter Maskrom mode to unbrick it by erasing the storage device.

1.1 Windows Users

1.1.1 Download Required Files

  • Get the necessary tools: Visit here, find RKDevTool_v3.19_for_window.zip and DriverAssitant_v5.12.zip in the 05_Tools directory, and download them to your local machine.
  • Install Rockchip USB driver and RKDevTool: Extract DriverAssitant_v5.12.zip to install the Rockchip USB driver, and extract RKDevTool_v3.19_for_window.zip to obtain the Rockchip flashing tool RKDevTool.
  • Get the loader: Visit here, enter the tools directory corresponding to your CPU model, and download MiniLoaderAll.bin.

1.1.2 Enter Maskrom Mode to Erase the Storage Device

  • Connect NanoPC-T4 to your computer using a USB data cable.
  • Start RKDevTool on your computer.
  • Disconnect the power from NanoPC-T4, hold down the MASK button, connect the power, and release the button when you see Found One MASKROM Device displayed at the bottom of the interface, as shown below:

Rkdevtool found one maskrom device.png

  • Click the Advanced Function tab in the RKDevTool interface.
  • In the Boot text box, select MiniLoaderAll.bin, then click the Download button.
  • Select EMMC, click Switch Storage, then click the EraseAll button to erase the eMMC.

Rkdevtool erase emmc.png

  • At this point, NanoPC-T4 is restored to its initial state and can be normally booted via SD card or eMMC.

1.2 Linux/Mac Users

1.2.1 Download the Required Files

  • Get the necessary tools: Visit here and find upgrade_tool_v2.30_for_linux.tgz (or for Mac users, select upgrade_tool_v2.25_for_mac.tgz) in the 05_Tools directory and download it locally.
  • Get the loader: Visit here, enter the tools directory corresponding to your CPU model, and download MiniLoaderAll.bin.

1.2.2 Installation for upgrade_tool

The following commands are for Linux, with only slight differences in file and directory names for Mac users:

tar xzf upgrade_tool_v2.30_for_linux.tgz
cd upgrade_tool_v2.30_for_linux
sudo cp upgrade_tool /usr/local/sbin/
sudo chmod 755 /usr/local/sbin/upgrade_tool

1.2.3 Enter Maskrom Mode to Erase the Storage Device

  • Connect NanoPC-T4 to the computer using a USB data cable.
  • Disconnect the power from NanoPC-T4, hold down the MASK button, connect the power, and release the button after 4 seconds.
  • Check the connection with the following command:
upgrade_tool LD

A result similar to "DevNo=1 Vid=0x2207,Pid=0x350b,LocationID=13 Mode=Maskrom SerialNo=" indicates that the device has been detected.

  • Erase the eMMC with the following command:
upgrade_tool EF MiniLoaderAll.bin
  • At this point, NanoPC-T4 has been restored to its initial state and can boot the system normally via SD card or eMMC.