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 13

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 10 jdoin
--
123
--                                                                   
124
-----------------------------------------------------------------------------------------------------------------------
125
--  TODO
126
--  ====
127
--
128
-----------------------------------------------------------------------------------------------------------------------
129 13 jdoin
library ieee;
130
use ieee.std_logic_1164.all;
131
use ieee.numeric_std.all;
132
use ieee.std_logic_unsigned.all;
133 10 jdoin
 
134
entity spi_slave is
135
    Generic (
136
        N : positive := 32;                                             -- 32bit serial word length is default
137
        CPOL : std_logic := '0';                                        -- SPI mode selection (mode 0 default)
138
        CPHA : std_logic := '0';                                        -- CPOL = clock polarity, CPHA = clock phase.
139
        PREFETCH : positive := 3);                                      -- prefetch lookahead cycles
140
    Port (
141
        clk_i : in std_logic := 'X';                                    -- internal interface clock (clocks di/do registers)
142
        spi_ssel_i : in std_logic := 'X';                               -- spi bus slave select line
143
        spi_sck_i : in std_logic := 'X';                                -- spi bus sck clock (clocks the shift register core)
144
        spi_mosi_i : in std_logic := 'X';                               -- spi bus mosi input
145
        spi_miso_o : out std_logic := 'X';                              -- spi bus spi_miso_o output
146
        di_req_o : out std_logic;                                       -- preload lookahead data request line
147
        di_i : in  std_logic_vector (N-1 downto 0) := (others => 'X');  -- parallel load data in (clocked in on rising edge of clk_i)
148
        wren_i : in std_logic := 'X';                                   -- user data write enable
149 12 jdoin
        wr_ack_o : out std_logic;                                       -- write acknowledge
150 10 jdoin
        do_valid_o : out std_logic;                                     -- do_o data valid strobe, valid during one clk_i rising edge.
151
        do_o : out  std_logic_vector (N-1 downto 0);                    -- parallel output (clocked out on falling clk_i)
152
        --- debug ports: can be removed for the application circuit ---
153
        do_transfer_o : out std_logic;                                  -- debug: internal transfer driver
154
        wren_o : out std_logic;                                         -- debug: internal state of the wren_i pulse stretcher
155 12 jdoin
        rx_bit_next_o : out std_logic;                                  -- debug: internal rx bit
156 13 jdoin
        state_dbg_o : out std_logic_vector (3 downto 0);                -- debug: internal state register
157 10 jdoin
        sh_reg_dbg_o : out std_logic_vector (N-1 downto 0)              -- debug: internal shift register
158
    );
159
end spi_slave;
160
 
161
--================================================================================================================
162
-- SYNTHESIS CONSIDERATIONS
163
-- ========================
164
-- There are several output ports that are used to simulate and verify the core operation. 
165
-- Do not map any signals to the unused ports, and the synthesis tool will remove the related interfacing
166
-- circuitry. 
167
-- The same is valid for the transmit and receive ports. If the receive ports are not mapped, the
168
-- synthesis tool will remove the receive logic from the generated circuitry.
169
--================================================================================================================
170
 
171 13 jdoin
architecture rtl of spi_slave is
172 10 jdoin
    -- constants to control FlipFlop synthesis
173 12 jdoin
    constant SHIFT_EDGE  : std_logic := (CPOL xnor CPHA);   -- MOSI data is captured and shifted at this SCK edge
174
    constant CHANGE_EDGE : std_logic := (CPOL xor CPHA);    -- MISO data is updated at this SCK edge
175
 
176
    ------------------------------------------------------------------------------------------
177 10 jdoin
    -- GLOBAL RESET:
178
    --      all signals are initialized to zero at GSR (global set/reset) by giving explicit
179
    --      initialization values at declaration. This is needed for all Xilinx FPGAs, and 
180
    --      especially for the Spartan-6 and newer CLB architectures, where a local reset can
