Learning Log
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 respondsSoftware 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 resource | Role in this boot process |
|---|---|
| BootROM | Holds startup code placed there by the chip manufacturer |
| On-chip SRAM | Provides a small working space that does not depend on external DDR |
| External DDR | Becomes 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 programsHere, “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 0x40000000These 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 32Reading 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:
| Address | Register | Role in this example |
|---|---|---|
0x02000098 | PD_CFG2 | Configures the functions of PD18 and other pins |
0x020000A0 | PD_DAT | Contains the data bits for port D pins |
The course demonstration uses:
xfel write32 0x02000098 0x100
xfel write32 0x020000A0 0x40000The 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 = 0x100The 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 = 0x40000Setting 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 changesSoftware 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

今天围绕 MangoPi 的实验,我遇到了几个相互关联的问题:
既然地址是软件给出的,读写操作也是 CPU 执行的,为什么 MMIO 被称为一种硬件设计方式?外部 DDR 还没有初始化时,初始化程序又在哪里运行?向 GPIO 寄存器写入一个数,为什么就能改变引脚状态,让 LED 亮起来?
我先把启动和访存过程串起来,再把这次的深挖目标锁定在:GPIO 控制寄存器中的比特,怎样对应到 PD18 的工作模式和输出电平。
一、为什么 MMIO 是硬件机制?
MMIO 是 Memory-Mapped I/O,即内存映射输入输出。它让 CPU 使用类似访问内存的读写方式,访问外设寄存器。
“映射进地址空间”不等于“变成了 DDR 内存”。 同样是一笔地址访问,最终响应它的可能是内存,也可能是 GPIO、UART 等外设。
软件负责指定地址、准备数据并发起访问。SoC 内部的地址译码与互连电路,则根据硬件规定的地址映射,把请求送到相应的设备。
可以先把它理解成:
CPU 发出地址、数据和读写请求
↓
SoC 地址译码与互连
↓
选中相应的内存控制器或外设
↓
目标设备响应访问软件不能随意指定一个地址,就让那个地址变成 GPIO 寄存器。它必须使用芯片规定的地址。
因此,MMIO 的关键在于:硬件建立地址与设备之间的对应关系,软件按照这个对应关系访问设备。
二、DDR 尚未初始化,初始化程序在哪里运行?
我原来的疑问是:程序运行需要内存,而初始化内存又需要运行程序,这不是循环依赖吗?
这里需要先区分几种存储空间:
| 存储空间 | 在这个启动过程中承担的作用 |
|---|---|
| BootROM | 保存芯片厂商预先放入的启动代码 |
| 片内 SRAM | 提供不依赖外部 DDR 的小容量工作空间 |
| 外部 DDR | 完成控制器、时序等初始化后,供后续程序使用 |
CPU 执行程序,并不要求指令和数据一定放在外部 DDR。 启动阶段可以先依靠 BootROM 和片内 SRAM,再准备外部 DDR。
在这个实验的 FEL 启动路径中,我可以把过程理解为:
Pi 通电,开始执行 BootROM 代码
↓
进入 FEL 模式,等待主机请求
↓
Mac 运行 xfel ddr d1
↓
目标芯片执行 DDR 初始化代码
↓
外部 DDR 可以正常用于后续程序的装载和运行这里的“初始化 DDR”,主要是在准备内存控制器及相关硬件配置,不是简单地把 DDR 里的所有字节清零。
三、程序怎样从 Mac 到 MangoPi,再开始执行?
DDR 准备好之后,还需要区分两件事:把程序写进去,以及让 CPU 开始执行它。
课程中的命令顺序是:
xfel ddr d1
xfel write 0x40000000 blink-actled.bin
xfel exec 0x40000000这三步分别表示:
1. 初始化目标板的 DDR。 2. 把程序文件写入目标内存,从指定地址开始存放。 3. 请求目标芯片从指定入口地址开始执行。
这里的 0x40000000 是本实验使用的装载与执行地址,不是所有 RISC-V 机器都必须使用的统一地址。
Mac 的 CPU 执行的是 xfel;MangoPi 的 CPU 先运行 FEL 相关代码,接收和处理请求,再把执行控制权交给新程序。终端里敲下一条命令,并不是 Mac 直接修改了 MangoPi 的 PC,而是经过了主机与目标芯片之间的通信和处理。
还可以用 hexdump 查看目标地址附近的内容:
xfel hexdump 0x40000000 32不过,读回数据只能帮助确认相应内存内容;它本身不能证明程序已经开始执行。
四、为什么写入 0x100 和 0x40000 能影响 LED?
这是我这次真正想拆开的节点。
课程使用的 MangoPi 板载蓝色 LED 由 PD18 控制。相关寄存器包括:
| 地址 | 寄存器 | 在本例中的作用 |
|---|---|---|
0x02000098 | PD_CFG2 | 配置 PD18 等引脚的功能 |
0x020000A0 | PD_DAT | 对应 D 组引脚的数据位 |
课程演示使用:
xfel write32 0x02000098 0x100
xfel write32 0x020000A0 0x40000第一步配置 PD18 为输出,第二步将它的输出数据位置为 1,在课程板卡的连接方式下点亮蓝色 LED。
1. 地址决定修改哪个寄存器
0x02000098 和 0x020000A0 是地址;0x100 和 0x40000 是写入的数据。
地址回答“访问谁”,寄存器的位定义回答“写进去的比特表示什么”。
只看一个十六进制数,并不知道它有什么硬件意义,必须结合它所在寄存器的定义。
2. 0x100 对应 PD18 的输出模式配置
PD18 的功能选择字段位于 PD_CFG2 中从 bit 8 开始的位置。输出模式的编码为 1,把这个编码放到对应位置,就得到:
1 << 8 = 0x100因此,这个值的意义来自“字段位置”和“模式编码”的组合。
换一个引脚或寄存器,字段位置可能就不同。
3. 0x40000 对应数据寄存器的 bit 18
在 PD_DAT 中,PD18 对应 bit 18,从 bit 0 开始计数:
1 << 18 = 0x40000把这一位置为 1,表示为 PD18 设置相应的高电平输出数据。前提是它已经被配置成 GPIO 输出模式。
因此,两步操作分别处理两个问题:
功能配置决定“这个引脚以什么方式工作”;数据配置决定“作为输出时,要输出什么状态”。
五、从终端命令到 LED 的完整链路
现在可以把这次操作串起来:
Mac 上运行 xfel write32
↓
经 USB 与目标芯片的 FEL 机制通信
↓
目标芯片完成指定地址的写入
↓
SoC 将访问送到 GPIO 控制器
↓
GPIO 寄存器中的模式或数据位发生变化
↓
GPIO 电路根据配置驱动 PD18
↓
板载 LED 的状态发生变化软件写下的是地址和比特,硬件电路实现的是这些比特所约定的行为。
这也回应了最开始的问题:MMIO 不只是软件里保存了一张地址表,而是硬件能够根据地址选择设备,并让对设备寄存器的访问产生相应效果。
实验成功记录
