Pads and Pin Multiplexing

One pin must choose the boot mode at reset and carry console data a moment later. A chip has far more signals than pins, so every pin has several jobs and one owner at a time.

You Will Be Able To

  • Name the two things a function sends to a pad, and say what a released pad is.
  • Read a function table, and split a register value into the select field of each pad.
  • Explain the idle level of a receiver, and how a strap is saved at reset.
Lesson
S7.4
Time
26 min
Runs on
Your browser
Check
6 questions

Your board boots correctly on the bench. Plug in the console cable, and it boots into the wrong mode. The boot jumper and the console share one pin. Who was driving it when the chip looked?

Before You Start

You need GPIO: an output driver, its output enable, and a released pin. You need The Anatomy of a Peripheral for pad signals.

The functions on this page are wires you have met. TX and RX belong to the UART. The serial clock belongs to SPI. PWM and capture belong to the timer.

Five questions this page answers
Question
1Which two things does a function send to a pad?
2What does z mean on a pin?
3What does the pin multiplexer choose, and where is the choice kept?
4A receiver loses its pin to another function. What does it hear?
5How can one pin set the boot mode and carry data afterwards?

The Idea: Many Signals, Few Pins, One Owner at a Time

A package has a fixed number of pins, and pins cost money. The devices inside the chip have far more signals than that. So most pins are shared. A board that needs a console uses a pin for the UART. Another board uses the same pin for a lamp.

Sharing is safe only if each pin has one owner at a time. A selector in front of each pad picks the owner. It must pass on two things from that owner: the level to drive, and whether to drive at all. A device that only listens must be able to switch the driver off.

A pin with its driver off is not at 0 and not at 1. The chip puts nothing on it. That state is called high impedance. A simulator prints it as the letter z. The board decides the level then: another chip, a resistor, or nothing at all.

Two smaller ideas follow. A device that has lost its pin must still hear something sensible, so it is given a fixed quiet level. And a pin can do one job at reset and another afterwards, if the chip saves what it saw at reset. A strap pin works that way. The board holds it at a level during reset, to tell the chip a choice such as the boot mode.

New Words

New words on this page
WordMeaning
High impedanceThe state of a wire that nothing drives. A simulator prints it as z. It is neither 0 nor 1.
Pad receiverThe part of a pad that senses the level of the pin and passes it into the chip.
Function of a padOne of the internal signals that a pad can serve: a GPIO bit, UART TX, a timer output.
Pin multiplexerThe selectors that connect each pad to one of its functions. Pin mux for short.
Select fieldThe bits of a register that choose the function of one pad.
Idle levelThe level a receiver is given while it is not connected to a pin. It is the level that means "nothing is happening".
Pull-down resistorA resistor from a wire to ground. It makes the wire 0 when nothing drives it.
The function table of the four pads in the lab
PadCode 0Code 1Code 2Code 3
0releasedGPIO0UART TXPWM
1releasedGPIO1UART RXCapture
2releasedGPIO2SPI clockPWM
3releasedGPIO3SPI inputCapture

The register is called PINMUX. It is at address 0x10004010, in the system control block. Bits 1 and 0 are the select field of pad 0, bits 3 and 2 of pad 1, and so on. Reset value 0. The table is our own choice for a four-pad slice of ap_soc.

One Picture: A Pad and Its Selectors

One Pad and Its Pin MultiplexerStep through the picture. It starts at the pin and ends at the register that chooses.
  • Pads
  • Control / FSM
  • Memory
  • Peripheral
