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/] [ZtexScanBus1.java] - Blame information for rev 4

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
    Scan bus for devices with ZTEX descriptor 1 and/or Cypress EZ-USB FX2 devices
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 used for finding the EZ-USB devices on the USB.
31
  * The devices found are stored as a list of {@link ZtexDevice1} instances.
32
  * @see ZtexDevice1
33
  */
34
 
35 2 ZTEX
public class ZtexScanBus1 {
36
    private Vector<ZtexDevice1> devices = new Vector<ZtexDevice1>();
37
 
38
// ******* ZtexScanBus1 ********************************************************
39 3 ZTEX
/**
40
  * Scans the USB for suitable devices and constructs a list of them.
41
  * Four kinds of search filters can be applied
42
  * <ol>
43
  *   <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
44
  *   <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
45
  *   <li> Incompatible devices can be excluded by the specification of the ZTEX product ID's, see {@link ZtexDevice1#compatible(int,int,int,int)}. </li>
46
  *   <li> If scanCypress is true, all devices (even unconfigured ones) with Cypress EZ-USB vendor and product ID's are considered. </li>
47
  * </ol>
48
  * @param usbVendorId USB vendor ID of the device to be searched for
49
  * @param usbProductId USB product ID of the device to be searched for
50
  * @param scanCypress Include devices with Cypress EZ-USB vendor and product ID's (even unconfigured ones)
51
  * @param quiet if true, don't print any warnings
52
  * @param interfaceVersion The required interface version (&lt;0 if no interface version is required)
53
  * @param productId0 Byte 0 of a given ZTEX product ID (&le;0 if not to be considered)
54
  * @param productId1 Byte 1 of a given ZTEX product ID (&le;0 if not to be considered)
55
  * @param productId2 Byte 2 of a given ZTEX product ID (&le;0 if not to be considered)
56
  * @param productId3 Byte 3 of a given ZTEX product ID (&le;0 if not to be considered)
57
  */
58 2 ZTEX
    public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanCypress, boolean quiet, int interfaceVersion, int productId0, int productId1, int productId2, int productId3 ) {
59
        LibusbJava.usb_find_busses();
60
        LibusbJava.usb_find_devices();
61
 
62
        Usb_Bus bus = LibusbJava.usb_get_busses();
63
 
64
        while ( bus != null ) {
65
            Usb_Device dev = bus.getDevices();
66
            while ( dev != null ) {
67
                try {
68
                    try {
69
                        ZtexDevice1 zdev = new ZtexDevice1( dev, usbVendorId, usbProductId );
70
                        if ( ( scanCypress && zdev.isCypress() ) ||
71
                             ( zdev.valid() && (interfaceVersion<0 || zdev.interfaceVersion()==interfaceVersion) && zdev.compatible(productId0, productId1, productId2, productId3) ) ) {
72
                            devices.add( zdev );
73
                        }
74
                    }
75 3 ZTEX
                    catch ( InvalidFirmwareException e ) {
76 2 ZTEX
                        if ( scanCypress && usbVendorId == ZtexDevice1.cypressVendorId && usbProductId == ZtexDevice1.cypressProductId ) {
77
                            try {
78
                                ZtexDevice1 zdev = new ZtexDevice1( dev, -1, -1 );
79
                                if ( zdev.isCypress() ) devices.add( zdev );
80
                            }
81 3 ZTEX
                            catch ( InvalidFirmwareException e2 ) {
82 2 ZTEX
                                if ( ! quiet )
83
                                    System.err.println( e2.getLocalizedMessage() );             // should never occur
84
                            }
85
                        }
86
                        else {
87
                            if ( ! quiet )
88
                                System.err.println( e.getLocalizedMessage() );
89
                        }
90
                    }
91
                }
92
                catch ( UsbException e ) {
93
                    if ( ! quiet )
94
                        System.err.println( e.getLocalizedMessage() );
95
                }
96
                dev = dev.getNext();
97
            }
98
            bus = bus.getNext();
99
        }
100
    }
101
 
102 3 ZTEX
/**
103
  * Scans the USB for suitable devices and constructs a list of them.
104
  * Three kinds of search filters can be applied
105
  * <ol>
106
  *   <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
107
  *   <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
108
  *   <li> If scanCypress is true, all devices (even unconfigured ones) with Cypress EZ-USB vendor and product ID's are considered. </li>
109
  * </ol>
110
  * @param usbVendorId USB vendor ID of the device to be searched for
111
  * @param usbProductId USB product ID of the device to be searched for
112
  * @param scanCypress Include devices with Cypress EZ-USB vendor and product ID's (even unconfigured ones)
113
  * @param quiet if true, don't print any warnings
114
  * @param interfaceVersion The required interface version (<0 if no interface version is required)
115
  */
116 2 ZTEX
    public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanCypress, boolean quiet, int interfaceVersion ) {
117
        this(usbVendorId, usbProductId, scanCypress, quiet, interfaceVersion, -1,-1,-1,-1 );
118
    }
119
 
120 3 ZTEX
/**
121
  * Scans the USB for suitable devices and constructs a list of them.
122
  * Two kinds of search filters can be applied
123
  * <ol>
124
  *   <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
125
  *   <li> If scanCypress is true, all devices (even unconfigured ones) with Cypress EZ-USB vendor and product ID's are considered. </li>
126
  * </ol>
127
  * @param usbVendorId USB vendor ID of the device to be searched for
128
  * @param usbProductId USB product ID of the device to be searched for
129
  * @param scanCypress Include devices with Cypress EZ-USB vendor and product ID's (even unconfigured ones)
130
  * @param quiet if true, don't print any warnings
131
  */
132 2 ZTEX
    public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanCypress, boolean quiet ) {
133
        this(usbVendorId, usbProductId, scanCypress, quiet, -1, -1,-1,-1,-1 );
134
    }
135
 
136
// ******* printBus ************************************************************
137 3 ZTEX
/**
138
  * Prints out a list of devices found.
139
  * @param out Where the output is to be printed to.
140
  */
141 2 ZTEX
    public void printBus( PrintStream out ) {
142
        for (int i=0; i<devices.size(); i++ ) {
143
            out.println( i + ": " + devices.elementAt(i).toString() );
144
        }
145
    }
146
 
147
// ******* numberOfDevices *****************************************************
148 3 ZTEX
/**
149
  * Returns the number of devices found.
150
  * @return the number of devices found.
151
  */
152 2 ZTEX
    public final int numberOfDevices () {
153
        return devices.size();
154
    }
155
 
156
// ******* device **************************************************************
157 3 ZTEX
/**
158
  * Returns a device from the list of devices.
159
  * @param i The device index.
160
  * @return a device from the list of devices.
161
  * @throws IndexOutOfBoundsException if i&lt;0 or i&ge;{@link #numberOfDevices()}
162
  */
163 2 ZTEX
    public final ZtexDevice1 device (int i) throws IndexOutOfBoundsException {
164
        if ( i<0 || i>=devices.size() )
165
            throw new IndexOutOfBoundsException( "Device number out of range. Valid numbers are 0.." + (devices.size()-1) );
166
        return devices.elementAt(i);
167
    }
168
}
169
 

powered by: WebSVN 2.1.0

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