Challenge - 5 Problems
SPI Master Controller Expert
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateSPI Master Clock Generation Output
What is the output frequency of the SPI clock (SCLK) generated by this VHDL process if the input clock is 50 MHz and the clock divider is set to 4?
process(clk)
begin
if rising_edge(clk) then
if counter = (divider / 2) - 1 then
sclk <= not sclk;
counter <= 0;
else
counter <= counter + 1;
end if;
end if;
end process;VHDL
signal sclk : std_logic := '0'; signal counter : integer := 0; constant divider : integer := 4; -- clk frequency = 50 MHz
Attempts:
2 left
💡 Hint
The SPI clock toggles every half period of the divided clock.
✗ Incorrect
The divider divides the input clock by 4. The sclk toggles every (divider/2) cycles, so the output frequency is 50 MHz / 4 = 12.5 MHz.
❓ Predict Output
intermediateSPI Master Data Shift Output
Given the following VHDL snippet for shifting out data MSB first on MOSI line, what is the value of MOSI after 3 clock cycles if data_out is "10110011" and shift_reg is initialized with data_out?
process(clk)
begin
if rising_edge(clk) then
if start = '1' then
shift_reg <= data_out;
elsif shifting = '1' then
MOSI <= shift_reg(7);
shift_reg <= shift_reg(6 downto 0) & '0';
end if;
end if;
end process;VHDL
signal shift_reg : std_logic_vector(7 downto 0); signal MOSI : std_logic; signal data_out : std_logic_vector(7 downto 0) := "10110011"; signal start : std_logic := '1'; signal shifting : std_logic := '0';
Attempts:
2 left
💡 Hint
Look at the MSB of data_out and how it shifts out.
✗ Incorrect
The MSB of data_out is '1'. After loading shift_reg, the first MOSI output is '1'. After 3 shifts, MOSI outputs the bits in order: '1', '0', '1'. After the third clock cycle, MOSI is '1'.
🔧 Debug
advancedIdentify the Error in SPI Chip Select Logic
What error will this VHDL code cause when trying to control the SPI chip select (CS) signal?
process(clk)
begin
if rising_edge(clk) then
if start = '1' then
CS <= '0';
else
CS <= '1';
end if;
end if;
end process;
CS <= '1';VHDL
signal CS : std_logic := '1';
signal start : std_logic;Attempts:
2 left
💡 Hint
Check if CS is assigned in multiple places.
✗ Incorrect
CS is assigned inside the process and also outside it concurrently. This creates multiple drivers on the same signal, causing a compilation error.
📝 Syntax
advancedSyntax Error in SPI State Machine
Which option contains the correct syntax for a VHDL case statement inside a process handling SPI states?
process(clk)
begin
if rising_edge(clk) then
case state is
when IDLE =>
CS <= '1';
when TRANSFER =>
CS <= '0';
when others =>
CS <= '1';
end case;
end if;
end process;Attempts:
2 left
💡 Hint
VHDL case statements use 'case ... is' and 'when ... =>' syntax.
✗ Incorrect
The correct syntax for a VHDL case statement uses 'case ... is' and 'when ... =>'. Using 'case ... of' or ':' instead of '=>' causes syntax errors.
🚀 Application
expertCalculate Total SPI Transfer Time
An SPI master controller runs with a 100 MHz system clock. It divides this clock by 8 to generate the SPI clock (SCLK). It transfers 16 bits per transaction. What is the total time in microseconds to complete one SPI transfer?
Assume the SPI clock toggles every (divider/2) cycles and each bit requires one full SCLK cycle.
Assume the SPI clock toggles every (divider/2) cycles and each bit requires one full SCLK cycle.
Attempts:
2 left
💡 Hint
Calculate SCLK frequency first, then multiply by bits and clock period.
✗ Incorrect
System clock is 100 MHz. Divider is 8, so SCLK frequency = 100 MHz / 8 = 12.5 MHz. One SCLK period = 1 / 12.5 MHz = 0.08 microseconds. For 16 bits, total time = 16 * 0.08 = 1.28 microseconds.
