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

Subversion Repositories spi_master_slave

[/] [spi_master_slave/] [trunk/] [syn/] [spi_slave.vhd] - Blame information for rev 16

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

Line No. Rev Author Line
1 10 jdoin
----------------------------------------------------------------------------------
2
-- Author:          Jonny Doin, jdoin@opencores.org
3
-- 
4
-- Create Date:     15:36:20 05/15/2011
5
-- Module Name:     SPI_SLAVE - RTL
6
-- Project Name:    SPI INTERFACE
7
-- Target Devices:  Spartan-6
8
-- Tool versions:   ISE 13.1
9
-- Description: 
10
--
11
--      This block is the SPI slave interface, implemented in one single entity.
12
--      All internal core operations are synchronous to the external SPI clock, and follows the general SPI de-facto standard.
13
--      The parallel read/write interface is synchronous to a supplied system master clock, 'clk_i'.
14
--      Synchronization for the parallel ports is provided by input data request and write enable lines, and output data valid line.
15
--      Fully pipelined cross-clock circuitry guarantees that no setup artifacts occur on the buffers that are accessed by the two 
16
--      clock domains.
17
--
18
--      The block is very simple to use, and has parallel inputs and outputs that behave like a synchronous memory i/o.
19
--      It is parameterizable via generics for the data width ('N'), SPI mode (CPHA and CPOL), and lookahead prefetch 
20
--      signaling ('PREFETCH').
21
--
22
--      PARALLEL WRITE INTERFACE
23
--      The parallel interface has a input port 'di_i' and an output port 'do_o'.
24
--      Parallel load is controlled using 3 signals: 'di_i', 'di_req_o' and 'wren_i'. 
25
--      When the core needs input data, a look ahead data request strobe , 'di_req_o' is pulsed 'PREFETCH' 'spi_sck_i' 
26
--      cycles in advance to synchronize a user pipelined memory or fifo to present the next input data at 'di_i' 
27
--      in time to have continuous clock at the spi bus, to allow back-to-back continuous load.
28
--      The data request strobe on 'di_req_o' is 2 'clk_i' clock cycles long.
29
--      The write to 'di_i' must occur at most one 'spi_sck_i' cycle before actual load to the core shift register, to avoid
30
--      race conditions at the register transfer.
31
--      The user circuit places data at the 'di_i' port and strobes the 'wren_i' line for one rising edge of 'clk_i'.
32
--      For a pipelined sync RAM, a PREFETCH of 3 cycles allows an address generator to present the new adress to the RAM in one
33
--      cycle, and the RAM to respond in one more cycle, in time for 'di_i' to be latched by the interface one clock before transfer.
34
--      If the user sequencer needs a different value for PREFETCH, the generic can be altered at instantiation time.
35
--      The 'wren_i' write enable strobe must be valid at least one setup time before the rising edge of the last clock cycle,
36
--      if continuous transmission is intended. 
37
--      When the interface is idle ('spi_ssel_i' is HIGH), the top bit of the latched 'di_i' port is presented at port 'spi_miso_o'.
38
--
39
--      PARALLEL WRITE PIPELINED SEQUENCE
40
--      =================================
41
--                     __    __    __    __    __    __    __ 
42
--      clk_i       __/  \__/  \__/  \__/  \__/  \__/  \__/  \...     -- parallel interface clock
43
--                           ___________                        
44
--      di_req_o    ________/           \_____________________...     -- 'di_req_o' asserted on rising edge of 'clk_i'
45
--                  ______________ ___________________________...
46
--      di_i        __old_data____X______new_data_____________...     -- user circuit loads data on 'di_i' at next 'clk_i' rising edge
47
--                                             ________                        
48
--      wren_i      __________________________/        \______...     -- 'wren_i' enables latch on rising edge of 'clk_i'
49
--                      
50
--
51
--      PARALLEL READ INTERFACE
52
--      An internal buffer is used to copy the internal shift register data to drive the 'do_o' port. When a complete 
53
--      word is received, the core shift register is transferred to the buffer, at the rising edge of the spi clock, 'spi_sck_i'.
54
--      The signal 'do_valid_o' is strobed 3 'clk_i' clocks after, to directly drive a synchronous memory or fifo write enable.
55
--      'do_valid_o' is synchronous to the parallel interface clock, and changes only on rising edges of 'clk_i'.
56
--      When the interface is idle, data at the 'do_o' port holds the last word received.
57
--
58
--      PARALLEL READ PIPELINED SEQUENCE
59
--      ================================
60
--                      ______        ______        ______        ______
61
--      clk_spi_i   ___/ bit1 \______/ bitN \______/bitN-1\______/bitN-2\__...  -- spi base clock
62
--                     __    __    __    __    __    __    __    __    __  
63
--      clk_i       __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \_...  -- parallel interface clock
64
--                  _________________ _____________________________________...  -- 1) received data is transferred to 'do_buffer_reg'
65
--      do_o        __old_data_______X__________new_data___________________...  --    after last bit received, at next shift clock.
66
--                                                   ____________               
67
--      do_valid_o  ________________________________/            \_________...  -- 2) 'do_valid_o' strobed for 2 'clk_i' cycles
68
--                                                                              --    on the 3rd 'clk_i' rising edge.
69
--
70
--
71
--      This design was originally targeted to a Spartan-6 platform, synthesized with XST and normal constraints.
72
--
73
------------------------------ COPYRIGHT NOTICE -----------------------------------------------------------------------
74
--                                                                   
75
--      This file is part of the SPI MASTER/SLAVE INTERFACE project http://opencores.org/project,spi_master_slave                
76
--                                                                   
77
--      Author(s):      Jonny Doin, jdoin@opencores.org
78
--                                                                   
79
--      Copyright (C) 2011 Authors and OPENCORES.ORG
80
--      --------------------------------------------
81
--                                                                   
82
--      This source file may be used and distributed without restriction provided that this copyright statement is not    
83
--      removed from the file and that any derivative work contains the original copyright notice and the associated 
84
--      disclaimer. 
85
--                                                                   
86
--      This source file is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
87
--      General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
88
--      (at your option) any later version.
89
--                                                                   
90
--      This source is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
91
--      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more  
92
--      details.
93
--
94
--      You should have received a copy of the GNU Lesser General Public License along with this source; if not, download 
95
--      it from http://www.opencores.org/lgpl.shtml
96
--                                                                   
97
------------------------------ REVISION HISTORY -----------------------------------------------------------------------
98
--
99
-- 2011/05/15   v0.10.0050  [JD]    created the slave logic, with 2 clock domains, from SPI_MASTER module.
100
-- 2011/05/15   v0.15.0055  [JD]    fixed logic for starting state when CPHA='1'.
101
-- 2011/05/17   v0.80.0049  [JD]    added explicit clock synchronization circuitry across clock boundaries.
102
-- 2011/05/18   v0.95.0050  [JD]    clock generation circuitry, with generators for all-rising-edge clock core.
103
-- 2011/06/05   v0.96.0053  [JD]    changed async clear to sync resets.
104
-- 2011/06/07   v0.97.0065  [JD]    added cross-clock buffers, fixed fsm async glitches.
105
-- 2011/06/09   v0.97.0068  [JD]    reduced control sets (resets, CE, presets) to the absolute minimum to operate, to reduce 
106
--                                  synthesis LUT overhead in Spartan-6 architecture.
107
-- 2011/06/11   v0.97.0075  [JD]    redesigned all parallel data interfacing ports, and implemented cross-clock strobe logic.
108 12 jdoin
-- 2011/06/12   v0.97.0079  [JD]    implemented wr_ack and di_req logic for state 0, and eliminated unnecessary registers reset.
109
-- 2011/06/17   v0.97.0079  [JD]    implemented wr_ack and di_req logic for state 0, and eliminated unnecessary registers reset.
110 10 jdoin
-- 2011/07/16   v1.11.0080  [JD]    verified both spi_master and spi_slave in loopback at 50MHz SPI clock.
111 12 jdoin
-- 2011/07/29   v2.00.0110  [JD]    FIX: CPHA bugs:
112
--                                      - redesigned core clocking to address all CPOL and CPHA configurations.
113
--                                      - added CHANGE_EDGE to the FSM register transfer logic, to have MISO change at opposite 
114
--                                        clock phases from SHIFT_EDGE.
115
--                                  Removed global signal setting at the FSM, implementing exhaustive explicit signal attributions
116
--                                  for each state, to avoid reported inference problems in some synthesis engines.
117
--                                  Streamlined port names and indentation blocks.
118 13 jdoin
-- 2011/08/01   v2.01.0115  [JD]    Adjusted 'do_valid_o' pulse width to be 2 'clk_i', as in the master core.
119
--                                  Simulated in iSim with the master core for continuous transmission mode.
120
-- 2011/08/02   v2.02.0120  [JD]    Added mux for MISO at reset state, to output di(N-1) at start. This fixed a bug in first bit.
121
--                                  The master and slave cores were verified in FPGA with continuous transmission, for all SPI modes.
122 16 jdoin
-- 2011/08/04   v2.02.0121  [JD]    Changed minor comment bugs in the combinatorial fsm logic.
123 10 jdoin
--
124
--                                                                   
125
-----------------------------------------------------------------------------------------------------------------------
126
--  TODO
127
--  ====
128
--
129
-----------------------------------------------------------------------------------------------------------------------
130 13 jdoin
library ieee;
131
use ieee.std_logic_1164.all;
132
use ieee.numeric_std.all;
133
use ieee.std_logic_unsigned.all;
134 10 jdoin
 
