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

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [rtl/] [core/] [neo430_spi.vhd] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
-- #################################################################################################
2
-- #  << NEO430 - Serial Peripheral Interface >>                                                   #
3
-- # ********************************************************************************************* #
4
-- # Frame format: 8-bit or 16-bit, MSB or LSB first, 2 clock modes, 8 clock speeds, 6 CS lines.   #
5
-- # 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 NEO430 Processor - https://github.com/stnolting/neo430                                    #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
library neo430;
43
use neo430.neo430_package.all;
44
 
45
entity neo430_spi is
46
  port (
47
    -- host access --
48
    clk_i       : in  std_ulogic; -- global clock line
49
    rden_i      : in  std_ulogic; -- read enable
50
    wren_i      : in  std_ulogic; -- write enable
51
    addr_i      : in  std_ulogic_vector(15 downto 0); -- address
52
    data_i      : in  std_ulogic_vector(15 downto 0); -- data in
53
    data_o      : out std_ulogic_vector(15 downto 0); -- data out
54
    -- clock generator --
55
    clkgen_en_o : out std_ulogic; -- enable clock generator
56
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
57
    -- com lines --
58
    spi_sclk_o  : out std_ulogic; -- SPI serial clock
59
    spi_mosi_o  : out std_ulogic; -- SPI master out, slave in
60
    spi_miso_i  : in  std_ulogic; -- SPI master in, slave out
61
    spi_cs_o    : out std_ulogic_vector(05 downto 0); -- SPI CS
62
    -- interrupt --
63
    spi_irq_o   : out std_ulogic -- transmission done interrupt
64
  );
65
end neo430_spi;
66
 
67
architecture neo430_spi_rtl of neo430_spi is
68
 
69
  -- IO space: module base address --
70
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
71
  constant lo_abb_c : natural := index_size_f(spi_size_c); -- low address boundary bit
72
 
73
  -- control reg bits --
74
  constant ctrl_spi_cs_sel0_c : natural :=  0; -- r/w: spi CS 0
75
  constant ctrl_spi_cs_sel1_c : natural :=  1; -- r/w: spi CS 1
76
  constant ctrl_spi_cs_sel2_c : natural :=  2; -- r/w: spi CS 2
77
  constant ctrl_spi_cs_sel3_c : natural :=  3; -- r/w: spi CS 3
78
  constant ctrl_spi_cs_sel4_c : natural :=  4; -- r/w: spi CS 4
79
  constant ctrl_spi_cs_sel5_c : natural :=  5; -- r/w: spi CS 5
80
  constant ctrl_spi_en_c      : natural :=  6; -- r/w: spi enable
81
  constant ctrl_spi_cpha_c    : natural :=  7; -- r/w: spi clock phase
82
  constant ctrl_spi_irq_en_c  : natural :=  8; -- r/w: spi transmission done interrupt enable
83
  constant ctrl_spi_prsc0_c   : natural :=  9; -- r/w: spi prescaler select bit 0
84
  constant ctrl_spi_prsc1_c   : natural := 10; -- r/w: spi prescaler select bit 1
85
  constant ctrl_spi_prsc2_c   : natural := 11; -- r/w: spi prescaler select bit 2
86
  constant ctrl_spi_dir_c     : natural := 12; -- r/w: shift direction (0: MSB first, 1: LSB first)
87
  constant ctrl_spi_size_c    : natural := 13; -- r/w: data size(0: 8-bit, 1: 16-bit)
88
  --       reserved           : natural := 14;
89
  constant ctrl_spi_busy_c    : natural := 15; -- r/-: spi transceiver is busy
90
 
91
  -- access control --
92
  signal acc_en : std_ulogic; -- module access enable
93
  signal addr   : std_ulogic_vector(15 downto 0); -- access address
94
  signal wr_en  : std_ulogic; -- word write enable
95
  signal rd_en  : std_ulogic; -- read enable
96
 
97
  -- accessible regs --
98
  signal ctrl : std_ulogic_vector(15 downto 0);
99
 
100
  -- clock generator --
101
  signal spi_clk : std_ulogic;
102
 
103
  -- spi transceiver --
104
  signal spi_busy     : std_ulogic;
105
  signal spi_state0   : std_ulogic;
106
  signal spi_state1   : std_ulogic;
