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?

The GPIO register list in the D1-H chip manual: module base address 0x02000000, PB_CFG0 offset 0x0030, and PB_DAT offset 0x0040.

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 = 0x02000040

The 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

PB_CFG0 field definitions in the D1-H chip manual: each pin's function-selection field occupies 4 bits, and 0001 selects output mode.

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:

PB_CFG0's 32-bit layout: PB7 through PB0 each occupy 4 bits. Setting every field to 0001 produces the combined value 0x11111111.

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 actionLowest 8 bits of PB_DATHexadecimal
Light PB0000000010x01
Light PB1000000100x02
Light PB2000001000x04
Light PB3000010000x08
………
Light PB7100000000x80
Return to PB6010000000x40
Return to PB5001000000x20

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 step

See you on November 13.