135
entity spi_slave is
136
    Generic (
137
        N : positive := 32;                                             -- 32bit serial word length is default
138
        CPOL : std_logic := '0';                                        -- SPI mode selection (mode 0 default)
139
        CPHA : std_logic := '0';                                        -- CPOL = clock polarity, CPHA = clock phase.
140
        PREFETCH : positive := 3);                                      -- prefetch lookahead cycles
141
    Port (
142
        clk_i : in std_logic := 'X';                                    -- internal interface clock (clocks di/do registers)
143
        spi_ssel_i : in std_logic := 'X';                               -- spi bus slave select line
144
        spi_sck_i : in std_logic := 'X';                                -- spi bus sck clock (clocks the shift register core)
145
        spi_mosi_i : in std_logic := 'X';                               -- spi bus mosi input
146
        spi_miso_o : out std_logic := 'X';                              -- spi bus spi_miso_o output
147
        di_req_o : out std_logic;                                       -- preload lookahead data request line
148
        di_i : in  std_logic_vector (N-1 downto 0) := (others => 'X');  -- parallel load data in (clocked in on rising edge of clk_i)
149
        wren_i : in std_logic := 'X';                                   -- user data write enable
150 12 jdoin
        wr_ack_o : out std_logic;                                       -- write acknowledge
151 10 jdoin
        do_valid_o : out std_logic;                                     -- do_o data valid strobe, valid during one clk_i rising edge.
152
        do_o : out  std_logic_vector (N-1 downto 0);                    -- parallel output (clocked out on falling clk_i)
153
        --- debug ports: can be removed for the application circuit ---
154
        do_transfer_o : out std_logic;                                  -- debug: internal transfer driver
155
        wren_o : out std_logic;                                         -- debug: internal state of the wren_i pulse stretcher
156 12 jdoin
        rx_bit_next_o : out std_logic;                                  -- debug: internal rx bit
157 13 jdoin
        state_dbg_o : out std_logic_vector (3 downto 0);                -- debug: internal state register
158 10 jdoin
        sh_reg_dbg_o : out std_logic_vector (N-1 downto 0)              -- debug: internal shift register
159
    );
