You are on page 1of 22

Programming ES 2011-09-08

The blinking LED program

Lecture 3
-

Programming ES
The blinking LED program
-
Jukka Mäki-Turja

Lecture overview

The Blinking LED program


• “Hello world!” for embedded systems

The Blinking LED program


• What?
• Why?
• How?

Getting the Blinking LED run on the EVK1100 board


• Compiling
• Linking and locating,
• Downloading,
• Running

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

“Hello world”
for ESs?

Classical “Hello world!”

Simple interaction using a given programming language


• Program & make it print the string "Hello, World!" on the user's
screen

Shows how the simplest program can be designed and run


• Serves as a useful benchmark for users of programming languages
and computer platforms

However, for many ES the "Hello, World!“ is impossible !


• Why????

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Why not “Hello world!” for ES?

Most embedded systems lack a monitor or similar device


• i.e., there is no place where the “Hello world!” string can be printed
• If a display is supported, a display driver must often be implemented
before any characters can be written to it
• Standard C library I/O routines might not be available for the given
hardware

Almost every ES has at least one software controllable LED


• This LED might also be a very valuable debugging tool

A substitute for "Hello, World!" in ES is a LED blinking program


• A suitable rate is 1 Hz (i.e., one complete on-off cycle per second)

Benefits:
• Little room for programming errors
• Very portable since almost all ES have LEDs 5

The Blinking
LED program

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

EVK1100 Development board

LED

Micro
controller

AT32U3A0 Microcontroller

8
8

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

The GPIO unit is controlled by hardware registers


volatile avr32_gpio_t * gpio = &AVR32_GPIO.port[GPIO_PORT];

To get the
right port To get the
MAX_PIN_NUMBER = 32
address of a
GPIO_PORT = AVR32_PIN_PB27 / MAX_PIN_NUMBER //Port index
GPIO port
GPIO_PIN = AVR32_PIN_PB27 & (MAX_PIN_NUMBER - 1) //Bit position

To get the
right pin

Each bit in the registers


correspond to a GPIO pin.

bit_value = (1 << GPIO_PIN); //Put a 1-bit at the correct position


gpio_port->ovrs = bit_value; //Write the bit to the gpio_port 9

Include files containing definitions of


LED0_GPIO and AVR_32_GPIO
The program
Index to get HW
#include <avr32/io.h>, "compiler.h”,"board.h”
address of the
#define LED0_PORT (LED0_GPIO & (GPIO_MAX_PIN_NUMBER-1)) GPIO reg
#define LED0_BIT_VALUE (1 << LED0_PORT)
#define CYCLES_PER_MS (115/11) Access the
correct bit in
port register
void mdelay(int milliseconds) {
long volatile cycles = (milliseconds * CYCLES_PER_MS);
while (cycles != 0) {
cycles--; Clock runs at 115 kHz and
} mdelay is 11 cycles (asm
Address to the
instructions)
} LED port
int main(void) {
volatile avr32_gpio_port_t * lp = & AVR32_GPIO.port[LED_PORT];
lp->gpers = LED0_BIT_VALUE ; Set bit in GPIO Enable
lp->oders = LED0_BIT_VALUE ; Register to indicate
while(1) { GPIO function
lp->ovrt = LED0_BIT_VALUE ; Set bit in Output Drive
mdelay(1000); Enable Register to
Toggle bit in Output
} drive the LED
Value Register to
} 10
Wait 1 for sec toggle LED

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

The infinite loop

Most ES programs include an infinite loop Infinite


• Different from most PC programs while(1) loop
• Typically, the loop surrounds a significant {
part of the program's functionality /* Do the job */
− E.g., as it does in the Blinking LED program }

Why using an infinite loop?


• The embedded software's job is normally never done
− It is intended to be run forever (or until the board is reset)
• Most ESs run only one piece of SW
− Although HW is important, the system is not a digital watch or a cellular
phone or a microwave oven without that SW
− If the software stops running, the HW is rendered useless

11

Getting
the Blinking
LED program
to run on the
EVK1100
board

12

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Making the example program run – the build process

Required steps to run LED toggle program:


• Compilation
− Each source file is compiled into an object file
• Linking and relocation
− The resulting object files are linked together
− Physical memory addresses are
assigned to data and code
− The result is a binary executable image file
• Downloading
− The executable is downloaded into the board memory
using a serial cable connecting with your PC
• Starting the execution
− The program is started using a debug monitor which
communicate over the serial cable connection

13

The build process


Preprocessing:
Compiler specific
keywords are
processed. E.g.,
#include and
#define

Compilation: Each
source file is
compiled into a
corresponding
object file
Assembling: An object files can
also be generated by
Linking: The object files are assembling an assembly file
linked together to one
relocatable object file
Relocation: Concrete
addresses are assigned to
Linking: Often there are other instructions and data to
files, e.g., libraries also linked in produce an executable file

14

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Compilation
and
object code

15

The compiler

Programming language text to another programming language


text transaltor
• E.g. a C-code sorce files translates to an object file

A native compilers output is intended to run on the same HW


platform as itself runs upon
• Most often the case in desktop programming

-
A cross-compiler runs on one HW plat-
form and produces code for another
• Most often the case in ES SW development

16

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

The object file

A specially formatted binary file containing instructions, data


and some other info
• Given in machine code format specific to the target CPU
• Usually given an .o ending by the compiler

Although parts of the object file contain executable code, it


cannot be executed directly
• Other files must be
added to resolve all
unresolved symbols
• This is made in the
linking stage

17

Example: compiling & linking


/******************
* File: main.c Contains object
*****************/ code for main.c
int foo(); Compiler main.o Object code contains an
int main() { unresolved call to foo
return 1 + foo();
}
Linker &
Locator a.exe

/******************
* File: foo.c The main.o and
*****************/ foo.o object code
Compiler foo.o files are combined
int foo() {
return 1;
The call to foo
} Contains object in main has
code for foo.c been resolved

18

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Object file formats

Contents of an object file can be seen as a very large, flexible


data structure
• The structure contain a number of standard fields
• E.g., header information, code sections, symbol table, …

Structure is often defined by a standard format


• E.g., Common Object File Format (COFF) or
• Executable and Linkable Format (ELF) - Used for your labs

Not all compilers produce all formats


• Some only produce object files in proprietary formats
− If you're using such a compiler, you often need to get all of your other
development tools from the same vendor

19

Object file contents

1) Header information
• Overall information of sections that follows
− Name of source file it was created from
− Creation date

2) Code sections (the object code)


• Blocks of instructions and data generated by the compiler/assembler
• The compiler has regrouped these blocks into related sections
− E.g., in gcc code blocks goes to a section called text,
− Initialized global variables (and their initial values) into a section called
data,
− Uninitialized global variables into a section called bss

20

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Object file contents cont’d

3) Global symbols defined and required in the file


• Symbols defined by the linker or to be imported from other files
• Previously main.o defined symbol main and required symbol foo to be
imported

4) Relocation information
• Places in the object code that have to be fixed when the locator
changes the addresses of the object code

5) Debugging information
• Mapping between the source- and object code
− Line number information
− Local symbols
− Description of C data structures

21

The assembler

An assembler is a code to object code translator


• Human-readable mnemonics to the equivalent line of opcode
(binary)
• In a sense, an assembler is also a compiler
− One that performs a much simpler one-to-one line translation
• E.g., the assembler used by gcc is called as

A disassembler does a translation the other way


• From machine object code to assembler code
• E.g., the program objdump can be used as a disassembler (and to
extract much useful information from an object file)
• Also used in low level debugging

22

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Linking &
Relocation

23

The linker

The object files must somehow be combined


• The object files themselves are individually incomplete
• Variables and function references have not yet been resolved

The linker combines these and resolves all unresolved


symbols

24

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

The linker process

A linker does not just link object files originating from the user
• A lot of other files are usually involved in the creation of an
executable

C Runtime
C Library
C Source Object File

OS
C Source Compiler Object File

C Source Start-up
Object File Linker &
Locator
Hand-written Linker script
assembly
Assembler Object File
Executable

25

Common additional files

