Learning Log
On-Chip Peripherals, Address Decoding, and Pin Multiplexing
The MangoPi's D1-H is a system on a chip (SoC). Besides the CPU, the chip integrates dozens of separate hardware modules called on-chip peripheral controllers, such as the PIO controller for GPIO ports and UART controllers for serial communication. Each controller contains its own digital logic and registers.
Peripheral registers are the “control and status panel” that each controller exposes to the CPU bus. The LEDs and current-limiting resistors plugged into our breadboard, by contrast, are outside the chip: they are off-chip components.
The number of physical pins around a chip package is limited. To make better use of them, the hardware allows functions from multiple controllers to share the same physical pin. Each on-chip controller has its own dedicated register block in a different memory-mapped address range. A single general-purpose register is not responsible for implementing all these functions or connecting all the peripherals.
How can multiple controllers share one physical pin?
This requires a multiplexer (MUX). As a first mental model, think of it as function-selection logic between the controller signals and the pin: a selection code determines which control path the physical pin uses. The available functions for each pin and their codes are specified by the chip design.
There are two different kinds of “selection” to distinguish:
First selection: address decoding — which register receives the write?
The CPU issues a write, for example writing 0x00000001 to address 0x02000030. Address decoding and the access path locate PB_CFG0 and complete the write; PB_CFG0 holds the value.
Second selection: pin multiplexing — which function controls the pin?
The PB0 function field in PB_CFG0 is 0001. These field bits determine the function selection. The MUX / function-selection logic selects the GPIO output path, so PB0 uses GPIO output rather than another available function such as UART.
Address decoding determines which register receives the data; the MUX uses the selection code stored in a register to determine which function the physical pin uses.
Thinking of the MUX as an automatic switch
A multiplexer can be pictured as an automatic switch with several inputs and one output. It takes two kinds of input:
- Candidate signals, such as GPIO output data and a UART controller's transmit signal. - A selection code that tells it which function path to choose.
When a pin is configured for GPIO output, the corresponding data-register bit in the PIO controller drives it. Once the pin is switched to the UART transmit function, its output path is taken over by the UART controller's transmit circuitry.
At that point, continuing to write 0 or 1 to the corresponding GPIO data-register bit no longer changes the pin's level through the GPIO output path.
The complete chain I connected today
CPU executes `sw` → address decoding locates CFG → CFG stores the function-selection code → the MUX selects the pin's control path → in GPIO output mode, DAT data drives the pin.
Original note

芒果派的 D1-H 是一颗 SoC。在这颗芯片内部,除了 CPU,还集成了几十个独立的硬件功能模块,称为片上外设控制器,例如 GPIO 端口控制器 PIO、UART 串口控制器等。这些控制器内部有各自的数字逻辑电路和寄存器。
外设寄存器只是每个控制器面向 CPU 总线开出的“控制与状态面板”。 而我们在面包板上插的 LED 灯、限流电阻,位于芯片外部,属于片外器件。
芯片封装边缘的金属引脚数量有限。为了解决引脚不够用的问题,硬件设计允许多个控制器的功能共享同一个物理引脚。每个片上控制器都有自己专属的一套寄存器组,分布在不同的内存映射地址区间,而不是用一个通用寄存器来实现所有不同的功能、连接不同的外设。
多个控制器怎样共享同一个物理引脚?
这里需要的是多路选择器(MUX)。可以先把它理解为位于控制器信号与引脚之间的功能选择逻辑:它根据选择码,决定物理引脚使用哪一条控制路径。每个引脚可选择的功能及其编码,由芯片设计规定。
这里有两种不同的“选择”,需要分开理解:
第一种选择:地址译码——写给谁?
CPU 发出写入操作,例如向地址 0x02000030 写入数据 0x00000001。地址译码与访问通路定位到 PB_CFG0,完成数据写入;PB_CFG0 保存这个值。
第二种选择:引脚复用——由谁控制?
PB_CFG0 中 PB0 的功能字段为 0001,这个字段的位就是功能选择的依据。MUX/功能选择逻辑根据它选择 GPIO 输出路径,使 PB0 引脚使用 GPIO 输出功能,而不是 UART 等其他可复用功能。
地址译码是在决定哪个寄存器收到数据;MUX 是在根据寄存器中保存的选择码,决定物理引脚使用哪种功能。
把 MUX 想象成一个自动开关
MUX(多路选择器)可以想象成一个有多个入口、一个出口的自动开关。它接受两类输入:
- 候选信号:例如 GPIO 的输出数据、UART 控制器的发送信号。 - 选择码:告诉它应该选择哪一条功能路径。
当我们把某个引脚配置为 GPIO 输出功能时,引脚由 PIO 控制器里的数据寄存器对应位驱动;一旦把该引脚切换为 UART 发送功能,物理引脚的输出路径就会改由 UART 控制器内部的发送电路接管。
这个时候,即使继续往 GPIO 数据寄存器对应位写入 0 和 1,也不会再通过这条 GPIO 输出路径改变该引脚的电平。
今天串起来的完整链路
CPU 执行 `sw` → 地址译码定位 CFG → CFG 保存功能选择码 → MUX 根据选择码确定引脚的控制路径 → GPIO 在输出模式下使用 DAT 数据驱动引脚。
原稿