160
end spi_slave;
161
 
162
--================================================================================================================
163
-- SYNTHESIS CONSIDERATIONS
164
-- ========================
165
-- There are several output ports that are used to simulate and verify the core operation. 
166
-- Do not map any signals to the unused ports, and the synthesis tool will remove the related interfacing
167
-- circuitry. 
168
-- The same is valid for the transmit and receive ports. If the receive ports are not mapped, the
169
-- synthesis tool will remove the receive logic from the generated circuitry.
170
--================================================================================================================
171
 
172 13 jdoin
architecture rtl of spi_slave is
173 10 jdoin
    -- constants to control FlipFlop synthesis
174 12 jdoin
    constant SHIFT_EDGE  : std_logic := (CPOL xnor CPHA);   -- MOSI data is captured and shifted at this SCK edge
175
    constant CHANGE_EDGE : std_logic := (CPOL xor CPHA);    -- MISO data is updated at this SCK edge
176
 
177
    ------------------------------------------------------------------------------------------
178 10 jdoin
    -- GLOBAL RESET:
179
    --      all signals are initialized to zero at GSR (global set/reset) by giving explicit
180
    --      initialization values at declaration. This is needed for all Xilinx FPGAs, and 
181
    --      especially for the Spartan-6 and newer CLB architectures, where a local reset can