C Runtime code:
• Whatever needed but not supported by the HW
− 32-bit arithmetic on a 16-bit machine
− Floating-point arithmetic
− Complex operations (e.g., modulo, variable-length shifts)
• Part of the compiler
• May have a large footprint
− Bigger for simpler machines
− Tens of bytes of data and tens of kilobytes of code

OS code:
• OS code is often linked together with the ES system
code to a single binary image
• FreeRTOS in the labs

26

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Common additional files

Startup code:
• A small piece of assembly code that prepares for the execution of
SW written in a high-level language
− For example, setting up the system stack
• Many ES cross-compilers include a file named startup.asm,
crt0.s, … holding startup code

C library code:
• ANSI-C supporting compiler must provide code that implements all
ANSI-C functions and variables
− E.g., functions such as printf, memmove, strcpy
− Many ES compilers only support subset of ANSI-C
− Many ES compilers provide their own extension to ANSI
• Comes with the compiler (often non-standard)

27

Libraries

A collection of object code files


• Also called archive (thereby the usual .a ending)
• E.g., libgcc.a holds C runtime code for the particular target HW

Included object file could be individually extracted


• Similar to zip, gunzip, tar, …

An index is often contained within the archive


• Display symbols defined in included object files
• Speeds up searching of and linking to files in library

GNU program ar creates, modifies, and extracts from archives

28

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Finding object files to link

A linker scans object files (and libraries) for symbols that have
been referenced but not yet defined
• Found object files are (extracted from library and) linked in
• An extracted file may itself include unresolved symbol refs

The linker searches for object files (and libraries) and at


predefined places
• Declarations of symbols/functions given by #include
− The preprocessor include header files into the C source file
− In gcc explicit locations to search for include files given by –I flag
• Object code file name is usually != the #include file name
− In gcc explicit locations to search for object files given by –l flag

29

The resulting relocatable

Linking output is a relocatable object file


• The result from merging all of the code and data sections and
resolving all symbol references

The relocatable file is almost complete


• Compilers or assemblers typically generate the executable with zero
as the lower-most, starting address

Programs can normally not start at this address on the target


HW
• The relocatable’s addresses must be adjusted to hold correct run-
time addresses
• This is the task of the locator

30

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Example: Linking

/******************
* File: main.c Compiler Code from main and foo Main’s call to foo has
*****************/ have been linked together been given an address
int foo();
main.o 00000000 <main>:
int main() {
return 1 + foo(); 0000: mov ip, sp
} 0004: stmdb sp!, {fp,ip,lr,pc}
Linker
0008: sub fp, ip, #4 ; 0x4
/****************** 000c: bl 0020 <foo>
* File: foo.c
*****************/ foo.o 0010: mov r3, r0
int foo() {
0014: add r3, r3, #1 ; 0x1
return 1; 0018: mov r0, r3
} Compiler
001c: ldmdb fp, {fp, sp, pc}

The addresses given might not be 00000020 <foo>:


were instructions are allowed to 0020: mov ip, sp
be stored in memory 0024: stmdb sp!, {fp,ip,lr,pc}
0028: sub fp, ip, #4 ; 0x4
Thus, the addresses given 002c: mov r3, #1 ; 0x1
to instructions might later 0030: mov r0, r3
be modified by the locator 0034: ldmdb fp, {fp, sp, pc} 31

The locator

Converts relocatable file to an executable binary image


• Assigns concrete memory addresses to each of the code and data
sections within the relocatable program/file
• Often part of the linker process

Might need information on the memory configuration on


the target hardware
• Provided by the ES programmer

Resulting in a file
containing a binary
memory image that
can be loaded onto
the target HW

32

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Example: Relocation

Memory map Locator updates addresses of


