1 |
2 |
M_artin |
--==========================================================================================================--
|
2 |
|
|
-- --
|
3 |
|
|
-- Copyright (C) 2011 by Martin Neumann martin@neumanns-mail.de --
|
4 |
|
|
-- --
|
5 |
|
|
-- This source file may be used and distributed without restriction provided that this copyright statement --
|
6 |
|
|
-- is not removed from the file and that any derivative work contains the original copyright notice and --
|
7 |
|
|
-- the associated disclaimer. --
|
8 |
|
|
-- --
|
9 |
|
|
-- This software is provided ''as is'' and without any express or implied warranties, including, but not --
|
10 |
|
|
-- limited to, the implied warranties of merchantability and fitness for a particular purpose. in no event --
|
11 |
|
|
-- shall the author or contributors be liable for any direct, indirect, incidental, special, exemplary, or --
|
12 |
|
|
-- consequential damages (including, but not limited to, procurement of substitute goods or services; loss --
|
13 |
|
|
-- of use, data, or profits; or business interruption) however caused and on any theory of liability, --
|
14 |
|
|
-- whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way --
|
15 |
|
|
-- out of the use of this software, even if advised of the possibility of such damage. --
|
16 |
|
|
-- --
|
17 |
|
|
--==========================================================================================================--
|
18 |
|
|
-- --
|
19 |
|
|
-- File name : usb_fs_master.vhd --
|
20 |
|
|
-- Author : Martin Neumann martin@neumanns-mail.de --
|
21 |
|
|
-- Description : USB FS Master, used with usb_Stimuli.vhd data source and usb_fs_monitor.vhd. --
|
22 |
|
|
-- --
|
23 |
|
|
--==========================================================================================================--
|
24 |
|
|
-- --
|
25 |
|
|
-- Change history --
|
26 |
|
|
-- --
|
27 |
|
|
-- Version / date Description --
|
28 |
|
|
-- --
|
29 |
|
|
-- 01 05 Mar 2011 MN Initial version --
|
30 |
|
|
-- --
|
31 |
|
|
-- End change history --
|
32 |
|
|
--==========================================================================================================--
|
33 |
|
|
-- --
|
34 |
|
|
-- http://en.wikipedia.org/wiki/Universal_Serial_Bus --
|
35 |
|
|
-- USB data is transmitted by toggling the data lines between the J state and the opposite K state. USB --
|
36 |
|
|
-- encodes data using the NRZI convention; a 0 bit is transmitted by toggling the data lines from J to K --
|
37 |
|
|
-- or vice-versa, while a 1 bit is transmitted by leaving the data lines as-is. --
|
38 |
|
|
-- To ensure a minimum density of signal transitions, USB uses bit stuffing - an extra 0 bit is inserted --
|
39 |
|
|
-- into the data stream after any appearance of six consecutive 1 bits. Seven consecutive '1's are always --
|
40 |
|
|
-- an error. --
|
41 |
|
|
-- A USB packet begins with an 8-bit synchronization sequence '00000001'. That is, after the initial idle --
|
42 |
|
|
-- state J, the data lines toggle KJKJKJKK. The final 1 bit (repeated K state) marks the end of the sync --
|
43 |
|
|
-- pattern and the beginning of the USB frame. For high bandwidth USB, the packet begins with a 32-bit --
|
44 |
|
|
-- synchronization sequence. --
|
45 |
|
|
-- A USB packet's end, called EOP (end-of-packet), is indicated by the transmitter driving 2 bit times of --
|
46 |
|
|
-- SE0 (D+ and D- both below max) and 1 bit time of J state. After this, the transmitter ceases to drive --
|
47 |
|
|
-- the D+/D- lines and the aforementioned pull up resistors hold it in the J (idle) state. Sometimes skew --
|
48 |
|
|
-- due to hubs can add as much as one bit time before the SE0 of the end of packet. --
|
49 |
|
|
-- This extra bit can result in a "bit stuff violation" if the six bits before it in the CRC are '1's. --
|
50 |
|
|
-- This bit should be ignored by receiver. --
|
51 |
|
|
-- A USB bus is reset using a prolonged (10 to 20 milliseconds) SE0 signal. --
|
52 |
|
|
-- --
|
53 |
|
|
--==========================================================================================================--
|
54 |
|
|
|
55 |
|
|
LIBRARY IEEE;
|
56 |
|
|
USE IEEE.std_logic_1164.all;
|
57 |
|
|
USE IEEE.std_logic_textio.all;
|
58 |
|
|
USE std.textio.all;
|
59 |
|
|
|
60 |
|
|
LIBRARY work;
|
61 |
|
|
USE work.usb_commands.all;
|
62 |
|
|
|
63 |
|
|
ENTITY usb_fs_master IS PORT(
|
64 |
|
|
-- USB Interface --
|
65 |
|
|
usb_clk : IN STD_LOGIC;
|
66 |
|
|
int_clk : IN STD_LOGIC;
|
67 |
|
|
rst_neg_ext : OUT STD_LOGIC;
|
68 |
|
|
usb_Dp : INOUT STD_LOGIC;
|
69 |
|
|
usb_Dn : INOUT STD_LOGIC;
|
70 |
|
|
-- Application Interface
|
71 |
|
|
RXval : IN STD_LOGIC; -- RX bytes available
|
72 |
|
|
RXdat : IN STD_LOGIC_VECTOR(7 DOWNTO 0); -- Received data bytes
|
73 |
|
|
RXrdy : OUT STD_LOGIC := '0'; -- Application ready for data
|
74 |
|
|
RXlen : IN STD_LOGIC_VECTOR(7 DOWNTO 0); -- Number of bytes available
|
75 |
|
|
TXval : OUT STD_LOGIC := '0'; -- Application has valid data
|
76 |
|
|
TXdat : OUT STD_LOGIC_VECTOR(7 DOWNTO 0); -- Data byte to send
|
77 |
|
|
TXrdy : IN STD_LOGIC; -- Entity is ready for data
|
78 |
|
|
TXroom : IN STD_LOGIC_VECTOR(7 DOWNTO 0); -- No of free bytes in TX
|
79 |
|
|
TXcork : OUT STD_LOGIC := '1'); -- Hold TX transmission
|
80 |
|
|
END usb_fs_master;
|
81 |
|
|
|
82 |
|
|
ARCHITECTURE SIM OF usb_fs_master IS
|
83 |
|
|
|
84 |
|
|
SIGNAL T_No : NATURAL;
|
85 |
|
|
SIGNAL crc_16 : STD_LOGIC_VECTOR(15 DOWNTO 0);
|
86 |
|
|
SIGNAL crc_5 : STD_LOGIC_VECTOR( 4 DOWNTO 0);
|
87 |
|
|
SIGNAL master_oe : STD_LOGIC;
|
88 |
|
|
SIGNAL stimuli_bit : STD_LOGIC := 'Z';
|
89 |
|
|
SIGNAL stop_sim : BOOLEAN := false;
|
90 |
|
|
SIGNAL stuffing_requ : BOOLEAN;
|
91 |
|
|
SIGNAL usb_request : usb_action;
|
92 |
|
|
|
93 |
|
|
function next_CRC_5 (Data: std_logic; crc: std_logic_vector(4 downto 0)) return std_logic_vector is
|
94 |
|
|
-- Copyright (C) 1999-2008 Easics NV. http://www.easics.com/webtools/crctool
|
95 |
|
|
variable d: std_logic;
|
96 |
|
|
variable c: std_logic_vector(4 downto 0);
|
97 |
|
|
variable new_crc: std_logic_vector(4 downto 0);
|
98 |
|
|
begin
|
99 |
|
|
d := Data;
|
100 |
|
|
c := crc;
|
101 |
|
|
new_crc(0) := d xor c(4);
|
102 |
|
|
new_crc(1) := c(0);
|
103 |
|
|
new_crc(2) := d xor c(1) xor c(4);
|
104 |
|
|
new_crc(3) := c(2);
|
105 |
|
|
new_crc(4) := c(3);
|
106 |
|
|
return new_crc;
|
107 |
|
|
end next_CRC_5;
|
108 |
|
|
|
109 |
|
|
function next_CRC_16 (Data: std_logic; crc: std_logic_vector(15 downto 0)) return std_logic_vector is
|
110 |
|
|
-- Copyright (C) 1999-2008 Easics NV. http://www.easics.com/webtools/crctool
|
111 |
|
|
variable d: std_logic;
|
112 |
|
|
variable c: std_logic_vector(15 downto 0);
|
113 |
|
|
variable new_crc: std_logic_vector(15 downto 0);
|
114 |
|
|
begin
|
115 |
|
|
d := Data;
|
116 |
|
|
c := crc;
|
117 |
|
|
new_crc(0) := d xor c(15);
|
118 |
|
|
new_crc(1) := c(0);
|
119 |
|
|
new_crc(2) := d xor c(1) xor c(15);
|
120 |
|
|
new_crc(14 DOWNTO 3) := c(13 DOWNTO 2);
|
121 |
|
|
new_crc(15) := d xor c(14) xor c(15);
|
122 |
|
|
return new_crc;
|
123 |
|
|
end next_CRC_16;
|
124 |
|
|
|
125 |
|
|
FUNCTION nrzi(data_bit, last_level : std_logic) RETURN STD_LOGIC IS
|
126 |
|
|
BEGIN
|
127 |
|
|
IF data_bit = '0' THEN
|
128 |
|
|
RETURN not last_level;
|
129 |
|
|
ELSE
|
130 |
|
|
RETURN last_level;
|
131 |
|
|
END IF;
|
132 |
|
|
END nrzi;
|
133 |
|
|
|
134 |
|
|
--==========================================================================================================--
|
135 |
|
|
|
136 |
|
|
begin
|
137 |
|
|
|
138 |
|
|
test_case : ENTITY work.usb_stimuli
|
139 |
|
|
PORT MAP(
|
140 |
|
|
-- Test Control Interface --
|
141 |
|
|
usb => usb_request,
|
142 |
|
|
T_No => T_No,
|
143 |
|
|
-- Application Interface
|
144 |
|
|
clk => int_clk,
|
145 |
|
|
rst_neg_ext => rst_neg_ext,
|
146 |
|
|
RXval => RXval,
|
147 |
|
|
RXdat => RXdat,
|
148 |
|
|
RXrdy => RXrdy,
|
149 |
|
|
RXlen => RXlen,
|
150 |
|
|
TXval => TXval,
|
151 |
|
|
TXdat => TXdat,
|
152 |
|
|
TXrdy => TXrdy,
|
153 |
|
|
TXroom => TXroom,
|
154 |
|
|
TXcork => TXcork
|
155 |
|
|
);
|
156 |
|
|
|
157 |
|
|
usb_fs_monitor : ENTITY work.usb_fs_monitor
|
158 |
|
|
port map (
|
159 |
|
|
clk_60MHz => int_clk,
|
160 |
|
|
master_oe => master_oe,
|
161 |
|
|
usb_Dp => usb_dp,
|
162 |
|
|
usb_Dn => usb_dn
|
163 |
|
|
);
|
164 |
|
|
|
165 |
|
|
master_oe <= '0' WHEN usb_request = idle OR usb_request = recv_eop ELSE '1';
|
166 |
|
|
|
167 |
|
|
p_usb_data : PROCESS
|
168 |
|
|
VARIABLE d_new : STD_LOGIC;
|
169 |
|
|
VARIABLE ones_cnt : NATURAL;
|
170 |
|
|
BEGIN
|
171 |
|
|
WAIT UNTIL rising_edge(usb_clk);
|
172 |
|
|
stuffing_requ <= FALSE;
|
173 |
|
|
IF stimuli_bit = 'L' THEN
|
174 |
|
|
usb_Dp <= '0';
|
175 |
|
|
usb_Dn <= '0';
|
176 |
|
|
ELSIF stimuli_bit = 'Z' THEN
|
177 |
|
|
usb_Dp <= 'Z';
|
178 |
|
|
usb_Dn <= 'L';
|
179 |
|
|
ELSIF stimuli_bit = '1' THEN
|
180 |
|
|
ones_cnt := ones_cnt +1;
|
181 |
|
|
d_new := nrzi('1', usb_Dp);
|
182 |
|
|
usb_Dp <= d_new;
|
183 |
|
|
usb_Dn <= not d_new;
|
184 |
|
|
IF ones_cnt = 6 THEN -- add stuffing bit
|
185 |
|
|
stuffing_requ <= TRUE;
|
186 |
|
|
ones_cnt := 0;
|
187 |
|
|
WAIT UNTIL rising_edge(usb_clk);
|
188 |
|
|
stuffing_requ <= FALSE;
|
189 |
|
|
d_new := nrzi('0', usb_Dp);
|
190 |
|
|
usb_Dp <= d_new;
|
191 |
|
|
usb_Dn <= not d_new;
|
192 |
|
|
END IF;
|
193 |
|
|
ELSE
|
194 |
|
|
ones_cnt := 0;
|
195 |
|
|
d_new := nrzi('0', usb_Dp);
|
196 |
|
|
usb_Dp <= d_new;
|
197 |
|
|
usb_Dn <= not d_new;
|
198 |
|
|
END IF;
|
199 |
|
|
END PROCESS;
|
200 |
|
|
|
201 |
|
|
p_stimuli_bit : PROCESS --always transfer LSB first (exception crc)
|
202 |
|
|
CONSTANT sync_data : std_logic_vector(7 DOWNTO 0) := X"80"; --USB FS : sync patter is KJKJKJKK
|
203 |
|
|
CONSTANT eop_data : std_logic_vector(3 DOWNTO 0) := "Z0LL"; --'L' forces both usb_up, usb_dn low !!
|
204 |
|
|
BEGIN
|
205 |
|
|
WAIT ON usb_request;
|
206 |
|
|
IF usb_request = reset THEN
|
207 |
|
|
usb_status <= usb_request;
|
208 |
|
|
stimuli_bit <= 'L';
|
209 |
|
|
WAIT FOR 5 us;
|
210 |
|
|
WAIT UNTIL rising_edge(usb_clk);
|
211 |
|
|
stimuli_bit <= 'Z';
|
212 |
|
|
usb_status <= idle;
|
213 |
|
|
ELSIF usb_request = sync THEN
|
214 |
|
|
usb_status <= usb_request;
|
215 |
|
|
FOR i IN 0 TO 7 LOOP -- Sync pattern
|
216 |
|
|
WAIT UNTIL rising_edge(usb_clk);
|
217 |
|
|
stimuli_bit <= sync_data(i);
|
218 |
|
|
END LOOP;
|
219 |
|
|
usb_status <= idle;
|
220 |
|
|
ELSIF usb_request = pid THEN
|
221 |
|
|
usb_status <= usb_request;
|
222 |
|
|
FOR i IN 0 TO 7 LOOP
|
223 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND NOT stuffing_requ;
|
224 |
|
|
stimuli_bit <= sv_usb_byte(i);
|
225 |
|
|
END LOOP;
|
226 |
|
|
crc_5 <= (others =>'1');
|
227 |
|
|
crc_16 <= (others =>'1');
|
228 |
|
|
usb_status <= idle;
|
229 |
|
|
ELSIF usb_request = addr THEN
|
230 |
|
|
usb_status <= usb_request;
|
231 |
|
|
FOR i IN 0 TO 10 LOOP
|
232 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND NOT stuffing_requ;
|
233 |
|
|
stimuli_bit <= sv_usb_addr(i);
|
234 |
|
|
crc_5 <= next_crc_5(sv_usb_addr(i),crc_5);
|
235 |
|
|
END LOOP;
|
236 |
|
|
usb_status <= idle;
|
237 |
|
|
ELSIF usb_request = wr_odd OR usb_request = wr_even THEN
|
238 |
|
|
usb_status <= usb_request;
|
239 |
|
|
FOR i IN 0 TO 7 LOOP
|
240 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND NOT stuffing_requ;
|
241 |
|
|
stimuli_bit <= sv_usb_byte(i);
|
242 |
|
|
crc_16 <= next_crc_16(sv_usb_byte(i),crc_16);
|
243 |
|
|
END LOOP;
|
244 |
|
|
usb_status <= idle;
|
245 |
|
|
-- WAIT for 1 ns;
|
246 |
|
|
ELSIF usb_request = wr_crc5 THEN
|
247 |
|
|
usb_status <= usb_request;
|
248 |
|
|
FOR i IN 4 DOWNTO 0 LOOP -- Token crc5, LSB last
|
249 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND NOT stuffing_requ;
|
250 |
|
|
stimuli_bit <= NOT crc_5(i);
|
251 |
|
|
END LOOP;
|
252 |
|
|
usb_status <= idle;
|
253 |
|
|
ELSIF usb_request = wr_crc16 THEN
|
254 |
|
|
usb_status <= usb_request;
|
255 |
|
|
FOR i IN 15 DOWNTO 0 LOOP -- Data crc16, LSB last
|
256 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND NOT stuffing_requ;
|
257 |
|
|
stimuli_bit <= NOT crc_16(i);
|
258 |
|
|
END LOOP;
|
259 |
|
|
usb_status <= idle;
|
260 |
|
|
ELSIF usb_request = send_eop THEN
|
261 |
|
|
usb_status <= usb_request;
|
262 |
|
|
FOR i IN 0 TO 3 LOOP
|
263 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND NOT stuffing_requ;
|
264 |
|
|
stimuli_bit <= eop_data(i);
|
265 |
|
|
END LOOP;
|
266 |
|
|
usb_status <= idle;
|
267 |
|
|
ELSIF usb_request = Recv_eop THEN
|
268 |
|
|
usb_status <= usb_request;
|
269 |
|
|
WAIT UNTIL rising_edge(usb_clk) AND usb_Dp ='0' AND usb_Dn ='0';
|
270 |
|
|
WAIT FOR 400 ns;
|
271 |
|
|
usb_status <= idle;
|
272 |
|
|
ELSE
|
273 |
|
|
stimuli_bit <= 'Z';
|
274 |
|
|
usb_status <= idle;
|
275 |
|
|
END IF;
|
276 |
|
|
END PROCESS;
|
277 |
|
|
|
278 |
|
|
END SIM;
|
279 |
|
|
|
280 |
|
|
--======================================== END OF usb_fs_master.vhd ========================================--
|