You are on page 1of 53

4412 Android Deveopment

REVISION

ORIGINATOR

0.1.0

Guangzhou FriendlyARM Co., Ltd

SCR

Guangzhou FriendlyARM Computer Tech Co., Ltd Confidential:


This document and information contained in it shall not be reproduced
by, used by, or disclosed to others except as expressly authorized in
writing by Guangzhou FriendlyARM Co., Ltd.

REV DATE
April 24, 2014

Guangzhou FriendlyARM
Computer Tech Co., Ltd
Guangzhou, China

Copyright@2013

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-1-

COPYRIGHT STATEMENT
The content (content being images, text, programs and scripts) of this English
manual is copyright Guangzhou FriendlyARM Computer Tech Co., Ltd. All
rights expressly reserved.
Any content of the manual printed or downloaded may not be sold, licensed,
transferred, copied or reproduced in whole or in part in any manner or in or on
any media to any person without the prior written consent of Guangzhou
FriendlyARM Computer Tech Co., Ltd including but not limited to:
transmission by any method
storage in any medium, system or program
display in any form
performance
hire, lease, rental or loan
Requests for permission to reproduce material from this manual should be
addressed to Guangzhou FriendlyARM Computer Tech Co., Ltd.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-2-

Index
1 INTRODUCTION................................................................................................................................................... - 5 2 SET UP ANDROID DEVELOPMENT ENVIRONMENT ............................................................................................ - 5 2.1 INSTALL JDK (JAVA SE DEVELOPMENT KIT) ........................................................................................ - 5 2.2. INSTALL ADT AND ANDROID SDK......................................................................................................... - 8 2.2.1 Download Android SDK .................................................................................................................. - 9 2.2.2 Start ADT Development Environment (Android Developer Tools) ........................................... - 10 2.3 SET UP ANDROID SIMULATOR ............................................................................................................... - 12 2.4 CREATE ANDROID APPLICATION .......................................................................................................... - 14 2.4.1 Create HelloWorld ..................................................................................................................... - 14 2.4.2 Run Android Application in Simulator ........................................................................................ - 16 2.5 SET UP TINY4412 DEBUG ENVIRONMENT ............................................................................................ - 18 2.5.1 Install USB ADB Driver................................................................................................................. - 18 2.5.2 ADB Testing .................................................................................................................................... - 19 2.5.2.1 Add ADB to PATH............................................................................................................... - 19 2.5.2.2 Testing ADB Functions ....................................................................................................... - 21 2.5.3 Run Applications on Tiny4412 in ADB......................................................................................... - 21 2.5.4 Debug Android Applications on Tiny4412 ................................................................................... - 24 3 ACCESS AND CONTROL HARDWARE COMPONENTS........................................................................................ - 24 3.1 HOW TO USE LIBRARY(LIBFRIENDLYARM-HARDWARE.SO) ................................................................ - 25 3.2 LIBRARY (LIBFRIENDLYARM-HARDWARE.SO) API .............................................................................. - 27 3.2.1 General File Operation APIs ......................................................................................................... - 27 3.2.2 Serial Port APIs .............................................................................................................................. - 29 3.2.3 LED APIs ........................................................................................................................................ - 31 3.2.4 PWM APIs ...................................................................................................................................... - 31 3.2.5 ADC APIs ........................................................................................................................................ - 32 3.2.6 I2C APIs .......................................................................................................................................... - 33 3.2.7 SPI APIs .......................................................................................................................................... - 35 3.2.8 GPIO APIs ...................................................................................................................................... - 39 3.3 CODE SAMPLES ..................................................................................................................................... - 43 3.3.1 LED ................................................................................................................................................. - 43 3.3.2 GPIO ............................................................................................................................................... - 43 3.3.2.1 Run Adroids Application as System................................................................................... - 43 3.3.2.2 Disable Kernel LED Driver ................................................................................................ - 45 3.3.2.3 Get GPIO LED Pin ............................................................................................................. - 45 3.3.2.4 Control LED via GPIO ....................................................................................................... - 46 3.3.3 Serial Port Communication ........................................................................................................... - 48 Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-3-

3.3.4 PWM ............................................................................................................................................... - 49 3.3.5 A/D Conversion .............................................................................................................................. - 50 3.3.6 I2C & EEPROM ............................................................................................................................ - 51 3.4 IMPORT PROJECT IN ADT..................................................................................................................... - 52 -

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-4-

