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

Project 3, Part 2: The Shift Register and Chip Select

The previous article defined the four SPI operating modes. This article implements the SPI master datapath and transaction framing logic. Unlike half-duplex UART transceivers, SPI operates as a full-duplex synchronous interface. A single shift register transfers outgoing data bits and captures incoming serial data simultaneously.

Full-Duplex Shift Register Architecture

SPI uses two concurrent data lines: Master-Out Slave-In (MOSI) and Master-In Slave-Out (MISO). On every active shift clock transition, the master drives one bit onto MOSI while sampling one bit from MISO.

Shift register: MSB out on MOSI, MISO in at the LSB, shifting left
Figure 1: One shift register serving both directions

module spi_shift_reg #(parameter WIDTH = 8) (
    input  logic             clk, rst_n,
    input  logic             shift_en,        // Pulse enable per SPI bit transition
    input  logic             load,            // Parallel load trigger
    input  logic [WIDTH-1:0] tx_data,
    input  logic             miso,
    output logic             mosi,
    output logic [WIDTH-1:0] rx_data
);
    logic [WIDTH-1:0] shift_reg;

    assign mosi = shift_reg[WIDTH-1];   // Transmit most significant bit (MSB) first

    always_ff @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            shift_reg <= {WIDTH{1'b0}};
        end else if (load) begin
            shift_reg <= tx_data;
        end else if (shift_en) begin
            shift_reg <= {shift_reg[WIDTH-2:0], miso};   // Shift left and capture MISO at bit 0
        end
    end

    assign rx_data = shift_reg;   // Parallel output available after WIDTH shifts
endmodule

The shift register outputs the most significant bit (MOSI) and shifts in MISO at bit position zero. After eight shift operations, the register contains the complete received byte. A single physical register handles both transmission and reception without separate buffering.

Chip-Select Framing and Setup Timing

The active-low chip select signal (CS_n) frames transactions. It selects external slave peripherals and synchronizes internal state machines:

always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
        cs_n <= 1'b1;   // Idle state: unselected
    end else begin
        unique case (spi_state)
            SPI_IDLE:  cs_n <= 1'b1;
            SPI_START: cs_n <= 1'b0;   // Assert CS_n prior to the first SCLK edge
            SPI_XFER:  cs_n <= 1'b0;   // Maintain active low throughout transfer
            SPI_DONE:  cs_n <= 1'b1;   // Deassert after transfer completes
            default:   cs_n <= 1'b1;
        endcase
    end
end

The master must assert CS_n low for an adequate setup interval before toggling SCLK. This setup margin ensures the slave device detects the active bus and enables its output drivers. Similarly, CS_n must remain low until the final clock edge completes to prevent truncated transfers.

Practice Exercise

A slave peripheral requires CS_n to assert at least 50 ns before the first SCLK transition. The master operates with a 20 MHz system clock (50 ns period). Determine the minimum clock cycles the controller must wait in state SPI_START before toggling SCLK.

Solution

At 20 MHz, each clock period equals:

$$T_{\text{clk}} = \frac{1}{20,\text{MHz}} = 50,\text{ns}$$

The state machine must maintain state SPI_START with CS_n = 0 for at least one full cycle (50 ns). To absorb clock routing delays and external pad skews, digital controllers typically add an extra cycle of margin. Configuring SPI_START for two clock cycles (100 ns) provides a robust timing margin.

Full-duplex SPI architectures use a single shift register to drive MOSI and sample MISO simultaneously. Chip-select signals enforce frame boundaries and must satisfy slave setup timing specifications. The next article combines this datapath with a multi-mode clock generator and validates all four modes.

← Project 3, Part 1: SPI and Its Four Modes Project 3, Part 3: The Mode-Configurable Master, Verified →