OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_spi.vhd] - Blame information for rev 68

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2 6 zero_gravi
-- # << NEORV32 - Serial Peripheral Interface Controller (SPI) >>                                  #
3 2 zero_gravi
-- # ********************************************************************************************* #
4 36 zero_gravi
-- # Frame format: 8/16/24/32-bit receive/transmit data, always MSB first, 2 clock modes,          #
5 50 zero_gravi
-- # 8 pre-scaled clocks (derived from system clock), 8 dedicated chip-select lines (low-active).  #
6 68 zero_gravi
-- # Interrupt: "transfer done"                                                                    #
7 2 zero_gravi
-- # ********************************************************************************************* #
8
-- # BSD 3-Clause License                                                                          #
9
-- #                                                                                               #
10 48 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
11 2 zero_gravi
-- #                                                                                               #
12
-- # Redistribution and use in source and binary forms, with or without modification, are          #
13
-- # permitted provided that the following conditions are met:                                     #
14
-- #                                                                                               #
15
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
16
-- #    conditions and the following disclaimer.                                                   #
17
-- #                                                                                               #
18
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
19
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
20
-- #    provided with the distribution.                                                            #
21
-- #                                                                                               #
22
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
23
-- #    endorse or promote products derived from this software without specific prior written      #
24
-- #    permission.                                                                                #
25
-- #                                                                                               #
26
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
27
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
28
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
29
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
30
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
31
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
32
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
33
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
34
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
35
-- # ********************************************************************************************* #
36
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
37
-- #################################################################################################
38
 
39
library ieee;
40
use ieee.std_logic_1164.all;
41
use ieee.numeric_std.all;
42
 
43
library neorv32;
44
use neorv32.neorv32_package.all;
45
 
46
entity neorv32_spi is
47
  port (
48
    -- host access --
49
    clk_i       : in  std_ulogic; -- global clock line
50
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
51
    rden_i      : in  std_ulogic; -- read enable
52
    wren_i      : in  std_ulogic; -- write enable
53
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
54
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
55
    ack_o       : out std_ulogic; -- transfer acknowledge
56
    -- clock generator --
57
    clkgen_en_o : out std_ulogic; -- enable clock generator
58
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
59
    -- com lines --
60 6 zero_gravi
    spi_sck_o   : out std_ulogic; -- SPI serial clock
61
    spi_sdo_o   : out std_ulogic; -- controller data out, peripheral data in
62
    spi_sdi_i   : in  std_ulogic; -- controller data in, peripheral data out
63 2 zero_gravi
    spi_csn_o   : out std_ulogic_vector(07 downto 0); -- SPI CS
64
    -- interrupt --
65 48 zero_gravi
    irq_o       : out std_ulogic -- transmission done interrupt
66 2 zero_gravi
  );
67
end neorv32_spi;
68
 
69
architecture neorv32_spi_rtl of neorv32_spi is
70
 
71
  -- IO space: module base address --
72
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
73
  constant lo_abb_c : natural := index_size_f(spi_size_c); -- low address boundary bit
74
 
75 65 zero_gravi
  -- control register --
76 66 zero_gravi
  constant ctrl_cs0_c   : natural :=  0; -- r/w: spi CS 0
77
  constant ctrl_cs1_c   : natural :=  1; -- r/w: spi CS 1
78
  constant ctrl_cs2_c   : natural :=  2; -- r/w: spi CS 2
79
  constant ctrl_cs3_c   : natural :=  3; -- r/w: spi CS 3
80
  constant ctrl_cs4_c   : natural :=  4; -- r/w: spi CS 4
81
  constant ctrl_cs5_c   : natural :=  5; -- r/w: spi CS 5
82
  constant ctrl_cs6_c   : natural :=  6; -- r/w: spi CS 6
83
  constant ctrl_cs7_c   : natural :=  7; -- r/w: spi CS 7
84 2 zero_gravi
  --
85 66 zero_gravi
  constant ctrl_en_c    : natural :=  8; -- r/w: spi enable
86
  constant ctrl_cpha_c  : natural :=  9; -- r/w: spi clock phase
87
  constant ctrl_prsc0_c : natural := 10; -- r/w: spi prescaler select bit 0
88
  constant ctrl_prsc1_c : natural := 11; -- r/w: spi prescaler select bit 1
89
  constant ctrl_prsc2_c : natural := 12; -- r/w: spi prescaler select bit 2
90
  constant ctrl_size0_c : natural := 13; -- r/w: data size lsb (00:  8-bit, 01: 16-bit)