1 Introduction
This document is created based on the Tiny4412 board and gives basic ideas on how
to develop applications on Android. Although it was created for the Tiny4412 it is a
good reference for all our boards including 4412, NanoPC, 210 and 6410 that can run
Android.

2 Set up Android Development Environment


In this chapter we will introduce how to set up the Android development environment
on Windows 7 (both 32bit and 64 bit work).

2.1 Install JDK (Java SE Development Kit)


Please go to http://www.oracle.com/technetwork/java/javase/downloads/index.html
and click on JDK

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-5-

On the pop-up window please check the Accept License Agreement box

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-6-

Please select the corresponding JDK. Windows 7 64bit corresponds to


jdk-7u51-windows-x64.exe. After download it double click to install.
After installation is done please add the JDK commands to the Path environment
variable.
After add the JDK commands to PATH you can verify it by commanding
java-version in the DOS commandline utility:

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-7-

2.2. Install ADT and Android SDK


Please go to http://developer.android.com/sdk/ to get the latest SDK.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-8-

You can select either 32 bit or 64 bit suitable to your system.


After download the package please uncompress it and you will find the following
files:

2.2.1 Download Android SDK


Our Android is 4.2.2 therefore users need to download Android4.2.2 SDK. Please
double click on SDK Manager and select Android 4.2.2(API 17) SDK Platform and
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

-9-

ARM EABI v7a System Image.

On the window please click on Install 4 packages and check the Accept License
option on the Choose Packages to Install dialog and click on Install.

2.2.2 Start ADT Development Environment (Android Developer Tools)


Please go to the adt-bundle-windows-x86_64 directory and then the eclipse
directory, double click on eclipse.exe to start ADT.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 10 -

When you start it you will be asked to set the workspace path where you will save
your projects source code. After finish you will see the following window

Actually ADT is built with Eclipse and is Eclipse + Android + SDK + ADT Plugin. In
the following sections we will use ADT
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 11 -

2.3 Set up Android Simulator


In ADT, click on WIndows and then Android Virtual Device Manager to start the
simulator. Click on New and the Create new Android Virtual Device(AVD)
window will pop up.
On this window you can make your own configurations:

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 12 -

Click on OK to complete it and you will find the simulator

Select a simulator and click on Start and Launch on the Launch Option dialog

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 13 -

2.4 Create Android Application


2.4.1 Create HelloWorld
We will create a HelloWorld to verify whether the ADT is set up correctly or not.
Please go to File -> New -> Android Application Project

After the New Android Project dialog pops up please type the following info
1) Application name: HelloWorld
2) Project name: HelloWorld
3) Package name: com.example.helloworld
4) Build target: API 17: Android 4.2(Jelly Bean)
5) Target SDK: API 17: Android 4.2(Jelly Bean)

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 14 -

Click on Next all the way till Finish to complete the project.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 15 -

2.4.2 Run Android Application in Simulator


To compile and run the HelloWorld please select the project in Package Explorer
and run it via Run -> Run As -> Android Application then ADT will start the
Android Simulator automatically and will run the HelloWorld.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 16 -

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 17 -

2.5 Set up Tiny4412 Debug Environment


2.5.1 Install USB ADB Driver
On the ADB window please click on Window -> Android SDK Manager to start
SDK Manager. You can check the Google USB Driver status from Extras. If it is
not Installed then please check Google USB Driver and click Install 1 package

After the installation is done please turn on the Tiny4412. After Android is fully
loaded please connect the board to a PC via a USB cable and follow the Windows 7
prompt to install the USB driver.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 18 -

If the installation fails please go to Device Manager and right click on Tiny4412
to update its driver.

2.5.2 ADB Testing


2.5.2.1 Add ADB to PATH
Please add the ADB directory to the PATH variable.
1) Right click on My Computer -> Properties and select Advanced
2) Click on Environmental Variables
3) Double click on the PATH variable from System Variables and add the directory of
adt-bundle to it.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 19 -

To test ADB commands please lauch the Windows command line utility, type adb
and enter. If you observe the following messages you will be able to work with ADB
successfully.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 20 -

2.5.2.2 Testing ADB Functions


1) Check Device Status
To check a device status please type #adb devices. If you observe the following
messages it means the Tiny4412 is connected successfully.

2) Enter ADB Shell


