Pin Mux: Every Function on Every Pad

A pad can carry several functions, one at a time, chosen by the pin mux. Test each cell of the pin table from firmware. Check its direction, its reset state, and what happens when two pads want one input.

You Will Be Able To

  • Read a pin table as a grid of cells, each one a function to test.
  • Select a function with PINMUX and prove it at the pad, with a board model or through GPIO IN.
  • State the reset state of the pads and why it is chosen.
  • Explain what the chip does when two pads select the same input function.
Lesson
V8.3
Time
26 min
Runs on
Your browser
Check
5 questions

A first bring-up program prints "hello" through the UART, runs to its end, and the board's terminal shows nothing. Nobody wrote the pin mux.

This lesson fills the grid below, cell by cell.

The Pin Table as a Grid to Fill
PadSelect 0Select 1Select 2Select 3
IO0releasedgpio (inout)uart_tx (out)pwm (out)
IO1releasedgpio (inout)uart_rx (in)capture (in)
IO2releasedgpio (inout)spi_sclk (out)pwm (out)
IO3releasedgpio (inout)spi_miso (in)capture (in)
IO4releasedgpio (inout)spi_mosi (out)event_in (in)
IO5releasedgpio (inout)spi_cs_n (out)—
IO6releasedgpio (inout)——
IO7releasedgpio (inout)——
IO8releasedgpio (inout)——
IO9releasedgpio (inout)——
IO10releasedgpio (inout)——
IO11releasedgpio (inout)——
IO12releasedgpio (inout)——
IO13releasedgpio (inout)——
IO14releasedgpio (inout)——
IO15releasedgpio (inout)——
IO15, alwayswake

Built from pins.csv of ap_soc v1.5 when this page is built: 28 rows for the sixteen IO pads. Every cell with a function is one thing a pin-mux test must reach.

Before You Start

Read Toggle Tests in Simulation and The Life of a C Test first.

Five questions this page answers
Question
1What does one cell of the pin table promise, and how do you test it?
2How can firmware check an output function the board does not watch?
3What state are the pads in after reset, and why?
4What does ap_soc do when two pads select the same input?
5Why does a toggle in GPIO mode not test the pin mux?

The Idea: Each Cell Is a Path

ap_soc has more signals that want a pad than it has pads. UART0, the SPI controller, the PWM output and the timer's event input all share the first six pads with GPIO. PINMUX in ap_sysctl gives each pad two bits, and those bits choose which function owns the pad.

So a pad is not one wire; it is up to four. Each cell of the grid is a separate path: from a block, through the mux, to the pad, or the other way. A test of the pin mux must select each cell and show the path works. A walk in GPIO mode only tests the select-1 column.

Two more promises hide in the table. The first is the reset state: before firmware writes PINMUX, which pads does the chip drive? The second is what happens when two pads select the same input function. Both are choices, and both need a test.

One Pad, Four PathsPINMUX chooses which block owns a pad. Each choice is a separate path, checked at the pad by a board model or read back through GPIO IN.
  • Peripheral
  • Control / FSM
  • Bus
  • Test
  • Data
  • Control
One Pad, Four PathsPINMUX chooses which block owns a pad. Each choice is a separate path, checked at the pad by a board model or read back through GPIO IN. peripheral UART0, SPI,Timer and PWM,GPIO block signals control PINMUX two bits a pad bus Pin mux select 0 to 3 bus Pad released at reset test Board models UART model, SPI flash peripheral GPIO IN reads every pad outputs and inputsselectthe chosen pathchecked hereor read back here
  1. control PINMUXtwo bits a pad
    • select → Pin mux
  2. bus Pin muxselect 0 to 3
    • the chosen path → Pad
  3. peripheral UART0, SPI, Timer and PWM, GPIOblock signals
    • outputs and inputs → Pin mux
  4. bus Padreleased at reset
    • checked here → Board models
    • or read back here → GPIO IN
  5. test Board modelsUART model, SPI flash
  6. peripheral GPIO INreads every pad
  1. Choose

    Firmware writes a pad's two bits of PINMUX; the mux connects one block signal to the pad.

  2. Drive or read

    An output function drives the pad; an input function reads it.

  3. Check at the pad

    A board model checks some functions; GPIO IN reads back any pad for the rest.

Read the diagram as text
  • UART0, SPI, Timer and PWM, GPIO: block signals
  • PINMUX: two bits a pad
  • Pin mux: select 0 to 3
  • Pad: released at reset
  • Board models: UART model, SPI flash
  • GPIO IN: reads every pad
  • UART0, SPI, Timer and PWM, GPIO → Pin mux: outputs and inputs
  • PINMUX → Pin mux: select
  • Pin mux → Pad: the chosen path
  • Pad → Board models: checked here
  • Pad → GPIO IN: or read back here

gen/rtl/ap_soc_pinmux.sv, generated from pins.csv (facts §27.11).

New Words

