1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
6 |
zero_gravi |
-- # << NEORV32 - Serial Peripheral Interface Controller (SPI) >> #
|
3 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
4 |
|
|
-- # Frame format: 8/16/24/32-bit RTX, MSB or LSB first, 2 clock modes, 8 clock speeds, #
|
5 |
|
|
-- # 8 dedicated CS lines (low-active). Interrupt: SPI_transfer_done #
|
6 |
|
|
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
10 |
|
|
-- # #
|
11 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
12 |
|
|
-- # permitted provided that the following conditions are met: #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
15 |
|
|
-- # conditions and the following disclaimer. #
|
16 |
|
|
-- # #
|
17 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
18 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
19 |
|
|
-- # provided with the distribution. #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
22 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
23 |
|
|
-- # permission. #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
26 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
27 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
28 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
29 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
30 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
31 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
32 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
33 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
34 |
|
|
-- # ********************************************************************************************* #
|
35 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
36 |
|
|
-- #################################################################################################
|
37 |
|
|
|
38 |
|
|
library ieee;
|
39 |
|
|
use ieee.std_logic_1164.all;
|
40 |
|
|
use ieee.numeric_std.all;
|
41 |
|
|
|
42 |
|
|
library neorv32;
|
43 |
|
|
use neorv32.neorv32_package.all;
|
44 |
|
|
|
45 |
|
|
entity neorv32_spi is
|
46 |
|
|
port (
|
47 |
|
|
-- host access --
|
48 |
|
|
clk_i : in std_ulogic; -- global clock line
|
49 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
50 |
|
|
rden_i : in std_ulogic; -- read enable
|
51 |
|
|
wren_i : in std_ulogic; -- write enable
|
52 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
53 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
54 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
55 |
|
|
-- clock generator --
|
56 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
57 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
58 |
|
|
-- com lines --
|
59 |
6 |
zero_gravi |
spi_sck_o : out std_ulogic; -- SPI serial clock
|
60 |
|
|
spi_sdo_o : out std_ulogic; -- controller data out, peripheral data in
|
61 |
|
|
spi_sdi_i : in std_ulogic; -- controller data in, peripheral data out
|
62 |
2 |
zero_gravi |
spi_csn_o : out std_ulogic_vector(07 downto 0); -- SPI CS
|
63 |
|
|
-- interrupt --
|
64 |
|
|
spi_irq_o : out std_ulogic -- transmission done interrupt
|
65 |
|
|
);
|
66 |
|
|
end neorv32_spi;
|
67 |
|
|
|
68 |
|
|
architecture neorv32_spi_rtl of neorv32_spi is
|
69 |
|
|
|
70 |
|
|
-- IO space: module base address --
|
71 |
|
|
constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
|
72 |
|
|
constant lo_abb_c : natural := index_size_f(spi_size_c); -- low address boundary bit
|
73 |
|
|
|
74 |
|
|
-- control reg bits --
|
75 |
|
|
constant ctrl_spi_cs0_c : natural := 0; -- r/w: spi CS 0
|
76 |
|
|
constant ctrl_spi_cs1_c : natural := 1; -- r/w: spi CS 1
|
77 |
|
|
constant ctrl_spi_cs2_c : natural := 2; -- r/w: spi CS 2
|
78 |
|
|
constant ctrl_spi_cs3_c : natural := 3; -- r/w: spi CS 3
|
79 |
|
|
constant ctrl_spi_cs4_c : natural := 4; -- r/w: spi CS 4
|
80 |
|
|
constant ctrl_spi_cs5_c : natural := 5; -- r/w: spi CS 5
|
81 |
|
|
constant ctrl_spi_cs6_c : natural := 6; -- r/w: spi CS 6
|
82 |
|
|
constant ctrl_spi_cs7_c : natural := 7; -- r/w: spi CS 7
|
83 |
|
|
--
|
84 |
|
|
constant ctrl_spi_en_c : natural := 8; -- r/w: spi enable
|
85 |
|
|
constant ctrl_spi_cpha_c : natural := 9; -- r/w: spi clock phase
|
86 |
|
|
constant ctrl_spi_prsc0_c : natural := 10; -- r/w: spi prescaler select bit 0
|
87 |
|
|
constant ctrl_spi_prsc1_c : natural := 11; -- r/w: spi prescaler select bit 1
|
88 |
|
|
constant ctrl_spi_prsc2_c : natural := 12; -- r/w: spi prescaler select bit 2
|
89 |
|
|
constant ctrl_spi_dir_c : natural := 13; -- r/w: shift direction (0: MSB first, 1: LSB first)
|
90 |
|
|
constant ctrl_spi_size0_c : natural := 14; -- r/w: data size (00: 8-bit, 01: 16-bit)
|
91 |
|
|
constant ctrl_spi_size1_c : natural := 15; -- r/w: data size (10: 24-bit, 11: 32-bit)
|
92 |
|
|
--
|
93 |
|
|
constant ctrl_spi_irq_en_c : natural := 16; -- r/w: spi transmission done interrupt enable
|
94 |
|
|
--
|
95 |
|
|
constant ctrl_spi_busy_c : natural := 31; -- r/-: spi transceiver is busy
|
96 |
|
|
|
97 |
|
|
-- access control --
|
98 |
|
|
signal acc_en : std_ulogic; -- module access enable
|
99 |
|
|
signal addr : std_ulogic_vector(31 downto 0); -- access address
|
100 |
|
|
signal wren : std_ulogic; -- word write enable
|
101 |
|
|
signal rden : std_ulogic; -- read enable
|
102 |
|
|
|
103 |
|
|
-- accessible regs --
|
104 |
|
|
signal ctrl : std_ulogic_vector(16 downto 0);
|
105 |
|
|
signal tx_data : std_ulogic_vector(31 downto 0);
|
106 |
|
|
|
107 |
|
|
-- clock generator --
|
108 |
|
|
signal spi_clk : std_ulogic;
|
109 |
|
|
|
110 |
|
|
-- spi transceiver --
|
111 |
|
|
signal spi_start : std_ulogic;
|
112 |
|
|
signal spi_busy : std_ulogic;
|
113 |
|
|
signal spi_state0 : std_ulogic;
|
114 |
|
|
signal spi_state1 : std_ulogic;
|
115 |
|
|
signal spi_rtx_sreg : std_ulogic_vector(31 downto 0);
|
116 |
|
|
signal spi_rx_data : std_ulogic_vector(31 downto 0);
|
117 |
|
|
signal spi_bitcnt : std_ulogic_vector(05 downto 0);
|
118 |
6 |
zero_gravi |
signal spi_sdi_ff0 : std_ulogic;
|
119 |
|
|
signal spi_sdi_ff1 : std_ulogic;
|
120 |
2 |
zero_gravi |
|
121 |
|
|
begin
|
122 |
|
|
|
123 |
|
|
-- Access Control -------------------------------------------------------------------------
|
124 |
|
|
-- -------------------------------------------------------------------------------------------
|
125 |
|
|
acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = spi_base_c(hi_abb_c downto lo_abb_c)) else '0';
|
126 |
|
|
addr <= spi_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
127 |
|
|
wren <= acc_en and wren_i;
|
128 |
|
|
rden <= acc_en and rden_i;
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
-- Read/Write Access ----------------------------------------------------------------------
|
132 |
|
|
-- -------------------------------------------------------------------------------------------
|
133 |
|
|
rw_access: process(clk_i)
|
134 |
|
|
begin
|
135 |
|
|
if rising_edge(clk_i) then
|
136 |
|
|
ack_o <= acc_en and (rden_i or wren_i);
|
137 |
|
|
spi_start <= '0';
|
138 |
|
|
-- write access --
|
139 |
|
|
if (wren = '1') then
|
140 |
|
|
-- control regsiter --
|
141 |
|
|
if (addr = spi_ctrl_addr_c) then
|
142 |
22 |
zero_gravi |
ctrl <= data_i(ctrl'left downto 0);
|
143 |
2 |
zero_gravi |
end if;
|
144 |
|
|
-- data regsiter --
|
145 |
|
|
if (addr = spi_rtx_addr_c) then
|
146 |
22 |
zero_gravi |
tx_data <= data_i;
|
147 |
2 |
zero_gravi |
spi_start <= '1';
|
148 |
|
|
end if;
|
149 |
|
|
end if;
|
150 |
|
|
-- read access --
|
151 |
|
|
data_o <= (others => '0');
|
152 |
|
|
if (rden = '1') then
|
153 |
|
|
if (addr = spi_ctrl_addr_c) then
|
154 |
|
|
data_o(ctrl_spi_cs0_c) <= ctrl(ctrl_spi_cs0_c);
|
155 |
|
|
data_o(ctrl_spi_cs1_c) <= ctrl(ctrl_spi_cs1_c);
|
156 |
|
|
data_o(ctrl_spi_cs2_c) <= ctrl(ctrl_spi_cs2_c);
|
157 |
|
|
data_o(ctrl_spi_cs3_c) <= ctrl(ctrl_spi_cs3_c);
|
158 |
|
|
data_o(ctrl_spi_cs4_c) <= ctrl(ctrl_spi_cs4_c);
|
159 |
|
|
data_o(ctrl_spi_cs5_c) <= ctrl(ctrl_spi_cs5_c);
|
160 |
|
|
data_o(ctrl_spi_cs6_c) <= ctrl(ctrl_spi_cs6_c);
|
161 |
|
|
data_o(ctrl_spi_cs7_c) <= ctrl(ctrl_spi_cs7_c);
|
162 |
|
|
--
|
163 |
|
|
data_o(ctrl_spi_en_c) <= ctrl(ctrl_spi_en_c);
|
164 |
|
|
data_o(ctrl_spi_cpha_c) <= ctrl(ctrl_spi_cpha_c);
|
165 |
|
|
data_o(ctrl_spi_prsc0_c) <= ctrl(ctrl_spi_prsc0_c);
|
166 |
|
|
data_o(ctrl_spi_prsc1_c) <= ctrl(ctrl_spi_prsc1_c);
|
167 |
|
|
data_o(ctrl_spi_prsc2_c) <= ctrl(ctrl_spi_prsc2_c);
|
168 |
|
|
data_o(ctrl_spi_dir_c) <= ctrl(ctrl_spi_dir_c);
|
169 |
|
|
data_o(ctrl_spi_size0_c) <= ctrl(ctrl_spi_size0_c);
|
170 |
|
|
data_o(ctrl_spi_size1_c) <= ctrl(ctrl_spi_size1_c);
|
171 |
|
|
--
|
172 |
|
|
data_o(ctrl_spi_irq_en_c) <= ctrl(ctrl_spi_irq_en_c);
|
173 |
|
|
--
|
174 |
|
|
data_o(ctrl_spi_busy_c) <= spi_busy;
|
175 |
|
|
else -- spi_rtx_addr_c
|
176 |
|
|
data_o <= spi_rx_data;
|
177 |
|
|
end if;
|
178 |
|
|
end if;
|
179 |
|
|
end if;
|
180 |
|
|
end process rw_access;
|
181 |
|
|
|
182 |
|
|
-- direct CS (output is low-active) --
|
183 |
|
|
spi_csn_o(0) <= '0' when (ctrl(ctrl_spi_cs0_c) = '1') else '1';
|
184 |
|
|
spi_csn_o(1) <= '0' when (ctrl(ctrl_spi_cs1_c) = '1') else '1';
|
185 |
|
|
spi_csn_o(2) <= '0' when (ctrl(ctrl_spi_cs2_c) = '1') else '1';
|
186 |
|
|
spi_csn_o(3) <= '0' when (ctrl(ctrl_spi_cs3_c) = '1') else '1';
|
187 |
|
|
spi_csn_o(4) <= '0' when (ctrl(ctrl_spi_cs4_c) = '1') else '1';
|
188 |
|
|
spi_csn_o(5) <= '0' when (ctrl(ctrl_spi_cs5_c) = '1') else '1';
|
189 |
|
|
spi_csn_o(6) <= '0' when (ctrl(ctrl_spi_cs6_c) = '1') else '1';
|
190 |
|
|
spi_csn_o(7) <= '0' when (ctrl(ctrl_spi_cs7_c) = '1') else '1';
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
-- Clock Selection ------------------------------------------------------------------------
|
194 |
|
|
-- -------------------------------------------------------------------------------------------
|
195 |
|
|
-- clock generator enable --
|
196 |
|
|
clkgen_en_o <= ctrl(ctrl_spi_en_c);
|
197 |
|
|
|
198 |
|
|
-- spi clock select --
|
199 |
|
|
spi_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_spi_prsc2_c downto ctrl_spi_prsc0_c))));
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
-- SPI Transceiver ------------------------------------------------------------------------
|
203 |
|
|
-- -------------------------------------------------------------------------------------------
|
204 |
|
|
spi_rtx_unit: process(clk_i)
|
205 |
|
|
begin
|
206 |
|
|
if rising_edge(clk_i) then
|
207 |
6 |
zero_gravi |
-- input (sdi) synchronizer --
|
208 |
|
|
spi_sdi_ff0 <= spi_sdi_i;
|
209 |
|
|
spi_sdi_ff1 <= spi_sdi_ff0;
|
210 |
2 |
zero_gravi |
|
211 |
|
|
-- serial engine --
|
212 |
|
|
spi_irq_o <= '0';
|
213 |
|
|
if (spi_state0 = '0') or (ctrl(ctrl_spi_en_c) = '0') then -- idle or disabled
|
214 |
|
|
case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
|
215 |
|
|
when "00" => spi_bitcnt <= "001000"; -- 8-bit mode
|
216 |
|
|
when "01" => spi_bitcnt <= "010000"; -- 16-bit mode
|
217 |
|
|
when "10" => spi_bitcnt <= "011000"; -- 24-bit mode
|
218 |
|
|
when others => spi_bitcnt <= "100000"; -- 32-bit mode
|
219 |
|
|
end case;
|
220 |
|
|
spi_state1 <= '0';
|
221 |
6 |
zero_gravi |
spi_sdo_o <= '0';
|
222 |
|
|
spi_sck_o <= '0';
|
223 |
2 |
zero_gravi |
if (ctrl(ctrl_spi_en_c) = '0') then -- disabled
|
224 |
|
|
spi_busy <= '0';
|
225 |
|
|
elsif (spi_start = '1') then -- start new transmission
|
226 |
|
|
case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
|
227 |
|
|
when "00" => spi_rtx_sreg <= tx_data(07 downto 0) & x"000000"; -- 8-bit mode
|
228 |
|
|
when "01" => spi_rtx_sreg <= tx_data(15 downto 0) & x"0000"; -- 16-bit mode
|
229 |
|
|
when "10" => spi_rtx_sreg <= tx_data(23 downto 0) & x"00"; -- 24-bit mode
|
230 |
|
|
when others => spi_rtx_sreg <= tx_data(31 downto 0); -- 32-bit mode
|
231 |
|
|
end case;
|
232 |
|
|
spi_busy <= '1';
|
233 |
|
|
end if;
|
234 |
|
|
spi_state0 <= spi_busy and spi_clk; -- start with next new clock pulse
|
235 |
|
|
|
236 |
|
|
else -- transmission in progress
|
237 |
|
|
if (spi_state1 = '0') then -- first half of transmission
|
238 |
|
|
|
239 |
6 |
zero_gravi |
spi_sck_o <= ctrl(ctrl_spi_cpha_c);
|
240 |
2 |
zero_gravi |
if (ctrl(ctrl_spi_dir_c) = '0') then
|
241 |
6 |
zero_gravi |
spi_sdo_o <= spi_rtx_sreg(31); -- MSB first
|
242 |
2 |
zero_gravi |
else
|
243 |
6 |
zero_gravi |
spi_sdo_o <= spi_rtx_sreg(0); -- LSB first
|
244 |
2 |
zero_gravi |
end if;
|
245 |
|
|
if (spi_clk = '1') then
|
246 |
|
|
spi_state1 <= '1';
|
247 |
|
|
if (ctrl(ctrl_spi_cpha_c) = '0') then
|
248 |
|
|
if (ctrl(ctrl_spi_dir_c) = '0') then
|
249 |
6 |
zero_gravi |
spi_rtx_sreg <= spi_rtx_sreg(30 downto 0) & spi_sdi_ff1; -- MSB first
|
250 |
2 |
zero_gravi |
else
|
251 |
6 |
zero_gravi |
spi_rtx_sreg <= spi_sdi_ff1 & spi_rtx_sreg(31 downto 1); -- LSB first
|
252 |
2 |
zero_gravi |
end if;
|
253 |
|
|
end if;
|
254 |
|
|
spi_bitcnt <= std_ulogic_vector(unsigned(spi_bitcnt) - 1);
|
255 |
|
|
end if;
|
256 |
|
|
else -- second half of transmission
|
257 |
|
|
|
258 |
6 |
zero_gravi |
spi_sck_o <= not ctrl(ctrl_spi_cpha_c);
|
259 |
2 |
zero_gravi |
if (spi_clk = '1') then
|
260 |
|
|
spi_state1 <= '0';
|
261 |
|
|
if (ctrl(ctrl_spi_cpha_c) = '1') then
|
262 |
|
|
if (ctrl(ctrl_spi_dir_c) = '0') then
|
263 |
6 |
zero_gravi |
spi_rtx_sreg <= spi_rtx_sreg(30 downto 0) & spi_sdi_ff1; -- MSB first
|
264 |
2 |
zero_gravi |
else
|
265 |
6 |
zero_gravi |
spi_rtx_sreg <= spi_sdi_ff1 & spi_rtx_sreg(31 downto 1); -- LSB first
|
266 |
2 |
zero_gravi |
end if;
|
267 |
|
|
end if;
|
268 |
|
|
if (spi_bitcnt = "000000") then
|
269 |
|
|
spi_state0 <= '0';
|
270 |
|
|
spi_busy <= '0';
|
271 |
|
|
spi_irq_o <= ctrl(ctrl_spi_irq_en_c);
|
272 |
|
|
end if;
|
273 |
|
|
end if;
|
274 |
|
|
end if;
|
275 |
|
|
end if;
|
276 |
|
|
end if;
|
277 |
|
|
end process spi_rtx_unit;
|
278 |
|
|
|
279 |
|
|
-- SPI receiver output --
|
280 |
|
|
spi_rx_output: process(ctrl, spi_rtx_sreg)
|
281 |
|
|
begin
|
282 |
|
|
case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
|
283 |
|
|
when "00" => spi_rx_data <= x"000000" & spi_rtx_sreg(7 downto 0); -- 8-bit mode
|
284 |
|
|
when "01" => spi_rx_data <= x"0000" & spi_rtx_sreg(15 downto 0); -- 16-bit mode
|
285 |
|
|
when "10" => spi_rx_data <= x"00" & spi_rtx_sreg(23 downto 0); -- 24-bit mode
|
286 |
|
|
when others => spi_rx_data <= spi_rtx_sreg(31 downto 0); -- 32-bit mode
|
287 |
|
|
end case;
|
288 |
|
|
end process spi_rx_output;
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
end neorv32_spi_rtl;
|