← Protocols Interviews

Interview Question · Concept

SPI Modes, CPOL and CPHA

L0 FresherL1 Juniorspimodes
  • Likely at Google
  • Likely at Qualcomm
  • Likely at Analog Devices

The Question

The interviewer asks

Explain SPI modes 0 to 3. In each mode, which clock edge launches data and which edge samples it?

Reveal the Model Answer

CPOL is the clock's idle level. CPHA picks the sampling edge. CPHA 0 samples on the first edge after the clock leaves idle; CPHA 1 samples on the second. The other edge launches the next bit. So modes 0 (CPOL 0, CPHA 0) and 3 (1, 1) sample on rising edges, and modes 1 and 2 on falling edges. With CPHA 0, the first bit must already be on the line when CS falls.

Four Modes, Two Sampling EdgesCPOL picks where the clock rests; CPHA picks the first or second edge. The result is one of two sampling edges.
Four modes, two sampling edgesCPOL picks where the clock rests; CPHA picks the first or second edge. The result is one of two sampling edges. component CPOL 0 clock rests low component CPOL 1 clock rests high component Mode 0 CPHA 0: first edge component Mode 1 CPHA 1: second edge component Mode 2 CPHA 0: first edge component Mode 3 CPHA 1: second edge component Samples rising launches falling component Samples falling launches rising
  1. Rising-edge family

    Modes 0 and 3 sample on rising edges. They differ only in where the clock rests, which matters at the edges of CS.

  2. Falling-edge family

    Modes 1 and 2 sample on falling edges. A device from one family on a controller from the other launches on the edge the controller samples.

Read the diagram as text
  • CPOL 0: clock rests low
  • CPOL 1: clock rests high
  • Mode 0: CPHA 0: first edge
  • Mode 1: CPHA 1: second edge
  • Mode 2: CPHA 0: first edge
  • Mode 3: CPHA 1: second edge
  • Samples rising: launches falling
  • Samples falling: launches rising
  • CPOL 0 → Mode 0
  • CPOL 0 → Mode 1
  • CPOL 1 → Mode 2
  • CPOL 1 → Mode 3
  • Mode 0 → Samples rising
  • Mode 3 → Samples rising
  • Mode 1 → Samples falling
  • Mode 2 → Samples falling

Arrows lead from the rest level through the mode to its sampling edge.

ModeCPOLCPHAClock at idleSample edgeLaunch edge
000lowrisingfalling (bit 7 at CS fall)
101lowfallingrising
210highfallingrising (bit 7 at CS fall)
311highrisingfalling

Same sample edge: modes 0 and 3, modes 1 and 2.

Self-Check

Tick each key point you covered in your answer.

What Interviewers Listen For (Fresher)

A good fresher answer fills in the mode table correctly. It says CPOL is the clock's idle level, and CPHA chooses the first or second clock edge for sampling.

Cross-Questions

Where the interviewer goes next. Open a node to practise that follow-up.

