Difference between revisions of "APITestPage"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
(updated by API)
 
(235 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Android Hardware Programming for RK3399|English]]
 
  
==简介==
+
{{BurnLinuxToExtDrive-Rockchip/zh|NanoPC-T4}}
友善电子开发了一个名为libfriendlyarm-hardware.so的函数库,用于Android应用程序访问开发板上的硬件资源,该函数库基于Android-NDK技术开发,提供便利的硬件访问接口,开发者无需掌握过多的嵌入式知识便可使用,有效提高开发进度。<br /><br />
+
{{BurnLinuxToExtDrive-Rockchip|NanoPC-T4}}
本篇文章仅适用于RK3399开发板,包括 NanoPi-NEO4, NanoPi-M4 和 NanoPC-T4。<br />
+
其他平台也有相应的支持,请移步这里查看:[[Android Thing: Android Hardware Programming]]
+
 
+
==支持如下RK3399官方Android BSP==
+
* Android 7.1.2
+
* Android 8.1
+
 
+
==支持如下RK3399硬件资源==
+
* Serial Port
+
* GPIO
+
* ADC
+
* PWM
+
* I2C
+
* RTC
+
* Watch dog
+
* SPI
+
等等, 接口包括I2C, SPI, GPIO常用的接口。<br />
+
[[File:NanoPC-T4+OLED.jpg|frameless|450px|Smart4418SDK+OLED]]
+
 
+
==支持的开发板==
+
* NanoPi-NEO4
+
* NanoPi-M4
+
* NanoPC-T4
+
* 其他开发板请移步这里查看:[[Android Thing: Android Hardware Programming]]
+
 
+
==开源Android示例==
+
所有硬件访问的示例程序均已集成到 Android 的源代码当中,位于 Android7.1.2 和 Android8.1 源代码的以下目录:vendor/friendlyelec/apps,也可以单独网上下载,下表中列出各个 Demo 的源代码地址:
+
===Android8.1===
+
::{| class="wikitable"
+
|-
+
| style="background: PaleTurquoise; color: black" colspan="2"| '''Android8.1示例'''       
+
|-
+
|Serial Port
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/SerialPortDemo
+
|-
+
|GPIO
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/GPIO_LED_Demo
+
|-
+
|ADC
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/ADCDemo
+
|-
+
|PWM
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/PWMDemo
+
|-
+
|I2C
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/I2C_LCD1602_Demo
+
|-
+
|RTC
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/RTC_Demo
+
|-
+
|Watch dog
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/WatchDogDemo
+
|-
+
|SPI
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-android-8.1/tree/master/vendor/friendlyelec/apps/SPI_OLED_Demo
+
|-
+
|}
+
===Android7.1.2===
+
::{| class="wikitable"
+
|-
+
| style="background: PaleTurquoise; color: black" colspan="2"| '''Android7.1.2示例'''       
+
|-
+
|Serial Port
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/SerialPortDemo
+
|-
+
|GPIO
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/GPIO_LED_Demo
+
|-
+
|ADC
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/ADCDemo
+
|-
+
|PWM
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/PWMDemo
+
|-
+
|I2C
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/I2C_LCD1602_Demo
+
|-
+
|RTC
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/RTC_Demo
+
|-
+
|Watch dog
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/WatchDogDemo
+
|-
+
|SPI
+
|style="width: 90%;"|
+
https://gitlab.com/friendlyelec/rk3399-nougat/tree/nanopc-t4-nougat/vendor/friendlyelec/apps/SPI_OLED_Demo
+
|-
+
|}
+
 
+
 
+
==如何在自已的程序中使用硬件访问库==
+
===集成libfriendlyarm-hardware.so到你的工程目录===
+
硬件访问库的so文件和java接口定义文件位于以下仓库,请先克隆到本地:
+
<syntaxhighlight lang="bash">
+
git clone https://github.com/friendlyarm/android-libfriendlyarm-hardware.git
+
</syntaxhighlight>
+
接着复制 libs 目录下的所有内容到你的工程目录下,然后在你的Android项目的src目录下创建com/friendlyarm目录,将java/AndroidSDK目录拷贝到此处,最后的目录的结构看上去是这样的 (注:AndroidStudio的项目可能会稍有不同,但大致如此):<br />
+
<syntaxhighlight lang="bash">
+
YourProject/
+
├── AndroidManifest.xml
+
├── libs
+
│   ├── arm64-v8a
+
│   │   └── libfriendlyarm-hardware.so
+
│   └── armeabi
+
│      └── libfriendlyarm-hardware.so
+
├── src
+
│   └── com
+
│      └── friendlyarm
+
│          ├── AndroidSDK
+
│          │   ├── BoardType.java
+
│          │   ├── FileCtlEnum.java
+
│          │   ├── GPIOEnum.java
+
│          │   ├── HardwareControler.java
+
│          │   ├── SPIEnum.java
+
│          │   ├── SPI.java
+
│          │   └── WatchDogEnum.java
+
</syntaxhighlight>
+
主要的接口都集中在 HardwareControler.java 中,其它都是一些常量定义,使用以下方法导入它们:
+
<syntaxhighlight lang="java">
+
import com.friendlyarm.AndroidSDK.HardwareControler;
+
import com.friendlyarm.AndroidSDK.SPIEnum;
+
import com.friendlyarm.AndroidSDK.GPIOEnum;
+
import com.friendlyarm.AndroidSDK.FileCtlEnum;
+
import com.friendlyarm.AndroidSDK.BoardType;
+
</syntaxhighlight>
+
===让你的app拥有system权限===
+
需要将你的 app提升至 system权限,否则会无法访问硬件,需要修改你的 app 的 AndroidManifest.xml 和 Android.mk这两个文件,同时,你的app需要到Android源码中编译,参考示例的方式部署即可。
+
====修改AndroidManifest.xml====
+
在应用程序的AndroidManifest.xml中的manifest节点中加入以下属性:
+
<syntaxhighlight lang="bash">
+
android:sharedUserId="android.uid.system"
+
</syntaxhighlight>
+
====修改Android.mk====
+
编写一个Android.mk文件(最简单的方法就是拷贝示例中的Android.mk文件),修改Android.mk文件,加入LOCAL_CERTIFICATE := platform这一行:
+
<syntaxhighlight lang="java">
+
LOCAL_PATH:= $(call my-dir)
+
include $(CLEAR_VARS)
+
 
