Challenge - 5 Problems
SPI Controller Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateSPI Controller State Machine Output
Given the following Verilog snippet for an SPI controller state machine, what is the value of
spi_busy after the start signal is asserted and the state machine moves from IDLE to TRANSFER?Verilog
typedef enum logic [1:0] {IDLE=2'b00, TRANSFER=2'b01, DONE=2'b10} state_t; state_t state = IDLE; logic spi_busy = 0; logic start = 0; always_ff @(posedge clk) begin case(state) IDLE: if(start) state <= TRANSFER; TRANSFER: state <= DONE; DONE: state <= IDLE; endcase spi_busy <= (state == TRANSFER); end
Attempts:
2 left
💡 Hint
Remember that
spi_busy is assigned after the state update in the same clock cycle.✗ Incorrect
The state variable updates on the clock edge, but
spi_busy is assigned based on the old state value before the update. So when start is asserted, state changes from IDLE to TRANSFER, but spi_busy still sees the old state value, which is IDLE, so it remains 0 in that clock cycle.🧠 Conceptual
intermediateSPI Clock Polarity and Phase Settings
In an SPI controller, what do the clock polarity (CPOL) and clock phase (CPHA) settings control?
Attempts:
2 left
💡 Hint
Think about how SPI devices synchronize data with the clock.
✗ Incorrect
CPOL defines whether the clock is idle high or low. CPHA defines whether data is sampled on the first or second clock edge. Together, they determine SPI mode.
🔧 Debug
advancedIdentify the Bug in SPI Shift Register
This Verilog code snippet is intended to shift out data on MOSI during SPI transfer. What is the bug that will cause incorrect data transmission?
Verilog
always_ff @(posedge spi_clk) begin if (start_transfer) begin shift_reg <= data_in; end else begin shift_reg <= {shift_reg[6:0], 1'b0}; end mosi <= shift_reg[7]; end
Attempts:
2 left
💡 Hint
Check the order of operations inside the always block.
✗ Incorrect
The MOSI output is assigned after the shift operation, so it outputs the shifted bit, not the current MSB. MOSI should be assigned before shifting to send the correct bit.
📝 Syntax
advancedVerilog Syntax for SPI Chip Select Control
Which of the following Verilog code snippets correctly implements active-low chip select (CS) control for an SPI controller?
Attempts:
2 left
💡 Hint
Remember that active-low means the signal is 0 when active.
✗ Incorrect
Option A uses correct bit values 1'b0 and 1'b1. Option A uses integers without bit width, which is allowed but less clear. Option A uses 0 without bit width, which is allowed but inconsistent. Option A uses character literals which are invalid in Verilog.
🚀 Application
expertSPI Controller Bit Count After Transfer
An SPI controller shifts out 8 bits per transfer. The controller uses a 3-bit counter
bit_cnt that increments on each SPI clock edge during transfer. After one full transfer, what is the value of bit_cnt?Verilog
logic [2:0] bit_cnt = 0; logic transfer_active = 1; always_ff @(posedge spi_clk) begin if (transfer_active) begin bit_cnt <= bit_cnt + 1; end else begin bit_cnt <= 0; end end
Attempts:
2 left
💡 Hint
Consider the bit width of the counter and how it increments.
✗ Incorrect
A 3-bit counter can count from 0 to 7. After 8 increments, it wraps back to 0. So after shifting out 8 bits, bit_cnt will be 7 (3'b111) on the last bit, then next increment resets or wraps.