91
  constant ctrl_size1_c : natural := 14; -- r/w: data size msb (10: 24-bit, 11: 32-bit)
92
  constant ctrl_cpol_c  : natural := 15; -- r/w: spi clock polarity
93 2 zero_gravi
  --
94 66 zero_gravi
  constant ctrl_busy_c  : natural := 31; -- r/-: spi transceiver is busy
95 65 zero_gravi
  --
96
  signal ctrl : std_ulogic_vector(15 downto 0);
97 2 zero_gravi
 
98
  -- access control --
99
  signal acc_en : std_ulogic; -- module access enable
100
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
101
  signal wren   : std_ulogic; -- word write enable
102
  signal rden   : std_ulogic; -- read enable
103
 
104
  -- clock generator --
105 65 zero_gravi
  signal spi_clk_en : std_ulogic;
106 2 zero_gravi
 
107
  -- spi transceiver --
108 65 zero_gravi
  type rtx_engine_t is record
109 66 zero_gravi
    state    : std_ulogic_vector(02 downto 0);
110 65 zero_gravi
    busy     : std_ulogic;
111 66 zero_gravi
    start    : std_ulogic;
112
    sreg     : std_ulogic_vector(31 downto 0);
113 65 zero_gravi
    bitcnt   : std_ulogic_vector(05 downto 0);
114
    bytecnt  : std_ulogic_vector(02 downto 0);
115 66 zero_gravi
    sdi_sync : std_ulogic_vector(01 downto 0);
116 65 zero_gravi
  end record;
117
  signal rtx_engine : rtx_engine_t;
118 2 zero_gravi
 
119 68 zero_gravi
  -- interrupt generator --
120
  type irq_t is record
121
    pending : std_ulogic; -- pending interrupt request
122
    set     : std_ulogic;
123
    clr     : std_ulogic;
124
  end record;
125
  signal irq : irq_t;
126
 
127 2 zero_gravi
begin
128
 
129
  -- Access Control -------------------------------------------------------------------------
130
  -- -------------------------------------------------------------------------------------------
131
  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';
132
  addr   <= spi_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
133
  wren   <= acc_en and wren_i;
134
  rden   <= acc_en and rden_i;
135
 
136
 
137
  -- Read/Write Access ----------------------------------------------------------------------
138
  -- -------------------------------------------------------------------------------------------
139
  rw_access: process(clk_i)
140
  begin
141
    if rising_edge(clk_i) then
142 65 zero_gravi
      -- bus access acknowledge --
143
      ack_o <= rden or wren;
144
 
145 36 zero_gravi
      -- write access --
146 2 zero_gravi
      if (wren = '1') then
147 65 zero_gravi
        if (addr = spi_ctrl_addr_c) then -- control register
148 66 zero_gravi
          ctrl(ctrl_cs0_c)   <= data_i(ctrl_cs0_c);
149
          ctrl(ctrl_cs1_c)   <= data_i(ctrl_cs1_c);
150
          ctrl(ctrl_cs2_c)   <= data_i(ctrl_cs2_c);
151
          ctrl(ctrl_cs3_c)   <= data_i(ctrl_cs3_c);
152
          ctrl(ctrl_cs4_c)   <= data_i(ctrl_cs4_c);
153
          ctrl(ctrl_cs5_c)   <= data_i(ctrl_cs5_c);
154
          ctrl(ctrl_cs6_c)   <= data_i(ctrl_cs6_c);
155
          ctrl(ctrl_cs7_c)   <= data_i(ctrl_cs7_c);
156 65 zero_gravi
          --
157 66 zero_gravi
          ctrl(ctrl_en_c)    <= data_i(ctrl_en_c);
158
          ctrl(ctrl_cpha_c)  <= data_i(ctrl_cpha_c);
159
          ctrl(ctrl_prsc0_c) <= data_i(ctrl_prsc0_c);
160
          ctrl(ctrl_prsc1_c) <= data_i(ctrl_prsc1_c);
161
          ctrl(ctrl_prsc2_c) <= data_i(ctrl_prsc2_c);
162
          ctrl(ctrl_size0_c) <= data_i(ctrl_size0_c);
163
          ctrl(ctrl_size1_c) <= data_i(ctrl_size1_c);
164
          ctrl(ctrl_cpol_c)  <= data_i(ctrl_cpol_c);
165 2 zero_gravi
        end if;
166
      end if;
167 65 zero_gravi
 
168 2 zero_gravi
      -- read access --
169
      data_o <= (others => '0');
170
      if (rden = '1') then
