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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   Firmware / Bitstream loader for ZTEX USB-FPGA Modules
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
/*
27
    Firmware Loader and FPGA Configurator
28
*/
29
 
30
import java.io.*;
31
import java.util.*;
32
 
33
import org.usb4java.*;
34
 
35
import ztex.*;
36
 
37
class FWLoader {
38
 
39
// ******* checkSnString *******************************************************
40
// make sure that snString is 10 chars long
41
    private static String checkSnString ( String snString ) {
42
        if ( snString.length()>10 ) {
43
            snString = snString.substring(0,10);
44
            System.err.println( "Serial number too long (max. 10 characters), truncated to `" + snString + "'" );
45
        }
46
        while ( snString.length()<10 )
47
            snString = '0' + snString;
48
        return snString;
49
    }
50
 
51
// ******* main ****************************************************************
52
    public static void main (String args[]) {
53
 
54
        final String helpMsg = new String (
55
                        "Global parameters:\n"+
56
                        "    -c               Scan for Cypress EZ-USB devices without ZTEX firmware\n"+
57
                        "    -v <VID> <PID>   Scan for devices with given Vendor ID and Product ID\n"+
58
                        "    -vc              Equal to -v 0x4b4 0x8613\n"+
59
                        "    -s <sn string>   Only scan for devices with that serial number\n"+
60
                        "    -d <number>      Device Number (default: 0, use -p to get a list)\n"+
61
                        "    -f               Force uploads\n"+
62
                        "    -p               Print a list of available devices\n"+
63
                        "    -h               This help \n\n"+
64
                        "Ordered parameters:\n"+
65
                        "    -i               Print device info\n"+
66
                        "    -ii              Print device info + capabilities\n"+
67
                        "    -if              Print FPGA state\n"+
68
                        "    -ic              Print configuration data and flash info\n"+
69
                        "    -il              Print log messages of FX3 based FPGA Boards\n"+
70
                        "    -ss <sn string>  Set the serial number, \n"+
71
                        "                     used with -uu or -ue or if configuration data present\n"+
72
                        "    -su <VID> <PID>  Set the USB ID's in configuration memory,\n"+
73
                        "                     0 to load ID's defined by firmware \n"+
74
                        "    -sp <string>     Set the product string in configuration memory, \n"+
75
                        "                     empty string to load firmware default\n"+
76
                        "    -ru              Reset EZ-USB Microcontroller\n"+
77
                        "    -uu <file>       Upload EZ-USB Firmware in ihx or img format to volatile memory\n"+
78
                        "    -bs 0|1|A        Bit swapping for bitstreams: 0: disable, 1: enable, A: automatic detection\n"+
79
                        "    -rf              Reset FPGA\n"+
80
                        "    -uf <bitstream>  Upload bitstream to FPGA\n"+
81
                        "    -sf <number>     Select FPGA (default: 0)\n"+
82
                        "    -re              Reset Firmware in non-volatile memory\n"+
83
                        "    -ue <file>       Upload Firmware in ihx or img format to non-volatile memory\n"+
84
                        "    -rm              Reset bitstream in Flash\n"+
85
                        "    -um <bitstream>  Upload bitstream to Flash\n"+
86
                        "    -uxf <ihx file>  Upload Firmware / data  to ATxmega Flash\n"+
87
                        "    -uxe <ihx file>  Upload data to ATxmega EEPROM\n"+
88
                        "    -rxf <index>     Read ATxmega Fuse\n" +
89
                        "    -wxf <index> <bitmask> <value>  Write ATxmega Fuse\n" +
90
                        "Serial number strings (<sn string>) must be 10 chars long, if shorter they are filled with 0's." );
91
 
92
        if ( ! System.getProperty("os.name").equalsIgnoreCase("linux") ) {
93
            Runtime.getRuntime().addShutdownHook(new Thread() {
94
                public void run() {
95
                    Scanner s=new Scanner(System.in);
96
                    System.out.println("Press <enter> to continue ...");
97
                    s.nextLine();
98
                }
99
            });
100
        }
101
 
102
// process global parameters
103
        try {
104
 
105
            int usbVendorId = ZtexDevice1.ztexVendorId;
106
            int usbProductId = -1;
107
            boolean cypress = false;
108
            int devNum = 0;
109
            boolean forceUpload = false;
110
            boolean printBus = false;
111
            String snString = null;
112
            int bs = -1;
113
 
114
            if ( args.length == 0 ) {
115
                    System.err.println(helpMsg);
116
                    System.exit(1);
117
            }
118
 
119
            for (int i=0; i<args.length; i++ ) {
120
                if ( args[i].equals("-c") ) {
121
                    cypress = true;
122
                }
123
                else if ( args[i].equals("-v") ) {
124
                    i++;
125
                    try {
126
                        if (i>=args.length)
127
                            throw new Exception();
128
                        usbVendorId = Integer.decode( args[i] );
129
                    }
130
                    catch (Exception e) {
131
                        System.err.println("Error: Vendor ID expected after -v");
132
                        System.err.println(helpMsg);
133
                        System.exit(1);
134
                    }
135
                    i++;
136
                    try {
137
                        if (i>=args.length)
138
                            throw new Exception();
139
                        usbProductId = Integer.decode( args[i] );
140
                    }
141
                    catch (Exception e) {
142
                        System.err.println("Error: Product ID expected after -v <VID>");
143
                        System.err.println(helpMsg);
144
                        System.exit(1);
145
                    }
146
                }
147
                else if ( args[i].equals("-vc") ) {
148
                    usbVendorId = ZtexDevice1.cypressVendorId;
149
                    usbProductId = ZtexDevice1.cypressProductIdFx2;
150
                }
151
                else if ( args[i].equals("-f") ) {
152
                    forceUpload = true;
153
                }
154
                else if ( args[i].equals("-p") ) {
155
                    printBus = true;
156
                }
157
                else if ( args[i].equals("-d") ) {
158
                    i++;
159
                    try {
160
                        if (i>=args.length)
161
                            throw new Exception();
162
                        devNum = Integer.parseInt( args[i] );
163
                    }
164
                    catch (Exception e) {
165
                        System.err.println("Error: Device number expected after -d");
166
                        System.err.println(helpMsg);
167
                        System.exit(1);
168
                    }
169
                }
170
                else if ( args[i].equals("-s") ) {
171
                    i++;
172
                    if (i>=args.length) {
173
                        System.err.println("Error: String expected after -s");
174
                        System.err.println(helpMsg);
175
                        System.exit(1);
176
                    }
177
                    snString = checkSnString(args[i]);
178
                }
179
                else if ( args[i].equals("-h") ) {
180
                        System.err.println(helpMsg);
181
                        System.exit(0);
182
                }
183
                else if ( args[i].equals("-i") || args[i].equals("-ii") || args[i].equals("-if") || args[i].equals("-ic") || args[i].equals("-il") || args[i].equals("-ru") || args[i].equals("-rf") || args[i].equals("-re") || args[i].equals("-rm") ) {
184
                }
185
                else if ( args[i].equals("-uu") || args[i].equals("-uf") || args[i].equals("-sf") || args[i].equals("-ue") || args[i].equals("-um") || args[i].equals("-bs") || args[i].equals("-uxf")  || args[i].equals("-uxe") || args[i].equals("-rxf") || args[i].equals("-ss") || args[i].equals("-sp")) {
186
                    i+=1;
187
                }
188
                else if ( args[i].equals("-su") ) {
189
                    i+=2;
190
                }
191
                else if ( args[i].equals("-wxf") ) {
192
                    i+=3;
193
                }
194
                else {
195
                    System.err.println("Error: Invalid Parameter: "+args[i]);
196
                    System.err.println(helpMsg);
197
                    System.exit(1);
198
                }
199
            }
200
 
201
// Scan the USB. This also creates and initializes a new USB context.
202
            ZtexScanBus1 bus = new ZtexScanBus1( usbVendorId, usbProductId, cypress, false, 1, snString);
203
            if ( bus.numberOfDevices() <= 0 ) {
204
                System.err.println("No devices found");
205
                System.exit(0);
206
            }
207
            if ( printBus )
208
                bus.printBus(System.out);
209
 
210
            Ztex1v1 ztex = new Ztex1v1 ( bus.device(devNum) );
211
            bus.unref();
212
 
213
            snString = null;
214
            for (int i=0; i<args.length; i++ ) {
215
                if ( args[i].equals("-i") ) {
216
                    System.out.println( ztex );
217
                }
218
                if ( args[i].equals("-ii") ) {
219
                    System.out.println( ztex );
220
                    String str = ztex.capabilityInfo("\n      ");
221
                    if ( str.equals("") ) {
222
                        System.out.println( "   No capabilities");
223
                    }
224
                    else {
225
                        System.out.println( "   Capabilities:\n      "+str);
226
                    }
227
                }
228
                if ( args[i].equals("-if") ) {
229
                    ztex.printFpgaState();
230
                }
231
                if ( args[i].equals("-ic") ) {
232
                    if ( ztex.config != null ) {
233
                        System.out.println("ZTEX Product: " + ztex.config.getName());
234
                        System.out.println("FPGA: " + ztex.config.getFpga());
235
                        if (ztex.config.getRamSize()>0)  System.out.println("RAM: " + (ztex.config.getRamSize() >> 20) + " MByte " + ztex.config.getRamType());
236
                    }
237
                    else {
238
                        System.out.println("(No configuration data found)");
239
                    }
240
                    String s = ztex.flashInfo(); if ( s.length()>0 ) System.out.println("Flash: " + s);
241
                    s = ztex.flash2Info(); if ( s.length()>0 ) System.out.println("2nd Flash: " + s);
242
                }
243
                if ( args[i].equals("-il") ) {
244
                    try {
245
                        ztex.debug2PrintNextLogMessages(System.out);
246
                        ztex.getUsb3Errors();
247
                        System.out.println("USB 3.0 send errors: "+ztex.usb3SndErrors + "  receive errors: "+ztex.usb3RcvErrors);
248
                    }
249
                    catch ( CapabilityException e ) {
250
                    }
251
                }
252
                else if ( args[i].equals("-ss") ) {
253
                    i++;
254
                    if ( i >= args.length ) {
255
                        System.err.println("Error: String expected after -ss");
256
                        System.err.println(helpMsg);
257
                        System.exit(1);
258
                    }
259
                    snString = checkSnString(args[i]);
260
                    if ( ztex.config != null ) {
261
                        ztex.config.setSN(snString);
262
                    }
263
                }
264
                else if ( args[i].equals("-su") ) {
265
                    i++;
266
                    int vid,pid;
267
                    try {
268
                        if (i>=args.length)
269
                            throw new Exception();
270
                        vid = Integer.decode( args[i] );
271
                    }
272
                    catch (Exception e) {
273
                        System.err.println("Error: Vendor ID expected after -su");
274
                        System.err.println(helpMsg);
275
                        System.exit(1);
276
                        vid = -1;
277
                    }
278
                    i++;
279
                    try {
280
                        if (i>=args.length)
281
                            throw new Exception();
282
                        pid = Integer.decode( args[i] );
283
                    }
284
                    catch (Exception e) {
285
                        System.err.println("Error: Product ID expected after -su <VID>");
286
                        System.err.println(helpMsg);
287
                        System.exit(1);
288
                        pid = -1;
289
                    }
290
                    if ( (vid>=0) && (pid>=0) ) {
291
                        if ( ztex.config == null ) {
292
                            System.err.println("Warning: can't write USB ID's: no configuration memory present");
293
                        } else {
294
                            ztex.config.setUsbIDs(vid,pid);
295
                        }
296
                    }
297
                }
298
                else if ( args[i].equals("-sp") ) {
299
                    i++;
300
                    if ( i >= args.length ) {
301
                        System.err.println("Error: String expected after -sp");
302
                        System.err.println(helpMsg);
303
                        System.exit(1);
304
                    }
305
                    if ( ztex.config == null ) {
306
                        System.err.println("Warning: can't write prodct string: no configuration memory present");
307
                    } else {
308
                        ztex.config.setProductString(args[i]);
309
                    }
310
                }
311
                else if ( args[i].equals("-ru") ) {
312
                    ztex.resetEzUsb();
313
                }
314
                else if ( args[i].equals("-uu") ) {
315
                    i++;
316
                    if ( i >= args.length ) {
317
                        System.err.println("Error: Filename expected after -uu");
318
                        System.err.println(helpMsg);
319
                        System.exit(1);
320
                    }
321
                    ZtexImgFile1 imgFile = new ZtexImgFile1( args[i] );
322
                    if ( snString != null )
323
                        imgFile.setSnString( snString );
324
                    System.out.println("Firmware upload time: " + ztex.uploadFirmware( imgFile, forceUpload ) + " ms");
325
                }
326
                else if ( args[i].equals("-bs") ) {
327
                    i++;
328
                    if ( (i>=args.length) || !( args[i].equals("0") || args[i].equals("1") || args[i].equalsIgnoreCase("A") ) ) {
329
                        System.err.println("Error: `0',`1' or `A' expected after -bs");
330
                        System.err.println(helpMsg);
331
                        System.exit(1);
332
                    }
333
                    if ( args[i].equals("0") )
334
                        bs = 0;
335
                    else if ( args[i].equals("1") )
336
                        bs = 1;
337
                    else bs = -1;
338
                }
339
                else if ( args[i].equals("-rf") ) {
340
                    ztex.resetFpga();
341
                }
342
                else if ( args[i].equals("-uf") ) {
343
                    i++;
344
                    if ( i >= args.length ) {
345
                        System.err.println("Error: Filename expected after -uf");
346
                        System.err.println(helpMsg);
347
                        System.exit(1);
348
                    }
349
                    System.out.println("FPGA configuration time: " + ztex.configureFpga( args[i], forceUpload, bs ) + " ms");
350
                }
351
                else if ( args[i].equals("-sf") ) {
352
                    i++;
353
                    int fn=-1;
354
                    try {
355
                        if (i>=args.length)
356
                            throw new Exception();
357
                        fn = Integer.parseInt( args[i] );
358
                    }
359
                    catch (Exception e) {
360
                        System.err.println("Error: Number expected after -sf");
361
                        System.err.println(helpMsg);
362
                        System.exit(1);
363
                    }
364
                    if ( fn >= 0 ) {
365
                        ztex.selectFpga(fn);
366
                    }
367
                }
368
                else if ( args[i].equals("-re") ) {
369
                    ztex.nvDisableFirmware();
370
                }
371
                else if ( args[i].equals("-ue") ) {
372
                    i++;
373
                    if ( i >= args.length ) {
374
                        System.err.println("Error: Filename expected after -ue");
375
                        System.err.println(helpMsg);
376
                        System.exit(1);
377
                    }
378
                    ImgFile imgFile;
379
                    try {
380
                        imgFile = new ZtexImgFile1( args[i] );
381
                        if ( snString != null ) ((ZtexImgFile1)imgFile).setSnString(snString);
382
                    }
383
                    catch ( IncompatibleFirmwareException e ) {
384
                        if ( !forceUpload ) throw e;
385
                        imgFile = new ImgFile( args[i] );
386
                    }
387
                    System.out.println("Firmware to non-volatile memory upload time: " + ztex.nvUploadFirmware( imgFile, forceUpload ) + " ms");
388
                }
389
                else if ( args[i].equals("-rm") ) {
390
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
391
                    ztex.flashResetBitstream();
392
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
393
                }
394
                else if ( args[i].equals("-um") ) {
395
                    i++;
396
                    if ( i >= args.length ) {
397
                        System.err.println("Error: Filename expected after -um");
398
                        System.err.println(helpMsg);
399
                        System.exit(1);
400
                    }
401
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
402
                    System.out.println("FPGA configuration time: " + ztex.flashUploadBitstream( args[i], bs ) + " ms");
403
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
404
                }
405
                else if ( args[i].equals("-uxf") ) {
406
                    i++;
407
                    if ( i >= args.length ) {
408
                        System.err.println("Error: Filename expected after -uxf");
409
                        System.err.println(helpMsg);
410
                        System.exit(1);
411
                    }
412
                    System.out.println("Firmware to ATxmega Flash upload time: " + ztex.xmegaWriteFirmware( new ImgFile(args[i]) ) + " ms");
413
                }
414
                else if ( args[i].equals("-uxe") ) {
415
                    i++;
416
                    if ( i >= args.length ) {
417
                        System.err.println("Error: Filename expected after -uxe");
418
                        System.err.println(helpMsg);
419
                        System.exit(1);
420
                    }
421
                    System.out.println("Firmware to ATxmega Flash upload time: " + ztex.xmegaWriteEeprom( new ImgFile(args[i]) ) + " ms");
422
                }
423
                else if ( args[i].equals("-rxf") ) {
424
                    i++;
425
                    int j = 0;
426
                    try {
427
                        if (i>=args.length)
428
                            throw new Exception();
429
                        j = Integer.parseInt( args[i] );
430
                    }
431
                    catch (Exception e) {
432
                        System.err.println("Error: Index number expected after -rxf");
433
                        System.err.println(helpMsg);
434
                        System.exit(1);
435
                    }
436
                    System.out.println("Fuse " + j + ": 0b" + Integer.toBinaryString(256 | ztex.xmegaFuseRead ( j )).substring(1));
437
                }
438
                else if ( args[i].equals("-wxf") ) {
439
                    i++;
440
                    int j=0, k=0, l=0;
441
                    try {
442
                        if (i>=args.length)
443
                            throw new Exception();
444
                        j = Integer.parseInt( args[i] );
445
                    }
446
                    catch (Exception e) {
447
                        System.err.println("Error: Index number expected after -wxf");
448
                        System.err.println(helpMsg);
449
                        System.exit(1);
450
                    }
451
                    i++;
452
                    try {
453
                        if (i>=args.length)
454
                            throw new Exception();
455
                        k = Integer.parseInt( args[i] );
456
                    }
457
                    catch (Exception e) {
458
                        System.err.println("Error: Bitmask expected after -wxf <index>");
459
                        System.err.println(helpMsg);
460
                        System.exit(1);
461
                    }
462
                    i++;
463
                    try {
464
                        if (i>=args.length)
465
                            throw new Exception();
466
                        l = Integer.parseInt( args[i] );
467
                    }
468
                    catch (Exception e) {
469
                        System.err.println("Error: Value expected after -wxf <index> <bitmask>");
470
                        System.err.println(helpMsg);
471
                        System.exit(1);
472
                    }
473
                    ztex.xmegaFuseWrite( j, (ztex.xmegaFuseRead(j) & ~k) | l );
474
                }
475
            }
476
 
477
            ztex.dispose();
478
 
479
        }
480
        catch (Exception e) {
481
            System.out.println("Error: "+e.getLocalizedMessage() );
482
        }
483
   }
484
 
485
}

powered by: WebSVN 2.1.0

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