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

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [examples/] [usb-xmega-1.0/] [flashbench/] [FlashBench.java] - Blame information for rev 5

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

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

powered by: WebSVN 2.1.0

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