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 2

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
public class ZtexDevice1 {
30
    public static final int cypressVendorId = 0x4b4;
31
    public static final int cypressProductId = 0x8613;
32
 
33
    private Usb_Device dev = null;
34
    private boolean isCypress = false;          // true if Cypress device
35
    private boolean valid = false;              // true if descriptor 1 is available
36
    private int usbVendorId = -1;
37
    private int usbProductId = -1;
38
    private String manufacturerString = null;
39
    private String productString = null;
40
    private String snString = null;
41
    private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descpriptor, not the USB product ID
42
    private byte fwVersion = 0;
43
    private byte interfaceVersion = 0;
44
    private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
45
    private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
46
 
47
// ******* byteArrayString *****************************************************
48
    public static String byteArrayString ( byte buf[] ) {
49
        String s = new String( "" );
50
        for ( int i = 0; i<buf.length; i++ ) {
51
            if ( i != 0 )
52
                s+=".";
53
            s+=buf[i] & 255;
54
        }
55
        return s;
56
    }
57
 
58
// ******* ZtexDevice1 *********************************************************
59
// Read the ZTEX descriptor if usbVendorId=pUsbVendorId and usbProductId=pUsbProductId
60
    public ZtexDevice1 (Usb_Device p_dev, int pUsbVendorId, int pUsbProductId) throws UsbException, ZtexDescriptorException {
61
        dev = p_dev;
62
 
63
        Usb_Device_Descriptor dd = dev.getDescriptor();
64
        usbVendorId = dd.getIdVendor() & 65535;
65
        usbProductId = dd.getIdProduct() & 65535;
66
 
67
        if ( usbVendorId == cypressVendorId  &&  usbProductId == cypressProductId ) {
68
            isCypress = true;
69
        }
70
 
71
        int handle = LibusbJava.usb_open(dev);
72
 
73
        if ( handle > 0 ) {
74
            if ( dd.getIManufacturer() > 0 )
75
                manufacturerString = LibusbJava.usb_get_string_simple( handle, dd.getIManufacturer() );
76
            if ( dd.getIProduct() > 0 )
77
                productString = LibusbJava.usb_get_string_simple( handle, dd.getIProduct() );
78
            if ( dd.getISerialNumber() > 0 )
79
                snString = LibusbJava.usb_get_string_simple( handle, dd.getISerialNumber() );
80
 
81
            if ( usbVendorId == pUsbVendorId  &&  usbProductId == pUsbProductId ) {
82
                if ( snString == null ) {
83
                    LibusbJava.usb_close(handle);
84
                    throw new ZtexDescriptorException( dev, "Not a ZTEX device" );  // ZTEX devices always have a SN. See also the next comment a few lines below
85
                }
86
 
87
                byte[] buf = new byte[42];
88
                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.
89
                if ( i < 0 ) {
90
                    LibusbJava.usb_close(handle);
91
                    throw new ZtexDescriptorException( dev, "Error reading ZTEX descriptor: " + LibusbJava.usb_strerror() );
92
                }
93
                else if ( i != 40 ) {
94
                    LibusbJava.usb_close(handle);
95
                    throw new ZtexDescriptorException( dev, "Error reading ZTEX descriptor: Invalid size: " + i );
96
                }
97
                if ( buf[0]!=40 || buf[1]!=1 || buf[2]!='Z' || buf[3]!='T' || buf[4]!='E' || buf[5]!='X' ) {
98
                    LibusbJava.usb_close(handle);
99
                    throw new ZtexDescriptorException( dev, "Invalid ZTEX descriptor" );
100
                }
101
                productId[0] = buf[6];
102
                productId[1] = buf[7];
103
                productId[2] = buf[8];
104
                productId[3] = buf[9];
105
                fwVersion = buf[10];
106
                interfaceVersion = buf[11];
107
                interfaceCapabilities[0] = buf[12];
108
                interfaceCapabilities[1] = buf[13];
109
                interfaceCapabilities[2] = buf[14];
110
                interfaceCapabilities[3] = buf[15];
111
                interfaceCapabilities[4] = buf[16];
112
                interfaceCapabilities[5] = buf[17];
113
                moduleReserved[0] = buf[18];
114
                moduleReserved[1] = buf[19];
115
                moduleReserved[2] = buf[20];
116
                moduleReserved[3] = buf[21];
117
                moduleReserved[4] = buf[22];
118
                moduleReserved[5] = buf[23];
119
                moduleReserved[6] = buf[24];
120
                moduleReserved[7] = buf[25];
121
                moduleReserved[8] = buf[26];
122
                moduleReserved[9] = buf[27];
123
                moduleReserved[10] = buf[28];
124
                moduleReserved[11] = buf[29];
125
 
126
                valid = true;
127
            }
128
        }
129
        else {
130
            throw new UsbException( dev, "Error opening device: " + LibusbJava.usb_strerror() );
131
        }
132
 
133
        LibusbJava.usb_close(handle);
134
    }
135
 
136
// ******* toString ************************************************************
137
    public String toString () {
138
        return "bus=" + dev.getBus().getDirname() + "  device=" + dev.getFilename() +
139
            "\n   " + ( isCypress ? "Cypress" : "" ) +
140
              ( manufacturerString == null ? "" : ("    Manufacturer=\""  + manufacturerString + "\"") ) +
141
              ( productString == null ? "" : ("  Product=\""  + productString + "\"") ) +
142
              ( snString == null ? "" : ("    SerialNumber=\""  + snString + "\"") ) +
143
            ( valid ? "\n   productID=" + byteArrayString(productId) + "  fwVer="+(fwVersion & 255) + "  ifVer="+(interfaceVersion & 255)  : "" );
144
    }
145
 
146
// ******* compatible **********************************************************
147
    public final boolean compatible( int productId0, int productId1, int productId2, int productId3 ) {
148
        return ( productId[0]==0 || productId0<=0 || (productId[0] & 255) == productId0 ) &&
149
               ( productId[1]==0 || productId1<=0 || (productId[1] & 255) == productId1 ) &&
150
               ( productId[2]==0 || productId2<=0 || (productId[2] & 255) == productId2 ) &&
151
               ( productId[3]==0 || productId3<=0 || (productId[3] & 255) == productId3 );
152
    }
153
 
154
// ******* dev *****************************************************************
155
    public final Usb_Device dev() {
156
        return dev;
157
    }
158
 
159
// ******* isCypress ***********************************************************
160
    public final boolean isCypress() {
161
        return isCypress;
162
    }
163
 
164
// ******* valid ***************************************************************
165
    public final boolean valid() {
166
        return valid;
167
    }
168
 
169
// ******* usbVendorId *********************************************************
170
    public final int usbVendorId() {
171
        return usbVendorId;
172
    }
173
 
174
// ******* usbProductId *********************************************************
175
    public final int usbProductId() {
176
        return usbProductId;
177
    }
178
 
179
// ******* manufacturerString **************************************************
180
    public final String manufacturerString() {
181
        return manufacturerString;
182
    }
183
 
184
// ******* productString *******************************************************
185
    public final String productString() {
186
        return productString;
187
    }
188
 
189
// ******* snString ************************************************************
190
    public final String snString() {
191
        return snString;
192
    }
193
 
194
// ******* productId ***********************************************************
195
    public final byte[] productId() {
196
        return productId;
197
    }
198
 
199
    public int productId( int i ) {
200
        return productId[i] & 255;
201
    }
202
 
203
// ******* fwVersion ***********************************************************
204
    public final int fwVersion() {
205
        return fwVersion & 255;
206
    }
207
 
208
// ******* interfaceVersion *****************************************************
209
    public final int interfaceVersion() {
210
        return interfaceVersion & 255;
211
    }
212
 
213
// ******* interfaceCapabilities ************************************************
214
    public final byte[] interfaceCapabilities() {
215
        return interfaceCapabilities;
216
    }
217
 
218
    public final int interfaceCapabilities( int i ) {
219
        return interfaceCapabilities[i] & 255;
220
    }
221
 
222
    public final boolean interfaceCapabilities( int i, int j ) {
223
        return  (i>=0) && (i<=5) && (j>=0) && (j<8) &&
224
                (((interfaceCapabilities[i] & 255) & (1 << j)) != 0);
225
    }
226
 
227
// ******* moduleReserved ******************************************************
228
    public final byte[] moduleReserved() {
229
        return moduleReserved;
230
    }
231
 
232
    public final int moduleReserved( int i ) {
233
        return moduleReserved[i] & 255;
234
    }
235
 
236
}

powered by: WebSVN 2.1.0

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