Overview
Curated: · Written: · Reviewed:
Microcontroller Architecture & Memory-Mapped I/O
In one line
A microcontroller puts the CPU, flash, SRAM and peripherals in one address space, and a bus decoder routes each load or store to whatever answers at that address. Reach for this model whenever a question asks what a register access actually does, where it lands, and whether it has completed.
Key definitions
- Bus decoder: logic that compares an address against region ranges and routes the transaction to flash, SRAM, a bridge or a device.
- Device memory: memory type with no caching, merging or speculation. Every read and write is a side effect.
- Posted write: a store that retires once the bridge accepts it, before the device has applied it.
- W1C: write-one-to-clear. Writing 1 clears the flag, writing 0 leaves it alone.
- Set/clear alias: a second address where a write touches only the bits set to 1, so no read-modify-write is needed.
- VTOR: the register holding the vector table base. On ARMv7-M the base must align to the exception count rounded up to a power of two, minimum 128 bytes.
Formulas and complexity
Per APB transfer, with SETUP and ACCESS in PCLK cycles and bridge sync in HCLK cycles:
Worked (hypothetical SoC, HCLK 100 MHz): UART on PCLK 50 MHz costs 2 x 20 ns + 10 ns = 50 ns. Watchdog on PCLK 25 MHz costs 2 x 40 ns + 10 ns = 90 ns. One polling iteration is 140 ns. Stack check: (ISR nesting 1200 B plus call chain 900 B, against a 2048 B MSP).
How it works
The core issues a load or store. The address and control signals go to the decoder, which picks the target. Bridges and wait states add latency, and the write may sit in a buffer.
The address alone decides which device answers. Clock gating, reset and pin mux are prerequisites the decoder does not check, so an access can complete against a dead block.
When to use and when not to
| Situation | Use | Why |
|---|---|---|
| Plain config register, single owner | volatile pointer, one store | No race, no side effect on the write |
| W1C status shared with an ISR | Write only the target bit | RMW can clear an event that arrived in between |
| Single bit set or clear, shared | Set/clear alias or critical section | One bus write, no lost update |
| Next step depends on the write | DSB, then readback of a side-effect-free bit | A posted write is only a request |
| FIFO data register | Read the full word once | Each read pops one entry |
| Polled peripheral buffer | Device attribute in the MPU | Normal cacheable memory serves stale data |
Traps interviewers probe
- volatile means atomic or ordered. It does neither. Fix: name the three layers (compiler, CPU/store buffer, bus completion) and cover each one with the right mechanism.
- Read-modify-write on a W1C register. The write-back clears flags you never read. Fix: write 1 to the target bit only, or use the clear alias.
- Assuming a store has taken effect. Fix: DSB, then read a side-effect-free register in the same block, and confirm on a logic analyzer.
- Shrinking the register struct. Deleting RESERVED0 moves SR from offset 0x10 to offset 0x04, so reads hit the wrong register. Fix: take offsets from the reference manual and add static asserts on offsetof.
- Globals used before startup copies .data. SystemInit runs before the .data copy and the BSS clear, so a value it reads is zero on cold boot and survives only a warm reset. Fix: do not read initialized globals before the copy, or use a hard-coded value.
Interview tips
Open with the three layers: volatile fixes compiler caching and ordering among volatile accesses, barriers fix CPU reordering, and only a completed device access (readback after DSB) shows the hardware did it.
Expect these follow-ups: a W1C race with an ISR (single owner, alias or critical section), why a debugger read can clear a flag or pop a FIFO, MPU Device attributes for a polled buffer, DMA coherency with cacheable memory, and what evidence you accept that a write worked (logic analyzer, readback, high-water mark).
30-second outline: "volatile stops the compiler caching or dropping the access, but it is not atomic and does not order the CPU or the bus. For a shared W1C register I write only the target bit or use the alias. For the next step I issue a DSB and read back a side-effect-free bit, then confirm on a probe. I check offsets against the reference manual and mark the region Device."
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.