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 3

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
   Copyright (C) 2008-2009 ZTEX e.K.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
/*
20
    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
  * <table bgcolor="#404040" cellspacing=1 cellpadding=4>
34
  *   <tr>
35
  *     <td bgcolor="#d0d0d0" valign="top"><b>Field name</b></td>
36
  *     <td bgcolor="#d0d0d0" valign="top"><b>Offset</b></td>
37
  *     <td bgcolor="#d0d0d0" valign="top"><b>Size</b></td>
38
  *     <td bgcolor="#d0d0d0" valign="top"><b>Description</b></td>
39
  *   </tr>
40
  *   <tr>
41
  *     <td bgcolor="#ffffff" valign="top">ZTEX_DESCRIPTOR_SIZE</td>
42
  *     <td bgcolor="#ffffff" valign="top">0</td>
43
  *     <td bgcolor="#ffffff" valign="top">1</td>
44
  *     <td bgcolor="#ffffff" valign="top">Size of the descriptor in bytes; must be 40 for descriptor version 1</td>
45
  *   </tr>
46
  *   <tr>
47
  *     <td bgcolor="#ffffff" valign="top">ZTEX_DESCRIPTOR_VERSION</td>
48
  *     <td bgcolor="#ffffff" valign="top">1</td>
49
  *     <td bgcolor="#ffffff" valign="top">1</td>
50
  *     <td bgcolor="#ffffff" valign="top">Descriptor version; 1 for version 1</td>
51
  *   </tr>
52
  *   <tr>
53
  *     <td bgcolor="#ffffff" valign="top">ZTEXID</td>
54
  *     <td bgcolor="#ffffff" valign="top">2</td>
55
  *     <td bgcolor="#ffffff" valign="top">4</td>
56
  *     <td bgcolor="#ffffff" valign="top">ID; must be "ZTEX"</td>
57
  *   </tr>
58
  *   <tr>
59
  *     <td bgcolor="#ffffff" valign="top">PRODUCT_ID</td>
60
  *     <td bgcolor="#ffffff" valign="top">6</td>
61
  *     <td bgcolor="#ffffff" valign="top">4</td>
62
  *     <td bgcolor="#ffffff" valign="top">Four numbers (0..255) representing the product ID and firmware compatibility information.<br>
63
  *         A firmware can overwrite an installed one<br>
64
  *        <pre>if ( INSTALLED.PRODUCTID[0]==0 || PRODUCTID[0]==0 || INSTALLED.PRODUCTID[0]==PRODUCTID[0] ) &&
65
   ( INSTALLED.PRODUCTID[1]==0 || PRODUCTID[1]==0 || INSTALLED.PRODUCTID[1]==PRODUCTID[1] ) &&
66
   ( INSTALLED.PRODUCTID[2]==0 || PRODUCTID[2]==0 || INSTALLED.PRODUCTID[2]==PRODUCTID[2] ) &&
67
   ( INSTALLED.PRODUCTID[3]==0 || PRODUCTID[3]==0 || INSTALLED.PRODUCTID[3]==PRODUCTID[3] ) </pre>
68
  *       Here is a list of the preserved product ID's:
69
  *       <table><tr><td>&nbsp</td><td>
70
  *         <table>
71
  *          <tr><td>0.0.0.0</td> <td>default Product ID (no product specified)</td></tr>
72
  *           <tr><td>1.*.*.*</td> <td>may be used for experimental purposes</td></tr>
73
  *           <tr><td>10.*.*.*</td> <td>used for ZTEX products</td></tr>
74
  *           <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>
75
  *           <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>
76
  *         </table></td></tr></table>
77
  *         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).
78
  *       </td>
79
  *   </tr>
80
  *   <tr>
81
  *     <td bgcolor="#ffffff" valign="top">FW_VERSION</td>
82
  *     <td bgcolor="#ffffff" valign="top">10</td>
83
  *     <td bgcolor="#ffffff" valign="top">1</td>
84
  *     <td bgcolor="#ffffff" valign="top">May be used to specify the firmware version.</td>
85
  *   </tr>
86
  *   <tr>
87
  *     <td bgcolor="#ffffff" valign="top">INTERFACE_VERSION</td>
88
  *     <td bgcolor="#ffffff" valign="top">11</td>
89
  *     <td bgcolor="#ffffff" valign="top">1</td>
90
  *     <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>
91
  *   </tr>
92
  *   <tr>
93
  *     <td bgcolor="#ffffff" valign="top">INTERFACE_CAPABILITIES</td>
94
  *     <td bgcolor="#ffffff" valign="top">12</td>
95
  *     <td bgcolor="#ffffff" valign="top">6</td>
96
  *     <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>
97
  *   </tr>
98
  *   <tr>
99
  *     <td bgcolor="#ffffff" valign="top">MODULE_RESERVED</td>
100
  *     <td bgcolor="#ffffff" valign="top">18</td>
101
  *     <td bgcolor="#ffffff" valign="top">12</td>
102
  *     <td bgcolor="#ffffff" valign="top">12 bytes for application specific use, i.e. they depend from the PRODUCT_ID </td>
103
  *   </tr>
104
  *   <tr>
105
  *     <td bgcolor="#ffffff" valign="top">SN_STRING</td>
106
  *     <td bgcolor="#ffffff" valign="top">30</td>
107
  *     <td bgcolor="#ffffff" valign="top">10</td>
108
  *     <td bgcolor="#ffffff" valign="top">A serial number string of 10 characters. The default SN is "0000000000"</td> </td>
109
  *   </tr>
110
  * </table>
111
  * @see Ztex1
112
  * @see Ztex1v1
113
  * @see ZtexScanBus1
114
*/
115
 
