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 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2 9 ZTEX
   Java host software API of ZTEX SDK
3
   Copyright (C) 2009-2014 ZTEX GmbH.
4 2 ZTEX
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
/*
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 8 ZTEX
  *           <tr><td>10.11.*.*</td> <td>ZTEX USB-FPGA-Module 1.2</td></tr>
76
  *           <tr><td>10.12.*.*</td> <td>ZTEX USB-FPGA-Module 1.11</td></tr>
77
  *           <tr><td>10.13.*.*</td> <td>ZTEX USB-FPGA-Module 1.15 (not 1.15y)</td></tr>
78
  *           <tr><td>10.14.*.*</td> <td>ZTEX USB-FPGA-Module 1.15x</td></tr>
79
  *           <tr><td>10.15.*.*</td> <td>ZTEX USB-FPGA-Module 1.15y</td></tr>
80
  *           <tr><td>10.20.*.*</td> <td>ZTEX USB-Module 1.0</td></tr>
81
  *           <tr><td>10.30.*.*</td> <td>ZTEX USB-XMEGA-Module 1.0</td></tr>
82
  *           <tr><td>10.0.1.1</td> <td>ZTEX BTCMiner firmware</td></tr>
83 2 ZTEX
  *         </table></td></tr></table>
84 8 ZTEX
  *         Please contact us (<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).
85 2 ZTEX
  *       </td>
86
  *   </tr>
87
  *   <tr>
88
  *     <td bgcolor="#ffffff" valign="top">FW_VERSION</td>
89
  *     <td bgcolor="#ffffff" valign="top">10</td>
90
  *     <td bgcolor="#ffffff" valign="top">1</td>
91
  *     <td bgcolor="#ffffff" valign="top">May be used to specify the firmware version.</td>
92
  *   </tr>
93
  *   <tr>
94
  *     <td bgcolor="#ffffff" valign="top">INTERFACE_VERSION</td>
95
  *     <td bgcolor="#ffffff" valign="top">11</td>
96
  *     <td bgcolor="#ffffff" valign="top">1</td>
97
  *     <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>
98
  *   </tr>
99
  *   <tr>
100
  *     <td bgcolor="#ffffff" valign="top">INTERFACE_CAPABILITIES</td>
101
  *     <td bgcolor="#ffffff" valign="top">12</td>
102
  *     <td bgcolor="#ffffff" valign="top">6</td>
103
  *     <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>
104
  *   </tr>
105
  *   <tr>
106
  *     <td bgcolor="#ffffff" valign="top">MODULE_RESERVED</td>
107
  *     <td bgcolor="#ffffff" valign="top">18</td>
108
  *     <td bgcolor="#ffffff" valign="top">12</td>
109
  *     <td bgcolor="#ffffff" valign="top">12 bytes for application specific use, i.e. they depend from the PRODUCT_ID </td>
110
  *   </tr>
111
  *   <tr>
112
  *     <td bgcolor="#ffffff" valign="top">SN_STRING</td>
113
  *     <td bgcolor="#ffffff" valign="top">30</td>
114
  *     <td bgcolor="#ffffff" valign="top">10</td>
115
  *     <td bgcolor="#ffffff" valign="top">A serial number string of 10 characters. The default SN is "0000000000"</td> </td>
116
  *   </tr>
117
  * </table>
118
  * @see Ztex1
119
  * @see Ztex1v1
120
  * @see ZtexScanBus1
121
*/
122
 
