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

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [java/] [ztex/] [ZtexDevice1.java] - Blame information for rev 5

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

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   Java Driver API for the ZTEX Firmware Kit
3 4 ZTEX
   Copyright (C) 2009-2010 ZTEX e.K.
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 3 ZTEX
/**
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 4 ZTEX
  * <a name="descriptor"></a>
34 3 ZTEX
  * <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 2 ZTEX
public class ZtexDevice1 {
118 4 ZTEX
/** * Cypress vendor ID: 0x4b4 */
119 2 ZTEX
    public static final int cypressVendorId = 0x4b4;
120 4 ZTEX
/** * EZ-USB USB product ID: 0x8613 */
121 2 ZTEX
    public static final int cypressProductId = 0x8613;
122
 
123 4 ZTEX
/** * 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 2 ZTEX
    private Usb_Device dev = null;
144
    private boolean isCypress = false;          // true if Cypress device
145
    private boolean valid = false;              // true if descriptor 1 is available
146
    private int usbVendorId = -1;
147
    private int usbProductId = -1;
148
    private String manufacturerString = null;
149
    private String productString = null;
150
    private String snString = null;
151 3 ZTEX
    private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descriptor, not the USB product ID
152 2 ZTEX
    private byte fwVersion = 0;
153
    private byte interfaceVersion = 0;
154
    private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
155
    private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
156
 
157
// ******* byteArrayString *****************************************************
158 3 ZTEX
/**
159
  * Produces a nice string representation of an array of bytes.
160
  * @param buf A byte array.
161
  * @return a nice string
162
  */
163 2 ZTEX
    public static String byteArrayString ( byte buf[] ) {
164
        String s = new String( "" );
165
        for ( int i = 0; i<buf.length; i++ ) {
166
            if ( i != 0 )
167
                s+=".";
168
            s+=buf[i] & 255;
169
        }
170
        return s;
171
    }
172
 
173
// ******* ZtexDevice1 *********************************************************
174 3 ZTEX
/**
175
  * Constructs an instance from a given USB device.<br>
176
  * If the given vendor and product id's match to the vendor and product id's of the given USB device,
177
  * the ZTEX descriptor 1 is attempted to read. If this fails, an {@link InvalidFirmwareException} is thrown.
178
  * To suppress this behavior (e.g. if the EZ-USB device is known to be unconfigured) the vendor and product id's
179
  * can be set to -1.
180
  * @param p_dev The USB device.
181
  * @param pUsbVendorId The given vendor ID.
182
  * @param pUsbProductId The given product ID.
183
  * @throws UsbException if an USB communication error occurs.
184
  * @throws InvalidFirmwareException if no valid ZTEX descriptor 1 is found.
185
  */
186
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId) throws UsbException, InvalidFirmwareException {
187 2 ZTEX
        dev = p_dev;
188
 
189 3 ZTEX
        Usb_Device_Descriptor dd = dev().getDescriptor();
190 2 ZTEX
        usbVendorId = dd.getIdVendor() & 65535;
191
        usbProductId = dd.getIdProduct() & 65535;
192
 
193
        if ( usbVendorId == cypressVendorId  &&  usbProductId == cypressProductId ) {
194
            isCypress = true;
195
        }
196
 
197
        int handle = LibusbJava.usb_open(dev);
198
 
199 4 ZTEX
//      if ( handle > 0 ) {
200 2 ZTEX
            if ( dd.getIManufacturer() > 0 )
201
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
202
            if ( dd.getIProduct() > 0 )
203
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
204
            if ( dd.getISerialNumber() > 0 )
205
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
206
 
207 4 ZTEX
            if ( usbVendorId == pUsbVendorId && (usbProductId == pUsbProductId || ( usbVendorId == ztexVendorId && pUsbProductId<0 && usbProductId>=ztexProductId && usbProductId<ztexProductIdMax ) ) ) {
208 2 ZTEX
                if ( snString == null ) {
209
                    LibusbJava.usb_close(handle);
210 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Not a ZTEX device" );  // ZTEX devices always have a SN. See also the next comment a few lines below
211 2 ZTEX
                }
212
 
213
                byte[] buf = new byte[42];
214
                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.
215
                if ( i < 0 ) {
216
                    LibusbJava.usb_close(handle);
217 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
218 2 ZTEX
                }
219
                else if ( i != 40 ) {
220
                    LibusbJava.usb_close(handle);
221 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
222 2 ZTEX
                }
223
                if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
224
                    LibusbJava.usb_close(handle);
225 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Invalid ZTEX descriptor" );
226 2 ZTEX
                }
227
                productId[0] = buf[6];
228
                productId[1] = buf[7];
229
                productId[2] = buf[8];
230
                productId[3] = buf[9];
231
                fwVersion = buf[10];
232
                interfaceVersion = buf[11];
233
                interfaceCapabilities[0] = buf[12];
234
                interfaceCapabilities[1] = buf[13];
235
                interfaceCapabilities[2] = buf[14];
236
                interfaceCapabilities[3] = buf[15];
237
                interfaceCapabilities[4] = buf[16];
238
                interfaceCapabilities[5] = buf[17];
239
                moduleReserved[0] = buf[18];
240
                moduleReserved[1] = buf[19];
241
                moduleReserved[2] = buf[20];
242
                moduleReserved[3] = buf[21];
243
                moduleReserved[4] = buf[22];
244
                moduleReserved[5] = buf[23];
245
                moduleReserved[6] = buf[24];
246
                moduleReserved[7] = buf[25];
247
                moduleReserved[8] = buf[26];
248
                moduleReserved[9] = buf[27];
249
                moduleReserved[10] = buf[28];
250
                moduleReserved[11] = buf[29];
251
 
252
                valid = true;
253
            }
