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

Subversion Repositories neorv32

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

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 36 zero_gravi
-- # Interrupt: SPI_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
  -- control reg bits --
76
  constant ctrl_spi_cs0_c    : natural :=  0; -- r/w: spi CS 0
77
  constant ctrl_spi_cs1_c    : natural :=  1; -- r/w: spi CS 1
78
  constant ctrl_spi_cs2_c    : natural :=  2; -- r/w: spi CS 2
79
  constant ctrl_spi_cs3_c    : natural :=  3; -- r/w: spi CS 3
80
  constant ctrl_spi_cs4_c    : natural :=  4; -- r/w: spi CS 4
81
  constant ctrl_spi_cs5_c    : natural :=  5; -- r/w: spi CS 5
82
  constant ctrl_spi_cs6_c    : natural :=  6; -- r/w: spi CS 6
83
  constant ctrl_spi_cs7_c    : natural :=  7; -- r/w: spi CS 7
84
  --
85
  constant ctrl_spi_en_c     : natural :=  8; -- r/w: spi enable
86
  constant ctrl_spi_cpha_c   : natural :=  9; -- r/w: spi clock phase
87
  constant ctrl_spi_prsc0_c  : natural := 10; -- r/w: spi prescaler select bit 0
88
  constant ctrl_spi_prsc1_c  : natural := 11; -- r/w: spi prescaler select bit 1
89
  constant ctrl_spi_prsc2_c  : natural := 12; -- r/w: spi prescaler select bit 2
90 36 zero_gravi
  constant ctrl_spi_size0_c  : natural := 13; -- r/w: data size (00:  8-bit, 01: 16-bit)
91
  constant ctrl_spi_size1_c  : natural := 14; -- r/w: data size (10: 24-bit, 11: 32-bit)
92 2 zero_gravi
  --
93
  constant ctrl_spi_busy_c   : natural := 31; -- r/-: spi transceiver is busy
94
 
95
  -- access control --
96
  signal acc_en : std_ulogic; -- module access enable
97
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
98
  signal wren   : std_ulogic; -- word write enable
99
  signal rden   : std_ulogic; -- read enable
100
 
101
  -- accessible regs --
102 48 zero_gravi
  signal ctrl        : std_ulogic_vector(14 downto 0);
103 36 zero_gravi
  signal tx_data_reg : std_ulogic_vector(31 downto 0);
104
  signal rx_data     : std_ulogic_vector(31 downto 0);
105 2 zero_gravi
 
106
  -- clock generator --
107
  signal spi_clk : std_ulogic;
108
 
109
  -- spi transceiver --
110 36 zero_gravi
  signal spi_start      : std_ulogic;
111
  signal spi_busy       : std_ulogic;
112
  signal spi_state0     : std_ulogic;
113
  signal spi_state1     : std_ulogic;
114
  signal spi_rtx_sreg   : std_ulogic_vector(31 downto 0);
115
  signal spi_rx_data    : std_ulogic_vector(31 downto 0);
116
  signal spi_bitcnt     : std_ulogic_vector(05 downto 0);
117
  signal spi_bitcnt_max : std_ulogic_vector(05 downto 0);
118
  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 36 zero_gravi
      -- write access --
138 2 zero_gravi
      spi_start <= '0';
139
      if (wren = '1') then
140 36 zero_gravi
        if (addr = spi_ctrl_addr_c) then -- control