181
    --      reduce the usability of the slice registers, due to the need to share the control
182
    --      set (RESET/PRESET, CLOCK ENABLE and CLOCK) by all 8 registers in a slice.
183
    --      By using GSR for the initialization, and reducing RESET local init to the bare
184
    --      essential, the model achieves better LUT/FF packing and CLB usability.
185 12 jdoin
    ------------------------------------------------------------------------------------------
186 13 jdoin
    -- internal state signals for register and combinatorial stages
187 12 jdoin
    signal state_next : natural range N downto 0 := 0;      -- state 0 is idle state
188
    signal state_reg : natural range N downto 0 := 0;       -- state 0 is idle state
189 13 jdoin
    -- shifter signals for register and combinatorial stages
190
    signal sh_next : std_logic_vector (N-1 downto 0);
191
    signal sh_reg : std_logic_vector (N-1 downto 0);
192 12 jdoin
    -- mosi and miso connections
193 13 jdoin
    signal rx_bit_next : std_logic;
194
    signal tx_bit_next : std_logic;
195
    signal tx_bit_reg : std_logic;
196
    -- buffered di_i data signals for register and combinatorial stages
197 12 jdoin
    signal di_reg : std_logic_vector (N-1 downto 0);
198 13 jdoin
    -- internal wren_i stretcher for fsm combinatorial stage
199 12 jdoin
    signal wren : std_logic;
200
    signal wr_ack_next : std_logic := '0';
201
    signal wr_ack_reg : std_logic := '0';
202 13 jdoin
    -- buffered do_o data signals for register and combinatorial stages
203
    signal do_buffer_next : std_logic_vector (N-1 downto 0);
204
    signal do_buffer_reg : std_logic_vector (N-1 downto 0);
205 10 jdoin
    -- internal signal to flag transfer to do_buffer_reg
206
    signal do_transfer_next : std_logic := '0';
207
    signal do_transfer_reg : std_logic := '0';
208
    -- internal input data request signal 
209 12 jdoin
    signal di_req_next : std_logic := '0';
210
    signal di_req_reg : std_logic := '0';
211 10 jdoin
    -- cross-clock do_valid_o logic
212
    signal do_valid_next : std_logic := '0';
213
    signal do_valid_A : std_logic := '0';
214
    signal do_valid_B : std_logic := '0';
215
    signal do_valid_C : std_logic := '0';
216
    signal do_valid_D : std_logic := '0';
217
    signal do_valid_o_reg : std_logic := '0';
218
    -- cross-clock di_req_o logic
219
    signal di_req_o_next : std_logic := '0';
220
    signal di_req_o_A : std_logic := '0';
221
    signal di_req_o_B : std_logic := '0';
222
    signal di_req_o_C : std_logic := '0';
223
    signal di_req_o_D : std_logic := '0';
224
    signal di_req_o_reg : std_logic := '0';
225
begin
226
    --=============================================================================================
227
    --  GENERICS CONSTRAINTS CHECKING
228
    --=============================================================================================
229
    -- minimum word width is 8 bits
230
    assert N >= 8
231
    report "Generic parameter 'N' error: SPI shift register size needs to be 8 bits minimum"
232
    severity FAILURE;
233
    -- maximum prefetch lookahead check
234
    assert PREFETCH <= N-5
235
    report "Generic parameter 'PREFETCH' error: lookahead count out of range, needs to be N-5 maximum"
236
    severity FAILURE;
237
 
238
    --=============================================================================================
239 12 jdoin
    --  GENERATE BLOCKS
240 10 jdoin
    --=============================================================================================
241
 
242
    --=============================================================================================
243 12 jdoin
    --  DATA INPUTS
244 10 jdoin
    --=============================================================================================
245 12 jdoin
    -- connect rx bit input
246
    rx_bit_proc : rx_bit_next <= spi_mosi_i;
247 10 jdoin
 
248
    --=============================================================================================
