Peripherals such as a UART, a timer or GPIO need very little from a bus: one register read or write at a time, now and then. Giving each of them an AXI4-Lite port would cost logic for nothing, so SoCs put them on a simpler bus, APB, and connect that bus to the fast one through a bridge. This article explains APB's two-phase transfer, how the bridge turns one AXI4-Lite transaction into one APB transfer, how it finds the peripheral from the address, and what it answers when a peripheral reports an error or a slot is empty. You build the bridge in two exercises: S09 for the basic transfer, S10 for wait states and errors.
11 min read | Intermediate
What You'll Learn
- Describe an APB transfer: the SETUP and ACCESS phases, PREADY and PSLVERR
- Turn one AXI4-Lite read or write into one APB transfer and its response
- Select a peripheral from address bits and answer SLVERR for an empty slot
- Explain why a peripheral access takes six cycles in this SoC
Before You Start
This section needs Verilog: see the Verilog Tutorial. Buses and Transactions introduces APB and the idea of a bridge; An AXI4-Lite SRAM explains the AXI4-Lite subordinate side, which the bridge also has. No earlier exercise is needed.
Why a Second Bus
AXI4-Lite has five channels so that reads and writes, addresses and data, can move independently. A UART's register does not need any of that: it is read or written once in a while, one access at a time, and a few cycles more or less do not matter. APB, the Advanced Peripheral Bus, is built for exactly that: one set of shared signals, one transfer at a time, no separate response channel. Every peripheral on it needs only a handful of gates to decode its accesses.
The bridge connects the two worlds. To the interconnect it is an AXI4-Lite subordinate that owns the whole 64 KB peripheral window at 0x4000_0000. Towards the peripherals it is the APB manager: it drives the shared signals, selects one peripheral at a time, and waits for its answer.
An APB Transfer
APB has one set of signals shared by all peripherals (paddr, pwrite, pwdata, pstrb, penable) plus one select line per peripheral (psel) and three signals back from each (prdata, pready, pslverr). A transfer has two phases:
- SETUP, exactly one cycle: the selected peripheral's PSEL is 1, PENABLE is 0, and the address, direction and (for a write) the data and byte enables are on the bus.
- ACCESS, one cycle or more: PENABLE is 1, everything else stays the same. The transfer ends at the clock edge where the peripheral's PREADY is 1. A write takes effect then; for a read, PRDATA holds the data then. PSLVERR = 1 at that moment means the peripheral could not do it.
After the transfer, PENABLE goes back to 0, and PSEL too unless another transfer starts at once. A peripheral that is never slow simply holds PREADY at 1, and every transfer then takes exactly two cycles.
From AXI4-Lite to APB
The bridge does one transaction at a time, from start to finish:
- Accept an AXI4-Lite write when both its address and its data are there, or a read. Register what you need: the address, the data and byte enables, whether it is a write, and which peripheral it is for. The AXI4-Lite channels let go of their payload after the handshake, so the bridge keeps its own copy for the APB phases.
- SETUP for one cycle, then ACCESS until the selected peripheral raises PREADY. Take PRDATA and PSLVERR at that edge.
- Respond on the AXI4-Lite side: BVALID or RVALID, with SLVERR (10) if the peripheral reported PSLVERR and OKAY otherwise, and the read data. Hold it until the manager accepts it, then go back to step 1.
These are its ports. NP is the number of peripherals, and each per-peripheral signal is packed like the interconnect's: peripheral i uses psel[i], prdata[32*i +: 32], pready[i] and pslverr[i].
module cvsoc_axil2apb #(
parameter int NP = 4 // number of APB peripherals
) (
input logic clk,
input logic rst_n,
// AXI4-Lite subordinate port: s_awaddr, s_awvalid, s_awready, s_wdata, s_wstrb, s_wvalid, s_wready,
// s_bresp, s_bvalid, s_bready, s_araddr, s_arvalid, s_arready, s_rdata, s_rresp, s_rvalid, s_rready
// APB manager: shared signals, one PSEL per peripheral
input logic [4*NP-1:0] slot, // peripheral i owns the 4 KB window where address bits [15:12] = slot[4*i +: 4]
output logic [NP-1:0] psel,
output logic penable,
output logic pwrite,
output logic [11:0] paddr,
output logic [31:0] pwdata,
output logic [3:0] pstrb,
input logic [32*NP-1:0] prdata,
input logic [NP-1:0] pready,
input logic [NP-1:0] pslverr
);
Finding the Peripheral
The 64 KB peripheral window is divided into sixteen slots of 4 KB, chosen by address bits [15:12]. The input slot tells the bridge which slot each peripheral sits in: peripheral i is in slot slot[4*i +: 4]. In cvsoc the UART is in slot 0 (0x4000_0000), the timer in slot 1, GPIO in slot 2 and the simulation control block in slot F (0x4000_F000). PADDR is the offset inside the slot, address bits [11:0], so every peripheral sees its own registers at offsets 0x000, 0x004 and so on, wherever it sits in the map.
Slots 3 to E are empty. A transaction to one of them must still be answered, or the manager would wait forever. The bridge answers it with SLVERR after the SETUP cycle, without selecting any peripheral and with read data 0. That is why the program badaddr's read of 0x4000_3000 got SLVERR rather than DECERR: the interconnect saw an address inside the peripheral window and passed it on; it is the bridge that knows the slot is empty.
Quick Check
Q: A program stores to 0x4000_2004. Which PSEL rises, and what are PADDR and PWRITE during the transfer? What happens if the program stores to 0x4000_7004 instead?
Show Answer
Bits [15:12] of 0x4000_2004 are 2, the GPIO's slot, so GPIO's PSEL rises, with PADDR = 0x004 and PWRITE = 1 through SETUP and ACCESS. For 0x4000_7004, slot 7 has no peripheral: no PSEL rises at all and the bridge answers the write with SLVERR.
Six Cycles per Access
Here is a write to GPIO from the core's manager port, cycle by cycle, as the reference design does it:
The manager port needs a cycle to see the request and offers it in cycle 2, where the bridge accepts it. Cycles 3 and 4 are the APB transfer; GPIO answers at once, so ACCESS lasts one cycle and the register is written at the end of cycle 4. In cycle 5 the bridge offers the write response, and in cycle 6 the manager port raises done. Six cycles, two more than an SRAM access: exactly the two APB phases. A peripheral that holds PREADY low for a while would stretch ACCESS by that many cycles. Running Programs on an SoC showed what six cycles per access mean for a program that polls the UART.
Where Is This Used?
Almost every microcontroller and application processor has an APB segment for its slow peripherals, behind one or more bridges: timers, watchdogs, UARTs, I2C and SPI controllers, GPIO, the registers of the clock and reset controller. Keeping them off the main interconnect keeps that interconnect small and fast, and lets the peripheral segment run on a slower clock, which saves power. Buses, Crossbars and Networks-on-Chip shows where such segments sit in larger chips.
Two Exercises
S09, the basic transfer. Accept AXI4-Lite reads and writes, run SETUP and ACCESS, and answer. In this part the peripherals always answer in the first ACCESS cycle, never report errors, and every address is in an owned slot. The test has four peripheral models in slots 0, 1, 2 and F; a monitor checks the APB phases at every clock edge (only one PSEL, SETUP followed by ACCESS, signals stable, PENABLE back to 0 after the transfer), and each transaction checks that the right peripheral saw exactly one transfer with the right address, data and byte enables, and that the AXI response carries its data.
S10, wait states and errors. The same module, now with peripherals that hold PREADY low for a random number of cycles and answer PSLVERR at offsets 0xE00 to 0xEFF, and with transactions to empty slots, which must get SLVERR without any APB transfer. The S09 test runs again too. If you built S09 with these cases in mind, S10 may pass at once.
Common Mistakes
Mistake #1: PENABLE in the SETUP cycle
Raising PENABLE together with PSEL skips SETUP, which the protocol requires: peripherals use that cycle to decode the address and prepare, and a register written on PSEL and PENABLE, like the GPIO you write next, would be written one cycle early and again in the real ACCESS cycle. The monitor checks that the first cycle of every transfer has PENABLE = 0.
Mistake #2: Not waiting for PREADY
Ending ACCESS after one cycle works with peripherals that always answer at once and fails with any that take longer: the bridge takes PRDATA before it is valid, and answers the manager while the peripheral is still busy. S10's peripherals wait.
Mistake #3: Forwarding the AXI4-Lite address directly
Driving PADDR from s_awaddr or s_araddr instead of a registered copy works only while the manager holds its address, which it does not have to do after the handshake. Register the request when you accept it.
Your Turn in the Lab
Exercise S09: write cvsoc_axil2apb for the basic transfer. Then S10: add wait states, PSLVERR and empty slots. Save the S10 version in your lab project for S16.
Summary
- APB is a simple shared bus for slow peripherals: a transfer is one SETUP cycle (PSEL) and ACCESS cycles (PSEL and PENABLE) until PREADY.
- The bridge is an AXI4-Lite subordinate for the 64 KB peripheral window; it registers each accepted transaction, runs one APB transfer and answers with OKAY, or SLVERR when the peripheral raised PSLVERR.
- Address bits [15:12] choose one of sixteen 4 KB slots; PADDR is bits [11:0]; an empty slot gets SLVERR with no APB transfer.
- A peripheral access takes six cycles in
cvsoc: the four of an SRAM access plus SETUP and ACCESS.