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.2/] [flashbench/] [FlashBench.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   flashbench -- Flash memory benchmark for ZTEX USB-FPGA Module 1.2
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
                "    -s <number>       Number of sectors to be tested, -1 means all (default: 10000)\n" +
35
                "    -f                Force uploads\n" +
36
                "    -p                Print bus info\n" +
37
                "    -w                Enable certain workarounds which may be required for vmware + windows\n"+
38
                "    -h                This help" );
39
 
40
    public ParameterException (String msg) {
41
        super( msg + "\n" + helpMsg );
42
    }
43
}
44
 
45
// *****************************************************************************
46
// ******* Test0 ***************************************************************
47
// *****************************************************************************
48
class FlashBench extends Ztex1v1 {
49
 
50
// ******* FlashBench **********************************************************
51
// constructor
52
    public FlashBench ( ZtexDevice1 pDev ) throws UsbException {
53
        super ( pDev );
54
    }
55
 
56
// ******* testRW **************************************************************
57
// measures read + write performance
58
    public double testRW ( int num ) throws UsbException, InvalidFirmwareException, CapabilityException {
59
        int secNum = 2048 / flashSectorSize();
60
        byte[] buf1 = new byte[flashSectorSize() * secNum];
61
        byte[] buf2 = new byte[flashSectorSize() * secNum];
62
        int errors = 0;
63
 
64
        long t0 = new Date().getTime();
65
 
66
        for ( int i=0; i<num; i+=secNum ) {
67
            int l = Math.min(num-i,secNum);
68
            int j=(int) Math.round(65535*Math.random());
69
            for (int k=0; k<flashSectorSize()*l; k++) {
70
                buf1[k] = (byte) (j & 255);
71
                j+=57;
72
            }
73
 
74
            System.out.print("Sector " + (i+l) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
75
            flashWriteSector(i,l,buf1);
76
            flashReadSector(i,l,buf2);
77
 
78
            int diffs=flashSectorSize()*l;
79
            for (int k=0; k<flashSectorSize()*l; k++)
80
                if ( buf1[k] == buf2[k] )
81
                    diffs -= 1;
82
            if ( diffs!=0 /*&& errors==0 */) {
83
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
84
            }
85
            if ( diffs!=0 )
86
                errors+=1;
87
        }
88
        System.out.println("testRW: " + errors +" errors detected");
89
 
90
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
91
    }
92
 
93
// ******* testW **************************************************************
94
// measures write performance
95
    public double testW ( int num, int seed ) throws UsbException, InvalidFirmwareException, CapabilityException {
96
        int secNum = 2048 / flashSectorSize();
97
        byte[] buf = new byte[flashSectorSize() * secNum];
98
        long t0 = new Date().getTime();
99
        for ( int i=0; i<num; i+=secNum ) {
100
            int j = Math.min(num-i,secNum);
101
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
102
            for (int k=0; k<flashSectorSize()*j; k++) {
103
                buf[k] = (byte) (seed & 255);
104
                seed+=79;
105
            }
106
            flashWriteSector(i,j,buf);
107
        }
108
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
109
    }
110
 
111
// ******* testR **************************************************************
112
// measures read performance
113
    public double testR ( int num, int seed ) throws UsbException, InvalidFirmwareException, CapabilityException {
114
        int secNum = 2048 / flashSectorSize();
115
        byte[] buf = new byte[flashSectorSize() * secNum];
116
        int errors = 0;
117
        long t0 = new Date().getTime();
118
        for ( int i=0; i<num; i+=secNum ) {
119
            int j = Math.min(num-i,secNum);
120
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
121
            flashReadSector(i,j,buf);
122
            int diffs = flashSectorSize()*j;
123
            for (int k=0; k<flashSectorSize()*j; k++) {
124
                if ( buf[k] == (byte) (seed & 255) )
125
                    diffs-=1;
126
                seed+=79;
127
            }
128
            if ( diffs!=0 && errors==0 ) {
129
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
130
            }
131
            if ( diffs!=0 )
132
                errors+=1;
133
        }
134
        System.out.println("testR: " + errors +" errors detected");
135
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
136
    }
137
 
138
// ******* main ****************************************************************
139
    public static void main (String args[]) {
140
 
141
        int devNum = 0;
142
        boolean force = false;
143
        boolean workarounds = false;
144
        int sectors = 10000;
145
 
146
        try {
147
// init USB stuff
148
            LibusbJava.usb_init();
149
 
150
// scan the USB bus
151
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
152
            if ( bus.numberOfDevices() <= 0) {
153
                System.err.println("No devices found");
154
                System.exit(0);
155
            }
156
 
157
// scan the command line arguments
158
            for (int i=0; i<args.length; i++ ) {
159
                if ( args[i].equals("-d") ) {
160
                    i++;
161
                    try {
162
                        if (i>=args.length) throw new Exception();
163
                        devNum = Integer.parseInt( args[i] );
164
                    }
165
                    catch (Exception e) {
166
                        throw new ParameterException("Device number expected after -d");
167
                    }
168
                }
169
                if ( args[i].equals("-s") ) {
170
                    i++;
171
                    try {
172
                        if (i>=args.length) throw new Exception();
173
                        sectors = Integer.parseInt( args[i] );
174
                    }
175
                    catch (Exception e) {
176
                        throw new ParameterException("Number of sectors expected after -s");
177
                    }
178
                }
179
                else if ( args[i].equals("-f") ) {
180
                    force = true;
181
                }
182
                else if ( args[i].equals("-p") ) {
183
                    bus.printBus(System.out);
184
                    System.exit(0);
185
                }
186
                else if ( args[i].equals("-p") ) {
187
                    bus.printBus(System.out);
188
                    System.exit(0);
189
                }
190
                else if ( args[i].equals("-w") ) {
191
                    workarounds = true;
192
                }
193
                else if ( args[i].equals("-h") ) {
194
                        System.err.println(ParameterException.helpMsg);
195
                        System.exit(0);
196
                }
197
                else throw new ParameterException("Invalid Parameter: "+args[i]);
198
            }
199
 
200
 
201
// create the main class            
202
            FlashBench ztex = new FlashBench ( bus.device(devNum) );
203
            ztex.certainWorkarounds = workarounds;
204
 
205
// upload the firmware if necessary
206
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("flashbench for UFM 1.2") ) {
207
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "flashbench.ihx", force ) + " ms");
208
            }
209
 
210
// print some information
211
            System.out.println("Capabilities: " + ztex.capabilityInfo(", "));
212
            System.out.println("Enabled: " + ztex.flashEnabled());
213
            System.out.println("Size: " + ztex.flashSize()+" Bytes");
214
//          ztex.printMmcState();
215
 
216
            if ( sectors<1 || sectors>ztex.flashSectors() ) sectors = ztex.flashSectors();
217
 
218
            System.out.println("Read + Write Performance: " + ztex.testRW(sectors) + "kb/s      \n");
219
            int seed = (int) Math.round(65535*Math.random());
220
            System.out.println("Write Performance: " + ztex.testW(sectors, seed) + "kb/s      ");
221
            System.out.println("Read Performance: " + ztex.testR(sectors, seed) + "kb/s     \n");
222
        }
223
        catch (Exception e) {
224
            System.out.println("Error: "+e.getLocalizedMessage() );
225
        }
226
   }
227
 
228
}

powered by: WebSVN 2.1.0

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