249
    --  CROSS-CLOCK PIPELINE TRANSFER LOGIC
250
    --=============================================================================================
251
    -- do_valid_o and di_req_o strobe output logic
252
    -- this is a delayed pulse generator with a ripple-transfer FFD pipeline, that generates a 
253
    -- fixed-length delayed pulse for the output flags, at the parallel clock domain
254 12 jdoin
    out_transfer_proc : process ( clk_i, do_transfer_reg, di_req_reg,
255 10 jdoin
                                  do_valid_A, do_valid_B, do_valid_D,
256
                                  di_req_o_A, di_req_o_B, di_req_o_D) is
257
    begin
258
        if clk_i'event and clk_i = '1' then                     -- clock at parallel port clock
259
            -- do_transfer_reg -> do_valid_o_reg
260
            do_valid_A <= do_transfer_reg;                      -- the input signal must be at least 2 clocks long
261
            do_valid_B <= do_valid_A;                           -- feed it to a ripple chain of FFDs
262
            do_valid_C <= do_valid_B;
263
            do_valid_D <= do_valid_C;
264
            do_valid_o_reg <= do_valid_next;                    -- registered output pulse
265
            --------------------------------
266 12 jdoin
            -- di_req_reg -> di_req_o_reg
267
            di_req_o_A <= di_req_reg;                           -- the input signal must be at least 2 clocks long
268 10 jdoin
            di_req_o_B <= di_req_o_A;                           -- feed it to a ripple chain of FFDs
269
            di_req_o_C <= di_req_o_B;
270
            di_req_o_D <= di_req_o_C;
271
            di_req_o_reg <= di_req_o_next;                      -- registered output pulse
272
        end if;
273
        -- generate a 2-clocks pulse at the 3rd clock cycle
274
        do_valid_next <= do_valid_A and do_valid_B and not do_valid_D;
275
        di_req_o_next <= di_req_o_A and di_req_o_B and not di_req_o_D;
276
    end process out_transfer_proc;
277
    -- parallel load input registers: data register and write enable
278 12 jdoin
    in_transfer_proc: process (clk_i, wren_i, wr_ack_reg) is
279 10 jdoin
    begin
280
        -- registered data input, input register with clock enable
281
        if clk_i'event and clk_i = '1' then
282
            if wren_i = '1' then
283
                di_reg <= di_i;                                 -- parallel data input buffer register
284
            end if;
285
        end  if;
286
        -- stretch wren pulse to be detected by spi fsm (ffd with sync preset and sync reset)
287
        if clk_i'event and clk_i = '1' then
288
            if wren_i = '1' then                                -- wren_i is the sync preset for wren
289
                wren <= '1';
290 12 jdoin
            elsif wr_ack_reg = '1' then                         -- wr_ack is the sync reset for wren
291 10 jdoin
                wren <= '0';
292
            end if;
293
        end  if;
294
    end process in_transfer_proc;
295
 
296
    --=============================================================================================
297 13 jdoin
    --  REGISTER TRANSFER PROCESSES
298 12 jdoin
    --=============================================================================================
299
    -- fsm state and data registers change on spi SHIFT_EDGE
300
    core_reg_proc : process (spi_sck_i, spi_ssel_i) is
301
    begin
302
        -- FFD registers clocked on SHIFT edge and cleared on idle (spi_ssel_i = 1)
303
        if spi_ssel_i = '1' then                                -- async clr
304
            state_reg <= 0;                                     -- state falls back to idle when slave not selected
305
        elsif spi_sck_i'event and spi_sck_i = SHIFT_EDGE then   -- on SHIFT edge, update all core registers
306
            state_reg <= state_next;                            -- core fsm changes state with spi SHIFT clock
307
        end if;
308
        -- FFD registers clocked on SHIFT edge
309
        if spi_sck_i'event and spi_sck_i = SHIFT_EDGE then      -- on fsm state change, update all core registers