New words on this page
WordMeaning
Pin muxThe logic that connects each pad to one of several block signals, chosen by a select.
SelectThe value of a pad's two PINMUX bits: 0 released, 1 GPIO, 2 the serial function, 3 the auxiliary function.
ReleasedSelect 0: the chip drives the pad with nothing, so the board decides its level.
DirectionWhether a function drives the pad (out) or reads it (in).
Loopback through GPIO INReading an output function's level back through GPIO IN, which sees every pad whatever PINMUX says.
Input conflictTwo pads selecting the same input function at once; the chip must define what the function then sees.

The Smallest Example: Writing the Word

Pad n owns bits 2n+1 and 2n of PINMUX. To put UART0 on IO0 and IO1 at select 2, write 2 into bits 1:0 and 2 into bits 3:2.

From cells to the PINMUX word
Cells chosenBits writtenPINMUX
IO0 select 2, IO1 select 21:0 = 10, 3:2 = 100x0000_000A
IO0 to IO5 select 2six fields of 100x0000_0AAA
IO2 select 3 only5:4 = 110x0000_0030

Worked by hand from facts §27.3; the runnable example below prints the first two.

Get one field wrong and the select lands in another pad's bits. The chip does exactly what the word says, so the wrong pad changes function and the right one stays released.

The Rules

  1. Rule 1: Each cell is its own path.

    Select it, then show the function works at the pad. One cell passing says nothing about the next.

  2. Rule 2: Check an output where it arrives.

    A board model, such as the UART model or the flash, checks some outputs. GPIO IN reads back any pad for the rest.

  3. Rule 3: Check an input by driving the pad.

    The board drives the pad, and the function must see it: a byte received, an event counted.

  4. Rule 4: The reset state releases every pad.

    With PINMUX at 0 the chip drives nothing, so a pad cannot fight the board before firmware decides. It is ap_soc's choice.

  5. Rule 5: An input conflict needs a defined answer.

    On ap_soc, capture selected on IO1 and IO3 at once sees the OR of the two pads. Test it, because firmware may do it by mistake.

  6. Rule 6: An always-on listener ignores the mux.

    IO15 reaches the wake detector whatever PINMUX says; test it with the mux at more than one value.

Sources for These Rules

The pin table and the generated mux: pins.csv and gen/rtl/ap_soc_pinmux.sv. PINMUX: facts §27.3. Pads and IO15: facts §27.11.

The UART model and the SPI flash model of the board: facts §27.12, tools/soc/ap_soc/bench/soc_tb/README.md.

Multiplexed I/O as a large part of an SoC's connections: MEHTA-DV 10.9.

The Lab: A Pin Tour from Firmware

The tour runs on the processor. It reads PINMUX and GPIO IN straight after reset, then gives IO0 and IO1 to UART0 and sends one byte. Then it gives IO2 to IO5 to the SPI controller and asks the board's flash for its ID.

The tour (main.c), built with the firmware kit.
#include "hal.h"
#include "tb.h"
#include "drivers/uart.h"
#include "drivers/spi.h"

int main(void)
{
    tb_puts("PINMUX ");
    tb_put_hex(reg_read32(SYSCTL_PINMUX));            /* 0 at reset: no pad is driven by the chip */
    tb_puts("\nGPIO IN ");
    tb_put_hex(reg_read32(GPIO_IN) & 0xffff);         /* the board's levels: IO1 is the idle UART line */
    tb_puts("\n");
    reg_write32(SYSCTL_PINMUX, (2u << 0) | (2u << 2));   /* IO0 UART TX, IO1 UART RX */
    uart_putc('K');
    uart_flush();
    reg_write32(SYSCTL_PINMUX, (2u << 0) | (2u << 2) | (2u << 4) | (2u << 6) | (2u << 8) | (2u << 10));
    unsigned id = spi_transfer(0x9f);                 /* the flash answers C0 FF EE */
    tb_puts("flash ");
    tb_put_hex(id);
    tb_puts("\n");
    if (id != 0xc0ffee) return 1;
    return 0;
}
The console lines and part of the result block of the run on the good chip.
CONSOLE: PINMUX 00000000
CONSOLE: GPIO IN 00000002
CONSOLE: flash 00c0ffee
uart_bytes=1 spi_frames=1 verdict=pass

Rule 4 at work in the first two lines. PINMUX reads 00000000 after reset. GPIO IN reads 00000002: only bit 1, the board's idle UART line. No pad is driven by the chip. Rule 2 in the rest: the board's UART model counted 1 byte, and the flash answered 00c0ffee.

Now the trap of the hook. The same tour with its PINMUX write removed still ends with the pass word. The UART wrote its byte into an empty pad:

The UART part of the tour, without its PINMUX write (the flash part removed).
without the PINMUX write: verdict=pass uart_bytes=0

