The Anatomy of a Peripheral
A sensor fires one short pulse, and its wire is quiet again. Software asks about it long afterwards. Something inside the chip had to keep the pulse. This page finds what.
You Will Be Able To
- Name the five parts of a peripheral, and say what each one does.
- Follow one write from the bus into a saved setting, and one event from a pin into a count.
- Explain why one event can raise two requests, and why each has its own clear.
- Lesson
- S7.1
- Time
- 24 min
- Runs on
- Your browser
- Check
- 6 questions
You tap the sensor once. The handler runs and clears its flag. Yet another request from the same device is still high. Did the chip count one tap twice?
Before You Start
You need S0.2: a device is reached through registers at addresses. You need S0.4: one APB write has a setup edge and an access edge.
You need S0.8 for what an interrupt is. S1.1 shows where the peripherals sit on the chip.
Read this page before the module on clocks, resets and power. That module takes the parts of a peripheral for granted.
| Question | |
|---|---|
| 1 | What are the five parts of a peripheral? |
| 2 | What keeps a setting after the bus has moved on? |
| 3 | Which part does the work, and when does it act? |
| 4 | Why can one event raise two requests, and what clears each? |
| 5 | A pulse reaches the pin and the count does not move. What can explain it? |
The Idea: Every Device Has the Same Five Parts
A wire tells you what is happening now. It cannot tell you what happened a moment ago. So a device that must answer "did a pulse come?" needs a circuit that remembers, and a way for software to ask.
Every device on the chip solves this in the same way. It has a part that talks to the bus, and registers that hold what software wrote. In the middle sits the logic that does the work. Wires go out to the pins. And it has a way to call for attention when something has happened.
Learn those five parts once, and every later device is a variation. A serial port shifts bits in the middle. A timer counts. A bank of pins drives and samples. The parts around the middle stay the same.
Without this picture, a new device is a list of register names. With it, you can ask three things of any device. Where does the setting live? What uses it? Who is told when the work is done?
New Words
| Word | Meaning |
|---|---|
| Bus interface | The part of a device that takes bus transfers and decides on which edge a write counts. |
| Register block of a device | The registers of one device: settings that software writes, and status that software reads. |
| Core logic | The part of a device that does its work. It acts on clock edges, with or without a bus transfer. |
| Arming a device | Switching a device on for an event, by writing a setting. A device that is not armed ignores the event. |
| Pending bit of a device | A flip-flop that an event sets. It stays 1 until someone clears it. It says that work waits, not how much. |
| DMA request | DMA is short for direct memory access: a block that copies data between a device and memory by itself. A DMA request is a wire from a device to that block. It says that data waits to be moved. |
| Acknowledge | The answer to a request: "I have dealt with it". It lets the device drop the request. |
One Picture: From the Bus to the Pin
- Bus
- Memory
- Peripheral
- Processor
- Pads
- Clock / reset
- The bus interface
Software reaches the device only through the bus. This part decides whether a transfer is for this device, and on which edge it counts.
- The register block
A write that counts changes a register. The register holds the setting after the bus has moved on. A read returns status.
- The core logic
This part does the work: it counts, shifts or compares. It reads the settings every clock. It needs no bus transfer to act.
- The pad signals
Most devices touch the world outside the chip. Their signals go to pins, and come from pins, through pads.
- Two ways to ask for service
When something has happened, the device raises a request. One wire asks software. Another can ask a block that copies data by itself.
- Clock and reset
Nothing above moves without a clock. Reset puts every register in a known state. A device with a stopped clock sees nothing.
Read the diagram as text
- Bus interface: takes one APB transfer at a time
- Register block: settings in, status out
- Core logic: does the work of the device
- Interrupt request: asks software for service
- DMA request: asks a copying block for service
- Pad signals: to and from the pins
- Clock and reset: every flip-flop inside needs both
- Bus interface → Register block: read and write
- Register block → Core logic: settings
- Core logic → Register block: status
- Core logic → Interrupt request: something happened
- Core logic → DMA request: data waits
- Pad signals → Core logic: a signal from outside
- Clock and reset → Register block: clock, reset
Every peripheral of ap_soc has these parts. What differs from device to device is the core logic in the middle.
The Smallest Example, Worked by Hand
Take the smallest device that has all five parts: a counter of pulses. It has one setting, the arm bit. Its core logic adds 1 to a count when a pulse arrives and the arm bit is 1. The same event sets two pending bits: one drives the interrupt request, one the DMA request.
| Offset | Register | What software does with it |
|---|---|---|
| 0x00 | Control | Writes 1 to bit 0 to arm the counter. |
| 0x04 | Count | Reads the number of pulses counted. |
| 0x08 | Status | Writes 1 to bit 0 to clear the pending bit of the interrupt. |
These three registers belong to this page alone. In the lab the counter sits at address 0x10002000, the place of the timer of ap_soc. The timer itself has other registers.
Now follow four moments. Fill in each row before you read it.
| Moment | What happens | Arm bit | Count | Interrupt request | DMA request |
|---|---|---|---|---|---|
| 1 | Software writes 1 to Control. | 1 | 0 | 0 | 0 |
| 2 | One pulse arrives. | 1 | 1 | 1 | 1 |
| 3 | Software writes 1 to Status. | 1 | 1 | 0 | 1 |
| 4 | The DMA block acknowledges. | 1 | 1 | 0 | 0 |
Moment 2 is one event with two results. Moment 3 is the row to remember. Software has finished, and the DMA request is still 1. It is not a second pulse. It is a second listener that has not been served yet.
The count never goes down. Clearing a request does not take a pulse away. The count says how many events came. A pending bit says only that some did.
The Rules
Rule 1: A write counts on one edge only.
It is the edge at which select, enable and ready are all 1. Before that edge the setting has not changed.
Rule 2: A register keeps the setting after the bus has moved on.
The core logic reads the register every clock. It does not need the address or the data any more.
Rule 3: The core logic acts on clock edges, not on bus transfers.
An event can arrive while the bus is idle, or busy with another device.
Rule 4: Each request has its own listener and its own clear.
Software clears the interrupt. The DMA block acknowledges its own request. Neither clears the other.
Rule 5: A pending bit is not a count.
Two events before service leave one bit at 1. When the number matters, read a counter.
Rule 6: No clock, no event.
A flip-flop samples only on a clock edge. A pulse that comes while the clock is stopped leaves no trace.
Sources for These Rules
Memory-mapped devices, their registers and their interfaces: Harris and Harris, Digital Design and Computer Architecture, ARM edition, chapter 9, sections 9.2 and 9.3.
The edge on which an APB write counts, and the reset of APB: Arm AMBA APB Protocol Specification, IHI 0024 issue E, sections 2.1, 3.1 and 4.1.
Rules 2 to 6 describe our own pulse counter. Its three registers, its two requests and their separate clears are our own choices for this page.
Real devices differ in how a request is cleared. Rule 4 is the common case, and the device description is the authority.
Now Run It
The lab at the end of this page is the pulse counter on APB. Test code, a bus-functional model, stands in for the processor: it drives the bus the way a processor would. It also plays the sensor and the DMA block.
Two rising-edge samples from the lab. The second edge completes this write.
Rule 1 at work: at the first edge of the picture PWDATA already holds 1 and PREADY is 1. But PENABLE is 0. The write does not count yet. At the second edge all three are 1. That edge sets the arm bit.
captured count=1 irq=1 dma_req=1
serviced count=1 irq=0 dma_req=1
drained count=1 irq=0 dma_req=0Rules 2 and 3 at work: the row "captured" has count=1. The pulse came after the write had ended, with the bus idle. Rule 4 at work: in "serviced" irq=0 and dma_req=1. In "drained" both are 0, and the count is still 1.
Two more runs of the same lab change one thing each.
| Run | count | irq | dma_req | Rule at work |
|---|---|---|---|---|
| Armed, one pulse | 1 | 1 | 1 | The run above. |
| Not armed, one pulse | 0 | 0 | 0 | Rule 2: the setting decides what the core logic does. |
| Armed, a pulse across two clock edges | 2 | 1 | 1 | Rule 5: two events, one pending bit. |
Recorded from the lab. The second run writes 0 to Control. The third holds the pulse for one more clock.
This lab leaves things out. The pulse is already lined up with the clock: a real pin needs a synchronizer first. The DMA block is one acknowledge wire. The interrupt request goes nowhere: no interrupt controller is in the lab.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Settings live in registers | Software holds the bus while the device works | The bus is free at once for other devices. Each setting costs flip-flops and an address. |
| The device asks for service | Software reads the status again and again | The processor does other work until it is called. The device needs a request wire and a pending bit. |
| Two requests, two clears | One request for both listeners | Each listener is served in its own time. The driver must know which clear belongs to which request. |
| A count beside the pending bit | The pending bit alone | No event is lost from the total. The counter costs more flip-flops, and can still wrap round. |
The Trap: One Clear for Two Listeners
Back to the tap. The handler ran, cleared its flag and returned. The DMA request stayed high, and it looked as if the chip had counted one tap twice. The count says otherwise: it reads 1.
Now suppose a designer "fixes" this. The write to Status is made to clear both pending bits. The handler is quick, and it usually runs first. So the DMA block usually finds its request gone, and the data it should have moved is never moved. Nothing reports an error.
A second trap is rule 6. On the bench the counter works. In the product the device is put to sleep by stopping its clock. The first pulse after wake-up is sometimes missing. The pulse came and went while no clock edge was there to sample it.
When a count does not move, check four things in order. Did the arm write count, on its access edge? Is the arm bit still 1? Does the clock of the device run? Does the pulse reach the device and last across a clock edge?
At Scale
This section looks ahead. A large chip has a hundred peripherals, and each one is this page again.
One checklist for every block
For each device ask six things. Which address? Which register arms it? Which event moves it? Who hears each request? Which clock and reset? Which pins?
Requests are routed
An interrupt request goes to an interrupt controller, which picks one of many. The Interrupt Path follows one. A DMA request goes to the DMA block: see DMA.
Clocks are switched off to save power
Then rule 6 is a design question. Which events must be seen while a block sleeps? What stays awake to see them?
Build one
To write a register block yourself as hardware, see the APB peripheral register map.
Try It
Run the lab. Find the first row in which one request has fallen and the other has not.
Change dma_ack = 1; to dma_ack = 0; and predict the last row before you run it.
Add one more @(negedge pclk); before pulse = 0; so that the pulse covers two rising edges. Predict count, and the two requests.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| wave e2, wave e3 | The APB signals of the arm write at two rising clock edges in a row. |
| count | The number that software reads from the count register. |
| irq | The interrupt request of the device. |
| dma_req | The DMA request of the device. |
| captured, serviced, drained | Three moments: after the pulse, after software cleared its flag, after the acknowledge. |
wave e2: psel=1 penable=0 pwrite=1 paddr=10002000 pwdata=00000001 pstrb=1111 pready=1 prdata=00000000 pslverr=0 wave e3: psel=1 penable=1 pwrite=1 paddr=10002000 pwdata=00000001 pstrb=1111 pready=1 prdata=00000000 pslverr=0 captured count=1 irq=1 dma_req=1 serviced count=1 irq=0 dma_req=1 drained count=1 irq=0 dma_req=0Read included file: soc-s71-counter.sv
// Timer-page teaching personality. pulse is a one-clock, already sampled event.
module ap_pulse_counter (
input logic pclk, presetn, psel, penable, pwrite,
input logic [11:0] offset,
input logic [31:0] pwdata,
input logic [3:0] pstrb,
input logic pulse, dma_ack,
output logic pready, pslverr, irq, dma_req,
output logic [31:0] prdata
);
localparam CTRL = 12'h000, COUNT = 12'h004, STATUS = 12'h008;
logic enabled;
logic [7:0] count;
wire valid = pwrite ? (offset == CTRL || offset == STATUS) :
(offset == CTRL || offset == COUNT || offset == STATUS);
assign pready = 1'b1;
assign pslverr = psel && penable && !valid;
wire wr = psel && penable && pready && pwrite && valid && pstrb[0];
always_comb begin
case (offset)
CTRL: prdata = {31'b0, enabled};
COUNT: prdata = {24'b0, count};
STATUS: prdata = {30'b0, dma_req, irq};
default: prdata = '0;
endcase
end
always_ff @(posedge pclk or negedge presetn) begin
if (!presetn) begin
enabled <= 0;
count <= 0;
irq <= 0;
dma_req <= 0;
end else begin
if (wr && offset == CTRL) enabled <= pwdata[0];
if (wr && offset == STATUS && pwdata[0]) irq <= 0;
if (dma_ack) dma_req <= 0;
if (pulse && enabled) begin
count <= count + 1'b1;
irq <= 1;
dma_req <= 1;
end
end
end
endmodule
Read included file: apb_tasks.svh
// apb_tasks.svh: the requester side of the bench, as tasks for the stimulus (read-only).
// apb_write(addr, data, strb, prot) and apb_read(addr, prot) run one transfer and leave the bus
// IDLE; q_write / q_read queue transfers and apb_run() sends the queue back to back with PSEL held
// high (ACCESS goes straight to SETUP). Each transfer prints one log line at its completion edge:
// e3-e4 WRITE 0x0100 data=0x11223344 strb=1111 waits=0 OKAY
// (setup edge - completion edge). The result of a read is in prdata_q and pslverr_q; the results
// of a queue are in rdv[i] and errv[i]. `cycle` counts rising edges from 0.
int cycle = -1;
always @(posedge pclk) cycle = cycle + 1;
bit quiet = 0;
// Requester bugs, for the broken twins:
bit move_in_wait = 0; // PADDR moves by 4 in the first wait cycle
bit enable_stuck = 0; // PENABLE stays 1 into the next queued transfer (no setup cycle)
bit strb_on_read = 0; // PSTRB = 1111 on reads
bit skip_setup = 0; // PSEL and PENABLE rise together
int give_up = 24; // the bench stops waiting after this many wait cycles
// APB5 wake-up (IHI 0024E 3.7), off by default: with wake_lead >= 0 the requester raises PWAKEUP
// wake_lead cycles before PSEL of a transfer that starts from IDLE, and lowers it after the last
// transfer of a run completes. Broken twin: wake_drop_in_wait lowers PWAKEUP in the first wait cycle.
// The wake-up request is wake_req; a bench built with wake copies it onto PWAKEUP.
int wake_lead = -1;
bit wake_drop_in_wait = 0;
logic wake_req = 0;
logic [31:0] prdata_q = 0;
logic pslverr_q = 0;
int waits_q = 0;
bit stuck_now = 0;
bit q_wr [0:63];
logic [31:0] q_addr [0:63], q_data [0:63], rdv [0:63];
logic [3:0] q_strb [0:63];
logic [2:0] q_prot [0:63];
bit errv [0:63];
int qn = 0;
// A text waveform for the trace blocks: one line per rising edge with the values sampled there.
bit watch = 0;
int wave_edge = -1;
always @(posedge pclk) begin
wave_edge = wave_edge + 1;
`ifdef APB_DECODER
if (watch) $display("wave e%0d: psel=%b pselx=%b penable=%b pwrite=%b paddr=%h pwdata=%h pstrb=%b pready=%b prdata=%h pslverr=%b",
wave_edge, psel, pselx, penable, pwrite, paddr, pwdata, pstrb, pready, prdata, pslverr);
`else
if (watch) $display("wave e%0d: psel=%b penable=%b pwrite=%b paddr=%h pwdata=%h pstrb=%b pready=%b prdata=%h pslverr=%b",
wave_edge, psel, penable, pwrite, paddr, pwdata, pstrb, pready, prdata, pslverr);
`endif
end
// One transfer: SETUP for one cycle, then ACCESS until PREADY. last = 0 keeps PSEL high for the
// next transfer.
task automatic apb_xfer(input bit wr, input logic [31:0] addr, input logic [31:0] data, input logic [3:0] strb,
input logic [2:0] prot, input bit last);
int s_edge, w;
if (wake_lead >= 0 && !psel) begin
if (!wake_req) begin wake_req = 1; repeat (wake_lead) begin @(posedge pclk); #1; end end
end
paddr = addr; pwrite = wr; pwdata = wr ? data : 32'h0;
pstrb = wr ? strb : (strb_on_read ? 4'hf : 4'h0);
pprot = prot; psel = 1;
s_edge = cycle + 1;
if (skip_setup || stuck_now) penable = 1;
else begin
penable = 0;
@(posedge pclk); #1 penable = 1;
end
stuck_now = 0;
w = 0;
@(posedge pclk);
while (pready !== 1'b1 && w < give_up) begin
w++;
#1 if (move_in_wait && w == 1) paddr = addr + 4;
if (wake_drop_in_wait && w == 1) wake_req = 0;
@(posedge pclk);
end
prdata_q = prdata; pslverr_q = (pslverr === 1'b1); waits_q = w;
#1;
if (!quiet) begin
if (w >= give_up) $display("e%0d-e%0d %0s 0x%04h no PREADY after %0d wait cycles, the bench gives up", s_edge, cycle, wr ? "WRITE" : "READ ", addr[15:0], w);
else if (wr) $display("e%0d-e%0d WRITE 0x%04h data=0x%08h strb=%b waits=%0d %0s", s_edge, cycle, addr[15:0], data, strb, w, pslverr_q ? "PSLVERR" : "OKAY");
else $display("e%0d-e%0d READ 0x%04h data=0x%08h waits=%0d %0s", s_edge, cycle, addr[15:0], prdata_q, w, pslverr_q ? "PSLVERR" : "OKAY");
end
if (last) begin
psel = 0; penable = 0; pwrite = 0; pstrb = 0;
if (wake_lead >= 0) wake_req = 0;
end else begin
penable = enable_stuck ? 1 : 0;
stuck_now = enable_stuck;
end
endtask
task automatic apb_write(input logic [31:0] addr, input logic [31:0] data, input logic [3:0] strb = 4'hf, input logic [2:0] prot = 3'b000);
apb_xfer(1, addr, data, strb, prot, 1);
endtask
task automatic apb_read(input logic [31:0] addr, input logic [2:0] prot = 3'b000);
apb_xfer(0, addr, 0, 0, prot, 1);
endtask
// The queue: transfers sent back to back by apb_run().
task automatic q_write(input logic [31:0] addr, input logic [31:0] data, input logic [3:0] strb = 4'hf, input logic [2:0] prot = 3'b000);
q_wr[qn] = 1; q_addr[qn] = addr; q_data[qn] = data; q_strb[qn] = strb; q_prot[qn] = prot; qn++;
endtask
task automatic q_read(input logic [31:0] addr, input logic [2:0] prot = 3'b000);
q_wr[qn] = 0; q_addr[qn] = addr; q_data[qn] = 0; q_strb[qn] = 0; q_prot[qn] = prot; qn++;
endtask
task automatic apb_run();
for (int i = 0; i < qn; i++) begin
apb_xfer(q_wr[i], q_addr[i], q_data[i], q_strb[i], q_prot[i], i == qn - 1);
rdv[i] = prdata_q; errv[i] = pslverr_q;
end
qn = 0;
endtask
task automatic idle(input int n);
repeat (n) @(posedge pclk);
#1;
endtask
Check Yourself
Common Mistakes
Wrong Count the setup edge as the write.
Ready can be 1 during setup. The write counts when enable is 1 as well.
Fix Look for select, enable and ready at 1 on the same edge.
Wrong Clear the DMA request with the interrupt clear.
The DMA block may not have moved its data yet.
Fix Give each request its own clear, and let its own listener use it.
Wrong Read a pending bit as the number of events.
A second event finds the bit already 1.
Fix Read the count when the number matters.
Remember
- A peripheral has five parts: bus interface, register block, core logic, service requests and pad signals, on a clock and a reset.
- A write counts on its access edge. The register then holds the setting, and the core logic acts on its own clock edges.
- One event can raise two requests. Each has its own listener and its own clear.
- A pending bit says that work waits. A count says how much.