OpenCores
URL https://opencores.org/ocsvn/usb_fpga_1_2/usb_fpga_1_2/trunk

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [java/] [ztex/] [Ztex1.java] - Blame information for rev 8

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2 8 ZTEX
   Java host software API of ZTEX EZ-USB FX2 SDK
3
   Copyright (C) 2009-2011 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 3 ZTEX
/*
20 2 ZTEX
    Functions for USB devices with ZTEX descriptor 1
21
*/
22
package ztex;
23
 
24
import java.io.*;
25
import java.util.*;
26
 
27
import ch.ntb.usb.*;
28
 
29 3 ZTEX
/**
30
  * This class implements the interface-independent part of the communication protocol for the interaction with the ZTEX firmware.<p>
31
  * All firmware implementations that provide the ZTEX descriptor 1 are supported.
32
  * A description of this descriptor can be found in {@link ZtexDevice1}.
33
  * <p>
34
  * The most important features of this class are the functions for uploading the firmware
35
  * and the renumeration management.
36
  * <p>
37
  * The interface dependent part of the communication protocol (currently only one is supported)
38
  * can be found in {@link Ztex1v1}.
39
  * @see ZtexDevice1
40
  * @see Ztex1v1
41
  */
42 2 ZTEX
public class Ztex1 {
43 8 ZTEX
    private final int maxDevNum = 1023;
44
    private long handle;
45 3 ZTEX
    private ZtexDevice1 dev = null;
46 8 ZTEX
    private boolean oldDevices[] = new boolean[maxDevNum+1];
47 2 ZTEX
    private String usbBusName = null;
48 8 ZTEX
    private boolean[] interfaceClaimed = new boolean[256];
49 4 ZTEX
/** * Setting to true enables certain workarounds, e.g. to deal with bad driver/OS implementations. */
50 3 ZTEX
    public boolean certainWorkarounds = false;
51
/** * The timeout for  control messages in ms. */
52
    public int controlMsgTimeout = 1000;        // in ms
53
    private long lastVendorCommandT = 0;
54 8 ZTEX
 
55
 
56 2 ZTEX
 
57
// ******* Ztex1 ***************************************************************
58 3 ZTEX
/**
59
  * Constructs an instance from a given device.
60
  * @param pDev The given device.
61
  * @throws UsbException if an communication error occurred.
62
  */
63 2 ZTEX
    public Ztex1 ( ZtexDevice1 pDev ) throws UsbException {
64
        dev = pDev;
65 8 ZTEX
 
66
        for (int i=0; i<256; i++)
67
            interfaceClaimed[i] = false;
68 2 ZTEX
 
69
        handle = LibusbJava.usb_open(dev.dev());
70 4 ZTEX
//      if ( handle<=0 ) 
71
//          throw new UsbException(dev.dev(), "Error opening device");
72 2 ZTEX
    }
73
 
74
// ******* finalize ************************************************************
75 3 ZTEX
/** * The destructor closes the USB file handle. */
76 2 ZTEX
    protected void finalize () {
77 8 ZTEX
        for (int i=0; i<256; i++)
78
            if ( interfaceClaimed[i] )
79
                LibusbJava.usb_release_interface(handle, i);
80
 
81 2 ZTEX
        LibusbJava.usb_close(handle);
82
    }
83
 
84
// ******* handle **************************************************************
85 3 ZTEX
/** * Returns the USB file handle. */
86 8 ZTEX
    public final long handle()
87 2 ZTEX
    {
88
        return handle;
89
    }
90
 
91
// ******* dev *****************************************************************
92 3 ZTEX
/**
93
  * Returns the corresponding {@link ZtexDevice1}.
94
  * @return the corresponding {@link ZtexDevice1}.
95
  */
96 2 ZTEX
    public final ZtexDevice1 dev()
97
    {
98
        return dev;
99
    }
100
 
101
// ******* valid ***************************************************************
102 3 ZTEX
/**
103
  * Returns true if ZTEX descriptor 1 is available.
104
  * @return true if ZTEX descriptor 1 is available.
105
  */
106 2 ZTEX
    public boolean valid ( ) {
107
        return dev.valid();
108
    }
109
 
110
// ******* checkValid **********************************************************
111 3 ZTEX
/**
112
  * Checks whether ZTEX descriptor 1 is available.
113
  * @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
114
  */
115 2 ZTEX
    public void checkValid () throws InvalidFirmwareException {
116
        if ( ! dev.valid() )
117
            throw new InvalidFirmwareException(this, "Can't read ZTEX descriptor 1");
118
    }
119
 
120
// ******* vendorCommand *******************************************************
121 3 ZTEX
/**
122
  * Sends a vendor command to Endpoint 0 of the EZ-USB device.
123
  * The command may be send multiple times until the {@link #controlMsgTimeout} is reached.
124
  * @param cmd The command number (0..255).
125
  * @param func The name of the command. This string is used for the generation of error messages.
126
  * @param value The value (0..65535), i.e bytes 2 and 3 of the setup data.
127
  * @param index The index (0..65535), i.e. bytes 4 and 5 of the setup data.
128
  * @param length The size of the payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
129
  * @param buf The payload data buffer.
130
  * @return the number of bytes sent.
131
  * @throws UsbException if a communication error occurs.
132
  */
133
    public synchronized int vendorCommand (int cmd, String func, int value, int index, byte[] buf, int length) throws UsbException {
134
        long t0 = new Date().getTime()-100;
135
        int trynum = 0;
136
        int i = -1;
137
        if ( controlMsgTimeout < 200 )
138
            controlMsgTimeout = 200;
139 5 ZTEX
//      while ( i<=0 && new Date().getTime()-t0<controlMsgTimeout ) {           // we repeat the message until the timeout has reached
140 3 ZTEX
            i = LibusbJava.usb_control_msg(handle, 0x40, cmd, value, index, buf, length, controlMsgTimeout);
141
            if ( certainWorkarounds ) {
142
                try {
143
                    Thread.sleep(2);
144
                }
145
                    catch ( InterruptedException e ) {
146
                }
147
            }
148
            lastVendorCommandT = new Date().getTime();
149
            if ( i < 0 ) {
150
                System.err.println("Warning (try " + (trynum+1) + "): " + LibusbJava.usb_strerror() );
151
                try {
152
                    Thread.sleep( 1 << trynum );                                // we don't want to bother the USB device to often
153
                }
154
                    catch ( InterruptedException e ) {
155
                }
156
                trynum++;
157
            }
158 5 ZTEX
//      } 
159 3 ZTEX
        if ( i < 0 )
160
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" )+ LibusbJava.usb_strerror());
161 2 ZTEX
        return i;
162
    }
163
 
164 3 ZTEX
/**
165
  * Sends a vendor command with no payload data to Endpoint 0 of the EZ-USB device.
166
  * The command may be send multiple times until the {@link #controlMsgTimeout} is reached.
167
  * @param cmd The command number (0..255).
168
  * @param func The name of the command. This string is used for the generation of error messages.
169
  * @param value The value (0..65535), i.e bytes 2 and 3 of the setup data.
170
  * @param index The index (0..65535), i.e. bytes 4 and 5 of the setup data.
171
  * @return the number of bytes sent.
172
  * @throws UsbException if a communication error occurs.
173
  */
174 2 ZTEX
    public int vendorCommand (int cmd, String func, int value, int index) throws UsbException {
175
        byte[] buf = { 0 };
176
        return vendorCommand (cmd, func, value, index, buf, 0);
177
    }
178
 
179 3 ZTEX
/**
180
  * Sends a vendor command with no payload data and no setup data to Endpoint 0 of the EZ-USB device.
181
  * The command may be send multiple times until the {@link #controlMsgTimeout} is reached.
182
  * @param cmd The command number (0..255).
183
  * @param func The name of the command. This string is used for the generation of error messages.
184
  * @return the number of bytes sent.
185
  * @throws UsbException if a communication error occurs.
186
  */
187 2 ZTEX
    public int vendorCommand (int cmd, String func) throws UsbException {
188
        byte[] buf = { 0 };
189
        return vendorCommand (cmd, func, 0, 0, buf, 0);
190
    }
191
 
192
// ******* vendorRequest *******************************************************
193 3 ZTEX
/**
194
  * Sends a vendor request to Endpoint 0 of the EZ-USB device.
195
  * The request may be send multiple times until the {@link #controlMsgTimeout} is reached.
196
  * @param cmd The request number (0..255).
197
  * @param func The name of the request. This string is used for the generation of error messages.
198
  * @param value The value (0..65535), i.e bytes 2 and 3 of the setup data.
199
  * @param index The index (0..65535), i.e. bytes 4 and 5 of the setup data.
200
  * @param maxlen The size of the requested payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
201
  * @param buf The payload data buffer.
202
  * @return the number of bytes received.
203
  * @throws UsbException if a communication error occurs.
204
  */
205
    public synchronized int vendorRequest (int cmd, String func, int value, int index, byte[] buf, int maxlen) throws UsbException {
206
        long t0 = new Date().getTime()-100;
207
        int trynum = 0;
208
        int i = -1;
209
        if ( controlMsgTimeout < 200 )
210
            controlMsgTimeout = 200;
211
        while ( i<=0 && new Date().getTime()-t0<controlMsgTimeout ) {            // we repeat the message until the timeout has reached
212
            /*
213
                The HSNAK mechanism of EP0 usually avoids that a request is sent before a command has been completed.
214
                Unfortunately this mechanism is only 99.99% reliable. Therefore we wait at least 1ms after the last
215
                command has been send before we transmit a new request.
216
            */
217
            long ms = new Date().getTime() - lastVendorCommandT;
218
            if ( ms < 2 ) {     //
219
                try {
220
                    Thread.sleep(1);
221
                }
222
                    catch ( InterruptedException e ) {
223
                }
224
            }
225
 
226
            i = LibusbJava.usb_control_msg(handle, 0xc0, cmd, value, index, buf, maxlen, controlMsgTimeout);
227
            if ( certainWorkarounds ) {
228
                try {
229
                    Thread.sleep(2);
230
                }
231
                    catch ( InterruptedException e ) {
232
                }
233
            }
234
            if ( i < 0 ) {
235
                System.err.println("Warning (try " + (trynum+1) + "): " + LibusbJava.usb_strerror() );
236
                try {
237
                    Thread.sleep( 1 << trynum );                                // we don't want to bother the USB device to often
238
                }
239
                    catch ( InterruptedException e ) {
240
                }
241
                trynum++;
242
            }
243
        }
244 2 ZTEX
        if ( i < 0 )
245
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + LibusbJava.usb_strerror());
246
        return i;
247
    }
