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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [examples/] [usb-fpga-1.15y/] [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 for ZTEX USB-FPGA Module 1.15b and 1.15y2
3
   Copyright (C) 2009-2014 ZTEX GmbH.
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
import java.io.*;
20
import java.util.*;
21
 
22
import ch.ntb.usb.*;
23
 
24
import ztex.*;
25
 
26
// *****************************************************************************
27
// ******* ParameterException **************************************************
28
// *****************************************************************************
29
// Exception the prints a help message
30
class ParameterException extends Exception {
31
    public final static String helpMsg = new String (
32
                "Parameters:\n"+
33
                "    -d <number>       Device Number (default: 0)\n" +
34
                "    -f                Force uploads\n" +
35
                "    -p                Print bus info\n" +
36
                "    -w                Enable certain workarounds\n"+
37
                "    -h                This help" );
38
 
39
    public ParameterException (String msg) {
40
        super( msg + "\n" + helpMsg );
41
    }
42
}
43
 
44
// *****************************************************************************
45
// ******* Test0 ***************************************************************
46
// *****************************************************************************
47
class UCEcho extends Ztex1v1 {
48
 
49
// ******* UCEcho **************************************************************
50
// constructor
51
    public UCEcho ( ZtexDevice1 pDev ) throws UsbException {
52
        super ( pDev );
53
    }
54
 
55
// ******* echo ****************************************************************
56
// writes a string to Endpoint 4, reads it back from Endpoint 2 and writes the output to System.out
57
    public void echo ( int fpga, String input ) throws UsbException, InvalidFirmwareException, IndexOutOfBoundsException {
58
        byte buf[] = input.getBytes();
59
 
60
        selectFpga(fpga);
61
 
62
        int i = LibusbJava.usb_bulk_write(handle(), 0x04, buf, buf.length, 1000);
63
        if ( i<0 )
64
            throw new UsbException("Error sending data: " + LibusbJava.usb_strerror());
65
        System.out.println("FPGA " + fpga + ": Send "+i+" bytes: `"+input+"'" );
66
 
67
        try {
68
            Thread.sleep( 10 );
69
        }
70
            catch ( InterruptedException e ) {
71
        }
72
 
73
        buf = new byte[1024];
74
        i = LibusbJava.usb_bulk_read(handle(), 0x82, buf, 1024, 1000);
75
        if ( i<0 )
76
            throw new UsbException("Error receiving data: " + LibusbJava.usb_strerror());
77
        System.out.println("FPGA " + fpga + ": Read "+i+" bytes: `"+new String(buf,0,i)+"'" );
78
    }
79
 
80
// ******* main ****************************************************************
81
    public static void main (String args[]) {
82
 
83
        int devNum = 0;
84
        boolean force = false;
85
        boolean workarounds = false;
86
 
87
        try {
88
// init USB stuff
89
            LibusbJava.usb_init();
90
 
91
// scan the USB bus
92
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
93
            if ( bus.numberOfDevices() <= 0) {
94
                System.err.println("No devices found");
95
                System.exit(0);
96
            }
97
 
98
// scan the command line arguments
99
            for (int i=0; i<args.length; i++ ) {
100
                if ( args[i].equals("-d") ) {
101
                    i++;
102
                    try {
103
                        if (i>=args.length) throw new Exception();
104
                        devNum = Integer.parseInt( args[i] );
105
                    }
106
                    catch (Exception e) {
107
                        throw new ParameterException("Device number expected after -d");
108
                    }
109
                }
110
                else if ( args[i].equals("-f") ) {
111
                    force = true;
112
                }
113
                else if ( args[i].equals("-p") ) {
114
                    bus.printBus(System.out);
115
                    System.exit(0);
116
                }
117
                else if ( args[i].equals("-p") ) {
118
                    bus.printBus(System.out);
119
                    System.exit(0);
120
                }
121
                else if ( args[i].equals("-w") ) {
122
                    workarounds = true;
123
                }
124
                else if ( args[i].equals("-h") ) {
125
                        System.err.println(ParameterException.helpMsg);
126
                        System.exit(0);
127
                }
128
                else throw new ParameterException("Invalid Parameter: "+args[i]);
129
            }
130
 
131
 
132
// create the main class            
133
            UCEcho ztex = new UCEcho ( bus.device(devNum) );
134
            ztex.certainWorkarounds = workarounds;
135
 
136
// upload the firmware if necessary
137
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("ucecho example for UFM 1.15y")  ) {
138
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "ucecho.ihx", force ) + " ms");
139
                force=true;
140
            }
141
 
142
            System.out.println("" + ztex.numberOfFpgas() + " FPGA's found");
143
 
144
// upload the bitstream if necessary
145
            for (int i=0; i<ztex.numberOfFpgas(); i++ ) {
146
                ztex.selectFpga(i);
147
                if ( force || ! ztex.getFpgaConfiguration() ) {
148
                    System.out.print("FPGA "+i+": ");
149
                    System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/ucecho.bit" , force ) + " ms");
150
                }
151
            }
152
 
153
// claim interface 0
154
            ztex.trySetConfiguration ( 1 );
155
            ztex.claimInterface ( 0 );
156
 
157
// read string from stdin and write it to USB device
158
            String str = "";
159
            BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
160
            while ( ! str.equals("quit") ) {
161
                System.out.print("Enter a string or `quit' to exit the program: ");
162
                str = reader.readLine();
163
                if ( ! str.equals("") ) {
164
                    for ( int i=0; i<ztex.numberOfFpgas(); i++ )
165
                        ztex.echo(i, str);
166
                }
167
                System.out.println("");
168
            }
169
 
170
// release interface 0
171
            ztex.releaseInterface( 0 );
172
 
173
        }
174
        catch (Exception e) {
175
            System.out.println("Error: "+e.getLocalizedMessage() );
176
        }
177
   }
178
 
179
}

powered by: WebSVN 2.1.0

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