Learning Log
Assignment 1: GPIO Configuration and the Larson Scanner
Assignment 1 builds on the single circuit from Lab1 and expands it into eight circuits, with each LED controlled by its own GPIO. By operating the eight GPIOs, the LEDs light up in sequence to create a back-and-forth scanning effect.
1. Connecting registers, pins, and LEDs
Configure the pins' operating modes in PB_CFG0 and store their output states in PB_DAT. The GPIO hardware uses those settings and values to change the pin levels, which in turn control whether the LEDs are on or off.
There are three questions to answer: 1. Where are the registers? 2. What should I write to select output mode? 3. Where should I write zeros and ones to change the pin levels?

PB_CFG0 controls the operating modes of PB0–PB7. The GPIO controller's base address is 0x02000000, so:
Full address of PB_CFG0: 0x02000000 + 0x0030 = 0x02000030
Full address of PB_DAT: 0x02000000 + 0x0040 = 0x02000040The GPIO hardware changes the pin levels according to the configuration registers and the values in PB_DAT.
2. Selecting GPIO output mode in PB_CFG0

For this experiment with the MangoPi's D1-H SoC, I am focusing on two components: the CPU and the GPIO controller.
The GPIO controller contains GPIO registers. Here, PB_CFG0 is 32 bits wide, and each pin has a 4-bit configuration field, so one register can configure the operating modes of eight pins. The manual specifies 0001 as the encoding for GPIO output mode.
To select GPIO output mode for all eight pins in PB_CFG0, set each of the eight fields to 0001:
First, configure the pins' operating modes:
lui a0, 0x2000
lui a1, 0x11111
addi a1, a1, 0x111
sw a1, 0x30(a0)3. Using PB_DAT to select high or low output
PB_DAT is also 32 bits wide. Its lowest eight bits correspond to PB0–PB7. Here, the question is whether a pin should output a high or low level.
Once a pin is configured as an output, only one bit is needed to determine its output level:
1. 0: low.
2. 1: high.
| Current action | Lowest 8 bits of PB_DAT | Hexadecimal |
|---|---|---|
| Light PB0 | 00000001 | 0x01 |
| Light PB1 | 00000010 | 0x02 |
| Light PB2 | 00000100 | 0x04 |
| Light PB3 | 00001000 | 0x08 |
| … | … | … |
| Light PB7 | 10000000 | 0x80 |
| Return to PB6 | 01000000 | 0x40 |
| Return to PB5 | 00100000 | 0x20 |
This is the state that a Larson Scanner needs to keep changing.
To move the light from PB0 to PB7 and then back from PB7 to PB0, we repeatedly write different values to the same PB_DAT register.
4. Expressing the back-and-forth scan as pseudocode
Initialize:
Configure 8 GPIOs as outputs
Current state = 1
Direction = toward PB7
Repeat:
Write the current state to PB_DAT
Delay briefly
If PB7 has been reached:
Change direction toward PB0
If PB0 has been reached:
Change direction toward PB7
Shift the current state one bit left or right,
according to the direction
Return to the repeat stepSee you on November 13.
Assignment 1 本质上是在 Lab1 的一个电路基础上,扩展成 8 个回路,每个 LED 分别受一个 GPIO 控制。通过操作 8 个 GPIO,让不同位置的 LED 依次亮起,形成往返扫描效果。
一、先把寄存器、引脚和 LED 的关系串起来
在 PB_CFG0 中配置引脚的工作模式,在 PB_DAT 中存放输出状态,GPIO 硬件根据这些配置和值改变引脚电平,最终控制 LED 的亮灭。
这里需要解决三个问题: 1. 寄存器在哪里? 2. 往里面写什么,才能选择输出模式? 3. 往哪里写 0 和 1,才能改变引脚电平?

PB_CFG0 控制 PB0~PB7 的工作模式。整个 GPIO 控制模块的基地址是 0x02000000,因此:
PB_CFG0 的完整地址:0x02000000 + 0x0030 = 0x02000030
PB_DAT 的完整地址:0x02000000 + 0x0040 = 0x02000040GPIO 硬件按照配置寄存器和 PB_DAT 中的值,改变各引脚的电平。
二、在 PB_CFG0 中选择 GPIO 输出功能

在 MangoPi 的 D1-H SoC 芯片中,我这次先关注两个部分:CPU 和 GPIO 控制器。
GPIO 控制器里有 GPIO 寄存器。这里的 PB_CFG0 是 32 位,每个引脚有 4 位配置空间,所以一个寄存器可以配置 8 个引脚的工作模式。对于 GPIO 输出模式,手册给出的编码是 0001。
因此,要在 PB_CFG0 里为这 8 个引脚选择 GPIO 输出功能,就需要把 8 个字段都设为 0001:
先配置引脚的工作模式:
lui a0, 0x2000
lui a1, 0x11111
addi a1, a1, 0x111
sw a1, 0x30(a0)三、用 PB_DAT 决定输出高电平还是低电平
PB_DAT 寄存器也是 32 位,它的低 8 位分别对应 PB0~PB7。这里需要回答的是:这个引脚要输出高电平还是低电平?
引脚设为输出模式后,每个引脚只需要 1 位来决定输出电平:
1. 0:低电平。
2. 1:高电平。
| 当前动作 | PB_DAT 低 8 位 | 十六进制 |
|---|---|---|
| 点亮 PB0 | 00000001 | 0x01 |
| 点亮 PB1 | 00000010 | 0x02 |
| 点亮 PB2 | 00000100 | 0x04 |
| 点亮 PB3 | 00001000 | 0x08 |
| …… | …… | …… |
| 点亮 PB7 | 10000000 | 0x80 |
| 回到 PB6 | 01000000 | 0x40 |
| 回到 PB5 | 00100000 | 0x20 |
这就是 Larson Scanner 需要不断改变的状态。
如果想让灯从 PB0 移到 PB7,再从 PB7 移回 PB0,在这个过程中,我们只需要不断给同一个 PB_DAT 寄存器写入不同的数值。
四、把往返扫描整理成伪代码
初始化:
配置 8 个 GPIO 为输出
当前状态 = 1
方向 = 向 PB7 移动
重复:
把当前状态写入 PB_DAT
延迟一小段时间
如果到达 PB7:
改为向 PB0 移动
如果到达 PB0:
改为向 PB7 移动
根据方向,将当前状态左移或右移一位
回到重复部分11.13 见。