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

Subversion Repositories usb_fpga_1_15

[/] [usb_fpga_1_15/] [trunk/] [examples/] [usb-1.0/] [flashbench/] [FlashBench.java] - Diff between revs 3 and 4

Only display areas with differences | Details | Blame | View Log

Rev 3 Rev 4
/*!
/*!
   flashbench -- Flash memory benchmark for ZTEX USB Module 1.0
   flashbench -- Flash memory benchmark for ZTEX USB Module 1.0
   Copyright (C) 2009-2011 ZTEX GmbH.
   Copyright (C) 2009-2014 ZTEX GmbH.
   http://www.ztex.de
   http://www.ztex.de
 
 
   This program is free software; you can redistribute it and/or modify
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3 as
   it under the terms of the GNU General Public License version 3 as
   published by the Free Software Foundation.
   published by the Free Software Foundation.
 
 
   This program is distributed in the hope that it will be useful, but
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
   General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, see http://www.gnu.org/licenses/.
   along with this program; if not, see http://www.gnu.org/licenses/.
!*/
!*/
 
 
import java.io.*;
import java.io.*;
import java.util.*;
import java.util.*;
 
 
import ch.ntb.usb.*;
import ch.ntb.usb.*;
 
 
import ztex.*;
import ztex.*;
 
 
// *****************************************************************************
// *****************************************************************************
// ******* ParameterException **************************************************
// ******* ParameterException **************************************************
// *****************************************************************************
// *****************************************************************************
// Exception the prints a help message
// Exception the prints a help message
class ParameterException extends Exception {
class ParameterException extends Exception {
    public final static String helpMsg = new String (
    public final static String helpMsg = new String (
                "Parameters:\n"+
                "Parameters:\n"+
                "    -d <number>       Device Number (default: 0)\n" +
                "    -d <number>       Device Number (default: 0)\n" +
                "    -s <number>       Number of sectors to be tested, -1 means all (default: 10000)\n" +
                "    -s <number>       Number of sectors to be tested, -1 means all (default: 10000)\n" +
                "    -f                Force uploads\n" +
                "    -f                Force uploads\n" +
                "    -p                Print bus info\n" +
                "    -p                Print bus info\n" +
                "    -w                Enable certain workarounds which may be required for vmware + windows\n"+
                "    -w                Enable certain workarounds which may be required for vmware + windows\n"+
                "    -h                This help" );
                "    -h                This help" );
 
 
    public ParameterException (String msg) {
    public ParameterException (String msg) {
        super( msg + "\n" + helpMsg );
        super( msg + "\n" + helpMsg );
    }
    }
}
}
 
 
// *****************************************************************************
// *****************************************************************************
// ******* Test0 ***************************************************************
// ******* Test0 ***************************************************************
// *****************************************************************************
// *****************************************************************************
class FlashBench extends Ztex1v1 {
class FlashBench extends Ztex1v1 {
 
 
// ******* FlashBench **********************************************************
// ******* FlashBench **********************************************************
// constructor
// constructor
    public FlashBench ( ZtexDevice1 pDev ) throws UsbException {
    public FlashBench ( ZtexDevice1 pDev ) throws UsbException {
        super ( pDev );
        super ( pDev );
    }
    }
 
 
// ******* testRW **************************************************************
// ******* testRW **************************************************************
// measures read + write performance
// measures read + write performance
    public double testRW ( int num ) throws UsbException, InvalidFirmwareException, CapabilityException {
    public double testRW ( int num ) throws UsbException, InvalidFirmwareException, CapabilityException {
        int secNum = 2048 / flashSectorSize();
        int secNum = 2048 / flashSectorSize();
        byte[] buf1 = new byte[flashSectorSize() * secNum];
        byte[] buf1 = new byte[flashSectorSize() * secNum];
        byte[] buf2 = new byte[flashSectorSize() * secNum];
        byte[] buf2 = new byte[flashSectorSize() * secNum];
        int errors = 0;
        int errors = 0;
 
 
        long t0 = new Date().getTime();
        long t0 = new Date().getTime();
 
 
        for ( int i=0; i<num; i+=secNum ) {
        for ( int i=0; i<num; i+=secNum ) {
            int l = Math.min(num-i,secNum);
            int l = Math.min(num-i,secNum);
            int j=(int) Math.round(65535*Math.random());
            int j=(int) Math.round(65535*Math.random());
            for (int k=0; k<flashSectorSize()*l; k++) {
            for (int k=0; k<flashSectorSize()*l; k++) {
                buf1[k] = (byte) (j & 255);
                buf1[k] = (byte) (j & 255);
                j+=57;
                j+=57;
            }
            }
 
 
            System.out.print("Sector " + (i+l) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
            System.out.print("Sector " + (i+l) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
            flashWriteSector(i,l,buf1);
            flashWriteSector(i,l,buf1);
            flashReadSector(i,l,buf2);
            flashReadSector(i,l,buf2);
 
 
            int diffs=flashSectorSize()*l;
            int diffs=flashSectorSize()*l;
            for (int k=0; k<flashSectorSize()*l; k++)
            for (int k=0; k<flashSectorSize()*l; k++)
                if ( buf1[k] == buf2[k] )
                if ( buf1[k] == buf2[k] )
                    diffs -= 1;
                    diffs -= 1;
            if ( diffs!=0 /*&& errors==0 */) {
            if ( diffs!=0 /*&& errors==0 */) {
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
            }
            }
            if ( diffs!=0 )
            if ( diffs!=0 )
                errors+=1;
                errors+=1;
        }
        }
        System.out.println("testRW: " + errors +" errors detected");
        System.out.println("testRW: " + errors +" errors detected");
 
 
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
    }
    }
 
 
// ******* testW **************************************************************
// ******* testW **************************************************************
// measures write performance
// measures write performance
    public double testW ( int num, int seed ) throws UsbException, InvalidFirmwareException, CapabilityException {
    public double testW ( int num, int seed ) throws UsbException, InvalidFirmwareException, CapabilityException {
        int secNum = 2048 / flashSectorSize();
        int secNum = 2048 / flashSectorSize();
        byte[] buf = new byte[flashSectorSize() * secNum];
        byte[] buf = new byte[flashSectorSize() * secNum];
        long t0 = new Date().getTime();
        long t0 = new Date().getTime();
        for ( int i=0; i<num; i+=secNum ) {
        for ( int i=0; i<num; i+=secNum ) {
            int j = Math.min(num-i,secNum);
            int j = Math.min(num-i,secNum);
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
            for (int k=0; k<flashSectorSize()*j; k++) {
            for (int k=0; k<flashSectorSize()*j; k++) {
                buf[k] = (byte) (seed & 255);
                buf[k] = (byte) (seed & 255);
                seed+=79;
                seed+=79;
            }
            }
            flashWriteSector(i,j,buf);
            flashWriteSector(i,j,buf);
        }
        }
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
    }
    }
 
 
// ******* testR **************************************************************
// ******* testR **************************************************************
// measures read performance
// measures read performance
    public double testR ( int num, int seed ) throws UsbException, InvalidFirmwareException, CapabilityException {
    public double testR ( int num, int seed ) throws UsbException, InvalidFirmwareException, CapabilityException {
        int secNum = 2048 / flashSectorSize();
        int secNum = 2048 / flashSectorSize();
        byte[] buf = new byte[flashSectorSize() * secNum];
        byte[] buf = new byte[flashSectorSize() * secNum];
        int errors = 0;
        int errors = 0;
        long t0 = new Date().getTime();
        long t0 = new Date().getTime();
        for ( int i=0; i<num; i+=secNum ) {
        for ( int i=0; i<num; i+=secNum ) {
            int j = Math.min(num-i,secNum);
            int j = Math.min(num-i,secNum);
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
            System.out.print("Sector " + (i+j) + "/" + num+ "  " + Math.round(10000.0*(i+1)/num)/100.0 + "%    \r");
            flashReadSector(i,j,buf);
            flashReadSector(i,j,buf);
            int diffs = flashSectorSize()*j;
            int diffs = flashSectorSize()*j;
            for (int k=0; k<flashSectorSize()*j; k++) {
            for (int k=0; k<flashSectorSize()*j; k++) {
                if ( buf[k] == (byte) (seed & 255) )
                if ( buf[k] == (byte) (seed & 255) )
                    diffs-=1;
                    diffs-=1;
                seed+=79;
                seed+=79;
            }
            }
            if ( diffs!=0 && errors==0 ) {
            if ( diffs!=0 && errors==0 ) {
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
                System.out.println("Error occured at sector " + i +": " + diffs + " differences");
            }
            }
            if ( diffs!=0 )
            if ( diffs!=0 )
                errors+=1;
                errors+=1;
        }
        }
        System.out.println("testR: " + errors +" errors detected");
        System.out.println("testR: " + errors +" errors detected");
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
        return num*flashSectorSize()*1.0/(new Date().getTime() - t0);
    }
    }
 
 
// ******* main ****************************************************************
// ******* main ****************************************************************
    public static void main (String args[]) {
    public static void main (String args[]) {
 
 
        int devNum = 0;
        int devNum = 0;
        boolean force = false;
        boolean force = false;
        boolean workarounds = false;
        boolean workarounds = false;
        int sectors = 10000;
        int sectors = 10000;
 
 
        try {
        try {
// init USB stuff
// init USB stuff
            LibusbJava.usb_init();
            LibusbJava.usb_init();
 
 
// scan the USB bus
// scan the USB bus
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
            if ( bus.numberOfDevices() <= 0) {
            if ( bus.numberOfDevices() <= 0) {
                System.err.println("No devices found");
                System.err.println("No devices found");
                System.exit(0);
                System.exit(0);
            }
            }
 
 
// scan the command line arguments
// scan the command line arguments
            for (int i=0; i<args.length; i++ ) {
            for (int i=0; i<args.length; i++ ) {
                if ( args[i].equals("-d") ) {
                if ( args[i].equals("-d") ) {
                    i++;
                    i++;
                    try {
                    try {
                        if (i>=args.length) throw new Exception();
                        if (i>=args.length) throw new Exception();
                        devNum = Integer.parseInt( args[i] );
                        devNum = Integer.parseInt( args[i] );
                    }
                    }
                    catch (Exception e) {
                    catch (Exception e) {
                        throw new ParameterException("Device number expected after -d");
                        throw new ParameterException("Device number expected after -d");
                    }
                    }
                }
                }
                if ( args[i].equals("-s") ) {
                if ( args[i].equals("-s") ) {
                    i++;
                    i++;
                    try {
                    try {
                        if (i>=args.length) throw new Exception();
                        if (i>=args.length) throw new Exception();
                        sectors = Integer.parseInt( args[i] );
                        sectors = Integer.parseInt( args[i] );
                    }
                    }
                    catch (Exception e) {
                    catch (Exception e) {
                        throw new ParameterException("Number of sectors expected after -s");
                        throw new ParameterException("Number of sectors expected after -s");
                    }
                    }
                }
                }
                else if ( args[i].equals("-f") ) {
                else if ( args[i].equals("-f") ) {
                    force = true;
                    force = true;
                }
                }
                else if ( args[i].equals("-p") ) {
                else if ( args[i].equals("-p") ) {
                    bus.printBus(System.out);
                    bus.printBus(System.out);
                    System.exit(0);
                    System.exit(0);
                }
                }
                else if ( args[i].equals("-p") ) {
                else if ( args[i].equals("-p") ) {
                    bus.printBus(System.out);
                    bus.printBus(System.out);
                    System.exit(0);
                    System.exit(0);
                }
                }
                else if ( args[i].equals("-w") ) {
                else if ( args[i].equals("-w") ) {
                    workarounds = true;
                    workarounds = true;
                }
                }
                else if ( args[i].equals("-h") ) {
                else if ( args[i].equals("-h") ) {
                        System.err.println(ParameterException.helpMsg);
                        System.err.println(ParameterException.helpMsg);
                        System.exit(0);
                        System.exit(0);
                }
                }
                else throw new ParameterException("Invalid Parameter: "+args[i]);
                else throw new ParameterException("Invalid Parameter: "+args[i]);
            }
            }
 
 
 
 
// create the main class            
// create the main class            
            FlashBench ztex = new FlashBench ( bus.device(devNum) );
            FlashBench ztex = new FlashBench ( bus.device(devNum) );
            ztex.certainWorkarounds = workarounds;
            ztex.certainWorkarounds = workarounds;
 
 
// upload the firmware if necessary
// upload the firmware if necessary
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("flashbench for UM 1.0") ) {
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("flashbench for UM 1.0") ) {
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "flashbench.ihx", force ) + " ms");
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "flashbench.ihx", force ) + " ms");
            }
            }
 
 
// print some information
// print some information
            System.out.println("Capabilities: " + ztex.capabilityInfo(", "));
            System.out.println("Capabilities: " + ztex.capabilityInfo(", "));
            System.out.println("Enabled: " + ztex.flashEnabled());
            System.out.println("Enabled: " + ztex.flashEnabled());
            System.out.println("Size: " + ztex.flashSize()+" Bytes");
            System.out.println("Size: " + ztex.flashSize()+" Bytes");
//          ztex.printMmcState();
//          ztex.printMmcState();
 
 
            if ( sectors<1 || sectors>ztex.flashSectors() ) sectors = ztex.flashSectors();
            if ( sectors<1 || sectors>ztex.flashSectors() ) sectors = ztex.flashSectors();
 
 
            System.out.println("Read + Write Performance: " + ztex.testRW(sectors) + "kb/s      \n");
            System.out.println("Read + Write Performance: " + ztex.testRW(sectors) + "kb/s      \n");
            int seed = (int) Math.round(65535*Math.random());
            int seed = (int) Math.round(65535*Math.random());
            System.out.println("Write Performance: " + ztex.testW(sectors, seed) + "kb/s      ");
            System.out.println("Write Performance: " + ztex.testW(sectors, seed) + "kb/s      ");
            System.out.println("Read Performance: " + ztex.testR(sectors, seed) + "kb/s     \n");
            System.out.println("Read Performance: " + ztex.testR(sectors, seed) + "kb/s     \n");
        }
        }
        catch (Exception e) {
        catch (Exception e) {
            System.out.println("Error: "+e.getLocalizedMessage() );
            System.out.println("Error: "+e.getLocalizedMessage() );
        }
        }
   }
   }
 
 
}
}
 
 

powered by: WebSVN 2.1.0

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