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

Subversion Repositories usb_fpga_2_16

[/] [usb_fpga_2_16/] [trunk/] [examples/] [usb-fpga-1.15/] [1.15a/] [intraffic/] [InTraffic.java] - Diff between revs 2 and 3

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

Rev 2 Rev 3
/*!
/*!
   intraffic -- example showing how the EZ-USB FIFO interface is used on ZTEX USB-FPGA Module 1.15b
   intraffic -- example showing how the EZ-USB FIFO interface is used on ZTEX USB-FPGA Module 1.15b
   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" +
                "    -f                Force uploads\n" +
                "    -f                Force uploads\n" +
                "    -p                Print bus info\n" +
                "    -p                Print bus info\n" +
                "    -w                Enable certain workarounds\n"+
                "    -w                Enable certain workarounds\n"+
                "    -h                This help" );
                "    -h                This help" );
 
 
    public ParameterException (String msg) {
    public ParameterException (String msg) {
        super( msg + "\n" + helpMsg );
        super( msg + "\n" + helpMsg );
    }
    }
}
}
 
 
// *****************************************************************************
// *****************************************************************************
// ******* USBReader ***********************************************************
// ******* USBReader ***********************************************************
// *****************************************************************************
// *****************************************************************************
class UsbReader extends Thread {
class UsbReader extends Thread {
    private final int bufNum = 8;
    private final int bufNum = 8;
    public final int bufSize = 512*1024;
    public final int bufSize = 512*1024;
    public byte[][] buf = new byte[bufNum][];
    public byte[][] buf = new byte[bufNum][];
    public int[] bufBytes = new int[bufNum];
    public int[] bufBytes = new int[bufNum];
    private int readCount = -1;
    private int readCount = -1;
    private int getCount = -1;
    private int getCount = -1;
    public boolean terminate = false;
    public boolean terminate = false;
    private Ztex1v1 ztex;
    private Ztex1v1 ztex;
 
 
    public UsbReader ( Ztex1v1 p_ztex ) {
    public UsbReader ( Ztex1v1 p_ztex ) {
        super ();
        super ();
        ztex = p_ztex;
        ztex = p_ztex;
        for (int i=0; i<bufNum; i++) {
        for (int i=0; i<bufNum; i++) {
            buf[i]=new byte[bufSize];
            buf[i]=new byte[bufSize];
        }
        }
    }
    }
 
 
    public int getBuffer () {
    public int getBuffer () {
        getCount += 1;
        getCount += 1;
        while (getCount >= readCount) {
        while (getCount >= readCount) {
            try {
            try {
                sleep(1);
                sleep(1);
            }
            }
            catch ( InterruptedException e) {
            catch ( InterruptedException e) {
            }
            }
        }
        }
        return getCount % bufNum;
        return getCount % bufNum;
    }
    }
 
 
    public void reset () {
    public void reset () {
        getCount = readCount + 1;
        getCount = readCount + 1;
    }
    }
 
 
    public void run() {
    public void run() {
        setPriority(MAX_PRIORITY);
        setPriority(MAX_PRIORITY);
 
 
// claim interface 0
// claim interface 0
        try {
        try {
            ztex.trySetConfiguration ( 1 );
            ztex.trySetConfiguration ( 1 );
            ztex.claimInterface ( 0 );
            ztex.claimInterface ( 0 );
        }
        }
        catch ( Exception e) {
        catch ( Exception e) {
            System.out.println("Error: "+e.getLocalizedMessage() );
            System.out.println("Error: "+e.getLocalizedMessage() );
            System.exit(2);
            System.exit(2);
        }
        }
 
 
 
 
// reader loop
// reader loop
        while ( !terminate ) {
        while ( !terminate ) {
            readCount += 1;
            readCount += 1;
 
 
            while ( readCount - bufNum >= getCount ) {
            while ( readCount - bufNum >= getCount ) {
                try {
                try {
                    sleep(1);
                    sleep(1);
                }
                }
                catch ( InterruptedException e) {
                catch ( InterruptedException e) {
                }
                }
            }
            }
 
 
            int i = readCount % bufNum;
            int i = readCount % bufNum;
            bufBytes[i] = LibusbJava.usb_bulk_read(ztex.handle(), 0x82, buf[i], bufSize, 1000);
            bufBytes[i] = LibusbJava.usb_bulk_read(ztex.handle(), 0x82, buf[i], bufSize, 1000);
//          System.out.println("Buffer " + i +": read " + bufBytes[i] + " bytes");
//          System.out.println("Buffer " + i +": read " + bufBytes[i] + " bytes");
        }
        }
 
 
// release interface 0
// release interface 0
        ztex.releaseInterface( 0 );
        ztex.releaseInterface( 0 );
 
 
    }
    }
}
}
 
 
 
 
// *****************************************************************************
// *****************************************************************************
// ******* Test0 ***************************************************************
// ******* Test0 ***************************************************************
// *****************************************************************************
// *****************************************************************************
class InTraffic extends Ztex1v1 {
class InTraffic extends Ztex1v1 {
 
 
// ******* InTraffic **************************************************************
// ******* InTraffic **************************************************************
// constructor
// constructor
    public InTraffic ( ZtexDevice1 pDev ) throws UsbException {
    public InTraffic ( ZtexDevice1 pDev ) throws UsbException {
        super ( pDev );
        super ( pDev );
    }
    }
 
 
// ******* 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;
 
 
        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");
                    }
                    }
                }
                }
                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            
            InTraffic ztex = new InTraffic ( bus.device(devNum) );
            InTraffic ztex = new InTraffic ( 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("intraffic example for UFM 1.15") ) {
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("intraffic example for UFM 1.15") ) {
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "intraffic.ihx", force ) + " ms");
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "intraffic.ihx", force ) + " ms");
                force = true;
                force = true;
            }
            }
 
 
// upload the bitstream if necessary
// upload the bitstream if necessary
            if ( force || ! ztex.getFpgaConfiguration() ) {
            if ( force || ! ztex.getFpgaConfiguration() ) {
                System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/intraffic.bit" , force ) + " ms");
                System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/intraffic.bit" , force ) + " ms");
            }
            }
 
 
// read the traffic
// read the traffic
            UsbReader reader = new UsbReader( ztex );
            UsbReader reader = new UsbReader( ztex );
            reader.start();
            reader.start();
 
 
// EZ-USB FIFO test (controlled mode)
// EZ-USB FIFO test (controlled mode)
            ztex.vendorCommand (0x60, "Set test mode", 0, 0);
            ztex.vendorCommand (0x60, "Set test mode", 0, 0);
            reader.reset();
            reader.reset();
 
 
            int vcurrent = -1;
            int vcurrent = -1;
            for (int i=0; i<1000; i++) {
            for (int i=0; i<1000; i++) {
                int j = reader.getBuffer();
                int j = reader.getBuffer();
                int bb = reader.bufBytes[j];
                int bb = reader.bufBytes[j];
                byte[] b = reader.buf[j];
                byte[] b = reader.buf[j];
                int current = vcurrent+1;
                int current = vcurrent+1;
                int lastwi = -1;
                int lastwi = -1;
                int aerrors = 0;
                int aerrors = 0;
                int ferrors = 0;
                int ferrors = 0;
                int errors = 0;
                int errors = 0;
                int prevErrors = 0;
                int prevErrors = 0;
 
 
                for (int k=1; k<bb; k+=2 ) {
                for (int k=1; k<bb; k+=2 ) {
                    if ( (b[k] & 0x80) == 0 ) {
                    if ( (b[k] & 0x80) == 0 ) {
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
                        if ( lastwi == 0 ) aerrors+=1;
                        if ( lastwi == 0 ) aerrors+=1;
                        if ( lastwi == 0 ) System.out.println("Alignment error: 0 at " + i + ":" + (k-1) );
                        if ( lastwi == 0 ) System.out.println("Alignment error: 0 at " + i + ":" + (k-1) );
                        lastwi = 0;
                        lastwi = 0;
                    }
                    }
                    else {
                    else {
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
 
 
                        vcurrent += 1;
                        vcurrent += 1;
                        if ( vcurrent % 100 == 90 )
                        if ( vcurrent % 100 == 90 )
                            vcurrent += 10;
                            vcurrent += 10;
 
 
                        if ( lastwi == 1 ) {
                        if ( lastwi == 1 ) {
                            aerrors+=1;
                            aerrors+=1;
                            System.out.println("Alignment error: 1 at " + i + ":" + (k-1) );
                            System.out.println("Alignment error: 1 at " + i + ":" + (k-1) );
                        }
                        }
                        else if ( vcurrent != current ) {
                        else if ( vcurrent != current ) {
                            if ( (i != 0) && ( k != 3) ) {
                            if ( (i != 0) && ( k != 3) ) {
                                System.out.println("Error: 0b" + Integer.toBinaryString(vcurrent) + " expected at " + i + ":" + (k-3) + " but " );
                                System.out.println("Error: 0b" + Integer.toBinaryString(vcurrent) + " expected at " + i + ":" + (k-3) + " but " );
                                System.out.println("       0b" + Integer.toBinaryString(current) + " found");
                                System.out.println("       0b" + Integer.toBinaryString(current) + " found");
                                errors+=1;
                                errors+=1;
                                prevErrors+=1;
                                prevErrors+=1;
                            }
                            }
                            vcurrent = current;
                            vcurrent = current;
                        }
                        }
                        else {
                        else {
//                          if ( prevErrors > 0 ) System.out.println("       0b" + Integer.toBinaryString(current) );
//                          if ( prevErrors > 0 ) System.out.println("       0b" + Integer.toBinaryString(current) );
                            if ( prevErrors == 1 )
                            if ( prevErrors == 1 )
                                ferrors +=1;
                                ferrors +=1;
                            prevErrors = 0;
                            prevErrors = 0;
                        }
                        }
 
 
                        lastwi = 1;
                        lastwi = 1;
//                      System.out.println(current);
//                      System.out.println(current);
                    }
                    }
//                  System.out.println(b[k]+"  " +b[k+1]);
//                  System.out.println(b[k]+"  " +b[k+1]);
                }
                }
                System.out.print("Buffer " + i + ": " + (errors-ferrors) + " errors,  " + ferrors + " FIFO errors,  " + aerrors + " alignment errors  \r");
                System.out.print("Buffer " + i + ": " + (errors-ferrors) + " errors,  " + ferrors + " FIFO errors,  " + aerrors + " alignment errors  \r");
            }
            }
            System.out.println();
            System.out.println();
 
 
// performance test (continous mode)
// performance test (continous mode)
            ztex.vendorCommand (0x60, "Set test mode", 1, 0);
            ztex.vendorCommand (0x60, "Set test mode", 1, 0);
            reader.reset();
            reader.reset();
 
 
            int words = 0;
            int words = 0;
            int intSum = 0;
            int intSum = 0;
            int intMax = 0;
            int intMax = 0;
            int intAdj = 0;
            int intAdj = 0;
            int lastwi = -1;
            int lastwi = -1;
            for (int i=0; i<1000; i++) {
            for (int i=0; i<1000; i++) {
                int j = reader.getBuffer();
                int j = reader.getBuffer();
                int bb = reader.bufBytes[j];
                int bb = reader.bufBytes[j];
                byte[] b = reader.buf[j];
                byte[] b = reader.buf[j];
                int current = vcurrent+1;
                int current = vcurrent+1;
 
 
                for (int k=1; k<bb; k+=2 ) {
                for (int k=1; k<bb; k+=2 ) {
                    if ( (b[k] & 0x80) == 0 ) {
                    if ( (b[k] & 0x80) == 0 ) {
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
                        current = ((b[k] & 0x7f) << 8) | (b[k-1] & 0xff);
                        if ( lastwi == 0 ) intAdj -= 1;
                        if ( lastwi == 0 ) intAdj -= 1;
                        lastwi = 0;
                        lastwi = 0;
                    }
                    }
                    else {
                    else {
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
                        current |= ((b[k] & 0x7f) << 23) | ((b[k-1] & 0xff) << 15);
 
 
                        if ( lastwi == 1 ) {
                        if ( lastwi == 1 ) {
                            intAdj += 1;
                            intAdj += 1;
                        }
                        }
                        else {
                        else {
                            vcurrent += 1;
                            vcurrent += 1;
                            int it = (current - vcurrent)*2 + intAdj;
                            int it = (current - vcurrent)*2 + intAdj;
                            if ( it > 0 && words > 0) {
                            if ( it > 0 && words > 0) {
                                intSum += it;
                                intSum += it;
                                if ( it > intMax )
                                if ( it > intMax )
                                    intMax = it;
                                    intMax = it;
                            }
                            }
                            words += 2;
                            words += 2;
                            vcurrent = current;
                            vcurrent = current;
                            intAdj = 0;
                            intAdj = 0;
                        }
                        }
                        lastwi = 1;
                        lastwi = 1;
//                      System.out.println(current);
//                      System.out.println(current);
                    }
                    }
//                  System.out.println(b[k]+"  " +b[k+1]);
//                  System.out.println(b[k]+"  " +b[k+1]);
                }
                }
                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");
                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");
            }
            }
            System.out.println();
            System.out.println();
 
 
 
 
            reader.terminate=true;
            reader.terminate=true;
 
 
        }
        }
        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.