Line 14... |
Line 14... |
|
|
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/.
|
!*/
|
!*/
|
|
|
/*
|
|
Upload firmware to Cypress EZ-USB device
|
|
*/
|
|
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.
|
|
*/
|
public class EzUsb {
|
public class EzUsb {
|
// ******* reset **************************************************************
|
// ******* reset **************************************************************
|
|
/**
|
|
* Controls the reset state of a Cypress EZ-USB device.
|
|
* @param handle The handle of the device.
|
|
* @param r The reset state (true means reset).
|
|
* @throws FirmwareUploadException if an error occurred while attempting to control the reset state.
|
|
*/
|
public static void reset ( int handle, boolean r ) throws FirmwareUploadException {
|
public static void reset ( int 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( 50 );
|
Thread.sleep( r ? 50 : 400 ); // give the firmware some time for initialization
|
}
|
}
|
catch ( InterruptedException e ) {
|
catch ( InterruptedException e ) {
|
}
|
}
|
}
|
}
|
|
|
// ******* uploadFirmware ******************************************************
|
// ******* uploadFirmware ******************************************************
|
// returns upload time in ms
|
/**
|
|
* Uploads the Firmware to a Cypress EZ-USB device.
|
|
* @param handle The handle of the device.
|
|
* @param ihxFile The firmware image.
|
|
* @return The upload time in ms.
|
|
* @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
|
|
*/
|
public static long uploadFirmware (int handle, IhxFile ihxFile ) throws FirmwareUploadException {
|
public static long uploadFirmware (int handle, IhxFile ihxFile ) throws FirmwareUploadException {
|
final int transactionBytes = 256;
|
final int transactionBytes = 256;
|
byte[] buffer = new byte[transactionBytes];
|
byte[] buffer = new byte[transactionBytes];
|
long t0 = new Date().getTime();
|
|
|
|
reset( handle, true ); // reset = 1
|
reset( handle, true ); // reset = 1
|
|
|
|
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
|
Line 72... |
Line 84... |
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();
|
|
|
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 new Date().getTime() - t0;
|
return t1 - t0;
|
}
|
}
|
}
|
}
|
|
|
|
|
No newline at end of file
|
No newline at end of file
|