To enter the ADB shell please type #adb shell. You can type exit to return to
DOS.

3) Install Software in ADB


For instance we have a software utility in D:\sinaweibo_2.0.4.apk we can type adb
install D:\sinaweibo_2.0.4.apk to install it.

2.5.3 Run Applications on Tiny4412 in ADB


Double click on eclipse/eclipse.exe to start ADT and the HelloWorld will be
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 21 -

automatically opened.
Please go to Package Explore, right click on HelloWorld and select Propertities,
click on Run/Debug Settings, select HelloWorld and click on Edit

On the Edit Configuration window click on Target and check Always prompt to
pick device on the Deployment Target Selection Mode tab

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 22 -

Click on OK to save and exit.


Now you can run the HelloWorld on your Tiny4412. Please select the HelloWorld
project and click on the Run button on the tools bar. On the Android Device Chooser
dialog select Coose a running Android device and select the 4.2.2 device (Tiny4412)
from Target

A moment later HelloWorld will run on your Tiny4412

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 23 -

When ADT asks whether you want to trace logcat messages you can choose Yes
and select debug

2.5.4 Debug Android Applications on Tiny4412


To debug an application on the Tiny4412 you need to stop it if it is running. Please go
to Open Perspective, click on Debug, add a break point and hit Run -> Debug.

3 Access and Control Hardware Components


In this chapter we provide some code samples on how to develop applications
accessing hardware components.
We developed a libfriendlyarm-hardware.so which is the basis for all our hardware
control applications.
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 24 -

3.1 How to Use Library(libfriendlyarm-hardware.so)


We have two DVDs that come with the Tiny4412 package. In the second DVD s
tools\libfriendlyarm-hardware there are all necessary .so files and JAVA JNIs

In our Android souce code package the libfriendlyarm-hardware.so file is located at


vendor/friendly-arm/exynos4412/rootdir/system/lib/libfriendlyarm-hardware.so.

In

the Android system it is under /system/lib/libfriendlyarm-hardware.so.


You can use the libfriendlyarm-hardware.so in the following ways:
1) Go to your applications directory and create a libs directory. Enter this libs
directory, create an armeabi directory and copy the libfriendlyarm-hardware.so file
to this directory.
2) Go back to your applications directory, enter the src directory and create
com\friendlyarm\AndroidSDK
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 25 -

.java

files

under

tools/libfriendlyarm-hardware/java/com/friendlyarm/AndroidSDK

to

your

3) Please

open

DVD

and

copy

all

the

src\com\friendlyarm\AndroidSDK
After all these steps are done please start ADT, select your project on the left and
refresh it you will see the list shown below

All the major interfaces are in HardwareControler. You need to import them before
you can use them
import com.friendlyarm.AndroidSDK.HardwareControler;
import com.friendlyarm.AndroidSDK.SPIEnum;
import com.friendlyarm.AndroidSDK.GPIOEnum;
import com.friendlyarm.AndroidSDK.FileCtlEnum;

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 26 -

3.2 Library (libfriendlyarm-hardware.so) API


You can call APIs in the libfriendlyarm-hardware.so via HardwareControler.

3.2.1 General File Operation APIs


API Name

Parameter and Return Value

int open(string devName, devName: device name


int flags)

Comment
Open device

flags: access mode including


read, write etc. Please refer
to FileCtlEnum.java

Return value: if it succeeds it


will return an file descriptor
otherwise it will return -1
Int ioctlWithIntValue(int fd, fd: file descriptor;
int cmd, int value)

ioctl operation

cmd: ioctl command


value: command parameters ;
Return value: if it succeeds it
will return 0 otherwise it will

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 27 -

return -1
int write (int fd, byte[] data) fd: file descriptor;

Write data to a device

data: data to be written to the which is opened.


device specified by fd;
Return value: if it succeeds it
will return the number of
bytes otherwise it will return
-1
int read (int fd, byte [] buf, fd: file descriptor;

Read data from a device

int len)

or file

buf: data buffer


len: number of bytes;
Return value: if it succeeds it
will return the number of
bytes otherwise it will return
-1. If before this function is
called the file pointer already
points to the end of the file
this

read

operation

will

return 0

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 28 -

int select (int fd, int sec, int fd: file descriptor;

Check

usec)

which is opened has data

sec: seconds of wait time;

whether

file

