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/] [tb_USBLITE_Core.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rehnmaak
--
2
--    opb_usblite - opb_uartlite replacement
3
--
4
--    opb_usblite is using components from Rudolf Usselmann see
5
--    http://www.opencores.org/cores/usb_phy/
6
--    and Joris van Rantwijk see http://www.xs4all.nl/~rjoris/fpga/usb.html
7
--
8
--    Copyright (C) 2010 Ake Rehnman
9
--
10
--    This program is free software: you can redistribute it and/or modify
11
--    it under the terms of the GNU General Public License as published by
12
--    the Free Software Foundation, either version 3 of the License, or
13
--    (at your option) any later version.
14
--
15
--    This program is distributed in the hope that it will be useful,
16
--    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
--    GNU General Public License for more details.
19
--
20
--    You should have received a copy of the GNU General Public License
21
--    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
--
23
 
24
library IEEE;
25
use IEEE.std_logic_1164.all;
26
use IEEE.numeric_std.all;
27
use STD.TEXTIO.all;
28
use IEEE.STD_LOGIC_TEXTIO.all;
29
 
30
 
31
entity tb_USBLITE_Core is
32
end entity tb_USBLITE_Core;
33
 
34
library unisim;
35
use unisim.all;
36
 
37
architecture akre of tb_USBLITE_Core is
38
 
39
  component usb_phy is
40
    port (
41
      clk : in std_logic;
42
      rst : in std_logic;
43
      phy_tx_mode : in std_logic;
44
      usb_rst : out std_logic;
45
 
46
                -- Transciever Interface
47
                  txdp : out std_logic;
48
                  txdn : out std_logic;
49
                  txoe : out std_logic;
50
                  rxd : in std_logic;
51
                  rxdp : in std_logic;
52
                  rxdn : in std_logic;
53
 
54
                -- UTMI Interface
55
                  DataOut_i : in std_logic_vector (7 downto 0);
56
                  TxValid_i : in std_logic;
57
                  TxReady_o : out std_logic;
58
                  RxValid_o : out std_logic;
59
                  RxActive_o : out std_logic;
60
                  RxError_o : out std_logic;
61
                  DataIn_o : out std_logic_vector (7 downto 0);
62
                  LineState_o : out std_logic_vector (1 downto 0)
63
    );
64
  end component usb_phy;
65
 
66
  component OPB_USBLITE_Core is
67
  generic (
68
    C_PHYMODE :       std_logic := '1';
69
    C_VENDORID :      std_logic_vector(15 downto 0) := X"1234";
70
    C_PRODUCTID :     std_logic_vector(15 downto 0) := X"5678";
71
    C_VERSIONBCD :    std_logic_vector(15 downto 0) := X"0200";
72
    C_SELFPOWERED :   boolean := false;
73
    C_RXBUFSIZE_BITS: integer range 7 to 12 := 10;
74
    C_TXBUFSIZE_BITS: integer range 7 to 12 := 10
75
    );
76
  port (
77
    Clk   : in std_logic;
78
    Reset : in std_logic;
79
    Usb_Clk : in std_logic;
80
    -- OPB signals
81
    OPB_CS : in std_logic;
82
    OPB_ABus : in std_logic_vector(0 to 1);
83
    OPB_RNW  : in std_logic;
84
    OPB_DBus : in std_logic_vector(7 downto 0);
85
    SIn_xferAck : out std_logic;
86
    SIn_DBus    : out std_logic_vector(7 downto 0);
87
    Interrupt : out std_logic;
88
    -- USB signals
89
                txdp : out std_logic;
90
                txdn : out std_logic;
91
                txoe : out std_logic;
92
                rxd : in std_logic;
93
                rxdp : in std_logic;
94
                rxdn : in std_logic
95
  );
96
  end component OPB_USBLITE_Core;
97
 
98
  signal clk : std_logic;
99
  signal usbclk : std_logic;
100
  signal reset : std_logic;
101
  signal rxdp : std_logic;
102
  signal rxdn : std_logic;
103
  signal rxd : std_logic;
104
  signal txdp : std_logic;
105
  signal txdn : std_logic;
106
  signal txoe : std_logic;
107
 
108
  signal usbtxdata : std_logic_vector (7 downto 0);
109
  signal usbtxvalid : std_logic;
110
  signal usbtxready : std_logic;
111
  signal usbrxvalid : std_logic;
112
  signal usbrxactive : std_logic;
113
  signal usbrxerror : std_logic;
114
  signal usbrxdata : std_logic_vector (7 downto 0);
115
  signal usblinestate : std_logic_vector (1 downto 0);
116
 
117
  signal Bus2IP_CS : std_logic := '0';
118
  signal Bus2IP_CE : std_logic := '0';
119
  signal Bus2IP_Addr : std_logic_vector(0 to 31) := X"00000000";
120
  signal Bus2IP_RNW : std_logic := '0';
121
  signal Bus2IP_Data : std_logic_vector(0 to 31);
122
  signal Bus2IP_BE : std_logic_vector(3 downto 0) := "0000";
123
  signal IP2Bus_Ack : std_logic := '0';
124
  signal IP2Bus_Data : std_logic_vector(0 to 31) := X"00000000";
125
  signal Interrupt : std_logic := '0';
126
 
127
  signal resetn : std_logic;
128
  signal usbrxdata_r : std_logic_vector(7 downto 0);
129
 
130
--  type txmemarray is array (0 to 2048) of std_logic_vector(7 downto 0);
131
  type txmemarray is array (0 to 255) of std_logic_vector(7 downto 0);
132
  shared variable txmem : txmemarray;
133
 
134
--  type bufferarray is array (0 to 16384) of std_logic_vector(7 downto 0);
135
  type bufferarray is array (0 to 255) of std_logic_vector(7 downto 0);
136
  shared variable buffer1 : bufferarray;
137
  shared variable buffer0 : bufferarray;
138
  shared variable buffer1_last : integer;
139
  shared variable len : integer;
140
  shared variable setup_pid : std_logic;
141
  shared variable error_cnt : integer;
142
 
143
  constant USBF_T_PID_OUT : std_logic_vector(3 downto 0):="0001";
144
  constant USBF_T_PID_IN : std_logic_vector(3 downto 0):="1001";
145
  constant USBF_T_PID_SOF : std_logic_vector(3 downto 0):="0101";
146
  constant USBF_T_PID_SETUP : std_logic_vector(3 downto 0):="1101";
147
  constant USBF_T_PID_DATA0 : std_logic_vector(3 downto 0):="0011";
148
  constant USBF_T_PID_DATA1 : std_logic_vector(3 downto 0):="1011";
149
  constant USBF_T_PID_DATA2 : std_logic_vector(3 downto 0):="0111";
150
  constant USBF_T_PID_MDATA : std_logic_vector(3 downto 0):="1111";
151
  constant USBF_T_PID_ACK : std_logic_vector(3 downto 0):="0010";
152
  constant USBF_T_PID_NACK : std_logic_vector(3 downto 0):="1010";
153
  constant USBF_T_PID_STALL : std_logic_vector(3 downto 0):="1110";
154
  constant USBF_T_PID_NYET : std_logic_vector(3 downto 0):="0110";
155
  constant USBF_T_PID_PRE : std_logic_vector(3 downto 0):="1100";
156
  constant USBF_T_PID_ERR : std_logic_vector(3 downto 0):="1100";
157
  constant USBF_T_PID_SPLIT : std_logic_vector(3 downto 0):="1000";
158
  constant USBF_T_PID_PING : std_logic_vector(3 downto 0):="0100";
159
  constant USBF_T_PID_RES : std_logic_vector(3 downto 0):="0000";
160
 
161
  constant  GET_STATUS : std_logic_vector(7 downto 0) := X"00";
162
        constant        CLEAR_FEATURE   : std_logic_vector(7 downto 0) :=        X"01";
163
        constant        SET_FEATURE     : std_logic_vector(7 downto 0) :=        X"03";
164
        constant        SET_ADDRESS     : std_logic_vector(7 downto 0) :=        X"05";
165
        constant        GET_DESCRIPTOR  : std_logic_vector(7 downto 0) :=        X"06";
166
        constant        SET_DESCRIPTOR  : std_logic_vector(7 downto 0) :=        X"07";
167
        constant        GET_CONFIG      : std_logic_vector(7 downto 0) :=        X"08";
168
        constant        SET_CONFIG      : std_logic_vector(7 downto 0) :=        X"09";
169
        constant        GET_INTERFACE   : std_logic_vector(7 downto 0) :=        X"0a";
170
        constant        SET_INTERFACE   : std_logic_vector(7 downto 0) :=        X"0b";
171
        constant        SYNCH_FRAME     : std_logic_vector(7 downto 0) :=        X"0c";
172
 
173
  procedure utmi_recv_pack (variable size : inout integer) is
174
  begin
175
    size := 0;
176
    while (usbrxactive /= '1') loop
177
      wait until rising_edge(usbclk);
178
    end loop;
179
    while (usbrxactive= '1') loop
180
        while (usbrxvalid /= '1' and usbrxactive = '1') loop
181
              wait until rising_edge(usbclk);
182
        end loop;
183
            if (usbrxvalid = '1' and usbrxactive = '1') then
184
                          txmem(size) := usbrxdata;
185
                          size := size + 1;
186
            end if;
187
            wait until rising_edge(usbclk);
188
    end loop;
189
  end procedure utmi_recv_pack;
190
 
191
  procedure utmi_send_pack (constant size : integer; signal usbtxdata: out std_logic_vector; signal usbtxvalid: out std_logic) is
192
    variable n : integer;
193
  begin
194
    for n in 0 to size-1 loop
195
      wait until rising_edge(usbclk);
196
            usbtxvalid <= '1';
197
      usbtxdata <= txmem(n);
198
      while (usbtxready/='1') loop
199
        wait until rising_edge(usbclk);
200
      end loop;
201
    end loop;
202
    wait until rising_edge(usbclk);
203
    usbtxvalid <= '0';
204
  end procedure utmi_send_pack;
205
 
206
  function crc5 (crc_in:std_logic_vector; din:std_logic_vector) return std_logic_vector is
207
    variable crc5x : std_logic_vector (4 downto 0);
208
  begin
209
    crc5x(0) :=  din(10) xor din(9) xor din(6) xor din(5) xor din(3) xor din(0) xor crc_in(0) xor crc_in(3) xor crc_in(4);
210
    crc5x(1) := din(10) xor din(7) xor din(6) xor din(4) xor din(1) xor crc_in(0) xor crc_in(1) xor crc_in(4);
211
    crc5x(2) := din(10) xor din(9) xor din(8) xor din(7) xor din(6) xor din(3) xor din(2) xor din(0) xor crc_in(0) xor crc_in(1) xor crc_in(2) xor crc_in(3) xor crc_in(4);
212
    crc5x(3) := din(10) xor din(9) xor din(8) xor din(7) xor din(4) xor din(3) xor din(1) xor crc_in(1) xor crc_in(2) xor crc_in(3) xor crc_in(4);
213
    crc5x(4) := din(10) xor din(9) xor din(8) xor din(5) xor din(4) xor din(2) xor crc_in(2) xor crc_in(3) xor crc_in(4);
214
    return crc5x;
215
  end function crc5;
216
 
217
  function crc16 (crc_in: std_logic_vector; din:std_logic_vector) return std_logic_vector is
218
    variable crc_out : std_logic_vector (15 downto 0);
219
  begin
220
                crc_out(0) :=    din(7) xor din(6) xor din(5) xor din(4) xor din(3) xor
221
                din(2) xor din(1) xor din(0) xor crc_in(8) xor crc_in(9) xor
222
                crc_in(10) xor crc_in(11) xor crc_in(12) xor crc_in(13) xor
223
                crc_in(14) xor crc_in(15);
224
                crc_out(1) :=   din(7) xor din(6) xor din(5) xor din(4) xor din(3) xor din(2) xor
225
                din(1) xor crc_in(9) xor crc_in(10) xor crc_in(11) xor
226
                crc_in(12) xor crc_in(13) xor crc_in(14) xor crc_in(15);
227
                crc_out(2) :=   din(1) xor din(0) xor crc_in(8) xor crc_in(9);
228
                crc_out(3) :=   din(2) xor din(1) xor crc_in(9) xor crc_in(10);
229
                crc_out(4) :=   din(3) xor din(2) xor crc_in(10) xor crc_in(11);
230
                crc_out(5) :=   din(4) xor din(3) xor crc_in(11) xor crc_in(12);
231
                crc_out(6) :=   din(5) xor din(4) xor crc_in(12) xor crc_in(13);
232
                crc_out(7) :=   din(6) xor din(5) xor crc_in(13) xor crc_in(14);
233
                crc_out(8) :=   din(7) xor din(6) xor crc_in(0) xor crc_in(14) xor crc_in(15);
234
                crc_out(9) :=   din(7) xor crc_in(1) xor crc_in(15);
235
                crc_out(10) :=  crc_in(2);
236
                crc_out(11) :=  crc_in(3);
237
                crc_out(12) :=  crc_in(4);
238
                crc_out(13) :=  crc_in(5);
239
                crc_out(14) :=  crc_in(6);
240
                crc_out(15) :=  din(7) xor din(6) xor din(5) xor din(4) xor din(3) xor din(2) xor
241
                din(1) xor din(0) xor crc_in(7) xor crc_in(8) xor crc_in(9) xor
242
                crc_in(10) xor crc_in(11) xor crc_in(12) xor crc_in(13) xor
243
                crc_in(14) xor crc_in(15);
244
                return crc_out;
245
        end function crc16;
246
 
247
        procedure recv_packet(variable pid: inout std_logic_vector(3 downto 0); variable size: inout integer) is
248
    variable del,n : integer;
249
    variable crc16r : std_logic_vector(15 downto 0);
250
    variable x,y : std_logic_vector(7 downto 0);
251
    variable s : LINE;
252
        begin
253
          crc16r := X"ffff";
254
    utmi_recv_pack(size);
255
    for n in 1 to size-3 loop
256
        y := txmem(n);
257
            x(7) := y(0);
258
            x(6) := y(1);
259
            x(5) := y(2);
260
            x(4) := y(3);
261
            x(3) := y(4);
262
            x(2) := y(5);
263
            x(1) := y(6);
264
            x(0) := y(7);
265
            crc16r := crc16(crc16r, x);
266
    end loop;
267
    n := size-2;
268
 
269
    y := crc16r(15 downto 8);
270
    x(7) := y(0);
271
    x(6) := y(1);
272
    x(5) := y(2);
273
    x(4) := y(3);
274
    x(3) := y(4);
275
    x(2) := y(5);
276
    x(1) := y(6);
277
    x(0) := y(7);
278
    crc16r(15 downto 8) := not(x);
279
 
280
    y := crc16r(7 downto 0);
281
    x(7) := y(0);
282
    x(6) := y(1);
283
    x(5) := y(2);
284
    x(4) := y(3);
285
    x(3) := y(4);
286
    x(2) := y(5);
287
    x(1) := y(6);
288
    x(0) := y(7);
289
    crc16r(7 downto 0) := not(x);
290
 
291
    if (crc16r /= txmem(n)&txmem(n+1)) then
292
      --$display("ERROR: CRC Mismatch: Expected: %h, Got: %h%h (%t)",crc16r, txmem[n], txmem[n+1], $time);
293
      WRITE(s,string'("ERROR: CRC Mismatch got:"));
294
      HWRITE(s,crc16r);
295
      WRITE(s,string'(" expected:"));
296
      HWRITE(s,txmem(n)&txmem(n+1));
297
      report s.all;
298
    end if;
299
 
300
    for n in 0 to size-3 loop
301
            buffer1(buffer1_last+n) := txmem(n+1);
302
          end loop;
303
    n := size-3;
304
 
305
    buffer1_last := buffer1_last+n;
306
 
307
    -- Check PID
308
    x := txmem(0);
309
 
310
    if (x(7 downto 4) /= not(x(3 downto 0))) then
311
      --$display("ERROR: Pid Checksum mismatch: Top: %h Bottom: %h (%t)",x[7:4], x[3:0], $time);
312
      WRITE(s,string'("ERROR: Pid Checksum mismatch: Top: "));
313
      HWRITE(s,x(7 downto 4));
314
      WRITE(s,string'(" Bottom:"));
315
      HWRITE(s,x(3 downto 0));
316
      report s.all;
317
    end if;
318
 
319
    pid := x(3 downto 0);
320
    size:= size-3;
321
 
322
  end procedure recv_packet;
323
 
324
  procedure send_token (constant fa:std_logic_vector(7 downto 0); constant ep:std_logic_vector(3 downto 0); constant pid:std_logic_vector(3 downto 0)) is
325
    variable tmp_data:std_logic_vector(15 downto 0);
326
    variable x,y:std_logic_vector(10 downto 0);
327
    variable len:integer;
328
  begin
329
    tmp_data := fa(6 downto 0)&ep&"00000";
330
    if (pid=USBF_T_PID_ACK)     then
331
      len := 1;
332
    else
333
        len := 3;
334
    end if;
335
    y := fa(6 downto 0)&ep;
336
    x(10) := y(4);
337
    x(9) := y(5);
338
    x(8) := y(6);
339
    x(7) := y(7);
340
    x(6) := y(8);
341
    x(5) := y(9);
342
    x(4) := y(10);
343
    x(3) := y(0);
344
    x(2) := y(1);
345
    x(1) := y(2);
346
    x(0) := y(3);
347
    y(4 downto 0)  := crc5("11111", x);
348
    tmp_data(4 downto 0)  := not(y(4 downto 0));
349
    tmp_data(15 downto 5) := x;
350
    txmem(0) := (not(pid)&pid);  -- PID
351
    txmem(1) := (       tmp_data(8)&tmp_data(9)&tmp_data(10)&tmp_data(11)&tmp_data(12)&tmp_data(13)&tmp_data(14)&tmp_data(15));
352
    txmem(2) := (       tmp_data(0)&tmp_data(1)&tmp_data(2)&tmp_data(3)&tmp_data(4)&tmp_data(5)&tmp_data(6)&tmp_data(7));
353
    utmi_send_pack(len,usbtxdata,usbtxvalid);
354
  end procedure send_token;
355
 
356
  procedure send_data (constant pid:std_logic_vector(3 downto 0); constant len:integer; constant mode:integer) is
357
    variable n : integer;
358
    variable crc16r : std_logic_vector(15 downto 0);
359
    variable x,y : std_logic_vector(7 downto 0);
360
  begin
361
    txmem(0) := not(pid)&pid;    -- PID
362
    crc16r := X"ffff";
363
    for n in 0 to len-1 loop
364
            if(mode=1)  then
365
              y := buffer1(buffer1_last+n);
366
            else
367
              y := std_logic_vector(to_unsigned(n,8));
368
            end if;
369
 
370
--          x(7 downto 0) := y(0 to 7);
371
            x(7) := y(0);
372
            x(6) := y(1);
373
            x(5) := y(2);
374
            x(4) := y(3);
375
            x(3) := y(4);
376
            x(2) := y(5);
377
            x(1) := y(6);
378
            x(0) := y(7);
379
            txmem(n+1) := y;
380
            crc16r := crc16(crc16r, x);
381
    end loop;
382
 
383
    buffer1_last := buffer1_last + len - 1;
384
    y := crc16r(15 downto 8);
385
--    x(7 downto 0) := y(0 to 7);
386
    x(7) := y(0);
387
    x(6) := y(1);
388
    x(5) := y(2);
389
    x(4) := y(3);
390
    x(3) := y(4);
391
    x(2) := y(5);
392
    x(1) := y(6);
393
    x(0) := y(7);
394
    txmem(len+1) := not(x);
395
 
396
    y := crc16r(7 downto 0);
397
--    x(7 downto 0) := y(0 to 7);
398
    x(7) := y(0);
399
    x(6) := y(1);
400
    x(5) := y(2);
401
    x(4) := y(3);
402
    x(3) := y(4);
403
    x(2) := y(5);
404
    x(1) := y(6);
405
    x(0) := y(7);
406
    txmem(len+2) := not(x);
407
 
408
    utmi_send_pack(len+3,usbtxdata,usbtxvalid);
409
  end procedure send_data;
410
 
411
  procedure data_in (constant fa:std_logic_vector(7 downto 0); constant pl_size:integer) is
412
    variable rlen : integer;
413
    variable pid : std_logic_vector(3 downto 0);
414
    variable expected_pid : std_logic_vector(3 downto 0);
415
    variable s : LINE;
416
  begin
417
          buffer1_last := 0;
418
                send_token(     fa,                 -- Function Address
419
                        X"00",          -- Logical Endpoint Number
420
                        USBF_T_PID_IN   -- PID
421
                );
422
          recv_packet(pid,rlen);
423
          if (setup_pid='1') then
424
            expected_pid := X"b"; -- DATA 1
425
          else
426
            expected_pid := X"3";  -- DATA 0
427
    end if;
428
          if (pid /= expected_pid) then
429
                --$display("ERROR: Data IN PID mismatch. Expected: %h, Got: %h (%t)",           expect_pid, pid, $time);
430
                report "ERROR: Data IN PID mismatch.";
431
          error_cnt := error_cnt + 1;
432
          end if;
433
 
434
          setup_pid := not(setup_pid);
435
          if (rlen /= pl_size) then
436
            report "ERROR: Data IN Size mismatch.";
437
                        --$display("ERROR: Data IN Size mismatch. Expected: %d, Got: %d (%t)",          pl_size, rlen, $time);
438
                  error_cnt := error_cnt + 1;
439
          end if;
440
 
441
    DEALLOCATE(s);
442
    WRITE(s,string'("RCV bytes: "));
443
    WRITE(s,rlen);
444
    report s.all;
445
          for n in 0 to rlen-1 loop
446
--                $display("RCV Data[%0d]: %h",n,buffer1[n]);
447
      DEALLOCATE(s);
448
      WRITE(s,string'("RCV Data["));
449
      WRITE(s,n);
450
      WRITE(s,string'("]="));
451
      HWRITE(s,buffer1(n));
452
      report s.all;
453
                end loop;
454
 
455
--        repeat(5)     @(posedge clk);
456
    wait for 1 us;
457
 
458
          send_token(   fa,             -- Function Address
459
                        X"00",                -- Logical Endpoint Number
460
                        USBF_T_PID_ACK  -- PID
461
                );
462
 
463
--        repeat(5)     @(posedge clk);
464
    wait for 1 us;
465
  end procedure data_in;
466
 
467
  procedure data_out(fa:std_logic_vector(7 downto 0); pl_size:integer) is
468
    variable len : integer;
469
  begin
470
          send_token(   fa,             -- Function Address
471
                        X"00",          -- Logical Endpoint Number
472
                        USBF_T_PID_OUT  -- PID
473
                        );
474
    wait until rising_edge(usbclk);
475
 
476
          if (setup_pid='0') then
477
                send_data(USBF_T_PID_DATA0, pl_size, 1);
478
          else
479
            send_data(USBF_T_PID_DATA1, pl_size, 1);
480
    end if;
481
 
482
          setup_pid := not(setup_pid);
483
 
484
          -- Wait for ACK
485
 
486
          utmi_recv_pack(len);
487
 
488
          if(txmem(0) /= X"d2") then
489
                        --$display("ERROR: ACK mismatch. Expected: %h, Got: %h (%t)",8'hd2, txmem[0], $time);
490
      report "ERROR: SETUP: ACK mismatch";
491
                  error_cnt := error_cnt + 1;
492
          end if;
493
 
494
          if(len /= 1) then
495
                  --$display("ERROR: SETUP: Length mismatch. Expected: %h, Got: %h (%t)",8'h1, len, $time);
496
      report "ERROR: SETUP: Length mismatch";
497
                  error_cnt := error_cnt + 1;
498
          end if;
499
 
500
                -- repeat(5)    @(posedge clk);
501
          wait for 1 us;
502
  end procedure data_out;
503
 
504
  procedure send_setup (constant fa:std_logic_vector(7 downto 0);
505
                        constant req_type:std_logic_vector(7 downto 0);
506
                        constant request:std_logic_vector(7 downto 0);
507
                        constant wValue:std_logic_vector(15 downto 0);
508
                        constant wIndex:std_logic_vector(15 downto 0);
509
                        constant wLength:std_logic_vector(15 downto 0)) is
510
  begin
511
    send_token(fa,X"0",USBF_T_PID_SETUP);
512
    wait for 1 us;
513
    buffer1(0) := req_type;
514
    buffer1(1) := request;
515
    buffer1(3) := wValue(15 downto 8);
516
    buffer1(2) := wValue(7 downto 0);
517
    buffer1(5) := wIndex(15 downto 8);
518
    buffer1(4) := wIndex(7 downto 0);
519
    buffer1(7) := wLength(15 downto 8);
520
    buffer1(6) := wLength(7 downto 0);
521
    buffer1_last := 0;
522
    send_data(USBF_T_PID_DATA0, 8, 1);
523
    utmi_recv_pack(len);
524
    if (txmem(0) /= x"d2") then
525
      --$display("ERROR: SETUP: ACK mismatch. Expected: %h, Got: %h (%t)",      8'hd2, txmem[0], $time);
526
      report "ERROR: SETUP: ACK mismatch";
527
      error_cnt := error_cnt + 1;
528
    end if;
529
    if (len /= 1) then
530
            --$display("ERROR: SETUP: Length mismatch. Expected: %h, Got: %h (%t)", 8'h1, len, $time);    
531
      report "ERROR: SETUP: Length mismatch. len="&integer'image(len);
532
      error_cnt := error_cnt + 1;
533
    end if;
534
 
535
    wait until rising_edge(usbclk);
536
    setup_pid := '1';
537
    wait until rising_edge(usbclk);
538
 
539
  end procedure send_setup;
540
 
541
  procedure send_sof(constant frmn:integer) is
542
    variable frmnv : std_logic_vector(10 downto 0);
543
    variable tmp_data : std_logic_vector(15 downto 0);
544
    variable x,y : std_logic_vector(10 downto 0);
545
  begin
546
    frmnv := std_logic_vector(to_unsigned(frmn,11));
547
    y := frmnv;
548
                x(10) := y(0);
549
                x(9) := y(1);
550
                x(8) := y(2);
551
                x(7) := y(3);
552
                x(6) := y(4);
553
                x(5) := y(5);
554
                x(4) := y(6);
555
                x(3) := y(7);
556
                x(2) := y(8);
557
                x(1) := y(9);
558
                x(0) := y(10);
559
 
560
                tmp_data(15 downto 5) := x;
561
                y(4 downto 0)  := crc5( X"1F", x );
562
                tmp_data(4 downto 0)  := not(y(4 downto 0));
563
                txmem(0) := not(USBF_T_PID_SOF)&USBF_T_PID_SOF;  -- PID
564
    txmem(1) :=         tmp_data(8)&tmp_data(9)&tmp_data(10)&tmp_data(11)&
565
                  tmp_data(12)&tmp_data(13)&tmp_data(14)&tmp_data(15);
566
    txmem(2) :=         tmp_data(0)&tmp_data(1)&tmp_data(2)&tmp_data(3)&
567
                              tmp_data(4)&tmp_data(5)&tmp_data(6)&tmp_data(7);
568
    txmem(1) :=         frmnv(7 downto 0);
569
    txmem(2) :=         tmp_data(0)&tmp_data(1)&tmp_data(2)&tmp_data(3)&
570
                              tmp_data(4)& frmnv(10 downto 8);
571
    utmi_send_pack(3,usbtxdata,usbtxvalid);
572
  end procedure send_sof;
573
 
574
 
575
  procedure buswrite(constant adr : in std_logic_vector(31 downto 0);
576
                     constant data : in std_logic_vector(31 downto 0);
577
                     signal Bus2IP_Clk : in std_logic;
578
                     signal Bus2IP_Addr : out std_logic_vector(0 to 31);
579
                     signal Bus2IP_Data : out std_logic_vector(0 to 31);
580
                     signal Bus2IP_BE : out std_logic_vector(0 to 3);
581
                     signal Bus2IP_CS : out std_logic;
582
                     signal Bus2IP_CE : out std_logic;
583
                     signal Bus2IP_RNW : out std_logic;
584
                     signal IP2Bus_Data : in std_logic_vector(0 to 31);
585
                     signal IP2Bus_Ack : in std_logic) is
586
  begin
587
        wait until (Bus2IP_Clk='1' and Bus2IP_Clk'event);
588
        wait for 1 ns;
589
        Bus2IP_Addr <= adr;
590
        Bus2IP_Data <= data;
591
        Bus2IP_RNW <= '0';
592
        Bus2IP_CE <= '1';
593
        Bus2IP_CS <= '1';
594
        Bus2IP_BE <= "1111";
595
        wait until (Bus2IP_Clk'event and Bus2IP_Clk='1' and IP2Bus_Ack='1');
596
        wait for 1 ns;
597
        Bus2IP_Addr <= (others=>'0');
598
        Bus2IP_Data <= (others=>'0');
599
        Bus2IP_RNW <= '0';
600
        Bus2IP_CE <= '0';
601
        Bus2IP_CS <= '0';
602
        Bus2IP_BE <= "0000";
603
  end buswrite;
604
 
605
  procedure busread (constant adr : in std_logic_vector(31 downto 0);
606
                     variable data : out std_logic_vector(31 downto 0);
607
                     signal Bus2IP_Clk : in std_logic;
608
                     signal Bus2IP_Addr : out std_logic_vector(0 to 31);
609
                     signal Bus2IP_Data : out std_logic_vector(0 to 31);
610
                     signal Bus2IP_BE : out std_logic_vector(0 to 3);
611
                     signal Bus2IP_CS : out std_logic;
612
                     signal Bus2IP_CE : out std_logic;
613
                     signal Bus2IP_RNW : out std_logic;
614
                     signal IP2Bus_Data : in std_logic_vector(0 to 31);
615
                     signal IP2Bus_Ack : in std_logic) is
616
  begin
617
        wait until (Bus2IP_Clk='1' and Bus2IP_Clk'event);
618
        wait for 1 ns;
619
        Bus2IP_Addr <= adr;
620
        Bus2IP_Data <= (others=>'0');
621
        Bus2IP_RNW <= '1';
622
        Bus2IP_CS <= '1';
623
        Bus2IP_CE <= '1';
624
        Bus2IP_BE <= "1111";
625
        wait until (Bus2IP_Clk'event and Bus2IP_Clk='1' and IP2Bus_Ack='1');
626
        data := IP2Bus_Data;
627
        wait for 1 ns;
628
        Bus2IP_Addr <= (others=>'0');
629
        Bus2IP_Data <= (others=>'0');
630
        Bus2IP_RNW <= '0';
631
        Bus2IP_CS <= '0';
632
        Bus2IP_CE <= '0';
633
        Bus2IP_BE <= "0000";
634
  end busread;
635
 
636
  procedure uartread (variable d : out std_logic_vector (7 downto 0)) is
637
    variable tmp : std_logic_vector(31 downto 0);
638
--    variable ss : LINE;    
639
  begin
640
    loop
641
       busread(X"00000002", tmp, clk, Bus2IP_Addr, Bus2IP_Data, Bus2IP_BE, Bus2IP_CS, Bus2IP_CE, Bus2IP_RNW, IP2Bus_Data, IP2Bus_Ack);
642
       exit when ((tmp and X"00000001") /= X"00000000");
643
    end loop;
644
--      DEALLOCATE(ss);
645
--      WRITE(ss,string'("uartstatus="));
646
--      HWRITE(ss,tmp);
647
--      report ss.all;    
648
    busread(X"00000000", tmp, clk, Bus2IP_Addr, Bus2IP_Data, Bus2IP_BE, Bus2IP_CS, Bus2IP_CE, Bus2IP_RNW, IP2Bus_Data, IP2Bus_Ack);
649
    d := tmp(7 downto 0);
650
  end procedure uartread;
651
 
652
  procedure uartwrite (variable d : std_logic_vector (7 downto 0)) is
653
    variable tmp : std_logic_vector(31 downto 0);
654
  begin
655
    loop
656
       busread(X"00000002", tmp, clk, Bus2IP_Addr, Bus2IP_Data, Bus2IP_BE, Bus2IP_CS, Bus2IP_CE, Bus2IP_RNW, IP2Bus_Data, IP2Bus_Ack);
657
       exit when ((tmp and X"00000004") /= X"00000000");
658
    end loop;
659
    tmp := X"00000000";
660
    tmp(7 downto 0) := d;
661
    buswrite(X"00000001", tmp, clk, Bus2IP_Addr, Bus2IP_Data, Bus2IP_BE, Bus2IP_CS, Bus2IP_CE, Bus2IP_RNW, IP2Bus_Data, IP2Bus_Ack);
662
 
663
  end procedure uartwrite;
664
 
665
  shared variable nn : integer;
666
  shared variable pid : std_logic_vector(3 downto 0);
667
  shared variable ss : LINE;
668
  shared variable dd : std_logic_vector(7 downto 0);
669
  shared variable rlen : integer;
670
begin
671
 
672
 
673
  resetn <= not(reset);
674
  rxd <= rxdp;
675
 
676
  usb_phy_inst : usb_phy
677
      port map (
678
      clk => usbclk,
679
      rst => resetn,
680
      phy_tx_mode => '1',
681
      usb_rst => open,
682
                  txdp => rxdp,
683
                  txdn => rxdn,
684
                  txoe => open,
685
                  rxd => txdp,
686
                  rxdp => txdp,
687
                  rxdn => txdn,
688
 
689
                -- UTMI Interface
690
                  DataOut_i => usbtxdata,
691
                  TxValid_i => usbtxvalid,
692
                  TxReady_o => usbtxready,
693
                  RxValid_o => usbrxvalid,
694
                  RxActive_o => usbrxactive,
695
                  RxError_o => usbrxerror,
696
                  DataIn_o => usbrxdata,
697
                  LineState_o => usblinestate
698
    );
699
 
700
 
701
  OPB_USBLITE_Core_inst : OPB_USBLITE_Core
702
  port map (
703
    Clk   => clk,
704
    Reset => reset,
705
    Usb_Clk => usbclk,
706
    OPB_CS => Bus2IP_CS,
707
    OPB_ABus => Bus2IP_Addr(30 to 31),
708
    OPB_RNW  => Bus2IP_RNW,
709
    OPB_DBus => Bus2IP_Data(24 to 31),
710
    SIn_xferAck => IP2Bus_Ack,
711
    SIn_DBus => IP2Bus_Data(24 to 31),
712
    Interrupt => Interrupt,
713
                txdp => txdp,
714
                txdn => txdn,
715
                txoe => txoe,
716
                rxd => rxd,
717
                rxdp => rxdp,
718
                rxdn => rxdn
719
  );
720
 
721
 
722
  process
723
  begin
724
    clk <= '0';
725
    wait for 6.66666ns;
726
    clk <= '1';
727
    wait for 6.66666ns;
728
  end process;
729
 
730
  process
731
  begin
732
    usbclk <= '0';
733
    wait for 10.41666ns;
734
    usbclk <= '1';
735
    wait for 10.41666ns;
736
  end process;
737
 
738
  process(usbclk)
739
  begin
740
    if (usbclk'event and usbclk='1') then
741
      if (usbrxvalid='1') then
742
        usbrxdata_r <= usbrxdata;
743
      end if;
744
    end if;
745
  end process;
746
 
747
  process
748
  begin
749
    wait for 100ns;
750
    reset <= '1';
751
    wait for 100ns;
752
    reset <= '0';
753
    wait for 100 us;
754
 
755
 report "Setting Address ...";
756
    wait for 1 us;
757
    send_setup( X"00",          -- Function Address
758
                X"00",                      -- Request Type
759
                SET_ADDRESS,            -- Request
760
                X"0012",                    -- wValue
761
                X"0000",                          -- wIndex
762
                X"0000"                     -- wLength
763
                );
764
 
765
-- Status OK            
766
                data_in(        X"00",          -- Function Address
767
 
768
          );
769
 
770
 report("Getting DEVICE descriptor ...");
771
    send_setup( X"12",          -- Function Address
772
                X"80",                      -- Request Type
773
                GET_DESCRIPTOR,       -- Request
774
                X"0100",                    -- wValue
775
                X"0000",                    -- wIndex
776
                X"0008"                     -- wLength
777
                );
778
 
779
    data_in(    X"12",          -- Function Address
780
                8                               -- Expected payload size
781
                );
782
 
783
-- Status OK
784
    data_out(   X"12",          -- Function Address
785
 
786
          );
787
 
788
 report("Getting whole DEVICE descriptor ...");
789
    send_setup( X"12",          -- Function Address
790
                X"80",                      -- Request Type
791
                GET_DESCRIPTOR,       -- Request
792
                X"0100",                    -- wValue
793
                X"0000",                    -- wIndex
794
                X"0012"                     -- wLength
795
                );
796
 
797
    data_in(    X"12",          -- Function Address
798
                18                      -- Expected payload size
799
                );
800
 
801
-- Status OK
802
    data_out(   X"12",          -- Function Address
803
 
804
          );
805
 
806
report "Getting CONFIGURATION descriptor ...";
807
    send_setup( X"12",          -- Function Address
808
                X"80",                      -- Request Type
809
                GET_DESCRIPTOR,       -- Request
810
                X"0200",                    -- wValue
811
                X"0000",                    -- wIndex
812
                X"0008"               -- wLength
813
                );
814
 
815
    data_in(    X"12",      -- Function Address
816
                8                                 -- Expected payload size
817
          );
818
 
819
-- Status OK
820
    data_out(   X"12",            -- Function Address
821
 
822
          );
823
 
824
report "Getting whole CONFIGURATION descriptor ...";
825
                send_setup(     X"12",          -- Function Address
826
                X"80",                      -- Request Type
827
                GET_DESCRIPTOR,       -- Request
828
                X"0200",                    -- wValue
829
                X"0000",              -- wIndex
830
                X"0043"                     -- wLength
831
                );
832
 
833
    data_in(    X"12",            -- Function Address
834
                64                          -- Expected payload size
835
          );
836
 
837
-- Status OK
838
    data_out(   X"12",            -- Function Address
839
 
840
          );
841
 
842
    data_in(    X"12",            -- Function Address
843
                3                           -- Expected payload size
844
          );
845
 
846
-- Status OK
847
    data_out(   X"12",            -- Function Address
848
 
849
          );
850
 
851
report "Set configuration 1...";
852
                send_setup(     X"12",          -- Function Address
853
                X"00",                      -- Request Type
854
                SET_CONFIG,           -- Request
855
                X"0001",                    -- wValue
856
                X"0000",              -- wIndex
857
                X"0000"                     -- wLength
858
                );
859
 
860
    data_in(    X"12",            -- Function Address
861
 
862
          );
863
 
864
    wait for 1 us;
865
 
866
report "EP1 OUT...";
867
          for nn in 0 to 63 loop
868
                  buffer1(nn) := std_logic_vector(to_unsigned(nn+32,8));
869
    end loop;
870
          buffer1_last := 0;
871
          pid := "0000";
872
 
873
          send_sof(0);    -- Send SOF
874
          wait until rising_edge(usbclk);
875
 
876
          send_token(   X"12",  -- Function Address
877
                        X"1",             -- Logical Endpoint Number
878
                        USBF_T_PID_OUT    -- PID
879
                        );
880
 
881
          wait until rising_edge(usbclk);
882
 
883
          if (pid="0000")       then
884
            send_data(USBF_T_PID_DATA0, 64, 1);
885
          else
886
            send_data(USBF_T_PID_DATA1, 64, 1);
887
          end if;
888
 
889
          pid := not(pid);
890
 
891
    -- Wait for ACK
892
          utmi_recv_pack(len);
893
 
894
                for nn in 0 to 63 loop
895
                  uartread(dd);
896
      DEALLOCATE(ss);
897
      WRITE(ss,string'("uartread="));
898
      HWRITE(ss,dd);
899
      report ss.all;
900
                end loop;
901
 
902
    wait for 1 us;
903
 
904
report "EP1 IN...";
905
 
906
                for nn in 0 to 63 loop
907
            dd := std_logic_vector(to_unsigned(nn,8));
908
      DEALLOCATE(ss);
909
      WRITE(ss,string'("uartwrite="));
910
      HWRITE(ss,dd);
911
      report ss.all;
912
                  uartwrite(dd);
913
                end loop;
914
 
915
 
916
        -- Send Data
917
          send_sof(1);          -- Send SOF
918
          send_token(   X"12",          -- Function Address
919
                        X"1",                       -- Logical Endpoint Number
920
                        USBF_T_PID_IN         -- PID
921
                        );
922
 
923
           recv_packet(pid,rlen);
924
 
925
          for nn in 0 to 63 loop
926
            dd := txmem(nn+1);
927
      DEALLOCATE(ss);
928
      WRITE(ss,string'("usb recv="));
929
      HWRITE(ss,dd);
930
      report ss.all;
931
    end loop;
932
 
933
          send_token(   X"12",          -- Function Address
934
                        X"1",                       -- Logical Endpoint Number
935
                        USBF_T_PID_ACK      -- PID
936
                        );
937
 
938
                wait for 10us;
939
 
940
                reset <= '1';
941
                wait for 1us;
942
                reset <= '0';
943
 
944
    wait;
945
  end process;
946
 
947
 
948
end architecture akre;

powered by: WebSVN 2.1.0

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