WhyWhat IfProve ItBreak ItCompareScale It

  1. SPI Modes, CPOL and CPHA
  2. WhyWhere the First CPHA 0 Bit Comes FromL1 Junior

    In mode 0 the first edge after CS falls is a sampling edge. When was the first bit launched?

    Reveal the Model Answer

    At the falling edge of CS. With CPHA 0 there is no launch edge before the first sample. So both sides put bit 7 out as soon as they are selected. They launch every later bit on the trailing edge. A peripheral that waits for a clock edge to drive bit 7 delivers the byte one bit late.

    Mode 0: CPOL 0, CPHA 0
    mode 0 012345678910111213141516171819CS_nCLKMOSIMISOMOSI sampled at 3MOSI sampled at 5MOSI sampled at 7MOSI sampled at 9MOSI sampled at 11MOSI sampled at 13MOSI sampled at 15MOSI sampled at 17MISO sampled at 3MISO sampled at 5MISO sampled at 7MISO sampled at 9MISO sampled at 11MISO sampled at 13MISO sampled at 15MISO sampled at 17
    Read phases and sample points (half clock)
    • mode 0: columns 1 through 18.
    • MOSI sampled at 3, 5, 7, 9, 11, 13, 15, 17.
    • MISO sampled at 3, 5, 7, 9, 11, 13, 15, 17.
    1. Cycle 1

      CPHA 0: the first bit is out as soon as CS falls, before any clock edge.

    2. Cycle 3

      The first edge, rising, is the sample edge. Each side changes its output on the falling edge between samples.

    Generated from the transaction and decoded back. Dots mark the sample edges.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Junior)

    A good junior answer says that for CPHA 0, CS falling acts as the launch of the first bit.

  3. CompareMode 0 Against Mode 3L1 Junior

    A serial flash supports both mode 0 and mode 3. What actually differs, and does it matter which one you use?

    Reveal the Model Answer

    Only the clock level while idle, at the CS edges: low in mode 0, high in mode 3. In both, the flash latches input on rising edges and drives output on falling edges. So data transfer is identical. It matters at the frame boundary: the controller must hold the idle clock level when it moves CS. The CS-to-clock setup and hold times still apply.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Junior)

    A good junior answer says only the idle clock level differs, and the data edges are the same.

  4. What IfThe Reply Shifted by One BitL1 JuniorL2 Mid-Level

    Asked if they ask what happens when the two sides disagree

    Your controller reads a peripheral's ID register as 52h, but the datasheet says A5h. Writes seem to work. What is your first suspect?

    log
    Illustrative peripheral-ID read
    Expected ID: A5h
    Received ID: 52h
    Write transactions appear to succeed
    Reveal the Model Answer

    A clean one-bit shift is the signature of an SPI mode mismatch, not noise. Both sides move one bit per clock but disagree on which edge carries it. The controller samples each bit just before the peripheral launches it, so it gets the previous bit. The lost bit turns up in the next byte. With a mode-0 controller and a mode-1 peripheral, writes can still pass. But their only hold time is the launch delay.

    Controller Mode 0, Peripheral Mode 1: Launching on the Sample EdgeThe peripheral changes MISO on the rising edge, the very edge on which the controller samples it. It wins only by its 1 ns launch delay.
    01234567891011121314151617181920212223242526272829303132333435CS_nCLKMOSIMISO
    1. Cycle 2

      The controller samples MISO on this rising edge. The peripheral launches on it too, 1 ns later.

    2. Cycle 18

      Second byte: every bit the controller takes is the bit before. Bit 7 of the echo is late, and so is every bit after it.

    Measured: two loopback bytes of A5h. The controller reads the second as 52h, one bit late. The peripheral still gets A5h, with only 1 ns of hold.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Junior)

    A good junior answer sees a one-bit shift as an SPI mode mismatch. It checks CPOL and CPHA against the device datasheet.

    Misconception

    "A wrong SPI mode gives random garbage."

    It gives the same bytes shifted by exactly one bit. In the twin, a mode-1 peripheral sampled on its own launch edge returns 52h 9Eh for A5h 3Ch. The bit lost from A5h reappears at the top of the next byte.

    Run It

    A mode-1 peripheral read by a mode-0 controller: every bit one place late, not noise. The fixed run puts the peripheral in mode 0.

    RUNIcarus Verilog · in your browser
    Edit the code, then run it
    Expected: FAIL: sent a5 3c, controller got 52 9e
    See the Fix
    `timescale 1ns/1ps
    // A mode-0 controller (CPOL 0, CPHA 0: samples MISO on rising SCLK) reads two bytes from a
    // peripheral holding A5h then 3Ch. Peripheral CPHA = 0: mode 0, bit 7 is out when CS falls and each later bit is launched on a falling edge.
    module periph #(parameter bit CPHA = 0) (input sclk, input cs_n, output reg miso);
      reg [15:0] sr = 16'ha53c;
      initial miso = 0;
      always @(negedge cs_n) if (!CPHA) begin miso = sr[15]; sr = sr << 1; end
      always @(negedge sclk) if (!cs_n && !CPHA) begin miso <= sr[15]; sr <= sr << 1; end
      always @(posedge sclk) if (!cs_n && CPHA) begin miso <= #1 sr[15]; sr <= sr << 1; end
    endmodule
    module tb;
      reg sclk = 0, cs_n = 1;
      wire miso;
      reg [15:0] rx;
      periph #(.CPHA(0)) p(sclk, cs_n, miso);
      always @(posedge sclk) rx <= {rx[14:0], miso};
      initial begin
        #10 cs_n = 0; #5;
        repeat (16) begin #5 sclk = 1; #5 sclk = 0; end
        #5 cs_n = 1;
        if (rx == 16'ha53c) $display("PASS: controller got a5 3c");
        else $display("FAIL: sent a5 3c, controller got %h %h", rx[15:8], rx[7:0]);
        $finish;
      end
    endmodule
    

    Prints: PASS: controller got a5 3c

    Misconception

    "It is a bit-order bug: the peripheral sends LSB first."

    A5h is 1010 0101, and that pattern reversed is the same. So a bit-order swap would still read A5h. The value 52h is A5h moved one place right, and only an edge disagreement explains that.

    Source Arithmetic (1010 0101 reversed is 1010 0101); ASIC Point lesson P7.2 (measured: controller 0, peripheral 1 reads A5h as 52h)

    Half-Truth

    "The writes work, so the peripheral mode must be right."

    In the mismatched pair, the peripheral still received A5h on the bench. But it had only 1 ns of hold, half the 2 ns data-in hold time (tCHDX) a W25Q128JV needs. It passes in simulation and fails on a board.

  5. WhyWhy Exactly One BitL1 Junior

    Why is a mode mismatch a shift of exactly one bit, never two?

    Reveal the Model Answer

    Both sides still move one bit per clock and agree on the number of clocks. They disagree only on which of the two edges in a clock period carries the bit. Those edges are half a period apart. Sampling on the launch edge reads the value from before the launch, which is the previous bit. So every bit lands exactly one place late.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Junior)

    A good junior answer explains that both sides count the same clocks but use edges half a period apart. So the controller samples the bit before the one it wants.

  6. What IfPassing in Simulation, Failing on the BoardL2 Mid-Level

    The mismatched direction passes in your zero-delay simulation. Why might it fail in silicon, and how do you compute the real margin?

    Reveal the Model Answer

    On the shared edge, the only hold time is the launching side's clock-to-output delay. So a model with no delays always takes the old bit and looks correct. In silicon, hold must beat the receiver's requirement, such as tCHDX of 2 ns on the W25Q128JV. For same-cycle reads, start from half a clock period. Subtract the peripheral's clock-to-output time (tCLQV) and the controller's input setup; the rest is the setup margin.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Mid-Level)

    A good mid-level answer says the hold time equals the launch delay. It works out the half-period setup budget using tCLQV, the flash clock-to-output time.

  7. Prove ItSolve P7.3.1: Sample MISO After It Becomes ValidSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem P7.3.1

  8. Break ItHow an SPI Monitor Picks Its EdgeL1 JuniorL2 Mid-Level

    You write a passive SPI monitor. How does it know which edge to sample, and what can a checker on the wires assert?

    Reveal the Model Answer

    From configuration, never by guessing: rising when CPOL equals CPHA, falling otherwise. A monitor that infers the mode can be fooled by the very mismatch it should catch. On the wires alone, a checker can assert the idle clock level at CS edges and MISO released while deselected. It can also check stable data on the sample edge, whole bytes on writes and CS-to-clock times. Whether the bytes are right needs a reference model.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Junior)

    A good junior answer says the monitor takes the mode from configuration, never from guessing.

  9. Prove ItSolve P7.5.2: Prove the SPI Sampling EdgeSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem P7.5.2

  10. Scale ItVerifying an SPI Flash ControllerL2 Mid-LevelL3 Senior

    You verify a QSPI flash controller that does reads, page programs and erases. What is in your bench, and which bugs would still escape?

    Reveal the Model Answer

    Use a wire monitor for every supported mode and lane width. Add wire checks that need no data: idle level, MISO and IO release, CS timing and whole bytes on writes. A strict flash model refuses what the datasheet forbids: programs without WEL, quad reads with QE clear, commands while BUSY. A scoreboard compares readback with a reference memory. Bugs escape through a lenient model, page-wrap writes, off-by-one dummy counts and turnaround timing.

    The Bug-Hunt BenchStimulus programs drive the controller; the flash answers; the checker watches the enables; each program reads its result back.
    The bug-hunt benchStimulus programs drive the controller; the flash answers; the checker watches the enables; each program reads its result back. component Stimulus programs seven tasks component Controller six faults component Flash model five faults component Contentionchecker ctrl_oe & flash_oe component Readback data ok or WRONG component Flash trace line command, clocks,bytes task callspinsctrl_oeflash_oeexpected bytes
    1. The wire eye

      The checker reads both enables on every clock. It catches a fight for the lanes and nothing else.

    2. The readback eye

      Each program knows what the flash should hold and reads it back. It catches what the flash did, legal wire or not.

    3. The record

      The flash prints a line per command. It rules on nothing; it is the evidence the other two eyes point at.

    Read the diagram as text
    • Stimulus programs: seven tasks
    • Controller: six faults
    • Flash model: five faults
    • Contention checker: ctrl_oe & flash_oe
    • Readback: data ok or WRONG
    • Flash trace line: command, clocks, bytes
    • Stimulus programs → Controller: task calls
    • Controller → Flash model: pins
    • Controller → Contention checker: ctrl_oe
    • Flash model → Contention checker: flash_oe
    • Flash model → Flash trace line
    • Stimulus programs → Readback: expected bytes

    Arrows are calls, pins and the values each observer reads.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Mid-Level)

    A good mid-level answer names the parts of the bench: a wire monitor, wire checks, a flash model and a readback scoreboard.

  11. Prove ItSolve P7.10.1: Check the Controller and Its Flash ModelSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem P7.10.1

  12. Prove ItShowing the Flash Bench Catches Each FaultL2 Mid-LevelL3 Senior

    Prove it: how do you show that your QSPI bench catches each class of fault, not just that the tests pass?

    Reveal the Model Answer

    Plant one fault per class, such as a missing WEL, a late IO release or a page-wrap error. Run every test against every fault and record which check fires. A fault with no mark is a hole, so add a program for it. On the lesson bench the wire checker caught 1 fault of 11, and readbacks caught the rest.

    Self-Check

    Tick each key point you covered in your answer.

    Tool as Truth

    "The wire checker is clean, so the controller is right."

    Most flash faults keep the wires legal. Only a readback against the datasheet rule shows what the flash did.

    Source Lesson P7.10 key points

  13. Prove ItSolve P7.2.2: Diagnose the Displaced SPI ReplySimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem P7.2.2

  14. Break ItHow an SPI Monitor Picks Its EdgeL1 JuniorL2 Mid-Level

    You write a passive SPI monitor. How does it know which edge to sample, and what can a checker on the wires assert?

    Reveal the Model Answer

    From configuration, never by guessing: rising when CPOL equals CPHA, falling otherwise. A monitor that infers the mode can be fooled by the very mismatch it should catch. On the wires alone, a checker can assert the idle clock level at CS edges and MISO released while deselected. It can also check stable data on the sample edge, whole bytes on writes and CS-to-clock times. Whether the bytes are right needs a reference model.

    Self-Check

    Tick each key point you covered in your answer.

    What Interviewers Listen For (Junior)

    A good junior answer says the monitor takes the mode from configuration, never from guessing.

  15. Prove ItSolve P7.5.2: Prove the SPI Sampling EdgeSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem P7.5.2

  16. Prove ItSolve P7.5.1: A Monitor for All Four SPI ModesSimulator-graded

    Show it, don't just say it: this Silicon Lab problem is graded by the simulator.

    Open Problem P7.5.1

What People Get Wrong

Common wrong answers and why they fail.

Misconception

"Mode 3 is mode 0 with the clock inverted, so it samples on falling edges."

Both CPOL and CPHA flip, so mode 3 still samples on rising edges. Only the idle level differs. That is why a flash such as the W25Q128JV supports modes 0 and 3. It latches input on rising edges in both.

Source Winbond W25Q128JV datasheet Rev. F §6.1 Standard SPI Instructions (modes 0 and 3 differ in the idle CLK level) and §4.2 (input latched on rising CLK)

Half-Truth

"CPHA 1 means sample on the falling edge."

This is true only when CPOL is 0. With CPOL 1, the second edge after idle is a rising edge. Name edges as leading and trailing, then map them through CPOL.

Learn It, Then Prove It