Difference between revisions of "FriendlyThings APIs"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[FriendlyThings APIs/zh|查看中文]]<
+
[[FriendlyThings APIs/zh|查看中文]]
  
 
{{FriendlyThings Introduction}}
 
{{FriendlyThings Introduction}}
 
{{FriendlyThings Installation Guides}}
 
{{FriendlyThings Installation Guides}}
  
==函数库(libfriendlyarm-things.so)接口说明==
+
==APIs in libfriendlyarm-things.so Library==
下面中列出HardwareControler类中的接口的定义,这些接口都是类方法,因此不需要创建HardwareControler对象实例:
+
Here is a list of HardwareControler APIs and these APIs are class interfaces and can be used directly, and you don't need to create a HardwareControler instance:
=== 通用的输入输出接口 ===
+
===GPIO APIs===
'''支持平台:全平台'''
+
'''Applicable Platforms: All'''
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Values'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 21: Line 21:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
devName: 要写入数据的<br />
+
devName: device that data will be written to<br />
flags: 打开文件的方式,例如可读可写还是只读打开,可选值需参考 FileCtlEnum.java<br />
+
flags: access mode, e.g. read-only,read/write etc. You can refer to FileCtlEnum.java<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回文件描述符,出错返回-1。<br />
+
Returns a file descriptor if it is a success or returns -1 if it is a failure.<br />
 
||
 
||
打开设备。
+
Opens a device
 
|-
 
|-
  
Line 40: Line 40:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
fd:设备文件描述符<br />
+
fd: device's file descriptor<br />
cmd: ioctl命令<br />
+
cmd: ioctl command<br />
value:命令参数,限整数<br />
+
value: parameter that will be used by the command. It can only be an integer.<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,出错返回-1。<br />
+
Returns 0 if it is a success or returns -1 if it is a failure.<br />
 
||
 
||
执行设备的ioctl操作
+
Commands ioctl operations
 
|-
 
|-
  
Line 59: Line 59:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
fd:设备文件描述符<br />
+
fd: device's file descriptor<br />
cmd: ioctl命令<br />
+
cmd: ioctl command<br />
buf:命令的参数<br />
+
buf: parameter that will be used by the command<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,出错返回-1。<br />
+
Returns 0 if it is a success or returns -1 if it is a failure.<br />
 
||
 
||
执行设备的ioctl操作, <br />
+
Commands ioctl operations<br />
如果 ioctl是一个结构体,<br />
+
if ioctl is a struct<br />
你需要将结构体的数据转换成字节流 <br />
+
you need to convert the struct to a byte stream<br />
 
|-
 
|-
  
Line 80: Line 80:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
fd: 要写入数据的文件描述符<br />
+
fd: device's file descriptor<br />
data: 要写入的数据<br />
+
data: data to be written to device<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回写入的字节数,出错返回-1。<br />
+
Returns the number of bytes written to the device if it is a success or returns -1 if it is a failure.<br />
 
||
 
||
向打开的设备或文件中写数据。
+
Writes characters to an opened device or file.
 
|-
 
|-
  
Line 99: Line 99:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
fd: 要读出数据的文件描述符<br />
+
fd: device's file descriptor<br />
buf: 存储数据的缓冲区<br />
+
buf: buffer to store data<br />
len: 要读取的字节数<br />
+
len: number of bytes to be read<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回读取的字节数,出错返回-1,如果在调read之前已到达文件末尾,则这次read返回0。<br />
+
Returns the number of bytes that are read from the device if it is a success or returns -1 if it is a failure. Returns 0 if the file pointer already points to the end of the file.<br />
 
||
 
||
从打开的设备或文件中读取数据。
+
Reads data from an opened device or file.
 
|-
 
|-
  
Line 119: Line 119:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
fd: 要查询的文件描述符<br />
+
fd: file descriptor<br />
sen: 阻塞等待数据多长时间(单位:秒)<br />
+
sen: wait time in second<br />
usec: 阻塞等待数据多长时间 (单位:纳秒,1毫秒=1000纳秒)<br />
+
usec: wait time in nanosecond<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
如果fd有数据可读,返回1, 如果没有数据可读,返回0,出错时返回-1。<br />
+
If the file has data it returns 1. If the file doesn't have data it returns 0. If this operation fails it returns -1.<br />
 
||
 
||
查询打开的设备或文件是否有数据可读。
+
Checks if an opened device or file has data for reading.
 
|-
 
|-
  
Line 136: Line 136:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
fd: 要关闭的文件描述符<br />
+
fd: file descriptor.<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
<br />
+
No<br />
 
||
 
||
关闭指定的文件描述符
+
Closes a file
 
|-
 
|-
  
  
 
|}
 
|}
=== 串口通讯的接口说明 ===
+
 
'''支持平台:全平台'''
+
===APIs for Serial Communication===
 +