248
 
249 3 ZTEX
/**
250
  * Sends a vendor request to Endpoint 0 of the EZ-USB device.
251
  * The request may be send multiple times until the {@link #controlMsgTimeout} is reached.
252
  * @param cmd The request number (0..255).
253
  * @param func The name of the request. This string is used for the generation of error messages.
254
  * @param maxlen The size of the requested payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
255
  * @param buf The payload data buffer.
256
  * @return the number of bytes sent.
257
  * @throws UsbException if a communication error occurs.
258
  */
259 2 ZTEX
    public int vendorRequest (int cmd, String func, byte[] buf, int maxlen) throws UsbException {
260
        return vendorRequest (cmd, func, 0, 0, buf, maxlen);
261
    }
262
 
263
// ******* vendorCommand2 ******************************************************
264 3 ZTEX
/**
265
  * Sends a vendor command to Endpoint 0 of the EZ-USB device and throws an {@link UsbException} if not all of the payload has been sent.
266
  * The command may be send multiple times until the {@link #controlMsgTimeout} is reached.
267
  * @param cmd The command number (0..255).
268
  * @param func The name of the command. This string is used for the generation of error messages.
269
  * @param value The value (0..65535), i.e bytes 2 and 3 of the setup data.
270
  * @param index The index (0..65535), i.e. bytes 4 and 5 of the setup data.
271
  * @param length The size of the payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
272
  * @param buf The payload data buffer.
273
  * @throws UsbException if a communication error occurs or if not all of the payload has been sent.
274
  */
