| 1 |
2 |
M_artin |
|
| 2 |
|
|
--==========================================================================================================--
|
| 3 |
|
|
-- --
|
| 4 |
|
|
-- Copyright (C) 2011 by Martin Neumann martin@neumanns-mail.de --
|
| 5 |
|
|
-- --
|
| 6 |
|
|
-- This source file may be used and distributed without restriction provided that this copyright statement --
|
| 7 |
|
|
-- is not removed from the file and that any derivative work contains the original copyright notice and --
|
| 8 |
|
|
-- the associated disclaimer. --
|
| 9 |
|
|
-- --
|
| 10 |
7 |
M_artin |
-- This software is provided ''as is'' and without any expressed or implied warranties, including, but not --
|
| 11 |
|
|
-- limited to, the implied warranties of merchantability and fitness for a particular purpose. In no event --
|
| 12 |
2 |
M_artin |
-- shall the author or contributors be liable for any direct, indirect, incidental, special, exemplary, or --
|
| 13 |
|
|
-- consequential damages (including, but not limited to, procurement of substitute goods or services; loss --
|
| 14 |
|
|
-- of use, data, or profits; or business interruption) however caused and on any theory of liability, --
|
| 15 |
|
|
-- whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way --
|
| 16 |
|
|
-- out of the use of this software, even if advised of the possibility of such damage. --
|
| 17 |
|
|
-- --
|
| 18 |
|
|
--==========================================================================================================--
|
| 19 |
|
|
-- --
|
| 20 |
|
|
-- File name : USB_tb.vhd --
|
| 21 |
|
|
-- Author : Martin Neumann martin@neumanns-mail.de --
|
| 22 |
7 |
M_artin |
-- Description : USB test bench - an example how to use the usb_master files together an US application. --
|
| 23 |
2 |
M_artin |
-- --
|
| 24 |
|
|
--==========================================================================================================--
|
| 25 |
|
|
-- --
|
| 26 |
|
|
-- Change history --
|
| 27 |
|
|
-- --
|
| 28 |
|
|
-- Version / date Description --
|
| 29 |
|
|
-- --
|
| 30 |
|
|
-- 01 05 Mar 2011 MN Initial version --
|
| 31 |
7 |
M_artin |
-- 02 15 Apr 2013 MN Simplified --
|
| 32 |
2 |
M_artin |
-- --
|
| 33 |
|
|
-- End change history --
|
| 34 |
|
|
--==========================================================================================================--
|
| 35 |
|
|
|
| 36 |
|
|
LIBRARY work, IEEE;
|
| 37 |
|
|
USE IEEE.std_logic_1164.ALL;
|
| 38 |
|
|
USE work.usb_commands.ALL;
|
| 39 |
|
|
|
| 40 |
|
|
ENTITY usb_tb IS
|
| 41 |
|
|
END usb_tb;
|
| 42 |
|
|
|
| 43 |
|
|
ARCHITECTURE sim OF usb_tb IS
|
| 44 |
|
|
|
| 45 |
7 |
M_artin |
CONSTANT BUFSIZE_BITS : Integer := 8;
|
| 46 |
|
|
TYPE outp_mode IS(RECV, SEND);
|
| 47 |
|
|
SIGNAL clk_60mhz : STD_LOGIC;
|
| 48 |
|
|
SIGNAL fpga_ready : STD_LOGIC;
|
| 49 |
2 |
M_artin |
SIGNAL online : STD_LOGIC;
|
| 50 |
7 |
M_artin |
SIGNAL outp_cntl : outp_mode;
|
| 51 |
|
|
SIGNAL outp_reg : STD_LOGIC_VECTOR(7 DOWNTO 0);
|
| 52 |
2 |
M_artin |
SIGNAL rst_neg_ext : STD_LOGIC;
|
| 53 |
7 |
M_artin |
SIGNAL reset_sync : STD_LOGIC;
|
| 54 |
|
|
SIGNAL rxdat : STD_LOGIC_VECTOR(7 DOWNTO 0);
|
| 55 |
|
|
SIGNAL rxlen : STD_LOGIC_VECTOR(BUFSIZE_BITS-1 DOWNTO 0);
|
| 56 |
|
|
SIGNAL rxrdy : STD_LOGIC;
|
| 57 |
|
|
SIGNAL rxval : STD_LOGIC;
|
| 58 |
|
|
SIGNAL txcork : STD_LOGIC;
|
| 59 |
|
|
SIGNAL txdat : STD_LOGIC_VECTOR(7 DOWNTO 0);
|
| 60 |
|
|
SIGNAL txrdy : STD_LOGIC;
|
| 61 |
|
|
SIGNAL txroom : STD_LOGIC_VECTOR(BUFSIZE_BITS-1 DOWNTO 0);
|
| 62 |
|
|
SIGNAL txval : STD_LOGIC;
|
| 63 |
|
|
SIGNAL usb_dn : STD_LOGIC := 'L';
|
| 64 |
|
|
SIGNAL usb_dp : STD_LOGIC := 'Z'; -- allow forcing 'H', avoid 'X'
|
| 65 |
|
|
SIGNAL usb_rst : STD_LOGIC;
|
| 66 |
2 |
M_artin |
|
| 67 |
|
|
BEGIN
|
| 68 |
|
|
|
| 69 |
|
|
p_clk_60MHz : PROCESS
|
| 70 |
|
|
BEGIN
|
| 71 |
|
|
clk_60MHz <= '0';
|
| 72 |
|
|
WAIT FOR 2 ns;
|
| 73 |
|
|
While true loop
|
| 74 |
|
|
clk_60MHz <= '0';
|
| 75 |
|
|
WAIT FOR 8000 ps;
|
| 76 |
|
|
clk_60MHz <= '1';
|
| 77 |
7 |
M_artin |
WAIT FOR 8667 ps; -- 60 MHz
|
| 78 |
|
|
-- WAIT FOR 8393 ps; -- 61 MHz
|
| 79 |
2 |
M_artin |
-- WAIT FOR 8949 ps; -- 59 MHz
|
| 80 |
|
|
end loop;
|
| 81 |
|
|
END PROCESS;
|
| 82 |
|
|
|
| 83 |
|
|
usb_fs_master : ENTITY work.usb_fs_master
|
| 84 |
7 |
M_artin |
PORT MAP (
|
| 85 |
2 |
M_artin |
rst_neg_ext => rst_neg_ext,
|
| 86 |
|
|
usb_Dp => usb_dp,
|
| 87 |
7 |
M_artin |
usb_Dn => usb_dn
|
| 88 |
2 |
M_artin |
);
|
| 89 |
|
|
|
| 90 |
7 |
M_artin |
usb_dp <= 'L' WHEN reset_sync ='1' OR FPGA_ready ='0' ELSE 'H' after 10 ns;
|
| 91 |
|
|
usb_dn <= 'L';
|
| 92 |
2 |
M_artin |
|
| 93 |
7 |
M_artin |
usb_fs_slave_1 : ENTITY work.usb_fs_port
|
| 94 |
2 |
M_artin |
GENERIC MAP(
|
| 95 |
|
|
VENDORID => X"FB9A",
|
| 96 |
|
|
PRODUCTID => X"FB9A",
|
| 97 |
|
|
VERSIONBCD => X"0020",
|
| 98 |
|
|
SELFPOWERED => FALSE,
|
| 99 |
|
|
BUFSIZE_BITS => BUFSIZE_BITS)
|
| 100 |
|
|
PORT MAP(
|
| 101 |
|
|
clk => clk_60MHz, -- i
|
| 102 |
|
|
rst_neg_ext => rst_neg_ext, -- i
|
| 103 |
7 |
M_artin |
reset_syc => reset_sync, -- o positive active, streched to the next clock
|
| 104 |
2 |
M_artin |
d_pos => usb_dp, -- io Pos USB data line
|
| 105 |
|
|
d_neg => usb_dn, -- io Neg USB data line
|
| 106 |
7 |
M_artin |
d_oe => OPEN,
|
| 107 |
2 |
M_artin |
USB_rst => USB_rst, -- o USB reset detected (SE0 > 2.5 us)
|
| 108 |
|
|
online => online, -- o High when the device is in Config state.
|
| 109 |
|
|
RXval => RXval, -- o High if a received byte available on RXDAT.
|
| 110 |
|
|
RXdat => RXdat, -- o Received data byte, valid if RXVAL is high.
|
| 111 |
|
|
RXrdy => RXrdy, -- i High if application is ready to receive.
|
| 112 |
|
|
RXlen => RXlen, -- o No of bytes available in receive buffer.
|
| 113 |
|
|
TXval => TXval, -- i High if the application has data to send.
|
| 114 |
|
|
TXdat => TXdat, -- i Data byte to send, must be valid if TXVAL is high.
|
| 115 |
|
|
TXrdy => TXrdy, -- o High if the entity is ready to accept the next byte.
|
| 116 |
|
|
TXroom => TXroom, -- o No of free bytes in transmit buffer.
|
| 117 |
|
|
TXcork => TXcork, -- i Temp. suppress transmissions at the outgoing endpoint.
|
| 118 |
|
|
FPGA_ready => FPGA_ready -- o Connect FPGA_ready to the pullup resistor logic
|
| 119 |
|
|
);
|
| 120 |
|
|
|
| 121 |
7 |
M_artin |
TXcork <= '0'; -- Don't hold TX transmission
|
| 122 |
|
|
TXdat <= outp_reg;
|
| 123 |
|
|
|
| 124 |
|
|
simple_application : process (clk_60MHz, reset_sync)
|
| 125 |
|
|
-- returns received bytes with twisted high - and low order nibbles --
|
| 126 |
|
|
begin
|
| 127 |
|
|
if reset_sync ='1' then
|
| 128 |
|
|
outp_cntl <= RECV;
|
| 129 |
|
|
outp_reg <= (OTHERS => '0');
|
| 130 |
|
|
TXval <= '0';
|
| 131 |
|
|
RXrdy <= '0';
|
| 132 |
|
|
elsif rising_edge(clk_60MHz) then
|
| 133 |
|
|
if outp_cntl = RECV then
|
| 134 |
|
|
TXval <= '0';
|
| 135 |
|
|
if RXval = '1' then
|
| 136 |
|
|
RXrdy <= '0';
|
| 137 |
|
|
outp_reg <= RXdat(3 DOWNTO 0) & RXdat(7 DOWNTO 4);
|
| 138 |
|
|
outp_cntl <= SEND;
|
| 139 |
|
|
else
|
| 140 |
|
|
-- RXrdy <= online;
|
| 141 |
|
|
RXrdy <= '1';
|
| 142 |
|
|
outp_cntl <= RECV;
|
| 143 |
|
|
end if;
|
| 144 |
|
|
else -- outp_cntl = SEND
|
| 145 |
|
|
if TXrdy = '1' then
|
| 146 |
|
|
TXval <= '1';
|
| 147 |
|
|
RXrdy <= '1';
|
| 148 |
|
|
outp_cntl <= RECV;
|
| 149 |
|
|
else
|
| 150 |
|
|
TXval <= '0';
|
| 151 |
|
|
RXrdy <= '0';
|
| 152 |
|
|
outp_cntl <= SEND;
|
| 153 |
|
|
end if;
|
| 154 |
|
|
end if;
|
| 155 |
|
|
end if;
|
| 156 |
|
|
end process;
|
| 157 |
|
|
|
| 158 |
2 |
M_artin |
END sim;
|
| 159 |
|
|
|