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/] [Ztex1.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
    Functions for USB devices with ZTEX descriptor 1
21
*/
22
package ztex;
23
 
24
import java.io.*;
25
import java.util.*;
26
 
27
import ch.ntb.usb.*;
28
 
29
public class Ztex1 {
30
    protected int handle;
31
    protected ZtexDevice1 dev = null;
32
    private boolean oldDevices[] = new boolean[128];
33
    private String usbBusName = null;
34
 
35
// ******* Ztex1 ***************************************************************
36
    public Ztex1 ( ZtexDevice1 pDev ) throws UsbException {
37
        dev = pDev;
38
 
39
        handle = LibusbJava.usb_open(dev.dev());
40
        if ( handle<=0 )
41
            throw new UsbException(dev.dev(), "Error opening device");
42
    }
43
 
44
// ******* finalize ************************************************************
45
    protected void finalize () {
46
        LibusbJava.usb_close(handle);
47
    }
48
 
49
// ******* handle **************************************************************
50
    public final int handle()
51
    {
52
        return handle;
53
    }
54
 
55
// ******* dev *****************************************************************
56
    public final ZtexDevice1 dev()
57
    {
58
        return dev;
59
    }
60
 
61
// ******* valid ***************************************************************
62
    public boolean valid ( ) {
63
        return dev.valid();
64
    }
65
 
66
// ******* checkValid **********************************************************
67
    public void checkValid () throws InvalidFirmwareException {
68
        if ( ! dev.valid() )
69
            throw new InvalidFirmwareException(this, "Can't read ZTEX descriptor 1");
70
    }
71
 
72
// ******* vendorCommand *******************************************************
73
    public int vendorCommand (int cmd, String func, int value, int index, byte[] buf, int length) throws UsbException {
74
        int i;
75
        if ( (i=LibusbJava.usb_control_msg(handle, 0x40, cmd, value, index, buf, length, 1000)) < 0 )
76
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + LibusbJava.usb_strerror());
77
        try {
78
            Thread.sleep(1);                            // avoid package loss (due to interrupts ?)
79
        }
80
            catch ( InterruptedException e ) {
81
        }
82
        return i;
83
    }
84
 
85
    public int vendorCommand (int cmd, String func, int value, int index) throws UsbException {
86
        byte[] buf = { 0 };
87
        return vendorCommand (cmd, func, value, index, buf, 0);
88
    }
89
 
90
    public int vendorCommand (int cmd, String func) throws UsbException {
91
        byte[] buf = { 0 };
92
        return vendorCommand (cmd, func, 0, 0, buf, 0);
93
    }
94
 
95
// ******* vendorRequest *******************************************************
96
    public int vendorRequest (int cmd, String func, int value, int index, byte[] buf, int maxlen) throws UsbException {
97
        int i = LibusbJava.usb_control_msg(handle, 0xc0, cmd, value, index, buf, maxlen, 1000);
98
        if ( i < 0 )
99
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + LibusbJava.usb_strerror());
100
        try {
101
            Thread.sleep(1);                            // avoid package loss (due to interrupts ?)
102
        }
103
            catch ( InterruptedException e ) {
104
        }
105
        return i;
106
    }
107
 
108
    public int vendorRequest (int cmd, String func, byte[] buf, int maxlen) throws UsbException {
109
        return vendorRequest (cmd, func, 0, 0, buf, maxlen);
110
    }
111
 
112
// ******* vendorCommand2 ******************************************************
113
    public void vendorCommand2 (int cmd, String func, int value, int index, byte[] buf, int length) throws UsbException {
114
        int i = vendorCommand (cmd, func, value, index, buf, length);
115
        if ( i != length )
116
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + "Send " + i + " byte of data instead of " + length + " bytes");
117
    }
118
 
119
    public void vendorCommand2 (int cmd, String func, int value, int index) throws UsbException {
120
        byte[] buf = { 0 };
121
        vendorCommand2 (cmd, func, value, index, buf, 0);
122
    }
123
 
124
    public void vendorCommand2 (int cmd, String func) throws UsbException {
125
        byte[] buf = { 0 };
126
        vendorCommand2 (cmd, func, 0, 0, buf, 0);
127
    }
128
 
129
// ******* vendorRequest2 ******************************************************
130
    public void vendorRequest2 (int cmd, String func, int value, int index, byte[] buf, int maxlen) throws UsbException {
131
        int i = vendorRequest(cmd, func, value, index, buf, maxlen);
132
        if ( i != maxlen )
133
            throw new UsbException( dev.dev(), (func != null ? func + ": " : "" ) + "Received " + i + " byte of data, expected "+maxlen+" bytes");
134
    }
135
 
136
    public void vendorRequest2 (int cmd, String func, byte[] buf, int maxlen) throws UsbException {
137
        vendorRequest2(cmd, func, 0, 0, buf, maxlen);
138
    }
139
 
140
// ******* findOldDevices ******************************************************
141
    private void findOldDevices () {
142
        Usb_Bus bus = dev.dev().getBus();
143
        usbBusName = bus.getDirname();
144
 
145
        for ( int i=0; i<=127; i++ )
146
            oldDevices[i] = false;
147
 
148
        Usb_Device d = bus.getDevices();
149
        while ( d != null ) {
150
            byte b = d.getDevnum();
151
            if ( b > 0 )
152
                oldDevices[b] = true;
153
            d = d.getNext();
154
        }
155
        oldDevices[dev.dev().getDevnum()] = false;
156
    }
