| 1 |
2 |
ZTEX |
/*!
|
| 2 |
|
|
Java Driver API for the ZTEX Firmware Kit
|
| 3 |
|
|
Copyright (C) 2008-2009 ZTEX e.K.
|
| 4 |
|
|
http://www.ztex.de
|
| 5 |
|
|
|
| 6 |
|
|
This program is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
| 8 |
|
|
published by the Free Software Foundation.
|
| 9 |
|
|
|
| 10 |
|
|
This program is distributed in the hope that it will be useful, but
|
| 11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 13 |
|
|
General Public License for more details.
|
| 14 |
|
|
|
| 15 |
|
|
You should have received a copy of the GNU General Public License
|
| 16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
| 17 |
|
|
!*/
|
| 18 |
|
|
|
| 19 |
|
|
package ztex;
|
| 20 |
|
|
|
| 21 |
|
|
import java.io.*;
|
| 22 |
|
|
import java.util.*;
|
| 23 |
|
|
|
| 24 |
|
|
import ch.ntb.usb.*;
|
| 25 |
|
|
|
| 26 |
3 |
ZTEX |
/**
|
| 27 |
|
|
* Provides methods for uploading firmware to Cypress EZ-USB devices.
|
| 28 |
|
|
*/
|
| 29 |
2 |
ZTEX |
public class EzUsb {
|
| 30 |
|
|
// ******* reset **************************************************************
|
| 31 |
3 |
ZTEX |
/**
|
| 32 |
|
|
* Controls the reset state of a Cypress EZ-USB device.
|
| 33 |
|
|
* @param handle The handle of the device.
|
| 34 |
|
|
* @param r The reset state (true means reset).
|
| 35 |
|
|
* @throws FirmwareUploadException if an error occurred while attempting to control the reset state.
|
| 36 |
|
|
*/
|
| 37 |
2 |
ZTEX |
public static void reset ( int handle, boolean r ) throws FirmwareUploadException {
|
| 38 |
|
|
byte buffer[] = { (byte) (r ? 1 : 0) };
|
| 39 |
|
|
int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, 0xE600, 0, buffer, 1, 1000); // upload j bytes
|
| 40 |
|
|
if ( k<0 )
|
| 41 |
|
|
throw new FirmwareUploadException( LibusbJava.usb_strerror() + ": unable to set reset="+buffer[0] );
|
| 42 |
|
|
else if ( k!=1 )
|
| 43 |
|
|
throw new FirmwareUploadException( "Unable to set reset="+buffer[0] );
|
| 44 |
|
|
try {
|
| 45 |
3 |
ZTEX |
Thread.sleep( r ? 50 : 400 ); // give the firmware some time for initialization
|
| 46 |
2 |
ZTEX |
}
|
| 47 |
|
|
catch ( InterruptedException e ) {
|
| 48 |
|
|
}
|
| 49 |
|
|
}
|
| 50 |
|
|
|
| 51 |
|
|
// ******* uploadFirmware ******************************************************
|
| 52 |
3 |
ZTEX |
/**
|
| 53 |
|
|
* Uploads the Firmware to a Cypress EZ-USB device.
|
| 54 |
|
|
* @param handle The handle of the device.
|
| 55 |
|
|
* @param ihxFile The firmware image.
|
| 56 |
|
|
* @return The upload time in ms.
|
| 57 |
|
|
* @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
|
| 58 |
|
|
*/
|
| 59 |
2 |
ZTEX |
public static long uploadFirmware (int handle, IhxFile ihxFile ) throws FirmwareUploadException {
|
| 60 |
|
|
final int transactionBytes = 256;
|
| 61 |
|
|
byte[] buffer = new byte[transactionBytes];
|
| 62 |
|
|
|
| 63 |
|
|
reset( handle, true ); // reset = 1
|
| 64 |
|
|
|
| 65 |
3 |
ZTEX |
long t0 = new Date().getTime();
|
| 66 |
2 |
ZTEX |
int j = 0;
|
| 67 |
|
|
for ( int i=0; i<=ihxFile.ihxData.length; i++ ) {
|
| 68 |
|
|
if ( i >= ihxFile.ihxData.length || ihxFile.ihxData[i] < 0 || j >=transactionBytes ) {
|
| 69 |
|
|
if ( j > 0 ) {
|
| 70 |
|
|
int k = LibusbJava.usb_control_msg(handle, 0x40, 0xA0, i-j, 0, buffer, j, 1000); // upload j bytes
|
| 71 |
|
|
if ( k<0 )
|
| 72 |
|
|
throw new FirmwareUploadException(LibusbJava.usb_strerror());
|
| 73 |
|
|
else if ( k!=j )
|
| 74 |
|
|
throw new FirmwareUploadException();
|
| 75 |
|
|
try {
|
| 76 |
|
|
Thread.sleep( 1 ); // to avoid package loss
|
| 77 |
|
|
}
|
| 78 |
|
|
catch ( InterruptedException e ) {
|
| 79 |
|
|
}
|
| 80 |
|
|
}
|
| 81 |
|
|
j = 0;
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
if ( i < ihxFile.ihxData.length && ihxFile.ihxData[i] >= 0 && ihxFile.ihxData[i] <= 255 ) {
|
| 85 |
|
|
buffer[j] = (byte) ihxFile.ihxData[i];
|
| 86 |
|
|
j+=1;
|
| 87 |
|
|
}
|
| 88 |
|
|
}
|
| 89 |
3 |
ZTEX |
long t1 = new Date().getTime();
|
| 90 |
2 |
ZTEX |
|
| 91 |
|
|
try {
|
| 92 |
|
|
EzUsb.reset(handle,false); // error (may caused re-numeration) can be ignored
|
| 93 |
|
|
}
|
| 94 |
|
|
catch ( FirmwareUploadException e ) {
|
| 95 |
|
|
}
|
| 96 |
3 |
ZTEX |
return t1 - t0;
|
| 97 |
2 |
ZTEX |
}
|
| 98 |
|
|
}
|
| 99 |
|
|
|