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