141 22 zero_gravi
          ctrl <= data_i(ctrl'left downto 0);
142 2 zero_gravi
        end if;
143 36 zero_gravi
        if (addr = spi_rtx_addr_c) then -- tx data
144
          tx_data_reg <= data_i;
145
          spi_start   <= '1';
146 2 zero_gravi
        end if;
147
      end if;
148
      -- read access --
149
      data_o <= (others => '0');
150
      if (rden = '1') then
151
        if (addr = spi_ctrl_addr_c) then
152
          data_o(ctrl_spi_cs0_c)    <= ctrl(ctrl_spi_cs0_c);
153
          data_o(ctrl_spi_cs1_c)    <= ctrl(ctrl_spi_cs1_c);
154
          data_o(ctrl_spi_cs2_c)    <= ctrl(ctrl_spi_cs2_c);
155
          data_o(ctrl_spi_cs3_c)    <= ctrl(ctrl_spi_cs3_c);
156
          data_o(ctrl_spi_cs4_c)    <= ctrl(ctrl_spi_cs4_c);
157
          data_o(ctrl_spi_cs5_c)    <= ctrl(ctrl_spi_cs5_c);
158
          data_o(ctrl_spi_cs6_c)    <= ctrl(ctrl_spi_cs6_c);
159
          data_o(ctrl_spi_cs7_c)    <= ctrl(ctrl_spi_cs7_c);
160
          --
161
          data_o(ctrl_spi_en_c)     <= ctrl(ctrl_spi_en_c);
162
          data_o(ctrl_spi_cpha_c)   <= ctrl(ctrl_spi_cpha_c);
163
          data_o(ctrl_spi_prsc0_c)  <= ctrl(ctrl_spi_prsc0_c);
164
          data_o(ctrl_spi_prsc1_c)  <= ctrl(ctrl_spi_prsc1_c);
165
          data_o(ctrl_spi_prsc2_c)  <= ctrl(ctrl_spi_prsc2_c);
166
          data_o(ctrl_spi_size0_c)  <= ctrl(ctrl_spi_size0_c);
167
          data_o(ctrl_spi_size1_c)  <= ctrl(ctrl_spi_size1_c);
168
          --
169
          data_o(ctrl_spi_busy_c)   <= spi_busy;
170
        else -- spi_rtx_addr_c
171 36 zero_gravi
          data_o <= rx_data;
172 2 zero_gravi
        end if;
173
      end if;
174
    end if;
175
  end process rw_access;
176
 
177 36 zero_gravi
  -- direct chip-select (CS) (output is low-active) --  
178
  spi_csn_o(7 downto 0) <= not ctrl(ctrl_spi_cs7_c downto ctrl_spi_cs0_c);
179 2 zero_gravi
 
180
 
181
  -- Clock Selection ------------------------------------------------------------------------
182
  -- -------------------------------------------------------------------------------------------
183
  -- clock generator enable --
184
  clkgen_en_o <= ctrl(ctrl_spi_en_c);
185
 
186
  -- spi clock select --
187
  spi_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_spi_prsc2_c downto ctrl_spi_prsc0_c))));
188
 
189
 
190
  -- SPI Transceiver ------------------------------------------------------------------------
191
  -- -------------------------------------------------------------------------------------------
192
  spi_rtx_unit: process(clk_i)
193
  begin
194
    if rising_edge(clk_i) then
195 6 zero_gravi
      -- input (sdi) synchronizer --
196
      spi_sdi_ff0 <= spi_sdi_i;
197
      spi_sdi_ff1 <= spi_sdi_ff0;
198 2 zero_gravi
 
199
      -- serial engine --
200 48 zero_gravi
      irq_o <= '0';
201 2 zero_gravi
      if (spi_state0 = '0') or (ctrl(ctrl_spi_en_c) = '0') then -- idle or disabled
202 36 zero_gravi
      -- --------------------------------------------------------------
203
        spi_bitcnt <= (others => '0');
204 2 zero_gravi
        spi_state1 <= '0';
205 36 zero_gravi
        spi_sdo_o  <= '0';
206
        spi_sck_o  <= '0';
207 2 zero_gravi
        if (ctrl(ctrl_spi_en_c) = '0') then -- disabled
208
          spi_busy <= '0';
209
        elsif (spi_start = '1') then -- start new transmission
210 36 zero_gravi
          spi_rtx_sreg <= tx_data_reg;
211
          spi_busy     <= '1';
212 2 zero_gravi
        end if;
213
        spi_state0 <= spi_busy and spi_clk; -- start with next new clock pulse