'''Applicable Platforms: All'''
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 167: Line 168:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
devName: 串口设备文件名<br />
+
devName: device name<br />
baud:    波特率 <br />
+
baud:    baud rate <br />
dataBits: 数据位 (取值 5~8,一般用8 )<br />
+
dataBits: databits(range of value: 5 ~ 8, 8 by default)<br />
stopBits: 停止位 (取值 1~2,一般用1 )<br />
+
stopBits: stopbits(range of value: 1 ~ 2, 1 by default)<br />
parityBit: 奇偶校验位(取值为单个字母,O表示奇校验,E表示偶校验,N表示无校验)<br />
+
parityBit: paritybit(range of value: O means odd check, E means even check and N means no check)<br />
flowCtrl: 数据流控制(取值为单个字母,H表示硬件流控制,S表示软件流控制,N表示不使用数据流控制)<br />
+
flowCtrl: flow control(range of value: H means hardware flow control, S means software flow control and N means no flow control.)<br />
返回值说明:<br />
+
Return Value:<br />
成功打开串口时,将返回串口的文件描述符,用该描述符可进行 read、write和select等操作,如果打开失败,则返回 -1<br />
+
Returns a file descriptor if it is a success. This file descriptor can be used in the functions of read, write, select and etc. It returns -1 if it is a failure.<br />
 
||
 
||
打开指定的串口设备,并返回文件描述符。
+
Opens a serial device and returns a file descriptor.
 
|-
 
|-
 
|}
 
|}
接口的使用说明:<br />
+
Comments:<br />
 
<br />
 
<br />
先通过调用openSerialPortEx 打开串口设备,然后可以在线程中、或者用 timer 通过调用 select 接口轮询串口设备是否有数据到来,如果有,则调用 read 接口读取数据。<br />
+
You need to open a serial device by invoking the openSerialPortEx function, poll the device's status by invoking the select function and read data from it if the device has data.<br />
 
<br />
 
<br />
要往串口中写入数据,调用 write 接口即可。<br />
+
To write data to a device you can invoke the write function.<br />
串口使用完毕后,需要调用 close 关闭串口。<br />
+
If no operations need to be performed on the device you need to invoke the close function to close it.<br />
 
<br />
 
<br />
  
=== 板载LED的接口说明 ===
+
===Onboad LED APIs===
'''支持平台:此接口仅适用于Tiny和Smart系列的核心板,例如Tiny4412,Smart4412,Smart4418'''
+
'''Applicable Platforms: FriendlyElec's Tiny and Smart CPU boards e.g. Tiny4412, Smart4412, Smart4418 and etc'''
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 203: Line 204:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
ledID: 指定要开关哪一个LED (取值0~3)<br />
+
ledID: LED index(value range: 0 ~ 3)<br />
ledState: 1表示亮,0表示灭<br />
+
ledState: 1 means on and 0 means off<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success or returns -1 if it is a failure.<br />
 
||
 
||
该接口用于开关LED灯。
+
Turns on/off LED.
 
|-
 
|-
 
|}
 
|}
  
=== 让PWM蜂鸣器发声和停止发声的接口说明 ===
+
===PWM Buzzer APIs===
'''支持平台:此接口仅适用于带蜂鸣器的Tiny和Smart系列底板,例如Tiny4412,Smart4412,Smart4418'''
+
'''Applicable Platforms: All FriendlyElec's Tiny and Smart carrier boards which have a PWM buzzer, e.g. Tiny4412, Smart4412 ,Smart4418 and etc'''
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 227: Line 228:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
frequency: 要发声的频率<br />
+
frequency: sound frequency<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success or returns -1 if it is a failure<br />
 
||
 
||
按指定的频率让蜂鸣器发声
+
Turns on a PWM buzzer to make sounds at a set frequency
 
|-
 
|-
  
Line 242: Line 243:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
<br />
+
No<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success or returns -1 if it is -1<br />
 
||
 
||
让蜂鸣器停止发声
+
Turn off a PWM buzzer.
 
|-
 
|-
 
|}
 
|}
  
=== 读取ADC的转换结果的接口说明 ===
+
=== ADC APIs===
'''支持平台:仅适用于三星平台的开发板,包括6410、210、4412'''
+
'''Applicable Platforms: 6410, 210 and 4412'''
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 265: Line 266:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
<br />
+
No<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回ADC转换的结果,失败返回-1<br />
+
Returns a value if it is a success otherwise returns -1<br />
 
||
 
||
读取第一个通道的ADC转换结果
+
Reads the value of the first channel of ADC.
 
|-
 
|-
  
Line 280: Line 281:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameters:<br />
channel: 读取指定通道的ADC的值<br />
+
channel: ADC channel<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回ADC转换的结果,失败返回-1<br />
+
Returns a value if it is a success otherwise returns -1<br />
 