107
  signal spi_rtx_sreg : std_ulogic_vector(15 downto 0);
108
  signal spi_rx_data  : std_ulogic_vector(15 downto 0);
109
  signal spi_bitcnt   : std_ulogic_vector(04 downto 0);
110
  signal spi_miso_ff0 : std_ulogic;
111
  signal spi_miso_ff1 : std_ulogic;
112
 
113
begin
114
 
115
  -- Access Control -----------------------------------------------------------
116
  -- -----------------------------------------------------------------------------
117
  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';
118
  addr   <= spi_base_c(15 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 1) & '0'; -- word aligned
119
  wr_en  <= acc_en and wren_i;
120
  rd_en  <= acc_en and rden_i;
121
 
122
 
123
  -- Write access -------------------------------------------------------------
124
  -- -----------------------------------------------------------------------------
125
  wr_access: process(clk_i)
126
  begin
127
    if rising_edge(clk_i) then
128
      if (wr_en = '1') then
129
        if (addr = spi_ctrl_addr_c) then
130
          ctrl <= data_i;
131
        end if;
132
      end if;
133
    end if;
134
  end process wr_access;
135
 
136
 
137
  -- Clock Selection ----------------------------------------------------------
138
  -- -----------------------------------------------------------------------------
139
  -- clock enable --
140
  clkgen_en_o <= ctrl(ctrl_spi_en_c);
141
 
142
  -- spi clock select --
143
  spi_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_spi_prsc2_c downto ctrl_spi_prsc0_c))));
144
 
145
 
146
  -- SPI transceiver ----------------------------------------------------------
147
  -- -----------------------------------------------------------------------------
148
  spi_rtx_unit: process(clk_i)
149
  begin
150
    if rising_edge(clk_i) then
151
      -- input (MISO) synchronizer --
152
      spi_miso_ff0 <= spi_miso_i;
153
      spi_miso_ff1 <= spi_miso_ff0;
154
      -- arbiter --
155
      spi_irq_o <= '0';
156
      if (spi_state0 = '0') or (ctrl(ctrl_spi_en_c) = '0') then -- idle or disabled
157
        if (ctrl(ctrl_spi_size_c) = '0') then -- 8 bit mode
158
          spi_bitcnt <= "01000";
159
        else -- 16 bit mode
160
          spi_bitcnt <= "10000";
161
        end if;
162
        spi_state1 <= '0';
163
        spi_mosi_o <= '0';
164
        spi_sclk_o <= '0';
165
        if (ctrl(ctrl_spi_en_c) = '0') then -- disabled
166
          spi_busy <= '0';
167
        elsif (wr_en = '1') and (addr = spi_rtx_addr_c) then
168
          if (ctrl(ctrl_spi_size_c) = '0') then -- 8 bit mode
169
            spi_rtx_sreg <= data_i(7 downto 0) & "00000000";
170
          else -- 16 bit mode
171
            spi_rtx_sreg <= data_i(15 downto 0);
172
          end if;
173
          spi_busy <= '1';
174
        end if;
175
        spi_state0 <= spi_busy and spi_clk; -- start with next new clock pulse
176
 
177
      else -- transmission in progress
178
        if (spi_state1 = '0') then -- first half of transmission
179
 
180
          spi_sclk_o <= ctrl(ctrl_spi_cpha_c);
181
          if (ctrl(ctrl_spi_dir_c) = '0') then
182
            spi_mosi_o <= spi_rtx_sreg(15); -- MSB first
183
          else
184
            spi_mosi_o <= spi_rtx_sreg(0); -- LSB first
185
          end if;
186
          if (spi_clk = '1') then
187
            spi_state1 <= '1';
188
            if (ctrl(ctrl_spi_cpha_c) = '0') then
189
              if (ctrl(ctrl_spi_dir_c) = '0') then
190
                spi_rtx_sreg <= spi_rtx_sreg(14 downto 0) & spi_miso_ff1; -- MSB first
191
              else
192
                spi_rtx_sreg <= spi_miso_ff1 & spi_rtx_sreg(15 downto 1); -- LSB first
193
              end if;
194
            end if;
195
            spi_bitcnt <= std_ulogic_vector(unsigned(spi_bitcnt) - 1);
196
          end if;
197
        else -- second half of transmission
198
 
199
          spi_sclk_o <= not ctrl(ctrl_spi_cpha_c);
