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/] [ZtexDevice1.java] - Blame information for rev 6

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
    USB device with ZTEX descriptor 1 and/or Cypress EZ-USB FX2 device
21
*/
22
package ztex;
23
 
24
import java.io.*;
25
import java.util.*;
26
 
27
import ch.ntb.usb.*;
28
 
29
/**
30
  * A class representing an EZ-USB device that supports the ZTEX descriptor 1 or an unconfigured EZ-USB device.<br>
31
  * Instances of this class are usually created by {@link ZtexScanBus1}.
32
  * The following table describes the ZTEX descriptor 1.
33
  * <a name="descriptor"></a>
34
  * <table bgcolor="#404040" cellspacing=1 cellpadding=4>
35
  *   <tr>
36
  *     <td bgcolor="#d0d0d0" valign="top"><b>Field name</b></td>
37
  *     <td bgcolor="#d0d0d0" valign="top"><b>Offset</b></td>
38
  *     <td bgcolor="#d0d0d0" valign="top"><b>Size</b></td>
39
  *     <td bgcolor="#d0d0d0" valign="top"><b>Description</b></td>
40
  *   </tr>
41
  *   <tr>
42
  *     <td bgcolor="#ffffff" valign="top">ZTEX_DESCRIPTOR_SIZE</td>
43
  *     <td bgcolor="#ffffff" valign="top">0</td>
44
  *     <td bgcolor="#ffffff" valign="top">1</td>
45
  *     <td bgcolor="#ffffff" valign="top">Size of the descriptor in bytes; must be 40 for descriptor version 1</td>
46
  *   </tr>
47
  *   <tr>
48
  *     <td bgcolor="#ffffff" valign="top">ZTEX_DESCRIPTOR_VERSION</td>
49
  *     <td bgcolor="#ffffff" valign="top">1</td>
50
  *     <td bgcolor="#ffffff" valign="top">1</td>
51
  *     <td bgcolor="#ffffff" valign="top">Descriptor version; 1 for version 1</td>
52
  *   </tr>
53
  *   <tr>
54
  *     <td bgcolor="#ffffff" valign="top">ZTEXID</td>
55
  *     <td bgcolor="#ffffff" valign="top">2</td>
56
  *     <td bgcolor="#ffffff" valign="top">4</td>
57
  *     <td bgcolor="#ffffff" valign="top">ID; must be "ZTEX"</td>
58
  *   </tr>
59
  *   <tr>
60
  *     <td bgcolor="#ffffff" valign="top">PRODUCT_ID</td>
61
  *     <td bgcolor="#ffffff" valign="top">6</td>
62
  *     <td bgcolor="#ffffff" valign="top">4</td>
63
  *     <td bgcolor="#ffffff" valign="top">Four numbers (0..255) representing the product ID and firmware compatibility information.<br>
64
  *         A firmware can overwrite an installed one<br>
65
  *        <pre>if ( INSTALLED.PRODUCTID[0]==0 || PRODUCTID[0]==0 || INSTALLED.PRODUCTID[0]==PRODUCTID[0] ) &&
66
   ( INSTALLED.PRODUCTID[1]==0 || PRODUCTID[1]==0 || INSTALLED.PRODUCTID[1]==PRODUCTID[1] ) &&
67
   ( INSTALLED.PRODUCTID[2]==0 || PRODUCTID[2]==0 || INSTALLED.PRODUCTID[2]==PRODUCTID[2] ) &&
68
   ( INSTALLED.PRODUCTID[3]==0 || PRODUCTID[3]==0 || INSTALLED.PRODUCTID[3]==PRODUCTID[3] ) </pre>
69
  *       Here is a list of the preserved product ID's:
70
  *       <table><tr><td>&nbsp</td><td>
71
  *         <table>
72
  *          <tr><td>0.0.0.0</td> <td>default Product ID (no product specified)</td></tr>
73
  *           <tr><td>1.*.*.*</td> <td>may be used for experimental purposes</td></tr>
74
  *           <tr><td>10.*.*.*</td> <td>used for ZTEX products</td></tr>
75
  *           <tr><td>10.11.*.*</td> <td><a href="http://www.ztex.de/usb-fpga-1/usb-fpga-1.2.e.html">ZTEX USB-FPGA-Module 1.2</a></td></tr>
76
  *           <tr><td>10.20.*.*</td> <td><a href="http://www.ztex.de/usb-1/usb-1.0.e.html">ZTEX USB-Module 1.0</a></td></tr>
77
  *         </table></td></tr></table>
78
  *         Please contact me (<a href="http://www.ztex.de/contact.e.html">http://www.ztex.de/contact.e.html</a>) if you want to register or reserve a Product ID (range).
79
  *       </td>
80
  *   </tr>
81
  *   <tr>
82
  *     <td bgcolor="#ffffff" valign="top">FW_VERSION</td>
83
  *     <td bgcolor="#ffffff" valign="top">10</td>
84
  *     <td bgcolor="#ffffff" valign="top">1</td>
85
  *     <td bgcolor="#ffffff" valign="top">May be used to specify the firmware version.</td>
86
  *   </tr>
87
  *   <tr>
88
  *     <td bgcolor="#ffffff" valign="top">INTERFACE_VERSION</td>
89
  *     <td bgcolor="#ffffff" valign="top">11</td>
90
  *     <td bgcolor="#ffffff" valign="top">1</td>
91
  *     <td bgcolor="#ffffff" valign="top">The interface version. This number specifies the protocol that is used for interfacing the host software. A description of interface version 1 can be found in {@link Ztex1v1} </td>
92
  *   </tr>
93
  *   <tr>
94
  *     <td bgcolor="#ffffff" valign="top">INTERFACE_CAPABILITIES</td>
95
  *     <td bgcolor="#ffffff" valign="top">12</td>
96
  *     <td bgcolor="#ffffff" valign="top">6</td>
97
  *     <td bgcolor="#ffffff" valign="top">6 bytes, each bit represents a capability. If set, the capability is supported. A description of the capabilities of interface version 1 can be found in {@link Ztex1v1} </td>
98
  *   </tr>
99
  *   <tr>
100
  *     <td bgcolor="#ffffff" valign="top">MODULE_RESERVED</td>
101
  *     <td bgcolor="#ffffff" valign="top">18</td>
102
  *     <td bgcolor="#ffffff" valign="top">12</td>
103
  *     <td bgcolor="#ffffff" valign="top">12 bytes for application specific use, i.e. they depend from the PRODUCT_ID </td>
104
  *   </tr>
105
  *   <tr>
106
  *     <td bgcolor="#ffffff" valign="top">SN_STRING</td>
107
  *     <td bgcolor="#ffffff" valign="top">30</td>
108
  *     <td bgcolor="#ffffff" valign="top">10</td>
109
  *     <td bgcolor="#ffffff" valign="top">A serial number string of 10 characters. The default SN is "0000000000"</td> </td>
110
  *   </tr>
111
  * </table>
112
  * @see Ztex1
113
  * @see Ztex1v1
114
  * @see ZtexScanBus1
115
*/
116
 