||
 
||
读取指定通道的ADC转换的结果
+
Reads the value of a specified channel of ADC
 
|-
 
|-
  
Line 295: Line 296:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
channels: 要读取通道的ADC频道数组<br />
+
channels: array of channels<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回多个ADC结果(数组),错误返回空<br />
+
Returns an array of values from specified channels if it is a success otherwise returns NULL.<br />
 
||
 
||
一次性读取多个频道的结果,性能好
+
Reads values of an array of channels
 
|-
 
|-
 
|}
 
|}
  
=== I2C接口说明 ===
+
=== I2C APIs===
'''支持平台:全平台'''
+
'''Applicable Platforms: All'''
在使用如下接口之前,首先需要使用open接口打开I2C设备,如下所示, 操作完成后,记得用HardwareControler.close关闭:<br />
+
Before you invoke any of the following APIs make sure to invoke the open function to open an I2C device. After finish operations make sure to invoke the HardwareControler.close function to close the device:<br />
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
int fd = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);
 
int fd = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);
 
</syntaxhighlight>
 
</syntaxhighlight>
以下接口需要该fd作来参数进行操作:<br />
+
The following APIs need to take a file descriptor as a parameter:<br />
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 323: Line 324:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
fd: I2C设备的文件描述符<br />
+
fd: I2C device's file descriptor<br />
slave: I2C设备地址,例如EEPROM设备一般是0x50<br />
+
slave: I2C device's address. For example in general an EEPROM's device address is 0x50<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success otherwise returns -1<br />
 
||
 
||
设置要操作的I2C设备地址,例如EEPROM设备一般是0x50
+
Sets an I2C device's address. For example in general an EEPROM's device address is 0x50.
 
|-
 
|-
  
Line 339: Line 340:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
fd: I2C设备的文件描述符<br />
+
fd: I2C device's file descriptor<br />
timeout: 超时时间<br />
+
timeout: timeout<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success otherwise returns -1<br />
 
<br />
 
<br />
 
||
 
||
设置超时时间(ioctl I2C_TIMEOUT)
+
Sets timeout(ioctl I2C_TIMEOUT)
 
|-
 
|-
  
Line 356: Line 357:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
fd: I2C设备的文件描述符<br />
+
fd: I2C device's file descriptor<br />
retries: 重试次数<br />
+
retries: number of retries<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success otherwise returns -1<br />
 
<br />
 
<br />
 
||
 
||
设置重试次数(ioctl I2C_RETRIES)
+
Sets the number of retries(ioctl I2C_RETRIES)
 
|-
 
|-
  
Line 376: Line 377:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
fd: I2C设备的文件描述符<br />
+
fd: I2C device's file descriptor<br />
pos: 字节位置<br />
+
pos: position to write data to<br />
byteData:要写入的数据<br />
+
byteData: data to be written to<br />
wait_ms: 等待指定的时间(毫秒)<br />
+
wait_ms: wait time in millisecond<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success otherwise returns -1<br />
 
<br />
 
<br />
 
||
 
||
写一个字节的数据到I2C设备的指定位置,并等待指定的时间(毫秒)
+
Writes a byte to the specified position of an I2C device in the specified wait time(in millisecond).
 
|-
 
|-
  
Line 397: Line 398:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
fd: I2C设备的文件描述符<br />
+
fd: I2C device's file descriptor<br />
pos: 字节位置<br />
+
pos: position to write data to<br />
wait_ms: 等待指定的时间(毫秒)<br />
+
wait_ms: wait time in millisecond<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回-1<br />
+
Returns 0 if it is a success otherwise returns -1<br />
 
<br />
 
<br />
 
||
 
||
从I2C设备指定的位置读一个字节的数据,并等待指定的时间(毫秒)
+
Reads a byte from the specified position of an I2C device in the specified wait time(in millisecond).
 
|-
 
|-
  
 
|}
 
|}
  
=== SPI接口说明 ===
+
===SPI APIs===
'''支持平台:全平台'''
+
'''Applicable Platforms: All'''
在使用如下接口之前,首先需要使用open接口打开SPI设备,如下所示, 操作完成后,记得用HardwareControler.close关闭:<br />
+
Before you invoke any of the following APIs make sure to invoke the open function to open an SPI device. After finish operations make sure to invoke the HardwareControler.close function to close the device:<br />
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
int spi_fd = HardwareControler.open("/dev/spidev1.0", FileCtlEnum.O_RDWR );
 
int spi_fd = HardwareControler.open("/dev/spidev1.0", FileCtlEnum.O_RDWR );
 
</syntaxhighlight>
 
</syntaxhighlight>
以下接口需要该spi_fd作来参数进行操作:<br />
+
The following APIs need to take a file descriptor spi_fd as a parameter:<br />
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 429: Line 430:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
bits: 字长,单位是比特<br />
+
bits: number of bits<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number<br />
 
