You are on page 1of 22

Embedded System

Design
BITS Pilani Sangmeshwar Kendre
Pilani Campus
BITS Pilani
Pilani Campus

ES ZG512
Embedded System Design
Lab Session No.2.
Contents

• ARM Cortex-M4
• Reset sequence
• Memory map
• Assembly language programming examples
• SVC

BITS Pilani, Pilani Campus


Reset sequence

• After reset and before the processor starts executing the


program, the Cortex-M processors read the first two words
from the memory.
• After these two words are read by the processor, the
processor then sets up the MSP and the Program Counter
(PC) with these values.

Source: Joseph Yiu, “The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors”

BITS Pilani, Pilani Campus


BITS Pilani, Pilani Campus
STM32F40xxx memory map
BITS Pilani, Pilani Campus
Example

Write ALP for ARM Cortex M4 to solve the following equation


varA = varB + varC + varD
Let varB=2, varC=4, varD=6

BITS Pilani, Pilani Campus


Solution method-1
varB EQU 2
varC EQU 4
varD EQU 6
AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler+1 ; reset vector

AREA mycode,CODE,READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV R1,#varB
MOV R2,#varC
MOV R3,#varD

ADD R0,R1,R2
ADD R0,R0,R3
Stop B Stop
END BITS Pilani, Pilani Campus
Solution method-2

AREA RESET, CODE, READONLY


EXPORT __Vectors
__Vectors
DCD 0x20001000 ; stack pointer value when
stack is empty
DCD Reset_Handler+1 ; reset vector

AREA mycode,CODE,READONLY
EXPORT Reset_Handler
ENTRY

BITS Pilani, Pilani Campus


Reset_Handler
LDR R1,varB
LDR R2,varC
LDR R3,varD
ADD R0,R1,R2
ADD R0,R0,R3

LDR R4,=varA
STR R0,[R4]

varB DCD 2; create variable varB with inirtial value 2


varC DCD 4; create variable varC with inirtial value 4
varD DCD 6; create variable varD with inirtial value 6

Stop B Stop

AREA myData, DATA, READWRITE


varA DCD 0; Reserve 4 bytes of memory

END
BITS Pilani, Pilani Campus
Branch

Version 7-M cores have more branch instructions than the


ARM7TDMI, but the types of allowable branches have some
limitations:
• B—Branch. This is the simplest form of branch, where
condition codes may be used to decide whether or not to
branch to a new address in the code.
• BX—Branch indirect. A registered value is used as a
branch target. If bit[0] of the address is a zero, a usage
fault exception will occur. Use this instruction carefully, as
the assembler will not be generating offsets or addresses
for you, and the value in the register must have bit[0] set.
• BL—Branch with Link. As with the ARM7TDMI, the Link
Register will hold a return address after a branch.
BITS Pilani, Pilani Campus
• BLX—Branch indirect with Link. This instruction is
similar to BL, only the address is held in a register.
• CBZ, CBNZ—Compare and Branch if Zero, Compare
and Branch if Nonzero. These two instructions are useful
in looping and can reduce the number of instructions.
• IT blocks—IF-THEN blocks. The IT instruction can be
used to avoid branching entirely with up to four instructions
in a block.

BITS Pilani, Pilani Campus


LOOP
• While Loops
Suppose we had the following C code:
i = 100;
while (i!= 0) {
//do something
i--;}
The while loop can be constructed on an ARM7TDMI as
MOV r3, #0x64
B Test
Loop .
. ; do something
.
SUB r3, r3, #1 ; i--
Test .. ; evaluate condition i = 0?
BNE Loop
BITS Pilani, Pilani Campus
The loop can be constructed for the Cortex-M4 using version 7-M instructions as
MOV r3, #0x64
Loop CBZ r3, Exit
; do something
SUB r3, #1 ; i--
B Loop
Exit

BITS Pilani, Pilani Campus


• For Loops
Suppose you wish to create a for loop to implement a counter
of some kind using a control expression to manage an
index i, which is declared as an integer:
for (i= 0; i < 10; i++)
{
instructions
}

BITS Pilani, Pilani Campus


MOV r1, #10 ; i = 10
LOOP
.
. ; instructions
.
SUBS r1, r1, #1 ; i = i -1
BNE LOOP ; if i = 0, finish
DONE ..

BITS Pilani, Pilani Campus


Example

• Suppose we have five 32-bit integers that need to be


summed together, where the integer data is stored in
memory. This might be equivalent to a C statement such as
sum = 0;
for (i = 0; i < 5; i + +) {
sum += a[i];
}

BITS Pilani, Pilani Campus


Solution
AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler+1 ; reset vector

AREA mycode,CODE,READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV R0,#0 ; sum=0;
MOV R1,#4 ; Number of elements-1
ADR R2, ARRAY_A ; Start of array
LOOP
LDR R3,[R2,R1,LSL #2] ; Load value from memory
ADD R0,R3,R0 ; sum+=a[i]
SUBS R1,R1,#1 ; i=i-1
BGE LOOP ; loop only if i>0
STOP B STOP

ARRAY_A DCD 1,2,3,4,5


END BITS Pilani, Pilani Campus
SVC Example

Write an program to perform the following


1. SVC is to be called from an application task running at
Thread unprivileged mode
2. Two parameters are passed to the handler via R1,R2.
3. If the SVC number is 0x20, then perform some task as per
SVC number.
4. Return and resume application task

BITS Pilani, Pilani Campus


AREA RESET, CODE, READONLY
EXPORT __Vectors
__Vectors
DCD 0x20001000,Reset_Handler+1,0,0,0,0,0,0,0,0,0,SVC_handler+1,0,0,0,0,0,0,0,0,0,0,0

AREA mycode,CODE,READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR R1,=0X20000200
MSR PSP,R1

MOV R0,#3
MSR CONTROL,R0

BITS Pilani, Pilani Campus


LDR R7,=SRC
LDR R1,[R7],#4
LDR R2,[R7]
LDR R8,=DST
SVC 250
STOP B STOP

SVC_handler
push {lr}
;Write logic here

pop {pc}
SRC DCD 0X30 ,0X20

AREA RES ,DATA, READWRITE


DST DCD 0,0
END
BITS Pilani, Pilani Campus
Thank you

BITS Pilani, Pilani Campus

You might also like