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.11/] [1.11a/] [memtest/] [MemTest.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   memtest -- DDR SDRAM FIFO for testing memory on ZTEX USB-FPGA Module 1.11a
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
                "    -c                Counter test pattern\n" +
35
                "    -f                Force uploads\n" +
36
                "    -p                Print bus info\n" +
37
                "    -w                Enable certain workarounds\n"+
38
                "    -h                This help" );
39
 
40
    public ParameterException (String msg) {
41
        super( msg + "\n" + helpMsg );
42
    }
43
}
44
 
45
// *****************************************************************************
46
// ******* USBReader ***********************************************************
47
// *****************************************************************************
48
class UsbReader extends Thread {
49
    private final int bufNum = 8;
50
    public final int bufSize = 512*1024;
51
    public byte[][] buf = new byte[bufNum][];
52
    public int[] bufBytes = new int[bufNum];
53
    private int readCount = -1;
54
    private int getCount = -1;
55
    public boolean terminate = false;
56
    private Ztex1v1 ztex;
57
 
58
    public UsbReader ( Ztex1v1 p_ztex ) {
59
        super ();
60
        ztex = p_ztex;
61
        for (int i=0; i<bufNum; i++) {
62
            buf[i]=new byte[bufSize];
63
        }
64
    }
65
 
66
    public int getBuffer () {
67
        getCount += 1;
68
        while (getCount >= readCount) {
69
            try {
70
                sleep(1);
71
            }
72
            catch ( InterruptedException e) {
73
            }
74
        }
75
        return getCount % bufNum;
76
    }
77
 
78
 
79
    public void run() {
80
        setPriority(MAX_PRIORITY);
81
 
82
// claim interface 0
83
        try {
84
            ztex.trySetConfiguration ( 1 );
85
            ztex.claimInterface ( 0 );
86
        }
87
        catch ( Exception e) {
88
            System.out.println("Error: "+e.getLocalizedMessage() );
89
            System.exit(2);
90
        }
91
 
92
 
93
// reader loop
94
        while ( !terminate ) {
95
            readCount += 1;
96
 
97
            while ( readCount - bufNum >= getCount ) {
98
                try {
99
                    sleep(1);
100
                }
101
                catch ( InterruptedException e) {
102
                }
103
            }
104
 
105
            int i = readCount % bufNum;
106
            bufBytes[i] = LibusbJava.usb_bulk_read(ztex.handle(), 0x82, buf[i], bufSize, 1000);
107
//          System.out.println("Buffer " + i +": read " + bufBytes[i] + " bytes");
108
        }
109
 
110
// release interface 0
111
            ztex.releaseInterface( 0 );
112
 
113
    }
114
}
115
 
116
// *****************************************************************************
117
// ******* Test0 ***************************************************************
118
// *****************************************************************************
119
class MemTest extends Ztex1v1 {
120
 
121
// ******* MemTest **************************************************************
122
// constructor
123
    public MemTest ( ZtexDevice1 pDev ) throws UsbException {
124
        super ( pDev );
125
    }
126
 
127
// ******* main ****************************************************************
128
    public static void main (String args[]) {
129
 
130
        int devNum = 0;
131
        boolean force = false;
132
        boolean workarounds = false;
133
        boolean genMode = true;
134
 
135
        try {
136
// init USB stuff
137
            LibusbJava.usb_init();
138
 
139
// scan the USB bus
140
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
141
            if ( bus.numberOfDevices() <= 0) {
142
                System.err.println("No devices found");
143
                System.exit(0);
144
            }
145
 
146
// scan the command line arguments
147
            for (int i=0; i<args.length; i++ ) {
148
                if ( args[i].equals("-d") ) {
149
                    i++;
150
                    try {
151
                        if (i>=args.length) throw new Exception();
152
                        devNum = Integer.parseInt( args[i] );
153
                    }
154
                    catch (Exception e) {
155
                        throw new ParameterException("Device number expected after -d");
156
                    }
157
                }
158
                else if ( args[i].equals("-f") ) {
159
                    force = true;
160
                }
161
                else if ( args[i].equals("-c") ) {
162
                    genMode = false;
163
                }
164
                else if ( args[i].equals("-p") ) {
165
                    bus.printBus(System.out);
166
                    System.exit(0);
167
                }
168
                else if ( args[i].equals("-p") ) {
169
                    bus.printBus(System.out);
170
                    System.exit(0);
171
                }
172
                else if ( args[i].equals("-w") ) {
173
                    workarounds = true;
174
                }
175
                else if ( args[i].equals("-h") ) {
176
                        System.err.println(ParameterException.helpMsg);
177
                        System.exit(0);
178
                }
179
                else throw new ParameterException("Invalid Parameter: "+args[i]);
180
            }
181
 
182
 
183
// create the main class            
184
            MemTest ztex = new MemTest ( bus.device(devNum) );
185
            ztex.certainWorkarounds = workarounds;
186
 
187
// upload the firmware if necessary
188
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("memtest example for UFM 1.11") ) {
189
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "memtest.ihx", force ) + " ms");
190
                force = true;
191
            }