usec: nanoseconds of wait


time;
Return value: if the selected
file has data it will return 1
otherwise it will return -1. If
there are no data it will
return 0

void close (int fd)

fd: file descriptor;

Close a device

return value: no

3.2.2 Serial Port APIs


API Name

Parameter and Return Value

Comment

int

devName: serial port name, options Open

openSerialPortEx

include:

serial

port

device

(String devName, /dev/s3c2410_serial1,

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 29 -

long

baud,

int /dev/s3c2410_serial2,

dataBits,

int

stopBits,

String

parityBit,

String

/dev/s3c2410_serial3,
/dev/ttyUSB0,

flowCtrl)

/dev/ttyUSB1,
/dev/ttyUSB2,
/dev/ttyUSB3,
baud: baud rate,
dataBits: data bits,
stopBits: stop bits
parityBit: parity bit (0 means even, E
means odd and N means no parity bit),
flowCtrl:

flow

control(H

means

hardware control, S means software


control and N means no flow control),
Return value: if it succeeds it will return
the devices file descriptor otherwise it
will return -1
Usually you call openSerialPortEx to open a serial device and then call timer or
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 30 -

select to inquiry whether there are data coming. If the deivce receives data you can call
read to read them. You can call write to write data to a serial device. After
operations are done please call close to close it.

3.2.3 LED APIs


API Name

Parameter and Return Value Comment

int setLedState (int ledID, ledID: ID of an LED which Turn on / off an LED
int ledState)

will be operated,
ledState: 1 means on and 0
mean off,
Return value: if it succeeds
it will return 0 otherwise it
will return -1

3.2.4 PWM APIs


API Name
Int

PWMPlay

frequency)

Parameter and Return Value Comment


(int frequency: PWMs sound Play PWM at a specified
frequency,

frequency

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 31 -

Return value: if it succeeds


it will return 0 otherwise it
will return -1
int PWMStop ()

Return value: if it succeeds Stop PWM


it will return 0 otherwise it
will return -1

3.2.5 ADC APIs


API Name

Parameter and Return Value Comment

int readADC ()

Return value: if it fails it Read the first channels


will return -1

int

readADCWithChannel channel:

(int channel)

specified

ADC result
ADC Read

channel

specified

ADC

channels result

Return value: if it fails it


will return -1
int

[] channels: specified ADC Read

readADCWithChannels (int channels


[] channels)

multiple

ADC

channels results

Return value: if it succeeds

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 32 -

it will return those ADCs


results otherwise it will
return NULL

3.2.6 I2C APIs


Before call the following APIs you need to open an I2C device.
int fd=HardwareControler.open(/dev/i2c-0, FileCtlEnum.O_RDWR);
API Name

Parameter and Return Value Comment

int setI2CSlave(int fd, int fd: I2C device descriptor


slave);

Set I2C address. EEPROM

slave: I2C device address. is 0x50


Usually EEPROM is 0x50
Return value: if it succeeds
it will return 0 otherwise it
will return -1.

int setI2CTimeout (int fd, fd: I2C device descriptor

Set

int timeout)

I2C_TIMEOUT)

timeout: time out

timeout