171 65 zero_gravi
        if (addr = spi_ctrl_addr_c) then -- control register
172 66 zero_gravi
          data_o(ctrl_cs0_c)   <= ctrl(ctrl_cs0_c);
173
          data_o(ctrl_cs1_c)   <= ctrl(ctrl_cs1_c);
174
          data_o(ctrl_cs2_c)   <= ctrl(ctrl_cs2_c);
175
          data_o(ctrl_cs3_c)   <= ctrl(ctrl_cs3_c);
176
          data_o(ctrl_cs4_c)   <= ctrl(ctrl_cs4_c);
177
          data_o(ctrl_cs5_c)   <= ctrl(ctrl_cs5_c);
178
          data_o(ctrl_cs6_c)   <= ctrl(ctrl_cs6_c);
179
          data_o(ctrl_cs7_c)   <= ctrl(ctrl_cs7_c);
180 2 zero_gravi
          --
181 66 zero_gravi
          data_o(ctrl_en_c)    <= ctrl(ctrl_en_c);
182
          data_o(ctrl_cpha_c)  <= ctrl(ctrl_cpha_c);
183
          data_o(ctrl_prsc0_c) <= ctrl(ctrl_prsc0_c);
184
          data_o(ctrl_prsc1_c) <= ctrl(ctrl_prsc1_c);
185
          data_o(ctrl_prsc2_c) <= ctrl(ctrl_prsc2_c);
186
          data_o(ctrl_size0_c) <= ctrl(ctrl_size0_c);
187
          data_o(ctrl_size1_c) <= ctrl(ctrl_size1_c);
188
          data_o(ctrl_cpol_c)  <= ctrl(ctrl_cpol_c);
189 2 zero_gravi
          --
190 66 zero_gravi
          data_o(ctrl_busy_c)  <= rtx_engine.busy;
191 65 zero_gravi
        else -- data register (spi_rtx_addr_c)
192 66 zero_gravi
          data_o <= rtx_engine.sreg;
193 2 zero_gravi
        end if;
194
      end if;
195
    end if;
196
  end process rw_access;
197
 
198 65 zero_gravi
  -- direct chip-select (CS), output is low-active --  
199 66 zero_gravi
  spi_csn_o(7 downto 0) <= not ctrl(ctrl_cs7_c downto ctrl_cs0_c);
200 2 zero_gravi
 
201 66 zero_gravi
  -- trigger new SPI transmission --
202
  rtx_engine.start <= '1' when (wren = '1') and (addr = spi_rtx_addr_c) else '0';
203 2 zero_gravi
 
204 66 zero_gravi
 
205
  -- Clock Selection ------------------------------------------------------------------------
206
  -- -------------------------------------------------------------------------------------------
207
  clkgen_en_o <= ctrl(ctrl_en_c); -- clock generator enable
208
  spi_clk_en  <= clkgen_i(to_integer(unsigned(ctrl(ctrl_prsc2_c downto ctrl_prsc0_c)))); -- clock select
209
 
210
 
211 65 zero_gravi
  -- Transmission Data Size -----------------------------------------------------------------
212
  -- -------------------------------------------------------------------------------------------
213
  data_size: process(ctrl)
214
  begin
215 66 zero_gravi
    case ctrl(ctrl_size1_c downto ctrl_size0_c) is
216 65 zero_gravi
      when "00"   => rtx_engine.bytecnt <= "001"; -- 1-byte mode
217
      when "01"   => rtx_engine.bytecnt <= "010"; -- 2-byte mode
218
      when "10"   => rtx_engine.bytecnt <= "011"; -- 3-byte mode
219
      when others => rtx_engine.bytecnt <= "100"; -- 4-byte mode
220
    end case;
221
  end process data_size;
222
 
223
 
224 2 zero_gravi
  -- SPI Transceiver ------------------------------------------------------------------------
225
  -- -------------------------------------------------------------------------------------------
226
  spi_rtx_unit: process(clk_i)
227
  begin
228
    if rising_edge(clk_i) then
229 6 zero_gravi
      -- input (sdi) synchronizer --
230 66 zero_gravi
      rtx_engine.sdi_sync <= rtx_engine.sdi_sync(0) & spi_sdi_i;
231 2 zero_gravi
 
232 66 zero_gravi
      -- output (sdo) buffer --
233
      case ctrl(ctrl_size1_c downto ctrl_size0_c) is
234
        when "00"   => spi_sdo_o <= rtx_engine.sreg(07); -- 8-bit mode
235
        when "01"   => spi_sdo_o <= rtx_engine.sreg(15); -- 16-bit mode
