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 12

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

powered by: WebSVN 2.1.0

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