200
          if (spi_clk = '1') then
201
            spi_state1 <= '0';
202
            if (ctrl(ctrl_spi_cpha_c) = '1') then
203
              if (ctrl(ctrl_spi_dir_c) = '0') then
204
                spi_rtx_sreg <= spi_rtx_sreg(14 downto 0) & spi_miso_ff1; -- MSB first
205
              else
206
                spi_rtx_sreg <= spi_miso_ff1 & spi_rtx_sreg(15 downto 1); -- LSB first
207
              end if;
208
            end if;
209
            if (spi_bitcnt = "00000") then
210
              spi_state0 <= '0';
211
              spi_busy   <= '0';
212
              spi_irq_o  <= ctrl(ctrl_spi_irq_en_c);
213
            end if;
214
          end if;
215
        end if;
216
      end if;
217
    end if;
218
  end process spi_rtx_unit;
219
 
220
  -- SPI receiver output --
221
  spi_rx_data <= (x"00" & spi_rtx_sreg(7 downto 0)) when (ctrl(ctrl_spi_size_c) = '0') else spi_rtx_sreg(15 downto 0);
222
 
223
  -- direct user-defined CS --  
224
  spi_cs_o(0) <= '0' when (ctrl(ctrl_spi_cs_sel0_c) = '1') else '1';
225
  spi_cs_o(1) <= '0' when (ctrl(ctrl_spi_cs_sel1_c) = '1') else '1';
226
  spi_cs_o(2) <= '0' when (ctrl(ctrl_spi_cs_sel2_c) = '1') else '1';
227
  spi_cs_o(3) <= '0' when (ctrl(ctrl_spi_cs_sel3_c) = '1') else '1';
228
  spi_cs_o(4) <= '0' when (ctrl(ctrl_spi_cs_sel4_c) = '1') else '1';
229
  spi_cs_o(5) <= '0' when (ctrl(ctrl_spi_cs_sel5_c) = '1') else '1';
230
 
231
 
232
  -- Read access --------------------------------------------------------------
233
  -- -----------------------------------------------------------------------------
234
  rd_access: process(clk_i)
235
  begin
236
    if rising_edge(clk_i) then
237
      data_o <= (others => '0');
238
      if (rd_en = '1') then
239
        if (addr = spi_ctrl_addr_c) then
240
          data_o(ctrl_spi_en_c)      <= ctrl(ctrl_spi_en_c);
241
          data_o(ctrl_spi_cpha_c)    <= ctrl(ctrl_spi_cpha_c);
242
          data_o(ctrl_spi_irq_en_c)  <= ctrl(ctrl_spi_irq_en_c);
243
          data_o(ctrl_spi_prsc0_c)   <= ctrl(ctrl_spi_prsc0_c);
244
          data_o(ctrl_spi_prsc1_c)   <= ctrl(ctrl_spi_prsc1_c);
245
          data_o(ctrl_spi_prsc2_c)   <= ctrl(ctrl_spi_prsc2_c);
246
          data_o(ctrl_spi_dir_c)     <= ctrl(ctrl_spi_dir_c);
247
          data_o(ctrl_spi_size_c)    <= ctrl(ctrl_spi_size_c);
248
          data_o(ctrl_spi_cs_sel0_c) <= ctrl(ctrl_spi_cs_sel0_c);
249
          data_o(ctrl_spi_cs_sel1_c) <= ctrl(ctrl_spi_cs_sel1_c);
250
          data_o(ctrl_spi_cs_sel2_c) <= ctrl(ctrl_spi_cs_sel2_c);
251
          data_o(ctrl_spi_cs_sel3_c) <= ctrl(ctrl_spi_cs_sel3_c);
252
          data_o(ctrl_spi_cs_sel4_c) <= ctrl(ctrl_spi_cs_sel4_c);
253
          data_o(ctrl_spi_cs_sel5_c) <= ctrl(ctrl_spi_cs_sel5_c);
254
          data_o(ctrl_spi_busy_c)    <= spi_busy;
255
        else -- spi_rtx_addr_c
256
          data_o(15 downto 0) <= spi_rx_data;
257
        end if;
258
      end if;
259
    end if;
260
  end process rd_access;
261
 
262
 
263
end neo430_spi_rtl;

powered by: WebSVN 2.1.0

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