URL
https://opencores.org/ocsvn/spi_master_slave/spi_master_slave/trunk
Subversion Repositories spi_master_slave
Compare Revisions
- This comparison shows the changes necessary to convert path
/spi_master_slave/trunk/rtl
- from Rev 11 to Rev 12
- ↔ Reverse comparison
Rev 11 → Rev 12
/spi_slave.vhd
105,9 → 105,16
-- 2011/06/09 v0.97.0068 [JD] reduced control sets (resets, CE, presets) to the absolute minimum to operate, to reduce |
-- synthesis LUT overhead in Spartan-6 architecture. |
-- 2011/06/11 v0.97.0075 [JD] redesigned all parallel data interfacing ports, and implemented cross-clock strobe logic. |
-- 2011/06/12 v0.97.0079 [JD] implemented wren_ack and di_req logic for state 0, and eliminated unnecessary registers reset. |
-- 2011/06/17 v0.97.0079 [JD] implemented wren_ack and di_req logic for state 0, and eliminated unnecessary registers reset. |
-- 2011/06/12 v0.97.0079 [JD] implemented wr_ack and di_req logic for state 0, and eliminated unnecessary registers reset. |
-- 2011/06/17 v0.97.0079 [JD] implemented wr_ack and di_req logic for state 0, and eliminated unnecessary registers reset. |
-- 2011/07/16 v1.11.0080 [JD] verified both spi_master and spi_slave in loopback at 50MHz SPI clock. |
-- 2011/07/29 v2.00.0110 [JD] FIX: CPHA bugs: |
-- - redesigned core clocking to address all CPOL and CPHA configurations. |
-- - added CHANGE_EDGE to the FSM register transfer logic, to have MISO change at opposite |
-- clock phases from SHIFT_EDGE. |
-- Removed global signal setting at the FSM, implementing exhaustive explicit signal attributions |
-- for each state, to avoid reported inference problems in some synthesis engines. |
-- Streamlined port names and indentation blocks. |
-- |
-- |
----------------------------------------------------------------------------------------------------------------------- |
136,13 → 143,13
di_req_o : out std_logic; -- preload lookahead data request line |
di_i : in std_logic_vector (N-1 downto 0) := (others => 'X'); -- parallel load data in (clocked in on rising edge of clk_i) |
wren_i : in std_logic := 'X'; -- user data write enable |
wr_ack_o : out std_logic; -- write acknowledge |
do_valid_o : out std_logic; -- do_o data valid strobe, valid during one clk_i rising edge. |
do_o : out std_logic_vector (N-1 downto 0); -- parallel output (clocked out on falling clk_i) |
--- debug ports: can be removed for the application circuit --- |
do_transfer_o : out std_logic; -- debug: internal transfer driver |
wren_o : out std_logic; -- debug: internal state of the wren_i pulse stretcher |
wren_ack_o : out std_logic; -- debug: wren ack from state machine |
rx_bit_reg_o : out std_logic; -- debug: internal rx bit |
rx_bit_next_o : out std_logic; -- debug: internal rx bit |
state_dbg_o : out std_logic_vector (5 downto 0); -- debug: internal state register |
sh_reg_dbg_o : out std_logic_vector (N-1 downto 0) -- debug: internal shift register |
); |
160,10 → 167,10
|
architecture RTL of spi_slave is |
-- constants to control FlipFlop synthesis |
constant SAMPLE_EDGE : std_logic := (CPOL xnor CPHA); |
constant SAMPLE_LEVEL : std_logic := SAMPLE_EDGE; |
constant SHIFT_EDGE : std_logic := (CPOL xor CPHA); |
-- |
constant SHIFT_EDGE : std_logic := (CPOL xnor CPHA); -- MOSI data is captured and shifted at this SCK edge |
constant CHANGE_EDGE : std_logic := (CPOL xor CPHA); -- MISO data is updated at this SCK edge |
|
------------------------------------------------------------------------------------------ |
-- GLOBAL RESET: |
-- all signals are initialized to zero at GSR (global set/reset) by giving explicit |
-- initialization values at declaration. This is needed for all Xilinx FPGAs, and |
172,21 → 179,23
-- set (RESET/PRESET, CLOCK ENABLE and CLOCK) by all 8 registers in a slice. |
-- By using GSR for the initialization, and reducing RESET local init to the bare |
-- essential, the model achieves better LUT/FF packing and CLB usability. |
-- |
------------------------------------------------------------------------------------------ |
-- internal state signals for register and combinational stages |
signal state_next : natural range N+1 downto 0 := 0; |
signal state_reg : natural range N+1 downto 0 := 0; |
signal state_next : natural range N downto 0 := 0; -- state 0 is idle state |
signal state_reg : natural range N downto 0 := 0; -- state 0 is idle state |
-- shifter signals for register and combinational stages |
signal sh_next : std_logic_vector (N-1 downto 0) := (others => '0'); |
signal sh_reg : std_logic_vector (N-1 downto 0) := (others => '0'); |
-- input bit sampled buffer |
signal rx_bit_reg : std_logic := '0'; |
-- mosi and miso connections |
signal rx_bit_next : std_logic := '0'; |
signal tx_bit_next : std_logic := '0'; |
signal tx_bit_reg : std_logic := '0'; |
-- buffered di_i data signals for register and combinational stages |
signal di_reg : std_logic_vector (N-1 downto 0) := (others => '0'); |
signal di_reg : std_logic_vector (N-1 downto 0); |
-- internal wren_i stretcher for fsm combinational stage |
signal wren : std_logic := '0'; |
signal wren_ack_next : std_logic := '0'; |
signal wren_ack_reg : std_logic := '0'; |
signal wren : std_logic; |
signal wr_ack_next : std_logic := '0'; |
signal wr_ack_reg : std_logic := '0'; |
-- buffered do_o data signals for register and combinational stages |
signal do_buffer_next : std_logic_vector (N-1 downto 0) := (others => '0'); |
signal do_buffer_reg : std_logic_vector (N-1 downto 0) := (others => '0'); |
194,7 → 203,8
signal do_transfer_next : std_logic := '0'; |
signal do_transfer_reg : std_logic := '0'; |
-- internal input data request signal |
signal di_req : std_logic := '0'; |
signal di_req_next : std_logic := '0'; |
signal di_req_reg : std_logic := '0'; |
-- cross-clock do_valid_o logic |
signal do_valid_next : std_logic := '0'; |
signal do_valid_A : std_logic := '0'; |
223,36 → 233,14
severity FAILURE; |
|
--============================================================================================= |
-- REGISTERED INPUTS |
-- GENERATE BLOCKS |
--============================================================================================= |
-- rx bit flop: capture rx bit after SAMPLE edge of sck |
rx_bit_proc : process (spi_sck_i, spi_mosi_i) is |
begin |
if spi_sck_i'event and spi_sck_i = SAMPLE_EDGE then |
rx_bit_reg <= spi_mosi_i; |
end if; |
end process rx_bit_proc; |
|
--============================================================================================= |
-- RTL CORE REGISTER PROCESSES |
-- DATA INPUTS |
--============================================================================================= |
-- fsm state and data registers change on spi SHIFT clock |
core_reg_proc : process (spi_sck_i, spi_ssel_i) is |
begin |
-- FFD registers clocked on SHIFT edge and cleared on idle (spi_ssel_i = 1) |
if spi_ssel_i = '1' then -- async clr |
state_reg <= 0; -- state falls back to idle when slave not selected |
elsif spi_sck_i'event and spi_sck_i = SHIFT_EDGE then -- on SHIFT edge, update all core registers |
state_reg <= state_next; -- core fsm changes state with spi SHIFT clock |
end if; |
-- FFD registers clocked on SHIFT edge |
if spi_sck_i'event and spi_sck_i = SHIFT_EDGE then -- on fsm state change, update all core registers |
sh_reg <= sh_next; -- core shift register |
do_buffer_reg <= do_buffer_next; -- registered data output |
do_transfer_reg <= do_transfer_next; -- cross-clock transfer flag |
wren_ack_reg <= wren_ack_next; -- wren ack for data load synchronization |
end if; |
end process core_reg_proc; |
-- connect rx bit input |
rx_bit_proc : rx_bit_next <= spi_mosi_i; |
|
--============================================================================================= |
-- CROSS-CLOCK PIPELINE TRANSFER LOGIC |
260,7 → 248,7
-- do_valid_o and di_req_o strobe output logic |
-- this is a delayed pulse generator with a ripple-transfer FFD pipeline, that generates a |
-- fixed-length delayed pulse for the output flags, at the parallel clock domain |
out_transfer_proc : process ( clk_i, do_transfer_reg, di_req, |
out_transfer_proc : process ( clk_i, do_transfer_reg, di_req_reg, |
do_valid_A, do_valid_B, do_valid_D, |
di_req_o_A, di_req_o_B, di_req_o_D) is |
begin |
272,8 → 260,8
do_valid_D <= do_valid_C; |
do_valid_o_reg <= do_valid_next; -- registered output pulse |
-------------------------------- |
-- di_req -> di_req_o_reg |
di_req_o_A <= di_req; -- the input signal must be at least 2 clocks long |
-- di_req_reg -> di_req_o_reg |
di_req_o_A <= di_req_reg; -- the input signal must be at least 2 clocks long |
di_req_o_B <= di_req_o_A; -- feed it to a ripple chain of FFDs |
di_req_o_C <= di_req_o_B; |
di_req_o_D <= di_req_o_C; |
284,7 → 272,7
di_req_o_next <= di_req_o_A and di_req_o_B and not di_req_o_D; |
end process out_transfer_proc; |
-- parallel load input registers: data register and write enable |
in_transfer_proc: process (clk_i, wren_i, wren_ack_reg) is |
in_transfer_proc: process (clk_i, wren_i, wr_ack_reg) is |
begin |
-- registered data input, input register with clock enable |
if clk_i'event and clk_i = '1' then |
296,7 → 284,7
if clk_i'event and clk_i = '1' then |
if wren_i = '1' then -- wren_i is the sync preset for wren |
wren <= '1'; |
elsif wren_ack_reg = '1' then -- wren_ack is the sync reset for wren |
elsif wr_ack_reg = '1' then -- wr_ack is the sync reset for wren |
wren <= '0'; |
end if; |
end if; |
303,63 → 291,115
end process in_transfer_proc; |
|
--============================================================================================= |
-- RTL CORE REGISTER PROCESSES |
--============================================================================================= |
-- fsm state and data registers change on spi SHIFT_EDGE |
core_reg_proc : process (spi_sck_i, spi_ssel_i) is |
begin |
-- FFD registers clocked on SHIFT edge and cleared on idle (spi_ssel_i = 1) |
if spi_ssel_i = '1' then -- async clr |
state_reg <= 0; -- state falls back to idle when slave not selected |
elsif spi_sck_i'event and spi_sck_i = SHIFT_EDGE then -- on SHIFT edge, update all core registers |
state_reg <= state_next; -- core fsm changes state with spi SHIFT clock |
end if; |
-- FFD registers clocked on SHIFT edge |
if spi_sck_i'event and spi_sck_i = SHIFT_EDGE then -- on fsm state change, update all core registers |
sh_reg <= sh_next; -- core shift register |
do_buffer_reg <= do_buffer_next; -- registered data output |
do_transfer_reg <= do_transfer_next; -- cross-clock transfer flag |
di_req_reg <= di_req_next; -- input data request |
wr_ack_reg <= wr_ack_next; -- wren ack for data load synchronization |
end if; |
-- FFD registers clocked on CHANGE edge |
if spi_sck_i'event and spi_sck_i = CHANGE_EDGE then |
tx_bit_reg <= tx_bit_next; -- update MISO driver from the MSb |
end if; |
end process core_reg_proc; |
|
--============================================================================================= |
-- RTL COMBINATIONAL LOGIC PROCESSES |
--============================================================================================= |
-- state and datapath combinational logic |
core_combi_proc : process ( sh_reg, state_reg, rx_bit_reg, do_buffer_reg, |
do_transfer_reg, di_reg, wren, wren_ack_reg) is |
core_combi_proc : process ( sh_reg, sh_next, state_reg, tx_bit_reg, rx_bit_next, do_buffer_reg, |
do_transfer_reg, di_reg, di_req_reg, wren, wr_ack_reg) is |
begin |
sh_next <= sh_reg; -- all output signals are assigned to (avoid latches) |
-- all output signals are assigned to (avoid latches) |
sh_next <= sh_reg; -- shift register |
tx_bit_next <= tx_bit_reg; -- MISO driver |
do_buffer_next <= do_buffer_reg; -- output data buffer |
do_transfer_next <= do_transfer_reg; -- output data flag |
wren_ack_next <= '0'; -- remove data load ack for all but the load stages |
di_req <= '0'; -- prefetch data request: deassert when shifting data |
spi_miso_o <= sh_reg(N-1); -- output serial data from the MSb |
state_next <= state_reg - 1; -- update next state at each sck pulse |
wr_ack_next <= wr_ack_reg; -- write enable acknowledge |
di_req_next <= di_req_reg; -- data input request |
state_next <= state_reg; -- fsm control state |
case state_reg is |
when (N) => |
-- acknowledge write enable |
wr_ack_next <= '1'; -- acknowledge data in transfer |
do_transfer_next <= '0'; -- reset transfer signal |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
tx_bit_next <= sh_reg(N-1); -- output next MSbit |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
sh_next(0) <= rx_bit_next; -- shift in rx bit into LSb |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when (N-1) downto (PREFETCH+3) => |
-- send bit out and shif bit in |
do_transfer_next <= '0'; -- reset transfer signal |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
wr_ack_next <= '0'; -- remove data load ack for all but the load stages |
tx_bit_next <= sh_reg(N-1); -- output next MSbit |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
when (PREFETCH+2) downto 2 => |
sh_next(0) <= rx_bit_next; -- shift in rx bit into LSb |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when (PREFETCH+2) downto 3 => |
-- raise data prefetch request |
di_req <= '1'; -- request data in advance to allow for pipeline delays |
di_req_next <= '1'; -- request data in advance to allow for pipeline delays |
wr_ack_next <= '0'; -- remove data load ack for all but the load stages |
tx_bit_next <= sh_reg(N-1); -- output next MSbit |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
sh_next(0) <= rx_bit_next; -- shift in rx bit into LSb |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when 2 => |
-- transfer parallel data on next state |
di_req_next <= '1'; -- request data in advance to allow for pipeline delays |
wr_ack_next <= '0'; -- remove data load ack for all but the load stages |
tx_bit_next <= sh_reg(N-1); -- output next MSbit |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_next; -- shift in rx bit into LSb |
do_transfer_next <= '1'; -- signal transfer to do_buffer on next cycle |
do_buffer_next <= sh_next; -- get next data directly into rx buffer |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when 1 => |
-- restart from state 'N' if more sck pulses come |
di_req <= '1'; -- request data in advance to allow for pipeline delays |
do_buffer_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift rx data directly into rx buffer |
do_buffer_next(0) <= rx_bit_reg; -- shift last rx bit into rx buffer |
do_transfer_next <= '1'; -- signal transfer to do_buffer |
state_next <= N; -- next state is top bit of new data |
sh_next(0) <= rx_bit_next; -- shift in rx bit into LSb |
sh_next(N-1 downto 1) <= di_reg(N-2 downto 0); -- shift inner bits |
tx_bit_next <= di_reg(N-1); -- first output bit comes from the MSb of parallel data |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
do_transfer_next <= '0'; -- clear signal transfer to do_buffer |
if wren = '1' then -- load tx register if valid data present at di_reg |
sh_next <= di_reg; -- load parallel data from di_reg into shifter |
wren_ack_next <= '1'; -- acknowledge data in transfer |
wr_ack_next <= '1'; -- acknowledge data in transfer |
state_next <= N; -- next state is top bit of new data |
else |
wr_ack_next <= '0'; -- remove data load ack for all but the load stages |
sh_next <= (others => '0'); -- load null data (output '0' if no load) |
state_next <= 0; -- next state is idle state |
end if; |
when 0 => |
di_req <= not wren_ack_reg; -- will request data if shifter empty |
do_transfer_next <= '0'; -- clear signal transfer to do_buffer |
spi_miso_o <= di_reg(N-1); -- shift out first tx bit from the MSb |
if CPHA = '0' then |
-- initial state for CPHA=0, when slave interface is first selected or idle |
state_next <= N-1; -- next state is top bit of new data |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
-- idle state: start and end of transmission |
if CPHA = '1' then |
wr_ack_next <= '1'; -- acknowledge data in transfer |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
sh_next(0) <= rx_bit_next; -- shift in rx bit into LSb |
sh_next(N-1 downto 1) <= di_reg(N-2 downto 0); -- shift inner bits |
wren_ack_next <= '1'; -- acknowledge data in transfer |
else |
-- initial state for CPHA=1, when slave interface is first selected or idle |
state_next <= N; -- next state is top bit of new data |
wr_ack_next <= '1'; -- acknowledge data in transfer |
di_req_next <= not wr_ack_reg; -- will request data if shifter empty |
sh_next <= di_reg; -- load parallel data from di_reg into shifter |
end if; |
end if; |
do_transfer_next <= '0'; -- clear signal transfer to do_buffer |
tx_bit_next <= di_reg(N-1); -- first output bit comes from the MSb of parallel data |
state_next <= N; -- next state is top bit of new data |
when others => |
state_next <= 0; -- state 0 is safe state |
state_next <= 0; -- safe state |
end case; |
end process core_combi_proc; |
|
367,9 → 407,11
-- RTL OUTPUT LOGIC PROCESSES |
--============================================================================================= |
-- data output processes |
do_o_proc : do_o <= do_buffer_reg; -- do_o always available |
do_valid_o_proc: do_valid_o <= do_valid_o_reg; -- copy registered do_valid_o to output |
di_req_o_proc: di_req_o <= di_req_o_reg; -- copy registered di_req_o to output |
spi_miso_o_proc: spi_miso_o <= tx_bit_reg; -- connect MISO driver |
do_o_proc : do_o <= do_buffer_reg; -- do_o always available |
do_valid_o_proc: do_valid_o <= do_valid_o_reg; -- copy registered do_valid_o to output |
di_req_o_proc: di_req_o <= di_req_o_reg; -- copy registered di_req_o to output |
wr_ack_o_proc: wr_ack_o <= wr_ack_reg; -- copy registered wr_ack_o to output |
|
--============================================================================================= |
-- DEBUG LOGIC PROCESSES |
377,9 → 419,8
-- these signals are useful for verification, and can be deleted or commented-out after debug. |
do_transfer_proc: do_transfer_o <= do_transfer_reg; |
state_debug_proc: state_dbg_o <= std_logic_vector(to_unsigned(state_reg, 6)); -- export internal state to debug |
rx_bit_reg_proc: rx_bit_reg_o <= rx_bit_reg; |
rx_bit_next_proc: rx_bit_next_o <= rx_bit_next; |
wren_o_proc: wren_o <= wren; |
wren_ack_o_proc: wren_ack_o <= wren_ack_reg; |
sh_reg_debug_proc: sh_reg_dbg_o <= sh_reg; -- export sh_reg to debug |
end architecture RTL; |
|
/spi_master.vhd
1,5 → 1,5
----------------------------------------------------------------------------------------------------------------------- |
-- Author: Jonny Doin, jdoin@opencores.org |
-- Author: Jonny Doin, jdoin@opencores.org, jonnydoin@gmail.com |
-- |
-- Create Date: 12:18:12 04/25/2011 |
-- Module Name: SPI_MASTER - RTL |
124,7 → 124,7
-- 2011/06/09 v0.97.0068 [JD] reduced control sets (resets, CE, presets) to the absolute minimum to operate, to reduce |
-- synthesis LUT overhead in Spartan-6 architecture. |
-- 2011/06/11 v0.97.0075 [JD] redesigned all parallel data interfacing ports, and implemented cross-clock strobe logic. |
-- 2011/06/12 v0.97.0079 [JD] streamlined wren_ack for all cases and eliminated unnecessary register resets. |
-- 2011/06/12 v0.97.0079 [JD] streamlined wr_ack for all cases and eliminated unnecessary register resets. |
-- 2011/06/14 v0.97.0083 [JD] (bug CPHA effect) : redesigned SCK output circuit. |
-- (minor bug) : removed fsm registers from (not rst_i) chip enable. |
-- 2011/06/15 v0.97.0086 [JD] removed master MISO input register, to relax MISO data setup time (to get higher speed). |
138,6 → 138,11
-- BUG: CPOL='0', CPHA='1' causes SCK to have one extra pulse with one sclk_i width at the end. |
-- 2011/07/18 v1.12.0105 [JD] CHG: spi sck output register changed to remove glitch at last clock when CPHA='1'. |
-- for CPHA='1', max spi clock is 25MHz. for CPHA= '0', max spi clock is >50MHz. |
-- 2011/07/24 v1.13.0125 [JD] FIX: 'sck_ena_ce' is on half-cycle advanced to 'fsm_ce', elliminating CPHA='1' glitches. |
-- Core verified for all CPOL, CPHA at up to 50MHz, simulates to over 100MHz. |
-- 2011/07/29 v1.14.0130 [JD] Removed global signal setting at the FSM, implementing exhaustive explicit signal attributions |
-- for each state, to avoid reported inference problems in some synthesis engines. |
-- Streamlined port names and indentation blocks. |
-- |
----------------------------------------------------------------------------------------------------------------------- |
-- TODO |
179,12 → 184,14
di_req_o : out std_logic; -- preload lookahead data request line |
di_i : in std_logic_vector (N-1 downto 0) := (others => 'X'); -- parallel data in (clocked on rising spi_clk after last bit) |
wren_i : in std_logic := 'X'; -- user data write enable, starts transmission when interface is idle |
wr_ack_o : out std_logic; -- write acknowledge |
do_valid_o : out std_logic; -- do_o data valid signal, valid during one spi_clk rising edge. |
do_o : out std_logic_vector (N-1 downto 0); -- parallel output (clocked on rising spi_clk after last bit) |
--- debug ports: can be removed or left unconnected for the application circuit --- |
sck_ena_o : out std_logic; -- debug: internal sck enable signal |
sck_ena_ce_o : out std_logic; -- debug: internal sck clock enable signal |
do_transfer_o : out std_logic; -- debug: internal transfer driver |
wren_o : out std_logic; -- debug: internal state of the wren_i pulse stretcher |
wren_ack_o : out std_logic; -- debug: wren ack from state machine |
rx_bit_reg_o : out std_logic; -- debug: internal rx bit |
state_dbg_o : out std_logic_vector (5 downto 0); -- debug: internal state register |
core_clk_o : out std_logic; |
208,10 → 215,10
-- spi bus clock, generated from the CPOL selected core clock polarity |
signal spi_2x_ce : std_logic := '1'; -- spi_2x clock enable |
signal spi_clk : std_logic := '0'; -- spi bus output clock |
signal spi_clk_reg : std_logic := '0'; -- output pipeline delay for spi sck |
signal spi_clk_reg : std_logic; -- output pipeline delay for spi sck (do NOT global initialize) |
-- core fsm clock enables |
signal fsm_ce : std_logic := '1'; -- fsm clock enable |
signal ena_sck_ce : std_logic := '1'; -- SCK clock enable |
signal sck_ena_ce : std_logic := '1'; -- SCK clock enable |
signal samp_ce : std_logic := '1'; -- data sampling clock enable |
-- |
-- GLOBAL RESET: |
235,14 → 242,14
signal di_reg : std_logic_vector (N-1 downto 0) := (others => '0'); |
-- internal wren_i stretcher for fsm combinatorial stage |
signal wren : std_logic := '0'; |
signal wren_ack_next : std_logic := '0'; |
signal wren_ack_reg : std_logic := '0'; |
signal wr_ack_next : std_logic := '0'; |
signal wr_ack_reg : std_logic := '0'; |
-- internal SSEL enable control signals |
signal ena_ssel_next : std_logic := '0'; |
signal ena_ssel_reg : std_logic := '0'; |
signal ssel_ena_next : std_logic := '0'; |
signal ssel_ena_reg : std_logic := '0'; |
-- internal SCK enable control signals |
signal ena_sck_next : std_logic := '0'; |
signal ena_sck_reg : std_logic := '0'; |
signal sck_ena_next : std_logic; |
signal sck_ena_reg : std_logic; |
-- buffered do_o data signals for register and combinatorial stages |
signal do_buffer_next : std_logic_vector (N-1 downto 0) := (others => '0'); |
signal do_buffer_reg : std_logic_vector (N-1 downto 0) := (others => '0'); |
336,45 → 343,46
end if; |
end if; |
end process core_clock_gen_proc; |
----------------------------------------------------------------------------------------------- |
|
--============================================================================================= |
-- GENERATE BLOCKS |
--============================================================================================= |
-- spi clk generator: generate spi_clk from core_clk depending on CPOL |
spi_sck_cpol_0_proc : |
if CPOL = '0' generate |
begin |
spi_clk <= core_clk; -- for CPOL=0, spi clk has idle LOW |
end generate; |
spi_sck_cpol_1_proc : |
if CPOL = '1' generate |
begin |
spi_clk <= core_n_clk; -- for CPOL=1, spi clk has idle HIGH |
end generate; |
spi_sck_cpol_0_proc: if CPOL = '0' generate |
begin |
spi_clk <= core_clk; -- for CPOL=0, spi clk has idle LOW |
end generate; |
|
spi_sck_cpol_1_proc: if CPOL = '1' generate |
begin |
spi_clk <= core_n_clk; -- for CPOL=1, spi clk has idle HIGH |
end generate; |
|
----------------------------------------------------------------------------------------------- |
-- Sampling clock enable generation: generate 'samp_ce' from 'core_ce' or 'core_n_ce' depending on CPHA |
-- always sample data at the half-cycle of the fsm update cell |
samp_ce_cpha_0_proc : |
if CPHA = '0' generate |
begin |
samp_ce <= core_ce; |
end generate; |
samp_ce_cpha_1_proc : |
if CPHA = '1' generate |
begin |
samp_ce <= core_n_ce; |
end generate; |
samp_ce_cpha_0_proc: if CPHA = '0' generate |
begin |
samp_ce <= core_ce; |
end generate; |
|
samp_ce_cpha_1_proc: if CPHA = '1' generate |
begin |
samp_ce <= core_n_ce; |
end generate; |
----------------------------------------------------------------------------------------------- |
-- FSM clock enable generation: generate 'fsm_ce' from core_ce or core_n_ce depending on CPHA |
fsm_ce_cpha_0_proc : |
if CPHA = '0' generate |
begin |
fsm_ce <= core_n_ce; -- for CPHA=0, latch registers at rising edge of negative core clock enable |
end generate; |
fsm_ce_cpha_1_proc : |
if CPHA = '1' generate |
begin |
fsm_ce <= core_ce; -- for CPHA=1, latch registers at rising edge of positive core clock enable |
end generate; |
fsm_ce_cpha_0_proc: if CPHA = '0' generate |
begin |
fsm_ce <= core_n_ce; -- for CPHA=0, latch registers at rising edge of negative core clock enable |
end generate; |
|
fsm_ce_cpha_1_proc: if CPHA = '1' generate |
begin |
fsm_ce <= core_ce; -- for CPHA=1, latch registers at rising edge of positive core clock enable |
end generate; |
|
ena_sck_ce <= core_n_ce; -- for CPHA=1, SCK is advanced one-half cycle |
sck_ena_ce <= core_n_ce; -- for CPHA=1, SCK is advanced one-half cycle |
|
--============================================================================================= |
-- REGISTERED INPUTS |
423,7 → 431,7
di_req_o_next <= di_req_o_A and di_req_o_B and not di_req_o_D; |
end process out_transfer_proc; |
-- parallel load input registers: data register and write enable |
in_transfer_proc: process ( pclk_i, wren_i, wren_ack_reg ) is |
in_transfer_proc: process ( pclk_i, wren_i, wr_ack_reg ) is |
begin |
-- registered data input, input register with clock enable |
if pclk_i'event and pclk_i = '1' then |
435,7 → 443,7
if pclk_i'event and pclk_i = '1' then |
if wren_i = '1' then -- wren_i is the sync preset for wren |
wren <= '1'; |
elsif wren_ack_reg = '1' then -- wren_ack is the sync reset for wren |
elsif wr_ack_reg = '1' then -- wr_ack is the sync reset for wren |
wren <= '0'; |
end if; |
end if; |
459,20 → 467,19
if sclk_i'event and sclk_i = '1' then |
if fsm_ce = '1' then |
sh_reg <= sh_next; -- shift register |
ena_ssel_reg <= ena_ssel_next; -- spi select enable |
ena_sck_reg <= ena_sck_next; -- spi clock enable |
ssel_ena_reg <= ssel_ena_next; -- spi select enable |
do_buffer_reg <= do_buffer_next; -- registered output data buffer |
do_transfer_reg <= do_transfer_next; -- output data transferred to buffer |
di_req_reg <= di_req_next; -- input data request |
wren_ack_reg <= wren_ack_next; -- wren ack for data load synchronization |
wr_ack_reg <= wr_ack_next; -- write acknowledge for data load synchronization |
end if; |
end if; |
-- FF registers clocked one-half cycle earlier than the fsm state |
-- if sclk_i'event and sclk_i = '1' then |
-- if ena_sck_ce = '1' then |
-- ena_sck_reg <= ena_sck_next; -- spi clock enable |
-- end if; |
-- end if; |
if sclk_i'event and sclk_i = '1' then |
if sck_ena_ce = '1' then |
sck_ena_reg <= sck_ena_next; -- spi clock enable: look ahead logic |
end if; |
end if; |
end process core_reg_proc; |
|
--============================================================================================= |
479,33 → 486,44
-- RTL combinatorial LOGIC PROCESSES |
--============================================================================================= |
-- state and datapath combinatorial logic |
core_combi_proc : process ( sh_reg, state_reg, rx_bit_reg, ena_ssel_reg, ena_sck_reg, do_buffer_reg, |
do_transfer_reg, di_reg, wren ) is |
core_combi_proc : process ( sh_reg, state_reg, rx_bit_reg, ssel_ena_reg, sck_ena_reg, do_buffer_reg, |
do_transfer_reg, wr_ack_reg, di_req_reg, di_reg, wren ) is |
begin |
sh_next <= sh_reg; -- all output signals are assigned to (avoid latches) |
ena_ssel_next <= ena_ssel_reg; -- controls the slave select line |
ena_sck_next <= ena_sck_reg; -- controls the clock enable of spi sck line |
ssel_ena_next <= ssel_ena_reg; -- controls the slave select line |
sck_ena_next <= sck_ena_reg; -- controls the clock enable of spi sck line |
do_buffer_next <= do_buffer_reg; -- output data buffer |
do_transfer_next <= do_transfer_reg; -- output data flag |
wren_ack_next <= '0'; -- remove data load ack for all but the load stages |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
wr_ack_next <= wr_ack_reg; -- write acknowledge |
di_req_next <= di_req_reg; -- prefetch data request |
state_next <= state_reg; -- next state |
spi_mosi_o <= sh_reg(N-1); -- shift out tx bit from the MSb |
state_next <= state_reg - 1; -- update next state at each sck pulse |
case state_reg is |
when (N+1) => -- this state is to enable SSEL before SCK |
ena_ssel_next <= '1'; -- tx in progress: will assert SSEL |
ena_sck_next <= '1'; -- enable SCK on next cycle (stays off on first SSEL clock cycle) |
ssel_ena_next <= '1'; -- tx in progress: will assert SSEL |
sck_ena_next <= '1'; -- enable SCK on next cycle (stays off on first SSEL clock cycle) |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when (N) => -- deassert 'di_rdy' |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when (N-1) downto (PREFETCH+3) => -- if rx data is valid, raise 'do_valid'. remove 'do_transfer' |
di_req_next <= '0'; -- prefetch data request: deassert when shifting data |
do_transfer_next <= '0'; -- reset transfer signal |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when (PREFETCH+2) downto 2 => -- raise prefetch 'di_req_o_next' signal and remove 'do_valid' |
di_req_next <= '1'; -- request data in advance to allow for pipeline delays |
sh_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift inner bits |
sh_next(0) <= rx_bit_reg; -- shift in rx bit into LSb |
wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages |
state_next <= state_reg - 1; -- update next state at each sck pulse |
when 1 => -- transfer rx data to do_buffer and restart if wren |
di_req_next <= '1'; -- request data in advance to allow for pipeline delays |
do_buffer_next(N-1 downto 1) <= sh_reg(N-2 downto 0); -- shift rx data directly into rx buffer |
514,22 → 532,25
if wren = '1' then -- load tx register if valid data present at di_i |
state_next <= N; -- next state is top bit of new data |
sh_next <= di_reg; -- load parallel data from di_reg into shifter |
ena_sck_next <= '1'; -- SCK enabled |
wren_ack_next <= '1'; -- acknowledge data in transfer |
sck_ena_next <= '1'; -- SCK enabled |
wr_ack_next <= '1'; -- acknowledge data in transfer |
else |
ena_sck_next <= '0'; -- SCK disabled: tx empty, no data to send |
sck_ena_next <= '0'; -- SCK disabled: tx empty, no data to send |
wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages |
state_next <= state_reg - 1; -- update next state at each sck pulse |
end if; |
when 0 => |
di_req_next <= '1'; -- will request data if shifter empty |
ena_sck_next <= '0'; -- SCK disabled: tx empty, no data to send |
sck_ena_next <= '0'; -- SCK disabled: tx empty, no data to send |
if wren = '1' then -- load tx register if valid data present at di_i |
ena_ssel_next <= '1'; -- enable interface SSEL |
ssel_ena_next <= '1'; -- enable interface SSEL |
state_next <= N+1; -- start from idle: let one cycle for SSEL settling |
spi_mosi_o <= di_reg(N-1); -- special case: shift out first tx bit from the MSb (look ahead) |
sh_next <= di_reg; -- load bits from di_reg into shifter |
wren_ack_next <= '1'; -- acknowledge data in transfer |
wr_ack_next <= '1'; -- acknowledge data in transfer |
else |
ena_ssel_next <= '0'; -- deassert SSEL: interface is idle |
ssel_ena_next <= '0'; -- deassert SSEL: interface is idle |
wr_ack_next <= '0'; -- remove write acknowledge for all but the load stages |
state_next <= 0; -- when idle, keep this state |
end if; |
when others => |
541,30 → 562,25
-- OUTPUT LOGIC PROCESSES |
--============================================================================================= |
-- data output processes |
spi_ssel_o_proc: spi_ssel_o <= not ena_ssel_reg; -- drive active-low slave select line |
do_o_proc : do_o <= do_buffer_reg; -- do_o always available |
do_valid_o_proc: do_valid_o <= do_valid_o_reg; -- copy registered do_valid_o to output |
di_req_o_proc: di_req_o <= di_req_o_reg; -- copy registered di_req_o to output |
spi_ssel_o_proc: spi_ssel_o <= not ssel_ena_reg; -- active-low slave select line |
do_o_proc: do_o <= do_buffer_reg; -- parallel data out |
do_valid_o_proc: do_valid_o <= do_valid_o_reg; -- data out valid |
di_req_o_proc: di_req_o <= di_req_o_reg; -- input data request for next cycle |
wr_ack_o_proc: wr_ack_o <= wr_ack_reg; -- write acknowledge |
----------------------------------------------------------------------------------------------- |
-- SCK out logic: pipeline phase compensation for the SCK line |
----------------------------------------------------------------------------------------------- |
-- This is a MUX with an output register. The register gives us a pipeline delay for the SCK line, |
-- enabling higher SCK frequency. The MOSI and SCK phase are compensated by the pipeline delay. |
spi_sck_o_gen_proc : process (sclk_i, ena_sck_reg, spi_clk, spi_clk_reg) is |
-- This is a MUX with an output register. |
-- The register gives us a pipeline delay for the SCK line, pairing with the state machine moore |
-- output pipeline delay for the MOSI line, and thus enabling higher SCK frequency. |
spi_sck_o_gen_proc : process (sclk_i, sck_ena_reg, spi_clk, spi_clk_reg) is |
begin |
-- if sclk_i'event and sclk_i = '1' then |
-- if ena_sck_reg = '1' then |
-- spi_clk_reg <= spi_clk; -- copy the selected clock polarity |
-- else |
-- spi_clk_reg <= CPOL; -- when clock disabled, set to idle polarity |
-- end if; |
-- end if; |
if ena_sck_reg = '1' then |
if sclk_i'event and sclk_i = '1' then |
if sclk_i'event and sclk_i = '1' then |
if sck_ena_reg = '1' then |
spi_clk_reg <= spi_clk; -- copy the selected clock polarity |
else |
spi_clk_reg <= CPOL; -- when clock disabled, set to idle polarity |
end if; |
else |
spi_clk_reg <= CPOL; -- when clock disabled, set to idle polarity |
end if; |
spi_sck_o <= spi_clk_reg; -- connect register to output |
end process spi_sck_o_gen_proc; |
574,15 → 590,16
--============================================================================================= |
-- these signals are useful for verification, and can be deleted or commented-out after debug. |
do_transfer_proc: do_transfer_o <= do_transfer_reg; |
state_dbg_proc: state_dbg_o <= std_logic_vector(to_unsigned(state_reg, 6)); -- export internal state to debug |
state_dbg_proc: state_dbg_o <= std_logic_vector(to_unsigned(state_reg, 6)); |
rx_bit_reg_proc: rx_bit_reg_o <= rx_bit_reg; |
wren_o_proc: wren_o <= wren; |
wren_ack_o_proc: wren_ack_o <= wren_ack_reg; |
sh_reg_dbg_proc: sh_reg_dbg_o <= sh_reg; -- export sh_reg to debug |
sh_reg_dbg_proc: sh_reg_dbg_o <= sh_reg; |
core_clk_o_proc: core_clk_o <= core_clk; |
core_n_clk_o_proc: core_n_clk_o <= core_n_clk; |
core_ce_o_proc: core_ce_o <= core_ce; |
core_n_ce_o_proc: core_n_ce_o <= core_n_ce; |
sck_ena_o_proc: sck_ena_o <= sck_ena_reg; |
sck_ena_ce_o_proc: sck_ena_ce_o <= sck_ena_ce; |
|
end architecture RTL; |
|