214
 
215
      else -- transmission in progress
216 36 zero_gravi
      -- --------------------------------------------------------------
217 2 zero_gravi
        if (spi_state1 = '0') then -- first half of transmission
218 36 zero_gravi
        -- --------------------------------------------------------------
219
          spi_sck_o <= ctrl(ctrl_spi_cpha_c);
220 2 zero_gravi
 
221 36 zero_gravi
          case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
222
            when "00"   => spi_sdo_o <= spi_rtx_sreg(07); -- 8-bit mode
223
            when "01"   => spi_sdo_o <= spi_rtx_sreg(15); -- 16-bit mode
224
            when "10"   => spi_sdo_o <= spi_rtx_sreg(23); -- 24-bit mode
225
            when others => spi_sdo_o <= spi_rtx_sreg(31); -- 32-bit mode
226
          end case;
227
 
228 2 zero_gravi
          if (spi_clk = '1') then
229
            spi_state1 <= '1';
230
            if (ctrl(ctrl_spi_cpha_c) = '0') then
231 36 zero_gravi
              spi_rtx_sreg <= spi_rtx_sreg(30 downto 0) & spi_sdi_ff1;
232 2 zero_gravi
            end if;
233 36 zero_gravi
            spi_bitcnt <= std_ulogic_vector(unsigned(spi_bitcnt) + 1);
234 2 zero_gravi
          end if;
235 36 zero_gravi
 
236 2 zero_gravi
        else -- second half of transmission
237 36 zero_gravi
        -- --------------------------------------------------------------
238
          spi_sck_o <= not ctrl(ctrl_spi_cpha_c);
239 2 zero_gravi
 
240
          if (spi_clk = '1') then
241
            spi_state1 <= '0';
242
            if (ctrl(ctrl_spi_cpha_c) = '1') then
243 36 zero_gravi
              spi_rtx_sreg <= spi_rtx_sreg(30 downto 0) & spi_sdi_ff1;
244 2 zero_gravi
            end if;
245 36 zero_gravi
            if (spi_bitcnt = spi_bitcnt_max) then
246 2 zero_gravi
              spi_state0 <= '0';
247
              spi_busy   <= '0';
248 48 zero_gravi
              irq_o      <= '1';
249 2 zero_gravi
            end if;
250
          end if;
251
        end if;
252
      end if;
253
    end if;
254
  end process spi_rtx_unit;
255
 
256 36 zero_gravi
 
257
  -- RTX Data size ------------------------------------------------------------------------
258
  -- -------------------------------------------------------------------------------------------
259
  data_size: process(ctrl)
260 2 zero_gravi
  begin
261
    case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
262 36 zero_gravi
      when "00"   => spi_bitcnt_max <= "001000"; -- 8-bit mode
263
      when "01"   => spi_bitcnt_max <= "010000"; -- 16-bit mode
264
      when "10"   => spi_bitcnt_max <= "011000"; -- 24-bit mode
265
      when others => spi_bitcnt_max <= "100000"; -- 32-bit mode
266 2 zero_gravi
    end case;
267 36 zero_gravi
  end process data_size;
268 2 zero_gravi
 
269
 
270 36 zero_gravi
  -- RX-Data Masking ------------------------------------------------------------------------
271
  -- -------------------------------------------------------------------------------------------
272
  rx_mapping: process(ctrl, spi_rtx_sreg)
273
  begin
274
    case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
275
      when "00"   => rx_data <= x"000000" & spi_rtx_sreg(07 downto 0); -- 8-bit mode
276
      when "01"   => rx_data <= x"0000"   & spi_rtx_sreg(15 downto 0); -- 16-bit mode
277
      when "10"   => rx_data <= x"00"     & spi_rtx_sreg(23 downto 0); -- 24-bit mode
278
      when others => rx_data <=             spi_rtx_sreg(31 downto 0); -- 32-bit mode
279
    end case;
280
  end process rx_mapping;
281
 
282
 
283 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.