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

Subversion Repositories opb_usblite

[/] [opb_usblite/] [trunk/] [pcores/] [opb_usblite_v1_00_a/] [hdl/] [vhdl/] [usb_packet.vhdl] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rehnmaak
--
2
--  USB 2.0 Packet-level logic.
3
--
4
--  This entity hides the details of the UTMI interface and handles
5
--  computation and verificaton of CRCs.
6
--
7
--  The low-level interface signals are named PHY_xxx and may be
8
--  connected to an UTMI compliant USB PHY, such as the SMSC GT3200.
9
--
10
--  The application interface signals are named P_xxx.
11
--  The receiving side of the interface operates as follows:
12
--    * At the start of an incoming packet, RXACT is set high.
13
--    * When a new byte arrives, RXRDY is asserted and the byte is put
14
--      on RXDAT. These signals are valid for only one clock cycle; the
15
--      application must accept them immediately.
16
--    * The first byte of a packet is the PID. Subsequent bytes contain
17
--      data and CRC. This entity verifies the CRC, but does not
18
--      discard it from the data stream.
19
--    * Some time after correctly receiving the last byte of a packet,
20
--      RXACT is deasserted; at the same time RXFIN is asserted for one cycle
21
--      to confirm the packet.
22
--    * If a corrupt packet is received, RXACT is deasserted without
23
--      asserting RXFIN.
24
--
25
--  The transmission side of the interface operates as follows:
26
--    * The application starts transmission by setting TXACT to 1 and setting
27
--      TXDAT to the PID value (with correctly mirrored high order bits).
28
--    * The entity asserts TXRDY when it needs the next payload byte.
29
--      On the following clock cycle, the application must then provide the
30
--      next payload byte on TXDAT, or deassert TXACT to indicate the end of
31
--      the packet. The signal on TXDAT must be held stable until the next
32
--      assertion of TXRDY.
33
--    * CRC bytes should not be included in the payload; the entity will
34
--      add them automatically.
35
--    * As part of the high speed handshake, the application may request
36
--      transmission of a continuous chirp K state by asserting CHIRPK.
37
--
38
--  Implementation note:
39
--  Transmission timing is a bit tricky due to the following issues:
40
--    * After the PHY asserts PHY_TXREADY, we must immediately provide
41
--      new data or deassert PHY_TXVALID on the next clock cycle.
42
--    * The PHY may assert PHY_TXREADY during subsequent clock cycles,
43
--      even though the average byte period is more than 40 cycles.
44
--    * We want to register PHY inputs and outputs to ensure valid timing.
45
--
46
--  To satisfy these requirements, we make the application run one byte
47
--  ahead. While keeping the current byte in the output register PHY_DATAOUT,
48
--  the application already provides the following data byte. That way, we
49
--  can respond to PHY_TXREADY immediately in the next cycle, with the
50
--  application following up in the clock cycle after that.
51
--
52
 
53
library ieee;
54
use ieee.std_logic_1164.all, ieee.numeric_std.all;
55
 
56
entity usb_packet is
57
 
