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-2.13/] [2.13c/] [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 and bitstream encryption example for ZTEX USB-FPGA Module 2.13
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
                "    -e                Use the encrypted bitstream\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
// ******* 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 encrypted = 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("-e") ) {
111
                    encrypted = 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("-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
 
132
// upload the firmware if necessary
133
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("ucecho example for UFM 2.13")  ) {
134
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "ucecho.ihx", force ) + " ms");
135
            }
136
 
137
// upload the bitstream if necessary
138
            if ( force || ! ztex.getFpgaConfiguration() ) {
139
                if ( encrypted ) System.out.println("FPGA configuration time: " + ztex.configureFpgaHS( "fpga/ucecho.runs/impl_2/ucecho.bit" , force, -1 ) + " ms");
140
                else System.out.println("FPGA configuration time: " + ztex.configureFpga( "fpga/ucecho.runs/impl_1/ucecho.bit" , force, -1 ) + " ms");
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
// release interface 0
159
            ztex.releaseInterface( 0 );
160
 
161
        }
162
        catch ( BitstreamUploadException e ) {
163
            System.out.println("Error: "+e.getLocalizedMessage() );
164
            if ( encrypted ) System.out.println("Make sure that the encryption key from key file fpga/ucecho.nky is loaded to battery-backed RAM");
165
        }
166
        catch (Exception e) {
167
            System.out.println("Error: "+e.getLocalizedMessage() );
168
        }
169
   }
170
 
171
}

powered by: WebSVN 2.1.0

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