SPI Master
Medium · Sequential · SystemVerilog · VHDL
A premium problem. Subscribe to open it in the editor, run the testbench and see where your design lands.
Design an SPI mode 0 master that sends tx_data on mosi while it receives a byte from miso into rx_data, both most significant bit first.
In SPI mode 0 the serial clock sclk idles low, the master changes mosi while sclk is low, and both ends sample their input as sclk rises.
Outside a transfer cs_n is high, sclk is low and mosi may take any value. A transfer starts after a cycle in which start is high and no transfer is in progress, and sends the tx_data of that cycle; start during a transfer has no effect. A transfer lasts 16 cycles, numbered from 0, with cs_n low throughout. sclk is low in the even cycles and high in the odd ones, so it rises 8 times. In cycles 2k and 2k+1, mosi carries bit 7-k of tx_data, and bit 7-k of the received byte is the value of miso in cycle 2k, the cycle before sclk rises. done is high in cycle 15 only, and in that cycle rx_data is the received byte; in every other cycle rx_data may take any value. In the cycle after cycle 15 the master is idle again.
resetn is a synchronous, active-low reset: it ends any transfer, sets cs_n high and sets sclk and done low.
- Serial interfaces
- State machines