* All firmware implementations that provide the ZTEX descriptor 1 are supported. * A description of this descriptor can be found in {@link ZtexDevice1}. *
* The most important features of this class are the functions for uploading the firmware * and the renumeration management. *
* The interface dependent part of the communication protocol (currently only one is supported)
* can be found in {@link Ztex1v1}.
* @see ZtexDevice1
* @see Ztex1v1
*/
public class Ztex1 {
private final int maxDevNum = 1023;
private long handle;
private ZtexDevice1 dev = null;
private boolean oldDevices[] = new boolean[maxDevNum+1];
private int oldDevNum = -1;
private String usbBusName = null;
private boolean[] interfaceClaimed = new boolean[256];
private boolean configurationSet = false;
/** * Setting to true enables certain workarounds, e.g. to deal with bad driver/OS implementations. */
public boolean certainWorkarounds = false;
/** * The timeout for control messages in ms. */
public int controlMsgTimeout = 1000; // in ms
private long lastVendorCommandT = 0;
// ******* Ztex1 ***************************************************************
/**
* Constructs an instance from a given device.
* @param pDev The given device.
* @throws UsbException if an communication error occurred.
*/
public Ztex1 ( ZtexDevice1 pDev ) throws UsbException {
dev = pDev;
init();
}
// ******* init ****************************************************************
/**
* Initializates the class.
* @throws UsbException if an communication error occurred.
*/
protected void init () throws UsbException {
for (int i=0; i<256; i++)
interfaceClaimed[i] = false;
handle = LibusbJava.usb_open(dev.dev());
// if ( handle<=0 )
// throw new UsbException(dev.dev(), "Error opening device");
}
// ******* finalize ************************************************************
/** * The destructor closes the USB file handle. */
protected void finalize () {
for (int i=0; i<256; i++)
if ( interfaceClaimed[i] )
LibusbJava.usb_release_interface(handle, i);
LibusbJava.usb_close(handle);
}
// ******* handle **************************************************************
/** * Returns the USB file handle. */
public final long handle()
{
return handle;
}
// ******* dev *****************************************************************
/**
* Returns the corresponding {@link ZtexDevice1}.
* @return the corresponding {@link ZtexDevice1}.
*/
public final ZtexDevice1 dev()
{
return dev;
}
// ******* valid ***************************************************************
/**
* Returns true if ZTEX descriptor 1 is available.
* @return true if ZTEX descriptor 1 is available.
*/
public boolean valid ( ) {
return dev.valid();
}
// ******* checkValid **********************************************************
/**
* Checks whether ZTEX descriptor 1 is available.
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
*/
public void checkValid () throws InvalidFirmwareException {
if ( ! dev.valid() )
throw new InvalidFirmwareException(this, "Can't read ZTEX descriptor 1");
}
// ******* vendorCommand *******************************************************
/**
* Sends a vendor command to Endpoint 0 of the EZ-USB device.
* The command may be send multiple times until the {@link #controlMsgTimeout} is reached.
* @param cmd The command number (0..255).
* @param func The name of the command. This string is used for the generation of error messages.
* @param value The value (0..65535), i.e bytes 2 and 3 of the setup data.
* @param index The index (0..65535), i.e. bytes 4 and 5 of the setup data.
* @param length The size of the payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
* @param buf The payload data buffer.
* @return the number of bytes sent.
* @throws UsbException if a communication error occurs.
*/
public synchronized int vendorCommand (int cmd, String func, int value, int index, byte[] buf, int length) throws UsbException {
long t0 = new Date().getTime()-100;
int trynum = 0;
int i = -1;
if ( controlMsgTimeout < 200 )
controlMsgTimeout = 200;
// while ( i<=0 && new Date().getTime()-t0
* 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 ihxFile The firmware image.
* @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 force 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 ( ZtexIhxFile1 ihxFile, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
// load the ihx file
// ihxFile.dataInfo(System.out);
// System.out.println(ihxFile);
// check for compatibility
if ( ! force && dev.valid() ) {
if ( ihxFile.interfaceVersion() != 1 )
throw new IncompatibleFirmwareException("Wrong interface version: Expected 1, got " + ihxFile.interfaceVersion() );
if ( ! dev.compatible ( ihxFile.productId(0), ihxFile.productId(1), ihxFile.productId(2), ihxFile.productId(3) ) )
throw new IncompatibleFirmwareException("Incompatible productId's: Current firmware: " + ZtexDevice1.byteArrayString(dev.productId())
+ " Ihx File: " + ZtexDevice1.byteArrayString(ihxFile.productId()) );
}
// scan the bus for comparison
findOldDevices();
// upload the firmware
long time = EzUsb.uploadFirmware( handle, ihxFile );
// find and init new device
initNewDevice("Device lost after uploading Firmware", false);
return time;
}
/**
* Uploads the firmware to the EZ-USB and manages the renumeration process.
*
* 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 force 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 );
}
/**
* Uploads the firmware to the EZ-USB and manages the renumeration process.
*
* 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 ihxIn Input stream from which the ihx file is read.
* @param name Name of the input.
* @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 force 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 ( InputStream ihxIn, String name, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
// load the ihx file
ZtexIhxFile1 ihxFile;
try {
ihxFile = new ZtexIhxFile1( ihxIn, name );
}
catch ( IOException e ) {
throw new FirmwareUploadException( e.getLocalizedMessage() );
}
catch ( IhxFileDamagedException e ) {
throw new FirmwareUploadException( e.getLocalizedMessage() );
}
return uploadFirmware( ihxFile, force );
}
// ******* resetEzUsb **********************************************************
/**
* Resets the EZ-USB and manages the renumeration process.
*
* After the reset 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).
* @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.
*/
public void resetEzUsb () throws FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
// scan the bus for comparison
findOldDevices();
// reset the EZ-USB
EzUsb.reset(handle,true);
try {
EzUsb.reset(handle,false); // error (may caused by re-numeration) can be ignored
}
catch ( FirmwareUploadException e ) {
}
// find and init new device
initNewDevice( "Device lost after resetting the EZ-USB", true );
}
// ******* toString ************************************************************
/**
* Returns a lot of useful information about the corresponding device.
* @return a lot of useful information about the corresponding device.
*/
public String toString () {
return dev.toString();
}
}
Details |
Compare with Previous |
View Log
Subversion Repositories usb_fpga_1_11
[/] [usb_fpga_1_11/] [trunk/] [java/] [ztex/] [Ztex1.java] - Blame information for rev 9
Line No.
Rev
Author
Line