Module 10: Capstone Projects · Project 3 — SPI Master Controller

Project 3, Part 3: The Mode-Configurable Master, Verified

The previous articles designed the SPI shift register datapath and transaction framing. This article implements the clock generator that supports all four SPI modes dynamically. We integrate the controller and verify full-duplex operation across each mode using an automated loopback testbench.

Clock Polarity Control Logic

The clock generator initializes SCLK to the idle voltage level specified by input cpol:

logic sclk_reg;
assign sclk = sclk_reg;

always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        sclk_reg <= cpol;   // Idle at the polarity specified by CPOL
    end else if (spi_state == SPI_XFER && sclk_toggle_tick) begin
        sclk_reg <= ~sclk_reg;
    end else if (spi_state != SPI_XFER) begin
        sclk_reg <= cpol;   // Return to idle state between transactions
    end
end

The signal sclk_reg idles at logic 0 when cpol = 0 and at logic 1 when cpol = 1. During transfers, sclk_toggle_tick inverts the clock line periodically.

Dynamic Clock Phase Decoding

Clock phase (cpha) defines whether sampling occurs on the leading or trailing edge:

logic leading_edge, trailing_edge;

// Leading edge is the first transition moving away from the idle level
assign leading_edge  = cpol ? sclk_falling : sclk_rising;
assign trailing_edge = cpol ? sclk_rising  : sclk_falling;

// CPHA=0 selects the leading edge; CPHA=1 selects the trailing edge
assign shift_en = cpha ? trailing_edge : leading_edge;

This logic evaluates physical edges relative to cpol. When cpol = 1, the leading transition is a falling edge. This combinational mapping guarantees correct sampling alignment across all four modes.

Controller State Machine

The master FSM sequences chip select assertion, clock generation, and transfer completion:

typedef enum logic [1:0] {SPI_IDLE, SPI_START, SPI_XFER, SPI_DONE} spi_state_t;
spi_state_t spi_state, spi_next_state;
logic [3:0] bit_count;

always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) spi_state <= SPI_IDLE;
    else        spi_state <= spi_next_state;
end

always_comb begin
    spi_next_state = spi_state;
    unique case (spi_state)
        SPI_IDLE:  if (start_xfer) spi_next_state = SPI_START;
        SPI_START: if (cs_setup_done) spi_next_state = SPI_XFER;
        SPI_XFER:  if (bit_count == 4'd8 && sclk_toggle_tick) spi_next_state = SPI_DONE;
        SPI_DONE:  spi_next_state = SPI_IDLE;
        default:   spi_next_state = SPI_IDLE;
    endcase
end

The SPI_START state enforces setup margins before SCLK toggles. State SPI_XFER counts eight complete bit intervals before transitioning to SPI_DONE.

Automated Multi-Mode Loopback Verification

To verify full-duplex transmission without external slaves, connect MISO directly to MOSI:

assign miso = mosi;   // Loopback MOSI output into MISO input

The testbench tests each mode configuration independently:

task automatic test_mode(bit cpol_val, bit cpha_val, bit [7:0] tx_byte);
    cpol       = cpol_val;
    cpha       = cpha_val;
    tx_data    = tx_byte;
    start_xfer = 1'b1;
    @(posedge clk);
    start_xfer = 1'b0;

    wait (spi_state == SPI_DONE);
    @(posedge clk);

    if (rx_data == tx_byte) begin
        $display("PASS: Mode (CPOL=%b, CPHA=%b) loopback verified for byte %h",
                 cpol_val, cpha_val, tx_byte);
    end else begin
        $display("FAIL: Mode (CPOL=%b, CPHA=%b) transmitted %h, received %h",
                 cpol_val, cpha_val, tx_byte, rx_data);
    end
    wait (spi_state == SPI_IDLE);
endtask

initial begin
    rst_n      = 0;
    start_xfer = 0;
    repeat (3) @(posedge clk);
    rst_n = 1;

    test_mode(1'b0, 1'b0, 8'hA5);   // Mode 0
    test_mode(1'b0, 1'b1, 8'hA5);   // Mode 1
    test_mode(1'b1, 1'b0, 8'hA5);   // Mode 2
    test_mode(1'b1, 1'b1, 8'hA5);   // Mode 3

    $finish;
end

Testing all four modes confirms that leading and trailing edge decoders function properly. Verifying a single mode is insufficient for a configurable multi-mode controller.

Practice Exercise

Explain why looping MOSI back to MISO provides a valid protocol test, even though physical slave devices generate independent data payloads.

Solution

The testbench validates the master controller datapath and clock edge sequencing. Looping MOSI to MISO forces the master shift register to capture its own serial output stream. If the master shifts data on the wrong clock edge or samples prematurely, rx_data will contain corrupted bits. Because the check evaluates whether rx_data matches tx_data, the loopback path verifies timing correctness across all four modes without requiring an external peripheral model.

The SPI master generates synchronous clocks that honor runtime CPOL and CPHA configurations. The control FSM guarantees proper chip-select setup margins before data shifting begins. Independent loopback testing verifies functional compliance across all four operational modes. This completes Project 3. Project 4 implements a synchronous Static RAM (SRAM) memory controller.

← Project 3, Part 2: The Shift Register and Chip Select Project 4, Part 1: What an SRAM Datasheet Demands →