/*!
|
/*!
|
Java host software API of ZTEX EZ-USB FX2 SDK
|
Java host software API of ZTEX SDK
|
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/.
|
!*/
|
!*/
|
|
|
package ztex;
|
package ztex;
|
|
|
import java.io.*;
|
import java.io.*;
|
import java.util.*;
|
import java.util.*;
|
|
|
import ch.ntb.usb.*;
|
import ch.ntb.usb.*;
|
|
|
/**
|
/**
|
* Provides methods for uploading firmware to Cypress EZ-USB devices.
|
* Provides methods for uploading firmware to Cypress EZ-USB devices.
|
*/
|
*/
|
public class EzUsb {
|
public class EzUsb {
|
// ******* reset **************************************************************
|
// ******* reset **************************************************************
|
/**
|
/**
|
* Controls the reset state of a Cypress EZ-USB device.
|
* Controls the reset state of a Cypress EZ-USB device.
|
* @param handle The handle of the device.
|
* @param handle The handle of the device.
|
* @param r The reset state (true means reset).
|
* @param r The reset state (true means reset).
|
* @throws FirmwareUploadException if an error occurred while attempting to control the reset state.
|
* @throws FirmwareUploadException if an error occurred while attempting to control the reset state.
|
*/
|
*/
|
public static void reset ( long handle, boolean r ) throws FirmwareUploadException {
|
public static void reset ( long handle, boolean r ) throws FirmwareUploadException {
|
byte buffer[] = { (byte) (r ? 1 : 0) };
|
byte buffer[] = { (byte) (r ? 1 : 0) };
|
int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, 0xE600, 0, buffer, 1, 1000); // upload j bytes
|
int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, 0xE600, 0, buffer, 1, 1000); // upload j bytes
|
if ( k<0 )
|
if ( k<0 )
|
throw new FirmwareUploadException( LibusbJava.usb_strerror() + ": unable to set reset="+buffer[0] );
|
throw new FirmwareUploadException( LibusbJava.usb_strerror() + ": unable to set reset="+buffer[0] );
|
else if ( k!=1 )
|
else if ( k!=1 )
|
throw new FirmwareUploadException( "Unable to set reset="+buffer[0] );
|
throw new FirmwareUploadException( "Unable to set reset="+buffer[0] );
|
try {
|
try {
|
Thread.sleep( r ? 50 : 400 ); // give the firmware some time for initialization
|
Thread.sleep( r ? 50 : 400 ); // give the firmware some time for initialization
|
}
|
}
|
catch ( InterruptedException e ) {
|
catch ( InterruptedException e ) {
|
}
|
}
|
}
|
}
|
|
|
// ******* uploadFirmware ******************************************************
|
// ******* uploadFirmware ******************************************************
|
/**
|
/**
|
* Uploads the Firmware to a Cypress EZ-USB device.
|
* Uploads the Firmware to a Cypress EZ-USB device.
|
* @param handle The handle of the device.
|
* @param handle The handle of the device.
|
* @param ihxFile The firmware image.
|
* @param ihxFile The firmware image.
|
* @return the upload time in ms.
|
* @return the upload time in ms.
|
* @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
|
* @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
|
*/
|
*/
|
public static long uploadFirmware (long handle, IhxFile ihxFile ) throws FirmwareUploadException {
|
public static long uploadFirmware (long handle, IhxFile ihxFile ) throws FirmwareUploadException {
|
final int transactionBytes = 256;
|
final int transactionBytes = 256;
|
byte[] buffer = new byte[transactionBytes];
|
byte[] buffer = new byte[transactionBytes];
|
|
|
reset( handle, true ); // reset = 1
|
reset( handle, true ); // reset = 1
|
|
|
long t0 = new Date().getTime();
|
long t0 = new Date().getTime();
|
int j = 0;
|
int j = 0;
|
for ( int i=0; i<=ihxFile.ihxData.length; i++ ) {
|
for ( int i=0; i<=ihxFile.ihxData.length; i++ ) {
|
if ( i >= ihxFile.ihxData.length || ihxFile.ihxData[i] < 0 || j >=transactionBytes ) {
|
if ( i >= ihxFile.ihxData.length || ihxFile.ihxData[i] < 0 || j >=transactionBytes ) {
|
if ( j > 0 ) {
|
if ( j > 0 ) {
|
int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, i-j, 0, buffer, j, 1000); // upload j bytes
|
int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, i-j, 0, buffer, j, 1000); // upload j bytes
|
if ( k<0 )
|
if ( k<0 )
|
throw new FirmwareUploadException(LibusbJava.usb_strerror());
|
throw new FirmwareUploadException(LibusbJava.usb_strerror());
|
else if ( k!=j )
|
else if ( k!=j )
|
throw new FirmwareUploadException();
|
throw new FirmwareUploadException();
|
try {
|
try {
|
Thread.sleep( 1 ); // to avoid package loss
|
Thread.sleep( 1 ); // to avoid package loss
|
}
|
}
|
catch ( InterruptedException e ) {
|
catch ( InterruptedException e ) {
|
}
|
}
|
}
|
}
|
j = 0;
|
j = 0;
|
}
|
}
|
|
|
if ( i < ihxFile.ihxData.length && ihxFile.ihxData[i] >= 0 && ihxFile.ihxData[i] <= 255 ) {
|
if ( i < ihxFile.ihxData.length && ihxFile.ihxData[i] >= 0 && ihxFile.ihxData[i] <= 255 ) {
|
buffer[j] = (byte) ihxFile.ihxData[i];
|
buffer[j] = (byte) ihxFile.ihxData[i];
|
j+=1;
|
j+=1;
|
}
|
}
|
}
|
}
|
long t1 = new Date().getTime();
|
long t1 = new Date().getTime();
|
|
|
try {
|
try {
|
EzUsb.reset(handle,false); // error (may caused re-numeration) can be ignored
|
EzUsb.reset(handle,false); // error (may caused re-numeration) can be ignored
|
}
|
}
|
catch ( FirmwareUploadException e ) {
|
catch ( FirmwareUploadException e ) {
|
}
|
}
|
return t1 - t0;
|
return t1 - t0;
|
}
|
}
|
}
|
}
|
|
|
|
|