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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [examples/] [fx2demo/] [Fx2Demo.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   fx2demo -- Demonstrates common features of the FX2
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   Licensed under the Apache License, Version 2.0 (the "License");
7
   you may not use this file except in compliance with the License.
8
   You may obtain a copy of the License at
9
 
10
       http://www.apache.org/licenses/LICENSE-2.0
11
 
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
%*/
18
 
19
import java.io.*;
20
import java.util.*;
21
import java.nio.*;
22
 
23
import org.usb4java.*;
24
 
25
import ztex.*;
26
 
27
// *****************************************************************************
28
// ******* ParameterException **************************************************
29
// *****************************************************************************
30
// Exception the prints a help message
31
class ParameterException extends Exception {
32
    public final static String helpMsg = new String (
33
                "Parameters:\n"+
34
                "    -d <number>  Device Number (default: 0)\n" +
35
                "    -f           Force uploads\n" +
36
                "    -p           Print bus info\n" +
37
                "    -h           This help" );
38
 
39
    public ParameterException (String msg) {
40
        super( msg + "\n" + helpMsg );
41
    }
42
}
43
 
44
// *****************************************************************************
45
// ******* Fx2Demo *************************************************************
46
// *****************************************************************************
47
//class Fx2Demo extends Ztex1v1 {
48
class Fx2Demo extends Ztex1v1 {
49
 
50
// ******* Fx2Demo *************************************************************
51
// constructor
52
    public Fx2Demo ( ZtexDevice1 pDev ) throws UsbException {
53
        super ( pDev );
54
    }
55
 
56
// ******* echo ****************************************************************
57
// writes a string to Endpoint 4, reads it back from Endpoint 2 and writes the output to System.out
58
    public void echo ( String input ) throws UsbException {
59
        int i = bulkWrite(0x04, allocateByteBuffer(input.getBytes()) , 1000);
60
        if ( i<0 ) throw new UsbException("Error sending data: " + LibUsb.strError(i));
61
        System.out.println("Send "+i+" bytes: `"+input+"'" );
62
 
63
        try {
64
            Thread.sleep( 10 );
65
        }
66
            catch ( InterruptedException e ) {
67
        }
68
 
69
        ByteBuffer buffer = BufferUtils.allocateByteBuffer(1024);
70
        i = bulkRead(0x82, buffer, 1000);
71
        if ( i<0 ) throw new UsbException("Error receiving data: " + LibUsb.strError(i));
72
        else if (i==0) System.out.println("Read "+0+" bytes" );
73
        else {
74
            byte[] buf = new byte[i];
75
            buffer.get(buf);
76
            System.out.println("Read "+i+" bytes: `"+new String(buf,0,i)+"'" );
77
        }
78
    }
79
 
80
 
81
// ******* main ****************************************************************
82
    public static void main (String args[]) {
83
 
84
        int devNum = 0;
85
        boolean force = false;
86
 
87
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
88
            Runtime.getRuntime().addShutdownHook(new Thread() {
89
                public void run() {
90
                    Scanner s=new Scanner(System.in);
91
                    System.out.println("Press <enter> to continue ...");
92
                    s.nextLine();
93
                }
94
            });
95
        }
96
 
97
        try {
98
// Scan the USB. This also creates and initializes a new USB context.
99
            ZtexScanBus1 bus = new ZtexScanBus1(ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
100
 
101
// scan the command line arguments
102
            for (int i=0; i<args.length; i++ ) {
103
                if ( args[i].equals("-d") ) {
104
                    i++;
105
                    try {
106
                        if (i>=args.length) throw new Exception();
107
                        devNum = Integer.parseInt( args[i] );
108
                    }
109
                    catch (Exception e) {
110
                        throw new ParameterException("Device number expected after -d");
111
                    }
112
                }
113
                else if ( args[i].equals("-f") ) {
114
                    force = true;
115
                }
116
                else if ( args[i].equals("-p") ) {
117
                    bus.printBus(System.out);
118
                    System.exit(0);
119
                }
120
                else if ( args[i].equals("-h") ) {
121
                    System.err.println(ParameterException.helpMsg);
122
                    System.exit(0);
123
                }
124
                else throw new ParameterException("Invalid Parameter: "+args[i]);
125
            }
126
 
127
// create the main class            
128
            if ( bus.numberOfDevices() <= 0) {
129
                System.err.println("No devices found");
130
                System.exit(0);
131
            }
132
            Fx2Demo ztex = new Fx2Demo ( bus.device(devNum) );
133
            bus.unref();
134
 
135
// upload the firmware if necessary
136
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("fx2demo for EZ-USB devices")  ) {
137
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "fx2demo.ihx", force ) + " ms");
138
            }
139
            else {
140
                ztex.resetDevice(false);        // sync data toggles
141
            }
142
 
143
// claim interface 0
144
            ztex.trySetConfiguration ( 1 );
145
            ztex.claimInterface ( 0 );
146
 
147
// read string from stdin and write it to USB device
148
            String str = "";
149
            BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
150
            while ( ! str.equals("quit") ) {
151
                System.out.print("Enter a string or `quit' to exit the program: ");
152
                str = reader.readLine();
153
                if ( ! str.equals("") )
154
                    ztex.echo(str);
155
                System.out.println("");
156
            }
157
 
158
// print staistics stored in debug stack
159
            byte[] buf = new byte[400];
160
            int j = ztex.debugReadMessages(true,buf);
161
            System.out.println(ztex.debugLastMsg() + " transactions: ");
162
            for (int i=0; i<j; i++ )
163
                System.out.println( "    " + (i+1) + ": converted " + ((buf[i*4+2] & 255) | ((buf[i*4+3] & 255)<<8)) + " / " + ((buf[i*4] & 255) | ((buf[i*4+1] & 255)<<8)) + " characters");
164
 
165
// release interface 0
166
            ztex.releaseInterface(0);
167
            ztex.dispose();
168
 
169
        }
170
        catch (Exception e) {
171
            System.out.println("Error: "+e.getLocalizedMessage() );
172
        }
173
 
174
   }
175
 
176
}

powered by: WebSVN 2.1.0

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