(ioctl

Return value: if it succeeds


it will return 0 otherwise it

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 33 -

will return -1
int setI2CRetries (int fd, int fd: I2C device descriptor,

Set retry number (ioctl

retries)

I2C_RETRIES)

retries: retry number,


Return value: if it succeeds
if will return 0 otherwise it
will return -1

int writeByteToI2C (int fd, fd: I2C device descriptor,

Write one byte to an I2C

int pos, byte byteData, int pos: byte position,

device and wait a time

wait_ms)

period

byteData: data to be written


to the device,
wait_ms: wait time in mini
seconds,
Return value: if it succeeds
it will return 0 otherwise it
will return -1

int readByteFromI2C (int fd: I2C device descriptor,

Read one byte from a

fd, int pos, int wait_ms)

specified position and wait

pos: byte position,


wait_ms:

wait

time

in

a time period in mini


seconds

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 34 -

mini-seconds,
If it succeeds if will return
0 otherwise it will return -1

3.2.7 SPI APIs


Before call the following APIs you need to open an SPI device.
int spi_fd=HardwareControler.open(/dev/spidev1.0, FileCtlEnum.O_RDWR);
API Name

Parameter and Return Value

Comment

int

spi_fd: SPI device descriptor, Set the number of bits per word

setSPIWriteBitsPerW bits: number of bits per word, to write to an SPI device. If


ord (int spi_fd, int
bits )
;

Return value: if it succeeds it


will return 0 otherwise it will
return a negative number.

bits=0 it will set the number of


bits

to

(ioctl

SPI_IOC_WR_BITS_PER_WO
RD)

int

spi_fd: SPI device descriptor, Set the number of bits per word

setSPIReadBitsPerW

bits: number of bits per word, to read from an SPI device.

ord (int spi_fd, int


bits )

Return value: if it succeeds it

(ioctl
SPI_IOC_RD_BITS_PER_WO

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 35 -

will return 0 otherwise it will RD)


return a negative number.
int

setSPIBitOrder spi_fd: SPI device descriptor, Set transmission order to either

(int spi_fd, int order)

MSB first or LSB first.

order:
SPIEnum.MSBFIRST

or

SPIEnum.LSBFIRST,
Return value: if it succeeds it
will return 0 otherwise it will
return a negative number.
int

spi_fd: SPI device descriptor, Set SPI clock divider

setSPIClockDivider

divider: clock divider such as

(int

spi_fd,

int SPIEnum.SPI_CLOCK_DIV

divider)

128,
Return value: if it succeeds it
will return 0 otherwise it will
return a negative number.

int

setSPIDataMode spi_fd: SPI device descriptor, Set SPI device mode

(int spi_fd, int mode)

mode: SPI device mode, the


options

include

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 36 -

SPIEnum.SPI_MODE0

SPIEnum.SPI_MODE3,
Return value: if it succeeds it
will return 0 otherwise it will
return a negative number.
int

spi_fd: SPI device descriptor, Transfer one Byte

SPItransferOneByte

byteData: data to be written

(int

spi_fd,

byte to an SPI device,

byteData,

int

spi_delay,

int

spi_speed,

int

spi_delay: delay time,


spi_speed:

transmission

speed,
spi_bits)
spi_bits: number of bits per
word,
Return value: if it succeeds it
will return 0 otherwise it will
return a negative number.
int
(int

SPItransferBytes spi_fd: SPI device descriptor, Transfer multiple bytes of data


spi_fd,

writeData,

byte[] writeData: data to be written


byte[] to an SPI device,

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 37 -

readBuff,

int readBuff: buffer to store data

spi_delay,

int

spi_speed,

int

spi_delay: delay time,


spi_speed:

transmission

spi_bits)
speed,
spi_bits: number of bits per
word,
Return value: if it succeeds it
will return 0 otherwise it will
return a negative number.
int writeBytesToSPI spi_fd: SPI device descriptor, Write multiple bytes of data to
(int

spi_fd,

byte[] writeData: data to be written an SPI device

writeData,

int to an SPI device,

spi_delay,

int

spi_speed,

int

spi_delay: delay time,


spi_speed:

transmission

spi_bits)
speed,
spi_bits: number of bits per
word,
Return value: if it succeeds it

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 38 -

will return 0 otherwise it will


return a negative number.
int

spi_fd: SPI device descriptor, Read multiple bytes of data

readBytesFromSPI

readBuff: buffer to store data, from an SPI device

(int

spi_fd,

byte[]

readBuff,

int

spi_delay,

int

spi_speed,

int

spi_delay: delay time,


spi_speed:

transmission

speed,
spi_bits)

spi_bits: number of bits per


word,
Return value: if it succeeds it
will return 0 otherwise it will
return a negative number.

3.2.8 GPIO APIs


In our system when users call libfriendlyarm-hardware.so to access GPIOs actually
the system calls GPIO APIs in sysfs. Therefore Android applications need to run as
system. There are two ways of running Android applications as system.
1) Compile an application and specify system signature
2) Compile with ADT and add system signature in apk
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 39 -

API Name

Parameter

and Comment

Return Value
int

exportGPIOPin

pin)