116 2 ZTEX
public class ZtexDevice1 {
117 3 ZTEX
/** * The Cypress vendor ID 0x4b4. */
118 2 ZTEX
    public static final int cypressVendorId = 0x4b4;
119 3 ZTEX
/** * The EZ-USB product ID 0x8613. */
120 2 ZTEX
    public static final int cypressProductId = 0x8613;
121
 
122
    private Usb_Device dev = null;
123
    private boolean isCypress = false;          // true if Cypress device
124
    private boolean valid = false;              // true if descriptor 1 is available
125
    private int usbVendorId = -1;
126
    private int usbProductId = -1;
127
    private String manufacturerString = null;
128
    private String productString = null;
129
    private String snString = null;
130 3 ZTEX
    private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descriptor, not the USB product ID
131 2 ZTEX
    private byte fwVersion = 0;
132
    private byte interfaceVersion = 0;
133
    private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
134
    private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
135
 
136
// ******* byteArrayString *****************************************************
137 3 ZTEX
/**
138
  * Produces a nice string representation of an array of bytes.
139
  * @param buf A byte array.
140
  * @return a nice string
141
  */
142 2 ZTEX
    public static String byteArrayString ( byte buf[] ) {
143
        String s = new String( "" );
144
        for ( int i = 0; i<buf.length; i++ ) {
145
            if ( i != 0 )
146
                s+=".";
147
            s+=buf[i] & 255;
148
        }
149
        return s;
150
    }
151
 
152
// ******* ZtexDevice1 *********************************************************
153 3 ZTEX
/**
154
  * Constructs an instance from a given USB device.<br>
155
  * If the given vendor and product id's match to the vendor and product id's of the given USB device,
156
  * the ZTEX descriptor 1 is attempted to read. If this fails, an {@link InvalidFirmwareException} is thrown.
157
  * To suppress this behavior (e.g. if the EZ-USB device is known to be unconfigured) the vendor and product id's
158
  * can be set to -1.
159
  * @param p_dev The USB device.
160
  * @param pUsbVendorId The given vendor ID.
161
  * @param pUsbProductId The given product ID.
162
  * @throws UsbException if an USB communication error occurs.
163
  * @throws InvalidFirmwareException if no valid ZTEX descriptor 1 is found.
164
  */
165
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId) throws UsbException, InvalidFirmwareException {
166 2 ZTEX
        dev = p_dev;
167
 
168 3 ZTEX
        Usb_Device_Descriptor dd = dev().getDescriptor();
169 2 ZTEX
        usbVendorId = dd.getIdVendor() & 65535;
170
        usbProductId = dd.getIdProduct() & 65535;
171
 
172
        if ( usbVendorId == cypressVendorId  &&  usbProductId == cypressProductId ) {
173
            isCypress = true;
174
        }
175
 
176
        int handle = LibusbJava.usb_open(dev);
177
 
178
        if ( handle > 0 ) {
179
            if ( dd.getIManufacturer() > 0 )
180
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
181
            if ( dd.getIProduct() > 0 )
182
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
183
            if ( dd.getISerialNumber() > 0 )
184
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
185
 
186
            if ( usbVendorId == pUsbVendorId  &&  usbProductId == pUsbProductId ) {
187
                if ( snString == null ) {
188
                    LibusbJava.usb_close(handle);
189 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
190 2 ZTEX
                }
191
 
192
                byte[] buf = new byte[42];
193
                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.
194
                if ( i < 0 ) {
195
                    LibusbJava.usb_close(handle);
196 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
197 2 ZTEX
                }
198
                else if ( i != 40 ) {
199
                    LibusbJava.usb_close(handle);
200 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
201 2 ZTEX
                }
202
                if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
203
                    LibusbJava.usb_close(handle);
204 3 ZTEX
                    throw new InvalidFirmwareException( dev, "Invalid ZTEX descriptor" );
205 2 ZTEX
                }
206
                productId[0] = buf[6];
207
                productId[1] = buf[7];
208
                productId[2] = buf[8];
209
                productId[3] = buf[9];
210
                fwVersion = buf[10];
211
                interfaceVersion = buf[11];
212
                interfaceCapabilities[0] = buf[12];
213
                interfaceCapabilities[1] = buf[13];
214
                interfaceCapabilities[2] = buf[14];
215
                interfaceCapabilities[3] = buf[15];
216
                interfaceCapabilities[4] = buf[16];
217
                interfaceCapabilities[5] = buf[17];
218
                moduleReserved[0] = buf[18];
219
                moduleReserved[1] = buf[19];
220
                moduleReserved[2] = buf[20];
221
                moduleReserved[3] = buf[21];
222
                moduleReserved[4] = buf[22];
223
                moduleReserved[5] = buf[23];
224
                moduleReserved[6] = buf[24];
225
                moduleReserved[7] = buf[25];
226
                moduleReserved[8] = buf[26];
227
                moduleReserved[9] = buf[27];
228
                moduleReserved[10] = buf[28];
229
                moduleReserved[11] = buf[29];
230
 
231
                valid = true;
232
            }
233
        }
