Atmel ARM-based Microcontrollers

chulavistajuniorΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

148 εμφανίσεις



Atmel

ARM
-
based
Microcontrollers

0
7

-

Android
NDK Development

ANDROID
TRAINING MANUAL

Prerequisites



Hardware Prerequisites



Atmel
®

SAM9M10
-
G45
-
EK

or SAM9X5
-
EK



USB
-
to
-
serial converter if your laptop does not have a serial interface




Software Prerequisites



Need install Ubuntu 11.04 in Windows





Estimated Completion Time
:
30

min
utes

Introduction

The Android NDK is a toolset that lets you embed components that make use of
native code in your Android applications.

In this
hands on we

will explain how to
use
NDK

to develop your own C
program
.



09/22
/2011





Atmel

ARM
-
based
:
09/22
/2011

Page
2

of
15

Table of Contents

Prerequisites

................................
................................
.....................
1

Introduction

................................
................................
.......................
1

Icon Key Identifiers

................................
................................
...........
3

1.

Assignments

................................
................................
..............
4

1.
1

What is the NDK?

................................
................................
................

4

1.2

When to develop in native code?

................................
.........................

4

1.3

How to develop in NDK?

................................
................................
......

4

1.4

How to build native code in NDK?

................................
.......................

7

1.5

Run native code on target board.

................................
........................

7

1.6

Develop native code to

control LED

................................
....................

8

Conclusion

................................
................................
......................

14

Resources
................................
................................
.......................

14

Atmel Technical Suppor
t Center

................................
.....................

14







Atmel

ARM
-
based
:
09/22
/2011

Page
3

of
15

Icon Key Identifiers

Icons are used to identify different assignment sections and reduce complexity.
These icons are:


Information

Delivers contextual information about a specific topic


Tips

Highlights useful t
ips and techniques


To Do List

Highlights objectives to be completed


Result

Highlights the expected result of an assignment step


Warning

Indicates important information


Execute

Highlights actions to be executed out of the target when
necessary






Atmel

ARM
-
based
:
09/22
/2011

Page
4

of
15

1.

Assignments

1.1

What is the NDK?

Android applications run in the Dalvik virtual machine. The NDK allows you to implement parts of your
applications using native
-
code languages such as C and C++. This can provide benefits to certain classes of
applications, in the form of

reuse of existing code and in some cases increased speed.

The NDK provides:



A set of tools and build files used to generate native code libraries from C and C++ sources



A way to embed the corresponding native libraries into an application package file (.a
pk) that can
be deployed on Android devices



A set of native system headers and libraries that will be supported in all future versions of the
Android platform, starting from Android 1.5. Applications that use native activities must be run on
Android 2.3 or

later.



Documentation, samples, and tutorials

The NDK provides stable headers for libc (the C library), libm (the Math library), OpenGL ES (3D graphics
library), the JNI interface, and other libraries
.

1.2

When to develop in native code?

The NDK will not benef
it most applications. As a developer, you need to balance its benefits against its
drawbacks; notably, using native code does not result in an automatic performance increase, but always
increases application complexity. In general, you should only use nati
ve code if it is essential to your
application, not just because you prefer to program in C/C++.


Typical good candidates for the NDK are self
-
contained, CPU
-
intensive operations that don't allocate much
memory, such as signal processing, physics simulatio
n, and so on. Simply re
-
coding a method to run in C
usually does not result in a large performance increase. When examining whether or not you should develop
in native code, think about your requirements and see if the Android framework APIs provide the fu
nctionality
that you need. The NDK can, however, can be an effective way to reuse a large corpus of existing C/C++
code.

1.3

How to develop in NDK?

Here's a detail overview of how you can develop native code with the Android NDK:


Create a Helloworld project to

start your NDK developing



Go to NDK’s sample directory, we will create our helloworld project in that directory

c
d ~/work/android
-
ndk
-
r7/samples



Create the project folder and a
directory

$PROJECT/jni/

m
kdir Helloworld && mkdir Helloworld/jni





Atmel

ARM
-
based
:
09/22
/2011

Page
5

of
15


Helloworld is

the project name
corresponds to the path of your Android application

project.


Life is easy. Write C code just as you did in
L
inux
.



Go
$PROJECT/jni/

folder which is put the source code

c
d Helloworld/jni



Open and edit the source code

gedit Helloworld.c &


Wr
ite code
like following picture shows

and save it
:



You are pretty free to organize the content of 'jni' as you want,

t
he directory names and
structure here will not influence the final

generated application packages, so you don't
have to use pseudo
-
uniqu
e names like

com.<mycompany>.<myproject>
as is the
case for application package names.


Writing an
Android.mk

build script:

gedit Android.mk &

An
Android.mk

file is written to describe your sources to the

build system.

The file is really a tiny GNU Makefil
e fragment that will be

parsed one or more times by the build system. As
such, you

should try to minimize the variables you declare there and

