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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [java/] [ztex/] [Ztex1.java] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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