123
public class ZtexDevice1 {
124
/** * Cypress vendor ID: 0x4b4 */
125
    public static final int cypressVendorId = 0x4b4;
126
/** * EZ-USB USB product ID: 0x8613 */
127
    public static final int cypressProductId = 0x8613;
128
 
129
/** * ZTEX vendor ID: 0x221a */
130
    public static final int ztexVendorId = 0x221A;
131
/**
132
  * USB product ID for ZTEX devices that support ZTEX descriptor 1: 0x100.
133
  * This product ID is intended for general purpose use and can be shared by all devices that base on ZTEX modules.
134
  * Different products are identified by a second product ID, namely the PRODUCT_ID field of the <a href="#descriptor"> ZTEX descriptor 1</a>.
135
  * <p>
136
  * Please read the <a href="http://www.ztex.de/firmware-kit/usb_ids.e.html">informations about USB vendor and product ID's<a>.
137
  * @see #ztexProductIdMax
138
  */
139
    public static final int ztexProductId = 0x100;
140
/**
141
  * Largest USB product ID for ZTEX devices that support ZTEX descriptor 1: 0x1ff.
142
  * 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.
143
  * <p>
144
  * Please read the <a href="http://www.ztex.de/firmware-kit/usb_ids.e.html">informations about USB vendor and product ID's<a>.
145
  * @see #ztexProductId
146
  */
147
    public static final int ztexProductIdMax = 0x1ff;
148
 
149
    private Usb_Device dev = null;
150
    private boolean valid = false;              // true if descriptor 1 is available
151
    private int usbVendorId = -1;
152
    private int usbProductId = -1;
153
    private String manufacturerString = null;
154
    private String productString = null;
155
    private String snString = null;
156
    private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descriptor, not the USB product ID
157
    private byte fwVersion = 0;
158
    private byte interfaceVersion = 0;
159
    private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
160
    private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
161
 
162
// ******* byteArrayString *****************************************************
163
/**
164
  * Produces a nice string representation of an array of bytes.
165
  * @param buf A byte array.
166
  * @return a nice string
167
  */
168
    public static String byteArrayString ( byte buf[] ) {
169
        String s = new String( "" );
170
        for ( int i = 0; i<buf.length; i++ ) {
171
            if ( i != 0 )
172
                s+=".";
173
            s+=buf[i] & 255;
174
        }
175
        return s;
176
    }
177
 
178
// ******* ZtexDevice1 *********************************************************
179
/**
180
  * Constructs an instance from a given USB device.<br>
181
  * If the given vendor and product id's match to the vendor and product id's of the given USB device,
182
  * the ZTEX descriptor 1 is attempted to read. If this fails, an {@link InvalidFirmwareException} is thrown.
183
  * To suppress this behavior (e.g. if the EZ-USB device is known to be unconfigured) the vendor and product id's
184
  * can be set to -1.
185
  * @param p_dev The USB device.
186
  * @param pUsbVendorId The given vendor ID.
187
  * @param pUsbProductId The given product ID.
188 5 ZTEX
  * @param allowUnconfigured If true, unconfigured devices are allowed.
189 2 ZTEX
  * @throws UsbException if an USB communication error occurs.
190
  * @throws InvalidFirmwareException if no valid ZTEX descriptor 1 is found.
191 5 ZTEX
  * @throws DeviceNotSupported if the device has the wrong USB ID's.
192 2 ZTEX
  */
193 5 ZTEX
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId, boolean allowUnconfigured) throws UsbException, InvalidFirmwareException, DeviceNotSupportedException  {
194 2 ZTEX
        dev = p_dev;
195
 
196
        Usb_Device_Descriptor dd = dev().getDescriptor();
197
        usbVendorId = dd.getIdVendor() & 65535;
198
        usbProductId = dd.getIdProduct() & 65535;
199
 
200 5 ZTEX
        if ( ! ( (
201
                   (usbVendorId == pUsbVendorId) &&
202
                   (usbProductId == pUsbProductId || ( usbVendorId == ztexVendorId && pUsbProductId<0 && usbProductId>=ztexProductId && usbProductId<ztexProductIdMax ) )
203
                 ) ||
204
                 (
205
                   allowUnconfigured && (usbVendorId == cypressVendorId) && (usbProductId == cypressProductId)
206
                 ) )  )
207
            throw new DeviceNotSupportedException(p_dev);
208 2 ZTEX
 
209 5 ZTEX
        long handle = LibusbJava.usb_open(dev);
210 2 ZTEX
 
211
//      if ( handle > 0 ) {
212
            if ( dd.getIManufacturer() > 0 )
213
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
214
            if ( dd.getIProduct() > 0 )
215
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
216
            if ( dd.getISerialNumber() > 0 )
217
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
218
 
219 5 ZTEX
            if ( snString == null ) {
220
                LibusbJava.usb_close(handle);
221
                if ( allowUnconfigured )
222
                    return;
223
                else
224 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
225 5 ZTEX
            }
226 2 ZTEX
 
227 5 ZTEX
            byte[] buf = new byte[42];
228
            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.
229
            if ( i < 0 ) {
230
                LibusbJava.usb_close(handle);
231
                if ( allowUnconfigured )
232
                    return;
233
                else
234 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
235 5 ZTEX
            }
236
            else if ( i != 40 ) {
237
                LibusbJava.usb_close(handle);
238
                if ( allowUnconfigured )
239
                    return;
240
                else
241 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
242 5 ZTEX
            }
243
            if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
244
                LibusbJava.usb_close(handle);
245
                if ( allowUnconfigured )
246
                    return;
247
                else
248 2 ZTEX
                    throw new InvalidFirmwareException( dev, "Invalid ZTEX descriptor" );
249
            }