117
public class ZtexDevice1 {
118
/** * Cypress vendor ID: 0x4b4 */
119
    public static final int cypressVendorId = 0x4b4;
120
/** * EZ-USB USB product ID: 0x8613 */
121
    public static final int cypressProductId = 0x8613;
122
 
123
/** * ZTEX vendor ID: 0x221a */
124
    public static final int ztexVendorId = 0x221A;
125
/**
126
  * USB product ID for ZTEX devices that support ZTEX descriptor 1: 0x100.
127
  * This product ID is intended for general purpose use and can be shared by all devices that base on ZTEX modules.
128
  * Different products are identified by a second product ID, namely the PRODUCT_ID field of the <a href="#descriptor"> ZTEX descriptor 1</a>.
129
  * <p>
130
  * Please read the <a href="http://www.ztex.de/firmware-kit/usb_ids.e.html">informations about USB vendor and product ID's<a>.
131
  * @see #ztexProductIdMax
132
  */
133
    public static final int ztexProductId = 0x100;
134
/**
135
  * Largest USB product ID for ZTEX devices that support ZTEX descriptor 1: 0x1ff.
136
  * USB product ID's from {@link #ztexProductId}+1 to ztexProductIdMax (0x101 to 0x1ff) are reserved for ZTEX devices and allow to identify products without reading the ZTEX descriptor.
137
  * <p>
138
  * Please read the <a href="http://www.ztex.de/firmware-kit/usb_ids.e.html">informations about USB vendor and product ID's<a>.
139
  * @see #ztexProductId
140
  */
141
    public static final int ztexProductIdMax = 0x1ff;
142
 
143
    private Usb_Device dev = null;
144
    private boolean valid = false;              // true if descriptor 1 is available
145
    private int usbVendorId = -1;
146
    private int usbProductId = -1;
147
    private String manufacturerString = null;
148
    private String productString = null;
149
    private String snString = null;
150
    private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descriptor, not the USB product ID
151
    private byte fwVersion = 0;
152
    private byte interfaceVersion = 0;
153
    private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
154
    private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
155
 
156
// ******* byteArrayString *****************************************************
157
/**
158
  * Produces a nice string representation of an array of bytes.
159
  * @param buf A byte array.
160
  * @return a nice string
161
  */
162
    public static String byteArrayString ( byte buf[] ) {
163
        String s = new String( "" );
164
        for ( int i = 0; i<buf.length; i++ ) {
165
            if ( i != 0 )
166
                s+=".";
167
            s+=buf[i] & 255;
168
        }
169
        return s;
170
    }
171
 
172
// ******* ZtexDevice1 *********************************************************
173
/**
174
  * Constructs an instance from a given USB device.<br>
175
  * If the given vendor and product id's match to the vendor and product id's of the given USB device,
176
  * the ZTEX descriptor 1 is attempted to read. If this fails, an {@link InvalidFirmwareException} is thrown.
177
  * To suppress this behavior (e.g. if the EZ-USB device is known to be unconfigured) the vendor and product id's
178
  * can be set to -1.
179
  * @param p_dev The USB device.
180
  * @param pUsbVendorId The given vendor ID.
181
  * @param pUsbProductId The given product ID.
182 5 ZTEX
  * @param allowUnconfigured If true, unconfigured devices are allowed.
183 2 ZTEX
  * @throws UsbException if an USB communication error occurs.
184
  * @throws InvalidFirmwareException if no valid ZTEX descriptor 1 is found.
185 5 ZTEX
  * @throws DeviceNotSupported if the device has the wrong USB ID's.
186 2 ZTEX
  */
187 5 ZTEX
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId, boolean allowUnconfigured) throws UsbException, InvalidFirmwareException, DeviceNotSupportedException  {
188 2 ZTEX
        dev = p_dev;
189
 
190
        Usb_Device_Descriptor dd = dev().getDescriptor();
191
        usbVendorId = dd.getIdVendor() & 65535;
192
        usbProductId = dd.getIdProduct() & 65535;
193
 
194 5 ZTEX
        if ( ! ( (
195
                   (usbVendorId == pUsbVendorId) &&
196
                   (usbProductId == pUsbProductId || ( usbVendorId == ztexVendorId && pUsbProductId<0 && usbProductId>=ztexProductId && usbProductId<ztexProductIdMax ) )
197
                 ) ||
198
                 (
199
                   allowUnconfigured && (usbVendorId == cypressVendorId) && (usbProductId == cypressProductId)
200
                 ) )  )
201
            throw new DeviceNotSupportedException(p_dev);
202 2 ZTEX
 
203 5 ZTEX
        long handle = LibusbJava.usb_open(dev);
204 2 ZTEX
 
205
//      if ( handle > 0 ) {
206
            if ( dd.getIManufacturer() > 0 )
207
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
208
            if ( dd.getIProduct() > 0 )
209
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
210
            if ( dd.getISerialNumber() > 0 )
211
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
212
 
213 5 ZTEX
            if ( snString == null ) {
214
                LibusbJava.usb_close(handle);
215
                if ( allowUnconfigured )
216
                    return;
217
                else
218 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Not a ZTEX device" );  // ZTEX devices always have a SN. See also the next comment a few lines below
219 5 ZTEX
            }
220 2 ZTEX
 
221 5 ZTEX
            byte[] buf = new byte[42];
222
            int i = LibusbJava.usb_control_msg(handle, 0xc0, 0x22, 0, 0, buf, 40, 500);   // Failing of this may cause problems under windows. Therefore we check for the SN above.
223
            if ( i < 0 ) {
224
                LibusbJava.usb_close(handle);
225
                if ( allowUnconfigured )
226
                    return;
227
                else
228 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
229 5 ZTEX
            }
230
            else if ( i != 40 ) {
231
                LibusbJava.usb_close(handle);
232
                if ( allowUnconfigured )
233
                    return;
234
                else
235 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
236 5 ZTEX
            }
237
            if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
238
                LibusbJava.usb_close(handle);
239
                if ( allowUnconfigured )
240
                    return;
241
                else
242 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Invalid ZTEX descriptor" );
243
            }
