Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 173 of 9363. Microcontroller Fundamentals and Bare-Metal CFree lesson

Mapping Peripheral Hardware to Memory Addresses

Learners will map internal hardware peripherals to specific memory locations to enable communication between the CPU and external components.

In this lesson, you will learn how the CPU treats physical hardware components—like LEDs or sensors—as if they were simple entries in the system's memory.

Memory-Mapped I/O (MMIO)

In high-performance computing, we worry about cache hierarchies and pipeline stalls. However, in embedded systems, the CPU needs a way to talk to the "outside world." We achieve this through Memory-Mapped I/O. Instead of creating special instructions to talk to hardware, the architecture assigns a specific memory address to each peripheral's control register.

When your code writes a value to a specific address, the hardware isn't actually storing that data in RAM. Instead, the memory bus identifies that the address belongs to a peripheral (like a GPIO controller) and routes that data directly to the hardware logic.

A Concrete Example

Imagine an LED connected to your microcontroller. The system designer has mapped the LED’s control register to the address 0x40021000. To turn the LED on, we simply write a bit to that memory location as if it were a standard variable.

// Define the memory address for the LED register
#define LED_REGISTER (*(volatile unsigned int*)0x40021000)

void turn_on_led() {
    // Writing to this address triggers the hardware logic
    LED_REGISTER = 0x01; 
}

The volatile keyword is critical here. It tells the compiler, "Do not optimize this away." Without it, a smart compiler might assume that because you aren't "reading" the variable, it can skip the store instruction—which would prevent the hardware from ever receiving your signal.

Why this matters

By treating hardware as memory, we can use standard load and store instructions to interact with peripherals. This unified interface keeps the instruction set simple and allows us to use standard C pointers to manipulate complex physical hardware. When the CPU executes a store instruction to that memory address, the bus hardware intercepts the request and flips a physical voltage switch on your chip, enabling the peripheral component to act accordingly.

Key Takeaway: Memory-Mapped I/O allows the CPU to control physical hardware by performing standard memory writes to specific, pre-assigned addresses, effectively bridging the gap between software logic and electrical signals.