182
    --      reduce the usability of the slice registers, due to the need to share the control
183
    --      set (RESET/PRESET, CLOCK ENABLE and CLOCK) by all 8 registers in a slice.
184
    --      By using GSR for the initialization, and reducing RESET local init to the bare
185
    --      essential, the model achieves better LUT/FF packing and CLB usability.
186 12 jdoin
    ------------------------------------------------------------------------------------------
187 13 jdoin
    -- internal state signals for register and combinatorial stages
188 12 jdoin
    signal state_next : natural range N downto 0 := 0;      -- state 0 is idle state
189
    signal state_reg : natural range N downto 0 := 0;       -- state 0 is idle state
190 13 jdoin
    -- shifter signals for register and combinatorial stages
191
    signal sh_next : std_logic_vector (N-1 downto 0);
192
    signal sh_reg : std_logic_vector (N-1 downto 0);
193 12 jdoin
    -- mosi and miso connections
194 13 jdoin
    signal rx_bit_next : std_logic;
195
    signal tx_bit_next : std_logic;
196
    signal tx_bit_reg : std_logic;
197
    -- buffered di_i data signals for register and combinatorial stages
198 12 jdoin
    signal di_reg : std_logic_vector (N-1 downto 0);
199 13 jdoin
    -- internal wren_i stretcher for fsm combinatorial stage
200 12 jdoin
    signal wren : std_logic;
201
    signal wr_ack_next : std_logic := '0';
202
    signal wr_ack_reg : std_logic := '0';
203 13 jdoin
    -- buffered do_o data signals for register and combinatorial stages
204
    signal do_buffer_next : std_logic_vector (N-1 downto 0);
205
    signal do_buffer_reg : std_logic_vector (N-1 downto 0);
206 10 jdoin
    -- internal signal to flag transfer to do_buffer_reg
207
    signal do_transfer_next : std_logic := '0';
208
    signal do_transfer_reg : std_logic := '0';
209
    -- internal input data request signal 
210 12 jdoin
    signal di_req_next : std_logic := '0';
211
    signal di_req_reg : std_logic := '0';
212 10 jdoin
    -- cross-clock do_valid_o logic
213
    signal do_valid_next : std_logic := '0';
214
    signal do_valid_A : std_logic := '0';
215
    signal do_valid_B : std_logic := '0';
216
    signal do_valid_C : std_logic := '0';
217
    signal do_valid_D : std_logic := '0';
218
    signal do_valid_o_reg : std_logic := '0';
219
    -- cross-clock di_req_o logic
220
    signal di_req_o_next : std_logic := '0';
221
    signal di_req_o_A : std_logic := '0';
222
    signal di_req_o_B : std_logic := '0';
223
    signal di_req_o_C : std_logic := '0';
224
    signal di_req_o_D : std_logic := '0';
225
    signal di_req_o_reg : std_logic := '0';
226
begin
227
    --=============================================================================================
228
    --  GENERICS CONSTRAINTS CHECKING
229
    --=============================================================================================
230
    -- minimum word width is 8 bits
231
    assert N >= 8
232
    report "Generic parameter 'N' error: SPI shift register size needs to be 8 bits minimum"
233
    severity FAILURE;