234
        else {
235
            throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
236
        }
237
 
238
        LibusbJava.usb_close(handle);
239
    }
240
 
241
// ******* toString ************************************************************
242 3 ZTEX
/**
243
  * Returns a string representation if the device with a lot of useful information.
244
  * @return a string representation if the device with a lot of useful information.
245
  */
246 2 ZTEX
    public String toString () {
247 3 ZTEX
        return "bus=" + dev().getBus().getDirname() + "  device=" + dev().getFilename() +
248 2 ZTEX
            "\n   " + ( isCypress ? "Cypress" : "" ) +
249
              ( manufacturerString == null ? "" : ("    Manufacturer=\""  + manufacturerString + "\"") ) +
250
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
251
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
252
            ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
253
    }
254
 
255
// ******* compatible **********************************************************
256 3 ZTEX
/**
257
  * Checks whether the given product ID is compatible to the device.<br>
258
  * The given product ID is compatible
259
  * <pre>if ( this.productId(0)==0 || productId0<=0 || this.productId(0)==productId0 ) &&
260
   ( this.productId(0)==0 || productId1<=0 || this.productId(1)==productId1 ) &&
261
   ( this.productId(2)==0 || productId2<=0 || this.productId(2)==productId2 ) &&
262
   ( this.productId(3)==0 || productId3<=0 || this.productId(3)==productId3 ) </pre>
263
  * @param productId0 Byte 0 of the given product ID
264
  * @param productId1 Byte 1 of the given product ID
265
  * @param productId2 Byte 2 of the given product ID
266
  * @param productId3 Byte 3 of the given product ID
267
  * @return true if the given product ID is compatible
268
  */
269 2 ZTEX
    public final boolean compatible( int productId0, int productId1, int productId2, int productId3 ) {
270
        return ( productId[0]==0 || productId0<=0 || (productId[0] & 255) == productId0 ) &&
271
               ( productId[1]==0 || productId1<=0 || (productId[1] & 255) == productId1 ) &&
272
               ( productId[2]==0 || productId2<=0 || (productId[2] & 255) == productId2 ) &&
273
               ( productId[3]==0 || productId3<=0 || (productId[3] & 255) == productId3 );
274
    }
275
 
276
// ******* dev *****************************************************************
277 3 ZTEX
/**
278
  * Returns the USB device.
279
  * @return the USB device.
280
  */
281 2 ZTEX
    public final Usb_Device dev() {
282
        return dev;
283
    }
284
 
285
// ******* isCypress ***********************************************************
286 3 ZTEX
/**
287
  * Returns true if the device has Cypress EZ-USB vendor and product ID's (0x4b4 and 0x8613).
288
  * @return true if the device has Cypress EZ-USB vendor and product ID's (0x4b4 and 0x8613).
289
  */
290 2 ZTEX
    public final boolean isCypress() {
291
        return isCypress;
292
    }
293
 
294
// ******* valid ***************************************************************
295 3 ZTEX
/**
296
  * Returns true if ZTEX descriptor 1 is available.
297
  * @return true if ZTEX descriptor 1 is available.
298
  */
