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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [examples/] [usb-fpga-2.13/] [2.13d/] [memfifo/] [MemFifo.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   memfifo -- implementation of EZ-USB slave FIFO's (input and output) a FIFO using the DDR3 SDRAM for ZTEX USB-FPGA Modules 2.13
3
   Copyright (C) 2009-2014 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
import java.io.*;
20
import java.util.*;
21
import java.text.*;
22
 
23
import ch.ntb.usb.*;
24
 
25
import ztex.*;
26
 
27
// *****************************************************************************
28
// ******* ParameterException **************************************************
29
// *****************************************************************************
30
// Exception the prints a help message
31
class ParameterException extends Exception {
32
    public final static String helpMsg = new String (
33
                "Parameters:\n"+
34
                "    -d <number>       Device Number (default: 0)\n" +
35
                "    -f                Force uploads\n" +
36
                "    -p                Print bus info\n" +
37
                "    -h                This help" );
38
 
39
    public ParameterException (String msg) {
40
        super( msg + "\n" + helpMsg );
41
    }
42
}
43
 
44
// *****************************************************************************
45
// ******* USBReader ***********************************************************
46
// *****************************************************************************
47
class UsbReader extends Thread {
48
    private final int bufNum = 8;
49
    public final int bufSize = 512*1024;
50
    public byte[][] buf = new byte[bufNum][];
51
    public int[] bufBytes = new int[bufNum];
52
    private int readCount = -1;
53
    private int getCount = -1;
54
    public boolean terminate = false;
55
    private Ztex1v1 ztex;
56
 
57
    public UsbReader ( Ztex1v1 p_ztex ) {
58
        super ();
59
        ztex = p_ztex;
60
        for (int i=0; i<bufNum; i++) {
61
            buf[i]=new byte[bufSize];
62
        }
63
    }
64
 
65
    public int getBuffer () {
66
        getCount += 1;
67
        while (getCount >= readCount) {
68
            try {
69
                sleep(1);
70
            }
71
            catch ( InterruptedException e) {
72
            }
73
        }
74
        return getCount % bufNum;
75
    }
76
 
77
    public void reset () {
78
        getCount = readCount + 1;
79
    }
80
 
81
    public void run() {
82
        setPriority(MAX_PRIORITY);
83
 
84
// reader loop
85
        while ( !terminate ) {
86
            readCount += 1;
87
 
88
            while ( readCount - bufNum >= getCount ) {
89
                try {
90
                    sleep(1);
91
                }
92
                catch ( InterruptedException e) {
93
                }
94
            }
95
 
96
            int i = readCount % bufNum;
97
            bufBytes[i] = LibusbJava.usb_bulk_read(ztex.handle(), 0x82, buf[i], bufSize, 1000);
98
//          System.out.println("Buffer " + i +": read " + bufBytes[i] + " bytes");
99
        }
100
 
101
    }
102
}
103
 
104
// *****************************************************************************
105
// ******* UsbWriter ***********************************************************
106
// *****************************************************************************
107
class UsbWriter extends Thread {
108
    private final int bufNum = 8;
109
    public final int bufSize = 512*1024;
110
    public byte[][] buf = new byte[bufNum][];
111
    private int writeCount = -1;
112
    private int getCount = -1;
113
    public boolean terminate = false;
114
    private Ztex1v1 ztex;
115
    public int errorBuffferCount = -1;          // buffer count if an error occurred; otherwise -1
116
    public int errorResult = 0;                  // error code or number of bytes if an error occurred
117
 
118
    public UsbWriter ( Ztex1v1 p_ztex ) {
119
        super ();
120
        ztex = p_ztex;
121
        for (int i=0; i<bufNum; i++) {
122
            buf[i]=new byte[bufSize];
123
        }
124
    }
125
 
126
    public int getBuffer () {
127
        getCount += 1;
128
        while (getCount - bufNum >= writeCount) {
129
            try {
130
                sleep(1);
131
            }
132
            catch ( InterruptedException e) {
133
            }
134
        }
135
        return getCount % bufNum;
136
    }
137
 
138
    public void reset () {
139
        getCount = writeCount + 1;
140
        errorBuffferCount = -1;
141
    }
142
 
143
    public void run() {
144
        setPriority(MAX_PRIORITY);
145
 
146
// writer loop
147
        while ( !terminate ) {
148
            writeCount += 1;
149
 
150
            while ( writeCount >= getCount ) {
151
                try {
152
                    sleep(1);
153
                }
154
                catch ( InterruptedException e) {
155
                }
156
            }
157
 
158
            int i = writeCount % bufNum;
159
            int j = LibusbJava.usb_bulk_write(ztex.handle(), 0x06, buf[i], bufSize, 1000);
160
            if ( j != bufSize ) {
161
                errorBuffferCount = writeCount;
162
                errorResult = j;
163
            }
164
//          System.out.println("Buffer " + i +": wrote " + j + " bytes");
165
        }
166
 
167
    }
168
}
169
 
170
// *****************************************************************************
171
// ******* UsbTestWriter *******************************************************
172
// *****************************************************************************
173
class UsbTestWriter extends Thread {
174
    public boolean terminate = false;
175
    private UsbWriter writer;
176
 
177
    public UsbTestWriter ( Ztex1v1 p_ztex ) {
178
        super ();
179
        writer = new UsbWriter( p_ztex );
180
    }
181
 
182
    public boolean error () {
183
        return writer.errorBuffferCount >= 0;
184
    }
185
 
186
    public void run() {
187
        writer.start();
188
 
189
        int k = 0;
190
        int cs = 47;
191
        int sync;
192
        Random random = new Random();
193
        while ( !terminate ) {
194
            byte[] b = writer.buf[writer.getBuffer()];
195
            for ( int i=0; i<writer.bufSize; i++ ) {
196
                int j = k & 15;
197
                sync = ( ((j & 1)==1) || (j==14) ) ? 128 : 0;
198
                if ( j == 15 ) {
199
                    b[i] = (byte) (sync | ((cs & 127) ^ (cs>>7)));
200
                    cs = 47;
201
                }
202
                else {
203
//                  b[i] = (byte) ( (j==0 ? (k>>4) & 127 : random.nextInt(128)) | sync );
204
                    b[i] = (byte) ( (((k>>4)+j) & 127) | sync );
205
                    cs += (b[i] & 255);
206
                }
207
                k=(k+1) & 65535;
208
            }
209
        }
210
 
211
        writer.terminate=true;
212
    }
213
}
214
 
215
 
216
// *****************************************************************************
217
// ******* MemFifo *************************************************************
218
// *****************************************************************************
219
class MemFifo extends Ztex1v1 {
220
 
221
    // constructor
222
    public MemFifo ( ZtexDevice1 pDev ) throws UsbException {
223
        super ( pDev );
224
    }
225
 
226
    // set mode
227
    public void setMode( int i ) throws UsbException {
228
        i = i & 3;
229
        vendorCommand (0x80, "Set test mode", i, 0);
230
    }
231
 
232
    // reset
233
    public void reset( ) throws UsbException {
234
        vendorCommand (0x81, "Reset:", 0, 0);
235
        try {
236
            Thread.sleep(300);
237
        }
238
        catch ( InterruptedException e) {
239
        }
240
        byte[] buf = new byte[4096];
241
        while ( LibusbJava.usb_bulk_read(handle(), 0x82, buf, buf.length, 100) == buf.length ) { };  // empties buffers
242
    }
243
 
244
    // reads data and verifies them. 
245
    // returns true if errors occurred
246
    // rate is data rate in kBytes; <=0 means unlimited
247
    public boolean verify ( UsbReader reader, int iz, int rate ) {
248
        boolean valid = false;
249
        int byte_cnt = 0, sync_cnt = 0, cs = 47, first = 255, prev_first = 255;
250
        boolean memError = false;
251
 
252
        for (int i=0; i<iz; i++) {
253
            if ( i>0 && rate>0 ) {
254
                try {
255
                    Thread.sleep(reader.bufSize/rate);
256
                }
257
                catch ( InterruptedException e) {
258
                }
259
            }
260
            int j = reader.getBuffer();
261
            int bb = reader.bufBytes[j];
262
            byte[] b = reader.buf[j];
263
            int merrors = 0;
264
            int ferrors = 0;
265
            int serrors = 0;
266
            boolean prev_sync = false;
267
 
268
            if ( bb != reader.bufSize ) {
269
                System.out.println("Data read error");
270
                memError = true;
271
                break;
272
            }
273
 
274
/*          for ( int l=0; l<512; l+=1) {
275
                System.out.print("   " + (b[l] & 255) );
276
                if ( (l & 15) == 15 ) System.out.println();
277
            } */
278
 
279
            for (int k=0; k<bb; k++ ) {
280
                byte_cnt++;
281
                sync_cnt++;
282
                if ( (b[k] & 128) == 0 ) sync_cnt=0;
283
                if ( byte_cnt == 1 ) {
284
                    prev_first = first;
285
                    first = b[k] & 255;
286
                }
287
//              if ( k<40 ) System.out.println(k + ": " + (b[k] & 255) + "  " + (b[k] & 127) + "   " + ((cs & 127) ^ (cs>>7)) + "  " + byte_cnt + "  " + sync_cnt +"    " + prev_first + "  " + first );
288
                if ( sync_cnt == 3 ) {
289
                    boolean serror = byte_cnt != 16;
290
                    boolean merror = ( b[k] & 127 ) != ((cs & 127) ^ (cs>>7));
291
                    boolean ferror = prev_sync && ( first != ((prev_first + 1) & 127) );
292
                    prev_sync = byte_cnt == 16;
293
//                  valid = valid || ( !serror && !merror && !ferror );
294
                    valid = valid || ( !serror && !merror );
295
                    if ( valid ) {
296
                        if ( serror ) serrors += 1;
297
                        else if ( merror ) merrors += 1;
298
                        else if ( ferror ) ferrors += 1;
299
                        if ( serror && (serrors <= 2) && (k>=byte_cnt-1) && (k+1<bb)  ) {
300
                            System.out.print( "Sync Error: " + byte_cnt + " at " + k + ": " );
301
                            for ( int l=1; l<=byte_cnt+1; l++ ) System.out.print((b[k-byte_cnt+l] < 0 ? "*" : "" ) + (b[k-byte_cnt+l] & 127) + " ");
302
                        }
303
//                      if ( ferror && (ferrors == 1) && (k+1<bb) ) System.out.println( "Fifo error: " + prev_first + " " + first + " " +  ( b[k+1] & 127 ) + ":    ");
304
                    }
305
                    cs=47;
306
                    byte_cnt = 0;
307
                } else {
308
                    cs += ( b[k] & 255);
309
                }
310
            }
311
 
312
            if ( ! valid ) {
313
                System.out.println("Invalid data");
314
                return true;
315
            }
316
 
317
            System.out.print("Buffer " + i + ": " + serrors + " sync errors,    " + merrors + " memory or transfer errors,  " + ferrors + " FIFO errors    \r");
318
            if ( merrors + ferrors + serrors > (i==0 ? 1 : 0 ) ) { // one error in first buffer may occurs ofter changing mode
319
                System.out.println();
320
                memError = true;
321
            }
322
        }
323
        System.out.println();
324
        return memError;
325
    }
326
 
327
// ******* main ****************************************************************
328
    public static void main (String args[]) {
329
 
330
        int devNum = 0;
331
        boolean force = false;
332
 
333
        try {
334
// init USB stuff
335
            LibusbJava.usb_init();
336
 
337
// scan the USB bus
338
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
339
            if ( bus.numberOfDevices() <= 0) {
340
                System.err.println("No devices found");
341
                System.exit(0);
342
            }
343
 
344
// scan the command line arguments
345
            for (int i=0; i<args.length; i++ ) {
346
                if ( args[i].equals("-d") ) {
347
                    i++;
348
                    try {
349
                        if (i>=args.length) throw new Exception();
350
                        devNum = Integer.parseInt( args[i] );
351
                    }
352
                    catch (Exception e) {
353
                        throw new ParameterException("Device number expected after -d");
354
                    }
355
                }
356
                else if ( args[i].equals("-f") ) {
357
                    force = true;
358
                }
359
                else if ( args[i].equals("-p") ) {
360
                    bus.printBus(System.out);
361
                    System.exit(0);
362
                }
363
                else if ( args[i].equals("-p") ) {
364
                    bus.printBus(System.out);
365
                    System.exit(0);
366
                }
367
                else if ( args[i].equals("-h") ) {
368
                        System.err.println(ParameterException.helpMsg);
369
                        System.exit(0);
370
                }
371
                else throw new ParameterException("Invalid Parameter: "+args[i]);
372
            }
373
 
374
            String errStr = "";
375
 
376
// create the main class            
377
            MemFifo ztex = new MemFifo ( bus.device(devNum) );
378
 
379
// upload the firmware if necessary
380
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("memfifo for UFM 2.13") ) {
381
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "memfifo.ihx", force ) + " ms");
382
                force = true;
383
            }
384
 
385
// upload the bitstream if necessary
386
            if ( force || ! ztex.getFpgaConfiguration() ) {
387
                System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/memfifo.runs/impl_1/memfifo.bit" , force, -1 ) + " ms");
388
            }
389
 
390
// claim tnterface
391
            ztex.trySetConfiguration ( 1 );
392
            ztex.claimInterface ( 0 );
393
 
394
// reset FIFO's 
395
            if ( !force ) {
396
                System.out.println("Resetting  FIFO's");
397
                ztex.reset();
398
            }
399
 
400
// PKTEND test
401
/*          {
402
                byte[] buf = new byte[65536];
403
                int i = LibusbJava.usb_bulk_write(ztex.handle(), 0x06, buf, buf.length, 1000);
404
//              System.out.println("PKTEND test: wrote "+i+" bytes");
405
                // number of read bytes is usually less than number written bytes because DRAM FIFO is usually never completely emptied in order to avoid small transactions
406
                i=LibusbJava.usb_bulk_read(ztex.handle(), 0x82, buf, buf.length, 1000);
407
                //
408
                int j = i & 511;
409
                System.out.println("PKTEND test: read "+i+" bytes, last paket: " + (j == 0 ? 512 : j) + " bytes" );
410
            } */
411
 
412
// start traffic reader
413
            UsbReader reader = new UsbReader( ztex );
414
            reader.start();
415
 
416
// Mode 1: 48 MByte/s Test data generator: used for speed test
417
            ztex.setMode(1);
418
            System.out.println("48 MByte/s test data generator: ");
419
            long t0 = new Date().getTime();
420
            ztex.verify(reader, 2000, 0);
421
            System.out.println("Read data rate: " + Math.round(reader.bufSize*2000.0/((new Date().getTime()-t0)*100.0)*0.1) + " MByte/s");
422
 
423
 
424
// Mode 2: 12 MByte/s Test data generator: tests flow control
425
            ztex.setMode(2);
426
            System.out.println("12 MByte/s test data generator: ");
427
            ztex.verify(reader, 2000, 0);
428
 
429
// PKTEND test
430
            {
431
                byte[] buf = new byte[65536];
432
                int i;
433
                while ( (i = LibusbJava.usb_bulk_write(ztex.handle(), 0x06, buf, buf.length, 1000)) == buf.length ) { }
434
                int j = i & 511;
435
                System.out.println("PKTEND test: last paket size: " + (j == 0 ? 512 : j) + " bytes" );
436
            }
437
 
438
// Mode 0: write+read mode
439
            ztex.reset();
440
            UsbTestWriter writer = new UsbTestWriter( ztex );
441
            writer.start();
442
            reader.reset();
443
 
444
            System.out.println("USB write + read mode: speed test");
445
            t0 = new Date().getTime();
446
            ztex.verify(reader, 1000, 0);
447
            System.out.println("Read data rate: " + Math.round(reader.bufSize*1000.0/((new Date().getTime()-t0)*100.0)*0.1) + " MByte/s");
448
 
449
            System.out.println("USB write + read mode: 5 MByte/s read rate");
450
            ztex.verify(reader, 1000, 5000);
451
 
452
            if ( writer.error() ) System.out.println("Write errors occured");
453
 
454
// Terminating threads
455
            writer.terminate=true;      // stop the writer
456
            reader.terminate=true;      // stop the reader
457
/*          for (int i=0; i<10000 &&  writer.isAlive() && reader.isAlive(); i++ ) {
458
                if ( ( i % 1000 ) == 999 ) System.out.print(".");
459
                try {
460
                    Thread.sleep(1);
461
                }
462
                catch ( InterruptedException e) {
463
                }
464
            }
465
            System.out.println();
466
 
467
// releases interface
468
            ztex.releaseInterface( 0 );  */
469
 
470
        }
471
        catch (Exception e) {
472
            System.out.println("Error: "+e.getLocalizedMessage() );
473
        }
474
 
475
   }
476
 
477
}

powered by: WebSVN 2.1.0

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