58
    port (
59
 
60
        -- 60 MHz UTMI clock.
61
        CLK :           in  std_logic;
62
 
63
        -- Synchronous reset of this entity.
64
        RESET :         in  std_logic;
65
 
66
        -- High to force chirp K transmission.
67
        P_CHIRPK :      in  std_logic;
68
 
69
        -- High while receiving a packet.
70
        P_RXACT :       out std_logic;
71
 
72
        -- Indicates next byte received; data must be read from RXDAT immediately.
73
        P_RXRDY :       out std_logic;
74
 
75
        -- High for one cycle to indicate successful completion of packet.
76
        P_RXFIN :       out std_logic;
77
 
78
        -- Received byte value. Valid if RXRDY is high.
79
        P_RXDAT :       out std_logic_vector(7 downto 0);
80
 
81
        -- High while transmitting a packet.
82
        P_TXACT :       in  std_logic;
83
 
84
        -- Request for next data byte; application must change TXDAT on the next clock cycle.
85
        P_TXRDY :       out std_logic;
86
 
87
        -- Data byte to transmit. Hold stable until next assertion of TXRDY.
88
        P_TXDAT :       in  std_logic_vector(7 downto 0);
89
 
90
        -- Connect to UTMI DataIn signal.
91
        PHY_DATAIN :    in std_logic_vector(7 downto 0);
92
 
93
        -- Connect to UTMI DataOut signal.
94
        PHY_DATAOUT :   out std_logic_vector(7 downto 0);
95
 
96
        -- Connect to UTMI TxValid signal.
97
        PHY_TXVALID :   out std_logic;
98
 
99
        -- Connect to UTMI TxReady signal.
100
        PHY_TXREADY :   in std_logic;
101
 
102
        -- Connect to UTMI RxActive signal.
103
        PHY_RXACTIVE :  in std_logic;
104
 
105
        -- Connect to UTMI RxValid signal.
106
        PHY_RXVALID :   in std_logic;
107
 
108
        -- Connect to UTMI RxError signal.
109
        PHY_RXERROR :   in std_logic );
110
 
111
end entity usb_packet;
112
 
113
architecture usb_packet_arch of usb_packet is
114
 
115
    -- State machine
116
    type t_state is (
117
        ST_NONE, ST_CHIRPK,
118
        ST_RWAIT, ST_RTOKEN, ST_RDATA, ST_RSHAKE,
119
        ST_TSTART, ST_TDATA, ST_TCRC1, ST_TCRC2 );
120
    signal s_state : t_state := ST_NONE;
121
    signal s_txfirst : std_logic := '0';
122
 
123
    -- Registered inputs
124
    signal s_rxactive : std_logic;
125
    signal s_rxvalid : std_logic;
126
    signal s_rxerror : std_logic;
127
    signal s_datain : std_logic_vector(7 downto 0);
128
    signal s_txready : std_logic;
129
 
130
    -- Byte pending for transmission
131
    signal s_dataout : std_logic_vector(7 downto 0);
132
 
133
    -- True if an incoming packet would be valid if it ended now.
134
    signal s_rxgoodpacket : std_logic;
135
 
136
    -- CRC computation
137
    constant crc5_gen : std_logic_vector(4 downto 0) := "00101";
138
    constant crc5_res : std_logic_vector(4 downto 0) := "01100";
139
    constant crc16_gen : std_logic_vector(15 downto 0) := "1000000000000101";
140
    constant crc16_res : std_logic_vector(15 downto 0) := "1000000000001101";
141
    signal crc5_buf : std_logic_vector(4 downto 0);
142
    signal crc16_buf : std_logic_vector(15 downto 0);
143
 
144
    -- Update CRC 5 to account for a new byte
145
    function crc5_upd(
146
        c : in std_logic_vector(4 downto 0);
147
        b : in std_logic_vector(7 downto 0) )
148
        return std_logic_vector
149
    is
150
        variable t : std_logic_vector(4 downto 0);
151
        variable y : std_logic_vector(4 downto 0);
152
    begin
153
        t := (
154
            b(0) xor c(4),
155
            b(1) xor c(3),
156
            b(2) xor c(2),
157
            b(3) xor c(1),
158
            b(4) xor c(0) );
159
        y := (
160
            b(5) xor t(1) xor t(2),
161
            b(6) xor t(0) xor t(1) xor t(4),
162
            b(5) xor b(7) xor t(0) xor t(3) xor t(4),
163
            b(6) xor t(1) xor t(3) xor t(4),
164
            b(7) xor t(0) xor t(2) xor t(3) );
165
        return y;
166
    end function;
167
 
168
    -- Update CRC-16 to account for new byte
169
    function crc16_upd(
170
        c : in std_logic_vector(15 downto 0);
171
        b : in std_logic_vector(7 downto 0) )
