Gnu Eprog Dist
Gnu Eprog Dist
Vijay Kumar B.
[email protected]
Zilogic Systems 1
What?
C Application
OS C Application
Hardware Hardware
Zilogic Systems 2
Why?
Zilogic Systems 3
How?
● 3 Example Scenarios
● Hello Embedded World – Add 2 numbers
in registers in assembly
● Add 2 numbers from memory in assembly
● Add 2 numbers from memory in C
Zilogic Systems 4
Scenario I - Overview
● Cortex-M3 Processor
● Writing Assembly Programs
● Emulating Cortex-M3 with Qemu
Zilogic Systems 5
ARMv7
Zilogic Systems 7
CM3 SoCs
Zilogic Systems 8
LM3S811
● Cortex-M3 core
● Memory
– 64KB Flash
– 8KB RAM
● Peripherals
– 10 bit ADCs
– I2C, SPI, UARTs, PWM
– 32 GPIOs
Zilogic Systems 9
Registers
● Load Store
Architecture
● Data processing
instructions –
register operands
● Large register file –
16 32-bit registers
Zilogic Systems 10
Registers (Contd.)
R0 R8
● R0 – R12
R1 R9 – General Purpose
R2 R10
R3 R11 ● R13
R4 R12
R5 R13 (SP)
– Stack Pointer
R6
R7
R14 (LR)
R15 (PC)
● R14
– Link Register
PSR PRIMASK
FAULTMASK
● R15
CONTROL BASEPRI
– Program Counter
Zilogic Systems 11
Memory Map
Zilogic Systems 12
Reset
Reset Vec.
0x0004
Initial SP
0x0000
Zilogic Systems 13
Assembly
Zilogic Systems 14
Hello Embedded World
.thumb
.syntax unified
sp: .word 0x100
reset: .word start+1
start:
mov r0, #5
mov r1, #4
add r2, r1, r0
stop: b stop
Zilogic Systems 15
Toolchain
Assembler (as)
Linker (ld)
Executable (.elf)
Zilogic Systems 16
Toolchain (Contd.)
Zilogic Systems 17
Toolchain (Contd.)
Zilogic Systems 18
Toolchain (Contd.)
$ arm-none-eabi-nm add.elf
...
00000004 t reset
00000000 t sp
00000008 t start
00000014 t stop
Zilogic Systems 19
Toolchain (Contd.)
Zilogic Systems 20
Toolchain (Contd.)
Zilogic Systems 21
Qemu
Zilogic Systems 22
Emulating in Qemu
Zilogic Systems 24
Scenario II - Overview
● Role of Linker
● Linker Scripts
● Placing data in RAM
Zilogic Systems 25
Linker
linker abc.o
b.s assembler b.o
Zilogic Systems 26
Linker (Contd.)
Linker
Symbol
Relocation
Resolution
Section Section
Merging Placement
Zilogic Systems 27
Linker (Contd.)
Linker
Symbol
Relocation
Resolution
Section Section
Merging Placement
Zilogic Systems 28
Symbol Resolution
● Functions are
defined in one file
● Referenced in
another file
● References are
marked unresolved
by the compiler
● Linker patches the
references
Zilogic Systems 29
Linker
Linker
Symbol
Relocation
Resolution
Section Section
Merging Placement
Zilogic Systems 30
Relocation
● Code generated
assuming it starts
from address X
● Code should start
from address Y
● Change addresses
assigned to labels
● Patch label
references
Zilogic Systems 31
Sections
Zilogic Systems 32
Sections (Contd.)
Zilogic Systems 33
Sections (Contd.)
Linker
Symbol
Relocation
Resolution
Section Section
Merging Placement
Zilogic Systems 35
Section Merging
Zilogic Systems 36
Linker
Linker
Symbol
Relocation
Resolution
Section Section
Merging Placement
Zilogic Systems 37
Section Placement
Zilogic Systems 38
strlen
Linker Script
Linker
Symbol
Relocation
Resolution
Can be controlled
through Linker scripts.
Section Section
Merging Placement
Zilogic Systems 42
Simple Linker Script
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
} 0xFFFF
SECTIONS {
.text : {
abc.o (.text);
def.o (.text);
} > FLASH def.o (.text)
}
abc.o (.text)
0x0
Zilogic Systems 43
Simple Linker Script
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
}
SECTIONS {
.text : { Section Merging
abc.o (.text);
def.o (.text);
} > FLASH
}
Zilogic Systems 44
Simple Linker Script
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
}
SECTIONS {
.text : {
abc.o (.text);
def.o (.text);
} > FLASH Section Placement
}
Zilogic Systems 45
Making it Generic
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
}
SECTIONS {
.text : {
* (.text); Wildcards to represent .text
} > FLASH form all input files
}
Zilogic Systems 46
Multiple Sections
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
}
0xFFFF
SECTIONS {
.text : { Dealing with
* (.text); mutiple sections
} > FLASH
.rodata : { .rodata
* (.rodata);
} > FLASH .text
0x0
}
Zilogic Systems 47
Data in RAM
Zilogic Systems 48
RAM is Volatile!
● RAM is volatile
● Data cannot be made available in RAM at
power-up
● All code and data should be in Flash at
power-up
● Startup code – copies data from Flash to
RAM
Zilogic Systems 49
RAM is Volatile! (Contd.)
Zilogic Systems 50
Linker Script Revisited
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
} 0x20001FFF
SECTIONS { SRAM
.text : { .data
0x20000000
* (.text);
} > FLASH
0x0000FFFF
.data : {
Flash
* (.data);
} > SRAM .text
0x00000000
}
Zilogic Systems 51
Linker Script Revisited
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
} 0x20001FFF
SECTIONS { SRAM
.text : { .data
0x20000000
* (.text);
} > FLASH
0x0000FFFF
.data : {
Flash .data
* (.data);
} > SRAM AT> FLASH .text
0x00000000
}
Zilogic Systems 52
Linker Script Revisited
MEMORY {
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x2000
}
SECTIONS {
.text : {
edata
* (.text); .data
etext = .; sdata
} > FLASH
.data : {
sdata = .;
* (.data); .data
edata = .; etext
.text
} > SRAM AT> FLASH
}
Zilogic Systems 53
Data in RAM
Zilogic Systems 54
Review
● C Environment Requirements
● C Sections
● C Source Code
● Linker Script
Zilogic Systems 56
Doing it in C
● Environment has to
be setup
– Stack pointer
– Non-initalized
global variables,
initialized to zero
– Initialized global
variables must
have their initial
value
Zilogic Systems 57
C Sections
Zilogic Systems 58
Credits
Zilogic Systems 59
Further Reading
Zilogic Systems 60