275
    public synchronized void vendorCommand2 (int cmd, String func, int value, int index, byte[] buf, int length) throws UsbException {
276 2 ZTEX
        int i = vendorCommand (cmd, func, value, index, buf, length);
277
        if ( i != length )
278
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + "Send " + i + " byte of data instead of " + length + " bytes");
279
    }
280
 
281
// ******* vendorRequest2 ******************************************************
282 3 ZTEX
/**
283
  * Sends a vendor request to Endpoint 0 of the EZ-USB device and throws an {@link UsbException} if not all of the payload has been received.
284
  * The request may be send multiple times until the {@link #controlMsgTimeout} is reached.
285
  * @param cmd The request number (0..255).
286
  * @param func The name of the request. This string is used for the generation of error messages.
287
  * @param value The value (0..65535), i.e bytes 2 and 3 of the setup data.
288
  * @param index The index (0..65535), i.e. bytes 4 and 5 of the setup data.
289
  * @param maxlen The size of the requested payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
290
  * @param buf The payload data buffer.
291
  * @throws UsbException if a communication error occurs or not all of the payload has been received.
292
  */
293 2 ZTEX
    public void vendorRequest2 (int cmd, String func, int value, int index, byte[] buf, int maxlen) throws UsbException {
294
        int i = vendorRequest(cmd, func, value, index, buf, maxlen);
295
        if ( i != maxlen )
296
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + "Received " + i + " byte of data, expected "+maxlen+" bytes");
297
    }