172
        return std_logic_vector
173
    is
174
        variable t : std_logic_vector(7 downto 0);
175
        variable y : std_logic_vector(15 downto 0);
176
    begin
177
        t := (
178
            b(0) xor c(15),
179
            b(1) xor c(14),
180
            b(2) xor c(13),
181
            b(3) xor c(12),
182
            b(4) xor c(11),
183
            b(5) xor c(10),
184
            b(6) xor c(9),
185
            b(7) xor c(8) );
186
        y := (
187
            c(7) xor t(0) xor t(1) xor t(2) xor t(3) xor t(4) xor t(5) xor t(6) xor t(7),
188
            c(6), c(5), c(4), c(3), c(2),
189
            c(1) xor t(7),
190
            c(0) xor t(6) xor t(7),
191
            t(5) xor t(6),
192
            t(4) xor t(5),
193
            t(3) xor t(4),
194
            t(2) xor t(3),
195
            t(1) xor t(2),
196
            t(0) xor t(1),
197
            t(1) xor t(2) xor t(3) xor t(4) xor t(5) xor t(6) xor t(7),
198
            t(0) xor t(1) xor t(2) xor t(3) xor t(4) xor t(5) xor t(6) xor t(7) );
199
        return y;
200
    end function;
201
 
202
begin
203
 
204
    -- Assign output signals
205
    P_RXACT <= s_rxactive;
206
    P_RXFIN <= (not s_rxactive) and (not s_rxerror) and s_rxgoodpacket;
207
    P_RXRDY <= s_rxactive and s_rxvalid;
208
    P_RXDAT <= s_datain;
209
 
210
    -- Assert P_TXRDY during ST_TSTART to acknowledge the PID byte,
211
    -- during the first cycle of ST_TDATA to acknowledge the first
212
    -- data byte, and whenever we need a new data byte during ST_TDATA.
213
    P_TXRDY <= '1' when (s_state = ST_TSTART)
214
               else (s_txfirst or s_txready) when (s_state = ST_TDATA)
215
               else '0';
216
 
217
    -- On every rising clock edge
218
    process is
219
        variable v_dataout : std_logic_vector(7 downto 0);
220
        variable v_txvalid : std_logic;
221
        variable v_crc_upd : std_logic;
222
        variable v_crc_data : std_logic_vector(7 downto 0);
223
        variable v_crc5_new : std_logic_vector(4 downto 0);
224
        variable v_crc16_new : std_logic_vector(15 downto 0);
225
    begin
226
        wait until rising_edge(CLK);
227
 
228
        -- Default assignment to temporary variables
229
        v_dataout := s_dataout;
230
        v_txvalid := '0';
231
        v_crc_upd := '0';
232
        v_crc_data := "00000000";
233
        v_crc5_new := "00000";
234
        v_crc16_new := "0000000000000000";
235
 
236
        -- Default assignment to s_txfirst
237
        s_txfirst <= '0';
238
 
239
        -- Register inputs
240
        s_rxactive <= PHY_RXACTIVE;
241
        s_rxvalid <= PHY_RXVALID;
242
        s_rxerror <= PHY_RXERROR;
243
        s_datain <= PHY_DATAIN;
244
        s_txready <= PHY_TXREADY;
245
 
246
        -- State machine
247
        if RESET = '1' then
248
 
249
            -- Reset entity
250
            s_state <= ST_NONE;
251
            s_rxgoodpacket <= '0';
252
 
253
        else
254
 
255
            case s_state is
256
                when ST_NONE =>
257
                    -- Waiting for incoming or outgoing packet
258
 
259
                    -- Initialize CRC buffers
260
                    crc5_buf <= "11111";
261
                    crc16_buf <= "1111111111111111";
262
                    s_rxgoodpacket <= '0';
263
 
264
                    if P_CHIRPK = '1' then