299 2 ZTEX
    public final boolean valid() {
300
        return valid;
301
    }
302
 
303
// ******* usbVendorId *********************************************************
304 3 ZTEX
/**
305
  * Returns the USB vendor ID of the device.
306
  * @return the USB vendor ID of the device.
307
  */
308 2 ZTEX
    public final int usbVendorId() {
309
        return usbVendorId;
310
    }
311
 
312
// ******* usbProductId *********************************************************
313 3 ZTEX
/**
314
  * Returns the USB product ID of the device.
315
  * @return the USB product ID of the device.
316
  */
317 2 ZTEX
    public final int usbProductId() {
318
        return usbProductId;
319
    }
320
 
321
// ******* manufacturerString **************************************************
322 3 ZTEX
/**
323
  * Returns the manufacturer string of the device.
324
  * @return the manufacturer string of the device.
325
  */
326 2 ZTEX
    public final String manufacturerString() {
327
        return manufacturerString;
328
    }
329
 
330
// ******* productString *******************************************************
331 3 ZTEX
/**
332
  * Returns the product string of the device.
333
  * @return the product string of the device.
334
  */
335 2 ZTEX
    public final String productString() {
336
        return productString;
337
    }
338
 
339
// ******* snString ************************************************************
340 3 ZTEX
/**
341
  * Returns the serial number string of the device.
342
  * @return the serial number string of the device.
343
  */
344 2 ZTEX
    public final String snString() {
345
        return snString;
346
    }
347
 
348
// ******* productId ***********************************************************
349 3 ZTEX
/**
350
  * Returns the product ID (all 4 bytes).
351
  * @return PRODUCT_ID, see above.
352
  */
353 2 ZTEX
    public final byte[] productId() {
354
        return productId;
355
    }
356
 
357 3 ZTEX
/**
358
  * Returns byte i of the product ID.
359
  * @return PRODUCT_ID[i], see above.
360
  * @param i index
361
  */
362 2 ZTEX
    public int productId( int i ) {
363
        return productId[i] & 255;
364
    }
365
 
366
// ******* fwVersion ***********************************************************
367 3 ZTEX
/**
368
  * Returns the firmware version.
369
  * @return FW_VERSION, see above.
370
  */
371 2 ZTEX
    public final int fwVersion() {
372
        return fwVersion & 255;
373
    }
374
 
375
// ******* interfaceVersion *****************************************************
376 3 ZTEX
/**
377
  * Returns the interface version.
378
  * @return INTERFACE_VERSION, see above.
379
  */
380 2 ZTEX
    public final int interfaceVersion() {
381
        return interfaceVersion & 255;
382
    }
383
 
384
// ******* interfaceCapabilities ************************************************
385 3 ZTEX
/**
386
  * Returns the interface capabilities (all 6 bytes).
387
  * @return INTERFACE_CAPABILITIES, see above.
388
  */
389 2 ZTEX
    public final byte[] interfaceCapabilities() {
390
        return interfaceCapabilities;
391
    }
392
 
393 3 ZTEX
/**
394
  * Returns byte i of the interface capabilities.
395
  * @return INTERFACE_CAPABILITIES[i], see above.
396
  * @param i index
397
  */
398 2 ZTEX
    public final int interfaceCapabilities( int i ) {
399
        return interfaceCapabilities[i] & 255;
400
    }
401
 
402 3 ZTEX
/**
403
  * Returns byte i, bit j  of the interface capabilities.
404
  * @return INTERFACE_CAPABILITIES[i].j, see above.
405
  * @param i byte index
406
  * @param j bit index
407
  */
408 2 ZTEX
    public final boolean interfaceCapabilities( int i, int j ) {
409
        return  (i>=0) && (i<=5) && (j>=0) && (j<8) &&
410
                (((interfaceCapabilities[i] & 255) & (1 << j)) != 0);
411
    }
412
 
413
// ******* moduleReserved ******************************************************
414 3 ZTEX
/**
415
  * Returns the application specific information (all 12 bytes).
416
  * @return MODULE_RESERVED, see above.
417
  */
418 2 ZTEX
    public final byte[] moduleReserved() {
419
        return moduleReserved;
420
    }
421
 
422 3 ZTEX
/**
423
  * Returns byte i of the application specific information.
424
  * @return MODULE_RESERVED[i], see above.
425
  * @param i index
426
  */
427 2 ZTEX
    public final int moduleReserved( int i ) {
428
        return moduleReserved[i] & 255;
429
    }
430
 
431
}

powered by: WebSVN 2.1.0

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