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.15/] [1.15b/] [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
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 ( String input ) throws UsbException {
58
        byte buf[] = input.getBytes();
59
        int i = LibusbJava.usb_bulk_write(handle(), 0x04, buf, buf.length, 1000);
60
        if ( i<0 )
61
            throw new UsbException("Error sending data: " + LibusbJava.usb_strerror());
62
        System.out.println("Send "+i+" bytes: `"+input+"'" );
63
 
64
        try {
65
            Thread.sleep( 10 );
66
        }
67
            catch ( InterruptedException e ) {
68
        }
69
 
70
        buf = new byte[1024];
71
        i = LibusbJava.usb_bulk_read(handle(), 0x82, buf, 1024, 1000);
72
        if ( i<0 )
73
            throw new UsbException("Error receiving data: " + LibusbJava.usb_strerror());
74
        System.out.println("Read "+i+" bytes: `"+new String(buf,0,i)+"'" );
75
    }
76
 
77
// ******* main ****************************************************************
78
    public static void main (String args[]) {
79
 
80
        int devNum = 0;
81
        boolean force = false;
82
        boolean workarounds = false;
83
 
84
        try {
85
// init USB stuff
86
            LibusbJava.usb_init();
87
 
88
// scan the USB bus
89
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
90
            if ( bus.numberOfDevices() <= 0) {
91
                System.err.println("No devices found");
92
                System.exit(0);
93
            }
94
 
95
// scan the command line arguments
96
            for (int i=0; i<args.length; i++ ) {
97
                if ( args[i].equals("-d") ) {
98
                    i++;
99
                    try {
100
                        if (i>=args.length) throw new Exception();
101
                        devNum = Integer.parseInt( args[i] );
102
                    }
103
                    catch (Exception e) {
104
                        throw new ParameterException("Device number expected after -d");
105
                    }
106
                }
107
                else if ( args[i].equals("-f") ) {
108
                    force = true;
109
                }
110
                else if ( args[i].equals("-p") ) {
111
                    bus.printBus(System.out);
112
                    System.exit(0);
113
                }
114
                else if ( args[i].equals("-p") ) {
115
                    bus.printBus(System.out);
116
                    System.exit(0);
117
                }
118
                else if ( args[i].equals("-w") ) {
119
                    workarounds = true;
120
                }
121
                else if ( args[i].equals("-h") ) {
122
                        System.err.println(ParameterException.helpMsg);
123
                        System.exit(0);
124
                }
125
                else throw new ParameterException("Invalid Parameter: "+args[i]);
126
            }
127
 
128
 
129
// create the main class            
130
            UCEcho ztex = new UCEcho ( bus.device(devNum) );
131
            ztex.certainWorkarounds = workarounds;
132
 
133
// upload the firmware if necessary
134
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("ucecho example for UFM 1.15")  ) {
135
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "ucecho.ihx", force ) + " ms");
136
                force = true;
137
            }
138
 
139
// upload the bitstream if necessary
140
            if ( force || ! ztex.getFpgaConfiguration() ) {
141
                System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/ucecho.bit" , force ) + " ms");
142
            }
143
 
144
 
145
// claim interface 0
146
            ztex.trySetConfiguration ( 1 );
147
            ztex.claimInterface ( 0 );
148
 
149
// read string from stdin and write it to USB device
150
            String str = "";
151
            BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
152
            while ( ! str.equals("quit") ) {
153
                System.out.print("Enter a string or `quit' to exit the program: ");
154
                str = reader.readLine();
155
                if ( ! str.equals("") )
156
                    ztex.echo(str);
157
                System.out.println("");
158
            }
159
 
160
// release interface 0
161
            ztex.releaseInterface( 0 );
162
 
163
        }
164
        catch (Exception e) {
165
            System.out.println("Error: "+e.getLocalizedMessage() );
166
        }
167
   }
168
 
169
}

powered by: WebSVN 2.1.0

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