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/] [ucecho/] [UCEcho.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   ucecho -- Uppercase conversion example using the low speed interface of default firmware
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.text.*;
22
import java.nio.*;
23
 
24
import org.usb4java.*;
25
 
26
import ztex.*;
27
 
28
// *****************************************************************************
29
// ******* ParameterException **************************************************
30
// *****************************************************************************
31
// Exception the prints a help message
32
class ParameterException extends Exception {
33
    public final static String helpMsg = new String (
34
                "Parameters:\n"+
35
                "    -d <number>       Device Number (default: 0)\n" +
36
                "    -r                Reset EZ-USB\n" +
37
                "    -p                Print bus info\n" +
38
                "    -h                This help" );
39
 
40
    public ParameterException (String msg) {
41
        super( msg + "\n" + helpMsg );
42
    }
43
}
44
 
45
 
46
// *****************************************************************************
47
// ******* UCEcho **************************************************************
48
// *****************************************************************************
49
class UCEcho extends Ztex1v1 {
50
 
51
    // constructor
52
    public UCEcho ( ZtexDevice1 pDev ) throws UsbException {
53
        super ( pDev );
54
    }
55
 
56
    public void echo ( int addr, String input ) throws InvalidFirmwareException, UsbException, CapabilityException, IndexOutOfBoundsException {
57
        byte buf[] = input.getBytes();
58
        if (buf.length<1) return;
59
        int length = (buf.length+3)>>2;
60
 
61
        byte buf3[] = new byte[length*4];
62
        for (int i=0; i<buf.length; i++) buf3[i]=buf[i];
63
 
64
        int buf2[] = new int[length];
65
        for (int i=0; i<length; i++)
66
            buf2[i] = (buf3[i*4+0] & 255) | ((buf3[i*4+1] & 255)<<8) | ((buf3[i*4+2] & 255)<<16) | ((buf3[i*4+3] & 255)<<24);
67
 
68
        System.out.println("Send " + length + " words to "+addr+" ...");
69
        defaultLsiSet(addr,buf2,length);
70
 
71
        try {
72
            Thread.sleep( 10 );
73
        }
74
        catch ( InterruptedException e) {
75
        }
76
 
77
        defaultLsiGet(addr,buf2,length);
78
        for (int i=0; i<length; i++) {
79
            buf3[i*4+0] = (byte)(buf2[i]);
80
            buf3[i*4+1] = (byte)(buf2[i]>>8);
81
            buf3[i*4+2] = (byte)(buf2[i]>>16);
82
            buf3[i*4+3] = (byte)(buf2[i]>>24);
83
        }
84
        System.out.println("Read "+length+" words starting from "+addr+": `"+new String(buf3,0,buf.length)+"'" );
85
 
86
        if ( length>1 ) {
87
            defaultLsiGet(addr+1,buf2,length-1);
88
            for (int i=0; i<length; i++) {
89
                buf3[i*4+0] = (byte)(buf2[i]);
90
                buf3[i*4+1] = (byte)(buf2[i]>>8);
91
                buf3[i*4+2] = (byte)(buf2[i]>>16);
92
                buf3[i*4+3] = (byte)(buf2[i]>>24);
93
            }
94
            System.out.println("Read "+(length-1)+" words starting from "+(addr+1)+": `"+new String(buf3,0,buf.length-4)+"'" );
95
        }
96
 
97
    }
98
 
99
 
100
// ******* main ****************************************************************
101
    public static void main (String args[]) {
102
 
103
        int devNum = 0;
104
        boolean reset = false;
105
 
106
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
107
            Runtime.getRuntime().addShutdownHook(new Thread() {
108
                public void run() {
109
                    Scanner s=new Scanner(System.in);
110
                    System.out.println("Press <enter> to continue ...");
111
                    s.nextLine();
112
                }
113
            });
114
        }
115
 
116
        try {
117
// Scan the USB. This also creates and initializes a new USB context.
118
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
119
 
120
// scan the command line arguments
121
            for (int i=0; i<args.length; i++ ) {
122
                if ( args[i].equals("-d") ) {
123
                    i++;
124
                    try {
125
                        if (i>=args.length) throw new Exception();
126
                        devNum = Integer.parseInt( args[i] );
127
                    }
128
                    catch (Exception e) {
129
                        throw new ParameterException("Device number expected after -d");
130
                    }
131
                }
132
                else if ( args[i].equals("-r") ) {
133
                    reset = true;
134
                }
135
                else if ( args[i].equals("-p") ) {
136
                    bus.printBus(System.out);
137
                    System.exit(0);
138
                }
139
                else if ( args[i].equals("-p") ) {
140
                    bus.printBus(System.out);
141
                    System.exit(0);
142
                }
143
                else if ( args[i].equals("-h") ) {
144
                    System.err.println(ParameterException.helpMsg);
145
                    System.exit(0);
146
                }
147
                else throw new ParameterException("Invalid Parameter: "+args[i]);
148
            }
149
 
150
            String errStr = "";
151
 
152
// create the main class            
153
            if ( bus.numberOfDevices() <= 0) {
154
                System.err.println("No devices found");
155
                System.exit(0);
156
            }
157
            UCEcho ztex = new UCEcho ( bus.device(devNum) );
158
            bus.unref();
159
 
160
            try {
161
 
162
// reset EZ-USB
163
                if ( reset ) {
164
                    System.out.println("Reset EZ-USB");
165
                    ztex.resetEzUsb();
166
                }
167
                else {
168
                    ztex.resetDevice(false);    // sync data toggles
169
                }
170
 
171
// check for firmware
172
                ztex.defaultCheckVersion(1);
173
                ztex.debug2PrintNextLogMessages(System.out);
174
                if (ztex.config == null ) throw new Exception("Invalid configuration data");
175
 
176
// upload the bitstream 
177
                String configFN = ztex.config.defaultBitstreamPath("ucecho");
178
                System.out.println("Found " + ztex.config.getName()+",  using bitstream "+configFN);
179
                System.out.println("FPGA configuration time: " + ztex.configureFpga( configFN , true, -1 ) + " ms");
180
 
181
                ztex.debug2PrintNextLogMessages(System.out);
182
 
183
// ucecho conversion
184
                String str = "";
185
                BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
186
                while ( ! str.equals("quit") ) {
187
                    System.out.print("Enter a string or `quit' to exit the program: ");
188
                    str = reader.readLine();
189
                    if ( ! str.equals("") )
190
                        ztex.echo(10,str);
191
                    System.out.println("");
192
                }
193
 
194
                ztex.debug2PrintNextLogMessages(System.out);
195
                ztex.dispose();  // this also releases claimed interfaces
196
 
197
            }
198
            catch (Exception e) {
199
                System.out.println("Error: "+e.getLocalizedMessage() );
200
                ztex.debug2PrintNextLogMessages(System.out);
201
            }
202
        }
203
        catch (Exception e) {
204
            System.out.println("Error: "+e.getLocalizedMessage() );
205
        }
206
 
207
   }
208
 
209
}

powered by: WebSVN 2.1.0

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