SPI Master Controller (Mode 0, Parameterized)
advanced · interface · 300 XP
Sign in to write your solution, run it in the simulator and submit it for grading.
Sign in or create a free account →Problem
SPI Master Controller — Mode 0 (CPOL=0, CPHA=0)
Implement a complete SPI master that initiates transfers, generates the serial clock (SCK), shifts data, and handles CS assertion/deassertion.
**SPI Mode 0 timing:**
- `CPOL=0`: SCK idles low
- `CPHA=0`: data is captured on rising SCK edge, shifted on falling SCK edge
- `MOSI` changes on falling SCK (so slave can capture on the rising edge)
- Slave `MISO` data is sampled on rising SCK
**Parameters**
- `DATA_WIDTH`: bits per transfer (default 8)
- `CLK_DIV`: divider for SCK frequency = `clk / (2 * CLK_DIV)` (default 4)
**Interface**
| Port | Direction | Width | Description |
|-------------|-----------|------------|--------------------------------|
| `clk` | input | 1 | System clock |
| `rst` | input | 1 | Sync reset |
| `start` | input | 1 | Start transfer pulse |
| `din` | input | DATA_WIDTH | Data to transmit (MOSI) |
| `cs_n` | output | 1 | Chip select (active low) |
| `sclk` | output | 1 | Serial clock |
| `mosi` | output | 1 | Master out, slave in |
| `miso` | input | 1 | Master in, slave out |
| `dout` | output | DATA_WIDTH | Data received (MISO) |
| `done` | output | 1 | Transfer complete pulse |
**FSM States:**
1. **IDLE:** Wait for `start`
2. **WAIT_EDGE:** Count clock divider to generate SCK edges (rising/falling)
3. **SHIFT_OUT:** On falling SCK, assert MOSI with next bit
4. **CAPTURE_IN:** On rising SCK, capture MISO bit
5. **DONE:** Assert `done` pulse, return to IDLE
**Behaviour:**
- Assert `cs_n=0` at start
- Shift `din` MSB-first on falling SCK edges
- Capture `miso` on rising SCK edges into `dout`
- After `DATA_WIDTH` bits, deassert `cs_n=1` and pulse `done`
- Ready for next transfer immediately
**Key insight:** SCK frequency is half the divide rate. A CLK_DIV of 4 means 4 system clocks per SCK half-period, so SCK runs at clk/(2*4)=clk/8.
Constraints
- Use parameterized DATA_WIDTH and CLK_DIV.
- Generate SCK by counting system clock cycles; toggle SCK every CLK_DIV cycles.
- Shift data MSB-first (bit [DATA_WIDTH-1] first).
- Mode 0: CPOL=0 (SCK idles low), CPHA=0 (sample on rising, shift on falling).
- done is a 1-cycle pulse; cs_n stays low throughout the transfer and deasserts after done.
Examples
Input: start=1, din=8'hAA, CLK_DIV=4
Output: 8 SCK cycles; cs_n=0 for duration; mosi shifts AA; dout=loopback
Standard transfer
Input: CLK_DIV=2 (faster SCK)
Output: Same transfer, 2x faster SCK frequency
Parameterized frequency
Common questions
- What does the SPI Master Controller (Mode 0, Parameterized) problem ask you to build?
- SPI Master Controller — Mode 0 (CPOL=0, CPHA=0) Implement a complete SPI master that initiates transfers, generates the serial clock (SCK), shifts data, and handles CS assertion/deassertion.
- What constraints does SPI Master Controller (Mode 0, Parameterized) have?
- Use parameterized DATA_WIDTH and CLK_DIV.; Generate SCK by counting system clock cycles; toggle SCK every CLK_DIV cycles.; Shift data MSB-first (bit [DATA_WIDTH-1] first).; Mode 0: CPOL=0 (SCK idles low), CPHA=0 (sample on rising, shift on falling).; done is a 1-cycle pulse; cs_n stays low throughout the transfer and deasserts after done.
- What is an example input and output for SPI Master Controller (Mode 0, Parameterized)?
- Input start=1, din=8'hAA, CLK_DIV=4 gives output 8 SCK cycles; cs_n=0 for duration; mosi shifts AA; dout=loopback. Standard transfer
- Which Verilog concepts does SPI Master Controller (Mode 0, Parameterized) practise?
- interface, SPI, protocol, interview, master.