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

Subversion Repositories usb_fpga_1_11

[/] [usb_fpga_1_11/] [trunk/] [java/] [ztex/] [Ztex1.java] - Blame information for rev 7

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

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

powered by: WebSVN 2.1.0

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