<br />
 
<br />
 
||
 
||
设置每次读SPI设备的字长,单位是比特。<br />
+
Sets the number of bits to read.<br />
虽然大部分SPI接口的字长是8或者16,仍然会有一些特殊的例子。<br />
+
In general the number of bits to read is either 8 or 16 but there are exceptions in which you need to set the number of bits to read.<br />
需要说明的是,如果这个成员为零的话,默认使用8作为字长(ioctl SPI_IOC_WR_BITS_PER_WORD)
+
If this parameter is set to 0 it reads 8 bits (ioctl SPI_IOC_WR_BITS_PER_WORD).
 
|-
 
|-
  
Line 449: Line 450:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
bits: 字长,单位是比特<br />
+
bits: number of bits<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
<br />
 
<br />
 
||
 
||
设置每次写SPI设备的字长,单位是比特 (ioctl SPI_IOC_RD_BITS_PER_WORD)
+
Sets the number of bits to read from an SPI device(ioctl SPI_IOC_RD_BITS_PER_WORD)
 
|-
 
|-
  
Line 467: Line 468:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
order: 传SPIEnum.MSBFIRST或SPIEnum.LSBFIRST<br />
+
order: SPIEnum.MSBFIRST or SPIEnum.LSBFIRST<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number<br />
 
||
 
||
设备SPI传输时是先传输低比特位还是高比特位,可选的参数有SPIEnum.MSBFIRST和SPIEnum.LSBFIRST
+
Sets either most significant bits(SPIEnum.MSBFIRST) or least significant bits(SPIEnum.LSBFIRST) to be transmitted first.
 
|-
 
|-
  
Line 484: Line 485:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
divider: 分频系数,传入在SPIEnum.java中定义的以SPI_CLOCK_开头的常量,例如:<br />
+
divider: a clock divider which should be a value specified in SPIEnum.java and starts with "SPI_CLOCK_" e.g.<br />
 
SPIEnum.SPI_CLOCK_DIV128<br />
 
SPIEnum.SPI_CLOCK_DIV128<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
||
 
||
设置SPI的分频系数
+
Sets an SPI clock divider.
 
|-
 
|-
  
Line 502: Line 503:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
mode: SPI设备的模式,可传入SPIEnum.SPI_MODE0 ~ SPIEnum.SPI_MODE3<br />
+
mode: SPI device's data mode. It can be any value among SPIEnum.SPI_MODE0 ~ SPIEnum.SPI_MODE3<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
||
 
||
设置SPI设备的模式
+
Sets SPI's data mode
 
|-
 
|-
  
Line 522: Line 523:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
byteData:要写入SPI设备的数据<br />
+
byteData: data to be written to an SPI device<br />
spi_delay:延时<br />
+
spi_delay: delay time<br />
spi_speed:传输速度<br />
+
spi_speed: transfer speed<br />
spi_bits:字长,单位是比特<br />
+
spi_bits: number of bits<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回读到的数据,失败返回负数<br />
+
Returns read data if it is a success otherwise returns a negative number<br />
 
<br />
 
<br />
 
||
 
||
同时发送与接收一个字节的数据,调用示例:
+
Transfers and receives a byte simultaneously. e.g.
 
int byteRet = SPItransferOneByte(spi_fd
 
int byteRet = SPItransferOneByte(spi_fd
 
, 0xAA
 
, 0xAA
Line 553: Line 554:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
writeData:要写入的数据<br />
+
writeData: data to be written to a device<br />
readBuff: 存放读取数据的缓冲区<br />
+
readBuff: buffer to store read data<br />
spi_delay:延时<br />
+
spi_delay: delay time<br />
spi_speed:传输速度<br />
+
spi_speed: transfer speed<br />
spi_bits:字长,单位是比特<br />
+
spi_bits: number of bits<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
<br />
 
<br />
 
||
 
||
同时发送与接收多个字节的数据
+
Transfers and receives multiple bytes simultaneously.
 
|-
 
|-
  
Line 578: Line 579:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
spi_fd: SPI设备的文件描述符<br />
+
spi_fd: SPI device's file descriptor<br />
writeData:要写入的数据<br />
+
writeData: data to be written to a device<br />
spi_delay:延时<br />
+
spi_delay: delay time<br />
spi_speed:传输速度<br />
+
spi_speed: transfer speed<br />
spi_bits:字长,单位是比特<br />
+
spi_bits: number of bits<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number<br />
 
<br />
 
<br />
 
||
 
||
写多个字节的数据到SPI设备
+
Writes multiple bytes to an SPI device.
 
|-
 
|-
  
Line 602: Line 603:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
readBuff: 存放读取数据的缓冲区<br />
+
readBuff: buffer to store read data<br />
spi_delay:延时<br />
+
spi_delay: delay time<br />
spi_speed:传输速度<br />
+
spi_speed: transfer speed<br />
spi_bits:字长,单位是比特<br />
+
spi_bits: number of bits<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
<br />
 
<br />
 
||
 
||
从SPI设备读取多个字节
+
Reads multiple bytes from an SPI device.
 
|-
 
|-
 
|}
 
|}
  
  
=== GPIO接口说明 ===
+
===GPIO APIs===
'''支持平台:全平台'''
+
'''Applicable Platforms: All'''
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
  
 
|-
 
