Line 1... |
Line 1... |
/*!
|
/*!
|
Firmware / Bitstream loader for the ZTEX Firmware Kit
|
Firmware / Bitstream loader for the ZTEX EZ-USB FX2 SDK
|
Copyright (C) 2009-2010 ZTEX e.K.
|
Copyright (C) 2009-2011 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.
|
Line 27... |
Line 27... |
|
|
import ztex.*;
|
import ztex.*;
|
|
|
class FWLoader {
|
class FWLoader {
|
|
|
|
// ******* checkSnString *******************************************************
|
|
// make sure that snString is 10 chars long
|
|
private static String checkSnString ( String snString ) {
|
|
if ( snString.length()>10 ) {
|
|
snString = snString.substring(0,10);
|
|
System.err.println( "Serial number too long (max. 10 characters), truncated to `" + snString + "'" );
|
|
}
|
|
while ( snString.length()<10 )
|
|
snString = '0' + snString;
|
|
return snString;
|
|
}
|
|
|
// ******* main ****************************************************************
|
// ******* main ****************************************************************
|
public static void main (String args[]) {
|
public static void main (String args[]) {
|
LibusbJava.usb_init();
|
LibusbJava.usb_init();
|
|
|
final String helpMsg = new String (
|
final String helpMsg = new String (
|
"Global parameters:\n"+
|
"Global parameters:\n"+
|
" -c Scan for Cypress EZ-USB devices without ZTEX firmware\n"+
|
" -c Scan for Cypress EZ-USB devices without ZTEX firmware\n"+
|
" -v <VID> <PID> Scan for devices with ZTEX firmware and the given Vendor ID and Product ID\n"+
|
" -v <VID> <PID> Scan for devices with given Vendor ID and Product ID\n"+
|
" -vc Equal to -v 0x4b4 0x8613\n"+
|
" -vc Equal to -v 0x4b4 0x8613\n"+
|
" -d <number> Device Number (default: 0)\n"+
|
" -s <sn string> Only scan for devices with that serial number\n"+
|
|
" -d <number> Device Number (default: 0, use -p to get a list)\n"+
|
" -f Force uploads\n"+
|
" -f Force uploads\n"+
|
" -p Print bus info\n"+
|
" -p Print a list of available devices\n"+
|
" -w Enable certain workarounds\n"+
|
" -w Enable certain workarounds\n"+
|
" -h This help \n\n"+
|
" -h This help \n\n"+
|
"Ordered parameters:\n"+
|
"Ordered parameters:\n"+
|
" -i Info\n"+
|
" -i Info\n"+
|
" -ii Info + capabilities\n"+
|
" -ii Info + capabilities\n"+
|
" -if Read FPGA state\n"+
|
" -if Read FPGA state\n"+
|
|
" -ss <sn string> Set the serial number of EZ-USB firmware (used with -uu or -ue)\n"+
|
" -ru Reset EZ-USB Microcontroller\n"+
|
" -ru Reset EZ-USB Microcontroller\n"+
|
" -uu <ihx file> Upload EZ-USB Firmware\n"+
|
" -uu <ihx file> Upload EZ-USB Firmware\n"+
|
" -bs 0|1|A Bit swapping for bitstreams: 0: disable, 1: enable, A: automatic detection\n"+
|
" -bs 0|1|A Bit swapping for bitstreams: 0: disable, 1: enable, A: automatic detection\n"+
|
" -rf Reset FPGA\n"+
|
" -rf Reset FPGA\n"+
|
" -uf <bitstream> Upload bitstream to FPGA\n"+
|
" -uf <bitstream> Upload bitstream to FPGA\n"+
|
" -re Reset EEPROM Firmware\n"+
|
" -re Reset EEPROM Firmware\n"+
|
" -ue <ihx file> Upload Firmware to EEPROM\n"+
|
" -ue <ihx file> Upload Firmware to EEPROM\n"+
|
" -rm Reset FLASH bitstream\n"+
|
" -rm Reset FLASH bitstream\n"+
|
" -um <bitstream> Upload bitstream to Flash");
|
" -um <bitstream> Upload bitstream to Flash\n"+
|
|
" -uxf <ihx file> Upload Firmware / data to ATxmega Flash\n"+
|
|
" -uxe <ihx file> Upload data to ATxmega EEPROM\n"+
|
|
" -rxf <index> Read ATxmega Fuse\n" +
|
|
" -wxf <index> <bitmask> <value> Write ATxmega Fuse\n" +
|
|
"Serial number strings (<sn string>) must be 10 chars long, if shorter filled with 0's." );
|
|
|
// process global parameters
|
// process global parameters
|
try {
|
try {
|
|
|
int usbVendorId = ZtexDevice1.ztexVendorId;
|
int usbVendorId = ZtexDevice1.ztexVendorId;
|
Line 66... |
Line 84... |
boolean cypress = false;
|
boolean cypress = false;
|
int devNum = 0;
|
int devNum = 0;
|
boolean forceUpload = false;
|
boolean forceUpload = false;
|
boolean printBus = false;
|
boolean printBus = false;
|
boolean workarounds = false;
|
boolean workarounds = false;
|
|
String snString = null;
|
int bs = -1;
|
int bs = -1;
|
|
|
|
if ( args.length == 0 ) {
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
|
for (int i=0; i<args.length; i++ ) {
|
for (int i=0; i<args.length; i++ ) {
|
if ( args[i].equals("-c") ) {
|
if ( args[i].equals("-c") ) {
|
cypress = true;
|
cypress = true;
|
}
|
}
|
else if ( args[i].equals("-v") ) {
|
else if ( args[i].equals("-v") ) {
|
Line 122... |
Line 146... |
System.err.println("Error: Device number expected after -d");
|
System.err.println("Error: Device number expected after -d");
|
System.err.println(helpMsg);
|
System.err.println(helpMsg);
|
System.exit(1);
|
System.exit(1);
|
}
|
}
|
}
|
}
|
|
else if ( args[i].equals("-s") ) {
|
|
i++;
|
|
if (i>=args.length) {
|
|
System.err.println("Error: String expected after -s");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
snString = checkSnString(args[i]);
|
|
}
|
else if ( args[i].equals("-h") ) {
|
else if ( args[i].equals("-h") ) {
|
System.err.println(helpMsg);
|
System.err.println(helpMsg);
|
System.exit(0);
|
System.exit(0);
|
}
|
}
|
else if ( args[i].equals("-i") || args[i].equals("-ii") || args[i].equals("-if") || args[i].equals("-ru") || args[i].equals("-rf") || args[i].equals("-re") || args[i].equals("-rm") ) {
|
else if ( args[i].equals("-i") || args[i].equals("-ii") || args[i].equals("-if") || args[i].equals("-ru") || args[i].equals("-rf") || args[i].equals("-re") || args[i].equals("-rm") ) {
|
}
|
}
|
else if ( args[i].equals("-uu") || args[i].equals("-uf") || args[i].equals("-ue") || args[i].equals("-um") || args[i].equals("-bs") ) {
|
else if ( args[i].equals("-uu") || args[i].equals("-uf") || args[i].equals("-ue") || args[i].equals("-um") || args[i].equals("-bs") || args[i].equals("-uxf") || args[i].equals("-uxe") || args[i].equals("-rxf") || args[i].equals("-ss")) {
|
i+=1;
|
i+=1;
|
}
|
}
|
|
else if ( args[i].equals("-wxf") ) {
|
|
i+=3;
|
|
}
|
else {
|
else {
|
System.err.println("Error: Invalid Parameter: "+args[i]);
|
System.err.println("Error: Invalid Parameter: "+args[i]);
|
System.err.println(helpMsg);
|
System.err.println(helpMsg);
|
System.exit(1);
|
System.exit(1);
|
}
|
}
|
}
|
}
|
|
|
// process ordered parameters
|
// process ordered parameters
|
ZtexScanBus1 bus = new ZtexScanBus1( usbVendorId, usbProductId, cypress, false, 1);
|
ZtexScanBus1 bus = new ZtexScanBus1( usbVendorId, usbProductId, cypress, false, 1, snString);
|
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);
|
}
|
}
|
if ( printBus )
|
if ( printBus )
|
bus.printBus(System.out);
|
bus.printBus(System.out);
|
|
|
Ztex1v1 ztex = new Ztex1v1 ( bus.device(devNum) );
|
Ztex1v1 ztex = new Ztex1v1 ( bus.device(devNum) );
|
ztex.certainWorkarounds = workarounds;
|
ztex.certainWorkarounds = workarounds;
|
|
|
|
snString = null;
|
for (int i=0; i<args.length; i++ ) {
|
for (int i=0; i<args.length; i++ ) {
|
if ( args[i].equals("-i") ) {
|
if ( args[i].equals("-i") ) {
|
System.out.println( ztex );
|
System.out.println( ztex );
|
}
|
}
|
if ( args[i].equals("-ii") ) {
|
if ( args[i].equals("-ii") ) {
|
Line 167... |
Line 204... |
}
|
}
|
}
|
}
|
if ( args[i].equals("-if") ) {
|
if ( args[i].equals("-if") ) {
|
ztex.printFpgaState();
|
ztex.printFpgaState();
|
}
|
}
|
|
else if ( args[i].equals("-ss") ) {
|
|
i++;
|
|
if ( i >= args.length ) {
|
|
System.err.println("Error: String expected after -ss");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
snString = checkSnString(args[i]);
|
|
}
|
else if ( args[i].equals("-ru") ) {
|
else if ( args[i].equals("-ru") ) {
|
ztex.resetEzUsb();
|
ztex.resetEzUsb();
|
}
|
}
|
else if ( args[i].equals("-uu") ) {
|
else if ( args[i].equals("-uu") ) {
|
i++;
|
i++;
|
if ( i >= args.length ) {
|
if ( i >= args.length ) {
|
System.err.println("Error: Filename expected after -uu");
|
System.err.println("Error: Filename expected after -uu");
|
System.err.println(helpMsg);
|
System.err.println(helpMsg);
|
System.exit(1);
|
System.exit(1);
|
}
|
}
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( args[i], forceUpload ) + " ms");
|
ZtexIhxFile1 ihxFile = new ZtexIhxFile1( args[i] );
|
|
if ( snString != null )
|
|
ihxFile.setSnString( snString );
|
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( ihxFile, forceUpload ) + " ms");
|
}
|
}
|
else if ( args[i].equals("-bs") ) {
|
else if ( args[i].equals("-bs") ) {
|
i++;
|
i++;
|
if ( (i>=args.length) || !( args[i].equals("0") || args[i].equals("1") || args[i].equalsIgnoreCase("A") ) ) {
|
if ( (i>=args.length) || !( args[i].equals("0") || args[i].equals("1") || args[i].equalsIgnoreCase("A") ) ) {
|
System.err.println("Error: `0',`1' or `A' expected after -bs");
|
System.err.println("Error: `0',`1' or `A' expected after -bs");
|
Line 214... |
Line 263... |
if ( i >= args.length ) {
|
if ( i >= args.length ) {
|
System.err.println("Error: Filename expected after -ue");
|
System.err.println("Error: Filename expected after -ue");
|
System.err.println(helpMsg);
|
System.err.println(helpMsg);
|
System.exit(1);
|
System.exit(1);
|
}
|
}
|
System.out.println("Firmware to EEPROM upload time: " + ztex.eepromUpload( args[i], forceUpload ) + " ms");
|
ZtexIhxFile1 ihxFile = new ZtexIhxFile1( args[i] );
|
|
if ( snString != null )
|
|
ihxFile.setSnString(snString);
|
|
System.out.println("Firmware to EEPROM upload time: " + ztex.eepromUpload( ihxFile, forceUpload ) + " ms");
|
}
|
}
|
else if ( args[i].equals("-rm") ) {
|
else if ( args[i].equals("-rm") ) {
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
ztex.flashResetBitstream();
|
ztex.flashResetBitstream();
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
Line 232... |
Line 284... |
}
|
}
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
System.out.println("FPGA configuration time: " + ztex.flashUploadBitstream( args[i], bs ) + " ms");
|
System.out.println("FPGA configuration time: " + ztex.flashUploadBitstream( args[i], bs ) + " ms");
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
|
}
|
}
|
|
else if ( args[i].equals("-uxf") ) {
|
|
i++;
|
|
if ( i >= args.length ) {
|
|
System.err.println("Error: Filename expected after -uxf");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
System.out.println("Firmware to ATxmega Flash upload time: " + ztex.xmegaWriteFirmware( new IhxFile(args[i]) ) + " ms");
|
|
}
|
|
else if ( args[i].equals("-uxe") ) {
|
|
i++;
|
|
if ( i >= args.length ) {
|
|
System.err.println("Error: Filename expected after -uxe");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
System.out.println("Firmware to ATxmega Flash upload time: " + ztex.xmegaWriteEeprom( new IhxFile(args[i]) ) + " ms");
|
|
}
|
|
else if ( args[i].equals("-rxf") ) {
|
|
i++;
|
|
int j = 0;
|
|
try {
|
|
if (i>=args.length)
|
|
throw new Exception();
|
|
j = Integer.parseInt( args[i] );
|
|
}
|
|
catch (Exception e) {
|
|
System.err.println("Error: Index number expected after -rxf");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
System.out.println("Fuse " + j + ": 0b" + Integer.toBinaryString(256 | ztex.xmegaFuseRead ( j )).substring(1));
|
|
}
|
|
else if ( args[i].equals("-wxf") ) {
|
|
i++;
|
|
int j=0, k=0, l=0;
|
|
try {
|
|
if (i>=args.length)
|
|
throw new Exception();
|
|
j = Integer.parseInt( args[i] );
|
|
}
|
|
catch (Exception e) {
|
|
System.err.println("Error: Index number expected after -wxf");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
i++;
|
|
try {
|
|
if (i>=args.length)
|
|
throw new Exception();
|
|
k = Integer.parseInt( args[i] );
|
|
}
|
|
catch (Exception e) {
|
|
System.err.println("Error: Bitmask expected after -wxf <index>");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
i++;
|
|
try {
|
|
if (i>=args.length)
|
|
throw new Exception();
|
|
l = Integer.parseInt( args[i] );
|
|
}
|
|
catch (Exception e) {
|
|
System.err.println("Error: Value expected after -wxf <index> <bitmask>");
|
|
System.err.println(helpMsg);
|
|
System.exit(1);
|
|
}
|
|
ztex.xmegaFuseWrite( j, (ztex.xmegaFuseRead(j) & ~k) | l );
|
|
}
|
}
|
}
|
}
|
}
|
catch (Exception e) {
|
catch (Exception e) {
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
}
|
}
|