Introduction To Cortex-M3 Programming: ARM University Program
Introduction To Cortex-M3 Programming: ARM University Program
Introduction To Cortex-M3 Programming: ARM University Program
Programming
Program Data
How is Data Stored in RAM
Data Types
Accessing Data using C and Assembly
Often hardware specific, e.g. memory usage constraints, available instructions etc.
Programming language
Embedded systems are usually programed using C (or C++) language and assembly
language
ARM-based tools can compile the C code or the assembly code to the executable file,
which can be executed by ARM-based processors
Less portable
Off-line Compilation
C Code
Compile Processor
Assembly Code Fetch
Instructio
Assemble
n Decode
Object Code Libraries Fetch
Link Execute
Program Image
Compile/ assemble
armcc
armcc armasm
armasm
.O Files
C/
C/
C/ C++
C/C++
C++
C++ C/
C/
C/ C++
C/C++
C++
C++
Object
Object Libraries
Libraries .S Files
Link
armlink
armlink &
&
armmar
armmar
.AXF File
Image
Image .LIB file
executable
.BIN File
binary
binary .HEX File
Disassembly File
C start-up routine
Code region
External Interrupts
0x00000040
SysTick 0x0000003C
PendSV 0x00000038
Reserved 0x00000034
Start-up routine & Debug monitor 0x00000030
Program code & SVCall 0x0000002C
C library code
Program Reserved 0x0000001C
Image Usage fault 0x00000018
Bus fault 0x00000014
MemManage fault 0x00000010
Hard fault vector 0x0000000C
NMI vector 0x00000008
Vector table Reset vector 0x00000004
Initial MSP value 0x00000000
Address
C Start-up code
Used to set up data memory and the initialization values for global data
variables
C library code
Object codes that inserted to the program image by the linkers
External RAM
Peripherals
Mainly used for data memory 0x3FFFFFFF
e.g. on-chip SRAM, SDRAM SRAM Region 512MB
0x20000000
0x1FFFFFFF
Mainly used for program image Code Region 512MB
0x00000000
e.g. on-chip FLASH
Global memory space
heap:
Grow
Stack Downwards
Static data – contains global variables
and static variables
/*
/* Set
Set stack
stack and
and heap
heap parameters
parameters */
*/
#define
#define STACK_BASE
STACK_BASE 0x10020000
0x10020000 //
// stack
stack start
start address
address
#define
#define STACK_SIZE
STACK_SIZE 0x5000
0x5000 //
// length
length of
of the
the stack
stack
#define
#define HEAP_BASE
HEAP_BASE 0x10001000
0x10001000 //
// heap
heap starts
starts address
address
#define
#define HEAP_SIZE
HEAP_SIZE 0x10000 – 0x6000
0x10000 – 0x6000 // heap length
// heap length
/*
/* inker
inker generated
generated stack
stack base
base addresses
addresses */
*/
extern
extern unsigned
unsigned int
int Image$$ARM_LIB_STACK$$ZI$$Limit
Image$$ARM_LIB_STACK$$ZI$$Limit
extern
extern unsigned
unsigned int
int Image$$ARM_LIB_STACKHEAP$$ZI$$Limit
Image$$ARM_LIB_STACKHEAP$$ZI$$Limit
……
Stack_Size
Stack_Size EQU
EQU 0x00000400
0x00000400 ;; 256KB
256KB of
of STACK
STACK
AREA
AREA STACK,
STACK, NOINIT,
NOINIT, READWRITE,
READWRITE, ALIGN=4
ALIGN=4
Stack_Mem
Stack_Mem SPACE
SPACE Stack_Size
Stack_Size
__initial_sp
__initial_sp
Heap_Size
Heap_Size EQU
EQU 0x00000400
0x00000400 ;; 1MB
1MB of
of HEAP
HEAP
AREA
AREA HEAP,
HEAP, NOINIT,
NOINIT, READWRITE,
READWRITE, ALIGN=4
ALIGN=4
__heap_base
__heap_base
Heap_Mem
Heap_Mem SPACE
SPACE Heap_Size
Heap_Size
__heap_limit
__heap_limit
Volatile
Can be changed outside of normal program flow: ISR, hardware
register
Compiler must be careful with optimizations
Static
Declared within function, retains value between function invocations
Scope is limited to function
#pragma
#pragma arm
arm section
section rodata="exceptions_area”
rodata="exceptions_area”
ExecFuncPtr
ExecFuncPtr exception_table[]
exception_table[] == {{
(ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit,
(ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit, /* /* Initial
Initial SP
SP */
*/
(ExecFuncPtr)__main,
(ExecFuncPtr)__main, /*
/* Initial
Initial PC
PC */
*/
NMIException,
NMIException,
HardFaultException,
HardFaultException,
MemManageException,
MemManageException,
BusFaultException,
BusFaultException,
UsageFaultException,
UsageFaultException,
0,
0, 0,
0, 0,
0, 0,
0, /*
/* Reserved
Reserved */
*/
SVCHandler,
SVCHandler,
DebugMonitor,
DebugMonitor,
0,
0, /*
/* Reserved
Reserved */
*/
PendSVC,
PendSVC,
SysTickHandler
SysTickHandler
/*
/* Configurable
Configurable interrupts
interrupts start
start here...*/
here...*/
};
};
#pragma
#pragma arm
arm section
section
#define
#define FLASH_BASE
FLASH_BASE *((volatile
*((volatile unsigned
unsigned long
long *)
*) (0x00000000UL)
(0x00000000UL)
#define
#define RAM_BASE
RAM_BASE *((volatile
*((volatile unsigned
unsigned long
long *)
*) (0x10000000UL)
(0x10000000UL)
#define
#define GPIO_BASE
GPIO_BASE *((volatile
*((volatile unsigned
unsigned long
long *)
*) (0x2009C000UL)
(0x2009C000UL)
#define
#define APB0_BASE
APB0_BASE *((volatile
*((volatile unsigned
unsigned long
long *)
*) (0x40000000UL)
(0x40000000UL)
#define
#define AHB_BASE
AHB_BASE *((volatile
*((volatile unsigned
unsigned long
long *)
*) (0x50000000UL)
(0x50000000UL)
#define
#define CM3_BASE
CM3_BASE *((volatile
*((volatile unsigned
unsigned long
long *)
*) (0xE0000000UL)
(0xE0000000UL)
Bit-band operation
Directly set the bit by writing ‘1’ to address 0x2200000C, which is the alias address
of forth bit of the 32-bit data at 0x20000000
;Read-Modify-Write
;Read-Modify-Write Operation
Operation ;Bit-band
;Bit-band Operation
Operation
LDR
LDR R1,
R1, =0x20000000
=0x20000000 ;Setup
;Setup address
address LDR
LDR R1,
R1, =0x2200000C
=0x2200000C ;Setup
;Setup address
address
LDR
LDR R0,
R0, [R1]
[R1] ;Read
;Read MOV
MOV R0,
R0, #1
#1 ;Load data
;Load data
ORR.W
ORR.W R0,
R0, #0x8
#0x8 ;Modify
;Modify bit
bit STR
STR R0,
R0, [R1]
[R1] ;Write
;Write
STR
STR R0,
R0, [R1]
[R1] ;Write back
;Write back
#define
#define RAM_Data1
RAM_Data1 *((volatile
*((volatile unsigned
unsigned long
long *)(0x20000000))
*)(0x20000000))
#define
#define RAM_Data1_Bit0
RAM_Data1_Bit0 *((volatile
*((volatile unsigned
unsigned long
long *)(0x22000000))
*)(0x22000000))
#define
#define RAM_Data1_Bit1
RAM_Data1_Bit1 *((volatile
*((volatile unsigned
unsigned long
long *)(0x22000004))
*)(0x22000004))
RAM_Data1=
RAM_Data1= RAM_Data1
RAM_Data1 || 0x01
0x01 ;Setup
;Setup bit0
bit0 without
without using
using bitband
bitband
RAM_Data1_Bit0=
RAM_Data1_Bit0= 0x00
0x00 ;clear
;clear bit0
bit0 using
using bitband
bitband
RAM_Data1_Bit1=
RAM_Data1_Bit1= 0x01
0x01 ;Setup bit1
;Setup bit1 using
using bitband
bitband
Data_Addr(BitBand(RAM_Data1,0))
Data_Addr(BitBand(RAM_Data1,0)) == 0x01
0x01 ;set
;set bit0
bit0
int
int my_add(int
my_add(int x1,
x1, int
int x2,
x2, int
int x3,
x3, int
int x4)
x4) {{
return
return (x1+x2+x3+x4);
(x1+x2+x3+x4);
}}
_asm
_asm int
int add_asm(
add_asm( int
int k1,
k1, int
int k2,
k2, int
int k3,
k3, int
int k4)
k4) {{
ADDS
ADDSR0,
R0, R0,
R0, R1
R1
ADDS
ADDSR0,
R0, R0,
R0, R2
R2
ADDS
ADDSR0,
R0, R0,
R0, R3
R3
BX
BX LRLR
}}
void
void main
main {{
int
int x;
x;
xx == add_asm
add_asm (11,22,33,44);
(11,22,33,44); //
// call
call assembly
assembly function
function
……
}}
Reference2
Cortex-M3 Technical Reference Manual:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337i/index.html
Reference3
Cortex-M3 Devices Generic User Guide:
http://infocenter.arm.com/help/topic/com.arm.doc.dui0552a/DUI0552A_cortex_m3_dgug.p
df