|-
Line 631: Line 632:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
pin: GPIO引脚编号<br />
+
pin: GPIO Pin<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
||
 
||
通知系统需要导出控制的GPIO引脚编号,<br />
+
Tells system to export a GPIO pin<br />
相当于执行命令 echo pin > /sys/class/gpio/export
+
It is identical to run "echo pin > /sys/class/gpio/export"
 
|-
 
|-
  
Line 647: Line 648:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
pin: GPIO引脚编号<br />
+
pin: GPIO Pin<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
||
 
||
通知系统取消导出某个GPIO引脚,<br />
+
Tells system to unexport a GPIO pin<br />
相当于执行命令 echo pin > /sys/class/gpio/unexport
+
It is identical to run "echo pin > /sys/class/gpio/unexport"
 
|-
 
|-
  
Line 663: Line 664:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
pin: GPIO引脚编号<br />
+
pin: GPIO Pin<br />
value: 传入GPIOEnum.LOW表示输出低电平,传入GPIOEnum.HIGH表示输出高电平<br />
+
value: it is either GPIOEnum.LOW which means a LOW level or GPIOEnum.HIGH which means a HIGH level<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
||
 
||
对某个引脚输出高或低电平
+
Sets a HIGH or LOW to a GPIO pin
 
|-
 
|-
  
Line 679: Line 680:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
pin: GPIO引脚编号<br />
+
pin: GPIO Pin<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回GPIOEnum.LOW表示输出低电平,<br />
+
Returns either GPIOEnum.LOW <br />
返回GPIOEnum.HIGH表示输出高电平,失败返回负数<br />
+
or GPIOEnum.HIGH if it is a success otherwise returns a negative number.<br />
 
||
 
||
查询某个引脚的状态(高或低电平)
+
Gets a GPIO pin's status(HIGH or LOW)
 
|-
 
|-
  
Line 695: Line 696:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
pin: GPIO引脚编号<br />
+
pin: GPIO Pin<br />
direction: 传入GPIOEnum.IN表示输入, <br />
+
direction: it is either GPIOEnum.IN which means the direction is INPUT <br />
GPIOEnum.OUT表示输出<br />
+
or GPIOEnum.OUT which means the direction is OUTPUT<br />
 
<br />
 
<br />
返回值说明:<br />
+
Return Value:<br />
成功返回0,失败返回负数<br />
+
Returns 0 if it is a success otherwise returns a negative number.<br />
 
||
 
||
配置引脚功能为输出或者输入
+
Sets a GPIO pin's direction.
 
|-
 
|-
  
Line 712: Line 713:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
参数说明:<br />
+
Parameter:<br />
pin: GPIO引脚编号<br />
+
pin: GPIO Pin<br />
 
<br />
 
<br />
成功返回GPIOEnum.IN表示输入,<br />
+
Returns either GPIOEnum.IN or<br />
返回 GPIOEnum.OUT表示输出,失败返回负数<br />
+
GPIOEnum.OUT if it is a success otherwise returns a negative number.<br />
 
||
 
||
查询引脚功能 (为输出或者输入)
+
Gets a GPIO pin's direction(either OUTPUT or INPUT)
 
|-
 
|-
 
|}
 
|}
  
=== 查询开发板型号 ===
+
===APIs for Checking Board Type===
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
| style="background: PaleTurquoise; color: black" | '''接口名称'''
+
| style="background: PaleTurquoise; color: black" | '''API Name'''
| style="background: PaleTurquoise; color: black" |'''参数与返回值说明'''
+
| style="background: PaleTurquoise; color: black" |'''Parameters and Return Value'''
| style="background: PaleTurquoise; color: black" |'''功能说明'''
+
| style="background: PaleTurquoise; color: black" |'''Function'''
 
|-
 
|-
 
|
 
|
Line 734: Line 735:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
||
 
||
成功返回一个代表开发板型号的数字,<br />
+
Returns a board type which is a number and <br />
数字的含义在 BoardType.java 中定义,<br />
+
is defined in BoardType.java if it is a success<br />
如果开发板无法识别,将失败负数 <br />
+
otherwise returns a negative number.<br />
 
||
 
||
查询开发板型号, 调用示例:<br />
+
Here is a code sample:<br />
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
private int mBoardType = HardwareControler.getBoardType();
 
