Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction To Cortex-M3 Programming: ARM University Program

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Introduction to Cortex-M3

Programming

ARM University Program


Copyright © ARM Ltd 2013 1
Module Syllabus
 Program Code
 C Language vs. Assembly Language
 Program-Generation Flow
 Cortex-M3 Program Image

 Program Data
 How is Data Stored in RAM
 Data Types
 Accessing Data using C and Assembly

 Mixed Assembly and C Programming


 Calling a C Function from Assembly
 Calling an Assembly Function from C
 Embedded Assembly

ARM University Program


Copyright © ARM Ltd 2013 2
Program Code

ARM University Program


Copyright © ARM Ltd 2013 3
Program Code Overview
 All microcontrollers need a program code to perform an intended task
 The program of embedded systems is usually developed at a relatively
lower level
 Less supports from operating systems

 lack of standard programing interface

 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

 A completely integrated program code can be also referred as a program image or an


executable file

ARM University Program


Copyright © ARM Ltd 2013 4
C Language vs. Assembly Language
Language Advantages Disadvantage
Easy to learn Limited or no direct access to core
registers and stack

Portable No direct control over instruction


C sequence generation

Easy handling of complex data No direct control over stack usage


structures

Allow direct control to each Take longer time to learn


instruction step and all memory

Assembly Allows direct access to instructions Difficult to manage data structure


that cannot be generated with C

Less portable

ARM University Program


Copyright © ARM Ltd 2013 5
Typical Program-Generation Flow
 The generation of program follows a typical development flow
 Compile  Assemble  Link  Download
 The generated executable file (or program image) is stored in the program memory
(normally an on-chip flash memory), to be fetched by the processor

Off-line Compilation
C Code
Compile Processor
Assembly Code Fetch
Instructio
Assemble
n Decode
Object Code Libraries Fetch
Link Execute
Program Image

Download Data Input Data Output


Processing
Program Memory

Typical program-generation flow

ARM University Program


Copyright © ARM Ltd 2013 6
Compilation using ARM-based Tools
C, C++
C/
C/C/
C/ C++
C++
C++
C++ C/
C/
C/ C++
C/C++
C++
C++
C/
C/ C++
C++ Assembly
Assembly ASM files

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

ARM University Program


Copyright © ARM Ltd 2013 7
Compiler Stages
 Parser
 Reads in C code,
 Checks for syntax errors,
 Forms intermediate code (tree representation)
 High-Level Optimizer
 Modifies intermediate code (processor-independent)
 Code Generator
 Creates assembly code step-by-step from each node of the intermediate code
 Allocates variable uses to registers
 Low-Level Optimizer
 Modifies assembly code (parts are processor-specific)
 Assembler
 Creates object code (machine code)
 Linker/Loader
 Creates executable image from object file

ARM University Program


Copyright © ARM Ltd 2013 8
Cortex-M3 Program Image
 What is a program image
 The program image (or sometimes referred as executable file) usually refers
to a piece of fully integrated code that is ready to execute

 In Cortex-M3, the program image includes:


 Vector table – includes the starting addresses of exceptions (vectors) and the
value of the main stack point (MSP)

 C start-up routine

 Program code – application code and data

 C library code – program codes for C library functions

ARM University Program


Copyright © ARM Ltd 2013 9
Cortex-M3 Program Image

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

ARM University Program


Copyright © ARM Ltd 2013 10
Cortex-M3 Program Image
 Vector table
 Contains the starting addresses of exceptions (vectors) and the value of the
main stack point (MSP)

 Can be programed in either C or assembly

 C Start-up code
 Used to set up data memory and the initialization values for global data
variables

 Is inserted by compiler/ linker automatically, labelled as “__main” by ARM


compiler, or “_start” by GNU C compiler

ARM University Program


Copyright © ARM Ltd 2013 11
Cortex-M3 Program Image
 Program code
 Program code refers the instructions generated from your application program. The
type of data in the program code includes:
 Initial values of variables – the local variables that initialized in functions or
subroutines during program execution time
 Constants – used in data values, address of peripherals, character strings, etc…
 Sometimes are stored together in data blocks called literal pools
 Additionally, constant data such as lookup tables, graphics image data (e.g.
bit map) can also be merged into the program images

 C library code
 Object codes that inserted to the program image by the linkers

ARM University Program


Copyright © ARM Ltd 2013 12
Program Image in Global Memory
 The program image is stored in the code region in the global memory
 Up to 512 MB memory space range from 0x00000000 to 0x1FFFFFFF
 Usually implemented on non-volatile memory, such as on-chip FLASH