A test that only checks its own pass word cannot see this. The byte must be checked where it arrives. Last, the tour on Q064, the chip whose SPI data input samples the clock pad. The walk of the previous lesson passed on it. The tour fails, and a passive monitor bound at the SPI block reports why:

The bench line and the end of the tour on chip Q064.
MONITOR spi: frame 0: 32 clocks, command 9f on the wire for 9f written, reply ffffff on the wire, 000000 stored
verdict=fail reason=checker

Rule 5 lives in the generated mux. Two pads can feed capture, and the generator wrote:

gen/rtl/ap_soc_pinmux.sv, generated from pins.csv.
assign capture = ((pinmux[3:2] == 2'd3) ? pad_in[1] : 1'b0)
|| ((pinmux[7:6] == 2'd3) ? pad_in[3] : 1'b0);

So capture on both IO1 and IO3 sees either pad's pulse. The graded problem covers the cells this tour does not: PWM, the event input and the wake pad.

Why It Is Built This Way

Why release every pad at reset, instead of starting with GPIO or UART? Because the board may already drive some pads. A chip that drove IO1 at reset would fight the board's UART line until firmware ran. Releasing everything is the safe default, at the price of one PINMUX write in every program.

Why test from firmware and not only with a proof? The proof of pins.csv checks the mux's wiring under each select. Firmware also checks the agreement between the header, the driver and the mux: that the code writes the field the hardware reads. That agreement is where the hook's silent UART came from.

Traps

The first trap is the silent pass. The firmware writes a byte, the UART takes it, and the test passes. Without a model at the pad, nobody notices that the pin mux was never set. Every output cell needs a check at the pad.

The second trap is a field in the wrong bits. Count pads from 1 instead of 0, and every select lands on a neighbour. The chip obeys, so nothing fails until a peripheral needs the pad it lost. Read PINMUX back against the value the table implies, right after you write it.

The third trap is two owners. Firmware gives capture to IO1 and later to IO3, and forgets the first. On ap_soc both pads now feed it. On another chip the first one might win. Read the pin table's conflict rule; do not guess it.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has sixteen pads and four selects; phone-class chips have hundreds of pads and more functions each.

  1. Pads with many selects

    Large chips give a pad eight or more functions and add pull, drive strength and slew settings per pad.

  2. Generated pin tables

    The mux, the header and the pad-ring checks are all generated from one pin table, as top-gen.mjs does here.

  3. Proof plus tests

    Teams prove the mux from the table and keep one firmware test per peripheral, as the bring-up programs.

Try It

Run it: IO0 and IO1 at select 2 make PINMUX 0x0000_000A.

Add the four SPI pads at select 2 (the predict question below). What is PINMUX now?

Put IO2 at select 3 instead. Which bits change?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: PINMUX = 0x0000000a IO0 select 2 IO1 select 2

Check Yourself

Predict the outputPads IO0 to IO5 are all set to select 2. What is the last line printed?
// PINMUX of ap_soc v1.5: two bits for each of sixteen pads; pad n uses bits 2n+1 and 2n.
module tb;
  logic [31:0] pinmux;
  initial begin
    pinmux = '0;                         // the reset value: every pad at select 0
    pinmux[2*0 +: 2] = 2'd2;          // IO0, select 2
    pinmux[2*1 +: 2] = 2'd2;          // IO1, select 2
    pinmux[2*2 +: 2] = 2'd2;          // IO2, select 2
    pinmux[2*3 +: 2] = 2'd2;          // IO3, select 2
    pinmux[2*4 +: 2] = 2'd2;          // IO4, select 2
    pinmux[2*5 +: 2] = 2'd2;          // IO5, select 2
    $display("PINMUX = 0x%08h", pinmux);
    for (int p = 0; p < 16; p++) if (pinmux[2*p +: 2] != 0) $display("IO%0d select %0d", p, pinmux[2*p +: 2]);
  end
endmodule
ConceptHow did the tour check the UART output?
ConceptWhat did GPIO IN read straight after reset in the tour?
ConceptWithout its PINMUX write, the UART part of the tour still passed. Why?
ConceptCapture is selected on IO1 and IO3 at once. What does it see on ap_soc?

Common Mistakes

Wrong Pass a UART test because the firmware reached its pass word.

Without the pin mux the byte never leaves the chip, and nothing in the firmware notices.

Fix Check every output at the pad: a board model or GPIO IN.

Wrong Write PINMUX and move on.

A field in the wrong bits changes another pad and leaves the intended one released.

Fix Read PINMUX back against the word the pin table implies.

Wrong Treat a GPIO walk as a test of the pin mux.

It exercises only select 1 of each pad.

Fix Select and check every cell of the pin table.

Remember

  • Each cell of the pin table is its own path; select it and check it at the pad.
  • GPIO IN reads every pad, so it can check outputs no board model watches.
  • ap_soc releases every pad at reset.
  • Two pads selecting capture feed it their OR; IO15 always reaches the wake detector.

Practice

Report a problem with this page