Line 1... |
Line 1... |
/*!
|
/*!
|
Java Driver API for the ZTEX Firmware Kit
|
Java host software API of 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 38... |
Line 38... |
* can be found in {@link Ztex1v1}.
|
* can be found in {@link Ztex1v1}.
|
* @see ZtexDevice1
|
* @see ZtexDevice1
|
* @see Ztex1v1
|
* @see Ztex1v1
|
*/
|
*/
|
public class Ztex1 {
|
public class Ztex1 {
|
private int handle;
|
private final int maxDevNum = 1023;
|
|
private long handle;
|
private ZtexDevice1 dev = null;
|
private ZtexDevice1 dev = null;
|
private boolean oldDevices[] = new boolean[128];
|
private boolean oldDevices[] = new boolean[maxDevNum+1];
|
private String usbBusName = null;
|
private String usbBusName = null;
|
|
private boolean[] interfaceClaimed = new boolean[256];
|
/** * Setting to true enables certain workarounds, e.g. to deal with bad driver/OS implementations. */
|
/** * Setting to true enables certain workarounds, e.g. to deal with bad driver/OS implementations. */
|
public boolean certainWorkarounds = false;
|
public boolean certainWorkarounds = false;
|
/** * The timeout for control messages in ms. */
|
/** * The timeout for control messages in ms. */
|
public int controlMsgTimeout = 1000; // in ms
|
public int controlMsgTimeout = 1000; // in ms
|
private long lastVendorCommandT = 0;
|
private long lastVendorCommandT = 0;
|
|
|
|
|
|
|
// ******* Ztex1 ***************************************************************
|
// ******* Ztex1 ***************************************************************
|
/**
|
/**
|
* Constructs an instance from a given device.
|
* Constructs an instance from a given device.
|
* @param pDev The given device.
|
* @param pDev The given device.
|
* @throws UsbException if an communication error occurred.
|
* @throws UsbException if an communication error occurred.
|
*/
|
*/
|
public Ztex1 ( ZtexDevice1 pDev ) throws UsbException {
|
public Ztex1 ( ZtexDevice1 pDev ) throws UsbException {
|
dev = pDev;
|
dev = pDev;
|
|
|
|
for (int i=0; i<256; i++)
|
|
interfaceClaimed[i] = false;
|
|
|
handle = LibusbJava.usb_open(dev.dev());
|
handle = LibusbJava.usb_open(dev.dev());
|
// if ( handle<=0 )
|
// if ( handle<=0 )
|
// throw new UsbException(dev.dev(), "Error opening device");
|
// throw new UsbException(dev.dev(), "Error opening device");
|
}
|
}
|
|
|
// ******* finalize ************************************************************
|
// ******* finalize ************************************************************
|
/** * The destructor closes the USB file handle. */
|
/** * The destructor closes the USB file handle. */
|
protected void finalize () {
|
protected void finalize () {
|
|
for (int i=0; i<256; i++)
|
|
if ( interfaceClaimed[i] )
|
|
LibusbJava.usb_release_interface(handle, i);
|
|
|
LibusbJava.usb_close(handle);
|
LibusbJava.usb_close(handle);
|
}
|
}
|
|
|
// ******* handle **************************************************************
|
// ******* handle **************************************************************
|
/** * Returns the USB file handle. */
|
/** * Returns the USB file handle. */
|
public final int handle()
|
public final long handle()
|
{
|
{
|
return handle;
|
return handle;
|
}
|
}
|
|
|
// ******* dev *****************************************************************
|
// ******* dev *****************************************************************
|
Line 322... |
Line 333... |
if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
|
if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
|
System.err.println("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
|
System.err.println("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
|
}
|
}
|
|
|
|
|
|
// ******* getInterfaceClaimed *************************************************
|
|
/**
|
|
* Returns true if interface is claimed.
|
|
* @return true if interface is claimed
|
|
* @param iface The interface number
|
|
*/
|
|
public boolean getInterfaceClaimed ( int iface ) {
|
|
return iface>=0 && iface<256 && interfaceClaimed[iface];
|
|
}
|
|
|
|
|
// ******* claimInterface ******************************************************
|
// ******* claimInterface ******************************************************
|
/**
|
/**
|
* Claims an interface.
|
* Claims an interface.
|
* @param iface The interface number (usually 0)
|
* @param iface The interface number (usually 0)
|
* @throws UsbException if an error occurs while attempting to claim the interface.
|
* @throws UsbException if an error occurs while attempting to claim the interface.
|
*/
|
*/
|
public void claimInterface ( int iface) throws UsbException{
|
public void claimInterface ( int iface) throws UsbException{
|
if ( LibusbJava.usb_claim_interface(handle(), iface) < 0 )
|
if ( ( iface<0 || iface>=256 || (! interfaceClaimed[iface]) ) && ( LibusbJava.usb_claim_interface(handle(), iface) < 0 ) )
|
throw new UsbException("Claiming interface " + iface + " failed: " + LibusbJava.usb_strerror());
|
throw new UsbException("Claiming interface " + iface + " failed: " + LibusbJava.usb_strerror());
|
|
if ( iface>=0 && iface < 256 )
|
|
interfaceClaimed[iface]=true;
|
}
|
}
|
|
|
|
|
// ******* releaseInterface ****************************************************
|
// ******* releaseInterface ****************************************************
|
/**
|
/**
|
* Releases an interface.
|
* Releases an interface.
|
* @param iface The interface number (usually 0)
|
* @param iface The interface number (usually 0)
|
*/
|
*/
|
public void releaseInterface ( int iface ) {
|
public void releaseInterface ( int iface ) {
|
|
if ( iface<0 || iface>=256 || interfaceClaimed[iface] )
|
LibusbJava.usb_release_interface(handle(), iface);
|
LibusbJava.usb_release_interface(handle(), iface);
|
|
if ( iface>=0 && iface < 256 )
|
|
interfaceClaimed[iface]=false;
|
|
|
}
|
}
|
|
|
|
|
// ******* findOldDevices ******************************************************
|
// ******* findOldDevices ******************************************************
|
private synchronized void findOldDevices () {
|
private synchronized void findOldDevices () throws DeviceLostException {
|
Usb_Bus bus = dev.dev().getBus();
|
Usb_Bus bus = dev.dev().getBus();
|
usbBusName = bus.getDirname();
|
usbBusName = bus.getDirname();
|
|
|
for ( int i=0; i<=127; i++ )
|
for ( int i=0; i<=maxDevNum; i++ )
|
oldDevices[i] = false;
|
oldDevices[i] = false;
|
|
|
Usb_Device d = bus.getDevices();
|
Usb_Device d = bus.getDevices();
|
while ( d != null ) {
|
while ( d != null ) {
|
byte b = d.getDevnum();
|
byte b = d.getDevnum();
|
|
if ( b > maxDevNum )
|
|
throw new DeviceLostException( "Device number too large: " + b + " > " + maxDevNum );
|
if ( b > 0 )
|
if ( b > 0 )
|
oldDevices[b] = true;
|
oldDevices[b] = true;
|
d = d.getNext();
|
d = d.getNext();
|
}
|
}
|
oldDevices[dev.dev().getDevnum()] = false;
|
oldDevices[dev.dev().getDevnum()] = false;
|
Line 375... |
Line 405... |
bus = bus.getNext();
|
bus = bus.getNext();
|
|
|
Usb_Device d = bus != null ? bus.getDevices() : null;
|
Usb_Device d = bus != null ? bus.getDevices() : null;
|
while ( d != null ) {
|
while ( d != null ) {
|
byte b = d.getDevnum();
|
byte b = d.getDevnum();
|
|
if ( b > maxDevNum )
|
|
throw new DeviceLostException( "Device number too large: " + b + " > " + maxDevNum );
|
if ( b > 0 && ! oldDevices[b] ) {
|
if ( b > 0 && ! oldDevices[b] ) {
|
if ( newDev != null )
|
if ( newDev != null )
|
throw new DeviceLostException( errMsg + "More than 2 new devices found" );
|
throw new DeviceLostException( errMsg + "More than 2 new devices found: " + newDev.getDevnum() + "(`" + newDev.getFilename() + "') and " + b + "(`" + d.getFilename() + "')");
|
newDev = d;
|
newDev = d;
|
}
|
}
|
d = d.getNext();
|
d = d.getNext();
|
}
|
}
|
|
|
return newDev;
|
return newDev;
|
}
|
}
|
|
|
// ******* initNewDevice *******************************************************
|
// ******* initNewDevice *******************************************************
|
private void initNewDevice ( String errBase ) throws DeviceLostException, UsbException, InvalidFirmwareException {
|
private void initNewDevice ( String errBase, boolean scanUnconfigured ) throws DeviceLostException, UsbException, InvalidFirmwareException {
|
// scan the bus for up to 60 s for a new device. Boot sequence may take a while.
|
// scan the bus for up to 60 s for a new device. Boot sequence may take a while.
|
Usb_Device newDev = null;
|
Usb_Device newDev = null;
|
for ( int i=0; i<300 && newDev==null; i++ ) {
|
for ( int i=0; i<300 && newDev==null; i++ ) {
|
try {
|
try {
|
Thread.sleep( 200 );
|
Thread.sleep( 200 );
|
Line 406... |
Line 438... |
// init new device
|
// init new device
|
Usb_Device_Descriptor dd = newDev.getDescriptor();
|
Usb_Device_Descriptor dd = newDev.getDescriptor();
|
int vid = dd.getIdVendor() & 65535;
|
int vid = dd.getIdVendor() & 65535;
|
int pid = dd.getIdProduct() & 65535;
|
int pid = dd.getIdProduct() & 65535;
|
try {
|
try {
|
dev = new ZtexDevice1( newDev, vid, pid );
|
dev = new ZtexDevice1( newDev, vid, pid, scanUnconfigured );
|
}
|
|
catch ( InvalidFirmwareException e ) {
|
|
if ( vid == ZtexDevice1.cypressVendorId && pid == ZtexDevice1.cypressProductId ) {
|
|
dev = new ZtexDevice1( newDev, -1, -1 );
|
|
}
|
|
else {
|
|
throw e;
|
|
}
|
}
|
|
catch ( DeviceNotSupportedException e ) {
|
|
throw new InvalidFirmwareException( e.getLocalizedMessage() );
|
}
|
}
|
|
|
handle = LibusbJava.usb_open( dev.dev() );
|
handle = LibusbJava.usb_open( dev.dev() );
|
}
|
}
|
|
|
// ******* uploadFirmware ******************************************************
|
// ******* uploadFirmware ******************************************************
|
/**
|
/**
|
Line 427... |
Line 455... |
* <p>
|
* <p>
|
* Before the firmware is uploaded the device is set into a reset state.
|
* Before the firmware is uploaded the device is set into a reset state.
|
* After the upload the firmware is booted and the renumeration starts.
|
* After the upload the firmware is booted and the renumeration starts.
|
* During this process the device disappears from the bus and a new one
|
* During this process the device disappears from the bus and a new one
|
* occurs which will be assigned to this class automatically (instead of the disappeared one).
|
* occurs which will be assigned to this class automatically (instead of the disappeared one).
|
* @param ihxFileName The file name of the firmware image in ihx format. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
* @param ihxFile The firmware image.
|
* @param force The compatibility check is skipped if true.
|
* @param force The compatibility check is skipped if true.
|
* @throws IncompatibleFirmwareException if the given firmware is not compatible to the installed one, see {@link ZtexDevice1#compatible(int,int,int,int)} (Upload can be enforced using the <tt>force</tt> parameter)
|
* @throws IncompatibleFirmwareException if the given firmware is not compatible to the installed one, see {@link ZtexDevice1#compatible(int,int,int,int)} (Upload can be enforced using the <tt>force</tt> parameter)
|
* @throws FirmwareUploadException If an error occurred while attempting to upload the firmware.
|
* @throws FirmwareUploadException If an error occurred while attempting to upload the firmware.
|
* @throws UsbException if a communication error occurs.
|
* @throws UsbException if a communication error occurs.
|
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
|
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
|
* @throws DeviceLostException if a device went lost after renumeration.
|
* @throws DeviceLostException if a device went lost after renumeration.
|
* @return the upload time in ms.
|
* @return the upload time in ms.
|
*/
|
*/
|
// returns upload time in ms
|
// returns upload time in ms
|
public long uploadFirmware ( String ihxFileName, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
|
public long uploadFirmware ( ZtexIhxFile1 ihxFile, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
|
// load the ihx file
|
// load the ihx file
|
ZtexIhxFile1 ihxFile;
|
|
try {
|
|
ihxFile = new ZtexIhxFile1( ihxFileName );
|
|
}
|
|
catch ( IOException e ) {
|
|
throw new FirmwareUploadException( e.getLocalizedMessage() );
|
|
}
|
|
catch ( IhxFileDamagedException e ) {
|
|
throw new FirmwareUploadException( e.getLocalizedMessage() );
|
|
}
|
|
// ihxFile.dataInfo(System.out);
|
// ihxFile.dataInfo(System.out);
|
// System.out.println(ihxFile);
|
// System.out.println(ihxFile);
|
|
|
// check for compatibility
|
// check for compatibility
|
if ( ! force && dev.valid() ) {
|
if ( ! force && dev.valid() ) {
|
Line 469... |
Line 487... |
|
|
// upload the firmware
|
// upload the firmware
|
long time = EzUsb.uploadFirmware( handle, ihxFile );
|
long time = EzUsb.uploadFirmware( handle, ihxFile );
|
|
|
// find and init new device
|
// find and init new device
|
initNewDevice("Device lost after uploading Firmware");
|
initNewDevice("Device lost after uploading Firmware", false);
|
|
|
return time;
|
return time;
|
}
|
}
|
|
|
|
/**
|
|
* Uploads the firmware to the EZ-USB and manages the renumeration process.
|
|
* <p>
|
|
* Before the firmware is uploaded the device is set into a reset state.
|
|
* After the upload the firmware is booted and the renumeration starts.
|
|
* During this process the device disappears from the bus and a new one
|
|
* occurs which will be assigned to this class automatically (instead of the disappeared one).
|
|
* @param ihxFileName The file name of the firmware image in ihx format. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
|
* @param force The compatibility check is skipped if true.
|
|
* @throws IncompatibleFirmwareException if the given firmware is not compatible to the installed one, see {@link ZtexDevice1#compatible(int,int,int,int)} (Upload can be enforced using the <tt>force</tt> parameter)
|
|
* @throws FirmwareUploadException If an error occurred while attempting to upload the firmware.
|
|
* @throws UsbException if a communication error occurs.
|
|
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
|
|
* @throws DeviceLostException if a device went lost after renumeration.
|
|
* @return the upload time in ms.
|
|
*/
|
|
// returns upload time in ms
|
|
public long uploadFirmware ( String ihxFileName, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
|
|
// load the ihx file
|
|
ZtexIhxFile1 ihxFile;
|
|
try {
|
|
ihxFile = new ZtexIhxFile1( ihxFileName );
|
|
}
|
|
catch ( IOException e ) {
|
|
throw new FirmwareUploadException( e.getLocalizedMessage() );
|
|
}
|
|
catch ( IhxFileDamagedException e ) {
|
|
throw new FirmwareUploadException( e.getLocalizedMessage() );
|
|
}
|
|
return uploadFirmware( ihxFile, force );
|
|
}
|
|
|
// ******* resetEzUsb **********************************************************
|
// ******* resetEzUsb **********************************************************
|
/**
|
/**
|
* Resets the EZ-USB and manages the renumeration process.
|
* Resets the EZ-USB and manages the renumeration process.
|
* <p>
|
* <p>
|
* After the reset the renumeration starts.
|
* After the reset the renumeration starts.
|
Line 499... |
Line 549... |
}
|
}
|
catch ( FirmwareUploadException e ) {
|
catch ( FirmwareUploadException e ) {
|
}
|
}
|
|
|
// find and init new device
|
// find and init new device
|
initNewDevice( "Device lost after resetting the EZ-USB" );
|
initNewDevice( "Device lost after resetting the EZ-USB", true );
|
}
|
}
|
|
|
// ******* toString ************************************************************
|
// ******* toString ************************************************************
|
/**
|
/**
|
* Returns a lot of useful information about the corresponding device.
|
* Returns a lot of useful information about the corresponding device.
|