254 4 ZTEX
//      }
255
//      else {
256
//          throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
257
//      }
258 2 ZTEX
 
259
        LibusbJava.usb_close(handle);
260
    }
261
 
262
// ******* toString ************************************************************
263 3 ZTEX
/**
264
  * Returns a string representation if the device with a lot of useful information.
265
  * @return a string representation if the device with a lot of useful information.
266
  */
267 2 ZTEX
    public String toString () {
268 4 ZTEX
        return "bus=" + dev().getBus().getDirname() + "  device=" + dev().getFilename() + "  ID=" + Integer.toHexString(usbVendorId) + ":" + Integer.toHexString(usbProductId) +
269 2 ZTEX
            "\n   " + ( isCypress ? "Cypress" : "" ) +
270
              ( manufacturerString == null ? "" : ("    Manufacturer=\""  + manufacturerString + "\"") ) +
271
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
272
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
273
            ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
274
    }
275
 
276
// ******* compatible **********************************************************
277 3 ZTEX
/**
278
  * Checks whether the given product ID is compatible to the device.<br>
279
  * The given product ID is compatible
280
  * <pre>if ( this.productId(0)==0 || productId0<=0 || this.productId(0)==productId0 ) &&
281
   ( this.productId(0)==0 || productId1<=0 || this.productId(1)==productId1 ) &&
282
   ( this.productId(2)==0 || productId2<=0 || this.productId(2)==productId2 ) &&
283
   ( this.productId(3)==0 || productId3<=0 || this.productId(3)==productId3 ) </pre>
284
  * @param productId0 Byte 0 of the given product ID
285
  * @param productId1 Byte 1 of the given product ID
286
  * @param productId2 Byte 2 of the given product ID
287
  * @param productId3 Byte 3 of the given product ID
288
  * @return true if the given product ID is compatible
289
  */
290 2 ZTEX
    public final boolean compatible( int productId0, int productId1, int productId2, int productId3 ) {
291
        return ( productId[0]==0 || productId0<=0 || (productId[0] & 255) == productId0 ) &&
292
               ( productId[1]==0 || productId1<=0 || (productId[1] & 255) == productId1 ) &&
293
               ( productId[2]==0 || productId2<=0 || (productId[2] & 255) == productId2 ) &&
294
               ( productId[3]==0 || productId3<=0 || (productId[3] & 255) == productId3 );
295
    }
296
 
297
// ******* dev *****************************************************************
298 3 ZTEX
/**
299
  * Returns the USB device.
300
  * @return the USB device.
301
  */
302 2 ZTEX
    public final Usb_Device dev() {
303
        return dev;
304
    }
305
 
306
// ******* isCypress ***********************************************************
307 3 ZTEX
/**
308
  * Returns true if the device has Cypress EZ-USB vendor and product ID's (0x4b4 and 0x8613).
309
  * @return true if the device has Cypress EZ-USB vendor and product ID's (0x4b4 and 0x8613).
310
  */
311 2 ZTEX
    public final boolean isCypress() {
312
        return isCypress;
313
    }
314
 
315
// ******* valid ***************************************************************
316 3 ZTEX
/**
317
  * Returns true if ZTEX descriptor 1 is available.
318
  * @return true if ZTEX descriptor 1 is available.
319
  */