265
                        -- Send continuous chirp K.
266
                        s_state <= ST_CHIRPK;
267
 
268
                    elsif s_rxactive = '1' then
269
                        -- Receiver starting
270
 
271
                        if s_rxerror = '1' then
272
                            -- Receive error at PHY level
273
                            s_state <= ST_RWAIT;
274
                        elsif s_rxvalid = '1' then
275
                            -- Got PID byte
276
                            if s_datain(3 downto 0) = not s_datain(7 downto 4) then
277
                                case s_datain(1 downto 0) is
278
                                    when "01" => -- token packet
279
                                        s_state <= ST_RTOKEN;
280
                                    when "11" => -- data packet
281
                                        s_state <= ST_RDATA;
282
                                    when "10" => -- handshake packet
283
                                        s_state <= ST_RSHAKE;
284
                                        s_rxgoodpacket <= '1';
285
                                    when others => -- PING token or special packet
286
                                        -- If this is a PING token, it will work out fine;
287
                                        -- otherwise it will be flagged as a bad packet
288
                                        -- either here or in usb_transact.
289
                                        s_state <= ST_RTOKEN;
290
                                end case;
291
                            else
292
                                -- Corrupt PID byte
293
                                s_state <= ST_RWAIT;
294
                            end if;
295
                        end if;
296
 
297
                    elsif P_TXACT = '1' then
298
                        -- Transmission starting; put data in output buffer
299
                        v_txvalid := '1';
300
                        v_dataout := P_TXDAT;
301
                        s_state <= ST_TSTART;
302
                    end if;
303
 
304
                when ST_CHIRPK =>
305
                    -- Sending continuous chirp K.
306
                    if P_CHIRPK = '0' then
307
                        s_state <= ST_NONE;
308
                    end if;
309
 
310
                when ST_RTOKEN =>
311
                    -- Receiving a token packet
312
                    if s_rxactive = '0' then
313
                        -- End of packet
314
                        s_rxgoodpacket <= '0';
315
                        s_state <= ST_NONE;
316
                    elsif s_rxerror = '1' then
317
                        -- Error at PHY level
318
                        s_rxgoodpacket <= '0';
319
                        s_state <= ST_RWAIT;
320
                    elsif s_rxvalid = '1' then
321
                        -- Just received a byte; update CRC
322
                        v_crc5_new := crc5_upd(crc5_buf, s_datain);
323
                        crc5_buf   <= v_crc5_new;
324
                        if v_crc5_new = crc5_res then
325
                            s_rxgoodpacket <= '1';
326
                        else
327
                            s_rxgoodpacket <= '0';
328
                        end if;
329
                    end if;
330
 
331
                when ST_RDATA =>
332
                    -- Receiving a data packet
333
                    if s_rxactive = '0' then
334
                        -- End of packet
335
                        s_rxgoodpacket <= '0';
336
                        s_state <= ST_NONE;
337
                    elsif s_rxerror = '1' then
338
                        -- Error at PHY level
339
                        s_rxgoodpacket <= '0';
340
                        s_state <= ST_RWAIT;
341
                    elsif s_rxvalid = '1' then
342
                        -- Just received a byte; update CRC
343
                        v_crc_upd := '1';
344
                        v_crc_data := s_datain;
345
                    end if;
346
 
347
                when ST_RWAIT =>
348
                    -- Wait until the end of the current packet
349
                    if s_rxactive = '0' then
350
                        s_state <= ST_NONE;
351
                    end if;
352
 
353
                when ST_RSHAKE =>
354
                    -- Receiving a handshake packet
355
                    if s_rxactive = '0' then
356
                        -- Got good handshake
357
                        s_rxgoodpacket <= '0';
358
                        s_state <= ST_NONE;
359
                    elsif s_rxerror = '1' or s_rxvalid = '1' then
360
                        -- Error or unexpected data byte in handshake packet
361
                        s_rxgoodpacket <= '0';