310
            sh_reg <= sh_next;                                  -- core shift register
311
            do_buffer_reg <= do_buffer_next;                    -- registered data output
312
            do_transfer_reg <= do_transfer_next;                -- cross-clock transfer flag
313
            di_req_reg <= di_req_next;                          -- input data request
314
            wr_ack_reg <= wr_ack_next;                          -- wren ack for data load synchronization
315
        end if;
316
        -- FFD registers clocked on CHANGE edge
317
        if spi_sck_i'event and spi_sck_i = CHANGE_EDGE then
318
            tx_bit_reg <= tx_bit_next;                          -- update MISO driver from the MSb
319
        end if;
320
    end process core_reg_proc;
321
 
322
    --=============================================================================================
323 13 jdoin
    --  COMBINATORIAL LOGIC PROCESSES
324 10 jdoin
    --=============================================================================================
325 13 jdoin
    -- state and datapath combinatorial logic
326 12 jdoin
    core_combi_proc : process ( sh_reg, sh_next, state_reg, tx_bit_reg, rx_bit_next, do_buffer_reg,
327
                                do_transfer_reg, di_reg, di_req_reg, wren, wr_ack_reg) is
328 10 jdoin
    begin
329 12 jdoin
        -- all output signals are assigned to (avoid latches)
330
        sh_next <= sh_reg;                                              -- shift register
331
        tx_bit_next <= tx_bit_reg;                                      -- MISO driver
332 10 jdoin
        do_buffer_next <= do_buffer_reg;                                -- output data buffer
333
        do_transfer_next <= do_transfer_reg;                            -- output data flag
334 12 jdoin
        wr_ack_next <= wr_ack_reg;                                      -- write enable acknowledge
335
        di_req_next <= di_req_reg;                                      -- data input request
336
        state_next <= state_reg;                                        -- fsm control state
337 10 jdoin
        case state_reg is
338 13 jdoin
 
339 10 jdoin
            when (N) =>
340 13 jdoin
                -- stretch do_valid
341
                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 10 jdoin
            when (N-1) downto (PREFETCH+3) =>
349 12 jdoin
                -- send bit out and shif bit in
350 10 jdoin
                do_transfer_next <= '0';                                -- reset transfer signal
351 12 jdoin
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
352
                wr_ack_next <= '0';                                     -- remove data load ack for all but the load stages
353
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
354 10 jdoin
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
355 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
356
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
357 13 jdoin
 
358 12 jdoin
            when (PREFETCH+2) downto 3 =>
359 10 jdoin
                -- raise data prefetch request
360 12 jdoin
                di_req_next <= '1';                                     -- request data in advance to allow for pipeline delays
361
                wr_ack_next <= '0';                                     -- remove data load ack for all but the load stages
362
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
363 10 jdoin
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
364 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
365
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
366 13 jdoin
 
367 12 jdoin
            when 2 =>
368
                -- transfer parallel data on next state
369
                di_req_next <= '1';                                     -- request data in advance to allow for pipeline delays
370
                wr_ack_next <= '0';                                     -- remove data load ack for all but the load stages
371
                tx_bit_next <= sh_reg(N-1);                             -- output next MSbit
372
                sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0);          -- shift inner bits
373
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
374
                do_transfer_next <= '1';                                -- signal transfer to do_buffer on next cycle
375
                do_buffer_next <= sh_next;                              -- get next data directly into rx buffer
376
                state_next <= state_reg - 1;                            -- update next state at each sck pulse
377 13 jdoin
 
378 10 jdoin
            when 1 =>
379
                -- restart from state 'N' if more sck pulses come
380 12 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
381
                sh_next(N-1 downto 1) <= di_reg(N-2 downto 0);          -- shift inner bits
382
                tx_bit_next <= di_reg(N-1);                             -- first output bit comes from the MSb of parallel data