320 2 ZTEX
    public final boolean valid() {
321
        return valid;
322
    }
323
 
324
// ******* usbVendorId *********************************************************
325 3 ZTEX
/**
326
  * Returns the USB vendor ID of the device.
327
  * @return the USB vendor ID of the device.
328
  */
329 2 ZTEX
    public final int usbVendorId() {
330
        return usbVendorId;
331
    }
332
 
333
// ******* usbProductId *********************************************************
334 3 ZTEX
/**
335
  * Returns the USB product ID of the device.
336
  * @return the USB product ID of the device.
337
  */
338 2 ZTEX
    public final int usbProductId() {
339
        return usbProductId;
340
    }
341
 
342
// ******* manufacturerString **************************************************
343 3 ZTEX
/**
344
  * Returns the manufacturer string of the device.
345
  * @return the manufacturer string of the device.
346
  */
347 2 ZTEX
    public final String manufacturerString() {
348
        return manufacturerString;
349
    }
350
 
351
// ******* productString *******************************************************
352 3 ZTEX
/**
353
  * Returns the product string of the device.
354
  * @return the product string of the device.
355
  */
356 2 ZTEX
    public final String productString() {
357
        return productString;
358
    }
359
 
360
// ******* snString ************************************************************
361 3 ZTEX
/**
362
  * Returns the serial number string of the device.
363
  * @return the serial number string of the device.
364
  */
365 2 ZTEX
    public final String snString() {
366
        return snString;
367
    }
368
 
369
// ******* productId ***********************************************************
370 3 ZTEX
/**
371
  * Returns the product ID (all 4 bytes).
372
  * @return PRODUCT_ID, see above.
373
  */
374 2 ZTEX
    public final byte[] productId() {
375
        return productId;
376
    }
377
 
378 3 ZTEX
/**
379
  * Returns byte i of the product ID.
380
  * @return PRODUCT_ID[i], see above.
381
  * @param i index
382
  */
383 2 ZTEX
    public int productId( int i ) {
384
        return productId[i] & 255;
385
    }
386
 
387
// ******* fwVersion ***********************************************************
388 3 ZTEX
/**
389
  * Returns the firmware version.
390
  * @return FW_VERSION, see above.
391
  */
392 2 ZTEX
    public final int fwVersion() {
393
        return fwVersion & 255;
394
    }
395
 
396
// ******* interfaceVersion *****************************************************
397 3 ZTEX
/**
398
  * Returns the interface version.
399
  * @return INTERFACE_VERSION, see above.
400
  */
401 2 ZTEX
    public final int interfaceVersion() {
402
        return interfaceVersion & 255;
403
    }
404
 
405
// ******* interfaceCapabilities ************************************************
406 3 ZTEX
/**
407
  * Returns the interface capabilities (all 6 bytes).
408
  * @return INTERFACE_CAPABILITIES, see above.
409
  */
410 2 ZTEX
    public final byte[] interfaceCapabilities() {
411
        return interfaceCapabilities;
412
    }
413
 
414 3 ZTEX
/**
415
  * Returns byte i of the interface capabilities.
416
  * @return INTERFACE_CAPABILITIES[i], see above.
417
  * @param i index
418
  */
419 2 ZTEX
    public final int interfaceCapabilities( int i ) {
420
        return interfaceCapabilities[i] & 255;
421
    }
422
 
423 3 ZTEX
/**
424
  * Returns byte i, bit j  of the interface capabilities.
425
  * @return INTERFACE_CAPABILITIES[i].j, see above.
426
  * @param i byte index
427
  * @param j bit index
428
  */
429 2 ZTEX
    public final boolean interfaceCapabilities( int i, int j ) {
430
        return  (i>=0) && (i<=5) && (j>=0) && (j<8) &&
431
                (((interfaceCapabilities[i] & 255) & (1 << j)) != 0);
432
    }
433
 
434
// ******* moduleReserved ******************************************************
435 3 ZTEX
/**
436
  * Returns the application specific information (all 12 bytes).
437
  * @return MODULE_RESERVED, see above.
438
  */
439 2 ZTEX
    public final byte[] moduleReserved() {
440
        return moduleReserved;
441
    }
442
 
443 3 ZTEX
/**
444
  * Returns byte i of the application specific information.
445
  * @return MODULE_RESERVED[i], see above.
446
  * @param i index
447
  */
448 2 ZTEX
    public final int moduleReserved( int i ) {
449
        return moduleReserved[i] & 255;
450
    }
451
 
452
}

powered by: WebSVN 2.1.0

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