Professional Documents
Culture Documents
Lecture 3
-
Programming ES
The blinking LED program
-
Jukka Mäki-Turja
Lecture overview
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
“Hello world”
for ESs?
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
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
LED
Micro
controller
AT32U3A0 Microcontroller
8
8
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
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
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
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
13
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
-
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
17
/******************
* 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
19
1) Header information
• Overall information of sections that follows
− Name of source file it was created from
− Creation date
20
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
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
22
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
Linking &
Relocation
23
The linker
24
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
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
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
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
28
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
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
29
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 locator
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
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
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
Downloading,
and running
the SW
35
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
After the executable has been built, the next step is to get it
onto the target HW
• Often called loading the image
37
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
38
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
39
40
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
41
Reading instructions
42
Jukka Mäki-Turja
Programming ES 2011-09-08
The blinking LED program
The
The End
End!
43
Jukka Mäki-Turja