5.5.1Embedded Systems & Real-Time Software

Microcontroller architecture — ARM Cortex-M series (M0, M3, M4, M7)

2,465 words11 min readdifficulty · medium

WHY does this family exist?

WHY a family instead of one chip? Because embedded needs span a huge range:

  • A $0.20 sensor that just toggles a pin → wants the cheapest, lowest-power core (M0).
  • A motor controller doing PID math at 20 kHz → wants fast multiply + maybe floating point (M4).
  • A camera pipeline doing FFTs → wants caches and high clock (M7).

One core can't be optimal for all, so ARM segments them.


The four cores at a glance

Core Pipeline Architecture DSP (SIMD) FPU Cache/MPU Typical clock
M0/M0+ 2-stage ARMv6-M optional MPU ~48 MHz
M3 3-stage ARMv7-M MPU ~72–120 MHz
M4 3-stage ARMv7E-M SIMD/DSP optional single-precision MPU ~100–180 MHz
M7 6-stage, dual-issue ARMv7E-M optional single +double I/D caches, MPU ~300–600 MHz
Figure — Microcontroller architecture — ARM Cortex-M series (M0, M3, M4, M7)

HOW the architecture is built (first principles)

1. Harvard-ish bus & the pipeline

A CPU does: Fetch instruction → Decode it → Execute it. If done one-at-a-time, each instruction takes 3 clock cycles. A pipeline overlaps them like an assembly line.

Why does M7 add a 6th stage + dual-issue? Two reasons:

  1. Shorter logic per stage → you can clock the chip higher (more MHz).
  2. Dual-issue (superscalar): fetch/decode two instructions per cycle → IPC\text{IPC} can exceed 1.

2. The interrupt machine: NVIC + exception model

The defining feature of Cortex-M for real-time is the NVIC (Nested Vectored Interrupt Controller).

3. Two stacks, two modes

Cortex-M has MSP (Main Stack Pointer) and PSP (Process Stack Pointer), plus Handler mode (interrupts) vs Thread mode (normal code). WHY two stacks? So an RTOS can give each task its own PSP stack while interrupts use the shared MSP — a crash in one task's stack can't corrupt the kernel's.

4. Memory-mapped everything

There are no special I/O instructions. A GPIO register lives at a fixed address like 0x40020000. You read/write it with a normal load/store. WHY? Simplicity + one uniform addressing model = simpler core = lower power.


Worked examples


Common mistakes (Steel-man + fix)


Recall Feynman: explain it to a 12-year-old

Imagine four robots that all read the same simple instruction cards.

  • M0 is a small cheap robot — it reads one card at a time, slow but sips almost no power.
  • M3 is a bit smarter and reads cards on a little conveyor belt so it never waits.
  • M4 has a calculator built into its hand — it can multiply huge numbers instantly (great for music/sound robots).
  • M7 is the big one: it reads two cards at once, runs super fast, and keeps a tiny notebook of stuff it just used (a cache) so it doesn't run back to the shelf. The clever robot trick they ALL share: when someone shouts "emergency!" (an interrupt), they instantly drop a bookmark (save registers) and jump to fix it in the exact same time, every time — that "every time the same" is why they run rockets and pacemakers.

Flashcards

What instruction set do all Cortex-M cores execute?
Thumb / Thumb-2 (16- and 32-bit mixed encoding) — there is no full 32-bit ARM (A32) state.
How many registers does the NVIC auto-stack on an interrupt, and which?
8 — R0–R3, R12, LR, PC, xPSR.
Why is the Cortex-M interrupt latency called "deterministic"?
Stacking + vector lookup take a fixed, known cycle count (~12 cyc), so worst-case latency is bounded and computable.
What does "vectored" mean in NVIC?
Each interrupt has its own slot in the vector table, so the CPU jumps straight to its handler without a software dispatch loop.
M0 vs M7 pipeline depth?
M0/M0+ = 2-stage; M7 = 6-stage and dual-issue (superscalar).
The big trap of the M4 FPU?
It is single-precision only; double math falls back to slow software emulation.
Which Cortex-M can do double-precision FP in hardware?
M7 (optional double-precision FPU); M4F is single-precision only.
What feature distinguishes M4 from M3 numerically?
DSP/SIMD instructions (e.g. SMLAD) and an optional single-precision FPU.
Formula for cycles to run I instructions through an N-stage pipeline (no stalls)?
(N − 1) + I.
Why two stack pointers (MSP/PSP)?
Lets an RTOS isolate each task's stack (PSP) from the kernel/interrupt stack (MSP) for safety.
Why might an M0+ be better for real-time than M7?
Shallow pipeline + no cache → less timing jitter; M7 cache misses make timing data-dependent.
How do you reach a peripheral register on Cortex-M?
Memory-mapped: a normal load/store to a fixed address (no special I/O instructions).
What register enables the FPU, and what happens if you forget?
CPACR (set CP10/CP11); FP instructions otherwise trap as a HardFault.
What memory gives M7 guaranteed timing despite caches?
TCM (Tightly-Coupled Memory).

Connections

Concept Map

designed for

segmented into

cheapest core

balanced control

numeric crunch

high throughput

adds pipeline + Thumb-2

adds DSP SIMD + FPU

adds caches + dual-issue

2-stage pipeline

6-stage superscalar

gives ideal

shorter stages

Cortex-M family

Low-power deterministic embedded

Range of needs

M0 / M0+

M3

M4

M7

Pipelining

IPC to 1 per clock

Higher clock MHz

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Cortex-M ek 32-bit RISC processor core hai jo microcontroller ke andar baitha rehta hai — yeh desktop CPU nahi hai, yeh chhote, deterministic, low-power, real-time kaam ke liye banaya gaya hai. M0 se M7 tak ek seedhi (ladder) samjho: jaise upar chadhte ho, core ko zyada instructions samajh aate hain, fetch karne ke faster tareeke milte hain, aur special math hardware (DSP, FPU) add hota hai. Lekin yeh upgrade free nahi hai — power aur silicon area mehnga padta hai. Isliye asli skill yeh hai: jo sabse chhota core tumhara deadline meet kare, wahi choose karo.

Pipeline ka funda simple hai: Fetch → Decode → Execute ek assembly line ki tarah overlap hote hain, isliye pipe bharne ke baad har cycle mein ek instruction nikalta hai. M7 mein 6-stage + dual-issue hai matlab ek hi cycle mein do instructions — isliye woh sabse fast hai, plus uske paas cache bhi hai. Par yaad rakho: deeper pipeline ka matlab "always faster" nahi — branch miss ka penalty bhi badhta hai, aur cache se timing data-dependent ho jaati hai, jo hard real-time ke liye risky hai.

Cortex-M ka sabse important hero hai NVIC — jab interrupt aata hai, hardware khud 8 registers stack pe push kar deta hai aur vector table se seedha handler pe jump karta hai, fixed cycles (~12) mein. Isi "har baar same time" ki wajah se inhe pacemaker, motor control, drones mein use karte hain. Aur ek bada trap: M4 ka FPU sirf single-precision hai — agar tum double use karoge to wapas slow software emulation chalu ho jaayega. float aur sinf use karo, aur FPU ko CPACR register mein enable karna mat bhoolna warna HardFault aa jaayega!

Go deeper — visual, from zero

Test yourself — Embedded Systems & Real-Time Software

Connections