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.
| Pad | Select 0 | Select 1 | Select 2 | Select 3 |
|---|---|---|---|---|
| IO0 | released | gpio (inout) | uart_tx (out) | pwm (out) |
| IO1 | released | gpio (inout) | uart_rx (in) | capture (in) |
| IO2 | released | gpio (inout) | spi_sclk (out) | pwm (out) |
| IO3 | released | gpio (inout) | spi_miso (in) | capture (in) |
| IO4 | released | gpio (inout) | spi_mosi (out) | event_in (in) |
| IO5 | released | gpio (inout) | spi_cs_n (out) | — |
| IO6 | released | gpio (inout) | — | — |
| IO7 | released | gpio (inout) | — | — |
| IO8 | released | gpio (inout) | — | — |
| IO9 | released | gpio (inout) | — | — |
| IO10 | released | gpio (inout) | — | — |
| IO11 | released | gpio (inout) | — | — |
| IO12 | released | gpio (inout) | — | — |
| IO13 | released | gpio (inout) | — | — |
| IO14 | released | gpio (inout) | — | — |
| IO15 | released | gpio (inout) | — | — |
| IO15, always | wake |
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.
| Question | |
|---|---|
| 1 | What does one cell of the pin table promise, and how do you test it? |
| 2 | How can firmware check an output function the board does not watch? |
| 3 | What state are the pads in after reset, and why? |
| 4 | What does ap_soc do when two pads select the same input? |
| 5 | Why 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.
- Peripheral
- Control / FSM
- Bus
- Test
- Data
- Control
- Choose
Firmware writes a pad's two bits of PINMUX; the mux connects one block signal to the pad.
- Drive or read
An output function drives the pad; an input function reads it.
- 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
| Word | Meaning |
|---|---|
| Pin mux | The logic that connects each pad to one of several block signals, chosen by a select. |
| Select | The value of a pad's two PINMUX bits: 0 released, 1 GPIO, 2 the serial function, 3 the auxiliary function. |
| Released | Select 0: the chip drives the pad with nothing, so the board decides its level. |
| Direction | Whether a function drives the pad (out) or reads it (in). |
| Loopback through GPIO IN | Reading an output function's level back through GPIO IN, which sees every pad whatever PINMUX says. |
| Input conflict | Two 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.
| Cells chosen | Bits written | PINMUX |
|---|---|---|
| IO0 select 2, IO1 select 2 | 1:0 = 10, 3:2 = 10 | 0x0000_000A |
| IO0 to IO5 select 2 | six fields of 10 | 0x0000_0AAA |
| IO2 select 3 only | 5:4 = 11 | 0x0000_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
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.
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.
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.
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.
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.
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.
#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;
}CONSOLE: PINMUX 00000000
CONSOLE: GPIO IN 00000002
CONSOLE: flash 00c0ffee
uart_bytes=1 spi_frames=1 verdict=passRule 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:
without the PINMUX write: verdict=pass uart_bytes=0A 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:
MONITOR spi: frame 0: 32 clocks, command 9f on the wire for 9f written, reply ffffff on the wire, 000000 stored
verdict=fail reason=checkerRule 5 lives in the generated mux. Two pads can feed capture, and the generator wrote:
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
This section looks ahead. ap_soc has sixteen pads and four selects; phone-class chips have hundreds of pads and more functions each.
Pads with many selects
Large chips give a pad eight or more functions and add pull, drive strength and slew settings per pad.
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.
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?
PINMUX = 0x0000000a IO0 select 2 IO1 select 2Check Yourself
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.