Advertisement
Guest User

VHDL axis_i2s2

a guest
Apr 10th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 8.15 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date: 03/30/2019 11:21:45 PM
  6. -- Design Name:
  7. -- Module Name: i2s_axis - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool Versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20.  
  21.  
  22. library IEEE;
  23. use IEEE.STD_LOGIC_1164.ALL;
  24. use ieee.std_logic_unsigned.all;
  25.  
  26. -- Uncomment the following library declaration if instantiating
  27. -- any Xilinx leaf cells in this code.
  28. --library UNISIM;
  29. --use UNISIM.VComponents.all;
  30.  
  31. entity i2s_axis is
  32.     Port ( sys_clock : in STD_LOGIC;
  33.            axis_reset_n : in STD_LOGIC;
  34.            tx_axis_s_data : in STD_LOGIC_VECTOR (31 downto 0);
  35.            tx_axis_s_valid : in STD_LOGIC;
  36.            tx_axis_s_ready : out STD_LOGIC;
  37.            tx_axis_s_last : in STD_LOGIC;
  38.            rx_axis_m_data : out STD_LOGIC_VECTOR (31 downto 0);
  39.            rx_axis_m_valid : out STD_LOGIC;
  40.            rx_axis_m_ready : in STD_LOGIC;
  41.            rx_axis_m_last : out STD_LOGIC;
  42.            tx_mclk : out STD_LOGIC;
  43.            tx_lrck : out STD_LOGIC;
  44.            tx_sclk : out STD_LOGIC;
  45.            tx_sdout : out STD_LOGIC;
  46.            rx_mclk : out STD_LOGIC;
  47.            rx_lrck : out STD_LOGIC;
  48.            rx_sclk : out STD_LOGIC;
  49.            rx_sdin : in STD_LOGIC);
  50. end i2s_axis;
  51.  
  52. architecture Behavioral of i2s_axis is
  53.  
  54.     --Output signals
  55.     signal tx_axis_s_ready_sig  : std_logic;
  56.     signal rx_axis_m_valid_sig  : std_logic;
  57.     signal rx_axis_m_last_sig   : std_logic;
  58.  
  59.  
  60.     signal lrck             : std_logic;
  61.     signal mclk             : std_logic;
  62.     signal sclk             : std_logic;
  63.     signal count            : std_logic_vector(8 downto 0) := (others => '0');
  64.     signal axis_clk         : std_logic;
  65.     signal tx_data_l        : std_logic_vector(31 downto 0) := (others => '0');
  66.     signal tx_data_r        : std_logic_vector(31 downto 0) := (others => '0');
  67.     signal tx_data_l_shift  : std_logic_vector(23 downto 0) := (others => '0');
  68.     signal tx_data_r_shift  : std_logic_vector(23 downto 0) := (others => '0');
  69.     signal din_sync_shift   : std_logic_vector(2 downto 0) := "000";
  70.     signal din_sync         : std_logic;
  71.     signal rx_data_l        : std_logic_vector(31 downto 0) := (others => '0');
  72.     signal rx_data_r        : std_logic_vector(31 downto 0) := (others => '0');
  73.     signal rx_data_l_shift  : std_logic_vector(23 downto 0) := (others => '0');
  74.     signal rx_data_r_shift  : std_logic_vector(23 downto 0) := (others => '0');
  75. begin
  76.  
  77.     axis_clk <= sys_clock;
  78.    
  79.     lrck <= count(8);
  80.     sclk <= count(2);
  81.     mclk <= axis_clk;
  82.     tx_lrck <= lrck;
  83.     tx_sclk <= sclk;
  84.     tx_mclk <= mclk;
  85.     rx_lrck <= lrck;
  86.     rx_sclk <= sclk;
  87.     rx_mclk <= mclk;
  88.    
  89.        
  90.     --output signals
  91.     tx_axis_s_ready <= tx_axis_s_ready_sig;
  92.     rx_axis_m_valid <= rx_axis_m_valid_sig;
  93.     rx_axis_m_last <= rx_axis_m_last_sig;
  94.  
  95.     process(axis_clk)
  96.     begin
  97.         if rising_edge(axis_clk) then
  98.             count <= count + '1';
  99.         end if;
  100.     end process;
  101.  
  102.     process(axis_clk)
  103.     begin
  104.         if( rising_edge(axis_clk)) then
  105.             if( axis_reset_n = '0') then
  106.                 tx_axis_s_ready_sig <= '0';
  107.             elsif (tx_axis_s_ready_sig = '1') and (tx_axis_s_valid = '1') and (tx_axis_s_last = '1') then --end of packet, cannot accept data until current one has been transmitted
  108.                 tx_axis_s_ready_sig <= '0';
  109.             elsif (count = "000000000") then  -- beginning of I2S frame, in order to avoid tearing, cannot accept data until frame complete
  110.                 tx_axis_s_ready_sig <= '0';
  111.             elsif (count = "111000111") then -- end of I2S frame, can accept data
  112.                 tx_axis_s_ready_sig <= '1';      
  113.             end if;
  114.         end if;
  115.     end process;
  116.  
  117.     process(axis_clk)    
  118.     begin
  119.         if( rising_edge(axis_clk)) then
  120.             if (axis_reset_n = '0') then
  121.                 tx_data_r <= (others => '0');
  122.                 tx_data_l <= (others => '0');
  123.             elsif (tx_axis_s_valid = '1' and tx_axis_s_ready_sig = '1') then
  124.                 if (tx_axis_s_last = '1') then
  125.                     tx_data_r <= tx_axis_s_data;
  126.                 else
  127.                     tx_data_l <= tx_axis_s_data;
  128.                 end if;
  129.             end if;
  130.         end if;
  131.     end process;
  132.  
  133.     --/* I2S TRANSMIT SHIFT REGISTERS */
  134.     process(axis_clk)
  135.     begin
  136.         if (rising_edge(axis_clk)) then
  137.             if (count = "000000110") then
  138.                 tx_data_l_shift <= tx_data_l(23 downto 0);
  139.                 tx_data_r_shift <= tx_data_r(23 downto 0);
  140.             elsif (count(2 downto 0) = "110") and (count(7 downto 3) >= "00001") and (count(7 downto 3) <= "11000") then
  141.                 if(count(8) = '1') then
  142.                     tx_data_r_shift <= tx_data_r_shift(22 downto 0) & '0';
  143.                 else
  144.                     tx_data_l_shift <= tx_data_l_shift(22 downto 0) & '0';
  145.                 end if;
  146.             end if;
  147.         end if;
  148.     end process;
  149.        
  150.     process(count,tx_data_r_shift,tx_data_l_shift)
  151.     begin
  152.         if (count (7 downto 3) <= "11000") and (count(7 downto 3) >= "00001") then
  153.             if(count(8) = '1') then
  154.                 tx_sdout <= tx_data_r_shift(23);
  155.             else
  156.                 tx_sdout <= tx_data_l_shift(23);
  157.             end if;
  158.         else
  159.             tx_sdout <= '0';
  160.         end if;
  161.     end process;
  162.    
  163.     --/* SYNCHRONIZE DATA IN TO AXIS CLOCK DOMAIN */
  164.     din_sync <= din_sync_shift(2);
  165.     process(axis_clk)
  166.     begin
  167.         if (rising_edge(axis_clk)) then
  168.             din_sync_shift <= (din_sync_shift(1 downto 0) & rx_sdin);
  169.         end if;
  170.     end process;    
  171.    
  172.     --/* I2S RECEIVE SHIFT REGISTERS */
  173.     process(axis_clk)
  174.     begin
  175.         if (rising_edge(axis_clk)) then
  176.             if(count (2 downto 0) = "011") and (count(7 downto 3) <= "11000") and (count(7 downto 3) >= "00001") then
  177.                 if (lrck = '1') then
  178.                     rx_data_r_shift <= rx_data_r_shift(22 downto 0) & din_sync;
  179.                 else
  180.                     rx_data_l_shift <= rx_data_l_shift(22 downto 0) & din_sync;
  181.                 end if;
  182.             end if;
  183.         end if;
  184.     end process;
  185.    
  186.     --/* AXIS MASTER CONTROLLER */
  187.     process(axis_clk)
  188.     begin
  189.         if (rising_edge(axis_clk)) then
  190.             if(axis_reset_n = '0') then
  191.                 rx_data_l <= (others => '0');
  192.                 rx_data_r <= (others => '0');
  193.             elsif (count = "111000111") and (rx_axis_m_valid_sig = '0') then
  194.                 rx_data_l <= ("00000000" & rx_data_l_shift);
  195.                 rx_data_r <= ("00000000" & rx_data_r_shift);
  196.             end if;
  197.         end if;
  198.     end process;
  199.    
  200.     mux : process(rx_axis_m_last_sig, rx_data_r, rx_data_l)
  201.     begin
  202.         case rx_axis_m_last_sig is
  203.             when '0' => rx_axis_m_data <= rx_data_l;
  204.             when others => rx_axis_m_data <= rx_data_r;
  205.         end case;
  206.     end process;
  207.    
  208.     process(axis_clk)
  209.     begin
  210.         if (rising_edge(axis_clk)) then
  211.             if(axis_reset_n = '0') then
  212.                 rx_axis_m_valid_sig <= '0';
  213.             elsif (count = "111000111") and (rx_axis_m_valid_sig = '0') then
  214.                 rx_axis_m_valid_sig <= '1';
  215.             elsif (rx_axis_m_valid_sig = '1') and (rx_axis_m_ready = '1') and (rx_axis_m_last_sig = '1') then
  216.                 rx_axis_m_valid_sig <= '0';
  217.             end if;
  218.         end if;
  219.     end process;
  220.    
  221.     process(axis_clk)
  222.     begin
  223.         if (rising_edge(axis_clk)) then
  224.             if(axis_reset_n = '0') then
  225.                 rx_axis_m_last_sig <= '0';
  226.             elsif (count = "111000111") and (rx_axis_m_valid_sig = '0') then
  227.                 rx_axis_m_last_sig <= '0';
  228.             elsif (rx_axis_m_valid_sig = '1') and (rx_axis_m_ready = '1') then
  229.                 rx_axis_m_last_sig <= not(rx_axis_m_last_sig);
  230.             end if;
  231.         end if;
  232.     end process;
  233.  
  234.  
  235. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement