A microcontroller must interact with hardware such as GPIO, timers, ADCs, UARTs, SPI controllers, watchdogs and communication blocks. The processor needs a consistent way to read and control these peripherals. One of the most common approaches is memory-mapped I/O.
Memory-mapped I/O means that peripheral registers are assigned addresses inside the processor's address space. From the CPU's perspective, accessing a peripheral can therefore look very similar to accessing memory. Writing to a specific address may set a GPIO pin, start a timer, enable a UART or trigger an ADC conversion.
Fundamentals and Technology Overview
What Is a Memory Map?
A processor sees an address space. Different address ranges may represent:
- flash,
- SRAM,
- peripherals,
- system-control resources.
The organization of these ranges is called the memory map.
A simplified example might look like:
| Address Range | Function |
|---|---|
| 0x08000000 | Flash |
| 0x20000000 | SRAM |
| 0x40000000 | Peripheral region |
The actual values depend on the microcontroller. The vendor reference manual defines the real map.
What Is a Memory-Mapped Peripheral?
A peripheral contains control and data registers. These registers are assigned memory addresses. For example, a GPIO block may have registers for:
- mode,
- output type,
- input data,
- output data,
- bit set/reset.
ST's STM32G4 documentation explicitly defines GPIO control and data registers as memory-mapped peripheral registers.
Basic Register Access Example
Imagine a GPIO output register located at 0x48000014.
A low-level write could conceptually look like:
*(volatile uint32_t *)0x48000014 = value;
In real projects, vendor header files and CMSIS structures normally provide symbolic names instead of raw addresses.
Why Memory-Mapped I/O Is Useful
Memory-mapped I/O allows the CPU to use ordinary load and store instructions for hardware access. The processor does not need a separate instruction set just for I/O. This simplifies CPU architecture, compilers, C programming and debugging.
Memory-Mapped I/O vs Port-Mapped I/O
Some processor architectures historically used a separate I/O address space, often called isolated or port-mapped I/O. Memory-mapped architectures instead place peripheral registers inside the normal address space. Modern microcontrollers use memory-mapped peripherals extensively.
Registers Are Not Normal RAM
A peripheral register may look like a software variable, but it is connected to hardware.
Reading may return:
- input pin status,
- timer count,
- ADC result,
- UART status.
Writing may start a conversion, clear an interrupt or change PWM duty cycle. The behavior is defined by the peripheral hardware.
Register Access Types
Registers may be:
- read-only,
- write-only,
- read/write.
A read-only register may expose GPIO input state. A write-only register may trigger a command. Read/write registers commonly hold configuration.
Bit Fields and Reserved Bits
A 32-bit register may contain several independent fields. For example, bits 0-1 may select a mode and another field may set a prescaler. The reference manual defines each field, its reset state and access rules.
Reserved bits should be handled exactly as documented. Arbitrary writes can create undefined behavior or compatibility issues.
Why volatile Is Essential
Hardware registers can change independently of software. A timer increments, an input pin changes or a peripheral sets a status flag without normal program assignment.
That is why memory-mapped register definitions normally use volatile.
Consider:
while (STATUS == 0) {
}
Without volatile, a compiler might optimize repeated reads. Declaring a register volatile tells the compiler that each requested access must actually occur.
Vendor Header Files
Developers rarely type raw addresses directly. A vendor header may allow code such as:
GPIOA->ODR |= (1u << 5);
GPIOA maps to the peripheral base address and ODR identifies a register at a fixed offset.
Peripheral Base Addresses and Offsets
Each peripheral normally has a base address. Individual registers sit at defined offsets.
Conceptually:
register address = peripheral base + register offset
Suppose UART base is 0x40004000, status offset is 0x00 and data offset is 0x04. Then the status register is 0x40004000 and the data register is 0x40004004.
Register Structures in C
C structures are often used to represent an entire peripheral block:
typedef struct {
volatile uint32_t MODER;
volatile uint32_t OTYPER;
volatile uint32_t OSPEEDR;
} GPIO_TypeDef;
A pointer to this structure is placed at the hardware base address.
Internal Bus Architecture
The CPU does not connect directly to every peripheral. Modern microcontrollers use internal buses. Arm-based systems commonly use AMBA technologies such as:
- AHB,
- APB.
Arm's SoC design material describes how processors, memories and peripherals are connected through these interconnects.
AHB is generally used for higher-performance resources such as SRAM and DMA. APB is commonly used for lower-bandwidth peripherals such as GPIO, UART and timers.
Performance, Memory, and Data Processing
Address Decoding
When the CPU issues a memory access, the bus fabric examines the address and determines which hardware block responds. This is called address decoding.
GPIO as a Practical Example
GPIO is an excellent example of memory-mapped I/O. Common registers include:
- MODER,
- OTYPER,
- OSPEEDR,
- IDR,
- ODR,
- BSRR.
ST's STM32G4 reference manual documents these as memory-mapped control and data registers.
Atomic Set and Reset Registers
Read-modify-write sequences can create race conditions. Many MCUs provide dedicated set and clear registers.
STM32's BSRR lets software set or reset selected GPIO bits in one write without modifying unrelated output bits.
The Read-Modify-Write Problem
Suppose software:
- reads a register,
- modifies one bit,
- writes it back.
An interrupt may change another bit between steps 1 and 3. The final write can erase the interrupt's change. Atomic set/clear registers avoid this problem.
Peripheral Side Effects
Peripheral registers can have side effects. Examples include:
- reading clears a flag,
- writing 1 clears an interrupt,
- reading a FIFO removes data.
Never assume a peripheral register behaves like ordinary RAM.
Write-One-to-Clear
Many status registers use write 1 to clear semantics. A 1 clears the corresponding flag, while a 0 leaves it unchanged. This can make status handling safer and more direct.
Memory Barriers
Modern processors and bus fabrics may reorder certain operations. For low-level synchronization or system control, memory barriers may be required.
Arm provides instructions such as:
- DMB,
- DSB,
- ISB.
These help guarantee ordering when required.
Memory Types and Attributes
Peripheral regions are normally treated as device memory, not ordinary cacheable RAM. This controls behaviors such as caching and speculative access so that software observes the current hardware state.
Memory Protection Unit
An MPU can restrict software access to memory regions and peripherals. This can protect:
- safety registers,
- privileged peripherals,
- critical memory.
For example, an ordinary task can be blocked from writing a motor-shutdown register.
Privileged Access
Some system resources can be restricted to privileged code. This improves stability, security and fault isolation.
Cortex-M Private Peripheral Bus
Cortex-M processors include a Private Peripheral Bus region. Arm documentation describes memory-mapped access to processor resources such as:
- NVIC,
- MPU,
- debug blocks,
- system control.
Interrupt configuration itself is therefore memory mapped.
Timer Registers
A timer may provide registers for:
- control,
- prescaler,
- counter,
- compare,
- capture.
Software configures these registers and the hardware timer then operates independently.
ADC Registers
An ADC may expose registers for:
- channel selection,
- trigger configuration,
- status,
- result data.
The CPU configures the converter and reads the result register after conversion.
Connectivity and System Integration
UART Registers
UART registers may include:
- control,
- baud rate,
- transmit data,
- receive data,
- interrupt flags.
All are presented through addresses in the memory map.
DMA and Memory-Mapped I/O
DMA controllers can move data directly between peripheral registers and SRAM without CPU intervention.
Example:
ADC data register → RAM buffer
Because both locations are addressable, DMA fits naturally into memory-mapped architectures.
Polling vs Interrupts
Software can poll a register:
while ((UART->STATUS & READY) == 0) {
}
Polling is simple but keeps the CPU active. Interrupt-driven designs let the CPU sleep or do other work until the peripheral needs service.
Register-Level Programming vs HAL
Direct register programming gives:
- precise control,
- low overhead,
- full hardware visibility.
A Hardware Abstraction Layer provides higher-level APIs that improve readability and development speed.
Many industrial projects use both. HAL is useful for initialization and noncritical code, while direct register access may be appropriate for tight timing or special hardware behavior.
CMSIS
CMSIS standardizes many Cortex-M software interfaces, including definitions for:
- NVIC,
- SysTick,
- core registers.
Vendor device headers extend the same model to chip-specific peripherals.
Address Alignment and Register Width
A 32-bit register is normally aligned to a 4-byte boundary. Access width should match the peripheral specification.
Incorrect alignment or width can cause:
- bus faults,
- incorrect data,
- extra cycles.
Endianness
Endianness determines byte order in multi-byte values. Many Cortex-M systems operate little-endian. This matters when interpreting network packets, buffers and raw memory.
Cache and Peripheral Regions
Peripheral regions are normally non-cacheable. Caching a hardware status register would be incorrect because software must observe the actual current device state.
Debugging Memory-Mapped I/O
Modern debuggers often provide peripheral register views. Engineers can inspect:
- GPIO configuration,
- timer state,
- interrupt flags,
- ADC registers.
A logic analyzer or oscilloscope can then verify the physical effect of register writes.
Common Mistakes
Wrong Base Address
Use vendor headers and the official reference manual.
Peripheral Clock Disabled
Many peripherals require their bus clock to be enabled before register access.
Ignoring Reset Values
Registers have documented reset states. Never assume a peripheral starts in the desired configuration.
Wrong Bit Mask
A bad mask can change unrelated fields.
Missing volatile
This can create subtle compiler-dependent failures.
Unsafe Read-Modify-Write
Use atomic set/clear features where available.
Industrial Relevance
Memory-mapped I/O is central to:
- PLCs,
- motor drives,
- robots,
- smart sensors,
- communication modules.
A motor-control ISR may directly read an ADC result and write a PWM compare value. These accesses must be fast and predictable.
Safety and Security
Safety systems can use memory protection to block ordinary code from changing watchdogs, shutdown controls or critical peripheral registers.
Security architectures can similarly restrict access to cryptographic hardware, secure storage and debug controls.
A Good Learning Exercise
To understand memory-mapped I/O:
- open the MCU memory map,
- find a GPIO base address,
- find the mode-register offset,
- configure one output,
- write the output register,
- inspect it with a debugger,
- observe the physical pin.
This simple exercise explains much of low-level MCU programming.
Conclusion
Memory-mapped I/O is one of the foundational ideas behind embedded microcontroller design.
Peripheral registers are assigned addresses inside the processor's address space. The CPU uses normal memory-access instructions to configure and control GPIO, timers, ADCs, UARTs, DMA, interrupt controllers and system resources.
The most important practices are:
- use the correct address,
- understand register semantics,
- declare hardware registers volatile,
- avoid unsafe read-modify-write operations,
- follow memory-ordering rules where required.
Once these concepts are clear, reference manuals become much easier to use and low-level embedded debugging becomes far more systematic.