234
    -- maximum prefetch lookahead check
235
    assert PREFETCH <= N-5
236
    report "Generic parameter 'PREFETCH' error: lookahead count out of range, needs to be N-5 maximum"
237
    severity FAILURE;
238
 
239
    --=============================================================================================
240 12 jdoin
    --  GENERATE BLOCKS
241 10 jdoin
    --=============================================================================================
242
 
243
    --=============================================================================================
244 12 jdoin
    --  DATA INPUTS
245 10 jdoin
    --=============================================================================================
246 12 jdoin
    -- connect rx bit input
247
    rx_bit_proc : rx_bit_next <= spi_mosi_i;
248 10 jdoin
 
249
    --=============================================================================================
250
    --  CROSS-CLOCK PIPELINE TRANSFER LOGIC
251
    --=============================================================================================
252
    -- do_valid_o and di_req_o strobe output logic
253
    -- this is a delayed pulse generator with a ripple-transfer FFD pipeline, that generates a 
254
    -- fixed-length delayed pulse for the output flags, at the parallel clock domain
255 12 jdoin
    out_transfer_proc : process ( clk_i, do_transfer_reg, di_req_reg,
256 10 jdoin
                                  do_valid_A, do_valid_B, do_valid_D,
257
                                  di_req_o_A, di_req_o_B, di_req_o_D) is
258
    begin
259
        if clk_i'event and clk_i = '1' then                     -- clock at parallel port clock
260
            -- do_transfer_reg -> do_valid_o_reg
261
            do_valid_A <= do_transfer_reg;                      -- the input signal must be at least 2 clocks long
262
            do_valid_B <= do_valid_A;                           -- feed it to a ripple chain of FFDs
263
            do_valid_C <= do_valid_B;
264
            do_valid_D <= do_valid_C;
265
            do_valid_o_reg <= do_valid_next;                    -- registered output pulse
266
            --------------------------------
267 12 jdoin
            -- di_req_reg -> di_req_o_reg
268
            di_req_o_A <= di_req_reg;                           -- the input signal must be at least 2 clocks long
269 10 jdoin
            di_req_o_B <= di_req_o_A;                           -- feed it to a ripple chain of FFDs
270
            di_req_o_C <= di_req_o_B;
271
            di_req_o_D <= di_req_o_C;
272
            di_req_o_reg <= di_req_o_next;                      -- registered output pulse
273
        end if;
274
        -- generate a 2-clocks pulse at the 3rd clock cycle
275
        do_valid_next <= do_valid_A and do_valid_B and not do_valid_D;
276
        di_req_o_next <= di_req_o_A and di_req_o_B and not di_req_o_D;
277
    end process out_transfer_proc;
278
    -- parallel load input registers: data register and write enable
279 12 jdoin
    in_transfer_proc: process (clk_i, wren_i, wr_ack_reg) is
280 10 jdoin
    begin
281
        -- registered data input, input register with clock enable
282
        if clk_i'event and clk_i = '1' then
283
            if wren_i = '1' then
284
                di_reg <= di_i;                                 -- parallel data input buffer register
285
            end if;
286
        end  if;
287
        -- stretch wren pulse to be detected by spi fsm (ffd with sync preset and sync reset)
288
        if clk_i'event and clk_i = '1' then
289
            if wren_i = '1' then                                -- wren_i is the sync preset for wren
290
                wren <= '1';
291 12 jdoin
            elsif wr_ack_reg = '1' then                         -- wr_ack is the sync reset for wren
292 10 jdoin
                wren <= '0';
293
            end if;
294
        end  if;
295
    end process in_transfer_proc;
296
 
297
    --=============================================================================================
298 13 jdoin
    --  REGISTER TRANSFER PROCESSES
299 12 jdoin
    --=============================================================================================
300
    -- fsm state and data registers change on spi SHIFT_EDGE
301
    core_reg_proc : process (spi_sck_i, spi_ssel_i) is
302
    begin
303
        -- FFD registers clocked on SHIFT edge and cleared on idle (spi_ssel_i = 1)
304
        if spi_ssel_i = '1' then                                -- async clr