The RAM memory to hold code
0xFFFFFFFF starts at address 0x00050000 calls, instructions and data to
Peripherals fit into RAM memory
0xFF000000
unused 00058228 <main>:
58228: mov ip, sp
0x04050000
SDRAM 5822c: stmdb sp!, {fp, ip, lr, pc}
Locator 58230: sub fp, ip, #4 ; 0x4
(64 MB)
0x00050000 58234: bl 58248 <foo>
unused 58238: mov r3, r0
0x00000000 5823c: add r3, r3, #1 ; 0x1
00000000 <main>:
58240: mov r0, r3
0000: mov ip, sp 58244: ldmdb fp, {fp, sp, pc}
0004: stmdb sp!, {fp,ip,lr,pc}
0008: sub fp, ip, #4 ; 0x4
000c: bl 0020 <foo> 00058248 <foo>:
0010: mov r3, r0
0014: add r3, r3, #1 ; 0x1
58248: mov ip, sp
0018: mov r0, r3 5824c: stmdb sp!, {fp, ip, lr, pc}
001c: ldmdb fp, {fp, sp, pc}
58250: sub fp, ip, #4 ; 0x4
00000020 <foo>: 58254: mov r3, #1 ; 0x1
0020: mov ip, sp
0024: stmdb sp!, {fp,ip,lr,pc}
58258: mov r0, r3
0028: sub fp, ip, #4 ; 0x4 5825c: ldmdb fp, {fp, sp, pc}
002c: mov r3, #1 ; 0x1
0030: mov r0, r3
0034: ldmdb fp, {fp, sp, pc}
Code after Other code parts, e.g., startup 33
linking code, libraries, … not shown

Dynamic linking
The dynamic linking
A feature provided by many OSes version of libgcc.a is
• Not so common within ES development though named libgcc.so

Postpones the resolving of symbols until program execution


• The executable still contains undefined symbols, plus a list of objects
or libraries that will provide definitions for these
• Loading the program will load these objects/libraries as well, and
perform a final linking

This approach offers two advantages:


• Often-used libraries need to be stored in only one location, and not
duplicated in every single binary
• If an error in a library function is corrected by replacing it
− Programs using the library dynamically will benefit from the correction
after restarting them
− Programs that included this function by static linking would have to be
re-linked first
34

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Downloading,
and running
the SW

35

Software development cycle

Bugs are often


discovered which
needs to be fixed

After the code has


been built it is often
downloaded and run
on the target HW An iterative process
which can take a lot
Debugger provides of time
more details of the
program execution A lot of testing is 36
often required

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Downloading the software

After the executable has been built, the next step is to get it
onto the target HW
• Often called loading the image

This can be made in many


different ways:
• Using a ROM device
• Using a debug monitor
• Using a JTAG interface – in the labs
?

37

Using a ROM device

Load the binary image into a ROM device and insert the chip
into a socket on the target board
• ROM = memory where data can be read, but never written
• Some ROM devices are reprogrammable using a special piece of
equipment called a device programmer or burner
• Hybrid memory types, such as Flash, offer ROM-like permanence,
but under some conditions it is possible to overwrite their data

Things to consider when using ROM devices:


• Turn off the board before inserting the ROM device
• Location of the first instruction to execute after board reset
− For the AT32UC3A processor this reset address is 0x80000000
• Hard to debug SW executing out of flash memory or ROM
− Thus, ROM devices are ok at system deployment, but
not during earlier testing and debugging phases

38

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Using a debug monitor

Many boards include a debug monitor


• A small program residing in ROM
• The first code executed when the board powers up
• Used to download software, start execution of the code, perform
basic memory operations, perform remote debugging, …

Also called as ROM monitor

Can often communicate with host through serial port

39

Using a JTAG interface

Microcontroller has an interface connecting an external device


• In the lab kit you will use the
Dragon JTAG debugger

Through this interface the user can


• Download code
• Read or write processor registers
• Modify system memory
• Command the processor to executer one instruction and halt

Very useful for development


• Easy loading and running
• Powerful debugging tool

40

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

Lecture overview revisited

The Blinking LED program


• “Hello world!” for embedded systems

The Blinking LED program


• What?
• Why?
• How?

Getting the Blinking LED run on the EVK1100 board


• Compiling
• Linking and locating,
• Downloading,
• Running

41

Reading instructions

Real-Time concepts for Embedded Systems


• Chapter 2 and 3

Programming Embbedded Systems


• Chapter 3 through 5

PREPRE FOR LABS!!!!


• Read the books
• Read through lab specifications
• Lot of work
• You need to brush up on volatile memory, bit-wise operations, bit
fields, binary, hex numbers , signed, unsigned, struct overlays, …

42

Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program

The
The End
End!
43

Jukka Mäki-Turja

You might also like