244 5 ZTEX
            productId[0] = buf[6];
245
            productId[1] = buf[7];
246
            productId[2] = buf[8];
247
            productId[3] = buf[9];
248
            fwVersion = buf[10];
249
            interfaceVersion = buf[11];
250
            interfaceCapabilities[0] = buf[12];
251
            interfaceCapabilities[1] = buf[13];
252
            interfaceCapabilities[2] = buf[14];
253
            interfaceCapabilities[3] = buf[15];
254
            interfaceCapabilities[4] = buf[16];
255
            interfaceCapabilities[5] = buf[17];
256
            moduleReserved[0] = buf[18];
257
            moduleReserved[1] = buf[19];
258
            moduleReserved[2] = buf[20];
259
            moduleReserved[3] = buf[21];
260
            moduleReserved[4] = buf[22];
261
            moduleReserved[5] = buf[23];
262
            moduleReserved[6] = buf[24];
263
            moduleReserved[7] = buf[25];
264
            moduleReserved[8] = buf[26];
265
            moduleReserved[9] = buf[27];
266
            moduleReserved[10] = buf[28];
267
            moduleReserved[11] = buf[29];
268
 
269
            valid = true;
270
 
271 2 ZTEX
//      }
272
//      else {
273
//          throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
274
//      }
275
 