private int mBoardType = HardwareControler.getBoardType();
Line 748: Line 749:
 
|}
 
|}
  
== 示例程序 ==
+
==Code Samples==
 
{{FriendlyThings Android8 Demos}}
 
{{FriendlyThings Android8 Demos}}
 
{{FriendlyThings Android7 Demos}}
 
{{FriendlyThings Android7 Demos}}
 
{{FriendlyThings Earlier version of Android Demos}}
 
{{FriendlyThings Earlier version of Android Demos}}

Latest revision as of 14:57, 28 December 2018

查看中文

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.


Template:FriendlyThings Installation Guides

2 APIs in libfriendlyarm-things.so Library

Here is a list of HardwareControler APIs and these APIs are class interfaces and can be used directly, and you don't need to create a HardwareControler instance:

2.1 GPIO APIs

Applicable Platforms: All

API Name Parameters and Return Values Function
int open(
String devName
, int flags)

Parameters:
devName: device that data will be written to
flags: access mode, e.g. read-only,read/write etc. You can refer to FileCtlEnum.java

Return Value:
Returns a file descriptor if it is a success or returns -1 if it is a failure.

Opens a device

int ioctlWithIntValue (
int fd,
int cmd,
int value)

Parameters:
fd: device's file descriptor
cmd: ioctl command
value: parameter that will be used by the command. It can only be an integer.

Return Value:
Returns 0 if it is a success or returns -1 if it is a failure.

Commands ioctl operations

ioctl(int fd, 
int cmd
, byte[] buf)

Parameters:
fd: device's file descriptor
cmd: ioctl command
buf: parameter that will be used by the command

Return Value:
Returns 0 if it is a success or returns -1 if it is a failure.

Commands ioctl operations
if ioctl is a struct
you need to convert the struct to a byte stream

int write(
int fd,
byte[] data)

Parameters:
fd: device's file descriptor
data: data to be written to device

Return Value:
Returns the number of bytes written to the device if it is a success or returns -1 if it is a failure.

Writes characters to an opened device or file.

int read(
int fd, 
byte[] buf, 
int len)

Parameters:
fd: device's file descriptor
buf: buffer to store data
len: number of bytes to be read

Return Value:
Returns the number of bytes that are read from the device if it is a success or returns -1 if it is a failure. Returns 0 if the file pointer already points to the end of the file.

Reads data from an opened device or file.

int select(
int fd, 
int sec, 
int usec)

Parameters:
fd: file descriptor
sen: wait time in second
usec: wait time in nanosecond

Return Value:
If the file has data it returns 1. If the file doesn't have data it returns 0. If this operation fails it returns -1.

Checks if an opened device or file has data for reading.

void close(int fd)

Parameters:
fd: file descriptor.

Return Value:
No

Closes a file

2.2 APIs for Serial Communication

Applicable Platforms: All

API Name Parameters and Return Value Function
int openSerialPortEx(
String devName,
long baud,
int dataBits,
int stopBits,
String parityBit,
String flowCtrl
)

Parameters:
devName: device name
baud: baud rate
dataBits: databits(range of value: 5 ~ 8, 8 by default)
stopBits: stopbits(range of value: 1 ~ 2, 1 by default)
parityBit: paritybit(range of value: O means odd check, E means even check and N means no check)
flowCtrl: flow control(range of value: H means hardware flow control, S means software flow control and N means no flow control.)
Return Value:
Returns a file descriptor if it is a success. This file descriptor can be used in the functions of read, write, select and etc. It returns -1 if it is a failure.

Opens a serial device and returns a file descriptor.

Comments:

You need to open a serial device by invoking the openSerialPortEx function, poll the device's status by invoking the select function and read data from it if the device has data.

To write data to a device you can invoke the write function.
If no operations need to be performed on the device you need to invoke the close function to close it.

2.3 Onboad LED APIs

Applicable Platforms: FriendlyElec's Tiny and Smart CPU boards e.g. Tiny4412, Smart4412, Smart4418 and etc

API Name Parameters and Return Value Function
int setLedState( 
int ledID, 
int ledState )

Parameters:
ledID: LED index(value range: 0 ~ 3)
ledState: 1 means on and 0 means off

Return Value:
Returns 0 if it is a success or returns -1 if it is a failure.

Turns on/off LED.

2.4 PWM Buzzer APIs

Applicable Platforms: All FriendlyElec's Tiny and Smart carrier boards which have a PWM buzzer, e.g. Tiny4412, Smart4412 ,Smart4418 and etc

API Name Parameters and Return Value Function
int PWMPlay(int frequency);

Parameter:
frequency: sound frequency

Return Value:
Returns 0 if it is a success or returns -1 if it is a failure

Turns on a PWM buzzer to make sounds at a set frequency