One Pad and Its Pin MultiplexerStep through the picture. It starts at the pin and ends at the register that chooses. component Pin the metal contact onthe package component Pad driver drives the pin, or letsit go component Pad receiver senses the level ofthe pin component Output selector picks one function:value and enable component Return selector pin level, or an idlelevel component Select field two bits for each pad,in PINMUX component Functions GPIO, a serial device,a timer drivelevelvalue and outputenableof the chosenfunctionpin levelto each receiverchooseschooses
  1. component Pinthe metal contact on the package
    • level → Pad receiver
  2. component Pad driverdrives the pin, or lets it go
    • drive → Pin
  3. component Pad receiversenses the level of the pin
    • pin level → Return selector
  4. component FunctionsGPIO, a serial device, a timer
    • value and output enable → Output selector
  5. component Output selectorpicks one function: value and enable
    • of the chosen function → Pad driver
  6. component Return selectorpin level, or an idle level
    • to each receiver → Functions
  7. component Select fieldtwo bits for each pad, in PINMUX
    • chooses → Output selector
    • chooses → Return selector
  1. The pad

    A pad stands between the chip and one pin. Its driver puts a level on the pin while its output enable is 1. Its receiver senses the pin.

  2. More functions than pins

    Several devices want this pin: a GPIO bit, a serial device, a timer. Each is called a function of the pad. Only one may own it.

  3. The way out

    An output selector picks one function. It passes on two things together: the value to drive, and the output enable.

  4. The way back

    A return selector gives the pin level to the one function that owns the pad. Every other receiver gets a fixed, quiet level.

  5. The select field

    Two bits for each pad make the choice. They live in a register, so software can change the owner of a pin.

Read the diagram as text
  • Pin: the metal contact on the package
  • Pad driver: drives the pin, or lets it go
  • Pad receiver: senses the level of the pin
  • Output selector: picks one function: value and enable
  • Return selector: pin level, or an idle level
  • Select field: two bits for each pad, in PINMUX
  • Functions: GPIO, a serial device, a timer
  • Pad driver → Pin: drive
  • Pin → Pad receiver: level
  • Functions → Output selector: value and output enable
  • Output selector → Pad driver: of the chosen function
  • Pad receiver → Return selector: pin level
  • Return selector → Functions: to each receiver
  • Select field → Output selector: chooses
  • Select field → Return selector: chooses

One pad of four in the lab. The output selector and the return selector together are the pin multiplexer.

The Smallest Example, Worked by Hand

Take pad 0 alone. Its three functions offer these values at this moment. The GPIO bit offers 0, with its output enable at 1. UART TX offers 0, and PWM offers 1. The board does not drive the pin.

Pad 0 under each code, by hand
Select fieldOwnerOutput enablePin
00nobody0z
01GPIO bit 010
10UART TX10
11PWM11

The first row is the released pad: output enable 0 and a z on the pin. In the other rows the level and the enable both come from the same owner. Rows 2 and 3 show the same level, 0, from two different owners. The level alone does not say who drives.

Now a whole register value. Software writes aa in hexadecimal to PINMUX. Split it into pairs of bits, from the right: one pair for each pad.

The value aa, split into select fields
Pad 3Pad 2Pad 1Pad 0
Bits of aa10101010
Code2222
Function, from the tableSPI inputSPI clockUART RXUART TX
Does the pad drive?noyesnoyes

Two of the four functions are inputs. Their pads must not drive, so the output enables are 0101: pads 2 and 0 only. That one word of four bits is the first thing to check after any write to PINMUX.

Last, the way back. Pad 1 serves UART RX under code 2. Under any other code the UART is cut off from the pin. It then hears a fixed 1, the level of a resting serial wire, whatever the pin does.

The Rules

  1. Rule 1: A pad drives only while its output enable is 1.

    With the enable at 0 the pad is released: high impedance, printed as z when nothing else drives the pin.

  2. Rule 2: The select field picks one owner for the pad.

    The value to drive and the output enable both come from that owner.

  3. Rule 3: Code 0 releases the pad, and reset sets every field to 0.

    A chip that leaves reset drives none of these pins until software chooses.

  4. Rule 4: A receiver without a pin gets its idle level.

    UART RX gets 1. The SPI input and the capture inputs get 0.

  5. Rule 5: A strap is sampled once, at the end of reset.

    The chip saves the level in a register. After that the pin is free for another job.

  6. Rule 6: A level on a pin does not say who drives it.

    A board resistor, another chip or our own driver can all make a 1. Look at the output enable.

Sources for These Rules

Pad drivers, input receivers and the package: Weste and Harris, CMOS VLSI Design, fourth edition, chapter 13, sections 13.2.2 and 13.6.1.

High impedance, nets with several drivers, and pull sources in a simulator: IEEE Std 1800-2023, clauses 6.3, 6.6 and 28.10.