do
not assume that anything is not
defined during parsing.





Atmel

ARM
-
based
:
09/22
/2011

Page
6

of
15


Now the code like following picture shows:



Now, let's

explain these lines:



LOCAL_PATH := $(call my
-
dir)

An Android.mk file must begin with the definition of the LOCAL_PATH variable.

It is used to locate
source files in the development tree. In this example,

the macro function 'my
-
dir', provided by the build
system, is used to return the path of the current directory (i.e. the directory containing the Android.mk
file itself).




include $(CLEAR_VARS)

The CLEAR_VARS variable is provided by the build system and points to a

special GNU Makefile that
will clear many

LOCAL_XXX variables for you

(e.g. LOCAL_MODULE, LOCAL_SRC_FILES,
LOCAL_STATIC_LIBRARIES, etc...)

with the exception of LOCAL_PATH. This is needed because all
build

control files are parsed in a single GNU Make execution context where

all variables are glo
bal.




LOCAL_MODULE :=
Helloworld

The LOCAL_MODULE variable must be defined to identify each module you

describe in your
Android.mk. The name must be *unique* and not contain

any spaces. Note that the build system will
automatically add proper

prefix and su
ffix to the corresponding generated file. In other words,

a shared
library module named 'foo' will generate 'libfoo.so'
.




LOCAL_SRC_FILES :=
Helloworld
.c

The LOCAL_SRC_FILES variables must contain a list of C and/or C++ source

files that will be built and
assembled into a module. Note that you should

not list header and included files here, because the




Atmel

ARM
-
based
:
09/22
/2011

Page
7

of
15

build system will

compute dependencies automatically for you; just list the source files

that will be
passed directly to a compiler, and you should be good.




i
nclude $(BUILD_EXECUTABLE)

T
o generate an executable module, BUILD_EXECUTABLE
is not the NDK recommended one
, for
most
case,

it just show

you

how

native code run on ta
r
get.

1.4

How to build native code in NDK?

The Android NDK r4 introduced a new tiny s
hell
script, named 'ndk
-
build'
to simplify building
machine code.
The script is located at the top
-
level directory of the NDK, and shall be invoked from the command
-
line when
in your application project

directory, or any of its sub
-
directories.


Use
ndk
-
build

to

build your native code

~/work/android
-
ndk
-
r7/ndk
-
build


The native code is build

without

any error



The
executable module is locate under
libs/armeabi

cd ~/work/android
-
ndk
-
r7/samples/Helloworld/libs/armeabi && ls


Here we can see some new fo
l
der is set up

during building
:

$PROJECT/libs

and
$PROJECT/obj
.

Android NDK
introduced support for prebuilt libraries (shared and

static),
the ability to include and use, in your applications
.

A

copy of your prebuilt

shared
library will be copied into $PROJECT/obj/local
, and another will be

copied and stripped
into
$PROJECT/libs/<abi>
.

1.5

Run native code on target board.

We will use the adb command

push

to copy files to
an emulator/device instance's data file. Unlike the install
co
mmand, which only copy
file
s

to a specific
loca
tion, the
push

command

let you copy arbitrary directories
and files to any location in an emulator/device instance.


Start the adb server:




Go to

<
android_sdk_path/platform
-
tools
>,

cd ~/work/android
-
sdk
-
linux/platform
-
tools/



E
x
e
cute
adb kill
-
server

t
o kill the adb server





Atmel

ARM
-
based
:
09/22
/2011

Page
8

of
15

sudo ./adb kill
-
server



T
hen restart the adb server with
adb start
-
server


sudo ./adb start
-
server


Check whether we can find the Android device now

./a
db devices


Here we can see the Android device is found



Copy
executable module

to t
arget
/tmp

folder
:



Go to the folder that native code is built:

cd
~/work/android
-
ndk
-
r7/samples/Helloworld/libs/armeabi
/



Using adb tool to push the built executable file to
/tmp

in target board

~/work/
android
-
sdk
-
linux/platform
-
tools/adb push Helloworld /t
mp


If multiple emulator/device instances are running, you need to specify a target
instance
when issuing adb commands. To do

so, use the
-
s

option in the commands
.
For example:

if we
want to push the file

to device,

we

will

use:

~/work/android
-
sdk
-
linux/pl
atform
-
tools/
adb

s 0123456789ABCDEF push Helloworld /tmp


Starting from now, you will have commands to type on the target board side by using
your serial console terminal: PuTTY. They will be represented using the icon:


Open P
u
TTY
,

run Helloworl
d
and we will see output.

/tmp
/Helloworld



1.6

Develop native code to control LED

We will step further t
o write a C code to control LED,

you will find life is easier when complete former step.





Atmel

ARM
-
based
:
09/22
/2011

Page
9

of
15


Create a led_control project and edit
your native sources under th
e
$PROJECT/jni/



Go to NDK’s sample directory, we will create our helloworld project in that directory