236
        when "10"   => spi_sdo_o <= rtx_engine.sreg(23); -- 24-bit mode
237
        when others => spi_sdo_o <= rtx_engine.sreg(31); -- 32-bit mode
238
      end case;
239
 
240
      -- defaults --
241
      spi_sck_o <= ctrl(ctrl_cpol_c);
242 68 zero_gravi
      irq.set   <= '0';
243 66 zero_gravi
 
244 2 zero_gravi
      -- serial engine --
245 66 zero_gravi
      rtx_engine.state(2) <= ctrl(ctrl_en_c);
246
      case rtx_engine.state is
247 2 zero_gravi
 
248 66 zero_gravi
        when "100" => -- enabled but idle, waiting for new transmission trigger
249
        -- ------------------------------------------------------------
250
          rtx_engine.bitcnt <= (others => '0');
251
          if (rtx_engine.start = '1') then -- trigger new transmission
252
            rtx_engine.sreg <= data_i;
253
            rtx_engine.state(1 downto 0) <= "01";
254
          end if;
255 65 zero_gravi
 
256 66 zero_gravi
        when "101" => -- start with next new clock pulse
257
        -- ------------------------------------------------------------
258 65 zero_gravi
          if (spi_clk_en = '1') then
259 66 zero_gravi
            rtx_engine.state(1 downto 0) <= "10";
260
          end if;
261
 
262
        when "110" => -- first half of bit transmission
263
        -- ------------------------------------------------------------
264
          spi_sck_o <= ctrl(ctrl_cpha_c) xor ctrl(ctrl_cpol_c);
265
          if (spi_clk_en = '1') then
266 65 zero_gravi
            rtx_engine.bitcnt <= std_ulogic_vector(unsigned(rtx_engine.bitcnt) + 1);
267 66 zero_gravi
            rtx_engine.state(1 downto 0) <= "11";
268 2 zero_gravi
          end if;
269 36 zero_gravi
 
270 66 zero_gravi
        when "111" => -- second half of bit transmission
271
        -- ------------------------------------------------------------
272
          spi_sck_o <= ctrl(ctrl_cpha_c) xnor ctrl(ctrl_cpol_c);
273 65 zero_gravi
          if (spi_clk_en = '1') then
274 66 zero_gravi
            rtx_engine.sreg <= rtx_engine.sreg(30 downto 0) & rtx_engine.sdi_sync(rtx_engine.sdi_sync'left);
275
            if (rtx_engine.bitcnt(5 downto 3) = rtx_engine.bytecnt) then -- all bits transferred?
276 68 zero_gravi
              irq.set <= '1'; -- interrupt!
277
              rtx_engine.state(1 downto 0) <= "00"; -- transmission done
278 66 zero_gravi
            else
279
              rtx_engine.state(1 downto 0) <= "10";
280 2 zero_gravi
            end if;
281
          end if;
282 65 zero_gravi
 
283 66 zero_gravi
        when others => -- "0--": SPI deactivated
284
        -- ------------------------------------------------------------
285
          rtx_engine.state(1 downto 0) <= "00";
286
 
287
      end case;
288 2 zero_gravi
    end if;
289
  end process spi_rtx_unit;
290
 
291 66 zero_gravi
  -- busy flag --
292
  rtx_engine.busy <= '0' when (rtx_engine.state(1 downto 0) = "00") else '1';
293 36 zero_gravi
 
294 66 zero_gravi
 
295 68 zero_gravi
  -- Interrupt Generator --------------------------------------------------------------------
296 36 zero_gravi
  -- -------------------------------------------------------------------------------------------
297 68 zero_gravi
  irq_generator: process(clk_i)
298
  begin
299
    if rising_edge(clk_i) then
300
      if (ctrl(ctrl_en_c) = '0') then
301
        irq.pending <= '0';
302
      else
303
        if (irq.set = '1') then
304
          irq.pending <= '1';
305
        elsif (irq.clr = '1') then
306
          irq.pending <= '0';
307
        end if;
308
      end if;
309
    end if;
310
  end process irq_generator;
311 2 zero_gravi
 
312 68 zero_gravi
  -- IRQ request to CPU --
313
  irq_o <= irq.pending;
314 2 zero_gravi
 
315 68 zero_gravi
  -- IRQ acknowledge --
316
  irq.clr <= '1' when ((rden = '1') and (addr = spi_rtx_addr_c)) or (wren = '1') else '0'; -- read data register OR write data/control register
317
 
318
 
319 2 zero_gravi
end neorv32_spi_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.