int PWMStop();

Parameter:
No

Return Value:
Returns 0 if it is a success or returns -1 if it is -1

Turn off a PWM buzzer.

2.5 ADC APIs

Applicable Platforms: 6410, 210 and 4412

API Name Parameters and Return Value Function
int readADC()

Parameter:
No

Return Value:
Returns a value if it is a success otherwise returns -1

Reads the value of the first channel of ADC.

int readADCWithChannel(int channel)

Parameters:
channel: ADC channel

Return Value:
Returns a value if it is a success otherwise returns -1

Reads the value of a specified channel of ADC

int[] readADCWithChannels(int[] channels);

Parameter:
channels: array of channels

Return Value:
Returns an array of values from specified channels if it is a success otherwise returns NULL.

Reads values of an array of channels

2.6 I2C APIs

Applicable Platforms: All Before you invoke any of the following APIs make sure to invoke the open function to open an I2C device. After finish operations make sure to invoke the HardwareControler.close function to close the device:

int fd = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);

The following APIs need to take a file descriptor as a parameter:

API Name Parameters and Return Value Function
int setI2CSlave(int fd, int slave);

Parameter:
fd: I2C device's file descriptor
slave: I2C device's address. For example in general an EEPROM's device address is 0x50

Return Value:
Returns 0 if it is a success otherwise returns -1

Sets an I2C device's address. For example in general an EEPROM's device address is 0x50.

int setI2CTimeout(int fd, int timeout)

Parameter:
fd: I2C device's file descriptor
timeout: timeout

Return Value:
Returns 0 if it is a success otherwise returns -1

Sets timeout(ioctl I2C_TIMEOUT)

int setI2CRetries(int fd, int retries)

Parameter:
fd: I2C device's file descriptor
retries: number of retries

Return Value:
Returns 0 if it is a success otherwise returns -1

Sets the number of retries(ioctl I2C_RETRIES)

int writeByteToI2C(int fd
, int pos
 byte byteData
 , int wait_ms)

Parameter:
fd: I2C device's file descriptor
pos: position to write data to
byteData: data to be written to
wait_ms: wait time in millisecond

Return Value:
Returns 0 if it is a success otherwise returns -1

Writes a byte to the specified position of an I2C device in the specified wait time(in millisecond).

int readByteFromI2C(int fd
, int pos
, int wait_ms);

Parameter:
fd: I2C device's file descriptor
pos: position to write data to
wait_ms: wait time in millisecond

Return Value:
Returns 0 if it is a success otherwise returns -1

Reads a byte from the specified position of an I2C device in the specified wait time(in millisecond).

2.7 SPI APIs

Applicable Platforms: All Before you invoke any of the following APIs make sure to invoke the open function to open an SPI device. After finish operations make sure to invoke the HardwareControler.close function to close the device:

int spi_fd = HardwareControler.open("/dev/spidev1.0", FileCtlEnum.O_RDWR );

The following APIs need to take a file descriptor spi_fd as a parameter:

API Name Parameters and Return Value Function
int setSPIWriteBitsPerWord( int spi_fd, int bits )

Parameter:
spi_fd: SPI device's file descriptor
bits: number of bits

Return Value:
Returns 0 if it is a success otherwise returns a negative number

Sets the number of bits to read.
In general the number of bits to read is either 8 or 16 but there are exceptions in which you need to set the number of bits to read.
If this parameter is set to 0 it reads 8 bits (ioctl SPI_IOC_WR_BITS_PER_WORD).

int setSPIReadBitsPerWord( int spi_fd
, int bits )

Parameter:
spi_fd: SPI device's file descriptor
bits: number of bits

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Sets the number of bits to read from an SPI device(ioctl SPI_IOC_RD_BITS_PER_WORD)

int setSPIBitOrder( int spi_fd
, int order)

Parameter:
spi_fd: SPI device's file descriptor
order: SPIEnum.MSBFIRST or SPIEnum.LSBFIRST

Return Value:
Returns 0 if it is a success otherwise returns a negative number

Sets either most significant bits(SPIEnum.MSBFIRST) or least significant bits(SPIEnum.LSBFIRST) to be transmitted first.

int setSPIClockDivider( int spi_fd
, int divider)

Parameter:
spi_fd: SPI device's file descriptor
divider: a clock divider which should be a value specified in SPIEnum.java and starts with "SPI_CLOCK_" e.g.
SPIEnum.SPI_CLOCK_DIV128

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Sets an SPI clock divider.

int setSPIDataMode( int spi_fd
, int mode)

Parameter:
spi_fd: SPI device's file descriptor
mode: SPI device's data mode. It can be any value among SPIEnum.SPI_MODE0 ~ SPIEnum.SPI_MODE3

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Sets SPI's data mode