157
 
158
// ******* findNewDevice *******************************************************
159
    private Usb_Device findNewDevice ( String errMsg ) throws DeviceLostException {
160
        Usb_Device newDev = null;
161
        LibusbJava.usb_find_busses();
162
        LibusbJava.usb_find_devices();
163
 
164
        Usb_Bus bus = LibusbJava.usb_get_busses();
165
        while ( bus != null && ! bus.getDirname().equals(usbBusName) )
166
            bus = bus.getNext();
167
 
168
        Usb_Device d = bus != null ? bus.getDevices() : null;
169
        while ( d != null ) {
170
            byte b = d.getDevnum();
171
            if ( b > 0 && ! oldDevices[b] ) {
172
                if ( newDev != null )
173
                    throw new DeviceLostException( errMsg + "More than 2 new devices found" );
174
                newDev = d;
175
            }
176
            d = d.getNext();
177
        }
178
 
179
        return newDev;
180
    }
181
 
182
// ******* initNewDevice *******************************************************
183
    private void initNewDevice ( String errBase ) throws DeviceLostException, UsbException, ZtexDescriptorException {
184
// scan the bus for up to 5 s for a new device
185
        Usb_Device newDev = null;
186
        for ( int i=0; i<20 && newDev==null; i++ ) {
187
            try {
188
                Thread.sleep( 250 );
189
            }
190
                catch ( InterruptedException e ) {
191
            }
192
            newDev = findNewDevice( errBase + ": " );
193
        }
194
        if ( newDev == null )
195
            throw new DeviceLostException( errBase + ": No new device found" );
196
 
197
// init new device
198
        Usb_Device_Descriptor dd = newDev.getDescriptor();
199
        int vid = dd.getIdVendor() & 65535;
200
        int pid = dd.getIdProduct() & 65535;
201
        try {
202
            dev = new ZtexDevice1( newDev, vid, pid );
203
        }
204
        catch ( ZtexDescriptorException e ) {
205
            if ( vid == ZtexDevice1.cypressVendorId && pid == ZtexDevice1.cypressProductId ) {
206
                dev = new ZtexDevice1( newDev, -1, -1 );
207
            }
208
            else {
209
                throw e;
210
            }
211
        }
212
        handle = LibusbJava.usb_open( dev.dev() );
213
    }
214
 
215
// ******* uploadFirmware ******************************************************
216
//  returns upload time in ms
217
    public long uploadFirmware ( String ihxFileName, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, ZtexDescriptorException, DeviceLostException {
218
// load the ihx file
219
        ZtexIhxFile1 ihxFile;
220
        try {
221
            ihxFile = new ZtexIhxFile1( ihxFileName );
222
        }
223
        catch ( IOException e ) {
224
            throw new FirmwareUploadException( e.getLocalizedMessage() );
225
        }
226
        catch ( IhxFileDamagedException e ) {
227
            throw new FirmwareUploadException( e.getLocalizedMessage() );
228
        }
229
//      ihxFile.dataInfo(System.out);
230
//      System.out.println(ihxFile);
231
 
232
// check for compatibility
233
        if ( ! force && dev.valid() ) {
234
            if ( ihxFile.interfaceVersion() != 1 )
235
                throw new IncompatibleFirmwareException("Wrong interface version: Expected 1, got " + ihxFile.interfaceVersion() );
236
 
237
            if ( ! dev.compatible ( ihxFile.productId(0), ihxFile.productId(1), ihxFile.productId(2), ihxFile.productId(3) ) )
238
                throw new IncompatibleFirmwareException("Incompatible productId's: Current firmware: " + ZtexDevice1.byteArrayString(dev.productId())
239
                    + "  Ihx File: " + ZtexDevice1.byteArrayString(ihxFile.productId()) );
240
        }
241
 
242
// scan the bus for comparison
243
        findOldDevices();
244
 
245
// upload the firmware
246
        long time = EzUsb.uploadFirmware( handle, ihxFile );
247
 
248
// find and init new device
249
        initNewDevice("Device lost after uploading Firmware");
250
 
251
        return time;
252
    }
253
 
254
// ******* resetEzUsb **********************************************************
255
    public void resetEzUsb () throws IncompatibleFirmwareException, FirmwareUploadException, UsbException, ZtexDescriptorException, DeviceLostException {
256
// scan the bus for comparison
257
        findOldDevices();
258
 
259
// reset the EZ-USB
260
        EzUsb.reset(handle,true);
261
        try {
262
            EzUsb.reset(handle,false);          // error (may caused re-numeration) can be ignored
263
        }
264
        catch ( FirmwareUploadException e ) {
265
        }
266
 
267
// find and init new device
268
        initNewDevice( "Device lost after resetting the EZ-USB" );
269
    }
270
 
271
// ******* toString ************************************************************
272
    public String toString () {
273
        return dev.toString();
274
    }
275
 
276
}

powered by: WebSVN 2.1.0

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