dm00051352 stm32f0xxx Cortex m0 Programming Manual Stmicroelectronics
dm00051352 stm32f0xxx Cortex m0 Programming Manual Stmicroelectronics
Programming manual
Introduction
This programming manual provides information for application and system-level software developers.
It gives a full description of the STM32 Cortex®-M0 processor programming model, instruction set and core peripherals.
The STM32 Cortex®-M0 processor is a high performance 32-bit processor designed for the microcontroller market. It offers
significant benefits to developers, including:
• outstanding processing performance combined with fast interrupt handling,
• enhanced system debug with extensive breakpoint and trace capabilities,
• efficient processor core, system, and memories,
• ultralow power consumption with integrated sleep modes, and
• patform security.
1 General information
italic Highlights important notes, introduces special terminology, denotes internal cross-references, and
citations.
< and > Enclose replaceable terms for assembler syntax where they appear in code or code fragments. For
example:
LDRSB<cond> <Rt>, [<Rn>, #<offset>]
bold Highlights interface elements, such as menu names. Denotes signal names. Also used for terms in
descriptive lists, where appropriate.
monospace Denotes text that you can enter at the keyboard, such as commands, file and program names, and
source code.
monospace Denotes a permitted abbreviation for a command or option. You can enter the underlined text instead of
the full command or option name.
monospace italic Denotes arguments to monospace text where the argument is to be replaced by a specific value.
monospace bold Denotes language keywords when used outside example code.
Cortex-M0 components
Cortex-M0 processor
Debug
Nested
Interrupts Breakpoint
Vectored Cortex-M0
and
Interrupt processor
watchpoint
Controller core
unit
(NVIC)
Debug
Debugger
Bus matrix Access Port
interface
(DAP)
DT30028V1
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, including a deep sleep function that enables the entire device to be rapidly
powered down.
The Cortex®-M0 does not support multiple privilege levels. It can always use all instructions and access all
resources.
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, with independent copies of the stack pointer:
• the main stack, and
• the process stack.
Please refer to Section 2.1.3 Core registers
In Thread mode, the CONTROL register controls whether the processor uses the main stack or the process stack,
see Core registers.
In Handler mode, the processor always uses the main stack.
The options for processor operations are:
R0
R1
R2
R3
Low registers
R4
R5
R6 General purpose registers
R7
R8
R9
High registers R10
R11
R12
Stack Pointer SP (R13) PSP MSP
Link Register LR (R14)
Program Counter PC (R15)
DT30029V1
CONTROL Control Register
1. Describes access type during program execution in Thread and Handler modes. Debug access can differ.
2. Bit[24] is the T-bit and is loaded from bit[0] of the reset vector.
General-purpose registers
R0-R12 are 32-bit general-purpose registers for data operations.
31 30 29 28 27 25 24 23 6 5 0
APSR N Z C V Reserved
DT71805V1
1. Reads of the EPSR bits return zero, and the processor ignores writes to these bits.
2. The processor ignores writes to the IPSR bits.
Bits Description
Bits Description
Bits Description
Attempts to read the EPSR directly through application software using the MSR instruction always return zero.
Attempts to write the EPSR using the MSR instruction in application software are ignored. Fault handlers can
examine EPSR value in the stacked PSR to indicate the operation that is at fault. See Section 2.3.6 Exception
entry and return.
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 Section 2.4 Fault
handling for more information.
Interruptable-restartable instructions
LDM and STM are interruptable-restartable instructions. If 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 0
Reserved
PRIMASK
DT71806V1
Bits Description
Control register
The CONTROL register controls the stack used when the processor is in Thread mode. See the register summary
in Table 3. Core register set summary for its attributes.
31 2 1 0
Reserved
DT71801V1
Table 9. CONTROL register bit definitions
Bits Description
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 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,
see Section 3.7.7 MSR. 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
Section 3.7.5 ISB
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
DT71802V1
Code 0.5GB
0x00000000
The processor reserves regions of the Private peripheral bus (PPB) address range for core peripheral registers,
see Section 4.1 About the STM32 Cortex®-M0 core peripherals.
Normal The processor can reorder transactions for efficiency, or perform speculative reads.
Device The processor preserves transaction order relative to other transactions to Device or Strongly-ordered
memory.
Strongly-ordered The processor preserves the transaction order relative to all other transactions.
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.
Additional memory attributes include:
Execute Never (XN) Means that the processor prevents instruction accesses. Any attempt to fetch an instruction from an XN
region causes a HardFault exception.
A2
A1 Device access
Normal access Strongly ordered access
Nonshareable Shareable
Normal access - - - -
Device access, nonshareable - < - <
Device access, shareable - - < <
Strongly ordered access - < < <
- 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.
0x60000000- External
Normal - Executable region for data.
0x9FFFFFFF RAM
0xA0000000- External
Device XN External device memory
0xDFFFFFFF device
This region includes the NVIC, System timer, and
0xED000000- Private Peripheral Strongly-
XN system control block. Only word accesses can be
0xED0FFFFF Bus ordered
used in this region.
0xED100000- This region includes all the STM32 standard
Device Device XN
0xFFFFFFFF peripherals.
1. See Section 2.2.1 Memory regions, types, and attributes for more information.
The Code, SRAM, and external RAM regions can hold programs.
DMB The Data Memory Barrier instruction ensures that outstanding memory transactions complete before subsequent
memory transactions. See Section 3.7.3 DMB.
DSB The Data Synchronization Barrier instruction ensures that outstanding memory transactions complete before
subsequent instructions execute. See Section 3.7.4 DSB.
ISB The Instruction Synchronization Barrier ensures that the effect of all completed memory transactions is recognizable by
subsequent instructions. See Section 3.7.5 ISB.
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. See Figure 7. Little-endian example for
an 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
DT71803V1
2.3 Exception model
This section describes the exception model.
Reset Reset is invoked on power-up or warm reset. The exception model treats reset as a special form of exception.
When reset is asserted, the operation of the processor stops, potentially at any point in an instruction. When
reset is deasserted, execution restarts in Thread mode from the address provided by the reset entry in the
vector table.
NMI A NonMaskable Interrupt (NMI) can be signaled by a peripheral or triggered by software. This is the highest
priority exception other than reset. It is permanently enabled and has a fixed priority of -2. NMIs cannot be:
• Masked or prevented from activation by any other exception
• Preempted by any exception other than Reset.
Hard fault A hard fault is an exception that occurs because of an error during normal exception processing. Hard faults
have a fixed priority of -1, meaning they have higher priority than any exception with configurable priority.
SVCall A supervisor call (SVC) is an exception that is triggered by the SVC instruction. In an OS environment,
applications can use SVC instructions to access OS kernel functions and device drivers.
PendSV PendSV is an interrupt-driven request for system-level service. In an OS environment, use PendSV for context
switching when no other exception is active.
SysTick A SysTick exception is an exception that the system timer generates when it reaches zero. Software can also
generate a SysTick exception. In an OS environment, the processor can use this exception as system tick.
Interrupt An interrupt, or IRQ, is an exception signaled by a peripheral, or generated by a software request. All interrupts
(IRQ) are asynchronous to instruction execution. In the system, peripherals use interrupts to communicate with the
processor.
12-13 - Reserved - - -
1. To simplify the software layer, the CMSIS only uses IRQ numbers and therefore uses negative values for exceptions other than interrupts.
The IPSR returns the Exception number. See Interrupt program status register.
2. See Section 2.3.4 Vector table for more information.
3. See Section 4.2.6 Interrupt priority register (IPR0-IPR7).
4. Increasing in steps of 4.
For an asynchronous exception other than reset, the processor can execute another instruction between when
the exception is triggered and when the processor enters the exception handler.
Software can disable the exceptions that Table 12. Properties of the different exception types shows as having
configurable priority, see:Section 4.2.3 Interrupt clear-enable register (ICER).
For more information about hard faults, see Section 2.4 Fault handling.
Interrupt Service Interrupts IRQ0 to IRQ31 are the exceptions handled by ISRs.
Routines (ISRs)
Fault handlers Hard fault is the only fault exception handled by the fault handlers.
System handlers NMI, PendSV, SVCall SysTick, and Hard fault exceptions are all system exceptions that are
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
DT30030V1
Initial SP value
0x00
Preemption When the processor is executing an exception handler, an exception can preempt the exception handler if
its priority is higher than the priority of the exception being handled. When one exception preempts another,
the exceptions are called nested exceptions. See Table 12. Properties of the different exception types more
information.
Return This occurs when the exception handler is completed, and:
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 original exception.
When one exception preempts another, the exceptions are nested.
Sufficient priority means that the exception has more priority than any limits set by the mask registers, see
Exception mask registers. 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 as stacking and the structure of
eight data words is referred as stack frame. The stack frame contains the following information:
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 was 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 executes one of the following instructions to
load the EXC_RETURN value into the PC:
• a POP instruction that loads the PC
• a BX instruction using any register.
EXC_RETURN is the value loaded into 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. Therefore, it starts the exception return sequence. Bits[3:0] of the EXC_RETURN value indicate the
required return stack and processor mode as shown in Table 1.
EXC_RETURN[31:0] Description
Lockup
The processor enters a lockup state if a hard fault occurs when executing the NMI or hard fault 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 occur:
• The reset of the processor.
Sleep-on-exit
If the SLEEPONEXIT bit of the SCR is set to 1, when the processor completes the execution of an exception
handler it returns to Thread mode and immediately enters sleep mode. Use this mechanism in applications that
only require the processor to run when an exception occurs.
This chapter is the reference material for the Cortex®-M0 instruction set description in a User Guide. The following
sections give general information:
Section 3.1 Instruction set summary
Section 3.2 CMSIS intrinsic functions
Section 3.3 About the instruction descriptions
Each of the following sections describes a functional group of Cortex®-M0 instructions. Together they describe all
the instructions supported by the Cortex®-M0 processor:
Section 3.4 Memory access instructions
Section 3.5 General data processing instructions
Section 3.6 Branch and control instructions
Section 3.7 Miscellaneous instructions
The CMSIS also provides a number of functions for accessing the special registers using MRS and MSR
instructions (see Table 15. CMSIS intrinsic functions to generate some Cortex-M0 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 operands. Operands
in some instructions are flexible in that they can either be a register or a constant (see Section 3.3.3 Shift
operations).
ASR
Arithmetic 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 copies the original bit[31] of the register into the left hand n bits of the
result (see Figure 10. ASR#3).
You can use the ASR operation to divide the signed value in the register Rm by 2n, with the result being rounded
towards negative-infinity.
When the instruction is ASRS, 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, all the bits in the result are set to the value of bit[31] of Rm.
If n is 32 or more and the carry flag is updated, it is updated to the value of bit[31] of Rm.
Carry
Flag
31 5 4 3 2 1 0
...
DT71807V1
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 11. LSR#3).
You can use the LSR #n 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.
0 0 0 Carry
Flag
31 5 4 3 2 1 0
...
DT71808V1
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 12. LSL#3).
You can use the LSL #n 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 or when LSL #n, with non-zero n, is used in operand2 with the instructions MOVS,
MVNS, ANDS, ORRS, ORNS, EORS, BICS, TEQ or TST, 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 ...
DT71809V1
ROR
Rotate 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. It also moves the right-hand n bits of the register into the lefthand n bits of the result (see
Figure 13. ROR #3).
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 the same as the value in Rm, and if the carry flag is updated, it is
updated to bit[31] of Rm.
ROR with shift length, n, more than 32 is the same as ROR with shift length n-32.
Carry
Flag
31 5 4 3 2 1 0
...
DT71810V1
3.3.4 Address alignment
An aligned access is an operation where a word-aligned address is used for a word, or multiple word access, or
where a halfword-aligned address is used for a halfword access. Byte accesses are always aligned.
There is no support for unaligned accesses on the Cortex-M0 processor. Any attempt to perform an unaligned
memory access operation results in a HardFault exception.
Table 17. Condition code suffixes and their relationship with the flags
3.4.1 ADR
Load PC-relative address.
Syntax
where:
• ‘Rd’ is the destination register
• ‘label’ is a PC-relative expression (see Section 3.3.5 PC-relative expressions)
Operation
ADR determines the address by adding an immediate value to the PC. It writes the result to the destination
register.
ADR produces position-independent code, because the address is PCrelative.
If you use ADR to generate a target address for a BX or BLX instruction, you must ensure that bit[0] of the
address you generate is set to1 for correct execution.
Restrictions
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
; TextMessage to R1
Syntax
where:
• ‘Rt’ is the register to load or store
• ‘Rn’ is the register on which the memory address is based
• ‘imm’ is an offset from Rn. If imm is omitted, it is assumed to be zero.
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
For 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
Section 3.3.4 Address alignment.
Condition flags
These instructions do not change the flags.
Examples
Syntax
where:
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
Section 3.3.4 Address alignment
Condition flags
These instructions do not change the flags.
Examples
; sum of R5 and R1
LDRSH R1, [R2, R3] ; Load a halfword from the memory address
Syntax
where:
• ‘Rt’ is the register to load or store
• ‘label’ is a PC-relative expression (see Section 3.3.5 PC-relative expressions)
Operation
Loads the register specified by Rt from the word in the memory specified by the label.
Restrictions
In these instructions, the label must be within 1020 bytes of the current PC and the word aligned.
Condition flags
These instructions do not change the flags.
Examples
; labelled as LookUpTable.
LDR R3, [PC, #100] ; Load R3 with memory word at (PC + 100).
Syntax
where:
• ‘Rn’ is the register on which the memory addresses are based
• ‘!’ is an optional writeback suffix. If ! is present, the final address that is loaded from or stored to is written
back into Rn.
• ‘reglist’ is a list of one or more registers to be loaded or stored, enclosed in braces. It can contain register
ranges. It must be comma-separated if it contains more than one register or register range (see Examples).
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 loads the registers in reglist with word values from memory addresses based on Rn.
STM stores the word values in the registers in reglist to memory addresses based on Rn.
The memory addresses used for accesses are at 4-byte intervals ranging from Rn to Rn + 4 * (n-1), where n is
the number of registers in reglist. The accesses happen 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 of Rn + 4 * (n) or 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 Section 3.3.4 Address alignment 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
STMIA R1!,{R2-R4,R6}
Incorrect examples
Syntax
PUSH reglist
POP reglist
where:
• ‘reglist’ is a non-empty list of registers (or register ranges), enclosed in braces. Commas must separate
register lists or ranges (see Section 3.4.5 LDM and STM).
Operation
• PUSH stores registers on the stack, with the highest numbered register using the highest memory address
and the lowest numbered register using the lowest 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 SP
register value 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.
See Section 3.4.5 LDM and STM for more information.
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
POP {R0,R6,PC} ; Pop r0,r6 and PC from the stack, then branch to new PC.
ADCS Add with carry Section 3.5.1 ADD{S}, ADCS, SUB{S}, SBCS, and RSBS
ADD(S) Add Section 3.5.1 ADD{S}, ADCS, SUB{S}, SBCS, and RSBS
ANDS Logical AND Section 3.5.2 ANDS, ORRS, EORS and BICS
ASRS Arithmetic shift right Section 3.5.3 ASRS, LSLS, LSRS and RORS
BICS Bit clear Section 3.5.2 ANDS, ORRS, EORS and BICS
CMN Compare negative Section 3.5.4 CMP and CMN
Syntax
where:
• S: causes an ADD or SUB instruction to update flags
• Rd: specifies the result register. If omitted,this value is assumed to take the same value as Rn, for example
ADDS R1,R2 is identical to ADDS R1,R1,R2.
• Rn: specifies the first source register
• Rm: specifies the second source register
• imm: specifies a constant immediate value.
Operation
The ADCS instruction adds the value in Rn to the value in Rm, adding a further 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, deducts a further one if the carry flag is set.
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 and Table 20. ADCS, ADD, RSBS, SBCS and SUB
operand restrictions).
Restrictions
Table 20. ADCS, ADD, RSBS, SBCS and SUB operand restrictions lists the legal combinations of register
specifiers and immediate values that can be used with each instruction.
Table 20. ADCS, ADD, RSBS, SBCS and SUB operand 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.
ADD
R0-R7 SP or PC - 0-1020 Immediate value must be an integer multiple of four.
SP SP - 0-508 Immediate value must be an integer multiple of 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.
SUB SP SP - 0-508 Immediate value must be an integer multiple of 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
ADCS R1, R1, R3 ; add the most significant words with carry
Multiword values do not have to use consecutive registers. Table 20. ADCS, ADD, RSBS, SBCS and SUB
operand restrictions shows instructions that subtract a 96bit 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.
SBCS R6, R6, R3 ; subtract the most significant words with carry
Table 20. ADCS, ADD, RSBS, SBCS and SUB operand restrictions shows the RSBS instruction used to perform
a 1's complement of a single register.
Syntax
where:
• ‘Rd’ is the destination register
• ‘Rn’ is the register holding the first operand and is the same as the destination register.
• ‘Rm’ is the second register.
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 Section 3.3.6 Conditional execution.
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
Syntax
where:
• ‘Rd’ is the destination register. If Rd is omitted, it is assumed to take the same value as Rm.
• ‘Rm’ is the register holding the value to be shifted
• ‘Rs’ is the register holding the shift length to apply to the value Rm.
• ‘imm’ is the shift length. The range of shift lengths depend on the instruction as follows:
• ASR: Shift length from 1 to 32
• LSL: Shift length from 0 to 31
• LSR: Shift length from 1 to 32
Note: MOVS Rd, Rm is the preferred syntax for LSLS Rd, Rm, #0.
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 Section 3.3.3 Shift operations).
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 Section 3.3.3 Shift
operations).
Examples
LSLS R1, R2, #3 ; Logical shift left by 3 bits with flag update
RORS R4, R4, R6 ; Rotate right by the value in the bottom byte of R6.
Syntax
CMN Rn, Rm
CMP Rn, Rm
where:
• ‘Rn’ is the register holding the first operand
• Rm is the register to compare with.
• imm is the immediate value to compare with.
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
In these instructions:
• CMN instruction Rn, and Rm must only specify R0-R7.
• CMP instruction:
– Rn and Rm can specify R0-R14
– imm 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
MVNS Rd, Rm
where:
• ‘S’ is an optional suffix. If S is specified, the condition code flags are updated on the result of the operation
(see Section 3.3.6 Conditional execution).
• ‘Rd’ is the destination register
• ‘Rm’ is a register
• ‘imm’ is any value in the range 0-255
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 MVNS instruction takes the value of Rm, performs a bitwise logical NOT 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 ignored
• A branch occurs to the address created by forcing bit[0] of that value 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 to the ARM instruction set.
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 flag
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
3.5.6 MULS
Multiply using 32-bit operands, and producing a 32-bit result.
Syntax
where:
• ‘Rd’ is the destination register
• ‘Rn, Rm’ are registers holding the values to be multiplied.
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
Section 3.3.6 Conditional execution.
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. It does not affect the C or V flags.
Examples
Syntax
op Rd, Rn
where:
• ‘op’ is one of:
REV: Reverse byte order in a word
REV16: Reverse byte order in each halfword independently
REVSH: Reverse byte order in the bottom halfword, and sign extends to 32 bits
• ‘Rd’ is the destination register
• ‘Rn’ is the register holding the operand
Operation
Use these instructions to change endianness of data:
• REV: Converts either:
– 32bit big-endian data into little-endian data or
– 32bit little-endian data into bigendian data.
• REV16: Converts either:
– 2 packed 16bit bigendian data into little-endian data or
– 2 packed 16bit little-endian data into bigendian data.
• REVSH: Converts either:
– 16bit signed bigendian data into 32bit signed little-endian data or
– 16bit signed little-endian data into 32bit signed bigendian data
Restrictions
In these instructions, Rd, and Rn must only specify R0-R7.
Condition flags
These instructions do not change the flags.
Examples
Syntax
SXTB Rd, Rm
SXTH Rd, Rm
UXTB Rd, Rm
UXTH Rd, Rm
where:
• ‘Rd’ is the destination register
• ‘Rn’ ,‘Rm’ are the registers holding the first and second operands
Operation
These instructions extract bits from the resulting value:
1. SXTB extracts bits[7:0] and sign extends to 32 bits
2. UXTB extracts bits[7:0] and zero extends to 32 bits
3. SXTH extracts bits[15:0] and sign extends to 32 bits
4. 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
UXTB R3, R1 ; Extract lowest byte of the value in R10 and zero
3.5.9 TST
Test bits.
Syntax
TST Rn, Rm
where:
• ‘Rn’ is the register holding the first operand
• ‘Rm’ is 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 flag
Examples
Syntax
B{cond} label
BL label
BX Rm
BLX Rm
where:
Table 22. Branch ranges
• ‘B’ is branch (immediate).
• ‘BL’ is branch with link (immediate).
• ‘BX’ is branch indirect (register).
• ‘BLX’ is branch indirect with link (register).
• ‘label’ is a PCrelative expression. See Section 3.3.5 PC-relative expressions.
• ‘Rm’ is a register that indicates an address to branch to.
• ’Cond’ is an optional condition code, see Section 3.3.6 Conditional execution.
Operation
All these instructions cause a branch to label, or to the address indicated in 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 cause a Hard fault exception if bit[0] of Rm is 0.
• The 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.
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
The restrictions are:
• Do not use SP or PC in the BX or BLX instruction
• For BX and BLX, 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.
Bcond is the only conditional instruction on the Cortex-M0 processor.
Condition flags
These instructions do not change the flags.
Examples
B loopA ; Branch to loopA
; stored in LR
; in R0
BKPT Breakpoint
CPSID Change Processor State, Disable Interrupts Section 3.7.2 CPSID CPSIE
CPSIE Change Processor State, Enable Interrupts Section 3.7.2 CPSID CPSIE
DMB Data Memory Barrier Section 3.7.3 DMB
DSB Data Synchronization Barrier Section 3.7.4 DSB
3.7.1 BKPT
Breakpoint.
Syntax
BKPT #imm
Operation
BKPT 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 produce a HardFault or go in to lockup if a debugger is not attached when a BKPT
instruction is executed. See Section 2.4 Fault handling for more information.
Restrictions: None
This instruction does not change the flags.
Examples
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 registers for more information
about these registers.
Restrictions
None
Condition flags
This instruction does not change the condition flags.
Examples
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 completed before any explicit memory accesses that appear, in program order,
after the DMB instruction. DMB does not affect the ordering or execution of instructions that do not access
memory.
Restrictions
None
Condition flags
This instruction does not change the flags.
Examples
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
None
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
None
Condition flags
This instruction does not change the flags.
Examples
3.7.6 MRS
Move the contents of a special register to a general-purpose register.
Syntax
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
MRS stores the contents of a special-purpose register to a general-purpose register. MRS 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 Section 3.7.7 MSR.
Restrictions
Rd must not be SP or PC.
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.
Restrictions
Rn must not be SP and must not be PC.
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 does nothing. NOP is not necessarily a time-consuming NOP. The processor might remove it from the
pipeline before it reaches the execution stage.
Use NOP for padding, for example to place the following instruction on a 64bit boundary.
Restrictions
None
Condition flags
This instruction does not change the flags.
Examples
NOP ; No operation
3.7.9 SEV
Send event.
Syntax
SEV
Operation
SEV is a hint instruction that causes an event to be signaled to all processors within a multiprocessor system. It
also sets the local event register to 1, see Section 2.5 Power management and Section 3.7.11 WFE.
Restrictions
None
Condition flags
This instruction does not change the flags.
Examples
3.7.10 SVC
Supervisor call.
Syntax
SVC #imm
Operation
The SVC instruction causes the SVC exception. imm is ignored by the processor. It can be retrieved by the
exception handler to determine what service is being requested.
Restrictions: None
Condition flags
This instruction does not change the flags.
Examples
SVC 0x32 ; Supervisor Call (SVC handler can extract the immediate value
3.7.11 WFE
Wait for event. WFE is a hint instruction.
Syntax
WFE
Operation
If the event register is 0, WFE suspends execution until one of the following events occurs:
• An exception, unless masked by exception mask registers or the current priority level
• An exception enters Pending state, if SEVONPEND in 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 returns immediately. For more information see Section 2.5 Power
management.
WFE is intended for power saving only. When writing software assume that WFE might behave as NOP.
Restrictions: None
Condition flags
This instruction does not change the flags.
Examples
3.7.12 WFI
Wait for Interrupt.
Syntax
WFI
Operation
WFI is a hint instruction that suspends execution until one of the following events occurs:
• An exception
• An interrupt becomes pending which would preempt if PRIMASK was clear
• A Debug Entry request, regardless of whether Debug is enabled.
WFI is intended for power saving only. When writing software assume that WFI might behave as a NOP
operation.
Restrictions
None
Condition flags
This instruction does not change the flags.
Examples
4 Core peripherals
0xE000E008-0xE000E00F Section 4.3 System control block (SCB) Table 32. SCB register map and reset values
0xE000E010-0xE000E01F Section 4.4 SysTick timer (STK) Section 4.4.6 SysTick register map
Section 4.2 Nested vectored interrupt controller
0xE000E100-0xE000E4EF Table 29. NVIC register map and reset values
(NVIC)
0xE000ED00-0xE000ED3F Section 4.3 System control block (SCB) Table 32. SCB register map and reset values
Section 4.2 Nested vectored interrupt controller
0xE000EF00-0xE000EF03 Section 4.2.9 NVIC register map
(NVIC)
10
11
9
8
7
6
5
4
3
2
1
0
SETENA [31:0]
rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs
10
11
9
8
7
6
5
4
3
2
1
0
CLRENA [31: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
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
10
11
9
8
7
6
5
4
3
2
1
0
SETPEND [31:0]
rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs
10
11
9
8
7
6
5
4
3
2
1
0
CLRPEND [31: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
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 24 23 16 15 8 7 0
...
IPR n PRI_( 4n+ 3) PRI_( 4n+ 2) PRI_( 4n+ 1) PRI_( 4n)
...
DT71811V1
Table 27. IPR bit assignments
Priority, byte
[31:24]
offset 3
Priority, byte
[23:16] Each priority field holds a priority value, 0-192. The lower the value, the greater the priority of the
offset 2
corresponding interrupt. The processor implements only bits[7:6] of each field, bits[5:0] read as zero
Priority, byte and ignore writes. This means writing 255 to a priority register saves value 192 to the register.
[15:8]
offset 1
Priority, byte
[7:0]
offset 0
See Section 4.2.2 Interrupt set-enable register (ISER) and Section 4.2.1 Accessing the Cortex-M0 NVIC
registers using CMSIS for more information about the interrupt priority array that provides the software view of the
interrupt priorities.
Find the IPR number and byte offset for interrupt N as follows:
• The corresponding IPR number, M, is given by M = N DIV 4
• The byte offset of the required Priority field in this register is N 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].
A level-sensitive interrupt is held asserted until the peripheral deasserts the interrupt signal. Typically this
happens because the ISR accesses the peripheral, causing it to clear the interrupt request. A pulse interrupt
is an interrupt signal sampled synchronously on the rising edge of the processor clock. To ensure that the NVIC
detects the interrupt, the peripheral must assert the interrupt signal for at least one clock cycle. During this clock
cycle, the NVIC detects the pulse and latches the interrupt.
When the processor enters the ISR. It automatically removes the pending state from the interrupt, see Hardware
and software control of interrupts. For a level-sensitive interrupt, if the signal is not deasserted before the
processor returns from the ISR, the interrupt becomes pending again, and the processor must execute its ISR
again. This means that the peripheral can hold the interrupt signal asserted until it no longer needs servicing.
In addition, the CMSIS provides a number of functions for NVIC control, including:
The input parameter IRQn is the IRQ number, see Section 2.3.2 Exception types. For more information about
these functions see the CMSIS documentation.
Offset Register 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
NVIC_ISER SETENA[31:0]
0x000
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NVIC_ICER CLRENA[31:0]
0x080
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NVIC_ISPR SETPEND[31:0]
0x100
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NVIC_ICPR CLRPEND[31:0]
0x180
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NVIC_IPR0-7 IP[3] IP[2] IP[1] IP[0]
0x300
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0xE000ED04 ICSR RW(1) 0x00000000 Section 4.3.2 Interrupt control and state register (ICSR)
0xE000ED0C AIRCR RW(1) 0xFA050000 Section 4.3.3 Application interrupt and reset control register (AIRCR)
10
11
9
8
7
6
5
4
3
2
1
0
Variant Constant
Implementer [31:24] PartNo [15:4] Revision [3:0]
[23:20] [19:16]
rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs rs
10
11
9
8
7
6
5
4
3
2
1
0
NMIPENDSET [31]
ISRPENDING [22]
PENDSVSET [28]
PENDSTCLR [25]
PENDSTSET [26]
PENDSVCLR[27]
VECTPENDING[17:1
VECTACTIVE[5:0]
2]
Res. Res. Reserved Reserved
r r r r r r r r r
w w r r r r r r r
w w w w w w w w w
Bits 5:0 VECTACTIVE [5:0]: Active vector. Contains the active exception number:
0: Thread mode
Other values: The exception number of the currently active exception.
Subtract 16 from this value to obtain CMSIS IRQ number required to index into the Interrupt Clear-Enable, Set-Enable,
Clear-Pending, Set-Pending, or Priority Registers. See Table 6. IPSR bit definitions.
1. This is the same value as IPSR bits[5:0], see Table 6. IPSR bit definitions.
10
11
9
8
7
6
5
4
3
2
1
0
VECT CLR ACTIVE
SYS RESET REQ
ENDIANESS
Reserved(read)/ VECTKEY[31:16](write) R
Reserved e
s.
r r r r r r r r r r r r r r r r
r w w
w w w w w w w w w w w w w w w w
10
11
9
8
7
6
5
4
3
2
1
0
SLEEP ON EXIT
SEVONPEND
SLEEPDEEP
R R
Reserved e e
s. s.
r r r
w w w
10
11
9
8
7
6
5
3
2
1
0
UN ALIGN_ TRP
STK ALIGN
Reserved
Reserved Reserved
r r
w w
10
11
9
8
7
6
5
4
3
2
1
0
PRI_11
r r r r Reserved
r r r r
w w w w
10
11
9
8
7
6
5
4
3
2
1
0
PRI_15 PRI_14
r r r r r r r r Reserved
r r r r r r r r
w w w w w w w w
Bits 31:24 PRI_15 [31:24]: Priority of system handler 15, SysTick exception.
This is Reserved when the SysTick timer is not implemented.
PENDSVCLR
PENDSVSET
PENDSTCLR
PENDSTSET
Reserved
Reserved
VECTPENDING[5:0] VECTACTIVE[5:0]
ICSR Reset
0x04 Reserved Reserved
Value
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
VECTCLRACTIVE
SYSRESETREQ
VECTRESET
ENDIANESS
VECTKEY[15:0]
AIRCRRes
0x0C Reserved
et Value
0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 0
SLEEPONEXIT
SEVONPEND
SLEEPDEEP
Reserved
Reserved
SCR Reset
0x10 Reserved
Value
0 0 0
UNALIGN_TRP
STKALIGN
CCR Reset
0x14 Reserved Reserved Reserved
Value
1 1
PRI11
SHPR2Res
0x1C Reserved
et Value
0 0 0 0 0 0 0 0
PRI15 PRI14
SHPR3Res
0x20 Reserved
et Value
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Writing to the STK_CVR clears the register and the COUNTFLAG status bit to 0. The write does not trigger the
SysTick exception logic.
Reading the register returns its value at the time that it is accessed.
When the processor is halted for debugging, the counter does not decrement.
0xE000E010 STK_CSR RW 0x00000000 Section 4.4.1 SysTick control and status register (STK_CSR)
0xE000E014 STK_RVR RW Unknown Section 4.4.2 SysTick reload value register (STK_RVR)
0xE000E018 STK_CVR RW Unknown Section 4.4.3 SysTick current value register (STK_CVR)
0xE000E01C STK_CALIB RO 0xC0000000 Section 4.4.4 SysTick calibration value register (STK_CALIB)
10
11
9
8
7
6
5
4
3
2
1
0
CLKSOURCE
COUNTFLAG
EN ABLE
TICK INT
Reserved Reserved
r r r r
w w w w
10
11
9
8
7
6
5
4
3
2
1
0
RELOAD [23:0]
Reserved r r r r r r r r r r r r r r r r r r r r r r r r
w w w w w w w w w w w w w w w w w w w w w w w w
10
11
9
8
7
6
5
4
3
2
1
0
CURRENT [23:0]
Reserved r r r r r r r r r r r r r r r r r r r r r r r r
w w w w w w w w w w w w w w w w w w w w w w w w
10
11
9
8
7
6
5
4
3
2
1
0
TENMS[23:0]
NO REF
SKEW
Reserved
r r r r r r r r r r r r r r r r r r r r r r r r r r
Bit 31 NOREF [31]: NOREF flag. Reads as one. Indicates that no separate reference clock is provided.
Bit 30 SKEW [30]: SKEW flag: Reads as one. Calibration value for the 10ms 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]: Calibration value. Reads as zero. Indicates that calibration value is not known.
TICK EN
STK_CSR COUNTFLAG CLKSOURCE
INT ABLE
0x00 Reserved Reserved
Reset Value 0 1 0 0
STK_RVR RELOAD[23:0]
0x04 Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
STK_CVR CURRENT[23:0]
0x08 Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
STK_CALIB TENMS[23:0]
0x0C Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Revision history
Table 35. Document revision history
Contents
1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1 About this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Typographical conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 About the STM32 Cortex®-M0 processor and core peripherals . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4.1 System level interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4.2 Integrated configurable debug. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.3 Cortex®-M0 processor features and benefits summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.4 Cortex®-M0 core peripherals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.7.7 MSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.7.8 NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.7.9 SEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.7.10 SVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.7.11 WFE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.7.12 WFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4 Core peripherals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
4.1 About the STM32 Cortex®-M0 core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2 Nested vectored interrupt controller (NVIC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2.1 Accessing the Cortex-M0 NVIC registers using CMSIS. . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.2.2 Interrupt set-enable register (ISER). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.2.3 Interrupt clear-enable register (ICER) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.2.4 Interrupt set-pending register (ISPR). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.2.5 Interrupt clear-pending register (ICPR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.2.6 Interrupt priority register (IPR0-IPR7) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.2.7 Level-sensitive and pulse interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.2.8 NVIC design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.2.9 NVIC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.3 System control block (SCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.3.1 CPUID base register (CPUID). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.3.2 Interrupt control and state register (ICSR). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.3.3 Application interrupt and reset control register (AIRCR) . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.3.4 System control register (SCR). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.3.5 Configuration and control register (CCR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.3.6 System handler priority registers (SHPRx) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.3.7 SCB usage hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.3.8 SCB register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.4 SysTick timer (STK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.4.1 SysTick control and status register (STK_CSR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4.2 SysTick reload value register (STK_RVR). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4.3 SysTick current value register (STK_CVR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.4.4 SysTick calibration value register (STK_CALIB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.4.5 SysTick design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.4.6 SysTick register map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
List of tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
List of figures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
List of tables
Table 1. Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Table 2. Summary of processor mode and stack usage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Table 3. Core register set summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Table 4. PSR register combinations and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Table 5. APSR bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Table 6. IPSR bit definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Table 7. EPSR bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Table 8. PRIMASK register bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Table 9. CONTROL register bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Table 10. Ordering of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Table 11. Memory access behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Table 12. Properties of the different exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Table 13. Exception return behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Table 14. Cortex-M0 instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Table 15. CMSIS intrinsic functions to generate some Cortex-M0 instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Table 16. CMSIS intrinsic functions to access the special registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Table 17. Condition code suffixes and their relationship with the flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Table 18. Memory access instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Table 19. Data processing instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Table 20. ADCS, ADD, RSBS, SBCS and SUB operand restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Table 21. Branch and control instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Table 22. Branch ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Table 23. Miscellaneous instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Table 24. STM32 core peripheral register regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Table 25. NVIC register summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Table 26. CMSIS access NVIC functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Table 27. IPR bit assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Table 28. CMSIS functions for NVIC control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Table 29. NVIC register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Table 30. Summary of the system control block registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Table 31. System fault handler priority fields and registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Table 32. SCB register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Table 33. System timer registers summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Table 34. SysTick register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Table 35. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
List of figures
Figure 1. STM32 Cortex®-M0 implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Figure 2. Processor core registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Figure 3. APSR, IPSR, and EPSR bit assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 4. PRIMASK register bit assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 5. CONTROL register bit assignments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 6. Memory map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 7. Little-endian example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Figure 8. Vector table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 9. Cortex®-M0 stack frame layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 10. ASR#3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Figure 11. LSR#3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Figure 12. LSL#3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Figure 13. ROR #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Figure 14. IPR register mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54