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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   mmio -- Memory mapped I/O example for ZTEX USB-FPGA Module 2.16
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
// ******* MMIO ****************************************************************
46
// *****************************************************************************
47
class MMIO extends Ztex1v1 {
48
 
49
// ******* MMIO ****************************************************************
50
// constructor
51
    public MMIO ( 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
        int i = bulkWrite(0x04, allocateByteBuffer(input.getBytes()) , 1000);
59
        if ( i<0 ) throw new UsbException("Error sending data: " + LibUsb.strError(i));
60
        System.out.println("Send "+i+" bytes: `"+input+"'" );
61
 
62
        try {
63
            Thread.sleep( 10 );
64
        }
65
            catch ( InterruptedException e ) {
66
        }
67
 
68
        ByteBuffer buffer = BufferUtils.allocateByteBuffer(1024);
69
        i = bulkRead(0x82, buffer, 1000);
70
        if ( i<0 ) throw new UsbException("Error receiving data: " + LibUsb.strError(i));
71
        else if (i==0) System.out.println("Read "+0+" bytes" );
72
        else {
73
            byte[] buf = new byte[i];
74
            buffer.get(buf);
75
            System.out.println("Read "+i+" bytes: `"+new String(buf,0,i)+"'" );
76
        }
77
    }
78
 
79
// ******* main ****************************************************************
80
    public static void main (String args[]) {
81
 
82
        int devNum = 0;
83
        boolean force = false;
84
 
85
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
86
            Runtime.getRuntime().addShutdownHook(new Thread() {
87
                public void run() {
88
                    Scanner s=new Scanner(System.in);
89
                    System.out.println("Press <enter> to continue ...");
90
                    s.nextLine();
91
                }
92
            });
93
        }
94
 
95
        try {
96
// Scan the USB. This also creates and initializes a new USB context.
97
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
98
 
99
// scan the command line arguments
100
            for (int i=0; i<args.length; i++ ) {
101
                if ( args[i].equals("-d") ) {
102
                    i++;
103
                    try {
104
                        if (i>=args.length) throw new Exception();
105
                        devNum = Integer.parseInt( args[i] );
106
                    }
107
                    catch (Exception e) {
108
                        throw new ParameterException("Device number expected after -d");
109
                    }
110
                }
111
                else if ( args[i].equals("-f") ) {
112
                    force = true;
113
                }
114
                else if ( args[i].equals("-p") ) {
115
                    bus.printBus(System.out);
116
                    System.exit(0);
117
                }
118
                else if ( args[i].equals("-p") ) {
119
                    bus.printBus(System.out);
120
                    System.exit(0);
121
                }
122
                else if ( args[i].equals("-h") ) {
123
                    System.err.println(ParameterException.helpMsg);
124
                    System.exit(0);
125
                }
126
                else throw new ParameterException("Invalid Parameter: "+args[i]);
127
            }
128
 
129
 
130
// create the main class            
131
            if ( bus.numberOfDevices() <= 0) {
132
                System.err.println("No devices found");
133
                System.exit(0);
134
            }
135
            MMIO ztex = new MMIO ( bus.device(devNum) );
136
            bus.unref();
137
 
138
            if (ztex.config == null ) throw new Exception("Invalid configuration data");
139
            byte majorVersion = ztex.config.getMajorVersion();
140
            byte minorVersion = ztex.config.getMinorVersion();
141
            String configFN = ztex.config.defaultBitstreamPath("mmio");
142
            String fwFN = "mmio-" + majorVersion + "_" + (minorVersion / 10) + (minorVersion % 10)+".ihx";
143
            String name="mmio example for UFM "+majorVersion + "." + (minorVersion / 10) + (minorVersion % 10);
144
            System.out.println("Found " + ztex.config.getName()+",  using firmware image " + fwFN + ", using bitstream "+configFN);
145
 
146
// upload the firmware if necessary
147
            if ( ! new File(fwFN).exists() )
148
                throw new Exception("Firmware image `" + fwFN + "' does not exist. This FPGA board seems not to be supported by this example");
149
 
150
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals(name)  ) {
151
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( fwFN, force ) + " ms");
152
                force = true;
153
            }
154
            else {
155
                ztex.resetDevice(false);        // sync data toggles
156
            }
157
 
158
// upload the bitstream
159
            System.out.println("FPGA configuration time: " + ztex.configureFpga( configFN , true ) + " ms");
160
 
161
// claim interface 0
162
            ztex.trySetConfiguration ( 1 );
163
            ztex.claimInterface ( 0 );
164
 
165
// read string from stdin and write it to USB device
166
            String str = "";
167
            BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
168
            while ( ! str.equals("quit") ) {
169
                System.out.print("Enter a string or `quit' to exit the program: ");
170
                str = reader.readLine();
171
                if ( ! str.equals("") )
172
                    ztex.echo(str);
173
                System.out.println("");
174
            }
175
 
176
            ztex.dispose();  // this also releases claimed interfaces
177
 
178
        }
179
        catch (Exception e) {
180
            System.out.println("Error: "+e.getLocalizedMessage() );
181
        }
182
   }
183
 
184
}

powered by: WebSVN 2.1.0

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