1 |
2 |
rehnmaak |
--
|
2 |
|
|
-- USB 2.0 Default control endpoint
|
3 |
|
|
--
|
4 |
|
|
-- This entity implements the minimal required functionality of
|
5 |
|
|
-- the default control endpoint.
|
6 |
|
|
--
|
7 |
|
|
-- The low-level interface signals are named T_xxx and should be conditionally
|
8 |
|
|
-- connected to the usb_transact interface in the following way:
|
9 |
|
|
-- * Always connect output signal C_ADDR to T_ADDR;
|
10 |
|
|
-- * Always connect input signals T_FIN, T_OSYNC, T_RXRDY, T_RXDAT, T_TXRDY;
|
11 |
|
|
-- * If T_ENDPT = 0, connect input signals T_IN, T_OUT, T_SETUP, T_PING;
|
12 |
|
|
-- otherwise pull these inputs to zero.
|
13 |
|
|
-- * If T_ENDPT = 0, connect output signals T_NAK, T_STALL, T_NYET, T_SEND,
|
14 |
|
|
-- T_ISYNC, T_TXDAT; otherwise another endpoint should drive these.
|
15 |
|
|
-- * If T_ENDPT = 0 and C_DSCBUSY = 0, connect output signal T_TXDAT;
|
16 |
|
|
-- otherwise if T_ENDPT = 0 and C_DSCBUSY = 1, drive T_TXDAT
|
17 |
|
|
-- from descriptor memory;
|
18 |
|
|
-- otherwise another endpoint drives T_TXDAT.
|
19 |
|
|
--
|
20 |
|
|
-- A device descriptor and a configuration descriptor must be provided
|
21 |
|
|
-- in external memory. If high speed mode is supported, an other-speed
|
22 |
|
|
-- device qualifier and other-speed configuration descriptor must also
|
23 |
|
|
-- be provided. In addition, string descriptors may optionally be provided.
|
24 |
|
|
-- Each descriptor may be at most 255 bytes long.
|
25 |
|
|
-- A maximum packet size of 64 bytes is assumed for control transfers.
|
26 |
|
|
--
|
27 |
|
|
-- This entity uses the following protocol to access descriptor data:
|
28 |
|
|
-- * When C_DSCBUSY is high, the entity is accessing descriptor data.
|
29 |
|
|
-- A descriptor is selected by signals C_DSCTYP and C_DSCINX;
|
30 |
|
|
-- a byte within this descriptor is selected by signal C_DSCOFF.
|
31 |
|
|
-- * Based on C_DSCTYP and C_DSCINX, the application must assign
|
32 |
|
|
-- the length of the selected descriptor to C_DSCLEN. If the selected
|
33 |
|
|
-- descriptor does not exist, the application must set C_DSCLEN to zero.
|
34 |
|
|
-- C_DSCLEN must be valid one clock after rising C_DSCBUSY and must
|
35 |
|
|
-- remain stable as long as C_DSCBUSY, C_DSCTYP and C_DSCINX remain
|
36 |
|
|
-- unchanged.
|
37 |
|
|
-- * When C_DSCRD is asserted, the application must put the selected
|
38 |
|
|
-- byte from the selected descriptor on T_TXDAT towards usb_transact.
|
39 |
|
|
-- The application must respond in the first clock cycle following
|
40 |
|
|
-- assertion of C_DSCRD.
|
41 |
|
|
-- * When C_DSCRD is not asserted, but C_DSCBUSY is still high,
|
42 |
|
|
-- the application must keep T_TXDAT unchanged. Changes to C_DSCOFF
|
43 |
|
|
-- must not affect T_TXDAT while C_DSCRD is low.
|
44 |
|
|
--
|
45 |
|
|
-- The standard device requests are handled as follows:
|
46 |
|
|
--
|
47 |
|
|
-- Clear Feature:
|
48 |
|
|
-- When clearing the ENDPOINT_HALT feature, reset the endpoint's
|
49 |
|
|
-- sync bits (as required by spec). Otherwise ignore but report
|
50 |
|
|
-- success status.
|
51 |
|
|
-- BAD: should return STALL when referring to invalid endpoint/interface.
|
52 |
|
|
--
|
53 |
|
|
-- Get Configuration:
|
54 |
|
|
-- Return 1 if configured, 0 if not configured.
|
55 |
|
|
--
|
56 |
|
|
-- Get Descriptor:
|
57 |
|
|
-- Handled by application through descriptor data interface as
|
58 |
|
|
-- described above.
|
59 |
|
|
--
|
60 |
|
|
-- Get Interface:
|
61 |
|
|
-- Always return zero byte.
|
62 |
|
|
-- BAD: should return STALL when referring to invalid endpoint/interface.
|
63 |
|
|
--
|
64 |
|
|
-- Get Status:
|
65 |
|
|
-- Return device status / endpoint status / zero.
|
66 |
|
|
-- BAD: should return STALL when referring to invalid endpoint/interface.
|
67 |
|
|
--
|
68 |
|
|
-- Set Address:
|
69 |
|
|
-- Store new address.
|
70 |
|
|
--
|
71 |
|
|
-- Set Configuration:
|
72 |
|
|
-- Switch between Configured and Address states; clear all endpoint
|
73 |
|
|
-- sync bits (as required by spec). Accepts only configuration values
|
74 |
|
|
-- 0 and 1.
|
75 |
|
|
--
|
76 |
|
|
-- Set Descriptor:
|
77 |
|
|
-- Not implemented; returns STALL. (Correct; request is optional.)
|
78 |
|
|
--
|
79 |
|
|
-- Set Feature:
|
80 |
|
|
-- Only ENDPOINT_HALT feature implemented; otherwise returns STALL.
|
81 |
|
|
-- BAD: every high speed device must support TEST_MODE.
|
82 |
|
|
--
|
83 |
|
|
-- Set Interface:
|
84 |
|
|
-- Not implemented; returns STALL.
|
85 |
|
|
-- (Correct; request is optional if no interfaces have alternate settings.)
|
86 |
|
|
--
|
87 |
|
|
-- Synch Frame:
|
88 |
|
|
-- Not implemented; returns STALL.
|
89 |
|
|
-- (Correct, assuming no isosynchronous endpoints.)
|
90 |
|
|
--
|
91 |
|
|
-- Non-standard requests are silently ignored but return success status.
|
92 |
|
|
-- This is incorrect, but necessary to get host software to accept usb_serial
|
93 |
|
|
-- as CDC-ACM device.
|
94 |
|
|
--
|
95 |
|
|
|
96 |
|
|
library ieee;
|
97 |
|
|
use ieee.std_logic_1164.all, ieee.numeric_std.all;
|
98 |
|
|
|
99 |
|
|
entity usb_control is
|
100 |
|
|
|
101 |
|
|
generic (
|
102 |
|
|
|
103 |
|
|
-- Highest endpoint number in use.
|
104 |
|
|
NENDPT : integer range 1 to 15 );
|
105 |
|
|
|
106 |
|
|
port (
|
107 |
|
|
|
108 |
|
|
-- 60 MHz UTMI clock.
|
109 |
|
|
CLK : in std_logic;
|
110 |
|
|
|
111 |
|
|
-- Synchronous reset of this entity.
|
112 |
|
|
RESET : in std_logic;
|
113 |
|
|
|
114 |
|
|
-- Current device address.
|
115 |
|
|
C_ADDR : out std_logic_vector(6 downto 0);
|
116 |
|
|
|
117 |
|
|
-- High when in Configured state.
|
118 |
|
|
C_CONFD : out std_logic;
|
119 |
|
|
|
120 |
|
|
-- Trigger clearing of sync/halt bits for IN endpoint.
|
121 |
|
|
C_CLRIN : out std_logic_vector(1 to NENDPT);
|
122 |
|
|
|
123 |
|
|
-- Trigger clearing of sync/halt bits for OUT endpoint.
|
124 |
|
|
C_CLROUT : out std_logic_vector(1 to NENDPT);
|
125 |
|
|
|
126 |
|
|
-- Current status of halt bit for IN endpoints.
|
127 |
|
|
C_HLTIN : in std_logic_vector(1 to NENDPT);
|
128 |
|
|
|
129 |
|
|
-- Current status of halt bit for IN endpoints.
|
130 |
|
|
C_HLTOUT : in std_logic_vector(1 to NENDPT);
|
131 |
|
|
|
132 |
|
|
-- Trigger setting of halt bit for IN endpoints.
|
133 |
|
|
C_SHLTIN : out std_logic_vector(1 to NENDPT);
|
134 |
|
|
|
135 |
|
|
-- Trigger setting of halt bit for OUT endpoints.
|
136 |
|
|
C_SHLTOUT : out std_logic_vector(1 to NENDPT);
|
137 |
|
|
|
138 |
|
|
-- High when accessing descriptor memory.
|
139 |
|
|
-- Note that C_DSCBUSY may go low in between packets of a single descriptor.
|
140 |
|
|
C_DSCBUSY : out std_logic;
|
141 |
|
|
|
142 |
|
|
-- Descriptor read enable. Asserted to request a descriptor byte;
|
143 |
|
|
-- in the next clock cycle, the application must update T_TXDAT.
|
144 |
|
|
C_DSCRD : out std_logic;
|
145 |
|
|
|
146 |
|
|
-- LSB bits of the requested descriptor type. Valid when C_DSCBUSY is high.
|
147 |
|
|
C_DSCTYP : out std_logic_vector(2 downto 0);
|
148 |
|
|
|
149 |
|
|
-- Requested descriptor index. Valid when C_DSCBUSY is high.
|
150 |
|
|
C_DSCINX : out std_logic_vector(7 downto 0);
|
151 |
|
|
|
152 |
|
|
-- Offset within requested descriptor. Valid when C_DSCBUSY and C_DSCRD are high.
|
153 |
|
|
C_DSCOFF : out std_logic_vector(7 downto 0);
|
154 |
|
|
|
155 |
|
|
-- Set to length of current descriptor by application.
|
156 |
|
|
C_DSCLEN : in std_logic_vector(7 downto 0);
|
157 |
|
|
|
158 |
|
|
-- High if the device is not drawing bus power.
|
159 |
|
|
C_SELFPOWERED : in std_logic;
|
160 |
|
|
|
161 |
|
|
-- Connect to T_IN from usb_transact when T_ENDPT = 0, otherwise pull to 0.
|
162 |
|
|
T_IN : in std_logic;
|
163 |
|
|
|
164 |
|
|
-- Connect to T_OUT from usb_transact when T_ENDPT = 0, otherwise pull to 0.
|
165 |
|
|
T_OUT : in std_logic;
|
166 |
|
|
|
167 |
|
|
-- Connect to T_SETUP from usb_transact when T_ENDPT = 0, otherwise pull to 0.
|
168 |
|
|
T_SETUP : in std_logic;
|
169 |
|
|
|
170 |
|
|
-- Connect to T_PING from usb_transact when T_ENDPT = 0, otherwise pull to 0.
|
171 |
|
|
T_PING : in std_logic;
|
172 |
|
|
|
173 |
|
|
-- Connect to T_FIN from ubs_transact.
|
174 |
|
|
T_FIN : in std_logic;
|
175 |
|
|
|
176 |
|
|
-- Connect to T_NAK towards usb_transact when T_ENDPT = 0.
|
177 |
|
|
T_NAK : out std_logic;
|
178 |
|
|
|
179 |
|
|
-- Connect to T_STALL towards usb_transact when T_ENDPT = 0.
|
180 |
|
|
T_STALL : out std_logic;
|
181 |
|
|
|
182 |
|
|
-- Connect to T_NYET towards usb_transact when T_ENDPT = 0.
|
183 |
|
|
T_NYET : out std_logic;
|
184 |
|
|
|
185 |
|
|
-- Connect to T_SEND towards usb_transact when T_ENDPT = 0.
|
186 |
|
|
T_SEND : out std_logic;
|
187 |
|
|
|
188 |
|
|
-- Connect to T_ISYNC towards usb_transact when T_ENDPT = 0.
|
189 |
|
|
T_ISYNC : out std_logic;
|
190 |
|
|
|
191 |
|
|
-- Connect to T_OSYNC from usb_transact.
|
192 |
|
|
T_OSYNC : in std_logic;
|
193 |
|
|
|
194 |
|
|
-- Connect to T_RXRDY from usb_transact.
|
195 |
|
|
T_RXRDY : in std_logic;
|
196 |
|
|
|
197 |
|
|
-- Connect to T_RXDAT from usb_transact.
|
198 |
|
|
T_RXDAT : in std_logic_vector(7 downto 0);
|
199 |
|
|
|
200 |
|
|
-- Connect to T_TXRDY from usb_transact.
|
201 |
|
|
T_TXRDY : in std_logic;
|
202 |
|
|
|
203 |
|
|
-- Connect to T_TXDAT towards usb_transact when T_ENDPT = 0 and C_DSCBUSY = '0'.
|
204 |
|
|
T_TXDAT : out std_logic_vector(7 downto 0) );
|
205 |
|
|
|
206 |
|
|
end entity usb_control;
|
207 |
|
|
|
208 |
|
|
architecture usb_control_arch of usb_control is
|
209 |
|
|
|
210 |
|
|
-- Constants for control request
|
211 |
|
|
constant req_getstatus : std_logic_vector(3 downto 0) := "0000";
|
212 |
|
|
constant req_clearfeature : std_logic_vector(3 downto 0) := "0001";
|
213 |
|
|
constant req_setfeature : std_logic_vector(3 downto 0) := "0011";
|
214 |
|
|
constant req_setaddress : std_logic_vector(3 downto 0) := "0101";
|
215 |
|
|
constant req_getdesc : std_logic_vector(3 downto 0) := "0110";
|
216 |
|
|
constant req_getconf : std_logic_vector(3 downto 0) := "1000";
|
217 |
|
|
constant req_setconf : std_logic_vector(3 downto 0) := "1001";
|
218 |
|
|
constant req_getiface : std_logic_vector(3 downto 0) := "1010";
|
219 |
|
|
|
220 |
|
|
-- State machine
|
221 |
|
|
type t_state is (
|
222 |
|
|
ST_IDLE, ST_STALL,
|
223 |
|
|
ST_SETUP, ST_SETUPERR, ST_NONSTANDARD, ST_ENDSETUP, ST_WAITIN,
|
224 |
|
|
ST_SENDRESP, ST_STARTDESC, ST_SENDDESC, ST_DONESEND );
|
225 |
|
|
signal s_state : t_state := ST_IDLE;
|
226 |
|
|
|
227 |
|
|
-- Current control request
|
228 |
|
|
signal s_ctlrequest : std_logic_vector(3 downto 0);
|
229 |
|
|
signal s_ctlparam : std_logic_vector(7 downto 0);
|
230 |
|
|
signal s_desctyp : std_logic_vector(2 downto 0);
|
231 |
|
|
signal s_answerlen : unsigned(7 downto 0);
|
232 |
|
|
signal s_sendbyte : std_logic_vector(7 downto 0) := "00000000";
|
233 |
|
|
|
234 |
|
|
-- Device state
|
235 |
|
|
signal s_addr : std_logic_vector(6 downto 0) := "0000000";
|
236 |
|
|
signal s_confd : std_logic := '0';
|
237 |
|
|
|
238 |
|
|
-- Counters
|
239 |
|
|
signal s_setupptr : unsigned(2 downto 0);
|
240 |
|
|
signal s_answerptr : unsigned(7 downto 0);
|
241 |
|
|
|
242 |
|
|
begin
|
243 |
|
|
|
244 |
|
|
-- Status signals
|
245 |
|
|
C_ADDR <= s_addr;
|
246 |
|
|
C_CONFD <= s_confd;
|
247 |
|
|
|
248 |
|
|
-- Memory interface
|
249 |
|
|
C_DSCBUSY <= T_IN when (s_state = ST_WAITIN) else
|
250 |
|
|
'1' when (s_state = ST_STARTDESC or s_state = ST_SENDDESC) else
|
251 |
|
|
'0';
|
252 |
|
|
C_DSCRD <= '1' when (s_state = ST_STARTDESC) else T_TXRDY;
|
253 |
|
|
C_DSCTYP <= s_desctyp;
|
254 |
|
|
C_DSCINX <= s_ctlparam;
|
255 |
|
|
C_DSCOFF <= std_logic_vector(s_answerptr);
|
256 |
|
|
|
257 |
|
|
-- Transaction interface
|
258 |
|
|
T_NAK <= '0';
|
259 |
|
|
T_STALL <= '1' when (s_state = ST_STALL) else '0';
|
260 |
|
|
T_NYET <= '0';
|
261 |
|
|
T_SEND <= '1' when ((s_state = ST_SENDRESP) or (s_state = ST_SENDDESC))
|
262 |
|
|
else '0';
|
263 |
|
|
T_ISYNC <= not std_logic(s_answerptr(6));
|
264 |
|
|
T_TXDAT <= s_sendbyte;
|
265 |
|
|
|
266 |
|
|
-- On every rising clock edge
|
267 |
|
|
process is
|
268 |
|
|
begin
|
269 |
|
|
wait until rising_edge(CLK);
|
270 |
|
|
|
271 |
|
|
-- Set endpoint reset/halt lines to zero by default
|
272 |
|
|
C_CLRIN <= (others => '0');
|
273 |
|
|
C_CLROUT <= (others => '0');
|
274 |
|
|
C_SHLTIN <= (others => '0');
|
275 |
|
|
C_SHLTOUT <= (others => '0');
|
276 |
|
|
|
277 |
|
|
-- State machine
|
278 |
|
|
if RESET = '1' then
|
279 |
|
|
|
280 |
|
|
-- Reset this entity
|
281 |
|
|
s_state <= ST_IDLE;
|
282 |
|
|
s_addr <= "0000000";
|
283 |
|
|
s_confd <= '0';
|
284 |
|
|
|
285 |
|
|
-- Trigger endpoint reset lines
|
286 |
|
|
C_CLRIN <= (others => '1');
|
287 |
|
|
C_CLROUT <= (others => '1');
|
288 |
|
|
|
289 |
|
|
else
|
290 |
|
|
|
291 |
|
|
case s_state is
|
292 |
|
|
|
293 |
|
|
when ST_IDLE =>
|
294 |
|
|
-- Idle; wait for SETUP transaction;
|
295 |
|
|
-- OUT transactions are ignored but acknowledged;
|
296 |
|
|
-- IN transactions send an empty packet.
|
297 |
|
|
s_answerptr <= to_unsigned(0, s_answerptr'length);
|
298 |
|
|
if T_SETUP = '1' then
|
299 |
|
|
-- Start of SETUP transaction
|
300 |
|
|
s_state <= ST_SETUP;
|
301 |
|
|
s_setupptr <= to_unsigned(0, s_setupptr'length);
|
302 |
|
|
end if;
|
303 |
|
|
|
304 |
|
|
when ST_STALL =>
|
305 |
|
|
-- Stalled; wait for next SETUP transaction;
|
306 |
|
|
-- respond to IN/OUT transactions with a STALL handshake.
|
307 |
|
|
if T_SETUP = '1' then
|
308 |
|
|
-- Start of SETUP transaction
|
309 |
|
|
s_state <= ST_SETUP;
|
310 |
|
|
s_setupptr <= to_unsigned(0, s_setupptr'length);
|
311 |
|
|
end if;
|
312 |
|
|
|
313 |
|
|
when ST_SETUP =>
|
314 |
|
|
-- In SETUP transaction; parse request structure.
|
315 |
|
|
s_answerptr <= to_unsigned(0, s_answerptr'length);
|
316 |
|
|
if T_RXRDY = '1' then
|
317 |
|
|
-- Process next request byte
|
318 |
|
|
case s_setupptr is
|
319 |
|
|
when "000" =>
|
320 |
|
|
-- bmRequestType
|
321 |
|
|
s_ctlparam <= T_RXDAT;
|
322 |
|
|
if T_RXDAT(6 downto 5) /= "00" then
|
323 |
|
|
-- non-standard device request
|
324 |
|
|
s_state <= ST_NONSTANDARD;
|
325 |
|
|
end if;
|
326 |
|
|
when "001" =>
|
327 |
|
|
-- bRequest
|
328 |
|
|
s_ctlrequest <= T_RXDAT(3 downto 0);
|
329 |
|
|
if T_RXDAT(7 downto 4) /= "0000" then
|
330 |
|
|
-- Unknown request
|
331 |
|
|
s_state <= ST_SETUPERR;
|
332 |
|
|
end if;
|
333 |
|
|
when "010" =>
|
334 |
|
|
-- wValue lsb
|
335 |
|
|
if s_ctlrequest /= req_getstatus then
|
336 |
|
|
s_ctlparam <= T_RXDAT;
|
337 |
|
|
end if;
|
338 |
|
|
when "011" =>
|
339 |
|
|
-- wValue msb
|
340 |
|
|
if s_ctlrequest = req_getdesc then
|
341 |
|
|
if T_RXDAT(7 downto 3) /= "00000" then
|
342 |
|
|
-- Unsupported descriptor type
|
343 |
|
|
s_state <= ST_SETUPERR;
|
344 |
|
|
end if;
|
345 |
|
|
end if;
|
346 |
|
|
-- Store descriptor type (assuming GET_DESCRIPTOR request)
|
347 |
|
|
s_desctyp <= T_RXDAT(2 downto 0);
|
348 |
|
|
when "100" =>
|
349 |
|
|
-- wIndex lsb
|
350 |
|
|
case s_ctlrequest is
|
351 |
|
|
when req_clearfeature =>
|
352 |
|
|
if s_ctlparam = "00000000" then
|
353 |
|
|
-- Clear ENDPOINT_HALT feature;
|
354 |
|
|
-- store endpoint selector
|
355 |
|
|
s_ctlparam <= T_RXDAT;
|
356 |
|
|
else
|
357 |
|
|
-- Unknown clear feature request
|
358 |
|
|
s_ctlparam <= "00000000";
|
359 |
|
|
end if;
|
360 |
|
|
when req_setfeature =>
|
361 |
|
|
if s_ctlparam = "00000000" then
|
362 |
|
|
-- Set ENDPOINT_HALT feature;
|
363 |
|
|
-- store endpoint selector
|
364 |
|
|
s_ctlparam <= T_RXDAT;
|
365 |
|
|
else
|
366 |
|
|
-- Unsupported set feature request
|
367 |
|
|
s_state <= ST_SETUPERR;
|
368 |
|
|
end if;
|
369 |
|
|
when req_getstatus =>
|
370 |
|
|
if s_ctlparam(1 downto 0) = "00" then
|
371 |
|
|
-- Get device status
|
372 |
|
|
s_sendbyte <= "0000000" & C_SELFPOWERED;
|
373 |
|
|
s_ctlparam <= "00000000";
|
374 |
|
|
elsif s_ctlparam(1 downto 0) = "10" then
|
375 |
|
|
-- Get endpoint status
|
376 |
|
|
s_sendbyte <= "00000000";
|
377 |
|
|
s_ctlparam <= T_RXDAT;
|
378 |
|
|
else
|
379 |
|
|
-- Probably get interface status
|
380 |
|
|
s_sendbyte <= "00000000";
|
381 |
|
|
s_ctlparam <= "00000000";
|
382 |
|
|
end if;
|
383 |
|
|
when others =>
|
384 |
|
|
-- Don't care about index.
|
385 |
|
|
end case;
|
386 |
|
|
when "101" =>
|
387 |
|
|
-- wIndex msb; don't care
|
388 |
|
|
when "110" =>
|
389 |
|
|
-- wLength lsb
|
390 |
|
|
s_answerlen <= unsigned(T_RXDAT);
|
391 |
|
|
when "111" =>
|
392 |
|
|
-- wLength msb
|
393 |
|
|
if T_RXDAT /= "00000000" then
|
394 |
|
|
s_answerlen <= "11111111";
|
395 |
|
|
end if;
|
396 |
|
|
s_state <= ST_ENDSETUP;
|
397 |
|
|
when others =>
|
398 |
|
|
-- Impossible
|
399 |
|
|
end case;
|
400 |
|
|
-- Increment position within SETUP packet
|
401 |
|
|
s_setupptr <= s_setupptr + 1;
|
402 |
|
|
elsif T_FIN = '1' then
|
403 |
|
|
-- Got short SETUP packet; answer with STALL status.
|
404 |
|
|
s_state <= ST_STALL;
|
405 |
|
|
elsif T_SETUP = '0' then
|
406 |
|
|
-- Got corrupt SETUP packet; ignore.
|
407 |
|
|
s_state <= ST_IDLE;
|
408 |
|
|
end if;
|
409 |
|
|
|
410 |
|
|
when ST_SETUPERR =>
|
411 |
|
|
-- In SETUP transaction; got request error
|
412 |
|
|
if T_FIN = '1' then
|
413 |
|
|
-- Got good SETUP packet that causes request error
|
414 |
|
|
s_state <= ST_STALL;
|
415 |
|
|
elsif T_SETUP = '0' then
|
416 |
|
|
-- Got corrupt SETUP packet; ignore
|
417 |
|
|
s_state <= ST_IDLE;
|
418 |
|
|
end if;
|
419 |
|
|
|
420 |
|
|
when ST_NONSTANDARD =>
|
421 |
|
|
-- Ignore non-standard requests
|
422 |
|
|
if T_SETUP = '0' then
|
423 |
|
|
s_state <= ST_IDLE;
|
424 |
|
|
end if;
|
425 |
|
|
|
426 |
|
|
when ST_ENDSETUP =>
|
427 |
|
|
-- Parsed request packet; wait for end of SETUP transaction
|
428 |
|
|
if T_FIN = '1' then
|
429 |
|
|
-- Got complet SETUP packet; handle it
|
430 |
|
|
case s_ctlrequest is
|
431 |
|
|
when req_getstatus =>
|
432 |
|
|
-- Prepare status byte and move to data stage
|
433 |
|
|
-- If s_ctlparam = 0, the status byte has already
|
434 |
|
|
-- been prepared in state S_SETUP.
|
435 |
|
|
for i in 1 to NENDPT loop
|
436 |
|
|
if unsigned(s_ctlparam(3 downto 0)) = i then
|
437 |
|
|
if s_ctlparam(7) = '1' then
|
438 |
|
|
s_sendbyte <= "0000000" & C_HLTIN(i);
|
439 |
|
|
else
|
440 |
|
|
s_sendbyte <= "0000000" & C_HLTOUT(i);
|
441 |
|
|
end if;
|
442 |
|
|
end if;
|
443 |
|
|
end loop;
|
444 |
|
|
s_state <= ST_WAITIN;
|
445 |
|
|
when req_clearfeature =>
|
446 |
|
|
-- Reset endpoint
|
447 |
|
|
for i in 1 to NENDPT loop
|
448 |
|
|
if unsigned(s_ctlparam(3 downto 0)) = i then
|
449 |
|
|
if s_ctlparam(7) = '1' then
|
450 |
|
|
C_CLRIN(i) <= '1';
|
451 |
|
|
else
|
452 |
|
|
C_CLROUT(i) <= '1';
|
453 |
|
|
end if;
|
454 |
|
|
end if;
|
455 |
|
|
end loop;
|
456 |
|
|
s_state <= ST_IDLE;
|
457 |
|
|
when req_setfeature =>
|
458 |
|
|
-- Set endpoint HALT
|
459 |
|
|
for i in 1 to NENDPT loop
|
460 |
|
|
if unsigned(s_ctlparam(3 downto 0)) = i then
|
461 |
|
|
if s_ctlparam(7) = '1' then
|
462 |
|
|
C_SHLTIN(i) <= '1';
|
463 |
|
|
else
|
464 |
|
|
C_SHLTOUT(i) <= '1';
|
465 |
|
|
end if;
|
466 |
|
|
end if;
|
467 |
|
|
end loop;
|
468 |
|
|
s_state <= ST_IDLE;
|
469 |
|
|
when req_setaddress =>
|
470 |
|
|
-- Move to status stage
|
471 |
|
|
s_state <= ST_WAITIN;
|
472 |
|
|
when req_getdesc =>
|
473 |
|
|
-- Move to data stage
|
474 |
|
|
s_state <= ST_WAITIN;
|
475 |
|
|
when req_getconf =>
|
476 |
|
|
-- Move to data stage
|
477 |
|
|
s_state <= ST_WAITIN;
|
478 |
|
|
when req_setconf =>
|
479 |
|
|
-- Set device configuration
|
480 |
|
|
if s_ctlparam(7 downto 1) = "0000000" then
|
481 |
|
|
s_confd <= s_ctlparam(0);
|
482 |
|
|
s_state <= ST_IDLE;
|
483 |
|
|
C_CLRIN <= (others => '1');
|
484 |
|
|
C_CLROUT <= (others => '1');
|
485 |
|
|
else
|
486 |
|
|
-- Unknown configuration number
|
487 |
|
|
s_state <= ST_STALL;
|
488 |
|
|
end if;
|
489 |
|
|
when req_getiface =>
|
490 |
|
|
-- Move to data stage
|
491 |
|
|
s_state <= ST_WAITIN;
|
492 |
|
|
when others =>
|
493 |
|
|
-- Unsupported request
|
494 |
|
|
s_state <= ST_STALL;
|
495 |
|
|
end case;
|
496 |
|
|
elsif T_SETUP = '0' then
|
497 |
|
|
-- Got corrupt SETUP packet; ignore
|
498 |
|
|
s_state <= ST_IDLE;
|
499 |
|
|
end if;
|
500 |
|
|
|
501 |
|
|
when ST_WAITIN =>
|
502 |
|
|
-- Got valid SETUP packet; waiting for IN transaction.
|
503 |
|
|
s_answerptr(5 downto 0) <= "000000";
|
504 |
|
|
if T_SETUP = '1' then
|
505 |
|
|
-- Start of next SETUP transaction
|
506 |
|
|
s_state <= ST_SETUP;
|
507 |
|
|
s_setupptr <= to_unsigned(0, s_setupptr'length);
|
508 |
|
|
elsif T_IN = '1' then
|
509 |
|
|
-- Start of IN transaction; respond to the request
|
510 |
|
|
case s_ctlrequest is
|
511 |
|
|
when req_getstatus =>
|
512 |
|
|
-- Respond with status byte, followed by zero byte.
|
513 |
|
|
s_state <= ST_SENDRESP;
|
514 |
|
|
when req_setaddress =>
|
515 |
|
|
-- Effectuate change of device address
|
516 |
|
|
s_addr <= s_ctlparam(6 downto 0);
|
517 |
|
|
s_state <= ST_IDLE;
|
518 |
|
|
when req_getdesc =>
|
519 |
|
|
-- Respond with descriptor
|
520 |
|
|
s_state <= ST_STARTDESC;
|
521 |
|
|
when req_getconf =>
|
522 |
|
|
-- Respond with current configuration
|
523 |
|
|
s_sendbyte <= "0000000" & s_confd;
|
524 |
|
|
s_state <= ST_SENDRESP;
|
525 |
|
|
when req_getiface =>
|
526 |
|
|
-- Respond with zero byte
|
527 |
|
|
s_sendbyte <= "00000000";
|
528 |
|
|
s_state <= ST_SENDRESP;
|
529 |
|
|
when others =>
|
530 |
|
|
-- Impossible
|
531 |
|
|
end case;
|
532 |
|
|
end if;
|
533 |
|
|
|
534 |
|
|
when ST_SENDRESP =>
|
535 |
|
|
-- Respond to IN with a preset byte,
|
536 |
|
|
-- followed by zero or more nul byte(s)
|
537 |
|
|
if T_IN = '0' then
|
538 |
|
|
-- Aborted IN transaction; wait for retry
|
539 |
|
|
s_state <= ST_WAITIN;
|
540 |
|
|
elsif T_TXRDY = '1' then
|
541 |
|
|
-- Need next data byte
|
542 |
|
|
s_sendbyte <= "00000000";
|
543 |
|
|
if (s_answerptr(0) = '1') or (s_answerlen(0) = '1') then
|
544 |
|
|
-- Reached end of transfer.
|
545 |
|
|
-- Note that we only ever send 1 or 2 byte answers.
|
546 |
|
|
s_state <= ST_DONESEND;
|
547 |
|
|
end if;
|
548 |
|
|
s_answerptr(5 downto 0) <= s_answerptr(5 downto 0) + 1;
|
549 |
|
|
end if;
|
550 |
|
|
|
551 |
|
|
when ST_STARTDESC =>
|
552 |
|
|
-- Fetching first byte of packet.
|
553 |
|
|
if T_IN = '0' then
|
554 |
|
|
-- Aborted IN transaction; wait for retry
|
555 |
|
|
s_state <= ST_WAITIN;
|
556 |
|
|
elsif unsigned(C_DSCLEN) = 0 then
|
557 |
|
|
-- Invalid descriptor.
|
558 |
|
|
s_state <= ST_STALL;
|
559 |
|
|
elsif (s_answerptr = unsigned(C_DSCLEN)) or
|
560 |
|
|
(s_answerptr = s_answerlen) then
|
561 |
|
|
-- Send an empty packet to complete the transfer.
|
562 |
|
|
s_state <= ST_DONESEND;
|
563 |
|
|
else
|
564 |
|
|
-- Send a normal descriptor packet.
|
565 |
|
|
s_state <= ST_SENDDESC;
|
566 |
|
|
end if;
|
567 |
|
|
s_answerptr(5 downto 0) <= s_answerptr(5 downto 0) + 1;
|
568 |
|
|
|
569 |
|
|
when ST_SENDDESC =>
|
570 |
|
|
-- Respond to IN with descriptor
|
571 |
|
|
if T_IN = '0' then
|
572 |
|
|
-- Aborted IN transaction; wait for retry
|
573 |
|
|
s_state <= ST_WAITIN;
|
574 |
|
|
elsif T_TXRDY = '1' then
|
575 |
|
|
-- Need next data byte
|
576 |
|
|
if (s_answerptr(5 downto 0) = 0) or
|
577 |
|
|
(s_answerptr = unsigned(C_DSCLEN)) or
|
578 |
|
|
(s_answerptr = s_answerlen) then
|
579 |
|
|
-- Just sent the last byte of the packet
|
580 |
|
|
s_state <= ST_DONESEND;
|
581 |
|
|
else
|
582 |
|
|
s_answerptr(5 downto 0) <= s_answerptr(5 downto 0) + 1;
|
583 |
|
|
end if;
|
584 |
|
|
end if;
|
585 |
|
|
|
586 |
|
|
when ST_DONESEND =>
|
587 |
|
|
-- Done sending packet; wait until IN transaction completes.
|
588 |
|
|
-- Note: s_answerptr contains the number of bytes sent so-far,
|
589 |
|
|
-- unless this is a multiple of 64, in which case s_answerptr
|
590 |
|
|
-- contains 64 less than the number of bytes sent; and unless
|
591 |
|
|
-- the last packet sent was an empty end-of-transfer packet,
|
592 |
|
|
-- in which case s_answerptr contains 1 more than the number
|
593 |
|
|
-- of bytes sent.
|
594 |
|
|
if T_FIN = '1' then
|
595 |
|
|
-- Host acknowledged transaction.
|
596 |
|
|
if s_answerptr(5 downto 0) = 0 then
|
597 |
|
|
-- The last sent packet was a full sized packet.
|
598 |
|
|
-- If s_answerptr + 64 = s_answerlen, the transfer
|
599 |
|
|
-- is now complete; otherwise the host will expect
|
600 |
|
|
-- more data. In either case, we go back to WAITIN.
|
601 |
|
|
-- This can't go wrong because WAITIN also listens
|
602 |
|
|
-- for the next SETUP and handles it properly.
|
603 |
|
|
s_state <= ST_WAITIN;
|
604 |
|
|
else
|
605 |
|
|
-- The last sent packet was not full sized;
|
606 |
|
|
-- it was either empty or reached the end of
|
607 |
|
|
-- the descriptor. In either case, the transfer
|
608 |
|
|
-- is now complete.
|
609 |
|
|
s_state <= ST_IDLE;
|
610 |
|
|
end if;
|
611 |
|
|
s_answerptr <= s_answerptr + 64;
|
612 |
|
|
elsif T_IN = '0' then
|
613 |
|
|
-- Transaction failed; wait for retry.
|
614 |
|
|
s_state <= ST_WAITIN;
|
615 |
|
|
end if;
|
616 |
|
|
|
617 |
|
|
end case;
|
618 |
|
|
|
619 |
|
|
end if;
|
620 |
|
|
|
621 |
|
|
end process;
|
622 |
|
|
|
623 |
|
|
end architecture usb_control_arch;
|