250 5 ZTEX
            productId[0] = buf[6];
251
            productId[1] = buf[7];
252
            productId[2] = buf[8];
253
            productId[3] = buf[9];
254
            fwVersion = buf[10];
255
            interfaceVersion = buf[11];
256
            interfaceCapabilities[0] = buf[12];
257
            interfaceCapabilities[1] = buf[13];
258
            interfaceCapabilities[2] = buf[14];
259
            interfaceCapabilities[3] = buf[15];
260
            interfaceCapabilities[4] = buf[16];
261
            interfaceCapabilities[5] = buf[17];
262
            moduleReserved[0] = buf[18];
263
            moduleReserved[1] = buf[19];
264
            moduleReserved[2] = buf[20];
265
            moduleReserved[3] = buf[21];
266
            moduleReserved[4] = buf[22];
267
            moduleReserved[5] = buf[23];
268
            moduleReserved[6] = buf[24];
269
            moduleReserved[7] = buf[25];
270
            moduleReserved[8] = buf[26];
271
            moduleReserved[9] = buf[27];
272
            moduleReserved[10] = buf[28];
273
            moduleReserved[11] = buf[29];
274
 
275
            valid = true;
276
 
277 2 ZTEX
//      }
278
//      else {
279
//          throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
280
//      }
281
 
282
        LibusbJava.usb_close(handle);
283
    }
284
 
285
// ******* toString ************************************************************
286
/**
287
  * Returns a string representation if the device with a lot of useful information.
288
  * @return a string representation if the device with a lot of useful information.
289
  */
290
    public String toString () {
291 5 ZTEX
 
292
        return "bus=" + dev().getBus().getDirname() + "  device=" + dev().getDevnum() + " (`" + dev().getFilename() + "')  ID=" + Integer.toHexString(usbVendorId) + ":" + Integer.toHexString(usbProductId) +"\n"  +
293 6 ZTEX
              ( manufacturerString == null ? "" : ("   Manufacturer=\""  + manufacturerString + "\"") ) +
294 2 ZTEX
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
295
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
296 5 ZTEX
              ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
297 2 ZTEX
    }
298
 
299
// ******* compatible **********************************************************
300
/**
301
  * Checks whether the given product ID is compatible to the device.<br>
302
  * The given product ID is compatible
303
  * <pre>if ( this.productId(0)==0 || productId0<=0 || this.productId(0)==productId0 ) &&
304
   ( this.productId(0)==0 || productId1<=0 || this.productId(1)==productId1 ) &&
305
   ( this.productId(2)==0 || productId2<=0 || this.productId(2)==productId2 ) &&
306
   ( this.productId(3)==0 || productId3<=0 || this.productId(3)==productId3 ) </pre>
307
  * @param productId0 Byte 0 of the given product ID
308
  * @param productId1 Byte 1 of the given product ID
309
  * @param productId2 Byte 2 of the given product ID
310
  * @param productId3 Byte 3 of the given product ID
311
  * @return true if the given product ID is compatible
312
  */
313
    public final boolean compatible( int productId0, int productId1, int productId2, int productId3 ) {
314
        return ( productId[0]==0 || productId0<=0 || (productId[0] & 255) == productId0 ) &&
315
               ( productId[1]==0 || productId1<=0 || (productId[1] & 255) == productId1 ) &&
316
               ( productId[2]==0 || productId2<=0 || (productId[2] & 255) == productId2 ) &&
317
               ( productId[3]==0 || productId3<=0 || (productId[3] & 255) == productId3 );
318
    }
319
 
320
// ******* dev *****************************************************************
321
/**
322
  * Returns the USB device.
323
  * @return the USB device.
324
  */