memory
 Normally separated from program data, which is allocated in the SRAM
region (or data region)

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

ARM University Program


Copyright © ARM Ltd 2013 13
Program Data

ARM University Program


Copyright © ARM Ltd 2013 14
How is Data Stored in RAM
 Typically, the data can be divided in
three sections: static data, stack, and High

heap:
Grow
Stack Downwards
 Static data – contains global variables
and static variables

 Stack – contains the temporary data for Memory


Address
local variables, parameter passing in
function calls, registers saving during Grow
Heap
exceptions, etc. Upwards

 Heap – contains the pieces of memory Static


spaces that dynamically reserved by Data
function calls, such as “alloc()”,
Low
“malloc()”

ARM University Program


Copyright © ARM Ltd 2013 15
Define Stack and Heap
 The stack and heap can be defined in either C language (with
linker file) or assembly language, for example in C:

/*
/* 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

……

ARM University Program


Copyright © ARM Ltd 2013 16
Define Stack and Heap
 Define stack and heap in assembly language:

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

ARM University Program


Copyright © ARM Ltd 2013 17
Data Types
 A number of standard data types are supported by the C
language
 However, their implementation is depending on the processor
architecture and C compiler
 In ARM programming, the data size is referred as byte, half
word, word, and double word:
 Byte: 8-bit;
 Half word: 16-bit;
 Word: 32-bit
 Double word: 64-bit

 The following table shows the implementation of different


data types
ARM University Program
Copyright © ARM Ltd 2013 18
Data Types
Data type Size Signed range Unsigned range
char, int8_t, uint8_t Byte -128 to 127 0 to 255
short, int16_t, uint16_t Half word -32768 to 32767 0 to 65535

int, int32_t, uint32_t, Word -2147483648 to 0 to 4294967295


long 2147483647
long long, int64_t, Double word -263 to 263-1 0 to 264-1
uint64_t
float Word -3.4028234 × 1038 to 3.4028234 × 1038
double, long double Double word -1.7976931348623157 ×10308 to
1.7976931348623157 ×10308
pointers Word 0x00 to 0xFFFFFFFF
enum Byte/ half word/ word Smallest possible data type
bool (C++), _bool(C) Byte True or false

wchar_t Half word 0 to 65535

ARM University Program


Copyright © ARM Ltd 2013 19
Data Types and Class Qualifiers
 Const
 Never written by program, can be put in ROM to save RAM

 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

ARM University Program


Copyright © ARM Ltd 2013 20
Example of Data Storage

Zero-Initialized int a, b; Constant data


static data const char c=123;
int d=31;
Initialization Data
Initialized static void main(void) {
data int i;
char f[32]; Startup Code
int *array;
Stack data
array =(int *)malloc(128); Program code
e = d + 7; .text
printf(“Hello!”);
Heap data Runtime Library
} Code

Usually stored in volatile Usually stored in non-volatile


memories, e.g. SRAM memories, e.g. FLASH

ARM University Program


Copyright © ARM Ltd 2013 21
Define Interrupt Vector in C
 The interrupt vector can be defined in either C language or assembly
language, for example in C:
typedef
typedef void(*
void(* const
const ExecFuncPtr)(void)
ExecFuncPtr)(void) __irq;
__irq;

#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

ARM University Program


Copyright © ARM Ltd 2013 22
Accessing Peripherals in C
 Define base addresses for peripherals, e.g.

#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)

 Write a value to a peripheral register, e.g.


//store
//store aa value
value to
to the
the memory
memory
RAM_BASE = 0x3FFFF
RAM_BASE = 0x3FFFF

 Read a value from a peripheral register, e.g.


//read
//read aa value
value from
from the
the memory
memory
i=
i= RAM_BASE;
RAM_BASE;

ARM University Program


Copyright © ARM Ltd 2013 23
Use Bit-band Operation in Assembly
 For example, in order to set bit[3] in word data in address 0x20000000:
 Read-Modify-Write operation
 Read the real data address (0x20000000)
 Modify the desired bit (retain other bits unchanged)
 Write the modified data back

 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

ARM University Program


Copyright © ARM Ltd 2013 24
Use Bit-band Operation in C
 The bit-band operation is not natively supported in most C compilers
 To make ease use of the bit-band feature, the address and the bit-band
address can be separately declared, for example:

#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

 Alternatively, use C macros to easily access bit-band alias, for example:


#define
#define RAM_Data1
RAM_Data1 *((volatile
*((volatile unsigned
unsigned long
long *)(0x20000000))
*)(0x20000000))
#define
#define BitBand(data_addr,
BitBand(data_addr, bit)
bit) ((data_addr
((data_addr && 0x0F0000000)
0x0F0000000) ++ 0x2000000
0x2000000 ++
((data_addr
((data_addr && 0xFFFFF)<<5)+(bit<<2))
0xFFFFF)<<5)+(bit<<2))
#define
#define Data_Addr (data_addr)
Data_Addr (data_addr) *((volatile
*((volatile unsigned
unsigned long*)(data_addr))
long*)(data_addr))

Data_Addr(BitBand(RAM_Data1,0))
Data_Addr(BitBand(RAM_Data1,0)) == 0x01
0x01 ;set
;set bit0
bit0

ARM University Program


Copyright © ARM Ltd 2013 25
Mixed Assembly and
C Programming

ARM University Program


Copyright © ARM Ltd 2013 26
Calling a C Function from Assembly
 When a C function is called from an assembly file, the
following areas should be aware:
 Register R0, R1, R2, R3, R12, and LR could be changed, hence it is
better to save them to the stack

 The value of SP should be aligned to a double-word address


boundary

 Input parameters have to be stored in the correct registers, for


example, registers R0 to R3 can be used for passing four parameters

 The return value is usually stored in R0

ARM University Program


Copyright © ARM Ltd 2013 27
Calling a C Function from Assembly
 For example, write an adding function in C:

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);
}}

 Call the C function from the assembly code, for example:


MOVS
MOVSR0,
R0, #0X1
#0X1 ;; parameter
parameter x1
x1
MOVS
MOVSR1,
R1, #0X3
#0X3 ;; parameter
parameter x2
x2
MOVS
MOVSR2,
R2, #0X5
#0X5 ;; parameter
parameter x3
x3
MOVS
MOVSR3,
R3, #0X7
#0X7 ;; parameter
parameter x4
x4
IMPORT
IMPORT my_add
my_add
BL my_add
BL my_add ;; call
call “my_add”
“my_add” function
function in
in CC

ARM University Program


Copyright © ARM Ltd 2013 28
Calling an Assembly Function from C
 When calling an assembly function from C code, following
areas should be aware:
 If registers R4 to R11 need to be changed, they have to be stacked
and restored in the assembly function

 If another function is called inside the assembly function, LR register


needs to be saved on the stack and used for return

 The function return value is normally stored in R0

ARM University Program


Copyright © ARM Ltd 2013 29
Calling an Assembly Function from C
 Write a function in assembly, for example:
EXPORT
EXPORT add_asm
add_asm
add_asm
add_asm FUNCTION
FUNCTION
ADDS
ADDSR0,
R0, R0,
R0, R1
R1
ADDS
ADDSR0,
R0, R0,
R0, R2
R2
ADDS
ADDSR0,
R0, R0,
R0, R3
R3
BX LR
BX LR ;
; result is
result is returned
returned in
in R0
R0
ENDFUNC
ENDFUNC

 Calling an assembly function in C, for example:


external
external int
int add_asm(
add_asm( int
int k1,
k1, int
int k2,
k2, int
int k3,
k3, int
int k4);
k4);
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
……
}}

ARM University Program


Copyright © ARM Ltd 2013 30
Embedded Assembly
 The embedded assembler allows developer to write
assembly functions inside C files, for example in C:

_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
……
}}

ARM University Program


Copyright © ARM Ltd 2013 31
Lab Exercise
 Lab Exercises
 Lab 1 - processing text in assembly language
 Execute assembly code on the mbed board (e.g. mbed LPC1768) using
the debugger (in ARM® Keil® MDK )
 Examine its execution at the processor level
 Lab 2 - square root approximation
 Write an assembly code subroutine to approximate the square root of an
argument using the bisection method
 Test your result on the mbed board (e.g. mbed LPC1768) using the
debugger (Keil MDK)

mbed LPC 1768

ARM University Program


Copyright © ARM Ltd 2013 32
ARM Keil® MDK
 Keil μVision has an Integrated Development Environment (IDE), which allows user to build a
project easily and quickly, the IDE includes:
 Project management;
 Make facilities;
 Source code editing;
 Program debugging;
 Complete simulation;
 A serial of ARM-based tools are
integrated in Keil μVision, including:
 Compiler
 Assembler
 Linker
 Format Converter
 Libraries

ARM University Program


Copyright © ARM Ltd 2013 33
Useful Resources
 Reference1
 Book: “The Definitive Guide to the ARM Cortex-M3” by Joseph Yiu, ISBN-10:
0123854776, ISBN-13: 978-1856179638, 12 Jan 2010

 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

ARM University Program


Copyright © ARM Ltd 2013 34

You might also like