Motion FPGA
Motion FPGA for the MachX02-7000HE Breakout Board
drv8711_spi.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @brief DRV8711 SPI interface
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 DRV8711 spi entity
13 ENTITY drv8711_spi IS
14  PORT (
15  clk_in : IN std_logic; --! Clock
16  rst_in : IN std_logic; --! Asynchronous reset
17  data_send_in : IN std_logic_vector(15 DOWNTO 0); --! Data to send
18  data_recv_out : OUT std_logic_vector(15 DOWNTO 0); --! Data received
19  xfer_adv_in : IN std_logic; --! Transfer advance flag
20  xfer_start_in : IN std_logic; --! Transfer start flag
21  xfer_done_out : OUT std_logic; --! Transfer done flag
22  spi_scs_out : OUT std_logic; --! SPI chip-select
23  spi_sclk_out : OUT std_logic; --! SPI clock
24  spi_mosi_out : OUT std_logic; --! SPI mosi
25  spi_miso_in : IN std_logic --! SPI miso
26  );
27 END ENTITY drv8711_spi;
28 
29 --! Architecture rtl of drv8711_spi entity
30 ARCHITECTURE rtl OF drv8711_spi IS
31 
32  TYPE t_xfer_state IS (xfer_idle, xfer_start_delay, xfer_data, xfer_end_delay, xfer_finish);
33 
34  SIGNAL xfer_state : t_xfer_state;
35 
36  SIGNAL xfer_start : std_logic;
37 
38  SIGNAL data_send : std_logic_vector(15 DOWNTO 0);
39 
40  SIGNAL data_recv : std_logic_vector(15 DOWNTO 0);
41 
42  SIGNAL spi_count : integer RANGE 0 TO 15;
43 
44  SIGNAL spi_sclk : std_logic;
45 
46 BEGIN
47 
48  pr_transfer : PROCESS (clk_in, rst_in) IS
49  BEGIN
50 
51  IF (rst_in = '1') THEN
52  xfer_state <= xfer_idle;
53  xfer_start <= '0';
54  data_send <= (OTHERS => '0');
55  data_recv <= (OTHERS => '0');
56  spi_count <= 0;
57  spi_sclk <= '0';
58  ELSIF (rising_edge(clk_in)) THEN
59  -- Latch the transfer start
60  xfer_start <= xfer_start OR xfer_start_in;
61 
62  -- Default done to low
63  xfer_done_out <= '0';
64 
65  -- Handle transfer when told to advance
66  IF (xfer_adv_in = '1') THEN
67 
68  CASE xfer_state IS
69 
70  WHEN xfer_idle =>
71  -- Detect start request
72  IF (xfer_start = '1') THEN
73  -- Begin transfer
74  data_send <= data_send_in;
75  spi_count <= 0;
76  spi_sclk <= '0';
77 
78  -- Transition to start delay
79  xfer_state <= xfer_start_delay;
80  END IF;
81 
82  WHEN xfer_start_delay =>
83  -- Delay complete, transition to transfer data
84  xfer_state <= xfer_data;
85 
86  WHEN xfer_data =>
87  -- Perform clocked transfer
88  IF (spi_sclk = '0') THEN
89  -- Sclk low->high: capture incoming data
90  data_recv <= data_recv(14 DOWNTO 0) & spi_miso_in;
91  ELSE
92  -- Sclk high->low: advance outbound data
93  data_send <= data_send(14 DOWNTO 0) & '0';
94  END IF;
95 
96  -- Update state on end of bit
97  IF (spi_sclk = '1') THEN
98  IF (spi_count = 15) THEN
99  -- Transfer complete, transition to end delay
100  xfer_state <= xfer_end_delay;
101  ELSE
102  -- Count bytes complete
103  spi_count <= spi_count + 1;
104  END IF;
105  END IF;
106 
107  -- Toggle sclk
108  spi_sclk <= NOT spi_sclk;
109 
110  WHEN xfer_end_delay =>
111  -- Delay complete, transition to finish
112  xfer_state <= xfer_finish;
113 
114  WHEN xfer_finish =>
115  -- Handle finish
116  xfer_start <= '0';
117  xfer_done_out <= '1';
118  xfer_state <= xfer_idle;
119 
120  WHEN OTHERS =>
121  xfer_state <= xfer_idle;
122 
123  END CASE;
124 
125  END IF;
126  END IF;
127 
128  END PROCESS pr_transfer;
129 
130  data_recv_out <= data_recv;
131 
132  spi_scs_out <= '1' WHEN xfer_state = xfer_start_delay ELSE
133  '1' WHEN xfer_state = xfer_data ELSE
134  '1' WHEN xfer_state = xfer_end_delay ELSE
135  '0';
136 
137  spi_sclk_out <= spi_sclk;
138 
139  spi_mosi_out <= data_send(15) WHEN xfer_state = xfer_data ELSE
140  '0';
141 
142 END ARCHITECTURE rtl;
out spi_mosi_outstd_logic
SPI mosi.
Definition: drv8711_spi.vhd:24
in spi_miso_instd_logic
SPI miso.
Definition: drv8711_spi.vhd:26
in xfer_start_instd_logic
Transfer start flag.
Definition: drv8711_spi.vhd:20
DRV8711 spi entity.
Definition: drv8711_spi.vhd:13
in xfer_adv_instd_logic
Transfer advance flag.
Definition: drv8711_spi.vhd:19
in data_send_instd_logic_vector( 15 DOWNTO 0)
Data to send.
Definition: drv8711_spi.vhd:17
out spi_sclk_outstd_logic
SPI clock.
Definition: drv8711_spi.vhd:23
_library_ ieeeieee
Using IEEE library.
in rst_instd_logic
Asynchronous reset.
Definition: drv8711_spi.vhd:16
out data_recv_outstd_logic_vector( 15 DOWNTO 0)
Data received.
Definition: drv8711_spi.vhd:18
out xfer_done_outstd_logic
Transfer done flag.
Definition: drv8711_spi.vhd:21
out spi_scs_outstd_logic
SPI chip-select.
Definition: drv8711_spi.vhd:22
in clk_instd_logic
Clock.
Definition: drv8711_spi.vhd:15