362
                        s_state <= ST_RWAIT;
363
                    end if;
364
 
365
                when ST_TSTART =>
366
                    -- Transmission starting;
367
                    -- PHY module sees our PHY_TXVALID signal;
368
                    -- PHY_TXREADY is undefined;
369
                    -- we assert P_TXRDY to acknowledge the PID byte
370
                    v_txvalid := '1';
371
                    -- Check packet type
372
                    case P_TXDAT(1 downto 0) is
373
                        when "11" => -- data packet
374
                            s_state <= ST_TDATA;
375
                            s_txfirst <= '1';
376
                        when "10" => -- handshake packet
377
                            s_state <= ST_RWAIT;
378
                        when others => -- should not happen
379
                    end case;
380
 
381
                when ST_TDATA =>
382
                    -- Sending a data packet
383
                    v_txvalid := '1';
384
                    if (s_txready = '1') or (s_txfirst = '1') then
385
                        -- Need next byte
386
                        if P_TXACT = '0' then
387
                            -- No more data; send first CRC byte
388
                            for i in 0 to 7 loop
389
                                v_dataout(i) := not crc16_buf(15-i);
390
                            end loop;
391
                            s_state <= ST_TCRC1;
392
                        else
393
                            -- Put next byte in output buffer
394
                            v_dataout := P_TXDAT;
395
                            -- And update the CRC
396
                            v_crc_upd := '1';
397
                            v_crc_data := P_TXDAT;
398
                        end if;
399
                    end if;
400
 
401
                when ST_TCRC1 =>
402
                    -- Sending the first CRC byte of a data packet
403
                    v_txvalid := '1';
404
                    if s_txready = '1' then
405
                        -- Just queued the first CRC byte; move to 2nd byte
406
                        for i in 0 to 7 loop
407
                            v_dataout(i) := not crc16_buf(7-i);
408
                        end loop;
409
                        s_state <= ST_TCRC2;
410
                    end if;
411
 
412
                when ST_TCRC2 =>
413
                    -- Sending the second CRC byte of a data packet
414
                    if s_txready = '1' then
415
                        -- Just sent the 2nd CRC byte; end packet
416
                        s_state <= ST_RWAIT;
417
                    else
418
                        -- Last byte is still pending
419
                        v_txvalid := '1';
420
                    end if;
421
 
422
            end case;
423
 
424
        end if;
425
 
426
        -- CRC-16 update
427
        if v_crc_upd = '1' then
428
            v_crc16_new := crc16_upd(crc16_buf, v_crc_data);
429
            crc16_buf   <= v_crc16_new;
430
            if s_state = ST_RDATA and v_crc16_new = crc16_res then
431
                -- If this is the last byte of the packet, it is a valid packet.
432
                s_rxgoodpacket <= '1';
433
            else
434
                s_rxgoodpacket <= '0';
435
            end if;
436
        end if;
437
 
438
        -- Drive data output to PHY
439
        if RESET = '1' then
440
            -- Reset.
441
            PHY_TXVALID <= '0';
442
            PHY_DATAOUT <= "00000000";
443
        elsif s_state = ST_CHIRPK then
444
            -- Continuous chirp-K.
445
            PHY_TXVALID <= P_CHIRPK;
446
            PHY_DATAOUT <= "00000000";
447
        elsif (PHY_TXREADY = '1') or (s_state = ST_NONE and P_TXACT = '1') then
448
            -- Move a data byte from the buffer to the output lines when the PHY
449
            -- accepts the previous byte, and also at the start of a new packet.
450
            PHY_TXVALID <= v_txvalid;
451
            PHY_DATAOUT <= v_dataout;
452
        end if;
453
 
454
        -- Keep pending output byte in register.
455
        s_dataout <= v_dataout;
456
 
457
    end process;
458
 
459
end architecture usb_packet_arch;
460
 

powered by: WebSVN 2.1.0

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