ARM Cortex-M3 Processor Registers and Operating Modes

INTRODUCTION

The ARM Cortex-M3 processor is a powerful, efficient microprocessor that relies heavily on its registers and operating modes for optimal performance in embedded systems. Registers are the small, high-speed storage locations within the processor that store data and instructions, making them essential for computation, control flow, and data handling. This blog will dive into the various registers within the ARM Cortex-M3, such as the General-Purpose Registers (R0-R15) and Special Registers like the Stack Pointer (SP), Link Register (LR), and Program Counter (PC). We’ll also explore the processor’s two key operating modes—Thread Mode and Handler Mode—and discuss how the system transitions between these modes to handle both regular execution and interrupt-driven tasks. Additionally, we’ll touch on the ARM Cortex-M3’s support for different privilege levels, memory-mapped register access, and floating-point operations.

  1. Introduction to Registers

Registers in the ARM Cortex-M3 processor play a crucial role in efficient data handling, computation, and control flow. These registers are located inside the processor and provide high-speed access to data and instructions.

  1. Total Registers in ARM Cortex-M3

The ARM Cortex-M3 processor consists of 37 registers in total:

  • 16 General-Purpose and Special Registers:R0-R15 (including SP, LR, and PC)
  • 3 Status Registers:xPSR (APSR, IPSR, EPSR)
  • 1 CONTROL Register
  • 8 Floating Point Registers (Optional if FPU is enabled):S0-S31
  • 9 System Control and Special Function Registers
  1. General-Purpose Registers (R0-R12)

The ARM Cortex-M3 has 13 general-purpose registers, named R0 to R12, which are used for data storage and computation. These registers are used in assembly instructions for arithmetic, logic, and data transfer operations.

  • R0-R3:Used for function parameters, return values, and temporary storage.
  • R4-R11:Used for general data storage and preserved across function calls.
  • R12:Typically used as an intra-procedure scratch register.
  1. Special Registers

Special registers control and monitor the processor’s operation.

  1. Stack Pointer (SP / R13)
  • The SP holds the address of the current stack top.
  • Cortex-M3 has two stack pointers: Main Stack Pointer (MSP) and Process Stack Pointer (PSP).
  • MSP is used in privileged mode, while PSP is used in user mode.
  1. Link Register (LR / R14)
  • The LR stores the return address of function calls and exception handling.
  • When a function is called, the return address is saved in LR.
  • The BLinstruction updates the LR.
  1. Program Counter (PC / R15)
  • The PC holds the address of the next instruction to be executed.
  • It automatically increments after each instruction fetch.
  • Can be modified to implement branching and control flow changes.
  1. Program Status Register (xPSR)

The Program Status Register (xPSR) is divided into three parts:

  • Application Program Status Register (APSR):Stores condition flags (N, Z, C, V).
  • Interrupt Program Status Register (IPSR):Indicates the currently executing exception number.
  • Execution Program Status Register (EPSR):Stores additional status bits.
  1. Control Register (CONTROL)

The CONTROL register determines:

  • Processor privilege level(Privileged or Unprivileged mode)
  • Stack selection(Main Stack Pointer or Process Stack Pointer)

Bits of CONTROL Register:

  • Bit 0: Stack selection (0 = MSP, 1 = PSP)
  • Bit 1: Privilege level (0 = Privileged, 1 = Unprivileged)
  1. System Control and Special Function Registers

Cortex-M3 supports exception and interrupt handling via:

  • Vector Table Offset Register (VTOR):Holds the base address of the vector table.
  • Interrupt Control and State Register (ICSR):Provides interrupt status and control.
  • System Control Register (SCR):Controls sleep mode behavior.
  • Configuration and Control Register (CCR):Configures system behaviors.
  • Nested Vectored Interrupt Controller (NVIC) Registers:Handles priority-based interrupt management.
  • Software Trigger Interrupt Register (STIR):Used to generate software interrupts.
  1. Floating Point Registers (if FPU is enabled)

If the optional Floating Point Unit (FPU) is present, additional floating-point registers (S0-S31) are available for floating-point computations.

  1. Operating Modes in ARM Cortex-M3

The ARM Cortex-M3 processor has two main operating modes:

  1. Thread Mode
  • This is the normal execution mode for applications.
  • It can run in either Privilegedor Unprivileged
  • The processor starts in Privileged mode but can switch to Unprivileged mode using the CONTROL register.
  1. Handler Mode
  • This mode is used to handle exceptions and interrupts.
  • It always runs in Privileged
  • The processor switches to this mode automatically when an interrupt occurs.
  1. Privilege Levels

ARM Cortex-M3 supports two privilege levels:

  • Privileged Mode:Has full access to system resources and control registers. Required for executing system-level code.
  • Unprivileged Mode:Restricted access to system control registers and certain operations. Used for running user applications securely.

Switching between these modes is done by modifying the CONTROL register.

  1. Memory-Mapped Register Access

Many registers in the Cortex-M3 processor are accessed through memory-mapped I/O, allowing direct reading and writing using memory addresses.