1 |
2 |
ZTEX |
/*!
|
2 |
9 |
ZTEX |
Java host software API of ZTEX SDK
|
3 |
|
|
Copyright (C) 2009-2014 ZTEX GmbH.
|
4 |
2 |
ZTEX |
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 |
|
|
/**
|
27 |
|
|
* Provides methods for uploading firmware to Cypress EZ-USB devices.
|
28 |
|
|
*/
|
29 |
|
|
public class EzUsb {
|
30 |
|
|
// ******* reset **************************************************************
|
31 |
|
|
/**
|
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 |
5 |
ZTEX |
public static void reset ( long handle, boolean r ) throws FirmwareUploadException {
|
38 |
2 |
ZTEX |
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 |
|
|
Thread.sleep( r ? 50 : 400 ); // give the firmware some time for initialization
|
46 |
|
|
}
|
47 |
|
|
catch ( InterruptedException e ) {
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
// ******* uploadFirmware ******************************************************
|
52 |
|
|
/**
|
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 |
3 |
ZTEX |
* @return the upload time in ms.
|
57 |
2 |
ZTEX |
* @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
|
58 |
|
|
*/
|
59 |
5 |
ZTEX |
public static long uploadFirmware (long handle, IhxFile ihxFile ) throws FirmwareUploadException {
|
60 |
2 |
ZTEX |
final int transactionBytes = 256;
|
61 |
|
|
byte[] buffer = new byte[transactionBytes];
|
62 |
|
|
|
63 |
|
|
reset( handle, true ); // reset = 1
|
64 |
|
|
|
65 |
|
|
long t0 = new Date().getTime();
|
66 |
|
|
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 |
|
|
long t1 = new Date().getTime();
|
90 |
|
|
|
91 |
|
|
try {
|
92 |
|
|
EzUsb.reset(handle,false); // error (may caused re-numeration) can be ignored
|
93 |
|
|
}
|
94 |
|
|
catch ( FirmwareUploadException e ) {
|
95 |
|
|
}
|
96 |
|
|
return t1 - t0;
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
|