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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_spi.vhd] - Diff between revs 50 and 65

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 50 Rev 65
Line 70... Line 70...
 
 
  -- IO space: module base address --
  -- IO space: module base address --
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
  constant lo_abb_c : natural := index_size_f(spi_size_c); -- low address boundary bit
  constant lo_abb_c : natural := index_size_f(spi_size_c); -- low address boundary bit
 
 
  -- control reg bits --
  -- control register --
  constant ctrl_spi_cs0_c    : natural :=  0; -- r/w: spi CS 0
  constant ctrl_spi_cs0_c    : natural :=  0; -- r/w: spi CS 0
  constant ctrl_spi_cs1_c    : natural :=  1; -- r/w: spi CS 1
  constant ctrl_spi_cs1_c    : natural :=  1; -- r/w: spi CS 1
  constant ctrl_spi_cs2_c    : natural :=  2; -- r/w: spi CS 2
  constant ctrl_spi_cs2_c    : natural :=  2; -- r/w: spi CS 2
  constant ctrl_spi_cs3_c    : natural :=  3; -- r/w: spi CS 3
  constant ctrl_spi_cs3_c    : natural :=  3; -- r/w: spi CS 3
  constant ctrl_spi_cs4_c    : natural :=  4; -- r/w: spi CS 4
  constant ctrl_spi_cs4_c    : natural :=  4; -- r/w: spi CS 4
Line 87... Line 87...
  constant ctrl_spi_prsc0_c  : natural := 10; -- r/w: spi prescaler select bit 0
  constant ctrl_spi_prsc0_c  : natural := 10; -- r/w: spi prescaler select bit 0
  constant ctrl_spi_prsc1_c  : natural := 11; -- r/w: spi prescaler select bit 1
  constant ctrl_spi_prsc1_c  : natural := 11; -- r/w: spi prescaler select bit 1
  constant ctrl_spi_prsc2_c  : natural := 12; -- r/w: spi prescaler select bit 2
  constant ctrl_spi_prsc2_c  : natural := 12; -- r/w: spi prescaler select bit 2
  constant ctrl_spi_size0_c  : natural := 13; -- r/w: data size (00:  8-bit, 01: 16-bit)
  constant ctrl_spi_size0_c  : natural := 13; -- r/w: data size (00:  8-bit, 01: 16-bit)
  constant ctrl_spi_size1_c  : natural := 14; -- r/w: data size (10: 24-bit, 11: 32-bit)
  constant ctrl_spi_size1_c  : natural := 14; -- r/w: data size (10: 24-bit, 11: 32-bit)
 
  constant ctrl_spi_cpol_c  : natural := 15; -- r/w: spi clock polarity
  --
  --
  constant ctrl_spi_busy_c   : natural := 31; -- r/-: spi transceiver is busy
  constant ctrl_spi_busy_c   : natural := 31; -- r/-: spi transceiver is busy
 
  --
 
  signal ctrl : std_ulogic_vector(15 downto 0);
 
 
  -- access control --
  -- access control --
  signal acc_en : std_ulogic; -- module access enable
  signal acc_en : std_ulogic; -- module access enable
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
  signal wren   : std_ulogic; -- word write enable
  signal wren   : std_ulogic; -- word write enable
  signal rden   : std_ulogic; -- read enable
  signal rden   : std_ulogic; -- read enable
 
 
  -- accessible regs --
 
  signal ctrl        : std_ulogic_vector(14 downto 0);
 
  signal tx_data_reg : std_ulogic_vector(31 downto 0);
 
  signal rx_data     : std_ulogic_vector(31 downto 0);
 
 
 
  -- clock generator --
  -- clock generator --
  signal spi_clk : std_ulogic;
  signal spi_clk_en : std_ulogic;
 
 
  -- spi transceiver --
  -- spi transceiver --
  signal spi_start      : std_ulogic;
  type rtx_engine_t is record
  signal spi_busy       : std_ulogic;
    busy     : std_ulogic;
  signal spi_state0     : std_ulogic;
    state0   : std_ulogic;
  signal spi_state1     : std_ulogic;
    state1   : std_ulogic;
  signal spi_rtx_sreg   : std_ulogic_vector(31 downto 0);
    rtx_sreg : std_ulogic_vector(31 downto 0);
  signal spi_rx_data    : std_ulogic_vector(31 downto 0);
    bitcnt   : std_ulogic_vector(05 downto 0);
  signal spi_bitcnt     : std_ulogic_vector(05 downto 0);
    bytecnt  : std_ulogic_vector(02 downto 0);
  signal spi_bitcnt_max : std_ulogic_vector(05 downto 0);
    sdi_ff0  : std_ulogic;
  signal spi_sdi_ff0    : std_ulogic;
    sdi_ff1  : std_ulogic;
  signal spi_sdi_ff1    : std_ulogic;
  end record;
 
  signal rtx_engine : rtx_engine_t;
 
 