305
            state_reg <= 0;                                     -- state falls back to idle when slave not selected
306
        elsif spi_sck_i'event and spi_sck_i = SHIFT_EDGE then   -- on SHIFT edge, update all core registers
307
            state_reg <= state_next;                            -- core fsm changes state with spi SHIFT clock
308
        end if;
309
        -- FFD registers clocked on SHIFT edge
310
        if spi_sck_i'event and spi_sck_i = SHIFT_EDGE then      -- on fsm state change, update all core registers
311
            sh_reg <= sh_next;                                  -- core shift register
312
            do_buffer_reg <= do_buffer_next;                    -- registered data output
313
            do_transfer_reg <= do_transfer_next;                -- cross-clock transfer flag
314
            di_req_reg <= di_req_next;                          -- input data request
315
            wr_ack_reg <= wr_ack_next;                          -- wren ack for data load synchronization
316
        end if;
317
        -- FFD registers clocked on CHANGE edge
318
        if spi_sck_i'event and spi_sck_i = CHANGE_EDGE then
319
            tx_bit_reg <= tx_bit_next;                          -- update MISO driver from the MSb
320
        end if;
321
    end process core_reg_proc;
322
 
323
    --=============================================================================================
324 13 jdoin
    --  COMBINATORIAL LOGIC PROCESSES
325 10 jdoin
    --=============================================================================================
326 13 jdoin
    -- state and datapath combinatorial logic
327 12 jdoin
    core_combi_proc : process ( sh_reg, sh_next, state_reg, tx_bit_reg, rx_bit_next, do_buffer_reg,
328
                                do_transfer_reg, di_reg, di_req_reg, wren, wr_ack_reg) is
329 10 jdoin
    begin
330 12 jdoin
        -- all output signals are assigned to (avoid latches)
331
        sh_next <= sh_reg;                                              -- shift register
332
        tx_bit_next <= tx_bit_reg;                                      -- MISO driver
333 10 jdoin
        do_buffer_next <= do_buffer_reg;                                -- output data buffer
334
        do_transfer_next <= do_transfer_reg;                            -- output data flag
335 12 jdoin
        wr_ack_next <= wr_ack_reg;                                      -- write enable acknowledge
336
        di_req_next <= di_req_reg;                                      -- data input request
337
        state_next <= state_reg;                                        -- fsm control state
338 10 jdoin
        case state_reg is
339 13 jdoin
 
340 16 jdoin
            when (N) =>                                                 -- deassert 'di_rdy' and stretch do_valid
341 13 jdoin
                wr_ack_next <= '0';                                     -- acknowledge data in transfer
342 12 jdoin
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
343
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
344 10 jdoin
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
345 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
346
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
347 13 jdoin
 
348 16 jdoin
            when (N-1) downto (PREFETCH+3) =>                           -- remove 'do_transfer' and shift bits
349
                do_transfer_next <= '0';                                -- reset 'do_valid' transfer signal
350 12 jdoin
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
351
                wr_ack_next <= '0';                                     -- remove data load ack for all but the load stages
352
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
353 10 jdoin
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
354 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
355
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
356 13 jdoin
 
357 16 jdoin
            when (PREFETCH+2) downto 3 =>                               -- raise prefetch 'di_req_o' signal
358 12 jdoin
                di_req_next <= '1';                                     -- request data in advance to allow for pipeline delays
359
                wr_ack_next <= '0';                                     -- remove data load ack for all but the load stages
360
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
361 10 jdoin
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
362 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
363
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
364 13 jdoin
 
365 16 jdoin
            when 2 =>                                                   -- transfer received data to do_buffer_reg on next cycle
366 12 jdoin
                di_req_next <= '1';                                     -- request data in advance to allow for pipeline delays
367
                wr_ack_next <= '0';                                     -- remove data load ack for all but the load stages
368
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
369
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
370
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
371
                do_transfer_next <= '1';                                -- signal transfer to do_buffer on next cycle
372
                do_buffer_next <= sh_next;                              -- get next data directly into rx buffer
373
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
374 13 jdoin
 
