You are on page 1of 24

Android on Beaglebone Black

Android on BeagleBone Black UTN-FRA Laboratorio de Software Libre Ernesto Gigliotti

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Contents Tutorial !" Creating an S

car! An!roi! Te"as #nstr$ments #mage for %eagle&one %lac'.

Tutorial #" Accessing the An!roi! Console &( A %. Tutorial $" Controlling )*#+s o,er An!roi! +perating S(stem. Tutorial %" Creating a &oot script for har!ware initiali-ation. Tutorial &" Compiling Te"as #nstr$ments An!roi! 'ernel from so$rce. Tutorial '" #.C mo!$les. /"ample with L012 temperat$re sensor. Tutorial (" UART1 an! UART. mo!$les. Tutorial )" S*# mo!$le. Tutorial *" Compiling a c$stom An!roi! !ri,er as a char !e,ice. Tutorial ! " Accessing to har!ware from c$stom An!roi! !ri,er.

E+a,-les" .tt-s"//sourceforge0net/-ro1ects/androidonbeaglebonebtutorials

License

T.is work is licensed under t.e Creati2e Co,,ons Attribution-S.areAlike %0 3nternational License0 To 2iew a co-4 of t.is license5 2isit .tt-"//creati2eco,,ons0org/licenses/b4-sa/%0 /deed0en6US0 7Android on Beaglebone Black8 b4 Ernesto Gigliotti0 Contact " ernestogigliotti9g,ail0co,

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Tutorial

!" Creating S

car! An!roi! Te"as #nstr$ments #mage for %eagle&one %lac'.