begin
begin
 
 
  -- Access Control -------------------------------------------------------------------------
  -- Access Control -------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
Line 131... Line 130...
  -- Read/Write Access ----------------------------------------------------------------------
  -- Read/Write Access ----------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  rw_access: process(clk_i)
  rw_access: process(clk_i)
  begin
  begin
    if rising_edge(clk_i) then
    if rising_edge(clk_i) then
      ack_o <= acc_en and (rden_i or wren_i);
      -- bus access acknowledge --
 
      ack_o <= rden or wren;
 
 
      -- write access --
      -- write access --
      spi_start <= '0';
 
      if (wren = '1') then
      if (wren = '1') then
        if (addr = spi_ctrl_addr_c) then -- control
        if (addr = spi_ctrl_addr_c) then -- control register
          ctrl <= data_i(ctrl'left downto 0);
          ctrl(ctrl_spi_cs0_c)   <= data_i(ctrl_spi_cs0_c);
        end if;
          ctrl(ctrl_spi_cs1_c)   <= data_i(ctrl_spi_cs1_c);
        if (addr = spi_rtx_addr_c) then -- tx data
          ctrl(ctrl_spi_cs2_c)   <= data_i(ctrl_spi_cs2_c);
          tx_data_reg <= data_i;
          ctrl(ctrl_spi_cs3_c)   <= data_i(ctrl_spi_cs3_c);
          spi_start   <= '1';
          ctrl(ctrl_spi_cs4_c)   <= data_i(ctrl_spi_cs4_c);
 
          ctrl(ctrl_spi_cs5_c)   <= data_i(ctrl_spi_cs5_c);
 
          ctrl(ctrl_spi_cs6_c)   <= data_i(ctrl_spi_cs6_c);
 
          ctrl(ctrl_spi_cs7_c)   <= data_i(ctrl_spi_cs7_c);
 
          --
 
          ctrl(ctrl_spi_en_c)    <= data_i(ctrl_spi_en_c);
 
          ctrl(ctrl_spi_cpha_c)  <= data_i(ctrl_spi_cpha_c);
 
          ctrl(ctrl_spi_prsc0_c) <= data_i(ctrl_spi_prsc0_c);
 
          ctrl(ctrl_spi_prsc1_c) <= data_i(ctrl_spi_prsc1_c);
 
          ctrl(ctrl_spi_prsc2_c) <= data_i(ctrl_spi_prsc2_c);
 
          ctrl(ctrl_spi_size0_c) <= data_i(ctrl_spi_size0_c);
 
          ctrl(ctrl_spi_size1_c) <= data_i(ctrl_spi_size1_c);
 
          ctrl(ctrl_spi_cpol_c)  <= data_i(ctrl_spi_cpol_c);
        end if;
        end if;
      end if;
      end if;
 
 
      -- read access --
      -- read access --
      data_o <= (others => '0');
      data_o <= (others => '0');
      if (rden = '1') then
      if (rden = '1') then
        if (addr = spi_ctrl_addr_c) then
        if (addr = spi_ctrl_addr_c) then -- control register
          data_o(ctrl_spi_cs0_c)    <= ctrl(ctrl_spi_cs0_c);
          data_o(ctrl_spi_cs0_c)    <= ctrl(ctrl_spi_cs0_c);
          data_o(ctrl_spi_cs1_c)    <= ctrl(ctrl_spi_cs1_c);
          data_o(ctrl_spi_cs1_c)    <= ctrl(ctrl_spi_cs1_c);
          data_o(ctrl_spi_cs2_c)    <= ctrl(ctrl_spi_cs2_c);
          data_o(ctrl_spi_cs2_c)    <= ctrl(ctrl_spi_cs2_c);
          data_o(ctrl_spi_cs3_c)    <= ctrl(ctrl_spi_cs3_c);
          data_o(ctrl_spi_cs3_c)    <= ctrl(ctrl_spi_cs3_c);
          data_o(ctrl_spi_cs4_c)    <= ctrl(ctrl_spi_cs4_c);
          data_o(ctrl_spi_cs4_c)    <= ctrl(ctrl_spi_cs4_c);
Line 163... Line 176...
          data_o(ctrl_spi_prsc0_c)  <= ctrl(ctrl_spi_prsc0_c);
          data_o(ctrl_spi_prsc0_c)  <= ctrl(ctrl_spi_prsc0_c);
          data_o(ctrl_spi_prsc1_c)  <= ctrl(ctrl_spi_prsc1_c);
          data_o(ctrl_spi_prsc1_c)  <= ctrl(ctrl_spi_prsc1_c);
          data_o(ctrl_spi_prsc2_c)  <= ctrl(ctrl_spi_prsc2_c);
          data_o(ctrl_spi_prsc2_c)  <= ctrl(ctrl_spi_prsc2_c);
          data_o(ctrl_spi_size0_c)  <= ctrl(ctrl_spi_size0_c);
          data_o(ctrl_spi_size0_c)  <= ctrl(ctrl_spi_size0_c);
          data_o(ctrl_spi_size1_c)  <= ctrl(ctrl_spi_size1_c);
          data_o(ctrl_spi_size1_c)  <= ctrl(ctrl_spi_size1_c);
 
          data_o(ctrl_spi_cpol_c)  <= ctrl(ctrl_spi_cpol_c);
          --
          --
          data_o(ctrl_spi_busy_c)   <= spi_busy;
          data_o(ctrl_spi_busy_c)  <= rtx_engine.busy;
        else -- spi_rtx_addr_c
        else -- data register (spi_rtx_addr_c)
          data_o <= rx_data;
          data_o <= rtx_engine.rtx_sreg;
        end if;
        end if;
      end if;
      end if;
    end if;
    end if;
  end process rw_access;
  end process rw_access;
 
 
  -- direct chip-select (CS) (output is low-active) --  
  -- direct chip-select (CS), output is low-active --  
  spi_csn_o(7 downto 0) <= not ctrl(ctrl_spi_cs7_c downto ctrl_spi_cs0_c);
  spi_csn_o(7 downto 0) <= not ctrl(ctrl_spi_cs7_c downto ctrl_spi_cs0_c);
 
 
 
 
  -- Clock Selection ------------------------------------------------------------------------
  -- Transmission Data Size -----------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- clock generator enable --
  data_size: process(ctrl)
  clkgen_en_o <= ctrl(ctrl_spi_en_c);
  begin
 
    case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
 
      when "00"   => rtx_engine.bytecnt <= "001"; -- 1-byte mode
 
      when "01"   => rtx_engine.bytecnt <= "010"; -- 2-byte mode
 
      when "10"   => rtx_engine.bytecnt <= "011"; -- 3-byte mode
 
      when others => rtx_engine.bytecnt <= "100"; -- 4-byte mode
 
    end case;
 
  end process data_size;
 
 
 
 
  -- spi clock select --
  -- Clock Selection ------------------------------------------------------------------------
  spi_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_spi_prsc2_c downto ctrl_spi_prsc0_c))));
  -- -------------------------------------------------------------------------------------------
 
  clkgen_en_o <= ctrl(ctrl_spi_en_c); -- clock generator enable
 
  spi_clk_en  <= clkgen_i(to_integer(unsigned(ctrl(ctrl_spi_prsc2_c downto ctrl_spi_prsc0_c)))); -- clock select
 
 
 
 
  -- SPI Transceiver ------------------------------------------------------------------------
  -- SPI Transceiver ------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  spi_rtx_unit: process(clk_i)
  spi_rtx_unit: process(clk_i)
  begin
  begin
    if rising_edge(clk_i) then
    if rising_edge(clk_i) then
      -- input (sdi) synchronizer --
      -- input (sdi) synchronizer --
      spi_sdi_ff0 <= spi_sdi_i;
      rtx_engine.sdi_ff0 <= spi_sdi_i;
      spi_sdi_ff1 <= spi_sdi_ff0;
      rtx_engine.sdi_ff1 <= rtx_engine.sdi_ff0;
 
 
      -- serial engine --
      -- serial engine --
      irq_o <= '0';
      if (rtx_engine.state0 = '0') or (ctrl(ctrl_spi_en_c) = '0') then -- idle or disabled
      if (spi_state0 = '0') or (ctrl(ctrl_spi_en_c) = '0') then -- idle or disabled
 
      -- --------------------------------------------------------------
      -- --------------------------------------------------------------
        spi_bitcnt <= (others => '0');
        spi_sck_o         <= ctrl(ctrl_spi_cpol_c);
        spi_state1 <= '0';
        rtx_engine.bitcnt <= (others => '0');
        spi_sdo_o  <= '0';
        rtx_engine.state1 <= '0';
        spi_sck_o  <= '0';
 
        if (ctrl(ctrl_spi_en_c) = '0') then -- disabled
        if (ctrl(ctrl_spi_en_c) = '0') then -- disabled
          spi_busy <= '0';
          rtx_engine.busy <= '0';
        elsif (spi_start = '1') then -- start new transmission
        elsif (wren = '1') and (addr = spi_rtx_addr_c) then -- start new transmission
          spi_rtx_sreg <= tx_data_reg;
          rtx_engine.rtx_sreg <= data_i;
          spi_busy     <= '1';
          rtx_engine.busy     <= '1';
        end if;
        end if;
        spi_state0 <= spi_busy and spi_clk; -- start with next new clock pulse
        rtx_engine.state0 <= rtx_engine.busy and spi_clk_en; -- start with next new clock pulse
 
 
      else -- transmission in progress
      else -- transmission in progress
      -- --------------------------------------------------------------
      -- --------------------------------------------------------------
        if (spi_state1 = '0') then -- first half of transmission
 
        -- --------------------------------------------------------------
 
          spi_sck_o <= ctrl(ctrl_spi_cpha_c);
 
 
 
 
        if (rtx_engine.state1 = '0') then -- first half of bit transmission
 
        -- --------------------------------------------------------------
 
          spi_sck_o <= ctrl(ctrl_spi_cpha_c) xor ctrl(ctrl_spi_cpol_c);
 
          --
          case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
          case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
            when "00"   => spi_sdo_o <= spi_rtx_sreg(07); -- 8-bit mode
            when "00"   => spi_sdo_o <= rtx_engine.rtx_sreg(07); -- 8-bit mode
            when "01"   => spi_sdo_o <= spi_rtx_sreg(15); -- 16-bit mode
            when "01"   => spi_sdo_o <= rtx_engine.rtx_sreg(15); -- 16-bit mode
            when "10"   => spi_sdo_o <= spi_rtx_sreg(23); -- 24-bit mode
            when "10"   => spi_sdo_o <= rtx_engine.rtx_sreg(23); -- 24-bit mode
            when others => spi_sdo_o <= spi_rtx_sreg(31); -- 32-bit mode
            when others => spi_sdo_o <= rtx_engine.rtx_sreg(31); -- 32-bit mode
          end case;
          end case;
 
          --
          if (spi_clk = '1') then
          if (spi_clk_en = '1') then
            spi_state1 <= '1';
 
            if (ctrl(ctrl_spi_cpha_c) = '0') then
            if (ctrl(ctrl_spi_cpha_c) = '0') then
              spi_rtx_sreg <= spi_rtx_sreg(30 downto 0) & spi_sdi_ff1;
              rtx_engine.rtx_sreg <= rtx_engine.rtx_sreg(30 downto 0) & rtx_engine.sdi_ff1;
            end if;
            end if;
            spi_bitcnt <= std_ulogic_vector(unsigned(spi_bitcnt) + 1);
            rtx_engine.bitcnt <= std_ulogic_vector(unsigned(rtx_engine.bitcnt) + 1);
 
            rtx_engine.state1 <= '1';
          end if;
          end if;
 
 
        else -- second half of transmission
        else -- second half of bit transmission
        -- --------------------------------------------------------------
        -- --------------------------------------------------------------
          spi_sck_o <= not ctrl(ctrl_spi_cpha_c);
          spi_sck_o <= ctrl(ctrl_spi_cpha_c) xnor ctrl(ctrl_spi_cpol_c);
 
          --
          if (spi_clk = '1') then
          if (spi_clk_en = '1') then
            spi_state1 <= '0';
 
            if (ctrl(ctrl_spi_cpha_c) = '1') then
            if (ctrl(ctrl_spi_cpha_c) = '1') then
              spi_rtx_sreg <= spi_rtx_sreg(30 downto 0) & spi_sdi_ff1;
              rtx_engine.rtx_sreg <= rtx_engine.rtx_sreg(30 downto 0) & rtx_engine.sdi_ff1;
            end if;
            end if;
            if (spi_bitcnt = spi_bitcnt_max) then
            if (rtx_engine.bitcnt(5 downto 3) = rtx_engine.bytecnt) then
              spi_state0 <= '0';
              rtx_engine.state0 <= '0';
              spi_busy   <= '0';
              rtx_engine.busy   <= '0';
              irq_o      <= '1';
 
            end if;
            end if;
 
            rtx_engine.state1 <= '0';
          end if;
          end if;
 
 
        end if;
        end if;
      end if;
      end if;
    end if;
    end if;
  end process spi_rtx_unit;
  end process spi_rtx_unit;
 
 
 
 
  -- RTX Data size ------------------------------------------------------------------------
  -- Interrupt ------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  data_size: process(ctrl)
  irq_o <= ctrl(ctrl_spi_en_c) and (not rtx_engine.busy); -- fire IRQ if transceiver idle
  begin
 
    case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
 
      when "00"   => spi_bitcnt_max <= "001000"; -- 8-bit mode
 
      when "01"   => spi_bitcnt_max <= "010000"; -- 16-bit mode
 
      when "10"   => spi_bitcnt_max <= "011000"; -- 24-bit mode
 
      when others => spi_bitcnt_max <= "100000"; -- 32-bit mode
 
    end case;
 
  end process data_size;
 
 
 
 
 
  -- RX-Data Masking ------------------------------------------------------------------------
 
  -- -------------------------------------------------------------------------------------------
 
  rx_mapping: process(ctrl, spi_rtx_sreg)
 
  begin
 
    case ctrl(ctrl_spi_size1_c downto ctrl_spi_size0_c) is
 
      when "00"   => rx_data <= x"000000" & spi_rtx_sreg(07 downto 0); -- 8-bit mode
 
      when "01"   => rx_data <= x"0000"   & spi_rtx_sreg(15 downto 0); -- 16-bit mode
 
      when "10"   => rx_data <= x"00"     & spi_rtx_sreg(23 downto 0); -- 24-bit mode
 
      when others => rx_data <=             spi_rtx_sreg(31 downto 0); -- 32-bit mode
 
    end case;
 
  end process rx_mapping;
 
 
 
 
 
end neorv32_spi_rtl;
end neorv32_spi_rtl;
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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