+
LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
 
+
LOCAL_PACKAGE_NAME := 你的项目名
+
 
+
LOCAL_CERTIFICATE := platform
+
LOCAL_MODULE_TAGS := optional
+
LOCAL_CFLAGS := -lfriendlyarm-hardware
+
 
+
include $(BUILD_PACKAGE)
+
</syntaxhighlight>
+
 
+
==RK3399平台Android硬件资源的程序访问==
+
===Serial Port===
+
目前可用的串口是UART4,设备名称为/dev/ttyS4,其他串口资源如下表所示,你也可以使用USB转串口来扩展:
+
::{| class="wikitable"
+
|-
+
|串口设备 || 串口资源占用情况
+
|-
+
|UART0  || 已被蓝牙占用
+
|-
+
|UART1  ||  已被千兆以太网占用 
+
|-
+
|UART2  ||  已被作为调试串口
+
|-
+
|UART3  ||  已被千兆以太网占用
+
|-
+
|UART4  ||  空闲,设备名称为 /dev/ttyS4 (注:需使用20180618之后的ROM)
+
|}
+
 
+
Android硬件库为串口提供的开发接口如下:
+
 
+
 
+
===GPIO===
+
 
+
===ADC===
+
 
+
===PWM===
+
 
+
===I2C===
+
 
+
===RTC===
+
 
+
===Watch dog===
+
 
+
===SPI===
+
 
+
 
+
==开发资料==
+
* 中文API文档:https://github.com/friendlyarm/AndroidHardwareAccess/blob/master/友善电子Android硬件开发指南.pdf
+

Latest revision as of 09:58, 29 October 2024

1 安装系统到M.2或USB硬盘

可以通过使用TF卡启动eFlasher系统,将引导和系统分别安装到不同存储设备,但是由于CPU不支持直接从M.2和USB设备引导,所以虽然系统可以安装到M.2和USB设备,但是引导仍然需要安装到eMMC或者TF卡。
操作步骤如下:

  • 准备一张32G或以上容量的TF卡;
  • 访问此处的下载地址下载文件名为XXXX-eflasher-multiple-os-YYYYMMDD-30g.img.gz的固件(位于"01_系统固件/02_SD卡刷机固件(SD-to-eMMC)"目录);
  • 将固件写入TF卡,在NanoPC-T4上连接好存储设备,插入TF卡上电开机,接下来要在界面上操作,如果没有显示设备,可以使用VNC代替,请参考使用VNC操作eFlasher;
  • 在eFlasher界面上,首先选择要安装的OS,然后选择引导安装的目的地 (通常选eMMC),以及选择系统安装的目的地(可以选eMMC,M.2硬盘,USB存储设备等),如下图所示:

Eflasher-select-boot-and-system-device.png

  • 没有eMMC时可使用TF卡作为引导,方法是将另一个TF卡通过USB读卡器插入USB端口,然后选择USB设备作为引导安装目的地,从而实现从TF卡引导,但系统存放在M.2或USB硬盘的目的;
  • 烧写完成后,从NanoPC-T4弹出SD卡,引导在eMMC的情况下,NanoPC-T4会自动重启至你刚刚烧写的系统,如果引导安装在TF卡,则需要拨掉电源,插入TF引导卡再上电开机;
  • 更详细的安装指南请参考此处;

2 Installing the System to M.2 or USB Drive

You can use a TF card to boot the eFlasher system, allowing the boot and system to be installed on different storage devices. However, since the CPU doesn’t support booting directly from M.2 and USB devices, the system can be installed on M.2 and USB devices, but the boot must still be installed on eMMC or a TF card.
Steps are as follows:

  • Prepare a TF card with a capacity of 32GB or larger.
  • Visit [the download link here](http://download.friendlyelec.com/APITestPage) to download the firmware file named XXXX-eflasher-multiple-os-YYYYMMDD-30g.img.gz (located in the “01_Official images/02_SD-to-eMMC images” directory).
  • Flash the firmware to the TF card, connect the storage device you intend to use on NanoPC-T4, insert the TF card and power on, we need to perform the operations in the eFlasher GUI. If your NanoPC-T4 does not have a display interface, you can use VNC; refer to Using VNC to Operate eFlasher.
  • In the eFlasher GUI, select the OS to install, and in the OS settings interface, choose the destination for boot installation (typically eMMC), then choose the destination for system installation (options include eMMC, M.2 hard drive, USB storage, etc.), as shown below:

Eflasher-select-boot-and-system-device.png

  • If no eMMC is available, the TF card can serve as the boot by inserting another TF card into the USB port via a USB card reader and selecting it as the boot destination, enabling booting from the TF card with the system stored on the M.2 or USB drive.
  • After flashing, eject the SD card from NanoPC-T4. If booting from eMMC, NanoPC-T4 will automatically restart into the newly flashed system. If boot installation is on a TF card, power off, insert the boot TF card, and power on again.
  • For a more detailed installation guide, please refer to this link.