192
 
193
            ztex.vendorCommand (0x60, "Set test pattern", (genMode ? 1 : 0), 0);
194
 
195
// upload the bitstream if necessary
196
            if ( force || ! ztex.getFpgaConfiguration() ) {
197
                System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/memtest.bit" , force ) + " ms");
198
            }
199
 
200
// read the traffic
201
            UsbReader reader = new UsbReader( ztex );
202
            reader.start();
203
 
204
// test     
205
            int vcurrent = -1;
206
            int prevErrors = 0;
207
 
208
            for (int i=0; i<1921; i++) {
209
                int j = reader.getBuffer();
210
                int bb = reader.bufBytes[j];
211
                byte[] b = reader.buf[j];
212
                int current = vcurrent+1;
213
                int lastwi = 1;
214
                int aerrors = 0;
215
                int ferrors = 0;
216
                int errors = 0;
217
 
218
                for (int k=1; k<bb; k+=2 ) {
219
                    if ( (b[k] & 0x80) == 0 ) {
220
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
221
                        if ( lastwi == 0 ) {
222
                            aerrors+=1;
223
                            System.out.println("Alignment error: 0 at " + i + ":" + (k-1) );
224
                        }
225
                        lastwi = 0;
226
                    }
227
                    else {
228
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
229
 
230
                        if ( genMode ) {
231
                            if ( (vcurrent & 1) == 1)
232
                                vcurrent = (vcurrent >> 1) | (1 << 29);
233
                            else
234
                                vcurrent = (vcurrent >> 1);
235
                        }
236
                        else {
237
                            vcurrent += 1;
238
                        }
239
 
240
                        if ( lastwi == 1 ) {
241
                            aerrors+=1;
242
                            System.out.println("Alignment error: 1 at " + i + ":" + (k-1) );
243
                        }
244
                        else if ( vcurrent != current ) {
245
                            if ( (i != 0) && ( k != 3) ) {
246
                                System.out.println("Error: 0b" + Integer.toBinaryString(vcurrent) + " expected at " + i + ":" + (k-3) + " but " );
247
                                System.out.println("       0b" + Integer.toBinaryString(current) + " found");
248
                                errors+=1;
249
                                prevErrors+=1;
250
                            }
251
                            vcurrent = current;
252
                        }
253
                        else {
254
//                          if ( prevErrors > 0 ) System.out.println("       0b" + Integer.toBinaryString(current) );
255
                            if ( prevErrors == 1 )
256
                                ferrors +=1;
257
                            prevErrors = 0;
258
                        }
259
 
260
                        lastwi = 1;
261
//                      System.out.println(current);
262
                    }
263
//                  System.out.println( (((b[k] & 0x7f) << 8) | (b[k-1] & 0xff)) + "  " + Integer.toBinaryString(b[k] & 255) + " " + Integer.toBinaryString(b[k-1] & 255));
264
                }
265
                System.out.print("Buffer " + i + ": " + (errors-ferrors) + " errors,  " + ferrors + " FIFO errors,  " + aerrors + " alignment errors    \r");
266
                if ( errors != 0 | aerrors != 0 ) System.out.println();
267
            }
268
            System.out.println();
269
 
270
// stop the reader
271
            reader.terminate=true;
272
 
273
        }
274
        catch (Exception e) {
275
            System.out.println("Error: "+e.getLocalizedMessage() );
276
        }
277
   }
278
 
279
}

powered by: WebSVN 2.1.0

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