From MMIO to Lighting an LED: How Does an Address Affect Real Hardware?

Today’s MangoPi experiment raised several connected questions:

If software supplies the address and the CPU performs the read or write, why is MMIO considered a hardware design mechanism? Where does the initialization program run before external DDR is initialized? And why can writing a number to a GPIO register change a pin’s state and turn on an LED?

I first connected the boot and memory-access processes, then narrowed this exploration to one question: how the bits in GPIO control registers correspond to PD18’s operating mode and output level.

1. Why is MMIO a hardware mechanism?

MMIO stands for Memory-Mapped I/O. It lets the CPU access peripheral registers using reads and writes similar to those used for memory.

Being mapped into the address space does not mean becoming DDR memory. The device responding to an address access might be memory, GPIO, UART, or another peripheral.

Software specifies an address, prepares data, and initiates an access. Address-decoding and interconnect circuits inside the SoC then route the request to the appropriate device according to the hardware-defined address map.

For now, I can understand the process as:

CPU issues an address, data, and a read/write request
              ↓
SoC address decoding and interconnect
              ↓
Select the appropriate memory controller or peripheral
              ↓
The target device responds

Software cannot choose an arbitrary address and thereby turn it into a GPIO register. It must use the addresses defined for the chip.

The key to MMIO is therefore: hardware establishes the correspondence between addresses and devices, and software accesses devices according to that correspondence.

2. Where does the initialization program run before DDR is initialized?

My original question was: programs need memory to run, but initializing memory requires running a program. Is that a circular dependency?

First, I need to distinguish several storage resources:

Storage resourceRole in this boot process
BootROMHolds startup code placed there by the chip manufacturer
On-chip SRAMProvides a small working space that does not depend on external DDR
External DDRBecomes available to later programs after controller, timing, and related initialization

Executing a program does not require its instructions and data to be stored in external DDR. Early startup can rely on BootROM and on-chip SRAM before preparing external DDR.

For the FEL boot path in this experiment, I can understand it as:

Pi powers on and begins executing BootROM code
                ↓
Enter FEL mode and wait for host requests
                ↓
Mac runs xfel ddr d1
                ↓
The target chip executes DDR initialization code
                ↓
External DDR is ready for loading and running later programs

Here, “initializing DDR” mainly means preparing the memory controller and related hardware settings. It does not simply mean clearing every byte in DDR to zero.

3. How does a program get from the Mac to the MangoPi and start running?

Once DDR is ready, there are still two distinct operations: writing the program into memory and getting the CPU to execute it.

The course uses this command sequence:

xfel ddr d1
xfel write 0x40000000 blink-actled.bin
xfel exec 0x40000000

These steps mean:

1. Initialize DDR on the target board. 2. Write the program file into target memory, starting at the specified address. 3. Ask the target chip to begin execution at the specified entry address.

Here, 0x40000000 is the load and execution address used in this experiment, not a universal address required by all RISC-V machines. The Mac’s CPU executes xfel. The MangoPi’s CPU first runs FEL-related code to receive and process requests, then hands execution over to the new program. Typing a terminal command does not mean the Mac directly changes the MangoPi’s PC; communication and processing take place between the host and target chip.

A hexdump can also show the contents near the target address:

xfel hexdump 0x40000000 32

Reading data back can help confirm the corresponding memory contents, but does not by itself prove that execution has begun.

4. Why do writes of 0x100 and 0x40000 affect the LED?

This is the part I really want to unpack in this exploration.

The course’s MangoPi board uses PD18 to control its blue onboard LED. The relevant registers include:

AddressRegisterRole in this example
0x02000098PD_CFG2Configures the functions of PD18 and other pins
0x020000A0PD_DATContains the data bits for port D pins

The course demonstration uses:

xfel write32 0x02000098 0x100
xfel write32 0x020000A0 0x40000

The first step configures PD18 as an output. The second sets its output data bit to 1, lighting the blue LED with the course board’s wiring.

1. The address determines which register is modified

0x02000098 and 0x020000A0 are addresses; 0x100 and 0x40000 are the data being written.

The address answers “which device or register do I access?” The register’s bit definitions answer “what do these written bits mean?”

A hexadecimal value alone does not reveal its hardware meaning. It has to be interpreted using the definition of the register containing it.

2. 0x100 corresponds to PD18’s output-mode configuration

PD18’s function-select field in PD_CFG2 starts at bit 8. The encoding for output mode is 1. Placing that encoding at the appropriate position gives:

1 << 8 = 0x100

The meaning of this value therefore comes from the combination of the field position and the mode encoding.

For a different pin or register, the field position may be different.

3. 0x40000 corresponds to bit 18 of the data register

In PD_DAT, PD18 corresponds to bit 18, counting from bit 0:

1 << 18 = 0x40000

Setting this bit to 1 sets the corresponding high-level output data for PD18, provided that the pin has already been configured as a GPIO output.

The two steps therefore address two different questions:

The function configuration determines how the pin operates; the data configuration determines what state it outputs when operating as an output.

5. The complete path from terminal command to LED

I can now connect the operation into one sequence:

Run xfel write32 on the Mac
            ↓
Communicate with the target chip’s FEL mechanism over USB
            ↓
The target chip performs the write to the specified address
            ↓
The SoC routes the access to the GPIO controller
            ↓
Mode or data bits in the GPIO registers change
            ↓
GPIO circuitry drives PD18 according to the configuration
            ↓
The onboard LED’s state changes

Software writes addresses and bits; hardware circuits implement the behavior those bits specify.

This also answers my opening question: MMIO is more than an address table held in software. Hardware selects a device based on the address and makes accesses to its registers produce the corresponding effects.

Successful experiment

Successful experiment: a Mac connected to a MangoPi, with the LED on the breadboard lit.