276
        LibusbJava.usb_close(handle);
277
    }
278
 
279
// ******* toString ************************************************************
280
/**
281
  * Returns a string representation if the device with a lot of useful information.
282
  * @return a string representation if the device with a lot of useful information.
283
  */
284
    public String toString () {
285 5 ZTEX
 
286
        return "bus=" + dev().getBus().getDirname() + "  device=" + dev().getDevnum() + " (`" + dev().getFilename() + "')  ID=" + Integer.toHexString(usbVendorId) + ":" + Integer.toHexString(usbProductId) +"\n"  +
287 6 ZTEX
              ( manufacturerString == null ? "" : ("   Manufacturer=\""  + manufacturerString + "\"") ) +
288 2 ZTEX
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
289
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
290 5 ZTEX
              ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
291 2 ZTEX
    }
292
 
293
// ******* compatible **********************************************************
294
/**
295
  * Checks whether the given product ID is compatible to the device.<br>
296
  * The given product ID is compatible
297
  * <pre>if ( this.productId(0)==0 || productId0<=0 || this.productId(0)==productId0 ) &&
298
   ( this.productId(0)==0 || productId1<=0 || this.productId(1)==productId1 ) &&
299
   ( this.productId(2)==0 || productId2<=0 || this.productId(2)==productId2 ) &&
300
   ( this.productId(3)==0 || productId3<=0 || this.productId(3)==productId3 ) </pre>
301
  * @param productId0 Byte 0 of the given product ID
302
  * @param productId1 Byte 1 of the given product ID
303
  * @param productId2 Byte 2 of the given product ID
304
  * @param productId3 Byte 3 of the given product ID
305
  * @return true if the given product ID is compatible
306
  */
307
    public final boolean compatible( int productId0, int productId1, int productId2, int productId3 ) {
308
        return ( productId[0]==0 || productId0<=0 || (productId[0] & 255) == productId0 ) &&
309
               ( productId[1]==0 || productId1<=0 || (productId[1] & 255) == productId1 ) &&
310
               ( productId[2]==0 || productId2<=0 || (productId[2] & 255) == productId2 ) &&
311
               ( productId[3]==0 || productId3<=0 || (productId[3] & 255) == productId3 );
312
    }
313
 
314
// ******* dev *****************************************************************
315
/**
316
  * Returns the USB device.
317
  * @return the USB device.
318
  */
