pm0223 stm32 Cortexm0 Mcus Programming Manual Stmicroelectronics
pm0223 stm32 Cortexm0 Mcus Programming Manual Stmicroelectronics
Programming manual
STM32 Cortex®-M0+ MCUs programming manual
Introduction
This programming manual provides information for application and system-level software
developers. It gives a full description of the programming model, instruction set, and core
peripherals of the Cortex®-M0+ processor.
Cortex®-M0+ is a high performance 32-bit processor designed for integration in
microcontrollers. It offers significant benefits to developers, including:
• Outstanding processing performance combined with fast interrupt handling
• Enhanced system debug with extensive breakpoint options
• Efficient processor core, system, and memories
• Ultralow power consumption with integrated sleep modes
• Platform security
Contents
2 Cortex-M0+ processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1 Programmers model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.1 Processor modes and privilege levels for software execution . . . . . . . . 12
2.1.2 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.3 Core registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1.4 Exceptions and interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.1.5 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1.6 The Cortex microcontroller software interface standard . . . . . . . . . . . . 19
2.2 Memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.2.1 Memory regions, types, and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.2.2 Memory system ordering of memory accesses . . . . . . . . . . . . . . . . . . . 21
2.2.3 Behavior of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2.4 Additional memory access constraints for caches and shared memory 23
2.2.5 Software ordering of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.6 Memory endianness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.3 Exception model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.3.1 Exception states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.3.2 Exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.3.3 Exception handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3.4 Vector table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3.5 Exception priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3.6 Exception entry and return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.4 Fault handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.4.1 Lockup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.7.1 BKPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.7.2 CPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.7.3 DMB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.7.4 DSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.7.5 ISB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.7.6 MRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.7.7 MSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.7.8 NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.7.9 SEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
3.7.10 SVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.7.11 WFE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.7.12 WFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
List of tables
List of figures
This document provides the information required for application and system-level software
development. It does not provide information on debug components, features, or operation.
This material is for microcontroller software and hardware engineers, including those who
have no experience of Arm®(a) products.
read/write (rw) The software can read and write to these bits.
read-only (r) The software can only read these bits.
write-only (w) The software can only write to this bit.
Reading the bit returns the reset value.
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
read/set (rs) The software can read as well as set this bit.
Writing ‘0’ has no effect on the bit value.
read/clear (rc_w) The software can read as well as clear this bit by writing any value.
read/clear (rc_w1) The software can read as well as clear this bit by writing 1.
Writing ‘0’ has no effect on the bit value.
read/clear (rc_w0) The software can read as well as clear this bit by writing 0.
Writing ‘1’ has no effect on the bit value.
toggle (t) The software can only toggle this bit by writing ‘1’. Writing ‘0’ has no
effect.
Reserved (Res.) Reserved bit, must be kept at reset value.
Cortex-M0+ Components
Cortex-M0+ Processor
Debug
Interrupts Nested
Vectored Cortex-M0+ Breakpoint & Micro Trace
Interrupt processor Watchpoint Buffer
Controller core Units (MTB)
(NVIC)
Memory Debugger
Protection interface
Unit (MPU)
Debug
Access
Bus matrix Port
MS33821V1
The Cortex-M0+ processor is built on a 32-bit processor core that is highly optimized for
area and power, with a 2-stage pipeline Von Neumann architecture. The processor delivers
exceptional energy efficiency through a small but powerful instruction set and extensively
optimized design, providing high-end processing hardware including a single-cycle
multiplier.
The Cortex-M0+ processor implements the ARMv6-M architecture, which is based on the
®
16-bit Thumb instruction set and includes Thumb-2 technology. This provides the
exceptional performance expected of a modern 32-bit architecture, with a higher code
density than other 8-bit and 16-bit microcontrollers.
The Cortex-M0+ processor closely integrates a configurable nested vectored interrupt
controller (NVIC), to deliver industry-leading interrupt performance. The NVIC:
• Includes a non-maskable interrupt (NMI).
• Provides zero jitter interrupt option.
• Provides four interrupt priority levels.
The tight integration of the processor core and NVIC provides fast execution of interrupt
service routines (ISRs), dramatically reducing the interrupt latency. This is achieved through
the hardware stacking of registers, and the ability to abandon and restart load-multiple and
store-multiple operations. Interrupt handlers do not require any assembler wrapper code,
removing any code overhead from the ISRs. Tail-chaining optimization also significantly
reduces the overhead when switching from one ISR to another.
To optimize low-power designs, the NVIC integrates with the sleep modes that include a
deep-sleep function that enables the entire device to be rapidly powered down.
System timer
The system timer, SysTick, is a 24-bit count-down timer. Use this as a real time
operating system (RTOS) tick timer or as a simple counter.
I/O port
The I/O port provides single-cycle loads and stores to tightly coupled peripherals.
2 Cortex-M0+ processor
Thread mode Executes application software. The processor enters thread mode when
it comes out of reset.
Handler mode Handles exceptions. The processor returns to thread mode when it has
finished all exception processing.
In thread mode, the CONTROL register controls whether software execution is privileged or
unprivileged, see CONTROL register on page 18. In Handler mode, software execution is
always privileged.
Only privileged software can write to the CONTROL register to change the privilege level for
software execution in thread mode. Unprivileged software can use the SVC instruction to
make a supervisor call to transfer control to privileged software.
2.1.2 Stacks
The processor uses a full descending stack. This means that the stack pointer indicates the
last stacked item on the stack memory. When the processor pushes a new item onto the
stack, it decrements the stack pointer and then writes the item to the new memory location.
The processor implements two stacks, the main stack and the process stack, with
independent copies of the stack pointer, see Stack pointer on page 14.
In thread mode, the CONTROL register controls whether the processor uses the main stack
or the process stack, see CONTROL register on page 18. In handler mode, the processor
always uses the main stack. The options for processor operations are:
Table 2. Summary of processor mode, execution privilege level, and stack use
options
Privilege level for
Processor mode Used to execute Stack used
software execution
R0
R1
R2
R3
Low registers
R4
R5
R6 General purpose registers
R7
R8
R9
High registers R10 Banked stack pointers
R11
R12
Active Stack Pointer SP (R13) PSP MSP
Link Register LR (R14)
Program Counter PC (R15)
MS33822V1
Stack pointer
The stack pointer (SP) is register R13. In thread mode, bit[1] of the CONTROL register
indicates the stack pointer to use:
• 0 = Main Stack Pointer (MSP). This is the reset value.
• 1 = Process Stack Pointer (PSP).
On reset, the processor loads the MSP with the value from address 0x00000000.
Link register
The link register (LR) is register R14. It stores the return information for subroutines,
function calls, and exceptions. On reset, the LR value is unknown.
Program counter
The program counter (PC) is register R15. It contains the current program address. On
reset, the processor loads the PC with the value of the reset vector, which is at address
0x00000004. Bit[0] of the value is loaded into the EPSR T-bit at reset and must be 1.
31 30 29 28 27 25 24 23 6 5 0
APSR N Z C V Reserved
MS33823V1
Access these registers individually or as a combination of any two or all three registers,
using the register name as an argument to the MSR or MRS instructions. For example:
• Read all of the registers using PSR with the MRS instruction.
• Write to the APSR using APSR with the MSR instruction.
The PSR combinations and attributes are:
See the instruction descriptions MRS on page 74 and MSR on page 75 for more information
about how to access the program status registers.
See The condition flags on page 43 for more information about the APSR negative, zero,
carry or borrow, and overflow flags.
[31:6] - Reserved
[5:0] Exception number This is the number of the current exception:
0 = Thread mode.
1 = Reserved.
2 = NMI.
3 = HardFault.
4-10 = Reserved.
11 = SVCall.
12, 13 = Reserved.
14 = PendSV.
15 = SysTick | Reserved.
16 = IRQ0.
.
.
47 = IRQ31.
48-63 = Reserved.
see Exception types on page 26 for more information.
[31:25] - Reserved.
[23:0] - Reserved.
Attempts by application software to read the EPSR directly using the MRS instruction always
return zero. Attempts to write the EPSR using the MRS instruction are ignored. Fault
handlers can examine the EPSR value in the stacked PSR to determine the cause of the
fault. See Exception entry and return on page 30. The following can clear the T bit to 0:
• Instructions BLX, BX, and POP{PC}.
• Restoration from the stacked xPSR value on an exception return.
• Bit[0] of the vector value on an exception entry.
Attempting to execute instructions when the T bit is 0 results in a HardFault or lockup. See
2.4.1: Lockup on page 33 for more information.
Interruptible-restartable instructions
The interruptible-restartable instructions are LDM and STM, PUSH, POP, and MULS. When
an interrupt occurs during the execution of one of these instructions, the processor
abandons execution of the instruction. After servicing the interrupt, the processor restarts
execution of the instruction from the beginning.
[31:1] - Reserved.
[0] PM Prioritizable interrupt mask:
0 = No effect.
1 = Prevents the activation of all exceptions with configurable
priority.
CONTROL register
The CONTROL register controls the stack used, and the privilege level for software
execution, when the processor is in thread mode. See the register summary in Table 3 on
page 13 for its attributes. The bit assignments are:
31 2 1 0
Reserved
SPSEL
nPRIV
MS33824V1
[31:2] - Reserved.
Defines the current stack:
0 = MSP is the current stack pointer.
[1] SPSEL
1 = PSP is the current stack pointer.
In Handler mode this bit reads as zero and ignores writes.
Defines the thread mode privilege level:
[0] nPRIV 0 = Privileged.
1 = Unprivileged.
Handler mode always uses the MSP, so the processor ignores explicit writes to the active
stack pointer bit of the CONTROL register when in Handler mode. The exception entry and
return mechanisms automatically update the CONTROL register.
In an OS environment, it is recommended that threads running in thread mode use the
process stack and the kernel and exception handlers use the main stack.
By default, thread mode uses the MSP. To switch the stack pointer used in thread mode to
the PSP, use the MSR instruction to set the active stack pointer bit to 1, 3.7.6: MRS on
page 74
Note: When changing the stack pointer, software must use an ISB instruction immediately after the
MSR instruction. This ensures that instructions after the ISB execute using the new stack
pointer. See 3.7.5: ISB on page 73.
handler mode to handle all exceptions except for reset. See Exception entry on page 31 and
Exception return on page 32 for more information.
The NVIC registers control interrupt handling. See Section 4.2: Nested vectored interrupt
controller for more information.
0xFFFFFFFF
Device 511MB
0xE0100000
0xE00FFFFF
Private peripheral bus 1MB
0xE0000000
0xDFFFFFFF
0xA0000000
0x9FFFFFFF
0x60000000
0x5FFFFFFF
Peripheral 0.5GB
0x40000000
0x3FFFFFFF
SRAM 0.5GB
0x20000000
0x1FFFFFFF
Code 0.5GB
0x00000000
MS33825V1
The processor reserves regions of the private peripheral bus (PPB) address range for core
peripheral registers, see 1.3: About the Cortex-M0+ processor and core peripherals on
page 9.
The different ordering requirements for device and Strongly ordered memory mean that the
memory system can buffer a write to device memory, but must not buffer a write to Strongly
ordered memory.
The additional memory attributes include.
Shareable For a shareable memory region, the memory system provides data
synchronization between bus masters in a system with multiple bus
masters, for example, a processor with a DMA controller.
Strongly-ordered memory is always shareable.
If multiple bus masters can access a non-shareable memory region,
software must ensure data coherency between the bus masters.
<This description is required only if the device is likely to be used in
systems where memory is shared between multiple processors.>
Execute Never (XN) Means the processor prevents instruction accesses. A HardFault
exception is generated on executing an instruction fetched from an
XN region of memory.
Normal access - - - -
MS33826V1
1. - Means that the memory system does not guarantee the ordering of the accesses.
< Means that accesses are observed in program order that is A1 is always observed before A2.
0xA0000000-
External device Device XN External device memory.
0xDFFFFFFF
The code, SRAM, and external RAM regions can hold programs.
The MPU can override the default memory access behavior described in this section. For
more information, see 4.5: Memory protection unit on page 98.
2.2.4 Additional memory access constraints for caches and shared memory
When a system includes caches or shared memory, some memory regions have additional
access constraints, and some regions are subdivided, as Table 12 shows:
0x00000000-
Code Normal - WT
0x1FFFFFFF
0x20000000-
SRAM Normal - WBWA
0x3FFFFFFF
0x40000000-
Peripheral Device - -
0x5FFFFFFF
0x60000000-
WBWA
0x7FFFFFFF
External RAM Normal -
0x80000000-
WT
0x9FFFFFFF
0xA0000000-
Shareable
0xBFFFFFFF
External device Device -
0xC0000000-
Non-shareable
0xDFFFFFFF
0xE0000000- Private Peripheral
Strongly- ordered Shareable -
0xE00FFFFF Bus
0xE0100000-
Device Device - -
0xFFFFFFFF
1. See 2.2.1: Memory regions, types, and attributes on page 21 for more information.
2. WT = Write through, no write allocate. WBWA = Write back, write allocate.
DMB The Data Memory Barrier (DMB) instruction ensures that outstanding memory
transactions complete before subsequent memory transactions. See DMB on
page 71.
DSB The Data Synchronization Barrier (DSB) instruction ensures that outstanding
memory transactions complete before subsequent instructions execute. See DSB
on page 72.
ISB The Instruction Synchronization Barrier (ISB) ensures that the effect of all
completed memory transactions is recognizable by subsequent instructions. See
ISB on page 73.
Vector table If the program changes an entry in the vector table, and then enables the
corresponding exception, use a DMB instruction between the operations.
This ensures that if the exception is taken immediately after being
enabled the processor uses the new exception vector.
Self-modifying code If a program contains self-modifying code, use an ISB instruction
immediately after the code modification in the program. This ensures
subsequent instruction execution uses the updated program.
Memory map switching If the system contains a memory map switching mechanism, use a DSB
instruction after switching the memory map. This ensures subsequent
instruction execution uses the updated memory map
MPU programming Use a DSB followed by an ISB instruction or exception return to ensure
that the new MPU configuration is used by subsequent instructions.
VTOR programming If the program updates the value of the VTOR, use a DMB instruction to
ensure that the new vector table is used for subsequent exceptions.
Memory accesses to strongly-ordered memory, such as the system control block, do not
require the use of DMB instructions.
Little-endian format
In little-endian format, the processor stores the least significant byte (lsbyte) of a word at the
lowest-numbered byte, and the most significant byte (msbyte) at the highest-numbered
byte. For example:
Memory Register
7 0
31 24 23 16 15 8 7 0
Address A B0 lsbyte B3 B2 B1 B0
A+1 B1
A+2 B2
A+3 B3 msbyte
MS33827V1
4-10 - Reserved - - -
12-13 - Reserved - - -
15 - Reserved - - -
0x00000040
16 and above 0 and above Interrupt (IRQ) Configurable(3) Asynchronous
and above(4)
1. To simplify the software layer, the CMSIS only uses IRQ numbers. It uses negative values for exceptions
other than interrupts. The IPSR returns the Exception number, see Interrupt program status register on
page 16
2. See Figure 7.: Vector table on page 29 for more information.
3. See 4.2.6: Interrupt priority registers on page 85
4. Increasing in steps of 4.
For an asynchronous exception, other than reset, the processor can execute additional
instructions between when the exception is triggered and when the processor enters the
exception handler.
Privileged software can disable the exceptions that Table 13 on page 27 shows as having
configurable priority, see 4.2.3: Interrupt clear-enable register on page 83.
For more information about HardFaults, see 2.4: Fault handling on page 33
Interrupt Service Routines (ISRs) Interrupts IRQ0 to IRQ31 are the exceptions handled by
ISRs
Fault handler HardFault is the only exception handled by the fault
handler.
System handlers NMI, PendSV, SVCall SysTick, and HardFault are all
system exceptions handled by system handlers.
47 31 IRQ31
0xBC
. . .
. . .
. . .
18 2 IRQ2
0x48
17 1 IRQ1
0x44
16 0 IRQ0
0x40
15 -1 SysTick
0x3C
14 -2 PendSV
0x38
13
Reserved
12
11 -5 SVCall
0x2C
10
9
8
7 Reserved
6
5
4
0x10
3 -13 HardFault
0x0C
2 -14 NMI
0x08
1 Reset
0x04
Initial SP value
0x00
MS33828V1
On system reset, the vector table is fixed at address 0x00000000. Privileged software can
write to the VTOR to relocate the vector table start address to a different memory location
with the respect to vector table size and granularity of TBLOFF settings (see Section 4.3.4:
Vector table offset register).
Note: Configurable priority values are in the range 0-192, in steps of 64. The reset, HardFault, and
NMI exceptions, with fixed negative priority values, always have higher priority than any
other exception.
Assigning a higher priority value to IRQ[0] and a lower priority value to IRQ[1] means that
IRQ[1] has higher priority than IRQ[0]. If both IRQ[1] and IRQ[0] are asserted, IRQ[1] is
processed before IRQ[0].
If multiple pending exceptions have the same priority, the pending exception with the lowest
exception number takes precedence. For example, if both IRQ[0] and IRQ[1] are pending
and have the same priority, then IRQ[0] is processed before IRQ[1].
When the processor is executing an exception handler, the exception handler is preempted
if a higher priority exception occurs. If an exception occurs with the same priority as the
exception being handled, the handler is not preempted, irrespective of the exception
number. However, the status of the new interrupt changes to pending.
Exception entry
Exception entry occurs when there is a pending exception with sufficient priority and either:
• The processor is in thread mode.
• The new exception is of higher priority than the exception being handled, in which case
the new exception preempts the exception being handled.
When one exception preempts another, the exceptions are nested.
Sufficient priority means that the exception has greater priority than any limit set by the
mask register, see Exception mask register on page 17. An exception with less priority than
this is pending but is not handled by the processor.
When the processor takes an exception, unless the exception is a tail-chained or a late-
arriving exception, the processor pushes information onto the current stack. This operation
is referred to as stacking and the structure of eight data words is referred to as a stack
frame. The stack frame contains the following information:
MS33829V1
Immediately after stacking, the stack pointer indicates the lowest address in the stack frame.
The stack frame is aligned to a double-word address.
The stack frame includes the return address. This is the address of the next instruction in
the interrupted program. This value is restored to the PC at exception return so that the
interrupted program resumes.
The processor performs a vector fetch that reads the exception handler start address from
the vector table. When stacking is complete, the processor starts executing the exception
handler. At the same time, the processor writes an EXC_RETURN value to the LR. This
indicates which stack pointer corresponds to the stack frame and what operation mode the
processor was in before the entry occurred.
If no higher priority exception occurs during exception entry, the processor starts executing
the exception handler, and automatically changes the status of the corresponding pending
interrupt to active.
If another higher priority exception occurs during exception entry, the processor starts
executing the exception handler for this exception, and does not change the pending status
of the earlier exception. This is the late arrival case.
Exception return
Exception return occurs when the processor is in handler mode and execution of one of the
following instructions attempts to set the PC to an EXC_RETURN value:
• A POP instruction that loads the PC.
• B PBX instruction using any register.
The processor saves an EXC_RETURN value to the LR on exception entry. The exception
mechanism relies on this value to detect when the processor has completed an exception
handler. Bits[31:4] of an EXC_RETURN value are 0xFFFFFFF. When the processor loads a
value matching this pattern to the PC it detects that the operation is a not a normal branch
operation and, instead that the exception is complete. As a result, it starts the exception
return sequence. Bits[3:0] of the EXC_RETURN value indicate the required return stack and
processor mode, as Table 14 shows.
Note: Only Reset and NMI can preempt the fixed priority HardFault handler. A HardFault can
preempt any exception other than Reset, NMI, or another HardFault.
2.4.1 Lockup
The processor enters a Lockup state if a fault occurs when executing the NMI or HardFault
handlers, or if the system generates a bus error when unstacking the PSR on an exception
return using the MSP. When the processor is in lockup state it does not execute any
instructions. The processor remains in lockup state until one of the following occurs:
• It is reset.
• A debugger halts it.
• An NMI occurs and the current lockup is in the HardFault handler.
Note: If lockup state occurs in the NMI handler a subsequent NMI does not cause the processor to
leave lockup state.
PWR_CR register will select entry in Stop or Standby mode, see the reference manual
chapter “low-power modes” for details.
This section describes the mechanisms for entering sleep mode, and the conditions for
waking up from sleep mode.
1 The processor sets the register to zero and continues executing instructions without
entering sleep mode.
See 3.7.11: WFE on page 79 for more information.
If the event register is 1, this indicates that the processor must not enter sleep mode on
execution of a WFE instruction. Typically, this is because of the assertion of an external
event, or because another processor in the system has executed a SEV instruction, see
3.7.9: SEV on page 77. Software cannot access this register directly.
Sleep-on-exit
If the SLEEPONEXIT bit of the SCR is set to 1, when the processor completes the execution
of an exception handler and returns to thread mode it immediately enters sleep mode. Use
this mechanism in applications that only require the processor to run when an interrupt
occurs.
Some embedded systems might have to execute system restore tasks after the processor
wakes up, and before it executes an interrupt handler. To achieve this set the PRIMASK.PM
bit to 1. If an interrupt arrives that is enabled and has a higher priority than current exception
priority, the processor wakes up but does not execute the interrupt handler until the
processor sets PRIMASK.PM to zero. For more information about PRIMASK, see Exception
mask register on page 17.
ADCS {Rd,} Rn, Rm Add with carry N,Z,C,V 3.5.1 on page 54.
ADD{S} {Rd,} Rn, <Rm|#imm> Add N,Z,C,V 3.5.1 on page 54.
ADR Rd, label PC-relative address to register - 3.4.1 on page 46.
ANDS {Rd,} Rn, Rm Bitwise AND N,Z 3.5.2 on page 56.
ASRS {Rd,} Rm, <Rs|#imm> Arithmetic shift right N,Z,C 3.5.3 on page 57.
B{cc} label Branch {conditionally} - 3.6.1 on page 66.
BICS {Rd,} Rn, Rm Bit clear N,Z 3.5.2 on page 56.
BKPT #imm Breakpoint - 3.7.1 on page 69.
BL label Branch with link - 3.6.1 on page 66.
BLX Rm Branch indirect with link - 3.6.1 on page 66.
BX Rm Branch indirect - 3.6.1 on page 66.
CMN Rn, Rm Compare negative N,Z,C,V 3.5.4 on page 59.
CMP Rn, <Rm|#imm> Compare N,Z,C,V 3.5.4 on page 59.
Change processor state, disable
CPSID i - 3.7.2 on page 70.
interrupts
Change processor state, enable
CPSIE i - 3.7.2 on page 70.
interrupts
DMB - Data memory barrier - 3.7.3 on page 71.
DSB - Data synchronization barrier - 3.7.4 on page 72.
EORS {Rd,} Rn, Rm Exclusive OR N,Z 3.5.2 on page 56.
ISB - Instruction synchronization barrier - 3.7.5 on page 73.
Load multiple registers, increment
LDM Rn{!}, reglist - 3.4.5 on page 50.
after
Load register from PC-relative
LDR Rt, label - 3.4.2 on page 47.
address
LDR Rt, [Rn, <Rm|#imm>] Load register with word - 3.4.2 on page 47.
LDRB Rt, [Rn, <Rm|#imm>] Load register with byte - 3.4.2 on page 47.
LDRH Rt, [Rn, <Rm|#imm>] Load register with halfword - 3.4.2 on page 47.
LDRSB Rt, [Rn, <Rm|#imm>] Load register with signed byte - 3.4.2 on page 47.
LDRSH Rt, [Rn, <Rm|#imm>] Load register with signed halfword - 3.4.2 on page 47.
LSLS {Rd,} Rn, <Rs|#imm> Logical shift left N,Z,C 3.5.3 on page 57.
LSRS {Rd,} Rn, <Rs|#imm> Logical shift right N,Z,C 3.5.3 on page 57.
MOV{S} Rd, Rm Move N,Z 3.5.5 on page 60.
Move to general register from special
MRS Rd, spec_reg - 3.7.6 on page 74.
register
Move to special register from general
MSR spec_reg, Rm N,Z,C,V 3.7.7 on page 75.
register
MULS Rd, Rn, Rm Multiply, 32-bit result N,Z 3.5.6 on page 61.
MVNS Rd, Rm Bitwise NOT N,Z 3.5.5 on page 60.
NOP - No operation - 3.7.8 on page 76.
ORRS {Rd,} Rn, Rm Logical OR N,Z 3.5.2 on page 56.
POP reglist Pop registers from stack - 3.4.6 on page 52.
PUSH reglist Push registers onto stack - 3.4.6 on page 52.
REV Rd, Rm Byte-reverse word - 3.5.7 on page 62.
REV16 Rd, Rm Byte-reverse packed halfwords - 3.5.7 on page 62.
REVSH Rd, Rm Byte-reverse signed halfword - 3.5.7 on page 62.
RORS {Rd,} Rn, Rs Rotate right N,Z,C 3.5.3 on page 57.
RSBS {Rd,} Rn, #0 Reverse subtract N,Z,C,V 3.5.1 on page 54.
SBCS {Rd,} Rn, Rm Subtract with carry N,Z,C,V 3.5.1 on page 54.
SEV - Send event - 3.7.9 on page 77.
Store multiple registers, increment
STM Rn!, reglist - 3.4.5 on page 50.
after
STR Rt, [Rn, <Rm|#imm>] Store register as word - 3.4.2 on page 47.
STRB Rt, [Rn, <Rm|#imm>] Store register as byte - 3.4.2 on page 47.
STRH Rt, [Rn, <Rm|#imm>] Store register as halfword - 3.4.2 on page 47.
SUB{S} {Rd,} Rn, <Rm|#imm> Subtract N,Z,C,V 3.5.1 on page 54.
SVC #imm Supervisor call - 3.7.10 on page 78.
SXTB Rd, Rm Sign extend byte - 3.5.8 on page 63.
SXTH Rd, Rm Sign extend halfword - 3.5.8 on page 63.
TST Rn, Rm Logical AND based test N,Z 3.5.9 on page 64.
UXTB Rd, Rm Zero extend a byte - 3.5.8 on page 63.
UXTH Rd, Rm Zero extend a halfword - 3.5.8 on page 63.
The CMSIS also provides a number of functions for accessing the special registers using
MRS and MSR instructions.
:
3.3.1 Operands
An instruction operand can be an Arm® register, a constant, or another instruction-specific
parameter. Instructions act on the operands and often store the result in a destination
register. When there is a destination register in the instruction, it is usually specified before
the other operands.
Figure 9. ASR#3
Carry
Flag
31 5 4 3 2 1 0
...
MS33830V1
LSR
Logical shift right by n bits moves the left-hand 32-n bits of the register Rm, to the right by n
places, into the right-hand 32-n bits of the result, and it sets the left-hand n bits of the result
to 0. See Figure 10 on page 41.
The user can use the LSR operation to divide the value in the register Rm by 2n, if the value
is regarded as an unsigned integer.
When the instruction is LSRS, the carry flag is updated to the last bit shifted out, bit[n-1], of
the register Rm.
Note: If n is 32 or more, then all the bits in the result are cleared to 0.
If n is 33 or more and the carry flag is updated, it is updated to 0.
Carry
0 0 0
Flag
31 5 4 3 2 1 0
...
MS33831V1
LSL
Logical shift left by n bits moves the right-hand 32-n bits of the register Rm, to the left by n
places, into the left-hand 32-n bits of the result, and it sets the right-hand n bits of the result
to 0. See Figure 11 on page 42.
The user can use the LSL operation to multiply the value in the register Rm by 2n, if the value
is regarded as an unsigned integer or a two’s complement signed integer. Overflow can
occur without warning.
When the instruction is LSLS the carry flag is updated to the last bit shifted out, bit[32-n],
of the register Rm. These instructions do not affect the carry flag when used with LSL#0.
Note: If n is 32 or more, then all the bits in the result are cleared to 0.
If n is 33 or more and the carry flag is updated, it is updated to 0.
0 0 0
31 5 4 3 2 1 0
Carry
Flag
...
MS33832V1
ROR
Rotate right by n bits moves the left-hand 32-nbits of the register Rm, to the right by n places,
into the right-hand 32-n bits of the result, and it moves the right-hand n bits of the register
into the left-hand n bits of the result. See Figure 12 on page 42.
When the instruction is RORS the carry flag is updated to the last bit rotation, bit[n-1], of the
register Rm.
Note: If n is 32, then the value of the result is same as the value in Rm, and if the carry flag is
updated, it is updated to bit[31] of Rm.
If ROR with shift length, n, greater than 32 is the same as ROR with shift length n-32.
Carry
Flag
31 5 4 3 2 1 0
...
MS33833V1
N Set to 1 when the result of the operation was negative, cleared to 0 otherwise
Z Set to 1 when the result of the operation was zero, cleared to 0 otherwise.
C Set to 1 when the operation resulted in a carry, cleared to 0 otherwise.
V Set to 1 when the operation caused overflow, cleared to 0 otherwise.
For more information about the APSR, see Program status register on page 14.
A carry occurs:
• If the result of an addition is greater than or equal to 232.
• If the result of a subtraction is positive or zero.
• As the result of a shift or rotate instruction.
Overflow occurs when the sign of the result, in bit[31], does not match the sign of the result
had the operation been performed at infinite precision, for example:
• If adding two negative values results in a positive value.
• If adding two positive values results in a negative value.
• If subtracting a positive value from a negative value generates a positive value.
• If subtracting a negative value from a positive value generates a negative value.
The compare operations are identical to subtracting, for CMP, or adding, for CMN, except that
the result is discarded. See the instruction descriptions for more information.
3.4.1 ADR
Generates a PC-relative address.
Syntax
ADR Rd, label
Where:
Operation
ADR generates an address by adding an immediate value to the PC, and writes the result to
the destination register.
ADR facilitates the generation of position-independent code, because the address is
PC-relative.
If the user uses ADR to generate a target address for a BX or BLX instruction, the user must
ensure that the bit[0] of the address the user generates is set to 1 for correct execution.
Restrictions
In this instruction Rd must specify R0-R7. The data-value addressed must be word aligned
and within 1020 bytes of the current PC.
Condition flags
This instruction does not change the flags.
Examples:
ADR R1, TextMessage ; Write address value of a location labeled as;
TextMessage to R1
ADR R3, [PC,#996] ; Set R3 to the value of PC + 996.
Operation
LDR, LDRB, and LDRH instructions load the register specified by Rt with either a word, byte,
or halfword data value from memory. Sizes less than word are zero extended to 32-bits
before being written to the register specified by Rt.
STR, STRB, and STRH instructions store the word, least-significant byte, or lower
halfword contained in the single register specified by Rt in to memory. The memory
address, to load from or store to, is the sum of the value in the register specified by either Rn
or SP and the immediate value imm.
Restrictions
In these instructions:
• Rt and Rn must only specify R0-R7.
• imm must be between:
– 0 and 1020 and an integer multiple of four for LDR and STR using SP as the base
register.
– 0 and 124 and an integer multiple of four for LDR and STR using R0-R7 as the
base register.
– 0 and 62 and an integer multiple of two for LDRH and STRH.
– 0 and 31 for LDRB and STRB.
• The computed address must be divisible by the number of bytes in the transaction, see
3.3.4: Address alignment on page 42.
Condition flags
These instructions do not change the flags.
Examples
LDR R4, [R7 ; Loads R4 from the address in R7.
STR R2,[R0,#const-struc] ; const-struc is an expression evaluating
; to a constant in the range 0-1020.
Syntax
LDR Rt, [Rn, Rm]
LDR<B|H> Rt, [Rn, Rm]
LDR<SB|SH> Rt, [Rn, Rm]
STR Rt, [Rn, Rm]
STR<B|H> Rt, [Rn, Rm]
Where:
Rt Is the register to load or store.
Rn Is the register on which the memory address is based
Rm s a register containing a value to be used as the offset
Operation
LDR, LDRB, LDRH, LDRSB and LDRSH load the register specified by Rt with either a
word, zero extended byte, zero extended halfword, sign extended byte or sign extended
halfword value from memory.
STR, STRB and STRH store the word, least-significant byte or lower halfword contained in
the single register specified by Rt into memory.
The memory address to load from or store to is the sum of the values in the registers
specified by Rn and Rm.
Restrictions
In these instructions:
• Rt, Rn, and Rm must only specify R0-R7.
• The computed memory address must be divisible by the number of bytes in the load or
store, see 3.3.4: Address alignment on page 42.
Condition flags
These instructions do not change the flags.
Examples
STR R0, [R5, R1] ; Store value of R0 into an address equal to
; sum of R5 and R1
LDRSH R1, [R2, R3] ; Load a halfword from the memory address
; specified by (R2 + R3), sign extend to 32-bits
; and write to R1.
Syntax
LDR Rt, label
Where:
Rt Is the register to load
label Is a PC-relative expression. See 3.3.5: PC-relative expressions on page 43.
Operation
Loads the register specified by Rt from the word in memory specified by label.
Restrictions
In these instructions, label must be within 1020 bytes of the current PC and word aligned.
Condition flags
These instructions do not change the flags.
Examples
LDR R0, LookUpTable ; Load R0 with a word of data from an address
; labelled as LookUpTable.
LDR R3, [PC, #100] ; Load R3 with memory word at (PC + 100).
LDMIA and LDMFD are synonyms for LDM. LDMIA refers to the base register being
Incremented After each access. LDMFD refers to its use for popping data from full
descending stacks.
STMIA and STMEA are synonyms for STM. STMIA refers to the base register being
incremented after each access. STMEA refers to its use for pushing data onto empty
ascending stacks.
Operation
LDM instructions load the registers in reglist with word values from memory addresses
based on Rn.
STM instructions store the word values in the registers in reglist to memory addresses
based on Rn.
The memory addresses used for the accesses are at 4-byte intervals ranging from the value
in the register specified by Rn to the value in the register specified by Rn + 4 * (n-1), where n
is the number of registers in reglist. The accesses happens in order of increasing
register numbers, with the lowest numbered register using the lowest memory address and
the highest number register using the highest memory address. If the writeback suffix is
specified, the value in the register specified by Rn + 4 *n is written back to the register
specified by Rn.
Restrictions
In these instructions:
• reglist and Rn are limited to R0-R7.
• The writeback suffix must always be used unless the instruction is an LDM where reglist
also contains Rn, in which case the writeback suffix must not be used.
• The value in the register specified by Rn must be word aligned. See 3.3.4: Address
alignment on page 42 for more information.
• For STM, if Rn appears in reglist, then it must be the first register in the list.
Condition flags
These instructions do not change the flags.
Examples
LDM R0,{R0,R3,R4} ; LDMIA is a synonym for LDM
STMIA R1!,{R2-R4,R6}
Incorrect examples
STM R5!,{R4,R5,R6} ;Value stored for R5 is unpredictable
LDM R2,{} ; There must be at least one register in the
list
Syntax
PUSH reglist
POP reglist
Where:
reglist Is a non-empty list of registers, enclosed in braces. It can contain register
ranges. It must be comma separated if it contains more than one register or
register range.
Operation
PUSH stores registers on the stack, with the lowest numbered register using the lowest
memory address and the highest numbered register using the highest memory address.
POP loads registers from the stack, with the lowest numbered register using the lowest
memory address and the highest numbered register using the highest memory address.
PUSH uses the value in the SP register minus four as the highest memory address, POP
uses the value in the SP register as the lowest memory address, implementing a full-
descending stack. On completion, PUSH updates the SP register to point to the location of
the lowest store value, POP updates the SP register to point to the location above the
highest location loaded.
If a POP instruction includes PC in its reglist, a branch to this location is performed when
the POP instruction has completed. Bit[0] of the value read for the PC is used to update the
APSR T-bit. This bit must be 1 to ensure correct operation.
Restrictions
In these instructions:
• reglist must use only R0-R7.
• The exception is LR for a PUSH and PC for a POP.
Condition flags
These instructions do not change the flags.
Examples
PUSH {R0,R4-R7} ; Push R0,R4,R5,R6,R7 onto the stack
PUSH {R2,LR} ; Push R2 and the link-register onto the stack
POP {R0,R6,PC} ; Pop r0,r6 and PC from the stack, then branch to
; the new PC.
Syntax
ADCS {Rd,} Rn, Rm
ADD{S} {Rd,} Rn, <Rm|#imm>
RSBS {Rd,} Rn, Rm, #0
SBCS {Rd,} Rn, Rm
SUB{S} {Rd,} Rn, <Rm|#imm>
Where:
S Causes an ADD or SUB instruction to update flags.
Rd Specifies the result register.
reglist Specifies the first source register.
Imm Specifies a constant immediate value.
When the optional Rd register specifier is omitted, it is assumed to take the same value as
Rn, for example ADDS R1,R2 is identical to ADDS R1,R1,R2.
Operation
The ADCS instruction adds the value in Rn to the value in Rm, adding another one if the carry
flag is set, places the result in the register specified by Rd and updates the N, Z, C, and V
flags.
The ADD instruction adds the value in Rn to the value in Rm or an immediate value specified
by imm and places the result in the register specified by Rd.
The ADDS instruction performs the same operation as ADD and also updates the N, Z, C and
V flags.
The RSBS instruction subtracts the value in Rn from zero, producing the arithmetic negative
of the value, and places the result in the register specified by Rd and updates the N, Z, C
and V flags.
The SBCS instruction subtracts the value of Rm from the value in Rn, if the carry flag is clear,
the result is reduced by one. It places the result in the register specified by Rd and updates
the N, Z, C and V flags.
The SUB instruction subtracts the value in Rm or the immediate specified by imm. It places
the result in the register specified by Rd.
The SUBS instruction performs the same operation as SUB and also updates the N, Z, C and
V flags.
Use ADC and SBC to synthesize multiword arithmetic, see Examples on page 55.
See also 3.4.1: ADR on page 46.
Restrictions
Table 21 lists the legal combinations of register specifiers and immediate values that can be
used with each instruction.
Table 21. ADC, ADD, RSB, SBC and SUB operand restrictions
Instruction Rd Rn Rm imm Restrictions
ADCS R0-R7 R0-R7 R0-R7 - Rd and Rn must specify the same register.
Rd and Rn must specify the same register.
R0-R15 R0-R15 R0-PC -
Rn and Rm must not both specify PC.
SP or Immediate value must be an integer multiple of
ADD R0-R7 - 0-1020
PC four.
Immediate value must be an integer multiple of
SP SP - 0-508
four.
R0-R7 R0-R7 - 0-7 -
ADDS R0-R7 R0-R7 - 0-255 Rd and Rn must specify the same register.
R0-R7 R0-R7 R0-R7 - -
RSBS R0-R7 R0-R7 - - -
SBCS R0-R7 R0-R7 R0-R7 - Rd and Rn must specify the same register.
Immediate value must be an integer multiple of
SUB SP SP - 0-508
four.
R0-R7 R0-R7 - 0-7 -
SUBS R0-R7 R0-R7 - 0-255 Rd and Rn must specify the same register.
R0-R7 R0-R7 R0-R7 - -
Examples
Example 1 shows two instructions that add a 64-bit integer contained in R0 and R1 to
another 64-bit integer contained in R2 and R3, and place the result in R0 and R1.
Multiword values do not have to use consecutive registers. Example 2 shows instructions
that subtract a 96-bit integer contained in R1, R2, and R3 from another contained in R4, R5,
and R6. The example stores the result in R4, R5, and R6.
Syntax
ANDS {Rd,} Rn, Rm
ORRS {Rd,} Rn, Rm
EORS {Rd,} Rn, Rm
BICS {Rd,} Rn, Rm
Where:
Operation
The AND, EOR, and ORR instructions perform bitwise AND, exclusive OR, and inclusive
OR operations on the values in Rn and Rm.
The BIC instruction performs an AND operation on the bits in Rn with the logical negation of
the corresponding bits in the value of Rm.
The condition code flags are updated on the result of the operation, see Condition flags on
page 47.
Restrictions
In these instructions, Rd, Rn, and Rm must only specify R0-R7.
Condition flags
These instructions:
Update the N and Z flags according to the result.
Do not affect the C or V flag.
Examples
ANDS R2, R2, R1
ORRS R2, R2, R5
ANDS R5, R5, R8
EORS R7, R7, R6
BICS R0, R0, R1
Syntax
ASRS {Rd,} Rm, Rs
ASRS {Rd,} Rm, #imm
LSLS {Rd,} Rm, Rs
LSLS {Rd,} Rm, #imm
LSRS {Rd,} Rm, Rs
LSRS {Rd,} Rm, #imm
RORS {Rd,} Rm, Rs
Where:
Operation
ASR, LSL, LSR, and ROR perform an arithmetic-shift-left, logical-shift-left, logical-shift-
right or a right-rotation of the bits in the register Rm by the number of places specified by the
immediate imm or the value in the least-significant byte of the register specified by Rs.
For details on what result is generated by the different instructions, see 3.3.3: Shift
operations on page 40.
Restrictions
In these instructions, Rd, Rm, and Rs must only specify R0-R7. For non-immediate
instructions, Rd and Rm must specify the same register.
Condition flags
These instructions update the N and Z flags according to the result.
The C flag is updated to the last bit shifted out, except when the shift length is 0, see 3.3.3:
Shift operations on page 40. The V flag is left unmodified.
Examples
ASRS R7, R5, #9 ; Arithmetic shift right by 9 bits
LSLS R1, R2, #3 ; Logical shift left by 3 bits with flag update
LSRS R4, R5, #6 ; Logical shift right by 6 bits
RORS R4, R4, R6 ; Rotate right by the value in the bottom byte of R6.
Syntax
CMN Rn, Rm
CMP Rn, #imm
CMP Rn, Rm
Where:
Operation
These instructions compare the value in a register with either the value in another register or
an immediate value. They update the condition flags on the result, but do not write the result
to a register.
The CMP instruction subtracts either the value in the register specified by Rm, or the
immediate imm from the value in Rn and updates the flags. This is the same as a SUBS
instruction, except that the result is discarded.
The CMN instruction adds the value of Rm to the value in Rn and updates the flags. This is the
same as an ADDS instruction, except that the result is discarded.
Restrictions
For the:
• CMN instruction Rn, and Rm must only specify R0-R7.
• CMP instruction:
– Rn and Rm can specify R0-R14.
– Immediate must be in the range 0-255.
Condition flags
These instructions update the N, Z, C and V flags according to the result.
Examples
CMP R2, R9
CMN R0, R2
Syntax
MOV{S} Rd, Rm
MOVS Rd, #imm
MVNS Rd, Rm
Where:
Operation
The MOV instruction copies the value of Rm into Rd.
The MOVS instruction performs the same operation as the MOV instruction, but also updates
the N and Z flags.
The MVSN instruction takes the value of Rm, performs a bitwise logical negate operation on
the value, and places the result into Rd.
Restrictions
In these instructions, Rd, and Rm must only specify R0-R7.
When Rd is the PC in a MOV instruction:
• Bit[0] of the result is discarded.
• A branch occurs to the address created by forcing the bit[0] of the result to 0. The T-bit
remains unmodified.
Note: Though it is possible to use MOV as a branch instruction, Arm® strongly recommends the
use of a BX, or BLX instruction to branch for software portability.
Condition flags
If S is specified, these instructions:
• update the N and Z flags according to the result
• do not affect the C or V flags.
Example
MOVS R0, #0x000B ; Write value of 0x000B to R0, flags get updated
MOVS R1, #0x0 ; Write value of zero to R1, flags are updated
MOV R10, R12 ; Write value in R12 to R10, flags are not updated
MOVS R3, #23 ; Write value of 23 to R3
MOV R8, SP ; Write value of stack pointer to R8
MVNS R2, R0 ; Write inverse of R0 to the R2 and update flags
3.5.6 MULS
Multiply using 32-bit operands, and producing a 32-bit result.
Syntax
MULS Rd, Rn, Rm
Where:
Operation
The MUL instruction multiplies the values in the registers specified by Rn and Rm, and places
the least significant 32 bits of the result in Rd. The condition code flags are updated on the
result of the operation, see 3.3.6: Conditional execution on page 43.
The results of this instruction does not depend on whether the operands are signed or
unsigned.
Restrictions
In this instruction:
• Rd, Rn, and Rm must only specify R0-R7.
• Rd must be the same as Rm.
Condition flags
This instruction:
• Updates the N and Z flags according to the result.
• Does not affect the C or V flags.
Examples
MULS R0, R2, R0 ; Multiply with flag update, R0 = R0 x R2
Syntax
REV Rd, Rn
REV16 Rd, Rn
REVSH Rd, Rn
Where:
Operation
Use these instructions to change the endianness of data:
RER
REV Converts 32-bit big-endian data into little-endian data or 32-bit little-endian
data into big-endian data.
REV16 Converts two packed 16-bit big-endian data into little-endian data or two
packed 16-bit little-endian data into big-endian data.
REVSH Converts 16-bit signed big-endian data into 32-bit signed little-endian data or
16-bit signed little-endian data into 32-bit signed big-endian data.
Restrictions
In these instructions, Rd, and Rn must only specify R0-R7.
Condition flags
These instructions do not change the flags.
Examples
REV R3, R7 ; Reverse byte order of value in R7 and write it to R3
REV16 R0, R0 ; Reverse byte order of each 16-bit halfword in R0
REVSH R0, R5 ; Reverse signed halfword
Syntax
SXTB Rd, Rm
SXTH Rd, Rm
UXTB Rd, Rm
UXTH Rd, Rm
Where:
Rd Is the destination register.
Rm Is the register holding the value to be extended.
Operation
• These instructions extract bits from the resulting value:
• SXTB extracts bits[7:0] and sign extends to 32 bits.
• UXTB extracts bits[7:0] and zero extends to 32 bits.
• SXTH extracts bits[15:0] and sign extends to 32 bits.
• UXTH extracts bits[15:0] and zero extends to 32 bits.
Restrictions
In these instructions, Rd and Rm must only specify R0-R7.
Condition flags
These instructions do not affect the flags.
Examples
SXTH R4, R6 ; Obtain the lower halfword of the
; value in R6 and then sign extend to
; 32 bits and write the result to R4.
UXTB R3, R1 ; Extract lowest byte of the value in R10 and zero
; extend it, and write the result to R3
3.5.9 TST
Test bits.
Syntax
TST Rn, Rm
Where:
Rn Is the register holding the first operand.
Rm The register to test against.
Operation
This instruction tests the value in a register against another register. It updates the condition
flags based on the result, but does not write the result to a register.
The TST instruction performs a bitwise AND operation on the value in Rn and the value in
Rm. This is the same as the ANDS instruction, except that it discards the result.
To test whether a bit of Rn is 0 or 1, use the TST instruction with a register that has that bit
set to 1 and all other bits cleared to 0.
Restrictions
In these instructions, Rn and Rm must only specify R0-R7.
Condition flags
This instruction:
• updates the N and Z flags according to the result
• does not affect the C or V flags.
Examples
TST R0, R1 ; Perform bitwise AND of R0 value and R1 value,
; condition code flags are updated but result is
discarded.
B{cc} Branch {conditionally} 3.6.1: B, BL, BX, and BLX on page 66.
BL Branch with Link 3.6.1: B, BL, BX, and BLX on page 66.
BLX Branch indirect with Link 3.6.1: B, BL, BX, and BLX on page 66.
BX Branch indirect 3.6.1: B, BL, BX, and BLX on page 66.
Syntax
B{cond} label
BL label
BX Rm
BLX Rm
Where:
Cond Is an optional condition code, see 3.3.6: Conditional execution on page 43.
label Is a PC-relative expression. See 3.3.5: PC-relative expressions on page 43.
Rm Is a register providing the address to branch to.
Operation
All these instructions cause a branch to the address indicated by the label or contained in
the register specified by Rm. In addition:
• the BL and BLX instructions write the address of the next instruction to LR, the link
register R14.
• the BX and BLX instructions result in a HardFault exception if bit[0] of Rm is 0.
BL and BLX instructions also set bit[0] of the LR to 1. This ensures that the value is suitable
for use by a subsequent POP {PC} or BX instruction to perform a successful return branch.
Table 23 shows the ranges for the various branch instructions.
.
B label −2 KB to +2 KB.
Bcond label −256 bytes to +254 bytes.
BL label −16 MB to +16 MB.
BX Rm Any value in register.
BLX Rm Any value in register.
Restrictions
In these instructions:
• Do not use SP or PC in the BX or BLX instruction.
• For BX and BLX, the bit[0] of Rm must be 1 for correct execution. Bit[0] is used to update
the EPSR T-bit and is discarded from the target address.
Condition flags
These instructions do not change the flags.
Examples
B loopA ; Branch to loopA
BL funC ; Branch with link (Call) to function funC, return address
; stored in LR
BX LR ; Return from function call
BLX R0 ; Branch with link and exchange (Call) to an address
stored
; in R0
BEQ labelD ; Conditionally branch to labelD if last flag setting
; instruction set the Z flag, else do not branch.
3.7.1 BKPT
Breakpoint.
Syntax
BKPT #imm
Where:
Imm Is an integer in the range 0-255.
Operation
The BKPT instruction causes the processor to enter debug state. Debug tools can use this to
investigate system state when the instruction at a particular address is reached.
Imm is ignored by the processor. If required, a debugger can use it to store additional
information about the breakpoint.
The processor might also produce a HardFault or go in to Lockup if a debugger is not
attached when a BKPT instruction is executed. See 2.4.1: Lockup on page 33 for more
information.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
BKPT #0 ; Breakpoint with immediate value set to 0x0.
3.7.2 CPS
Change processor state.
Syntax
CPSID i
CPSIE i
Operation
CPS changes the PRIMASK special register values. CPSID causes interrupts to be disabled
by setting PRIMASK. CPSIE cause interrupts to be enabled by clearing PRIMASK. See
Exception mask register on page 17 for more information about these registers.
Restrictions
If the current mode of execution is not privileged, then this instruction behaves as a NOP and
does not change the current state of PRIMASK.
Condition flags
This instruction does not change the condition flags.
Examples
CPSID i ; Disable all interrupts except NMI (set PRIMASK.PM)
CPSIE i ; Enable interrupts (clear PRIMASK.PM)
3.7.3 DMB
Data memory barrier.
Syntax
DMB
Operation
DMB acts as a data memory barrier. It ensures that all explicit memory accesses that appear
in program order before the DMB instruction are observed before any explicit memory
accesses that appear in program order after the DMB instruction. DMB does not affect the
ordering of instructions that do not access memory.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
DMB ; Data memory barrier
3.7.4 DSB
Data synchronization barrier.
Syntax
DSB
Operation
DSB acts as a special data synchronization memory barrier. Instructions that come after the
DSB, in program order, do not execute until the DSB instruction completes. The DSB
instruction completes when all explicit memory accesses before it complete.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
DSB ; Data synchronisation barrier
3.7.5 ISB
Instruction synchronization barrier.
Syntax
ISB
Operation
ISB acts as an instruction synchronization barrier. It flushes the pipeline of the processor, so
that all instructions following the ISB are fetched from cache or memory again, after the ISB
instruction has been completed.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
ISB ; Instruction synchronization barrier
3.7.6 MRS
Move the contents of a special register to a general-purpose register.
Syntax
MRS Rd, spec_reg
Where:
Rd Is the general purpose destination register.
spec_reg Is one of the special purpose registers: APSR, IPSR, EPSR, IEPSR,
IAPSR, EAPSR, PSR, MSP, PSP, PRIMASK, or CONTROL.
Operation
MSR stores the contents of a special-purpose register to a general purpose register. The MSR
instruction can be combined with the MSR instruction to produce read-modify-write
sequences, which are suitable for modifying a specific flag in the PSR.
See 3.7.7: MSR on page 75.
Restrictions
In this instruction, Rd must not be SP or PC.
If the current mode of execution is not privileged, then the values of all registers other than
the APSR read as zero.
Condition flags
This instruction does not change the flags.
Examples
MRS R0, PRIMASK ; Read PRIMASK value and write it to R0
3.7.7 MSR
Move the contents of a general-purpose register into the specified special register.
Syntax
MSR spec_reg, Rn
Where:
Rn Is the general-purpose source register.
spec_reg Is the special-purpose destination register: APSR, IPSR, EPSR, IEPSR,
IAPSR, EAPSR, PSR, MSP, PSP, PRIMASK, or CONTROL.
Operation
MSR updates one of the special registers with the value from the register specified by Rn.
See 3.7.6: MRS on page 74.
Restrictions
In this instruction, Rn must not be SP and must not be PC.
If the current mode of execution is not privileged, then all attempts to modify any register
other than the APSR are ignored.
Condition flags
This instruction updates the flags explicitly based on the value in Rn.
Examples
MSR CONTROL, R1 ; Read R1 value and write it to the CONTROL register.
3.7.8 NOP
No operation.
Syntax
NOP
Operation
NOP performs no operation and is not guaranteed to be time consuming. The processor
might remove it from the pipeline before it reaches the execution stage.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
NOP ; No operation
3.7.9 SEV
Send Event.
Syntax
SEV
Operation
SEV causes an event to be signaled to all processors within a multiprocessor system. It also
sets the local event register, see2.5: Power management on page 33.
See also 3.7.11: WFE on page 79.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
SEV ; Send event
3.7.10 SVC
Supervisor call.
Syntax
SVC #imm
Where:
Imm Is an integer in the range 0-255.
Operation
The SVC instruction causes the SVC exception.
Imm is ignored by the processor. If required, it can be retrieved by the exception handler to
determine what service is being requested.
Restrictions
Executing the SVC instruction, while the current execution priority level is greater than or
equal to that of the SVCall handler, results in a fault being generated.
Condition flags
This instruction does not change the flags.
Examples
SVC #0x32 ; Supervisor call (SVC handler can extract the immediate
value.
; by locating it through the stacked PC)
3.7.11 WFE
Wait for event.
Syntax
WFE
Operation
If the event register is 0, WFE suspends execution until one of the following events occurs:
• An exception, unless masked by the exception mask registers or the current priority
level.
• An exception enters the pending state, if SEVONPEND in the system control register is
set.
• A debug entry request, if debug is enabled.
• An event signaled by a peripheral or another processor in a multiprocessor system
using the SEV instruction.
If the event register is 1, WFE clears it to 0 and completes immediately.
For more information, see 2.5: Power management on page 33.
Note: WFE is intended for power saving only. When writing the software, it is assumed that WFE
might behave as NOP.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
WFE ; Wait for event
3.7.12 WFI
Wait for interrupt.
Syntax
WFI
Operation
WFI suspends execution until one of the following events occurs:
• An exception.
• An interrupt becomes pending which would preempt if PRIMASK.PM was clear.
• A debug entry request, regardless of whether debug is enabled.
Note: WFI is intended for power saving only. When writing software, it is assumed that WFI might
behave as a NOP operation.
Restrictions
There are no restrictions.
Condition flags
This instruction does not change the flags.
Examples
WFI ; Wait for interrupt
• the required privilege gives the privilege level required to access the register, as
follows:
Privileged
Only privileged software can access the register.
Unprivileged
Both unprivileged and privileged software can access the register.
SETPENA[31:16]
rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SETPENA[15:0]
rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs
If a pending interrupt is enabled, the NVIC activates the interrupt based on its priority. If an
interrupt is not enabled, asserting its interrupt signal changes the interrupt state to pending,
but the NVIC never activates the interrupt, regardless of its priority.
CLRENA[31:16]
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CLRENA[15:0]
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
SETPEND[31:16]
rs rs rs rs rs rs rs rs rs rs7 rs rs rs rs rs rs
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SETPEND[15:0]
rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs
CLRPEND[31:16]
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CLRPEND[15:0]
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
Note: Writing 1 to an NVIC_ICPR bit does not affect the active state of the corresponding
interrupt.
31 24 23 16 15 8 7 0
...
...
MS33834V1
[31:24] Priority, byte offset 3 Each priority field holds a priority value, 0-192. The
lower the value, the greater the priority of the
[23:16] Priority, byte offset 2
corresponding interrupt. The processor implements
[15:8] Priority, byte offset 1 only bits[7:6] of each field, bits [5:0] read as zero
and ignore writes. This means writing 255 to a
[7:0] Priority, byte offset 0
priority register saves value 192 to the register.
See 4.2.1: Accessing the Cortex-M0+ NVIC registers using CMSIS on page 82 for more
information about the access to the interrupt priority array, which provides the software view
of the interrupt priorities.
Find the NVIC_IPR number and byte offset for interrupt M as follows:
• The corresponding NVIC_IPR number, N, is given by N = N DIV 4.
• The byte offset of the required priority field in this register is M MOD 4, where:
– Byte offset 0 refers to register bits[7:0].
– Byte offset 1 refers to register bits[15:8].
– Byte offset 2 refers to register bits[23:16].
– Byte offset 3 refers to register bits[31:24].
For a level-sensitive interrupt, if the interrupt signal is still asserted, the state of the interrupt
does not change. Otherwise, the state of the interrupt changes to inactive.
For a pulse interrupt, state of the interrupt changes to:
– Inactive, if the state was pending.
– Active, if the state was active and pending.
The input parameter IRQn is the IRQ number, see Table 13 on page 27. For more
information about these functions, see the CMSIS documentation.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PART No REVISION
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
NMIPE PEND PEND PEND PENDS ISRPR ISRPE
VECTPENDING[8:4]
NDSET Reserved SVSET SVCLR STSET TCLR Reserved EEMPT NDING Reserved
rw rw w rw w r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
VECTPENDING[3:0] VECTACTIVE[8:0]
Reserved
r r r r rw rw rw rw rw rw rw rw rw
When the user writes to the ICSR, the effect is unpredictable if:
• write 1 to the PENDSVSET bit and write 1 to the PENDSVCLR bit
• write 1 to the PENDSTSET bit and write 1 to the PENDSTCLR bit.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TBLOFF[31:16]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TBLOFF[15:7]
Reserved
rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
VECTKEYSTAT
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SYS VECT
ENDIA
RESET CLR Reserv
NESS Reserved REQ ACTIVE ed
r w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SLEEP
SEVON SLEEP
ON
Reserved PEND Res. DEEP Res.
EXIT
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
NON
UN USER
STK BFHF DIV_0_ BASE
ALIGN_ SET
Reserved ALIGN NMIGN Reserved TRP Res. THRD
TRP MPEND
ENA
rw rw rw rw rw rw
PendSV PRI_14
System handler priority register 3 on page 95.
SysTick PRI_15
Each PRI_N field is 8 bits wide, but the processor implements only bits[7:6] of each field,
and bits[5:0] read as zero and ignore writes.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PRI_6[7:4] PRI_6[3:0]
Reserved
rw rw rw rw r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PRI_5[7:4] PRI_5[3:0] PRI_4[7:4] PRI_4[7:4]
rw rw rw rw r r r r rw rw rw rw r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PRI_15 PRI_14
rw rw rw rw r r r r rw rw rw rw r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
Note: When the processor is halted for debugging the counter does not decrement.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
rc_r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
RELOAD
Reserved
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RELOAD
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
To generate a multi-shot timer with a period of N processor clock cycles, use a RELOAD
value of N-1. For example, if the SysTick interrupt is required every 100 clock pulses, set
RELOAD to 99.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CURRENT
Reserved
rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CURRENT
rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w rc_w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
NO
SKEW TENMS[23:16]
REF Reserved
r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TENMS[15:0]
r r r r r r r r r r r r r r r r
Bit 31 NOREF: Reads as zero. Indicates that separate reference clock is provided. The frequency of
this clock is HCLK/8.
Bit 30 SKEW: Reads as one. Calibration value for the 1ms inexact timing is not known because
TENMS is not known. This can affect the suitability of SysTick as a software real time clock.
Bits 29:24 Reserved, must be kept cleared.
Bits 23:0 TENMS[23:0]:
Indicates the calibration value when the SysTick counter runs on HCLK max/8 as external
clock. The value is product dependent, please refer to the Product Reference Manual, SysTick
Calibration Value section. When HCLK is programmed at the maximum frequency, the SysTick
period is 1ms.
If calibration information is not known, calculate the calibration value required from the
frequency of the processor clock or external clock.
Use the MPU registers to define the MPU regions and their attributes. Table 35 on page 99
shows the MPU registers
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved IREGION[7:0]
r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SEPA
DREGION[7:0]
Reserved RATE
r r r r r r r r r
Bits 15:8 DREGION[7:0]: Indicates the number of supported MPU data regions:
0x00 = Zero regions if the device does not include the MPU.
0x08 = Eight regions if the device includes the MPU.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PRIVD HFNMI EN
Reserved
EFENA ENA ABLE
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved REGION
Normally, the user writes the required region number to this register before accessing the
MPU_RBAR or MPU_RASR. However, the user can change the region number by writing to
the MPU_RBAR with the VALID bit set to 1, see MPU region base address register on
page 102. This write updates the value of the REGION field.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
ADDR[31:N]...
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
If the region size is 32B, the ADDR field is bits [31:5], and there is no reserved field.
For information about access permission, see MPU access permission attributes on
page 105.
Table 38 shows the AP encodings that define the access permissions for privileged and
unprivileged software.
.
Subregions
Regions are divided into eight equal-sized subregions. Set the corresponding bit in the SRD
field of the MPU_RASR to disable a subregion, see MPU region attribute and size register
on page 103. The least significant bit of SRD controls the first subregion, and the most
significant bit controls the last subregion. Disabling a subregion means another region
overlapping the disabled range matches instead. If no other enabled region overlaps the
disabled subregion the MPU issues a fault.
In most microcontroller implementations, the shareability and cache policy attributes do not
affect the system behavior. However, using these settings for the MPU regions can make
the application code more portable. The values given are for typical situations. In special
systems, such as multiprocessor designs or designs with a separate DMA engine, the
shareability attribute might be important. In these cases refer to the recommendations of the
memory device manufacturer.
5 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgment.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, refer to www.st.com/trademarks. All other product
or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.