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-1.15/] [1.15b/] [intraffic/] [InTraffic.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   intraffic -- example showing how the EZ-USB FIFO interface is used on ZTEX USB-FPGA Module 1.15b
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
 
22
import ch.ntb.usb.*;
23
 
24
import ztex.*;
25
 
26
// *****************************************************************************
27
// ******* ParameterException **************************************************
28
// *****************************************************************************
29
// Exception the prints a help message
30
class ParameterException extends Exception {
31
    public final static String helpMsg = new String (
32
                "Parameters:\n"+
33
                "    -d <number>       Device Number (default: 0)\n" +
34
                "    -f                Force uploads\n" +
35
                "    -p                Print bus info\n" +
36
                "    -w                Enable certain workarounds\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
// claim interface 0
85
        try {
86
            ztex.trySetConfiguration ( 1 );
87
            ztex.claimInterface ( 0 );
88
        }
89
        catch ( Exception e) {
90
            System.out.println("Error: "+e.getLocalizedMessage() );
91
            System.exit(2);
92
        }
93
 
94
 
95
// reader loop
96
        while ( !terminate ) {
97
            readCount += 1;
98
 
99
            while ( readCount - bufNum >= getCount ) {
100
                try {
101
                    sleep(1);
102
                }
103
                catch ( InterruptedException e) {
104
                }
105
            }
106
 
107
            int i = readCount % bufNum;
108
            bufBytes[i] = LibusbJava.usb_bulk_read(ztex.handle(), 0x82, buf[i], bufSize, 1000);
109
//          System.out.println("Buffer " + i +": read " + bufBytes[i] + " bytes");
110
        }
111
 
112
// release interface 0
113
        ztex.releaseInterface( 0 );
114
 
115
    }
116
}
117
 
118
 
119
// *****************************************************************************
120
// ******* Test0 ***************************************************************
121
// *****************************************************************************
122
class InTraffic extends Ztex1v1 {
123
 
124
// ******* InTraffic **************************************************************
125
// constructor
126
    public InTraffic ( ZtexDevice1 pDev ) throws UsbException {
127
        super ( pDev );
128
    }
129
 
130
// ******* main ****************************************************************
131
    public static void main (String args[]) {
132
 
133
        int devNum = 0;
134
        boolean force = false;
135
        boolean workarounds = false;
136
 
137
        try {
138
// init USB stuff
139
            LibusbJava.usb_init();
140
 
141
// scan the USB bus
142
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
143
            if ( bus.numberOfDevices() <= 0) {
144
                System.err.println("No devices found");
145
                System.exit(0);
146
            }
147
 
148
// scan the command line arguments
149
            for (int i=0; i<args.length; i++ ) {
150
                if ( args[i].equals("-d") ) {
151
                    i++;
152
                    try {
153
                        if (i>=args.length) throw new Exception();
154
                        devNum = Integer.parseInt( args[i] );
155
                    }
156
                    catch (Exception e) {
157
                        throw new ParameterException("Device number expected after -d");
158
                    }
159
                }
160
                else if ( args[i].equals("-f") ) {
161
                    force = true;
162
                }
163
                else if ( args[i].equals("-p") ) {
164
                    bus.printBus(System.out);
165
                    System.exit(0);
166
                }
167
                else if ( args[i].equals("-p") ) {
168
                    bus.printBus(System.out);
169
                    System.exit(0);
170
                }
171
                else if ( args[i].equals("-w") ) {
172
                    workarounds = true;
173
                }
174
                else if ( args[i].equals("-h") ) {
175
                        System.err.println(ParameterException.helpMsg);
176
                        System.exit(0);
177
                }
178
                else throw new ParameterException("Invalid Parameter: "+args[i]);
179
            }
180
 
181
 
182
// create the main class            
183
            InTraffic ztex = new InTraffic ( bus.device(devNum) );
184
            ztex.certainWorkarounds = workarounds;
185
 
186
// upload the firmware if necessary
187
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("intraffic example for UFM 1.15") ) {
188
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "intraffic.ihx", force ) + " ms");
189
                force = true;
190
            }
191
 
192
// upload the bitstream if necessary
193
            if ( force || ! ztex.getFpgaConfiguration() ) {
194
                System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/intraffic.bit" , force ) + " ms");
195
            }
196
 
197
// read the traffic
198
            UsbReader reader = new UsbReader( ztex );
199
            reader.start();
200
 
201
// EZ-USB FIFO test (controlled mode)
202
            ztex.vendorCommand (0x60, "Set test mode", 0, 0);
203
            reader.reset();
204
 
205
            int vcurrent = -1;
206
            for (int i=0; i<1000; i++) {
207
                int j = reader.getBuffer();
208
                int bb = reader.bufBytes[j];
209
                byte[] b = reader.buf[j];
210
                int current = vcurrent+1;
211
                int lastwi = -1;
212
                int aerrors = 0;
213
                int ferrors = 0;
214
                int errors = 0;
215
                int prevErrors = 0;
216
 
217
                for (int k=1; k<bb; k+=2 ) {
218
                    if ( (b[k] & 0x80) == 0 ) {
219
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
220
                        if ( lastwi == 0 ) aerrors+=1;
221
                        if ( lastwi == 0 ) System.out.println("Alignment error: 0 at " + i + ":" + (k-1) );
222
                        lastwi = 0;
223
                    }
224
                    else {
225
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
226
 
227
                        vcurrent += 1;
228
                        if ( vcurrent % 100 == 90 )
229
                            vcurrent += 10;
230
 
231
                        if ( lastwi == 1 ) {
232
                            aerrors+=1;
233
                            System.out.println("Alignment error: 1 at " + i + ":" + (k-1) );
234
                        }
235
                        else if ( vcurrent != current ) {
236
                            if ( (i != 0) && ( k != 3) ) {
237
                                System.out.println("Error: 0b" + Integer.toBinaryString(vcurrent) + " expected at " + i + ":" + (k-3) + " but " );
238
                                System.out.println("       0b" + Integer.toBinaryString(current) + " found");
239
                                errors+=1;
240
                                prevErrors+=1;
241
                            }
242
                            vcurrent = current;
243
                        }
244
                        else {
245
//                          if ( prevErrors > 0 ) System.out.println("       0b" + Integer.toBinaryString(current) );
246
                            if ( prevErrors == 1 )
247
                                ferrors +=1;
248
                            prevErrors = 0;
249
                        }
250
 
251
                        lastwi = 1;
252
//                      System.out.println(current);
253
                    }
254
//                  System.out.println(b[k]+"  " +b[k+1]);
255
                }
256
                System.out.print("Buffer " + i + ": " + (errors-ferrors) + " errors,  " + ferrors + " FIFO errors,  " + aerrors + " alignment errors  \r");
257
            }
258
            System.out.println();
259
 
260
// performance test (continous mode)
261
            ztex.vendorCommand (0x60, "Set test mode", 1, 0);
262
            reader.reset();
263
 
264
            int words = 0;
265
            int intSum = 0;
266
            int intMax = 0;
267
            int intAdj = 0;
268
            int lastwi = -1;
269
            for (int i=0; i<1000; i++) {
270
                int j = reader.getBuffer();
271
                int bb = reader.bufBytes[j];
272
                byte[] b = reader.buf[j];
273
                int current = vcurrent+1;
274
 
275
                for (int k=1; k<bb; k+=2 ) {
276
                    if ( (b[k] & 0x80) == 0 ) {
277
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
278
                        if ( lastwi == 0 ) intAdj -= 1;
279
                        lastwi = 0;
280
                    }
281
                    else {
282
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
283
 
284
                        if ( lastwi == 1 ) {
285
                            intAdj += 1;
286
                        }
287
                        else {
288
                            vcurrent += 1;
289
                            int it = (current - vcurrent)*2 + intAdj;
290
                            if ( it > 0 && words > 0) {
291
                                intSum += it;
292
                                if ( it > intMax )
293
                                    intMax = it;
294
                            }
295
                            words += 2;
296
                            vcurrent = current;
297
                            intAdj = 0;
298
                        }
299
                        lastwi = 1;
300
//                      System.out.println(current);
301
                    }
302
//                  System.out.println(b[k]+"  " +b[k+1]);
303
                }
304
                System.out.print("Buffer " + i + ": " + Math.round(words*6000.0/(words+intSum))/100.0 + "MB/s, max. interrupt: " + Math.round(intMax/150.0)/100 + "ms    \r");
305
            }
306
            System.out.println();
307
 
308
 
309
            reader.terminate=true;
310
 
311
        }
312
        catch (Exception e) {
313
            System.out.println("Error: "+e.getLocalizedMessage() );
314
        }
315
   }
316
 
317
}

powered by: WebSVN 2.1.0

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