Re3$irements:
This t$torial is &ase! in a *C with U&$nt$ 14 56&it +perating S(stem. 0icro S car! class 17 preferentiall(.

First of all8 we ha,e to !ownloa! the T# An!roi! #mage from T#9s we& page: http://!ownloa!s.ti.com/sitara:an!roi!/es!/T#:An!roi!: e,;it/T#:An!roi!:<%:6:.:.: e,;it:6:1: 1/in!e":F S.html #n the =*re>%$ilt #mages? section8 !ownloa! =%eagle&one %lac'?

@hen the file is finishe! to !ownloa!8 we ha,e to $ncompress it. #nsi!e the fol!er we will fin! the script =m'mmc>an!roi!.sh? with this script we will &e a&le to create an An!roi! #mage for o$r %eagle&one %lac'.

Steps
1A *l$g (o$r micro S car! in the S Car! rea!er.

#f (o$ ha,e a note&oo' with an integrate! rea!er8 the car! will appear in /!e,/mmc&l'. #f (o$ ha,e an US% rea!er8 the car! will appear in /!e,/s!c or /!e,/s!B it will !epen! how man( partitions (o$ ha,e in (o$r comp$ter. #n this t$torial we will $se mmc&l' !e,ice .A As root8 grant e"ec$tion permissions for the script: chmod +x mkmmc-android.sh car!:

4A /"ec$te the script an! image will &e copie! into the S ./mkmmc-android.sh /dev/mmcblk

:ARN3NG" #f (o$ p$t a wrong path where it sa(s =mmc&l'?(o$ can !estro( (o$r partition. +nce the script finishe!8 (o$ can p$t the S An!roi! will &oot. car! into the %eagle&one %lac' an! power it8

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Tutorial

#" Accessing the An!roi! Console &( A %

Re3$irements:
An!roi! S ; installe! in (o$r comp$ter

@hen we pl$g the %eagle9s mini US% connector to o$r comp$ter8 we can connect to An!roi! &( A %. The first step is config$ring U&$nt$ for recogni-e the %eagle&one %lac' as a ,ali! US% !e,ice.

Steps
1A As root8 create the file =/etc/$!e,/r$les.!/21>an!roi!.r$les ? nano /etc/udev/rules.d/51-android.rules .A *$t the following te"t insi!e the file: SU%SCST/0DDE$s&E8 SCSFSFi!Gen!orHDDE1I!1E8 0+ /DE7555E SU%SCST/0DDE$s&E8 SCSFSFi!Gen!orHDDE7621E8 0+ /DE7555E 4A As root8 change the file9s permissions: chmod a+r /etc/udev/rules.d/51-android.rules 6A As root8 go to the a!& !irector( insi!e An!roi!>S ; an! 'ill a!& an! restart it listing the !e,ices !etecte! J%eagle&one %lac' m$st &e pl$gge!A: ./adb kill-server ./adb devices The o$tp$t will &e something li'e: List of devices attached 0123456789ABCDEF device 2A Then we are a&le to enter in the An!roi! console t(ping: ./adb shell 5A if we want to cop( a file from o$r comp$ter to the %eagle&one %lac' we can $se the =p$sh? a!&9s comman!: ./adb push ~/beagleboneblack/foo.txt /etc/ @e can too open /clipse an! $se the &oar! as another em$lator or phone8 !ownloa!ing applications in it.

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Tutorial

$" Controlling )*#+s o,er An!roi! +perating S(stem

#n this t$torial we are going to control $ser9s le! in the &oar!. *rocessor pins for Users Le!s: USR7 USR1 USR. USR4 )*#+1:.1 )*#+1:.. )*#+1:.4 )*#+1:.6

)*#+ control is not a,aila&le for !efa$lt8 we nee! to create a !irector( for each )*#+ we want to control8 an! there will &e files in there that will allow $s to control the pins.

Steps
1A /nter to An!roi! Console &( a!& ./adb shell .A )o to !irector( s(s/class/gpio: cd /sys/class/gpio 4A There will fin! the files an! !irectories: export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport /ach gpiochipBB correspon!s to a 4. &it )*#+ *ort. For pin n$m&er calc$lation8 we nee! to calc$late the follow: pin n$m&er D Jport n$m&er K 4.A L gpio n$m&er for e"ample8 for $ser le! 7 J )*#+1:.1 A pin n$m&er D J1 K 4.A L .1 D 24 4A Now we 'now the pin n$m&er we want to control8 we nee! to generate the !irector( for this pin8 that is ma!e writing the pin n$m&er in the =e"port? file: echo 53 > /sys/class/gpio/export 6A one that8 a new !irector( will appear: <- This directory is new

export gpio53 gpiochip0 gpiochip32 gpiochip64

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black gpiochip96 unexport 2A )o to this !irector(: cd gpio53 5A There we will fin! a co$ple of files: active_low direction edge power subsystem uevent value 1A @e will $se =!irection? file to config$re the pin as inp$t or o$tp$t. @e write =hight? in it for o$tp$t: echo high > direction IA Now we can control the pin state &( =,al$e? file8 writing a 18 $ser le! 7 will t$rn on8 an! writing a 78 $ser le! 7 will t$rn off. echo 0 > value echo 1 > value

Consi!erations:
@hen we re&oot the &oar! the =gpio24? !irector( will not &e a,aila&le an! we will nee! to create it again. This pro&lem is sa,e! $sing a &oot script where this initiali-ation is ma!e.

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Tutorial

%" Creating a &oot script for har!ware initiali-ation.

#f we want that gpios Jor another har!ware mo!$leA are initiali-e! when An!roi! &oots8 we nee! to create a script with the gpios config$ration steps. @e will name this file = scri-tg-ios0s.?

The content of this file will &e:


MConfig$re L/ US/R 7 > )*#+1:.1 echo 24 N /s(s/class/gpio/e"port c! /s(s/class/gpio/gpio24 M irection : o$t echo high N !irection echo 7 N ,al$e chmo! 111 ,al$e chmo! 111 !irection Now we nee! to e"ec$te this script at the &eginning of the An!roi! &ooting. For that8 we nee! to e!it the =init0rc? file that is place! in An!roi!9s file s(stem root. Since we can not e!it the file insi!e the An!roi! +perating S(stem Jwe !o not ha,e a te"t e!itor in An!roi! consoleA we are going to e!it the file pl$gging the S car! in o$r comp$ter an! e!iting the file from there. @hen we !o that8 fo$r partitions will appear: &oot rootfs $sr!ata !ata

#n rootfs partition8 we will fin! the init.rc file. Now we can e!it it with ,im or nano.

Steps
1A +pen init.rc file with nano .A Search for the lines: ser,ice !e&$gger! /s(stem/&in/!e&$gger! class main %ellow this lines8 we are going to write o$r script initiali-ation: ser,ice scriptgpios /s(stem/&in/sh /s(stem/etc/scriptgpios.sh class main oneshot 4A Cop( =scriptgpios.sh? from (o$r comp$ter to =/s(stem/etc? in the rootfs partition of the S car!. This file m$st ha,e e"ec$tion permissions. Then we $nmo$nt all partitions an! p$t S time gpios will &e config$re!. car! again in the &oar!8 when An!roi! &oots8 this

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Tutorial

&" Compiling Te"as #nstr$ments An!roi! 'ernel from so$rce.

#f we want to compile An!roi! 'ernel J $sef$l if we want to create o$r c$stom !ri,ers A we nee! to !ownloa! T#> e,;it from Te"as we& page: http://!ownloa!s.ti.com/sitara:an!roi!/es!/T#:An!roi!: e,;it/T#:An!roi!:<%:6:.:.: e,;it:6:1: 1/in!e":F S.html #n the =T# An!roi! So$rces? section8 we !ownloa! =T# An!roi! <% 6.... So$rces? e,;itG6.1.1 A0442"

Steps
1A The file !ownloa!e! is a =.&in? we nee! to change e"ec$tion permission an! e"ec$te it: chmod +x TI_Android_JB_4.2.2_DevKit_4.1.1.bin ./TI_Android_JB_4.2.2_DevKit_4.1.1.bin @e s$ppose this file is in =O/&eagle&one&lac'? !irector(8 if (o$ are $sing another path8 pa( attention an! change it in all parts where it appears. .A +nce the &inar( file !ecompresse!8 !irector( =T#:An!roi!:<%:6....: e,;it:6.1.1? will appear. Now we can compile the ;ernel: cd ~/beagleboneblack/TI_Android_JB_4.2.2_DevKit_4.1.1/kernel PATH=$HOME/beagleboneblack/TI_Android_JB_4.2.2_DevKit_4.1.1/prebuilts/gcc/linuxx86/arm/arm-eabi-4.6/bin:$PATH make ARCH=arm CROSS_COMPILE=arm-eabi- distclean make ARCH=arm CROSS_COMPILE=arm-eabi- am335x_evm_android_defconfig make ARCH=arm CROSS_COMPILE=arm-eabi- uImage This process can ta'e a co$ple of min$tes !epen!ing of (o$r comp$ter. 4A ;ernel image will &e create! in T#:An!roi!:<%:6....: e,;it:6.1.1/'ernel/arch/arm/&oot. File9s name is =$#mage?. *$t the S Car! in (o$r car! rea!er an! cop( this file into =&oot? partition.

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black

Tutorial

'" #.C mo!$les. /"ample with L012 temperat$re sensor.

Re3$irements:
A &oar! with an L012 #.C temperat$re sensor. An!roi! N ; installe! in (o$r comp$ter.

@e are going to config$re i.c port an! we are going to $se it for rea!ing an L012 temperat$re sensor. #f we na,igate An!roi! !irector(8 in /!e, we are going to fin! =i.c>4? !e,ice. This !e,ice correspon!s with the pins 1P an! .7 from *P connector on the &oar!. ;* -in < 3#C function 1P .7 SCL S A

Steps
1A @e will connect L012 chip to this port. *ower s$ppl( can &e ta'en from the &oar! from pins 1 an! 6. .A @rite a C program for $se i.c !e,ice as a character !e,ice for rea!ing L012. For that8 we nee! to create a proQect !irector(: =lm12test?. Then we nee! to create the =Qni? !irector( insi!e. #nsi!e Qni fol!er8 we are going to create a =0ain.c? file. mkdir lm75test cd lm75test mkdir jni cd jni nano Main.c 4A @rite the C co!e $sing open8 rea!8 write8 an! ioctl f$nctions:
Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Rst!io.hN Rst!li&.hN Ri.c>!e,.hN Rs(s/ioctl.hN Rs(s/t(pes.hN Rs(s/stat.hN Rfcntl.hN

int fileS char Kfilename D E/!e,/i.c>4ES if JJfile D openJfilename8 +:R @RAA R 7A e"itJ1AS //1 7 7 1 A. A1 A7 R/@ int a!!r D 7"6IS // e,ice a!!ress is 7"6I if JioctlJfile8 #.C:SLAG/8 a!!rA R 7A e"itJ1AS $nsigne! char &$fT17U D F7HS ifJwriteJfile8 &$f81A VD 1A e"itJ1AS int r D rea!Jfile8&$f8.AS $nsigne! char tempW D &$fT7US $nsigne! char tempL D &$fT1US

UTN FRA LSL http://www.lslfra.com.ar

Android on Beaglebone Black


$nsigne! short tempS tempDtempWS tempDtempRR1S tempDtempX7"FFF/S tempLDtempLNN1S tempLDtempLX7"7771S tempDtempYtempLS float t D JJfloatAtempA/.S printfJETemperat$re:Zf[nE8tAS

6A To compile this co!e we will $se An!roi!>N ;8 for that reason8 we nee! to create two e"tra files: =Application.m'? an! =An!roi!.m'? Application.m': A**:+*T#0 :D release A**:*LATF+R0 :D an!roi!>12 An!roi!.m': L+CAL:*ATW :D \Jcall m(>!irA incl$!e \JCL/AR:GARSA L+CAL:0+ UL/ :D lm12test L+CAL:SRC:F#L/S :D 0ain.c incl$!e \J%U#L :/B/CUTA%L/A 2A @e can compile o$r co!e now8 insi!e Qni !irector( we e"ec$te N ; compiler Jthe path can change if (o$ are $sing a newer ,ersionA ~/adt-bundle-linux-x86_64-20130219/android-ndk-r8d/ndk-build 5A #f compilation was +;8 it will generate the e"ec$ta&le file in: lm12test/li&/armea&i/lm12test 1A Cop( the e"ec$ta&le into %eagle&one9s An!roi! etc !irector(: ./adb push lm75test/lib/armeabi/lm75test /etc/ IA /nter to An!roi! console8 change e"ec$tion permissions for o$r program an! e"ec$te it. ./adb shell cd etc chmod 777 lm75test ./lm75test The program will print the act$al temperat$re rea! from L012.

Consi!erations
#f we want8 an( An!roi! application can access this !e,ice8 we nee! to a!! to the &oot script we ma!e &efore8 the permissions for the !e,ice: chmod 777 /dev/i2c-3

UTN FRA LSL http://www.lslfra.com.ar

17

Android on Beaglebone Black

Tutorial

(" UART1 an! UART. mo!$les.

Re3$irements:
&$s(&o" m$st &e installe!

UART 1 an! . pino$t: Connector ;* *#N .6 *#N .5 *#N .1 *#N .. UART1:TB UART1:RB UART.:TB UART.:RB

UART1 is manage! &( tt(+1 !e,ice J/!e,/tt(+1 A UART. is manage! &( tt(+1 !e,ice J/!e,/tt(+.A %( !efa$lt8 pins are in 0+ /1 J)*#+A if we want to $se this pins as UARTs we nee! to change pins m$" config$ration.

Steps
1A @e can see UART1 pins m$" config$ration with the following comman!: cat /sys/kernel/debug/omap_mux/uart1_rxd .A %( !efa$lt8 pin m$" is )*#+8 we nee! to change it to UART1 RB echo 20 > /sys/kernel/debug/omap_mux/uart1_rxd Now when we chec' config$ration8 it will &e: name: uart1_rxd.uart1_rxd (0x44e10980/0x980 = 0x0020), b NA, t NA mode: OMAP_MUX_MODE0 | AM33XX_PIN_INPUT_PULLDOWN signals: uart1_rxd | mmc1_sdwp | NA | i2c1_sda | NA | pr1_uart0_rxd_mux1 | NA | gpio0_14 4A Same wa( we change t" pin config$ration. echo 0 > /sys/kernel/debug/omap_mux/uart1_txd 6A Now we !o the same for UART.8 in this case pins are calle! spi7:!7 an! spi7:scl' in An!ori! file s(stem. echo 1 > /sys/kernel/debug/omap_mux/spi0_d0 echo 21 > /sys/kernel/debug/omap_mux/spi0_sclk 2A To test UARTs we are going to $se =microcom? an application incl$!e! in &$s(&o" similar to =minicom?. /data/busybox/busybox microcom -s 9600 ttyO1 /data/busybox/busybox microcom -s 9600 ttyO2 N=TE" See steps 16 an! 12 from T$torial 7P for &$s(&o" installation.
UTN FRA LSL http://www.lslfra.com.ar 11

Android on Beaglebone Black

Tutorial

)" S*# mo!$le.

Re3$irements:
T#> e,;it m$st &e installe!. An!roi! ;ernel m$st &e compile!.

S*#7 pino$t: Connector ;* *#N 11 *#N 1I *#N .1 *#N .. S*#7:CS7 S*#7: 1 S*#7: 7 S*#7:SCL;

S*# is not ena&le! &( !efa$lt8 we nee! to e!it =&oar!>am442"e,m.c? file8 enter into 'ernel 9s men$config options an! ena&le S*# /G an! re>compile An!roi! 'ernel with this feat$res8 other wa( S*# !e,ice will not appear in /!e, !irector(. %( !efa$lt8 pins are in 0+ /1 J)*#+A if we want to $se this pins as S*# pins8 we nee! to change pins m$" config$ration.

Steps
1A @e are going to ena&le S*#7 &eca$se S*#1 is $se! &( W 0# mo!$le an! it cannot &e $se!. /!it =&oar!>am442"e,m.c? file in 'ernel9s !irector( =arch/arm/mach>omap./? nano arch/arm/mach-omap2/board-am335xevm.c .A Change =am442":spi7:sla,e:info? str$ct for the follow one:
static str$ct spi:&oar!:info am442":spi7:sla,e:infoTU D F F .mo!alias D Espi!e,E8 .ma":spee!:h- D 6I7777778 //6I 0&ps .&$s:n$m D 18 .chip:select D 78 .mo!e D S*#:0+ /:18 H8 HS

4A Change =spi7:init? f$nction for the follow one:


static ,oi! spi7:initJint e,m:i!8 int profileA F set$p:pin:m$"Jspi7:pin:m$"AS spi:register:&oar!:infoJam442":spi7:sla,e:info8 ARRAC:S#]/Jam442":spi7:sla,e:infoAAS ret$rnS H

6A A!! in the =&eagle&one:&lac':!e,:cfgTU? arra(8 this line :


Fspi7:init8 /G:+N:%AS/%+AR 8 *R+F#L/:N+N/H8

UTN FRA LSL http://www.lslfra.com.ar

1.

Android on Beaglebone Black 2A %efore compile 'ernel8 enter into men$config options an! ena&le S*# settings: cd ~/beagleboneblack/TI_Android_JB_4.2.2_DevKit_4.1.1/kernel PATH=$HOME/beagleboneblack/TI_Android_JB_4.2.2_DevKit_4.1.1/prebuilts/gcc/linuxx86/arm/arm-eabi-4.6/bin:$PATH make ARCH=arm CROSS_COMPILE=arm-eabi- distclean make ARCH=arm CROSS_COMPILE=arm-eabi- am335x_evm_android_defconfig make ARCH=arm CROSS_COMPILE=arm-eabi- menuconfig 0a'e s$re =0cS*#? an! =User mo!e S*#? are ena&le!. e,ice ri,ers >N S*# S$pport >N TKU 0cS*# !ri,er for +0A* TKU User mo!e S*# !e,ice !ri,er s$pport 5A Compile 'ernel make ARCH=arm CROSS_COMPILE=arm-eabi- uImage 1A Cop( 'ernel9s image into S car! an! &oot %eagle&one %lac'. Now !e,ice =spi!e,1.7? sho$l! appear in /!e, !irector(. root@android:/dev # ls spi* -la crw------- root root 153, 0 2000-01-01 00:00 spidev1.0

IA @ARN#N): #n t$torial 718 we change pin m$" for UART.8 this pins are the same for S*#7 cl' an! !7. 0a'e s$re pins spi7:scl' an! spi7:!7 are in 0+ /7 chec'ing m$" files: cat /sys/kernel/debug/omap_mux/spi0_sclk name: spi0_d0.spi0_d0 (0x44e10954/0x954 = 0x0000), b NA, t NA mode: OMAP_MUX_MODE0 | AM33XX_PIN_OUTPUT signals: spi0_d0 | uart2_txd | i2c2_scl | NA | NA | NA | NA | gpio0_3 cat /sys/kernel/debug/omap_mux/spi0_d0 name: spi0_d0.spi0_d0 (0x44e10954/0x954 = 0x0020), b NA, t NA mode: OMAP_MUX_MODE0 | AM33XX_PIN_INPUT_PULLDOWN signals: spi0_d0 | uart2_txd | i2c2_scl | NA | NA | NA | NA | gpio0_3 #f pins are not as the o$tp$t a&o,e8 we nee! to change them writing a =7? in this files8 also we nee! to !isa&le p$ll>$p in spi7:!78 writing =.7?. echo 0 >> /sys/kernel/debug/omap_mux/spi0_sclk echo 20 >> /sys/kernel/debug/omap_mux/spi0_d0 PA @e can chec' S*# mo!$le writing a simple C program an! connecting spi7:!7 an! spi7:!1 ph(sicall( with a wire. The program will transmit an! recei,e a few &(tes. Create a n!' proQect: mkdir spiTestProject cd spiTestProject mkdir jni cd jni nano spitest.c

UTN FRA LSL http://www.lslfra.com.ar

14

Android on Beaglebone Black 17A @rite hea!er files an! glo&al ,aria&les for spi config$ration.
Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e static static static static Rst!io.hN Rst!li&.hN Rfcntl.hN Rs(s/ioctl.hN Rspi!e,.hN

const char K!e,ice D E/!e,/spi!e,1.7ES $intI:t mo!e D S*#:0+ /:1S $intI:t &its D IS $int4.:t spee! D 177777S // 177;h-

Then we are going to !efine an error f$nction:


static ,oi! pa&ortJconst char KsA F perrorJsAS a&ortJAS H

Now we write a transfer f$nction for spi test8 this f$nction will recei,e spi!e,9s file !escriptor:
static ,oi! transferJint f!A F int retS $intI:t t"TU D F7"2287"C487"4C87"7187"I787"FF87"77HS $intI:t r"Tsi-eofJt"AU D F7HS str$ct spi:ioc:transfer tr D F .t":&$f D J$nsigne! longAt"8 .r":&$f D J$nsigne! longAr"8 .len D si-eofJt"A8 .!ela(:$secs D 278 .spee!:h- D spee!8 .&its:per:wor! D &its8 .cs:changeD18 HS ret D ioctlJf!8 S*#:#+C:0/SSA)/J1A8 XtrAS if Jret R 1A pa&ortJEcan9t sen! spi messageEAS printfJEr":EAS for Jret D 7S ret R si-eofJt"AS retLLA printfJEZ..B E8 r"TretUAS printfJE[r[nEAS

At the en!8 we write the main f$nction8 where we open spi!e, !e,ice an! we config$re it $sing ioctl f$nction &efore call =transfer? f$nction:
int mainJint argc8 char Karg,TUA F int ret D 7S int f!8 co$nterS f! D openJ!e,ice8 +:R @RAS if Jf! R 7A pa&ortJEcan9t open !e,iceEAS // set mo!e ret D ioctlJf!8 S*#:#+C:@R:0+ /8 Xmo!eAS if Jret DD >1A pa&ortJEcan9t set spi mo!eEAS ret D ioctlJf!8 S*#:#+C:R :0+ /8 Xmo!eAS if Jret DD >1A pa&ortJEcan9t get spi mo!eEAS

UTN FRA LSL http://www.lslfra.com.ar

16

Android on Beaglebone Black


// set &its per wor! ret D ioctlJf!8 S*#:#+C:@R:%#TS:*/R:@+R 8 X&itsAS if Jret DD >1A pa&ortJEcan9t set &its per wor!EAS ret D ioctlJf!8 S*#:#+C:R :%#TS:*/R:@+R 8 X&itsAS if Jret DD >1A pa&ortJEcan9t get &its per wor!EAS // set ma" spee! hret D ioctlJf!8 S*#:#+C:@R:0AB:S*// :W]8 Xspee!AS if Jret DD >1A pa&ortJEcan9t set ma" spee! h-EAS ret D ioctlJf!8 S*#:#+C:R :0AB:S*// :W]8 Xspee!AS if Jret DD >1A pa&ortJEcan9t get ma" spee! h-EAS // Start Test printfJEspi mo!e: Z![nE8 mo!eAS printfJE&its per wor!: Z![nE8 &itsAS printfJEma" spee!: Z! W- JZ! ;W-A[nE8 spee!8 spee!/1777AS transferJf!AS closeJf!AS H ret$rn retS

11A @e also nee! to create An!roi!.m' an! Application.m': An!roi!.m'


L+CAL:*ATW :D \Jcall m(>!irA incl$!e \JCL/AR:GARSA L+CAL:L L#%S :D >llog L+CAL:0+ UL/ :D testSpi L+CAL:SRC:F#L/S :D spitest.c incl$!e \J%U#L :/B/CUTA%L/A

Application.m'
A**:+*T#0 A**:*LATF+R0 :D release :D an!roi!>12

1.A Now we can compile this program $sing an!roi!>n!'8 in Qni !irector(8 e"ec$te: ~/adt-bundle-linux-x86_64-20130219/android-ndk-r8d/ndk-build 5A #f compilation was +;8 it will generate the e"ec$ta&le file in: spiTest*roQect/li&s/armea&i/ 1A Cop( the e"ec$ta&le into %eagle&one9s An!roi! etc !irector(: ./adb push spiTestProject/libs/armeabi/testSpi /etc/ IA /nter to An!roi! console8 change e"ec$tion permissions for o$r program an! e"ec$te it. ./adb shell cd etc chmod 777 testSpi ./testSpi

UTN FRA LSL http://www.lslfra.com.ar

12

Android on Beaglebone Black

Tutorial

*" Compiling a c$stom An!roi! !ri,er as a char !e,ice.

Re3$irements:
T#> e,;it m$st &e installe! An!roi! ;ernel m$st &e compile!

Steps
1A Create a fol!er for the ;ernel 0o!$le proQect mkdir moduleProject cd moduleProject .A Create a C file name! =c$stom0o!$le.c?: nano customModule.c 4A @e nee! to write the 'ernel mo!$le8 first of all8 we incl$!e some hea!ers files an! !efines:
Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Rlin$"/mo!$le.hN Rlin$"/'ernel.hN Rlin$"/fs.hN Rlin$"/'!e,:t.hN Rasm/$access.hN

0+ UL/:L#C/NS/JE)*LEAS 0+ UL/:AUTW+RJE/rnesto )igliotti Rernestogigliotti^gmail.comNEAS 0+ UL/: /SCR#*T#+NJEAn!roi! C$stom ri,erEAS

6A Then8 we !efine some constants an! the file operations str$ct$re8 this str$ct$re will contain the f$nctions that will &e calle! &( the program $ser when it calls rea!8write8open8ioctl an! close f$nctions.
M!efine 0+ UL/:NA0/ Ec$stom0o!$leE $nsigne! int mo!$le:maQorS str$ct file:operations fopsS

2A Now we can write the =init:mo!$le? f$nction8 this f$nction will &e calle! when the mo!$le is installe!.
int init:mo!$leJ,oi!A F int res$ltS // Asign file operation f$nctions fops.open D open:mo!$leS fops.release D release:mo!$leS //fops.rea! D rea!:mo!$leS //fops.write D write:mo!$leS //fops.$nloc'e!:ioctl D ioctl:mo!$leS //:::::::::::::::::::::::::::::: // inamic reser,ation for maQor n$m&er res$ltDregister:chr!e,J78 0+ UL/:NA0/8 XfopsAS if Jres$lt R 7A F print'J;/RN:@ARN#N) Ean!roi!C$stom0o!$leN Jinit:mo!$leA error o&taining maQor n$m&er[nEAS ret$rn res$ltS H mo!$le:maQor D res$ltS print'J ;/RN:#NF+ Ean!roi!C$stom0o!$leN Jint:mo!$leA loa!e! +;[nEAS

UTN FRA LSL http://www.lslfra.com.ar

15

Android on Beaglebone Black


ret$rn 7S

5A @rite clean$p:mo!$le f$nction. This f$nction will &e calle! when the mo!$le is $nloa!e!:
,oi! clean$p:mo!$leJ,oi!A F $nregister:chr!e,Jmo!$le:maQor8 0+ UL/:NA0/AS print'J ;/RN:#NF+ Ean!roi!C$stom0o!$leN Jclean$p:mo!$leA $nloa!e! +;[nEAS H

1A @rite the open:mo!$le f$nction. This f$nction will &e calle! when $ser9s program calls =open? f$nction o,er o$r !e,ice.
int open:mo!$leJstr$ct ino!e Kpino!e8 str$ct file KpfileA F int minorD 0#N+RJpino!e>Ni:r!e,AS print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN JopenA minorDZ! +;[nE8minorAS ret$rn 7S H

IA @rite release:mo!$le f$nction. This f$nction will &e calle! ehrn $ser9s program calls =close? f$nction o,er o$r !e,ice.
int release:mo!$leJstr$ct ino!e Kpino!e8 str$ct file KpfileA F int minorD 0#N+RJpino!e>Ni:r!e,AS print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN JreleaseA minorDZ! +;[nE8minorAS ret$rn 7S H

PA %efore we write the rea!8write an! ioctl f$nctions8 we are going to compile this 'ernel mo!$le Q$st with the f$nctions mentione! a&o,e. For that8 we nee! to create a 0a'efile. Create a file name! =0a'efile? with this content: 0a'efile:
o&Q>m LDc$stom0o!$le.o ;/RN/L #R _D O/&eagle&one&lac'/T#:An!roi!:<%:6....: e,;it:6.1.1/'ernel *@ :D \Jshell pw!A CR+SS:C+0*#L/DO/&eagle&one&lac'/T#:An!roi!:<%:6....: e,;it:6.1.1/pre&$ilts/gcc/lin$">"I5/arm/arm>ea&i>6.5/&in/ arm>ea&i> ARCWDarm !efa$lt: clean: \J0A;/A >C \J;/RN/L #RA 0D\J*@ A ARCWDarm CR+SS:C+0*#L/D\JCR+SS:C+0*#L/A mo!$les \J0A;/A >C \J;/RN/L #RA 0D\J*@ A clean

#n this 0a'file we pass the 'ernel path insi!e T#> e,;it. ;ernel m$st &e copile!. Also we pass the compiler9s path. Change this paths if (o$ ha,e T#> e,;it in another place. 17A @rite f$ction protot(pes at the &egining of the file: int open:mo!$leJstr$ct ino!e Kpino!e8 str$ct file KpfileAS int release:mo!$leJstr$ct ino!e Kpino!e8 str$ct file KpfileAS 11A Now we can compile the mo!$le e"ec$ting =ma'e? comman!: make File =c$stom0o!$le.'o? wil &e generate!.
UTN FRA LSL http://www.lslfra.com.ar 11

Android on Beaglebone Black

1.A Now we can install o$r mo!$le into the An!roi! +perating S(stem. Cop( the mo!$le &( a!& an! install it: ./adb push /home/USER/beagleboneblack/moduleProject/customModule.ko /etc ./adb shell cd /etc insmod customModule.ko 14A The mo!$le will &e installe! an! we can see 'ernel9s log messages in 'msg file: cat /proc/kmsg +$tp$t will &e: <6>[ 700.884185] androidCustomModule> (int_module) loaded OK

@e can $nloa! the mo!$le with =rmmo!? comman! 16A Now o$r mo!$le is wor'ing8 we nee! to create one or more !e,ices in /!e, !irector(. @e nee! to $se =m'no!? comman! for that8 &$t this comman! is not part of T# An!roi! !istri&$tion. @e nee! to install &$s(&o". This application will allow $s to $se a &ig set of stan!ar! Lin$"9s comman!s on An!roi!8 incl$!e! =m'no!? ownlo! &$s(&o" from: https://gforge.ti.com/gf/proQect/omapan!roi!/wi'i/_ pagenameD#nstallingL%$s(&o"Lcomman!LlineLtools 12A @e nee! to cop( &$s(&o" e"ec$ta&le into /!ata/&$s(&o" in An!roi! files(stem: ./adb shell mkdir /data cd /data mkdir busybox CTRL+D (quit from Android console) ./adb push /home/USER/beagleboneblack/busybox /data/busybox/ 15A Now we nee! to create a script for mo!$le9s installation J $sing insmo! an! m'no! comman!s A this script will install the mo!$le an! it will create the !e,ices in /!e, !irector(: install0o!$le.sh
MV/s(stem/&in/sh mo!$leDc$stom0o!$le !e,iceD/!e,/an!roi!>c$stom>!e,> permD555 rmmo! \Fmo!$leH insmo! \Fmo!$leH.'o maQorD`cat /proc/!e,ices Y grep \Fmo!$leH Y /!ata/&$s(&o"/&$s(&o" c$t >!9 9 >f1` rm >f \F!e,iceHK /!ata/&$s(&o"/&$s(&o" m'no! \F!e,iceH7 c \FmaQorH 7 /!ata/&$s(&o"/&$s(&o" m'no! \F!e,iceH1 c \FmaQorH 1 chmo! \FpermH \F!e,iceH_

#n this script we !elete the mo!$le8 then we install it an! we get the maQor n$m&er generate!.
UTN FRA LSL http://www.lslfra.com.ar 1I

Android on Beaglebone Black Then the script !elete c$rrent !e,ices8 an! creates the new ones with the maQor n$m&er o&taine! &efore. 11A Cop( c$stom0o!$le.'o an! install0o!$le.sh into /etc on An!roi! &( a!&: ./adb push /home/USER/beagleboneblack/moduleProject/customModule.ko /etc ./adb push /home/USER/beagleboneblack/moduleProject/installModule.sh /etc 1IA /nter to An!roi! console an! e"ec$te the script: ./adb shell cd /etc chmod 777 installModule.sh ./installModule #f instalation is +;8 we will &e a&le to see two new !e,ices in /!e, /!e,/an!roi!>c$stom>!e,>7 /!e,/an!roi!>c$stom>!e,>1

1PA Now mo!$le is wor'ing8 we are going to complete the mo!$le C program with write8rea! an! ioctl f$nctions. +pen again c$stom0o!$le.c an! a!! rea! f$nction: First the protot(pe at the &egining:
static ssi-e:t rea!:mo!$leJstr$ct file Kfilp8char K&$ffer8si-e:t length8loff:t KoffsetAS

Then the f$nction:


static ssi-e:t rea!:mo!$leJstr$ct file Kfilp8 char K&$ffer8 /K The &$ffer to fill with !ata K/ si-e:t length8 /K The length of the &$ffer K/ loff:t KoffsetA /K +$r offset in the file K/ F int &(tes:rea! D 7S int minorD 0#N+RJfilp>Nf:!entr(>N!:ino!e>Ni:r!e,AS print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN Jrea!A minor: Zi [nE8minorAS char messageT56UDEWello from mo!$le[7ES char K msg:*tr D messageS while JlengthN7AF /K p$t:$ser copies !ata from K the 'ernel !ata segment to the $ser !ata segment. K/ p$t:$serJKJmsg:*trA8 &$fferLLAS length>>S &(tes:rea!LLS ifJKJmsg:*trADD7A &rea'S msg:*trLLS

H ret$rn &(tes:rea!S H

#n this mo!$le8 we are sim$lating a !e,ice which we can rea! the message =Wello from !e,ice? =p$t:$ser? f$nction is $se! to cop( !ata from 'ernel space to $ser space. o not forget $ncomment fops assignment in init:mo!$le f$nction.

UTN FRA LSL http://www.lslfra.com.ar

1P

Android on Beaglebone Black .7A @rite the =write? f$nction an! its protot(pe: *rotot(pe:
static ssi-e:t write:mo!$leJstr$ct file Kpfile8 const char K&$f8 si-e:t si-e:&$f8 loff:t Kf:posAS

F$nction:
static ssi-e:t write:mo!$leJstr$ct file Kpfile8 const char K&$f8 si-e:t si-e:&$f8 loff:t Kf:posA F $nsigne! long not:copie!S int menorD 0#N+RJpfile>Nf:!entr(>N!:ino!e>Ni:r!e,AS print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN JwriteA minor: Zi [nE8menorAS char msg#nT56US not:copie!D::cop(:from:$serJmsg#n8&$f8si-e:&$fAS // cop( from $ser space to 'ernel space if Jnot:copie!N7A F print'J;/RN:@ARN#N) Ean!roi!C$stom0o!$leN JwriteA error cop(ing !ata from $ser[nEAS ret$rnJ>/FAULTAS H print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN JwriteA msg:Zs [nE8msg#nAS JKf:posALDsi-e:&$fS ret$rnJsi-e:&$fAS

#n this case8 this f$nction prints &( 'ernel log the message that it recei,es for writing. .1A The last f$nction we are going to write is =ioctl? this f$nction will allow $s to config$re o$r !e,ice thro$gh comman!s. @e nee! to create o$r hea!er file calle! =c$stom0o!$le.h? c$stom0o!$le.h:
M!efine M!efine M!efine M!efine M!efine M!efine CUST+0:0+ CUST+0:0+ CUST+0:0+ CUST+0:0+ CUST+0:0+ CUST+0:0+ UL/:#+C:N0A)#C 9c9 UL/:#+C:C0 :7 :#+JCUST+0:0+ UL/:#+C:N0A)#C8 1A UL/:#+C:C0 :1 :#+JCUST+0:0+ UL/:#+C:N0A)#C8 .A UL/:#+C:C0 :. :#+JCUST+0:0+ UL/:#+C:N0A)#C8 4A UL/:#+C:C0 :4 :#+JCUST+0:0+ UL/:#+C:N0A)#C8 6A UL/:#+C:C0 :6 :#+@JCUST+0:0+ UL/:#+C:N0A)#C8 28 1A

#n o$r hea!er file we !efine a =magic n$m&er? for the !ri,er an! comman!s for !e,ice config$ration. User will call ioctl passing this comman!s to the !ri,er. #octl f$nction:
long ioctl:mo!$leJstr$ct file Kpfile8 $nsigne! int cm!8 $nsigne! long argsA F int minorD 0#N+RJpfile>Nf:!entr(>N!:ino!e>Ni:r!e,AS print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN JioctlA minor: Zi [nE8minorAS if J:#+C:TC*/Jcm!A VD CUST+0:0+ UL/:#+C:N0A)#CA ret$rn >/#NGALS if J:#+C:NRJcm!A ND 2A // we ha,e onl( 2 comman!s ret$rn >/#NGALS print'J;/RN:#NF+ Ean!roi!C$stom0o!$leN JioctlA C0 : Z! [nE8 :#+C:NRJcm!AAS switchJcm!A F case CUST+0:0+ UL/:#+C:C0 :7: // ... &rea'S // +ther comman!s //... !efa$lt: ret$rn >/#NGALS H ret$rn 7S

UTN FRA LSL http://www.lslfra.com.ar

.7

Android on Beaglebone Black ..A Compile the mo!$le again an! cop( it into %eagle%one9s An!roi!. Now we are going to create a Test program for o$r !e,ice. Create a =!e,iceTest*roQect? !irector(8 a Qni fol!er insi!e8 cop( the mo!$le hea!er file insi!e Qni fol!er an! create test.c file. mkdir deviceTestProject cd deviceTestProject mkdir jni cd jni cp ~/beagleboneblack/moduleProject/customModule.h . nano test.c .4A @rite a test program $sing open8 rea!8 write an! ioctl f$nctions on o$r !e,ice: test.c:
Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Mincl$!e Rs(s/ioctl.hN Rs(s/t(pes.hN Rs(s/stat.hN Rfcntl.hN R$nist!.hN Rst!io.hN Ec$stom0o!$le.hE

int mainJA F int file78 res$ltS printfJE ri,er Test[nEAS file7DopenJE/!e,/an!roi!>c$stom>!e,>7E8+:R @RAS if Jfile7DD>1A ret$rnJ>1AS char msgT56US int r D rea!Jfile78msg856AS printfJERea! msg: Zs [nE8msgAS char &$fferT56UDE0essage from $ser[7ES writeJfile78&$ffer856AS res$ltDioctlJfile78 CUST+0:0+ UL/:#+C:C0 :78NULLAS if Jres$ltDD7A printfJEioctl +;[nEAS else printfJEioctl /RR+R[nEAS closeJfile7AS ret$rn 7S

.6A Compile this test program $sing N ;. files first: Application.m':


A**:+*T#0 :D release A**:*LATF+R0 :D an!roi!>12

o not forget create Application.m' an! An!roi!.m'

An!roi!.m':
L+CAL:*ATW :D \Jcall m(>!irA incl$!e \JCL/AR:GARSA L+CAL:0+ UL/ :D !e,iceTest L+CAL:SRC:F#L/S :D test.c incl$!e \J%U#L :/B/CUTA%L/A

)o to Qni fol!er an! compile the program $sing N ;: ~/adt-bundle-linux-x86_64-20130219/android-ndk-r8d/ndk-build

UTN FRA LSL http://www.lslfra.com.ar

.1

Android on Beaglebone Black .2A Cop( e"ec$ta&le file in /li&s/armea&i into %eagle%oar!9s An!roi! &( a!&: ./adb push /home/USER/beagleboneblack/deviceTestProject/libs/armeabi/deviceTest /etc/ .5A )o to An!roi! console an! e"ec$te !e,iceTest program: ./ a!& shell c! etc chmo! 111 !e,iceTest ./!e,iceTest *rogram9s +$tp$t will &e: Driver Test Read msg: Hello from module ioctl OK ;ernel log9s +$tp$t will &e: <6>[ <6>[ <6>[ <6>[ <6>[ <6>[ <6>[ <6>[ 5295.389801] 5309.067596] 5309.074859] 5309.080596] 5309.085845] 5309.092895] 5309.098052] 5309.103607] androidCustomModule> androidCustomModule> androidCustomModule> androidCustomModule> androidCustomModule> androidCustomModule> androidCustomModule> androidCustomModule> (int_module) loaded OK (open) minor=0 OK (read) minor: 0 (write) minor: 0 (write) msg:Message from user (ioctl) minor: 0 (ioctl) CMD: 1 (release) minor=0 OK

Consi!erations
#f we nee! o$t c$stom !e,ices alwa(s a,aila&le8 we nee! to call =install0o!$le.sh? script when An!roi! is &ooting8 this is possi&le a!!ing it to the &oot script we alrea!( ma!e.

UTN FRA LSL http://www.lslfra.com.ar

..

Android on Beaglebone Black

Tutorial ! " Accessing to har!ware from c$stom An!roi! !ri,er.

Re3$irements:
C$stom !ri,er m$st alrea!( wor'.

Until now8 o$r !ri,er Q$st can manage a local ram memor( portion witho$t interaction with har!ware8 if we want to rea!/write processor9s registers for controlling har!ware mo!$les we can not !o this !irectl(8 we nee! to $se f$nctions to con,ert ph(sical a!!resses to ,irt$al a!!resses.

Steps
1A Use =re3$est:mem:region? f$nction to reser,e ph(sical a!!resses for accessing. This reser,ation sho$l! &e !one in mo!$le9s init f$nction.
str$ct reso$rce Kres D re3$est:mem:regionJ*WCS:A ifJresVDNULLA F // Reser,ation +; H R868Earea:nameEAS

#n this e"ample8 we are reser,ing 6 &(tes starting from a!!ress =*WCS:A assigning to this area the name =area:name? @hen memor( area is reser,e!8 we can chec' that $sing: cat /proc/iomem

R? an! we are

.A Same wa(8 we sho$l! release this memor( area in mo!$le9s close f$nction:
release:mem:regionJ*WCS:A R86AS

4A Now we can o&tain a pointer for rea!/write $sing =ioremap:nocache? f$nction


$nsigne! int K a!!r D J$nsigne! int KAioremap:nocacheJ*WCS:A R86AS

6A Using iorea!4. an! iowrite4. we can rea!/write this memor( a!!ress:


$nsigne! int ,al$e D iorea!4.Ja!!rAS ,al$eD,al$e Y J1RR.1AS iowrite4.J,al$e8a!!rAS

2A Release the pointer:


io$nmapJa!!rAS

This wa( we can rea!/write ph(sical a!!resses in o$r !ri,er an! $se processor9s har!ware. This hea!ers files m$st &e incl$!e!:
Mincl$!e Rlin$"/ioport.hN Mincl$!e Rasm/io.hN

UTN FRA LSL http://www.lslfra.com.ar

.4

Android on Beaglebone Black

Bibliogra-.4 Te"as #nstr$ments An!roi! images http://!ownloa!s.ti.com/sitara:an!roi!/es!/T#:An!roi!: e,;it/T#:An!roi!:<%:6:.:.: e,;it:6:1:1/in!e":F S.html Te"as #nstr$ments An!roi! e,;it )$i!e http://processors.wi'i.ti.com/in!e".php/A0442B:/G0>S;:An!roi!: e,'it:)$i!e /na&le S*# /G: http://comm$nistco!e.co.$'/&log/&log*ost.php_&log*ost# D1 Config$re UARTS: http://www.gigamega&log.com/.71./71/../&eagle&one>co!ing>171>$sing>the>serial>a n!>analog>pins/ Create 'ernel mo!$le http://www.it.$c4m.es/alcortes/!ocs/ap$ntes:lao/lao/no!e5I.html =%eagle%one %lac' S(stem . Reference 0an$al? > )eral! Cole(8 +cto&er .714. =A0442" AR0a Corte"T0>AI 0icroprocessors. Technical Reference 0an$al? > Te"as #nstr$ments8 +cto&er .711.

UTN FRA LSL http://www.lslfra.com.ar

.6

You might also like