(int pin:

GPIO

pin Export a GPIO pin specified by


pin. It equals running command

number,

Return value: if it echo pin > /sys/class/gpio/export


succeeds

it

will

return 0 otherwise it
will

return

negative number.
int unexportGPIOPin (int pin:
pin)

GPIO

pin Unexport a GPIO pin specified by


pin. It equals running command

number,

Return value: if it echo


succeeds

it

pin

>

will /sys/class/gpio/unexport

return 0 otherwise it
will

return

negative number.
int setGPIOValue (int pin, pin:
int value)

GPIO

pin Set a GPIO High or Low

number,
value:

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 40 -

GPIOEnum.LOW or
GPIOEnum.HIGH,
Return value: if it
succeeds

it

will

return 0 otherwise it
will

return

negative number.
int getGPIOValue (int pin) pin:

GPIO

pin Check the status of a GPIO pin

number,
Return value: if it
succeeds

it

return

will
either

GPIOEnum.LOW or
GPIOEnum.HIGH
otherwise

it

return

negative

will

number.
int setGPIODirection (int pin:
pin, int direction)

GPIO

pin Set a GPIO pin to Input or Output

number,

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 41 -

direction:
GPIOEnum.IN

or

GPIOEnum.OUT
Return value: if it
succeeds

it

will

return 0 otherwise it
will

return

negative number.
int getGPIODirection (int pin:
pin)

GPIO

pin Check the direction of a GPIO pin

number,
Return value: if it
succeeds

it

return

will
either

GPIOEnum.IN

or

GPIOEnum.OUT
otherwise

it

return

negative

will

number.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 42 -

3.3 Code Samples


3.3.1 LED
Users can call HardwareControler.setLedState to set an LED
import com.friendlyarm.AndroidSDK.HardwareControler;
//Turn on LED1:
HardwareControler.setLedState(0,1);
//Turn off LED1:
HardwareControler.setLedState(0,0);

This code sample only applies to onboard LEDs.

3.3.2 GPIO
This code sample can work with external LEDs. It directly accesses LEDs via GPIOs
without accessing LED drivers. It requires disabling Linux kernels LED driver first and
then runs this code

3.3.2.1 Run Adroids Application as System


In our system when users call libfriendlyarm-hardware.so to access GPIOs actually
the system calls GPIO APIs in sysfs. Therefore Android applications need to run as
system. There are two means of running Android applications as system.
1) Compile an application and specify system signature
2) Compile with ADT and add system signature in apk
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 43 -

We take the first one: compile our code in Android source code directory and put the
GPIO_LED_Demo directory to packages/apps. For instance if you have mounted
Tiny4412 DVD A to /mnt/iso please follow the steps below.
Go to Android 4.2.2 source code directory
# cd /opt/FriendlyARM/tiny4412/android-4.2.2_r1/
Set Android environment variables
# . setenv
Copy GPIO_LED_Demo to packages/apps
# cd packages/apps
# cp /mnt/iso/Android/examples/GPIO_LED_Demo . a
Run mm to compile the project
#cd GPIO_LED_Demo
# mm

The compiled apk file is out/target/product/tiny4412/system/app/GPIODemo.apk.


We use adb to install and run it
# adb install r out/target/product/tiny4412/system/app/GPIODemo.apk

Please note the following two points:


1) A projects AndroidManifest.xml needs to be specified as : android:sharedUserId =
android.uid.system
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.friendlyarm.GPIODemo"
android:sharedUserId="android.uid.system"
android:versionCode="1"
android:versionName="1.0">

2) You need to add one line LOCAL_CERTIFICATE:|=platform in Android.mk.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 44 -

3.3.2.2 Disable Kernel LED Driver


To run this application you need to disable the kernels LED driver.
You need to configure Androids kernel by unchecking LED Support for
FriendlyARM Tiny4412 GPIO LEDs in Device Drivers -> Character devices and
recompile the kernel.

3.3.2.3 Get GPIO LED Pin


To access a GPIO you need to get its pin specification. You can get these details from
the CPU boards schematics.

LED 1~ 4 correspond to GMP4 (0 ~ 3)


We need to get the pin numbers in the kernel. You can get them by following the
steps below:
1) Connect your Tiny4412 to a PC via a (crossover) serial cable and enter Android
2) In Hyperterminal ( or Minicom) please run the commands below
# cd /sys/class/gpio
# for i in gpiochip* ; do echo `cat $i/label`: `cat $i/base` ; done

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 45 -

GPM4 corresponds to 79 and each LED has an offset therefore LED1 is 79 + 0 (79)
and LED 4 is 79 + 3 (82)

3.3.2.4 Control LED via GPIO