325
    public final Usb_Device dev() {
326
        return dev;
327
    }
328
 
329
// ******* valid ***************************************************************
330
/**
331
  * Returns true if ZTEX descriptor 1 is available.
332
  * @return true if ZTEX descriptor 1 is available.
333
  */
334
    public final boolean valid() {
335
        return valid;
336
    }
337
 
338
// ******* usbVendorId *********************************************************
339
/**
340
  * Returns the USB vendor ID of the device.
341
  * @return the USB vendor ID of the device.
342
  */
343
    public final int usbVendorId() {
344
        return usbVendorId;
345
    }
346
 
347
// ******* usbProductId *********************************************************
348
/**
349
  * Returns the USB product ID of the device.
350
  * @return the USB product ID of the device.
351
  */
352
    public final int usbProductId() {
353
        return usbProductId;
354
    }
355
 
356
// ******* manufacturerString **************************************************
357
/**
358
  * Returns the manufacturer string of the device.
359
  * @return the manufacturer string of the device.
360
  */
361
    public final String manufacturerString() {
362
        return manufacturerString;
363
    }
364
 
365
// ******* productString *******************************************************
366
/**
367
  * Returns the product string of the device.
368
  * @return the product string of the device.
369
  */
370
    public final String productString() {
371
        return productString;
372
    }
373
 
374
// ******* snString ************************************************************
375
/**
376
  * Returns the serial number string of the device.
377
  * @return the serial number string of the device.
378
  */
379
    public final String snString() {
380
        return snString;
381
    }
382
 
383
// ******* productId ***********************************************************
384
/**
385
  * Returns the product ID (all 4 bytes).
386
  * @return PRODUCT_ID, see above.
387
  */
388
    public final byte[] productId() {
389
        return productId;
390
    }
391
 
392
/**
393
  * Returns byte i of the product ID.
394
  * @return PRODUCT_ID[i], see above.
395
  * @param i index
396
  */
397
    public int productId( int i ) {
398
        return productId[i] & 255;
399
    }
400
 
401
// ******* fwVersion ***********************************************************
402
/**
403
  * Returns the firmware version.
404
  * @return FW_VERSION, see above.
405
  */
406
    public final int fwVersion() {
407
        return fwVersion & 255;
408
    }
409
 
410
// ******* interfaceVersion *****************************************************
411
/**
412
  * Returns the interface version.
413
  * @return INTERFACE_VERSION, see above.
414
  */
415
    public final int interfaceVersion() {
416
        return interfaceVersion & 255;
417
    }
418
 
419
// ******* interfaceCapabilities ************************************************
420
/**
421
  * Returns the interface capabilities (all 6 bytes).
422
  * @return INTERFACE_CAPABILITIES, see above.
423
  */
424
    public final byte[] interfaceCapabilities() {
425
        return interfaceCapabilities;
426
    }
427
 
428
/**
429
  * Returns byte i of the interface capabilities.
430
  * @return INTERFACE_CAPABILITIES[i], see above.
431
  * @param i index
432
  */
433
    public final int interfaceCapabilities( int i ) {
434
        return interfaceCapabilities[i] & 255;
435
    }
436
 
437
/**
438
  * Returns byte i, bit j  of the interface capabilities.
439
  * @return INTERFACE_CAPABILITIES[i].j, see above.
440
  * @param i byte index
441
  * @param j bit index
442
  */
443
    public final boolean interfaceCapabilities( int i, int j ) {
444
        return  (i>=0) && (i<=5) && (j>=0) && (j<8) &&
445
                (((interfaceCapabilities[i] & 255) & (1 << j)) != 0);
446
    }
447
 
448
// ******* moduleReserved ******************************************************
449
/**
450
  * Returns the application specific information (all 12 bytes).
451
  * @return MODULE_RESERVED, see above.
452
  */
453
    public final byte[] moduleReserved() {
454
        return moduleReserved;
455
    }
456
 
457
/**
458
  * Returns byte i of the application specific information.
459
  * @return MODULE_RESERVED[i], see above.
460
  * @param i index
461
  */
462
    public final int moduleReserved( int i ) {
463
        return moduleReserved[i] & 255;
464
    }
465
 
466
}

powered by: WebSVN 2.1.0

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