int SPItransferOneByte( int spi_fd
, byte byteData
, int spi_delay
, int spi_speed
, int spi_bits)

Parameter:
spi_fd: SPI device's file descriptor
byteData: data to be written to an SPI device
spi_delay: delay time
spi_speed: transfer speed
spi_bits: number of bits

Return Value:
Returns read data if it is a success otherwise returns a negative number

Transfers and receives a byte simultaneously. e.g. int byteRet = SPItransferOneByte(spi_fd , 0xAA , 0 /*delay*/ , 500000/*speed*/ ,8/*bits*/)

int SPItransferBytes(int spi_fd
, byte[] writeData
, byte[] readBuff
, int spi_delay
, int spi_speed
, int spi_bits)

Parameter:
spi_fd: SPI device's file descriptor
writeData: data to be written to a device
readBuff: buffer to store read data
spi_delay: delay time
spi_speed: transfer speed
spi_bits: number of bits

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Transfers and receives multiple bytes simultaneously.

int writeBytesToSPI(int spi_fd
, byte[] writeData
, int spi_delay
, int spi_speed
, int spi_bits)

Parameter:
spi_fd: SPI device's file descriptor
writeData: data to be written to a device
spi_delay: delay time
spi_speed: transfer speed
spi_bits: number of bits

Return Value:
Returns 0 if it is a success otherwise returns a negative number

Writes multiple bytes to an SPI device.

int readBytesFromSPI(int spi_fd
, byte[] readBuff
, int spi_delay
, int spi_speed
, int spi_bits)

Parameter:
readBuff: buffer to store read data
spi_delay: delay time
spi_speed: transfer speed
spi_bits: number of bits

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Reads multiple bytes from an SPI device.


2.8 GPIO APIs

Applicable Platforms: All

API Name Parameters and Return Value Function
int exportGPIOPin(int pin)

Parameter:
pin: GPIO Pin

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Tells system to export a GPIO pin
It is identical to run "echo pin > /sys/class/gpio/export"

int unexportGPIOPin(int pin)

Parameter:
pin: GPIO Pin

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Tells system to unexport a GPIO pin
It is identical to run "echo pin > /sys/class/gpio/unexport"

int setGPIOValue(int pin, int value)

Parameter:
pin: GPIO Pin
value: it is either GPIOEnum.LOW which means a LOW level or GPIOEnum.HIGH which means a HIGH level

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Sets a HIGH or LOW to a GPIO pin

int getGPIOValue(int pin)

Parameter:
pin: GPIO Pin

Return Value:
Returns either GPIOEnum.LOW
or GPIOEnum.HIGH if it is a success otherwise returns a negative number.

Gets a GPIO pin's status(HIGH or LOW)

int setGPIODirection(int pin, int direction)

Parameter:
pin: GPIO Pin
direction: it is either GPIOEnum.IN which means the direction is INPUT
or GPIOEnum.OUT which means the direction is OUTPUT

Return Value:
Returns 0 if it is a success otherwise returns a negative number.

Sets a GPIO pin's direction.

int getGPIODirection(int pin)

Parameter:
pin: GPIO Pin

Returns either GPIOEnum.IN or

GPIOEnum.OUT if it is a success otherwise returns a negative number.

Gets a GPIO pin's direction(either OUTPUT or INPUT)

2.9 APIs for Checking Board Type

API Name Parameters and Return Value Function
int getBoardType()

Returns a board type which is a number and

is defined in BoardType.java if it is a success

otherwise returns a negative number.

Here is a code sample:

private int mBoardType = HardwareControler.getBoardType();
if (mBoardType == BoardType.NanoPC_T4) {
    // do something...
}

3 Code Samples

3.1 Android8.1

3.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

3.2 Android7.1.2

3.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

3.3 Earlier version of Android

3.3.1 Applicable Boards & OS

  • NanoPC-T3/NanoPi-M3: Android 5
  • Smart4418 SDK/SOM-4418/NanoPC-T2/NanoPi-M2/NanoPi S2: Android 5, Android 4.4
  • Tiny4412: Android 4.2, Android 5
  • Tiny210/Smart210/Mini210: Android 4.2
  • Tiny6410/Mini6410: Android 2.3
Earlier version of Android Demos
Serial Port

https://github.com/friendlyarm/friendlythings-examples/tree/master/SerialPortDemo

GPIO

https://github.com/friendlyarm/friendlythings-examples/tree/master/GPIO_LED_Demo

ADC

https://github.com/friendlyarm/friendlythings-examples/tree/master/ADCDemo

PWM

https://github.com/friendlyarm/friendlythings-examples/tree/master/PWMDemo

I2C

https://github.com/friendlyarm/friendlythings-examples/tree/master/I2C_LCD1602_Demo

SPI

https://github.com/friendlyarm/friendlythings-examples/tree/master/SPI_OLED_Demo