After we get LED kernel IDs we need to call exportGPIOPin to export pins file
system nodes (echo pin > /sys/class/gpio/export). Since the nodes generation has some
time delay you need to add a timer before the next statement.
for (int i = 0; i <4; i++) {
int pin = 79 + i;
HardwareControler.exportGPIOPin(pin);
}
Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 46 -

step = STEP_INIT_GPIO_DIRECTION;
timer.schedule(init_task, 100, 100);

100 mini-seconds later pins will be set to OUTPUT and HIGH


private TimerTask init_task = new TimerTask() {
publicvoid run() {
Log.d(TAG, "init_task " + step);
if (step == STEP_INIT_GPIO_DIRECTION) {
for (int i=0; i<4; i++) {
if (HardwareControler.setGPIODirection(79+i, GPIOEnum.OUT) == 0) {
} else {
Log.v("TimerTask", "setGPIODirection failed");
}
}
step ++;
} elseif (step == STEP_CLOSE_ALL_LED) {
for (int i=0; i<4; i++) {
if (HardwareControler.setGPIOValue(79+i, GPIOEnum.HIGH) == 0) {
} else {
Log.v(TAG, "setGPIOValue failed");
}
}
step ++;
} elseif (step == STEP_INIT_VIEW) {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}
};

When users click on the CheckBox the corresponding LED pin will be set to
OUTPUT and HIGH
HardwareControler.setGPIOValue(79+led.code,led.isSelected() ? GPIOEnum.LOW:GPIOEnum.HIGH)

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 47 -

3.3.3 Serial Port Communication


This code sample shows how to start serial port communications
devfd = HardwareControler.openSerialPort( devName, speed, dataBits, stopBits );

If users want to set flow control and parity bit you can call the following function
devfd = HardwareControler.openSerialPortEx( devName, speed, dataBits, stopBits, parityBit, flowCtrl );

During serial port communications we need to poll a serial port and check whether it
receives data and needs to show them if there are any. We add a timer such that the
function will check a serial port every 500 ms.
private final int BUFSIZE = 512;
private byte[] buf = new byte[BUFSIZE];
if (HardwareControler.select(devfd, 0, 0) == 1) {
int retSize = HardwareControler.read(devfd, buf, BUFSIZE);

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 48 -

String str = new String(buf, 0, retSize);


}

When writing data to a serial port you can call HardwareControler.write:


String str = toEditor.getText().toString();
if (str.length() > 0) {
ret = HardwareControler.write(devfd, str.getBytes());
}

3.3.4 PWM
This code sample shows how the PWM beeper will be called to beep
HardwareControler.PWMPlay(1000);

To stop the PWM you can call


HardwareControler.PWMStop();

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 49 -

3.3.5 A/D Conversion


This code sample shows how the 4412 A/D conversion works
int[] channels = {0,1,2,3};
int[] adc = HardwareControler.readADCWithChannels(channels);

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 50 -

3.3.6 I2C & EEPROM


This code sample shows how the 4412 I2C EEPROM works. The code sample firstly
opens /dev/i2c-0 and then sets EEPROMs address to 0x50 and writes a byte to
EEPROM by calling writeByteToI2C or reads a byte from EEPROM by calling
readByteFromI2C
devFD = HardwareControler.open("/dev/i2c-0", FileCtlEnum.O_RDWR);
HardwareControler.setI2CSlave(devFD, 0x50);
HardwareControler.writeByteToI2C(devFD, 0, 0x20, 10);
byte ret = HardwareControler.readByteFromI2C(devFD, 0, 10);

The 4412s EEPROM can store up to 256 bytes therefore the range of the read/write
position is 0 ~ 255. Each time it can only read/write one byte and there is a time interval
between two read/write operations. Usually the time interval is 10ms which is specified
in writeByteToI2C.

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 51 -

3.4 Import Project in ADT


Lets take PWMDemo as an example. Please copy the PWMDemo project to your
PC. Start ADT and go to File -> Import and select Existing Android Code Into
Workspace

Select PWMDemo and click on Finish

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 52 -

Address: Room 1705,Block A1, Longyuan Plaza, Longkouxi Road, Guangzhou, China, 510640
Website: http://www.arm9.net
Sales:
+86-20-85201025
Tech Support: +86-13719442657
Fax:
+86-20-85261505
Email for Business and Cooperation: capbily@163.com
Email for Tech Support: dev_friendlyarm@163.com

- 53 -