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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   fx3demo -- Demonstrates common features of the FX3
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
// ******* Fx3Demo *************************************************************
46
// *****************************************************************************
47
class Fx3Demo extends Ztex1v1 {
48
 
49
// ******* Debug ***************************************************************
50
// constructor
51
    public Fx3Demo ( 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
 
80
// ******* main ****************************************************************
81
    public static void main (String args[]) {
82
 
83
        int devNum = 0;
84
        boolean force = false;
85
 
86
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
87
            Runtime.getRuntime().addShutdownHook(new Thread() {
88
                public void run() {
89
                    Scanner s=new Scanner(System.in);
90
                    System.out.println("Press <enter> to continue ...");
91
                    s.nextLine();
92
                }
93
            });
94
        }
95
 
96
        try {
97
 
98
// Scan the USB. This also creates and initializes a new USB context.
99
            ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
100
 
101
// scan the command line arguments
102
            for (int i=0; i<args.length; i++ ) {
103
                if ( args[i].equals("-d") ) {
104
                    i++;
105
                    try {
106
                        if (i>=args.length) throw new Exception();
107
                        devNum = Integer.parseInt( args[i] );
108
                    }
109
                    catch (Exception e) {
110
                        throw new ParameterException("Device number expected after -d");
111
                    }
112
                }
113
                else if ( args[i].equals("-f") ) {
114
                    force = true;
115
                }
116
                else if ( args[i].equals("-p") ) {
117
                    bus.printBus(System.out);
118
                    System.exit(0);
119
                }
120
                else if ( args[i].equals("-p") ) {
121
                    bus.printBus(System.out);
122
                    System.exit(0);
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
            if ( bus.numberOfDevices() <= 0) {
134
                System.err.println("No devices found");
135
                System.exit(0);
136
            }
137
            Fx3Demo ztex = new Fx3Demo ( bus.device(devNum) );
138
            bus.unref();
139
 
140
// upload the firmware if necessary
141
            if ( force || ! ztex.valid() || ! ztex.dev().productString().equals("Demo for FX3 Boards")  ) {
142
                System.out.println("Trying to overwrite firmware in RAM. This only works if a board specific firmware with reset support is running.\n"+
143
                                   "If uploading firmware fails please restart board with disabled firmware" );
144
                System.out.println("Firmware upload time: " + ztex.uploadFirmware( "fx3demo.img", force ) + " ms");
145
            }
146
            else {
147
                ztex.resetDevice(false);        // sync data toggles
148
            }
149
 
150
 
151
            ztex.debug2PrintNextLogMessages(System.out);
152
 
153
// claim interface 0
154
            ztex.trySetConfiguration ( 1 );
155
            ztex.claimInterface ( 0 );
156
 
157
// print log
158
            ztex.debug2PrintNextLogMessages(System.out);
159
 
160
// test flash
161
            if ( ztex.flashEnabled() ) {
162
                System.out.println("SPI Flash");
163
                System.out.println("Sector size: "+ztex.flashSectorSize());
164
                System.out.println("Sectors: "+ztex.flashSectors());
165
//              ztex.printSpiState();
166
 
167
                byte[] buf0 = new byte[ztex.flashSectorSize()];
168
                byte[] buf1 = new byte[ztex.flashSectorSize()];
169
                byte[] buf2 = new byte[ztex.flashSectorSize()];
170
                for (int i=0; i<ztex.flashSectorSize(); i++ ) {
171
                    buf1[i] = (byte)((i % 251) & 255);
172
                    buf2[i] = (byte) 255;
173
                }
174
                try {
175
                    System.out.println("Reading backup of sector 7");
176
                    ztex.flashReadSector(7, 1, buf0);
177
                    System.out.println("Writing sector 7");
178
                    ztex.flashWriteSector(7, 1, buf1);
179
                    System.out.println("Reading sector 7");
180
                    ztex.flashReadSector(7, 1, buf2);
181
                    boolean errors = false;
182
                    for (int i=0; i<ztex.flashSectorSize(); i++ ) {
183
                        if ( buf1[i] != buf2[i] ) {
184
                            errors = true;
185
                            System.out.println("Flash error at " + i + ":  " + (buf1[i] & 255) + " " + (buf2[i] & 255)+"        ");
186
                        }
187
                    }
188
                    if ( ! errors )  System.out.println("No Flash errors");
189
                    System.out.println("Writing backup of sector 7");
190
                    ztex.flashWriteSector(7, 1, buf0);
191
                }
192
                catch (Exception e) {
193
                    System.out.println("Flash test failed: " + e.getLocalizedMessage() );
194
                    ztex.printSpiState();
195
                }
196
            }
197
            else {
198
                System.out.println("No SPI Flash found or SPI Flash disabled due to reboot");
199
            }
200
 
201
            ztex.debug2PrintNextLogMessages(System.out);
202
 
203
// speed test
204
            try {
205
                ZtexEventHandler eventHandler = new ZtexEventHandler(ztex);
206
                eventHandler.start();
207
                ZtexUsbReader bulkReader = new ZtexUsbReader( ztex, 1, false, 16, 256*1024 );
208
                bulkReader.start(-1);
209
                long oldByteCount = 0;
210
                for ( int i=0; i<5; i++ ) {
211
                    System.out.print("1s speed test: ");
212
                    try { Thread.sleep(1000); } catch ( InterruptedException e) { }
213
                    System.out.println(Math.round((bulkReader.byteCount()-oldByteCount)/1000000.0) + " MByte/s");
214
                    oldByteCount = bulkReader.byteCount();
215
                }
216
                if ( !bulkReader.cancelWait(10000) ) System.err.println("Unable to cancel reading");
217
                if ( !eventHandler.terminate() ) System.err.println("Unable to terminate event handler");
218
            }
219
            catch ( Exception e ) {
220
                System.out.println("Speed test failed: " + e.getLocalizedMessage() );
221
            }
222
 
223
            ztex.debug2PrintNextLogMessages(System.out);
224
 
225
// read string from stdin and write it to USB device
226
            try {
227
                String str = "";
228
                BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
229
                while ( ! str.equals("quit") ) {
230
                    System.out.print("Enter a string or `quit' to exit the program: ");
231
                    str = reader.readLine();
232
                    if ( ! str.equals("") ) ztex.echo(str);
233
                    ztex.debug2PrintNextLogMessages(System.out);
234
                    System.out.println("");
235
                }
236
            }
237
            catch ( Exception e ) {
238
                System.out.println("ucecho test failed: " + e.getLocalizedMessage() );
239
            }
240
 
241
            ztex.debug2PrintNextLogMessages(System.out);
242
 
243
            ztex.dispose();  // this also releases claimed interfaces
244
 
245
        }
246
        catch (Exception e) {
247
            System.out.println("Error: "+e.getLocalizedMessage() );
248
        }
249
   }
250
 
251
}

powered by: WebSVN 2.1.0

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