The edge on which the write to PINMUX counts: Arm AMBA APB Protocol Specification, IHI 0024 issue E, sections 3.1 and 4.1.

Rules 2 to 5 describe our own pad slice. The four-row table, code 0 as "released", the reset value, the idle levels and the single strap sample are our own choices.

Real pads have more settings: pull resistors, drive strength, an input enable. This page leaves them out.

Now Run It

The lab at the end of this page has the four pads and their pin multiplexer, behind the bridge to APB. Test code, a bus-functional model, stands in for the processor and plays the board. The board holds pad 0 high during reset, has a pull-down resistor on pad 1, and leaves pads 2 and 3 alone.

Printed by the lab on Icarus. The four bits of oe and pad are pads 3, 2, 1 and 0, from left to right.
reset oe=0000 pad=zz01 rx=1 strap=0
released oe=0000 pad=zz0z rx=1 strap=1
aux oe=0101 capture=1000
serial oe=0101 pad=z100 rx=0 strap=1
gpio oe=1001 pad=1z00 rx=1 strap=1
mux readback=55

Rule 1 at work: the row "reset" has oe=0000 and pad=zz01. Pads 3 and 2 are z: nobody drives them. Pad 1 is 0 from its pull-down resistor. Pad 0 is 1 from the board. Rule 5 at work: in "released" the board has let pad 0 go, and strap is 1.

Rule 2 at work: after the write of aa the row "serial" has oe=0101, the word of the hand table. UART TX drives a 0 on pad 0 and the SPI clock a 1 on pad 2. Pad 3 is an input with nothing on it, so it is z.

Rule 4 at work: compare rx in two rows. In "serial" pad 1 feeds UART RX, and rx=0, the pulled-down pin. In "gpio" the pad has another owner, and rx=1, although the pin is still 0.

Rule 6 at work: in "gpio" pad 0 is 0 and in "serial" it was 0 as well. The owner changed from UART TX to a GPIO bit, and the pin did not show it.

This lab leaves things out. The levels are ideal: no voltage, no delay. A real pad has pull resistors and a drive strength of its own. The strap is sampled by a pulse from the test code, not by a reset controller.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Several functions for each pinOne pin for each signalA small, cheap package. Not every set of functions can be used at once.
The enable travels with the valueOne selector for the value onlyAn input function switches the driver off. Each pad needs a second selector.
Every pad released after resetA default function on each padThe chip cannot fight the board before software runs. Software must set every pin up.
An idle level for a cut-off receiverLeave it on the pin, or at 0A device hears nothing false. The right level depends on the device.
The strap saved in a registerRead the pin whenever the boot mode is neededThe pin is free after reset. The board must hold it steady until the sample.

The Trap: A Frame Nobody Sent, and a Boot Mode Read Too Late

Start with rule 4 broken. A designer gives every cut-off receiver a 0, because 0 feels quiet. The UART is cut off from its pin for a moment while software sets the pins up. Its receive input falls to 0. To a UART a falling wire is a start bit. It gathers eight bits of nothing and hands software a byte that nobody sent.

The idle level must come from the device, not from habit. A serial wire rests at 1. A capture input rests at 0, because its event is a rise. That is why the lab gives rx a 1 and capture a 0.

Now the board that boots wrongly with the console cable. The boot code read the boot mode from the live pin, late, after the pin had been given to the UART. With the cable in, the level on that pin was the console. Rule 5 is the cure: sample the strap once at the end of reset, and read the saved bit ever after.

In the lab the saved bit stays 1 through every row after "released". UART TX and then a GPIO bit drove pad 0 to 0, and the strap did not move.

At Scale

This page4 pads, 4 codes each
A phone chiphundreds of pads, 8 or more functions each

This section looks ahead. A large chip has hundreds of pads, and the function table is a document that many teams share.

  1. The table is generated

    Nobody writes hundreds of selectors by hand. A table like the one on this page is the source, and a tool writes the hardware from it.

  2. The table is proved

    A formal tool can check that every pad has at most one driver. It can also check that every function reaches its pad. See the formal app map and connectivity checking.

  3. More settings for each pad

    Pull resistors, drive strength and an input enable are register fields too, next to the select field.

  4. Pins that stay awake

    Some pads keep their state while the rest of the chip sleeps, so that a button can wake it.

