Memory-Mapped I/O
Embedded Systems & Firmware · Embedded Systems & Firmware track
5 min read
In this module
Hardware Is Just Memory Addresses
In Memory Fundamentals you learned that the CPU reaches memory through addresses. Embedded systems reuse that exact same mechanism to control hardware. Memory-mapped I/O (MMIO) reserves certain addresses for peripherals rather than RAM. A write to one of those addresses sends a command to a piece of hardware. A read returns the hardware's current state.
So driving hardware turns out to use the same LOAD and STORE instructions you've been writing all along. Try the memory-mapped-io simulation and watch a write to a "device register" flip a virtual LED.
GPIO: General Purpose Input/Output
The simplest peripheral is GPIO (General Purpose I/O). Each GPIO pin on the microcontroller can be configured as an input (reading a button press) or an output (driving an LED). GPIO is controlled through memory-mapped registers:
- A direction register sets each pin as input or output
- A data register reads input pin states or sets output pin states
- A pull-up/pull-down register configures internal resistors
To turn on an LED wired to pin 5, you set bit 5 of the GPIO output data register. In assembly, that's nothing more than a STORE instruction to the register's address. Setting one bit without disturbing the others means reading the register, ORing in the bit and writing it back, which an interrupt can tear in half; many chips add a set/reset register (STM32 calls it BSRR) so a single write changes only the bits you name. Bare-Metal Programming comes back to this.
Real firmware almost never writes a whole register blind, because other pins share it. It reads the register, changes the bits it owns and writes the result back. Try it: the data register below holds 0b01001010, so LEDs on pins 1, 3 and 6 are lit. Turn pin 5 on and pin 1 off without disturbing pins 3 and 6.
Bit Manipulation Puzzle
Worked Example: Finding Pin 5 on a Real Chip
On an STM32F4, the reference manual (RM0090) puts GPIO port A's registers at a base address of 0x40020000. Each register sits at a fixed offset from that base:
| Register | Offset | Address | Job |
|---|---|---|---|
| ODR (output data) | 0x14 | 0x40020014 | Current output level of each pin |
| BSRR (bit set/reset) | 0x18 | 0x40020018 | Write-only: set or clear chosen pins |
BSRR is split in two. Writing a 1 to bit n of the low half (bits 0-15) drives pin n high; writing a 1 to bit n + 16 drives it low. Zeros do nothing. So, to turn the LED on pin 5 on and off:
#define GPIOA_BSRR (*(volatile uint32_t *)0x40020018u)
GPIOA_BSRR = 1u << 5; /* 0x00000020: pin 5 high */GPIOA_BSRR = 1u << (5 + 16); /* 0x00200000: pin 5 low */Each line comes down to one store to the register. No read, no OR, nothing for an interrupt to tear in half, and the other fifteen pins on the port stay exactly as they were.
When a C compiler sees a write to a memory address followed by a read from the same address, it might optimise away the read (it "knows" the value). But for memory-mapped I/O, the hardware can change the value between the write and read. The C keyword volatile tells the compiler not to optimise away these accesses. Forgetting volatile is a common embedded bug that causes peripherals to misbehave. It doesn't make an access atomic, though, and it doesn't order it against ordinary memory accesses or DMA; that takes interrupt masking, atomic instructions or barriers.
Predict: this loop waits for a byte to arrive on USART1 by polling bit 5 (RXNE, "receive register not empty") of its status register at 0x40011000. The pointer isn't volatile. What does GCC at -O2 make of it?
uint32_t *sr = (uint32_t *)0x40011000u;while ((*sr & (1u << 5)) == 0) { }It reads the register once. As far as the compiler can see, nothing in the loop changes *sr, so reading it again would give the same answer. Compiled for a Cortex-M4 with arm-none-eabi-gcc -O2, the result is one load, one test, and then, if the bit was clear, a branch to itself:
ldr r3, [r3] @ read the status register once lsls r3, r3, #26 @ shift bit 5 into the sign bit bpl .L3 @ not set yet? go to .L3 bx lr.L3: b .L3 @ spin here for everThe byte can arrive, the hardware can set RXNE, and the CPU will never look. Declare the pointer volatile uint32_t * and the load moves inside the loop, so every pass reads the hardware again.
Communication Peripherals
Microcontrollers include built-in communication interfaces, each controlled through its own set of memory-mapped registers:
UART (Universal Asynchronous Receiver/Transmitter) handles serial communication. Drop a byte into the UART data register and the hardware shifts it out bit by bit on the TX pin. A byte arriving on the RX pin gets reassembled and dropped into the receive data register for you to read.
SPI (Serial Peripheral Interface) is a faster synchronous protocol using clock, data-in, data-out, and chip-select lines. It connects sensors, displays, flash memory chips, and other peripherals to the microcontroller.
I2C (Inter-Integrated Circuit) uses just two wires (clock and data) to communicate with multiple devices on a shared bus. Each device has a unique address.
Interrupts
Polling a peripheral, sitting in a loop checking its status register, burns CPU cycles for nothing. Interrupts fix that. The hardware itself signals the CPU when something happens — a byte arrived, a timer expired, a button got pressed. The CPU pauses what it was doing, saves its state, and jumps to an interrupt handler, a function written specifically for that event. When the handler finishes, the CPU picks up exactly where it left off.
Interrupts echo the signal handling ideas from Process Management & Scheduling, just much closer to the metal. They're the hardware mechanism that makes efficient I/O possible without an operating system mediating every access.