H7.3 Last Receive Bit Lost

MediumFixIcarus · browser samples / server checksReview the topic

Fix an SPI controller that never receives the last bit of a frame.

Hardware context: SPI controller connected to devices with explicit mode and chip-select requirements. Repair a one-edge error affecting the first or final transferred bit.

A temperature sensor returns odd readings through a new SPI controller. MSB first, every reply is even. LSB first, the top bit is always 0. The bits that reach rx_word are correct.

The module spi_master is a full-duplex SPI controller. A start captures the mode, width, divisor, bit order and target device. Then it clocks the frame, sampling one reply bit per SCK period. Done pulses one half period after the last edge.

Each test resets the controller, starts one frame and records the pins after each of 144 clk edges. A model peripheral puts each reply bit on MISO before its sampling edge. The whole record string must match the expected one exactly.

The tests cover all four modes, both bit orders, both devices, widths 1, 4, 8 and 16, and divisors 1 to 4.

Find the bug and fix it.

Signals in the Timeline

SignalWidthRole
mode2input of each check
word_bits5input of each check
half_period3input of each check
Full specification
The ports (keep them)
module spi_master(
  input  logic clk, reset, abort_frame, start,
  input  logic [15:0] tx_word,
  input  logic [7:0]  width, divisor,
  input  logic cpol, cpha, lsb_first, device,
  input  logic miso,
  output logic [1:0]  cs_n,
  output logic sck, mosi, busy, done,
  output logic [15:0] rx_word);
  // your controller
endmodule
ModeCPOLCPHAIdle SCKSamples onDrives on
000lowleading edge (rising)trailing edge (falling)
101lowtrailing edge (falling)leading edge (rising)
210highleading edge (falling)trailing edge (rising)
311hightrailing edge (rising)leading edge (falling)

CPOL is the idle level of SCK. A leading edge leaves the idle level, and a trailing edge returns to it. With CPHA = 0, bit 0 is placed on the data wire before the first edge. This is called the preload.

Logical bit 0 is the first bit on the wire. It is the most significant bit (MSB) of the word, unless lsb_first = 1. Only the low width bits of a word take part.

RuleWhat a correct controller does
Sizeswidth is 1 to 16 bits. divisor is 1 to 4 clk edges per half period of SCK.
Startstart = 1 while idle captures every setting and begins a frame. A start while busy is ignored.
Selectdevice 0 drives cs_n = 2 (cs_n[0] low). device 1 drives cs_n = 1 (cs_n[1] low). Idle is 3.
ReceiveEach sampled MISO bit goes straight to its own place in rx_word. Bits not yet sampled read 0.
FinishBoth selects go high, busy falls and done is 1 for exactly one edge. MOSI keeps its last value.
ResetSynchronous, active high. Selects high, SCK = cpol, MOSI = 0, busy = 0, rx_word = 0.
Abortabort_frame = 1 cancels the frame. Selects high, SCK = the captured idle level, MOSI = 0, rx_word = 0, no done.
Edge t (from the start edge)What the pins show after the edge
t = 0The start edge. One select goes low, SCK is idle and busy = 1. MOSI holds bit 0 when CPHA = 0, else 0.
t = divisor, 2 x divisor, ...SCK toggles once per divisor edges, 2 x width times in all. The last toggle is at t = 2 x width x divisor.
t = (2k + 1 + CPHA) x divisorBit k (counting from 0) of the reply is sampled and appears in rx_word.
t = (2 x width + 1) x divisorBoth selects rise, busy = 0 and done = 1 for this edge only.

After every clk edge the tests write one record, such as 20110:003c;. The digits are cs_n (0 to 3), sck, mosi, busy and done. The four hex digits after the colon are rx_word.

The result compares the whole record string. trace_matches is yes only when every record matches. The two lengths help spot a short or long transcript.

Examples

Example 1

Mode 0, Eight Bits
Input
Mode 0, 8 bits, divisor 2, MSB first, device 0. Send 0xa5; the peripheral replies 0x3c.
Output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes

The start edge pulls cs_n to 2 and preloads bit 7 of 0xa5 on MOSI. SCK toggles every 2 edges up to t = 32. Samples land at t = 2, 6, ..., 30. Done pulses at t = 34 with rx_word = 0x003c.

Example 2

Mode 3, Four Bits, LSB First
Input
Mode 3, 4 bits, divisor 1, LSB first, device 1. Send 9; the peripheral replies 6.
Output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes

cs_n goes to 1 and SCK idles high. MOSI is 0 until the first leading edge at t = 1 drives bit 0. Samples land at t = 2, 4, 6 and 8. Done pulses at t = 9 with rx_word = 0x0006.

Example 3

Mode 1, Sixteen Bits
Input
Mode 1, 16 bits, divisor 4, MSB first, device 0. Send 0x8001; the peripheral replies 0xa55a.
Output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes

Leading edges at t = 4, 12, ... drive the bits. Trailing edges at t = 8, 16, ..., 128 sample them. Done pulses at t = 132 with rx_word = 0xa55a.

Constraints and Contract

  • width is 1 to 16 and divisor is 1 to 4. Inputs are never x or z.
  • Only the digital pins are checked. Analog setup and hold timing is not part of the tests.
  • Keep the module name and the ports.

Hints

Hint 1

Which logical bit is missing? Turn the symptom into a bit number for MSB-first and LSB-first frames.

Hint 2

Find the SCK edge that samples the last bit. With edge_count counting edges from 0, it is 2 x width - 2 + CPHA.

Hint 3

Look at the condition on the receive branch. Its extra limit on edge_count stops before the last sampling edge.

What the grader checks: Each test runs the controller on real clock edges for one frame. Every select, SCK, MOSI, busy, done and rx_word value is compared after every edge. Analog timing is not checked.

35 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 7 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Mode 0, Eight Bits

Input
Mode 0, 8 bits, divisor 2, MSB first, device 0. Send 0xa5; the peripheral replies 0x3c.
Expected output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes

The start edge pulls cs_n to 2 and preloads bit 7 of 0xa5 on MOSI. SCK toggles every 2 edges up to t = 32. Samples land at t = 2, 6, ..., 30. Done pulses at t = 34 with rx_word = 0x003c.

Run samples executes all 3 public checks. Check all tests runs 35 checks on the server.

Icarus · browser samples / server checks