383
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
384 10 jdoin
                if wren = '1' then                                      -- load tx register if valid data present at di_reg
385 12 jdoin
                    wr_ack_next <= '1';                                 -- acknowledge data in transfer
386
                    state_next <= N;                                    -- next state is top bit of new data
387 10 jdoin
                else
388 12 jdoin
                    wr_ack_next <= '0';                                 -- remove data load ack for all but the load stages
389 10 jdoin
                    sh_next <= (others => '0');                         -- load null data (output '0' if no load)
390 12 jdoin
                    state_next <= 0;                                    -- next state is idle state
391 10 jdoin
                end if;
392 13 jdoin
 
393 10 jdoin
            when 0 =>
394 13 jdoin
                sh_next(0) <= rx_bit_next;                              -- shift in rx bit into LSb
395
                sh_next(N-1 downto 1) <= di_reg(N-2 downto 0);          -- shift inner bits
396
                wr_ack_next <= '1';                                     -- acknowledge data in transfer
397
                di_req_next <= '0';                                     -- prefetch data request: deassert when shifting data
398 12 jdoin
                do_transfer_next <= '0';                                -- clear signal transfer to do_buffer
399
                tx_bit_next <= di_reg(N-1);                             -- first output bit comes from the MSb of parallel data
400
                state_next <= N;                                        -- next state is top bit of new data
401 13 jdoin
 
402 10 jdoin
            when others =>
403 12 jdoin
                state_next <= 0;                                        -- safe state
404 13 jdoin
 
405 10 jdoin
        end case;
406
    end process core_combi_proc;
407
 
408
    --=============================================================================================
409 13 jdoin
    --  OUTPUT LOGIC PROCESSES
410 10 jdoin
    --=============================================================================================
411
    -- data output processes
412 12 jdoin
    do_o_proc :         do_o <= do_buffer_reg;                          -- do_o always available
413
    do_valid_o_proc:    do_valid_o <= do_valid_o_reg;                   -- copy registered do_valid_o to output
414
    di_req_o_proc:      di_req_o <= di_req_o_reg;                       -- copy registered di_req_o to output
415
    wr_ack_o_proc:      wr_ack_o <= wr_ack_reg;                         -- copy registered wr_ack_o to output
416 10 jdoin
 
417 13 jdoin
    -----------------------------------------------------------------------------------------------
418
    -- MISO driver process: copy next tx bit at reset
419
    -----------------------------------------------------------------------------------------------
420
    -- this is a MUX that selects the combinatorial next tx bit at reset, and the registered tx bit
421
    -- at sequential operation. The mux gives us a preload of the first bit, simplifying the shifter logic.
422
    spi_miso_o_proc: process (spi_ssel_i, tx_bit_reg, tx_bit_next) is
423
    begin
424
        if spi_ssel_i = '1' then
425
            spi_miso_o <= tx_bit_next;                                  -- copy next => reg at reset
426
        else
427
            spi_miso_o <= tx_bit_reg;
428
        end if;
429
    end process spi_miso_o_proc;
430
 
431 10 jdoin
    --=============================================================================================
432
    --  DEBUG LOGIC PROCESSES
433
    --=============================================================================================
434 13 jdoin
    -- these signals are useful for verification, and can be deleted after debug.
435 10 jdoin
    do_transfer_proc:   do_transfer_o <= do_transfer_reg;
436 13 jdoin
    state_debug_proc:   state_dbg_o <= std_logic_vector(to_unsigned(state_reg, 4)); -- export internal state to debug
437 12 jdoin
    rx_bit_next_proc:   rx_bit_next_o <= rx_bit_next;
438 10 jdoin
    wren_o_proc:        wren_o <= wren;
439
    sh_reg_debug_proc:  sh_reg_dbg_o <= sh_reg;                                     -- export sh_reg to debug
440 13 jdoin
end architecture rtl;
441 10 jdoin
 

powered by: WebSVN 2.1.0

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