319
    public final Usb_Device dev() {
320
        return dev;
321
    }
322
 
323
// ******* valid ***************************************************************
324
/**
325
  * Returns true if ZTEX descriptor 1 is available.
326
  * @return true if ZTEX descriptor 1 is available.
327
  */
328
    public final boolean valid() {
329
        return valid;
330
    }
331
 
332
// ******* usbVendorId *********************************************************
333
/**
334
  * Returns the USB vendor ID of the device.
335
  * @return the USB vendor ID of the device.
336
  */
337
    public final int usbVendorId() {
338
        return usbVendorId;
339
    }
340
 
341
// ******* usbProductId *********************************************************
342
/**
343
  * Returns the USB product ID of the device.
344
  * @return the USB product ID of the device.
345
  */
346
    public final int usbProductId() {
347
        return usbProductId;
348
    }
349
 
350
// ******* manufacturerString **************************************************
351
/**
352
  * Returns the manufacturer string of the device.
353
  * @return the manufacturer string of the device.
354
  */
355
    public final String manufacturerString() {
356
        return manufacturerString;
357
    }
358
 
359
// ******* productString *******************************************************
360
/**
361
  * Returns the product string of the device.
362
  * @return the product string of the device.
363
  */
364
    public final String productString() {
365
        return productString;
366
    }
367
 
368
// ******* snString ************************************************************
369
/**
370
  * Returns the serial number string of the device.
371
  * @return the serial number string of the device.
372
  */
373
    public final String snString() {
374
        return snString;
375
    }
376
 
377
// ******* productId ***********************************************************
378
/**
379
  * Returns the product ID (all 4 bytes).
380
  * @return PRODUCT_ID, see above.
381
  */
382
    public final byte[] productId() {
383
        return productId;
384
    }
385
 
386
/**
387
  * Returns byte i of the product ID.
388
  * @return PRODUCT_ID[i], see above.
389
  * @param i index
390
  */
391
    public int productId( int i ) {
392
        return productId[i] & 255;
393
    }
394
 
395
// ******* fwVersion ***********************************************************
396
/**
397
  * Returns the firmware version.
398
  * @return FW_VERSION, see above.
399
  */
400
    public final int fwVersion() {
401
        return fwVersion & 255;
402
    }
403
 
404
// ******* interfaceVersion *****************************************************
405
/**
406
  * Returns the interface version.
407
  * @return INTERFACE_VERSION, see above.
408
  */
409
    public final int interfaceVersion() {
410
        return interfaceVersion & 255;
411
    }
412
 
413
// ******* interfaceCapabilities ************************************************
414
/**
415
  * Returns the interface capabilities (all 6 bytes).
416
  * @return INTERFACE_CAPABILITIES, see above.
417
  */
418
    public final byte[] interfaceCapabilities() {
419
        return interfaceCapabilities;
420
    }
421
 
422
/**
423
  * Returns byte i of the interface capabilities.
424
  * @return INTERFACE_CAPABILITIES[i], see above.
425
  * @param i index
426
  */
427
    public final int interfaceCapabilities( int i ) {
428
        return interfaceCapabilities[i] & 255;
429
    }
430
 
431
/**
432
  * Returns byte i, bit j  of the interface capabilities.
433
  * @return INTERFACE_CAPABILITIES[i].j, see above.
434
  * @param i byte index
435
  * @param j bit index
436
  */
437
    public final boolean interfaceCapabilities( int i, int j ) {
438
        return  (i>=0) && (i<=5) && (j>=0) && (j<8) &&
439
                (((interfaceCapabilities[i] & 255) & (1 << j)) != 0);
440
    }
441
 
442
// ******* moduleReserved ******************************************************
443
/**
444
  * Returns the application specific information (all 12 bytes).
445
  * @return MODULE_RESERVED, see above.
446
  */
447
    public final byte[] moduleReserved() {
448
        return moduleReserved;
449
    }
450
 
451
/**
452
  * Returns byte i of the application specific information.
453
  * @return MODULE_RESERVED[i], see above.
454
  * @param i index
455
  */
456
    public final int moduleReserved( int i ) {
457
        return moduleReserved[i] & 255;
458
    }
459
 
460
}

powered by: WebSVN 2.1.0

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