298
 
299 3 ZTEX
/**
300
  * Sends a vendor request to Endpoint 0 of the EZ-USB device and throws an {@link UsbException} if not all of the payload has been received.
301
  * The request may be send multiple times until the {@link #controlMsgTimeout} is reached.
302
  * @param cmd The request number (0..255).
303
  * @param func The name of the request. This string is used for the generation of error messages.
304
  * @param maxlen The size of the requested payload data (0..65535), i.e. bytes 6 and 7 of the setup data.
305
  * @param buf The payload data buffer.
306
  * @throws UsbException if a communication error occurs or not all of the payload has been received.
307
  */
308 2 ZTEX
    public void vendorRequest2 (int cmd, String func, byte[] buf, int maxlen) throws UsbException {
309
        vendorRequest2(cmd, func, 0, 0, buf, maxlen);
310
    }
311
 
312 4 ZTEX
 
313
 
314
// ******* setConfiguration ****************************************************
315
/**
316
  * Sets the configuration.
317
  * @param config The configuration number (usually 1)
318
  * @throws UsbException if an error occurs while attempting to set the configuration.
319
  */
320
    public void setConfiguration ( int config) throws UsbException{
321
        if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
322
            throw new UsbException("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
323
    }
324
 
325
 
326
// ******* trySetConfiguration ****************************************************
327
/**
328
  * Tries to set the configuration.
329
  * If an error occurs while attempting to set the configuration, a warning messaage is printed to stderr.
330
  * @param config The configuration number (usually 1)
331
  */
332
    public void trySetConfiguration ( int config) {
333
        if ( LibusbJava.usb_set_configuration(handle(), config) < 0 )
334
            System.err.println("Setting configuration to " + config + " failed: " + LibusbJava.usb_strerror());
335
    }
336
 
337
 
338 8 ZTEX
// ******* getInterfaceClaimed *************************************************
339
/**
340
  * Returns true if interface is claimed.
341
  * @return true if interface is claimed
342
  * @param iface The interface number
343
  */
344
    public boolean getInterfaceClaimed ( int iface ) {
345
        return iface>=0 && iface<256 && interfaceClaimed[iface];
346
    }
347
 
348
 
349 4 ZTEX
// ******* claimInterface ******************************************************
350
/**
351
  * Claims an interface.
352
  * @param iface The interface number (usually 0)
353
  * @throws UsbException if an error occurs while attempting to claim the interface.
354
  */
355
    public void claimInterface ( int iface) throws UsbException{
356 8 ZTEX
        if ( ( iface<0 || iface>=256 || (! interfaceClaimed[iface]) ) && ( LibusbJava.usb_claim_interface(handle(), iface) < 0 ) )
357 4 ZTEX
            throw new UsbException("Claiming interface " + iface + " failed: " + LibusbJava.usb_strerror());
358 8 ZTEX
        if ( iface>=0 && iface < 256 )
359
            interfaceClaimed[iface]=true;
360 4 ZTEX
    }
361
 
362
 
363
// ******* releaseInterface ****************************************************
364
/**
365
  * Releases an interface.
366
  * @param iface The interface number (usually 0)
367
  */
368
    public void releaseInterface ( int iface ) {
369 8 ZTEX
        if ( iface<0 || iface>=256 || interfaceClaimed[iface] )
370
            LibusbJava.usb_release_interface(handle(), iface);
371
        if ( iface>=0 && iface < 256 )
372
            interfaceClaimed[iface]=false;
373
 
374 4 ZTEX
    }
375
 
376
 
377 2 ZTEX
// ******* findOldDevices ******************************************************
378 8 ZTEX
    private synchronized void findOldDevices () throws DeviceLostException {
379 2 ZTEX
        Usb_Bus bus = dev.dev().getBus();
380
        usbBusName = bus.getDirname();
381
 
382 8 ZTEX
        for ( int i=0; i<=maxDevNum; i++ )
383 2 ZTEX
            oldDevices[i] = false;
384
 
385
        Usb_Device d = bus.getDevices();
386
        while ( d != null ) {
387
            byte b = d.getDevnum();
388 8 ZTEX
            if ( b > maxDevNum )
389
                throw new DeviceLostException( "Device number too large: " + b + " > " + maxDevNum );
390 2 ZTEX
            if ( b > 0 )
391
                oldDevices[b] = true;
392
            d = d.getNext();
393
        }
394
        oldDevices[dev.dev().getDevnum()] = false;
395
    }
396
 
397
// ******* findNewDevice *******************************************************
398 3 ZTEX
    private synchronized Usb_Device findNewDevice ( String errMsg ) throws DeviceLostException {
399 2 ZTEX
        Usb_Device newDev = null;
400
        LibusbJava.usb_find_busses();
401
        LibusbJava.usb_find_devices();
402
 
403
        Usb_Bus bus = LibusbJava.usb_get_busses();
404
        while ( bus != null && ! bus.getDirname().equals(usbBusName) )
405
            bus = bus.getNext();
406
 
407
        Usb_Device d = bus != null ? bus.getDevices() : null;
408
        while ( d != null ) {
409
            byte b = d.getDevnum();
410 8 ZTEX
            if ( b > maxDevNum )
411
                throw new DeviceLostException( "Device number too large: " + b + " > " + maxDevNum );
412 2 ZTEX
            if ( b > 0 && ! oldDevices[b] ) {
413
                if ( newDev != null )
414 8 ZTEX
                    throw new DeviceLostException( errMsg + "More than 2 new devices found: " + newDev.getDevnum() + "(`" + newDev.getFilename() + "') and " + b + "(`" + d.getFilename() + "')");
415 2 ZTEX
                newDev = d;
416
            }
417
            d = d.getNext();
418
        }
419
 
420
        return newDev;
421
    }
422
 
423
// ******* initNewDevice *******************************************************
424 8 ZTEX
    private void initNewDevice ( String errBase, boolean scanUnconfigured ) throws DeviceLostException, UsbException, InvalidFirmwareException {
425 3 ZTEX
// scan the bus for up to 60 s for a new device. Boot sequence may take a while.
426 2 ZTEX
        Usb_Device newDev = null;
427 3 ZTEX
        for ( int i=0; i<300 && newDev==null; i++ ) {
428 2 ZTEX
            try {
429 3 ZTEX
                Thread.sleep( 200 );
430 2 ZTEX
            }
431
                catch ( InterruptedException e ) {
432
            }
433
            newDev = findNewDevice( errBase + ": " );
434
        }
435
        if ( newDev == null )
436
            throw new DeviceLostException( errBase + ": No new device found" );
437
 
438
// init new device
439
        Usb_Device_Descriptor dd = newDev.getDescriptor();
440
        int vid = dd.getIdVendor() & 65535;
441
        int pid = dd.getIdProduct() & 65535;
442
        try {
443 8 ZTEX
            dev = new ZtexDevice1( newDev, vid, pid, scanUnconfigured );
444 2 ZTEX
        }
445 8 ZTEX
        catch ( DeviceNotSupportedException e ) {
446
            throw new InvalidFirmwareException( e.getLocalizedMessage() );
447 2 ZTEX
        }
448 8 ZTEX
 
449 2 ZTEX
        handle = LibusbJava.usb_open( dev.dev() );
450
    }
451
 
452
// ******* uploadFirmware ******************************************************
453 3 ZTEX
/**
454
  * Uploads the firmware to the EZ-USB and manages the renumeration process.
455
  * <p>
456
  * Before the firmware is uploaded the device is set into a reset state.
457
  * After the upload the firmware is booted and the renumeration starts.
458
  * During this process the device disappears from the bus and a new one
459
  * occurs which will be assigned to this class automatically (instead of the disappeared one).
460 8 ZTEX
  * @param ihxFile The firmware image.
461 3 ZTEX
  * @param force The compatibility check is skipped if true.
462
  * @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 <tt>force</tt> parameter)
463
  * @throws FirmwareUploadException If an error occurred while attempting to upload the firmware.
464
  * @throws UsbException if a communication error occurs.
465
  * @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
466
  * @throws DeviceLostException if a device went lost after renumeration.
467 5 ZTEX
  * @return the upload time in ms.
468 3 ZTEX
  */
469 2 ZTEX
//  returns upload time in ms
470 8 ZTEX
    public long uploadFirmware ( ZtexIhxFile1 ihxFile, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
471 2 ZTEX
// load the ihx file
472
//      ihxFile.dataInfo(System.out);
473
//      System.out.println(ihxFile);
474
 
475
// check for compatibility
476
        if ( ! force && dev.valid() ) {
477
            if ( ihxFile.interfaceVersion() != 1 )
478
                throw new IncompatibleFirmwareException("Wrong interface version: Expected 1, got " + ihxFile.interfaceVersion() );
479
 
480
            if ( ! dev.compatible ( ihxFile.productId(0), ihxFile.productId(1), ihxFile.productId(2), ihxFile.productId(3) ) )
481
                throw new IncompatibleFirmwareException("Incompatible productId's: Current firmware: " + ZtexDevice1.byteArrayString(dev.productId())
482
                    + "  Ihx File: " + ZtexDevice1.byteArrayString(ihxFile.productId()) );
483
        }
484
 
485
// scan the bus for comparison
486
        findOldDevices();
487
 
488
// upload the firmware
489
        long time = EzUsb.uploadFirmware( handle, ihxFile );
490
 
491
// find and init new device
492 8 ZTEX
        initNewDevice("Device lost after uploading Firmware", false);
493 2 ZTEX
 
494
        return time;
495
    }
496
 
497 8 ZTEX
/**
498
  * Uploads the firmware to the EZ-USB and manages the renumeration process.
499
  * <p>
500
  * Before the firmware is uploaded the device is set into a reset state.
501
  * After the upload the firmware is booted and the renumeration starts.
502
  * During this process the device disappears from the bus and a new one
503
  * occurs which will be assigned to this class automatically (instead of the disappeared one).
504
  * @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).
505
  * @param force The compatibility check is skipped if true.
506
  * @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 <tt>force</tt> parameter)
507
  * @throws FirmwareUploadException If an error occurred while attempting to upload the firmware.
508
  * @throws UsbException if a communication error occurs.
509
  * @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
510
  * @throws DeviceLostException if a device went lost after renumeration.
511
  * @return the upload time in ms.
512
  */
513
//  returns upload time in ms
514
    public long uploadFirmware ( String ihxFileName, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
515
// load the ihx file
516
        ZtexIhxFile1 ihxFile;
517
        try {
518
            ihxFile = new ZtexIhxFile1( ihxFileName );
519
        }
520
        catch ( IOException e ) {
521
            throw new FirmwareUploadException( e.getLocalizedMessage() );
522
        }
523
        catch ( IhxFileDamagedException e ) {
524
            throw new FirmwareUploadException( e.getLocalizedMessage() );
525
        }
526
        return uploadFirmware( ihxFile, force );
527
    }
528
 
529 2 ZTEX
// ******* resetEzUsb **********************************************************
530 3 ZTEX
/**
531
  * Resets the EZ-USB and manages the renumeration process.
532
  * <p>
533
  * After the reset the renumeration starts.
534
  * During this process the device disappears from the bus and a new one
535
  * occurs which will be assigned to this class automatically (instead of the disappeared one).
536
  * @throws FirmwareUploadException If an error occurred while attempting to upload the firmware.
537
  * @throws UsbException if a communication error occurs.
538
  * @throws InvalidFirmwareException if ZTEX descriptor 1 is not available.
539
  * @throws DeviceLostException if a device went lost after renumeration.
540
  */
541
    public void resetEzUsb () throws FirmwareUploadException, UsbException, InvalidFirmwareException, DeviceLostException {
542 2 ZTEX
// scan the bus for comparison
543
        findOldDevices();
544
 
545
// reset the EZ-USB
546
        EzUsb.reset(handle,true);
547
        try {
548 3 ZTEX
            EzUsb.reset(handle,false);          // error (may caused by re-numeration) can be ignored
549 2 ZTEX
        }
550
        catch ( FirmwareUploadException e ) {
551
        }
552
 
553
// find and init new device
554 8 ZTEX
        initNewDevice( "Device lost after resetting the EZ-USB", true );
555 2 ZTEX
    }
556
 
557
// ******* toString ************************************************************
558 3 ZTEX
/**
559
  * Returns a lot of useful information about the corresponding device.
560
  * @return a lot of useful information about the corresponding device.
561
  */
562 2 ZTEX
    public String toString () {
563
        return dev.toString();
564
    }
565
 
566
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.