Motion FPGA
Motion FPGA for the MachX02-7000HE Breakout Board
spi_slave.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @brief SPI Slave module
4 -------------------------------------------------------------------------------
5 
6 --! Using IEEE library
7 LIBRARY ieee;
8 
9 --! Using IEEE standard logic components
10 USE ieee.std_logic_1164.ALL;
11 
12 --! @brief SPI Slave module
13 --!
14 --! This SPI slave works with SPI signals using the following format:
15 --! - CPOL = 0: sclk low when idle.
16 --! - CPHA = 1: data written on first edge and read on second.
17 --!
18 --! This SPI slave provides a simple SPI shift register for reading/writing
19 --! data. At the start of a transfer when the CS goes low the 'dat_rd_reg_in'
20 --! is latched for transmitting.
21 --!
22 --! At the end of transfer when the CS goes high the module loads the received
23 --! data into 'dat_wr_reg_out' then drives the 'dat_wr_done_out' signal for
24 --! one clock indicating new data has been written to the SPI block.
25 ENTITY spi_slave IS
26  GENERIC (
27  size : natural RANGE 1 TO natural'high --! Size of the SPI data
28  );
29  PORT (
30  clk_in : IN std_logic; --! Clock
31  rst_in : IN std_logic; --! Asynchronous reset
32  spi_cs_in : IN std_logic; --! SPI Chip-select
33  spi_sclk_in : IN std_logic; --! SPI Clock
34  spi_mosi_in : IN std_logic; --! SPI MOSI
35  spi_miso_out : OUT std_logic; --! SPI MISO
36  dat_rd_reg_in : IN std_logic_vector(size - 1 DOWNTO 0); --! Data Read Register value
37  dat_wr_reg_out : OUT std_logic_vector(size - 1 DOWNTO 0); --! Data Write Register value
38  dat_wr_done_out : OUT std_logic --! Data Write Done flag
39  );
40 END ENTITY spi_slave;
41 
42 --! Architecture rtl of spi_slave entity
43 ARCHITECTURE rtl OF spi_slave IS
44 
45  --! Flag indicating a transfer is in progress.
46  SIGNAL in_xfer : std_logic;
47 
48  --! SPI shift register
49  SIGNAL shift : std_logic_vector(size - 1 DOWNTO 0);
50 
51  --! Signal indicating rise of sclk
52  SIGNAL sclk_rise : std_logic;
53 
54  --! Signal indicating fall fo sclk
55  SIGNAL sclk_fall : std_logic;
56 
57 BEGIN
58 
59  --! Edge detector for SCLK signal
60  i_sclk_edge : ENTITY work.edge_detect(rtl)
61  PORT MAP (
62  clk_in => clk_in,
63  rst_in => rst_in,
67  );
68 
69  --! @brief SPI shift process
70  pr_shift : PROCESS (clk_in, rst_in) IS
71  BEGIN
72 
73  IF (rst_in = '1') THEN
74  -- Asynchronous reset of state
75  in_xfer <= '0';
76  shift <= (OTHERS => '0');
77  spi_miso_out <= '0';
78  dat_wr_reg_out <= (OTHERS => '0');
79  dat_wr_done_out <= '0';
80  ELSIF (rising_edge(clk_in)) THEN
81  -- Default dat_wr_done_out to 0 (set only on end transfer)
82  dat_wr_done_out <= '0';
83 
84  -- Handle transfer
85  IF (in_xfer = '0') THEN
86  IF (spi_cs_in = '0') THEN
87  -- Start transfer
88  in_xfer <= '1';
90  END IF;
91  ELSE
92  IF (spi_cs_in = '1') THEN
93  -- End transfer
94  in_xfer <= '0';
96  dat_wr_done_out <= '1';
97  ELSIF (sclk_rise = '1') THEN
98  -- First edge - write data
99  spi_miso_out <= shift(size - 1);
100  ELSIF (sclk_fall = '1') THEN
101  -- Second edge - capture data
102  shift <= shift(size - 2 DOWNTO 0) & spi_mosi_in;
103  END IF;
104  END IF;
105  END IF;
106 
107  END PROCESS pr_shift;
108 
109 END ARCHITECTURE rtl;
out rise_outstd_logic
Rising edge output.
Definition: edge_detect.vhd:21
in spi_mosi_instd_logic
SPI MOSI.
Definition: spi_slave.vhd:34
std_logic_vector( size- 1 DOWNTO 0) shift
SPI shift register.
Definition: spi_slave.vhd:49
in rst_instd_logic
Asynchronous reset.
Definition: edge_detect.vhd:19
Edge detect entity.
Definition: edge_detect.vhd:16
in sig_instd_logic
Input signal.
Definition: edge_detect.vhd:20
out spi_miso_outstd_logic
SPI MISO.
Definition: spi_slave.vhd:35
in clk_instd_logic
Clock.
Definition: spi_slave.vhd:30
_library_ ieeeieee
Using IEEE library.
Definition: spi_slave_tb.vhd:7
std_logic sclk_rise
Signal indicating rise of sclk.
Definition: spi_slave.vhd:52
in spi_sclk_instd_logic
SPI Clock.
Definition: spi_slave.vhd:33
in dat_rd_reg_instd_logic_vector( size- 1 DOWNTO 0)
Data Read Register value.
Definition: spi_slave.vhd:36
SPI Slave module.
Definition: spi_slave.vhd:25
in clk_instd_logic
Clock.
Definition: edge_detect.vhd:18
std_logic sclk_fall
Signal indicating fall fo sclk.
Definition: spi_slave.vhd:55
sizenatural range 1 TO natural'high
Size of the SPI data.
Definition: spi_slave.vhd:28
out dat_wr_done_outstd_logic
Data Write Done flag.
Definition: spi_slave.vhd:39
in rst_instd_logic
Asynchronous reset.
Definition: spi_slave.vhd:31
std_logic in_xfer
Flag indicating a transfer is in progress.
Definition: spi_slave.vhd:46
out fall_outstd_logic
Falling edge output.
Definition: edge_detect.vhd:23
in spi_cs_instd_logic
SPI Chip-select.
Definition: spi_slave.vhd:32
out dat_wr_reg_outstd_logic_vector( size- 1 DOWNTO 0)
Data Write Register value.
Definition: spi_slave.vhd:37