c
d ~/work/android
-
ndk
-
r7/samples



Create the project folder and a
directory

$PROJECT/jni/

m
kdir
led_control && mkdir led_control
/jni



Go to the
jni

folder

c
d led_control/jni


Add header files



Open the source file

gedit led_control.c &



Add following header file in the file:


It will look like following:



For all the
led_contorl
’s source code you can take the file in USB key as reference.
All the source code is
available in
/media/KINGSTON/03_Android_Training/

Source_code/android_app/full/led_control
/


If you use above reference code y
ou need uncomment the macro according to your
target board type, otherwise you will get compiling errors.






Atmel

ARM
-
based
:
09/22
/2011

Page
10

of
15


Add control LED function

(open the LED device file and write
0/1

in it to control LED)



Add turn on and turn off LED function






Atmel

ARM
-
based
:
09/22
/2011

Page
11

of
15


Add macro definition

of device file and LED status
, which

depend
s

on the board

type



For

SAM9M
10
-
G45
-
EK:




For

SAM9X5
-
EK






Atmel

ARM
-
based
:
09/22
/2011

Page
12

of
15


Add main function



Writing

an
Android.mk

build script:

gedit Android.mk &


Build script l
ike following:






Atmel

ARM
-
based
:
09/22
/2011

Page
13

of
15


Use ndk
-
build to build your native code

~/work/android
-
ndk
-
r7/ndk
-
build


Copy
executable module

to target
/tmp

folder
:



Go to the folder that native code is built:

cd
~/work/andro
id
-
ndk
-
r7/samples/
led_control
/libs/armeabi
/



Using adb tool to push the built executable file to
/tmp

in target board

~/work/
android
-
sdk
-
linux/platform
-
tools/adb push led_control /tmp


Starting from now, you will have commands to type on the target board si
de by using
your serial console terminal: PuTTY. They will be represented using the icon:


Open PuTTY
,

run
led_control

and we will see
led turn on and turn off
.

/tmp/
led_control


You can change

the main function to add a loop to blink the LED.














LED to be controlled


SAM9X5
-
EK

SAM9
M10
-
G4
5
-
EK





Atmel

ARM
-
based
:
09/22
/2011

Page
14

of
15

Conclusion

You
now
know
how to
use NDK to develop your own C
program
.

It is just like develop
in Linux.

Resources

http://www.atmel.com/

http://www.linux4sam.org/

http://www.at91.com/android4sam/

Atmel Technical Support Center

Atmel has several support channels available.

We encourage you to register and use our web portal for several reasons:



All your requests are

managed in one place. It is easy to submit a new
request and get the status of pending requests.



FAQ Access. We provide a large FAQ database through our website.



You can subscribe to free newsletters about Atmel ARM
-
based
microcontrollers.


Support channe
ls:



Go to
http://support.atmel.no/

for
support

for a
ll Atmel microcontrollers
.



Contact
at91@atmel.com

for
support

for all Atmel ARM
-
based
microcontrollers.
































Atmel Corporation

2325 Orchard Parkway

San Jose, CA 95131

USA

Tel:

(+1)(408) 441
-
0311

Fax:

(+1)(408) 487
-
2600

www.atmel.com



Atmel Asia Limited

Unit 01
-
5 & 16, 19F

BEA Tower, Millennium City 5

418 Kwun Tong Roa
d

Kwun Tong, Kowloon

HONG KONG

Tel:

(+852) 2245
-
6100

Fax:

(+852) 2722
-
1369

Atmel Munich GmbH

Business Campus

Parkring 4

D
-
85748 Garching b. Munich

GERMANY

Tel:

(+49) 89
-
31970
-
0

Fax:

(+49) 89
-
3194621

Atmel Japan

9F, Tonetsu Shinkawa Bldg.

1
-
24
-
8 Shinkawa

Ch
uo
-
ku, Tokyo 104
-
0033

JAPAN

Tel:


(+81)(3) 3523
-
3551

Fax:


(+81)(3) 3523
-
7581





© 2011 Atmel Corporation. All rights reserved. / Rev.:



Atmel

ARM
-
based
:
09/22
/2011

Atmel
®
, logo and combinations thereof, and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries.

Other terms and product
names may be trademarks of others.

Disclaimer: The information in this docum
ent is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellect
ual property right is granted by
this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL T
ERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL
ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING,
BUT NOT LIMITED TO, THE
IMPLIED WARRANTY OF MERCHANTABILITY, FITNE
SS FOR A PARTICULAR PURPOSE, OR NON
-
INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS

INTERRUPTION, OR LOS
S OF
INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF S
UCH DAMAGES. Atmel makes no
representations or warranties with respect to the accuracy or completeness of the contents of this

document and reserves the right to make changes to specifications and products descriptions at
any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically p
rovided otherwise, Atmel products a
re not suitable for, and shall not
be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in appli
cations intended to support or sustain life.