Try It

Run the lab. In each row the four bits are pads 3, 2, 1 and 0, from left to right.

Change 32'h55 to 32'h00 and show("gpio") to show("off"). Predict oe, pad and rx before you run it.

Change the write of aa to a6. Which pad now has another function? Predict the row "serial".

Names Printed by the Lab
NameMeaning
oeThe output enables of the four pad drivers.
padThe levels of the four pins. A z is a pin that nobody drives.
rxWhat the receive input of UART0 hears.
strapThe saved boot choice: pad 0 as it was at the end of reset.
captureWhat the capture inputs of the timer hear, one for each pad.
reset, releasedDuring reset, and after the board has let pad 0 go.
aux, serial, gpioAfter PINMUX was written with ff, aa and 55.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: reset oe=0000 pad=zz01 rx=1 strap=0 released oe=0000 pad=zz0z rx=1 strap=1 aux oe=0101 capture=1000 serial oe=0101 pad=z100 rx=0 strap=1 gpio oe=1001 pad=1z00 rx=1 strap=1 mux readback=55
Read included file: ahb_apb_bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
  input  logic        hclk,
  input  logic        hresetn,
  input  logic        hsel,
  input  logic [31:0] haddr,
  input  logic [1:0]  htrans,
  input  logic        hwrite,
  input  logic [3:0]  hprot,
  input  logic [31:0] hwdata,
  input  logic [3:0]  hwstrb,
  input  logic        hready,
  output logic        hreadyout,
  output logic        hresp,
  output logic [31:0] hrdata,
  output logic        psel,
  output logic        penable,
  output logic        pwrite,
  output logic [31:0] paddr,
  output logic [31:0] pwdata,
  output logic [3:0]  pstrb,
  output logic [2:0]  pprot,
  input  logic        pready,
  input  logic [31:0] prdata,
  input  logic        pslverr);
  localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
  bit trace = 0;
  int st = IDLE;
  logic        wr = 0;
  logic [31:0] a = 0;
  logic [2:0]  prot = 0;
  wire take = hsel && hready && htrans[1];
  assign psel      = (st == SETUP || st == ACCESS);
  assign penable   = (st == ACCESS);
  assign pwrite    = wr;
  assign paddr     = {a[31:2], 2'b00};     // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
  assign pwdata    = wr ? hwdata : 32'h0;
  assign pstrb     = wr ? hwstrb : 4'h0;
  assign pprot     = prot;
  assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
  assign hresp     = (st == ERR1 || st == ERR2);
  assign hrdata    = (st == ACCESS) ? prdata : 32'h0;
  always @(posedge hclk or negedge hresetn) begin
    if (!hresetn) st <= IDLE;
    else begin
      case (st)
        SETUP:  st <= ACCESS;
        ACCESS: if (pready === 1'b1) begin
                  if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
                  st <= (pslverr === 1'b1) ? ERR1 : IDLE;
                end
        ERR1:   st <= ERR2;
        default: st <= IDLE;
      endcase
      // A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
      // or at the edge an access completes with OKAY.
      if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
        wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
      end
    end
  end
endmodule
Read included file: bus.svh
// One aligned word at a time, using the P4 bridge. Include inside tb.
logic hclk = 0, hresetn = 0, hwrite = 0;
logic [31:0] haddr = 0, hwdata = 0, result;
logic [1:0] htrans = 0;
wire hsel = haddr[31:16] == 16'h1000;
wire [31:0] hrdata, paddr, pwdata, prdata;
wire hready, hresp, psel, penable, pwrite;
bit trace_on = 0;

always #5 hclk = !hclk;

ahb_apb_bridge bridge(
  .hclk, .hresetn, .haddr, .hsel, .hwdata, .htrans, .hwrite,
  .hprot(4'b0011), .hwstrb(4'hf), .hready, .hreadyout(hready), .hresp, .hrdata,
  .paddr, .pwdata, .pwrite, .psel, .penable, .pready(1'b1), .prdata, .pslverr(1'b0)
);

always @(posedge hclk) begin
  if (trace_on)
    $display("bus req=%b sel=%b enable=%b ready=%b", htrans[1], psel, penable, hready);
end

task automatic access_word(input bit writing, input logic [31:0] address,
                           input logic [31:0] data, output logic [31:0] result);
  if (address[31:16] != 16'h1000 || address[1:0] != 0)
    $fatal(1, "This lab models aligned words in the APB window");
  @(negedge hclk);
  haddr = address;
  hwrite = writing;
  htrans = 2'b10;
  @(negedge hclk); // The address was accepted on the intervening rising edge.
  htrans = 0;
  haddr = 0;
  hwdata = data;
  do @(posedge hclk); while (!hready);
  if (hresp) $fatal(1, "Access failed at %08h", address);
  result = hrdata;
  #1;
endtask
Read included file: pads.sv
// Each 12-bit row holds four three-bit endpoint numbers, indexed by function select.
// The board table is an elaboration parameter. An empty table releases every pad.
module pad_slice #(parameter logic [47:0] ROUTES = 0) (input wire clk,reset_n,psel,penable,pwrite,sample,
  input wire [31:0] paddr,pwdata, output wire [31:0] prdata,
  input wire [3:0] gpio_data,gpio_oe, input wire uart_tx,pwm,spi_clk,
  inout wire [3:0] pad, output logic strap, output wire uart_rx,spi_in,
  output wire [3:0] capture,oe);
  localparam MUX = 32'h10004010;
  logic [7:0] sel;
  wire [3:0] rx_lines, spi_lines;
  assign prdata = paddr == MUX ? {24'b0,sel} : 0;
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      sel <= 0;
      strap <= 0;
    end else begin
      if (sample) strap <= pad[0];
      if (psel && penable && pwrite && paddr == MUX) sel <= pwdata[7:0];
    end
  end
  localparam GPIO=1, UART_TX=2, UART_RX=3, SCLK=4, MISO=5, PWM=6, CAPTURE=7;
  for (genvar p=0; p<4; p++) begin : drivers
    localparam logic [11:0] ROW = ROUTES[12*p +: 12];
    wire [1:0] field = sel[2*p +: 2];
    wire [2:0] endpoint = ROW[3*field +: 3];
    logic data, drive;
    always_comb begin
      case (endpoint)
        GPIO:    {drive,data} = {gpio_oe[p],gpio_data[p]};
        UART_TX: {drive,data} = {1'b1,uart_tx};
        SCLK:    {drive,data} = {1'b1,spi_clk};
        PWM:     {drive,data} = {1'b1,pwm};
        default: {drive,data} = 2'b00;
      endcase
    end
    assign oe[p] = drive;
    assign pad[p] = drive ? data : 1'bz;
    assign rx_lines[p] = endpoint == UART_RX ? pad[p] : 1'b1;
    assign spi_lines[p] = endpoint == MISO ? pad[p] : 1'b0;
    assign capture[p] = endpoint == CAPTURE ? pad[p] : 1'b0;
  end
  // This table assigns at most one source each to UART RX and SPI input.
  assign uart_rx = &rx_lines;
  assign spi_in = |spi_lines;
endmodule

Check Yourself

Predict the outputIn this run the last write to PINMUX is 00. What is the last row the lab prints?
`timescale 1ns/1ns
module tb;
  `include "bus.svh"
  localparam MUX=32'h10004010;
  tri [3:0] pad;
  bit board_drive=1, capture_pulse=0;
  assign pad[3]=capture_pulse ? 1'b1 : 1'bz;
  assign pad[0]=board_drive ? 1'b1 : 1'bz;
  pulldown(pad[1]);
  wire [3:0] oe, capture;
  wire strap,uart_rx,spi_in;
  bit sample=0;
  pad_slice #(.ROUTES(48'hf48d08ec8c88)) pins(.clk(hclk),.reset_n(hresetn),.psel,.penable,.pwrite,.paddr,.pwdata,
    .prdata,.pad,.sample,.strap,.uart_rx,.spi_in,.capture,.oe,
    .gpio_data(4'b1010),.gpio_oe(4'b1001),.uart_tx(1'b0),.pwm(1'b1),.spi_clk(1'b1));
  task automatic show(input string label);
    $display("%s oe=%b pad=%b rx=%b strap=%b",label,oe,pad,uart_rx,strap);
  endtask
  initial begin
    repeat (2) @(negedge hclk);
    show("reset");
    hresetn=1;
    sample=1;                        // the end of reset: the chip samples pad 0 once, as the strap
    @(negedge hclk);
    sample=0;
    board_drive=0;                   // the board lets pad 0 go
    #1; show("released");
    access_word(1,MUX,32'hff,result);// PINMUX = ff: code 3 for every pad
    capture_pulse=1;                 // the board sends a pulse into pad 3
    #1; $display("aux oe=%b capture=%b",oe,capture);
    capture_pulse=0;
    access_word(1,MUX,32'haa,result);// PINMUX = aa: code 2 for every pad
    show("serial");
    access_word(1,MUX,32'h00,result);    // PINMUX = 00: code 0 for every pad
    show("off");
    $finish;
  end
  initial #50000 $fatal(1,"Transaction timed out");
endmodule
Read included file: ahb_apb_bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
  input  logic        hclk,
  input  logic        hresetn,
  input  logic        hsel,
  input  logic [31:0] haddr,
  input  logic [1:0]  htrans,
  input  logic        hwrite,
  input  logic [3:0]  hprot,
  input  logic [31:0] hwdata,
  input  logic [3:0]  hwstrb,
  input  logic        hready,
  output logic        hreadyout,
  output logic        hresp,
  output logic [31:0] hrdata,
  output logic        psel,
  output logic        penable,
  output logic        pwrite,
  output logic [31:0] paddr,
  output logic [31:0] pwdata,
  output logic [3:0]  pstrb,
  output logic [2:0]  pprot,
  input  logic        pready,
  input  logic [31:0] prdata,
  input  logic        pslverr);
  localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
  bit trace = 0;
  int st = IDLE;
  logic        wr = 0;
  logic [31:0] a = 0;
  logic [2:0]  prot = 0;
  wire take = hsel && hready && htrans[1];
  assign psel      = (st == SETUP || st == ACCESS);
  assign penable   = (st == ACCESS);
  assign pwrite    = wr;
  assign paddr     = {a[31:2], 2'b00};     // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
  assign pwdata    = wr ? hwdata : 32'h0;
  assign pstrb     = wr ? hwstrb : 4'h0;
  assign pprot     = prot;
  assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
  assign hresp     = (st == ERR1 || st == ERR2);
  assign hrdata    = (st == ACCESS) ? prdata : 32'h0;
  always @(posedge hclk or negedge hresetn) begin
    if (!hresetn) st <= IDLE;
    else begin
      case (st)
        SETUP:  st <= ACCESS;
        ACCESS: if (pready === 1'b1) begin
                  if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
                  st <= (pslverr === 1'b1) ? ERR1 : IDLE;
                end
        ERR1:   st <= ERR2;
        default: st <= IDLE;
      endcase
      // A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
      // or at the edge an access completes with OKAY.
      if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
        wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
      end
    end
  end
endmodule
Read included file: bus.svh
// One aligned word at a time, using the P4 bridge. Include inside tb.
logic hclk = 0, hresetn = 0, hwrite = 0;
logic [31:0] haddr = 0, hwdata = 0, result;
logic [1:0] htrans = 0;
wire hsel = haddr[31:16] == 16'h1000;
wire [31:0] hrdata, paddr, pwdata, prdata;
wire hready, hresp, psel, penable, pwrite;
bit trace_on = 0;

always #5 hclk = !hclk;

ahb_apb_bridge bridge(
  .hclk, .hresetn, .haddr, .hsel, .hwdata, .htrans, .hwrite,
  .hprot(4'b0011), .hwstrb(4'hf), .hready, .hreadyout(hready), .hresp, .hrdata,
  .paddr, .pwdata, .pwrite, .psel, .penable, .pready(1'b1), .prdata, .pslverr(1'b0)
);

always @(posedge hclk) begin
  if (trace_on)
    $display("bus req=%b sel=%b enable=%b ready=%b", htrans[1], psel, penable, hready);
end

task automatic access_word(input bit writing, input logic [31:0] address,
                           input logic [31:0] data, output logic [31:0] result);
  if (address[31:16] != 16'h1000 || address[1:0] != 0)
    $fatal(1, "This lab models aligned words in the APB window");
  @(negedge hclk);
  haddr = address;
  hwrite = writing;
  htrans = 2'b10;
  @(negedge hclk); // The address was accepted on the intervening rising edge.
  htrans = 0;
  haddr = 0;
  hwdata = data;
  do @(posedge hclk); while (!hready);
  if (hresp) $fatal(1, "Access failed at %08h", address);
  result = hrdata;
  #1;
endtask
Read included file: pads.sv
// Each 12-bit row holds four three-bit endpoint numbers, indexed by function select.
// The board table is an elaboration parameter. An empty table releases every pad.
module pad_slice #(parameter logic [47:0] ROUTES = 0) (input wire clk,reset_n,psel,penable,pwrite,sample,
  input wire [31:0] paddr,pwdata, output wire [31:0] prdata,
  input wire [3:0] gpio_data,gpio_oe, input wire uart_tx,pwm,spi_clk,
  inout wire [3:0] pad, output logic strap, output wire uart_rx,spi_in,
  output wire [3:0] capture,oe);
  localparam MUX = 32'h10004010;
  logic [7:0] sel;
  wire [3:0] rx_lines, spi_lines;
  assign prdata = paddr == MUX ? {24'b0,sel} : 0;
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      sel <= 0;
      strap <= 0;
    end else begin
      if (sample) strap <= pad[0];
      if (psel && penable && pwrite && paddr == MUX) sel <= pwdata[7:0];
    end
  end
  localparam GPIO=1, UART_TX=2, UART_RX=3, SCLK=4, MISO=5, PWM=6, CAPTURE=7;
  for (genvar p=0; p<4; p++) begin : drivers
    localparam logic [11:0] ROW = ROUTES[12*p +: 12];
    wire [1:0] field = sel[2*p +: 2];
    wire [2:0] endpoint = ROW[3*field +: 3];
    logic data, drive;
    always_comb begin
      case (endpoint)
        GPIO:    {drive,data} = {gpio_oe[p],gpio_data[p]};
        UART_TX: {drive,data} = {1'b1,uart_tx};
        SCLK:    {drive,data} = {1'b1,spi_clk};
        PWM:     {drive,data} = {1'b1,pwm};
        default: {drive,data} = 2'b00;
      endcase
    end
    assign oe[p] = drive;
    assign pad[p] = drive ? data : 1'bz;
    assign rx_lines[p] = endpoint == UART_RX ? pad[p] : 1'b1;
    assign spi_lines[p] = endpoint == MISO ? pad[p] : 1'b0;
    assign capture[p] = endpoint == CAPTURE ? pad[p] : 1'b0;
  end
  // This table assigns at most one source each to UART RX and SPI input.
  assign uart_rx = &rx_lines;
  assign spi_in = |spi_lines;
endmodule
ConceptWhich two things does the owner of a pad send to it?
ConceptWhat does z mean on a pin in the lab?
ConceptWhat chooses the function of a pad, and where is the choice kept?
ConceptPad 1 is given to GPIO. What does the receive input of the UART hear?
ConceptHow can one pin set the boot mode and carry data afterwards?

Common Mistakes

Wrong Take a high pin as proof that the chip drives it.

A board resistor or another chip can make the same level.

Fix Read the output enable beside the pin level.

Wrong Give every cut-off receiver a 0.

To a UART a low wire is the start of a byte.

Fix Give each receiver the idle level of its own device.

Wrong Read the boot mode from the live pin.

After reset the pin has another owner.

Fix Sample the strap once at the end of reset, and read the saved bit.

Remember

  • The owner of a pad sends it a value and an output enable. A released pad is high impedance, printed as z.
  • A select field for each pad picks the owner. Code 0 releases the pad, and reset selects code 0.
  • A receiver that has lost its pin hears its idle level: 1 for a UART.
  • A strap is sampled once at the end of reset and saved, so the pin can do another job afterwards.

Practice

Report a problem with this page