375 16 jdoin
            when 1 =>                                                   -- transfer rx data to do_buffer and restart if new data is written
376 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
377
                sh_next(N-1 downto 1) <= di_reg(N-2 downto 0);          -- shift inner bits
378
                tx_bit_next <= di_reg(N-1);                             -- first output bit comes from the MSb of parallel data
379
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
380 10 jdoin
                if wren = '1' then                                      -- load tx register if valid data present at di_reg
381 12 jdoin
                    wr_ack_next <= '1';                                 -- acknowledge data in transfer
382
                    state_next <= N;                                    -- next state is top bit of new data
383 10 jdoin
                else
384 12 jdoin
                    wr_ack_next <= '0';                                 -- remove data load ack for all but the load stages
385 10 jdoin
                    sh_next <= (others => '0');                         -- load null data (output '0' if no load)
386 12 jdoin
                    state_next <= 0;                                    -- next state is idle state
387 10 jdoin
                end if;
388 13 jdoin
 
389 16 jdoin
            when 0 =>                                                   -- idle state: start and end of transmission
390 13 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
391
                sh_next(N-1 downto 1) <= di_reg(N-2 downto 0);          -- shift inner bits
392
                wr_ack_next <= '1';                                     -- acknowledge data in transfer
393
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
394 12 jdoin
                do_transfer_next <= '0';                                -- clear signal transfer to do_buffer
395
                tx_bit_next <= di_reg(N-1);                             -- first output bit comes from the MSb of parallel data
396
                state_next <= N;                                        -- next state is top bit of new data
397 13 jdoin
 
398 10 jdoin
            when others =>
399 12 jdoin
                state_next <= 0;                                        -- safe state
400 13 jdoin
 
401 10 jdoin
        end case;
402
    end process core_combi_proc;
403
 
404
    --=============================================================================================
405 13 jdoin
    --  OUTPUT LOGIC PROCESSES
406 10 jdoin
    --=============================================================================================
407
    -- data output processes
408 12 jdoin
    do_o_proc :         do_o <= do_buffer_reg;                          -- do_o always available
409
    do_valid_o_proc:    do_valid_o <= do_valid_o_reg;                   -- copy registered do_valid_o to output
410
    di_req_o_proc:      di_req_o <= di_req_o_reg;                       -- copy registered di_req_o to output
411
    wr_ack_o_proc:      wr_ack_o <= wr_ack_reg;                         -- copy registered wr_ack_o to output
412 10 jdoin
 
413 13 jdoin
    -----------------------------------------------------------------------------------------------
414
    -- MISO driver process: copy next tx bit at reset
415
    -----------------------------------------------------------------------------------------------
416
    -- this is a MUX that selects the combinatorial next tx bit at reset, and the registered tx bit
417
    -- at sequential operation. The mux gives us a preload of the first bit, simplifying the shifter logic.
418
    spi_miso_o_proc: process (spi_ssel_i, tx_bit_reg, tx_bit_next) is
419
    begin
420
        if spi_ssel_i = '1' then
421
            spi_miso_o <= tx_bit_next;                                  -- copy next => reg at reset
422
        else
423
            spi_miso_o <= tx_bit_reg;
424
        end if;
425
    end process spi_miso_o_proc;
426
 
427 10 jdoin
    --=============================================================================================
428
    --  DEBUG LOGIC PROCESSES
429
    --=============================================================================================
430 13 jdoin
    -- these signals are useful for verification, and can be deleted after debug.
431 10 jdoin
    do_transfer_proc:   do_transfer_o <= do_transfer_reg;
432 13 jdoin
    state_debug_proc:   state_dbg_o <= std_logic_vector(to_unsigned(state_reg, 4)); -- export internal state to debug
433 12 jdoin
    rx_bit_next_proc:   rx_bit_next_o <= rx_bit_next;
434 10 jdoin
    wren_o_proc:        wren_o <= wren;
435
    sh_reg_debug_proc:  sh_reg_dbg_o <= sh_reg;                                     -- export sh_reg to debug
436 13 jdoin
end architecture rtl;
437 10 jdoin
 

powered by: WebSVN 2.1.0

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