Unit3 ARM Cortex Architecture
Unit3 ARM Cortex Architecture
Unit3 ARM Cortex Architecture
ARM CORTEX
❖ CORTEX A: Application processors which are designed to handle complex
applications such as high end embedded operating systems.(Eg- Symbian,
Linux,windows embedded)
➢ requiring the highest processing power, virtual memory system
➢ Required support with memory management units (MMUs)
➢ Required enhanced Java support (optional) and a secure program execution
environment.
➢ Eg: Smart phones, Electronics wallet.
ARM CORTEX
❖ CORTEX R: Real-time, high-performance processors targeted primarily at
the higher end of the real-time market.
➢ Used in applications which requires high processing power and high reliability & low
latency is important.
➢ Eg: high-end breaking systems and hard drive controllers
Cortex-M3 supports only the Thumb-2 (and traditional Thumb) instruction set
so it is not backward compatible with traditional ARM processors.
Cortex M3 Features
Cortex-M3 addresses the requirements for the 32-bit embedded processor
market din following ways
R0–R12 are 32-bit general-purpose registers for data operations. Divided into
low registers (R0–R7) & high registers (R8-R12)
There are two stack pointers (R13) They are banked so that only one is visible
at a time.
This duality allows two separate stack memories to be set up. When using the
register name R13, you can only access the current SP
Cortex M3 Registers
1. Main Stack Pointer (MSP): The default stack pointer, used by the operating
system (OS) kernel and exception handler and and all application codes
that require privileged access.
2. Process Stack Pointer (PSP): Used by user application code
❖ R14 is the link register (LR). Inside BL function1 ; Call function1 using Branch
with Link instruction.
an assembly program, you can
; PC = function1 and
write it as either R14 or LR
❖ When a subroutine is called, the ; LR = the next instruction in main
BX LR ; Return
Cortex M3 Registers
Program Counter (R15)
Eg: MRS r0, PSR ; Read the combined program status word
N Negative
Z Zero
C Carry/borrow
V Overflow
T T Thumb state, always 1; trying to clear this bit will cause a fault exception
PRIMASK A 1-bit register, when this is set, NMI and the hard fault exception; all other
interrupts and exceptions are masked. The default value is 0 (No masking)
FAULTMASK A 1-bit register, when this is set, it allows only the NMI, and all interrupts
and fault handling exceptions are disabled. The default value is 0 (No
masking)
BASEPRI A register of up to 8 bits It defines the masking priority level. When this is
set, it disables all interrupts of the same or lower level.
default value is 0.
MASK Registers (SFR)
In assembly language, the MRS and MSR instructions are used. For example
❖ access to the system control space that is part of the memory region for
configuration registers and debugging components are blocked.
❖ instructions that access special registers (such as MSR, except when
accessing APSR) cannot be used.
❖ If a program running at the user access level tries to access SCS or special
registers, a fault exception will occur.
❖ The core in a privileged access level can switch the program into the user
access level using the control register. BIT [0]-1
Operation modes
❖ When an exception takes place, the processor will always switch to a
privileged state and return to the previous state when exiting the
exception handler
❖ user program cannot change back to the privileged state directly by
writing to the control register.
❖ It has to go through an exception handler that programs the control
register to switch the processor back into privileged access level when
returning to thread mode
Operation modes
Operation modes (Switching at interrupt)
Operation modes (Switching at interrupt user)
Operation modes
Operation modes (Switching at interrupt user)
❖ The support of privileged and user access levels provides a more secure
and robust architecture.
❖ If a user program goes wrong, it will not be able to corrupt control
registers in the Nested Vectored Interrupt Controller (NVIC)
❖ If the Memory Protection Unit (MPU) is present, it is possible to block user
programs from accessing memory regions used by privileged processes.
❖ In simple applications, there is no need to separate the privileged and
user access levels
Operation modes (Switching at interrupt user)
❖ We can separate the user application stack from the kernel stack memory
to avoid the possibility of crashing a system caused by stack operation
errors in user programs.
❖ With this arrangement, the user program (running in thread mode) uses
the PSP, and the exception handlers use the MSP
❖ The switching of SPs is automatic upon entering or leaving the exception
handlers
Operation modes (Switching at interrupt user)
❖ We can separate the user application stack from the kernel stack memory
to avoid the possibility of crashing a system caused by stack operation
errors in user programs.
❖ With this arrangement, the user program (running in thread mode) uses
the PSP, and the exception handlers use the MSP
❖ The switching of SPs is automatic upon entering or leaving the exception
handlers
Exceptions & Interrupts in ARM
Exceptions & Interrupts in ARM
❖ Cortex M3 have fixed number of system exceptions and a number of
interrupts, commonly called IRQ.
❖ The number of interrupt inputs on a Cortex-M3 microcontroller depends
on the individual design.
❖ There is also a nonmaskable interrupt (NMI) input signal. In most cases,
the NMI could be connected to a watchdog timer or a voltage-monitoring
block that warns the processor when the voltage drops below a certain
level.
❖ use of NMI depends on the design of the microcontroller or system-on-
chip (SoC) product.
Vector Table in Cortex M3
Vector Table in Cortex M3
❖ To determine the starting address of the exception handler, a vector table
mechanism is used.
❖ The vector table is relocatable, and the relocation is controlled by a
relocation register in the NVIC.
❖ After reset, this relocation control register is reset to 0; therefore, the
vector table is located in address 0x0 after reset.
Stack Implementation in Cortex M3
Self Study
Two stack model in cortex M3
❖ There are two SP in cortex M3. SP register to be used is controlled by the
control register bit 1 (CONTROL[1].
❖ When CONTROL[1] is 0, the MSP is used for both thread mode and
handler mode In this arrangement, the main program and the exception
handlers share the same stack memory region.
❖ This is the default setting after power-up.
Two stack model in cortex M3
CONTROL[1]=0- Thread and handler use main Stack
Two stack model in cortex M3
❖ When the CONTROL[1] is 1, the PSP is used in thread mode.
❖ In this arrangement the main program and the exception handler can
have separate stack memory regions.
❖ This can prevent a stack error in a user application from damaging the
stack used by the OS.
❖ in this situation, the automatic stacking and unstacking mechanism will
use PSP, whereas stack operations inside the handler will use MSP
Two stack model in cortex M3
CONTROL[1]=1 Thread and handler use separate Stack
Memory Map of CORTEX M3
Memory Map of CORTEX M3
Memory Map of CORTEX M3
❖ The Cortex-M3 processor has a total of 4 GB of address space.
❖ Program code can be located in the code region, the Static Random
Access Memory (SRAM) region, or the external RAM region.
❖ it is best to put the program code in the code region because of
instruction fetches and data accesses are carried out simultaneously on
two separate bus interfaces.
❖ 0.5Gb address range is allocated to on-chip peripherals.
❖ 1 GB memory space are allocated for external RAM and external devices.
➢ Data coming from external peripherals can be stored in that location.
Memory Map of CORTEX M3
❖ 0.5-GB memory is allocated for the system-level components such as
➢ internal peripheral buses, external peripheral bus, and vendor-specific system
peripherals
❖ There are two segments of the private peripheral bus (PPB) those are
accessed as AHB & APB peripherals.
➢ AHB: or Cortex-M3 internal AHB peripherals only; this includes NVIC, FPB, DWT, and ITM
➢ APB: for Cortex-M3 internal APB devices as well as external peripherals (external to the
Cortex-M3 processor) vendor specific peripherals.
❖ The NVIC is located in a memory region called the system control space
(SCS)
Memory Map of CORTEX M3
❖ Besides providing interrupt control features, this region also provides the
control registers for SYSTICK, MPU, and code debugging control
Memory Map of CORTEX M3
Self Study
❖ These two memory regions can be accessed like normal memory, but
they can also be accessed via a separate memory region called the bit-
band alias
Bit- Band Operations
❖ When the bit-band alias address is used, each individual bit can be
accessed separately in the least significant bit (LSB) of each word-aligned
address.
❖ Bit-band region: This is a memory address region that supports bit-band
operation.
❖ Bit-band alias: Access to the bit-band alias will cause an access (a bit-band
operation) to the bit-band region. (Note: A memory remapping is
performed.)
Bit- Band Operations
Remapping of Bit- Band Address
Writing with & without Bit- Band
Writing with & without Bit- Band
Without Bit Band With Bit band
Read address 0x22000008. This read access is remapped into read access to
0x20000000. The return value is 1 (bit[2] of 0x3355AACC).
❖ Startup_device.s
CMSIS file structure
Benefits of CMSIS
Instruction set in Cortex M3
❖ To support Thumb 2 instruction set the Unified Assembler Language(UAL)
was developed to allow selection of 16-bit and 32-bit instructions.
➢ ADD R0, R1 ; R0 = R0 + R1, using Traditional Thumb syntax
➢ ADD R0, R0, R1 ; Equivalent instruction using UAL syntax
❖ If no suffix is given, the assembler tool can choose either instruction but
usually defaults to 16-bit Thumb code to get a smaller size. (with
minimum code size)
Instruction set in Cortex M3
Note:
Some 16 bit Thumb some instructions change the flags in APSR, even if the S
suffix is not used. However, when the UAL syntax is used, whether the
instruction changes the flag depends on the S suffix.
Now R0=0x3456789A
Instruction set in Cortex M3
MVN Instruction
<Rd> Specifies the destination register. If <Rd> is omitted, this register is the same
as <Rn>.
MOV R1,#5
ORN R1, R3
Instruction set in Cortex M3 : Shift & Rotate
Instruction set in Cortex M3 :
Call and Unconditional Branch
Assume R1=0x00008000
Assume R1=0x0005000
Eg: LDRD RD5, RD6 [R1, #0x02]
Instruction set in Cortex M3 : Load/ store(Multiple)
1. LDM - Load multiple values
2. STM - Store multiple values
❖ The TST (test) instruction is more like the AND instruction. It ANDs two values
and updates the flags. However, the result is not stored in any register.
➢ TST R0, R1 ; Calculate R0 AND R1 and update flag
➢ TST R0, #0x12 ; Calculate R0 AND 0x12 and update flag
Instruction set in Cortex M3 : Conditional attributes
Instruction set in Cortex M3
❖ Combined branch & conditions
➢ BEQ label : Branch to address 'label' if Z flag is set
➢ CBZ (compare and branch if zero) and
➢ CBNZ (compare and branch if nonzero)
Instruction set in Cortex M3
Eg: In assembly
The compare and branch instructions
only support forward branches MOV R0, #5 ; Set loop counter
exit(1); BL exit
} email_looks_okay
...
Instruction set in Cortex M3
Combined Compare and Conditional Branch
● The IT (IF-THEN) block is very useful for handling small conditional code. It
avoids branch penalties because there is no change to program flow.
● It can provide a maximum of four conditionally executed instructions.
● The first statement after the IT command must be TRUE-THEN-EXECUTE,
which is always written as ITxyz, where T means THEN and E meansELSE
Instruction set in Cortex M3
IT<x><y><z> <cond> ; IT instruction (<x>, <y>,; <z> can be T or E)
REV reverses the byte order in a data word, and REVH reverses the byte order
inside a half word
Eg: 1. R0=0x12345678
RBIT.W R0, R1
R0=0xC430872D
The four instructions SXTB, SXTH, UXTB, and UXTH are used to extend a byte or
half word data into
For SXTB/SXTH, the data are sign extended using bit[7]/bit[15] of Rn. With UXTB
and UXTH the value is zero extended to 32-bit.
Bit Field Clear (BFC) clears 1–31 adjacent bits in any position of a register. The
syntax of the instruction is as follows:
For example,
LDR R0,=0x1234FFFF
For example,
LDR R0,=0x12345678
LDR R1,=0x3355AACC