OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_04/usb_fpga_2_04/trunk

Subversion Repositories usb_fpga_2_04

[/] [usb_fpga_2_04/] [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-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
/*
20
    Firmware Loader and FPGA Configurater
21
*/
22
 
23
import java.io.*;
24
import java.util.*;
25
 
26
import ch.ntb.usb.*;
27
 
28
import ztex.*;
29
 
30
class FWLoader {
31
 
32
// ******* checkSnString *******************************************************
33
// make sure that snString is 10 chars long
34
    private static String checkSnString ( String snString ) {
35
        if ( snString.length()>10 ) {
36
            snString = snString.substring(0,10);
37
            System.err.println( "Serial number too long (max. 10 characters), truncated to `" + snString + "'" );
38
        }
39
        while ( snString.length()<10 )
40
            snString = '0' + snString;
41
        return snString;
42
    }
43
 
44
// ******* main ****************************************************************
45
    public static void main (String args[]) {
46
        LibusbJava.usb_init();
47
 
48
        final String helpMsg = new String (
49
                        "Global parameters:\n"+
50
                        "    -c               Scan for Cypress EZ-USB devices without ZTEX firmware\n"+
51
                        "    -v <VID> <PID>   Scan for devices with given Vendor ID and Product ID\n"+
52
                        "    -vc              Equal to -v 0x4b4 0x8613\n"+
53
                        "    -s <sn string>   Only scan for devices with that serial number\n"+
54
                        "    -d <number>      Device Number (default: 0, use -p to get a list)\n"+
55
                        "    -f               Force uploads\n"+
56
                        "    -p               Print a list of available devices\n"+
57
                        "    -w               Enable certain workarounds\n"+
58
                        "    -h               This help \n\n"+
59
                        "Ordered parameters:\n"+
60
                        "    -i               Print device info\n"+
61
                        "    -ii              Print device info + capabilities\n"+
62
                        "    -if              Print FPGA state\n"+
63
                        "    -ic              Print configuration data and flash info\n"+
64
                        "    -ss <sn string>  Set the serial number, \n"+
65
                        "                     used with -uu or -ue or if configuration data present\n"+
66
                        "    -ru              Reset EZ-USB Microcontroller\n"+
67
                        "    -uu <ihx file>   Upload EZ-USB Firmware\n"+
68
                        "    -bs 0|1|A        Bit swapping for bitstreams: 0: disable, 1: enable, A: automatic detection\n"+
69
                        "    -rf              Reset FPGA\n"+
70
                        "    -uf <bitstream>  Upload bitstream to FPGA\n"+
71
                        "    -sf <number>     Select FPGA (default: 0)\n"+
72
                        "    -re              Reset EEPROM Firmware\n"+
73
                        "    -ue <ihx file>   Upload Firmware to EEPROM\n"+
74
                        "    -rm              Reset bitstream in Flash\n"+
75
                        "    -um <bitstream>  Upload bitstream to Flash\n"+
76
                        "    -uxf <ihx file>  Upload Firmware / data  to ATxmega Flash\n"+
77
                        "    -uxe <ihx file>  Upload data to ATxmega EEPROM\n"+
78
                        "    -rxf <index>     Read ATxmega Fuse\n" +
79
                        "    -wxf <index> <bitmask> <value>  Write ATxmega Fuse\n" +
80
                        "Serial number strings (<sn string>) must be 10 chars long, if shorter filled with 0's." );
81
 
82
// process global parameters
83
        try {
84
 
85
            int usbVendorId = ZtexDevice1.ztexVendorId;
86
            int usbProductId = -1;
87
            boolean cypress = false;
88
            int devNum = 0;
89
            boolean forceUpload = false;
90
            boolean printBus = false;
91
            boolean workarounds = false;
92
            String snString = null;
93
            int bs = -1;
94
 
95
            if ( args.length == 0 ) {
96
                    System.err.println(helpMsg);
97
                    System.exit(1);
98
            }
99
 
100
            for (int i=0; i<args.length; i++ ) {
101
                if ( args[i].equals("-c") ) {
102
                    cypress = true;
103
                }
104
                else if ( args[i].equals("-v") ) {
105
                    i++;
106
                    try {
107
                        if (i>=args.length)
108
                            throw new Exception();
109
                        usbVendorId = Integer.decode( args[i] );
110
                    }
111
                    catch (Exception e) {
112
                        System.err.println("Error: Vendor ID expected after -v");
113
                        System.err.println(helpMsg);
114
                        System.exit(1);
115
                    }
116
                    i++;
117
                    try {
118
                        if (i>=args.length)
119
                            throw new Exception();
120
                        usbProductId = Integer.decode( args[i] );
121
                    }
122
                    catch (Exception e) {
123
                        System.err.println("Error: Product ID expected after -v <VID>");
124
                        System.err.println(helpMsg);
125
                        System.exit(1);
126
                    }
127
                }
128
                else if ( args[i].equals("-vc") ) {
129
                    usbVendorId = ZtexDevice1.cypressVendorId;
130
                    usbProductId = ZtexDevice1.cypressProductId;
131
                }
132
                else if ( args[i].equals("-f") ) {
133
                    forceUpload = true;
134
                }
135
                else if ( args[i].equals("-p") ) {
136
                    printBus = true;
137
                }
138
                else if ( args[i].equals("-w") ) {
139
                    workarounds = true;
140
                }
141
                else if ( args[i].equals("-d") ) {
142
                    i++;
143
                    try {
144
                        if (i>=args.length)
145
                            throw new Exception();
146
                        devNum = Integer.parseInt( args[i] );
147
                    }
148
                    catch (Exception e) {
149
                        System.err.println("Error: Device number expected after -d");
150
                        System.err.println(helpMsg);
151
                        System.exit(1);
152
                    }
153
                }
154
                else if ( args[i].equals("-s") ) {
155
                    i++;
156
                    if (i>=args.length) {
157
                        System.err.println("Error: String expected after -s");
158
                        System.err.println(helpMsg);
159
                        System.exit(1);
160
                    }
161
                    snString = checkSnString(args[i]);
162
                }
163
                else if ( args[i].equals("-h") ) {
164
                        System.err.println(helpMsg);
165
                        System.exit(0);
166
                }
167
                else if ( args[i].equals("-i") || args[i].equals("-ii") || args[i].equals("-if") || args[i].equals("-ic") || args[i].equals("-ru") || args[i].equals("-rf") || args[i].equals("-re") || args[i].equals("-rm") ) {
168
                }
169
                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")) {
170
                    i+=1;
171
                }
172
                else if ( args[i].equals("-wxf")  ) {
173
                    i+=3;
174
                }
175
                else {
176
                    System.err.println("Error: Invalid Parameter: "+args[i]);
177
                    System.err.println(helpMsg);
178
                    System.exit(1);
179
                }
180
            }
181
 
182
// process ordered parameters
183
            ZtexScanBus1 bus = new ZtexScanBus1( usbVendorId, usbProductId, cypress, false, 1, snString);
184
            if ( bus.numberOfDevices() <= 0 ) {
185
                System.err.println("No devices found");
186
                System.exit(0);
187
            }
188
            if ( printBus )
189
                bus.printBus(System.out);
190
 
191
            Ztex1v1 ztex = new Ztex1v1 ( bus.device(devNum) );
192
            ztex.certainWorkarounds = workarounds;
193
 
194
            snString = null;
195
            for (int i=0; i<args.length; i++ ) {
196
                if ( args[i].equals("-i") ) {
197
                    System.out.println( ztex );
198
                }
199
                if ( args[i].equals("-ii") ) {
200
                    System.out.println( ztex );
201
                    String str = ztex.capabilityInfo("\n      ");
202
                    if ( str.equals("") ) {
203
                        System.out.println( "   No capabilities");
204
                    }
205
                    else {
206
                        System.out.println( "   Capabilities:\n      "+str);
207
                    }
208
                }
209
                if ( args[i].equals("-if") ) {
210
                    ztex.printFpgaState();
211
                }
212
                if ( args[i].equals("-ic") ) {
213
                    if ( ztex.config != null ) {
214
                        System.out.println("ZTEX Product: " + ztex.config.getName());
215
                        System.out.println("FPGA: " + ztex.config.getFpga());
216
                        if (ztex.config.getRamSize()>0)  System.out.println("RAM: " + (ztex.config.getRamSize() >> 20) + " MByte " + ztex.config.getRamType());
217
                    }
218
                    else {
219
                        System.out.println("(No configuration data found)");
220
                    }
221
                    String s = ztex.flashInfo(); if ( s.length()>0 ) System.out.println("Flash: " + s);
222
                }
223
                else if ( args[i].equals("-ss") ) {
224
                    i++;
225
                    if ( i >= args.length ) {
226
                        System.err.println("Error: String expected after -ss");
227
                        System.err.println(helpMsg);
228
                        System.exit(1);
229
                    }
230
                    snString = checkSnString(args[i]);
231
                    if ( ztex.config != null ) {
232
                        ztex.config.setSN(snString);
233
                    }
234
                }
235
                else if ( args[i].equals("-ru") ) {
236
                    ztex.resetEzUsb();
237
                }
238
                else if ( args[i].equals("-uu") ) {
239
                    i++;
240
                    if ( i >= args.length ) {
241
                        System.err.println("Error: Filename expected after -uu");
242
                        System.err.println(helpMsg);
243
                        System.exit(1);
244
                    }
245
                    ZtexIhxFile1 ihxFile = new ZtexIhxFile1( args[i] );
246
                    if ( snString != null )
247
                        ihxFile.setSnString( snString );
248
                    System.out.println("Firmware upload time: " + ztex.uploadFirmware( ihxFile, forceUpload ) + " ms");
249
                }
250
                else if ( args[i].equals("-bs") ) {
251
                    i++;
252
                    if ( (i>=args.length) || !( args[i].equals("0") || args[i].equals("1") || args[i].equalsIgnoreCase("A") ) ) {
253
                        System.err.println("Error: `0',`1' or `A' expected after -bs");
254
                        System.err.println(helpMsg);
255
                        System.exit(1);
256
                    }
257
                    if ( args[i].equals("0") )
258
                        bs = 0;
259
                    else if ( args[i].equals("1") )
260
                        bs = 1;
261
                    else bs = -1;
262
                }
263
                else if ( args[i].equals("-rf") ) {
264
                    ztex.resetFpga();
265
                }
266
                else if ( args[i].equals("-uf") ) {
267
                    i++;
268
                    if ( i >= args.length ) {
269
                        System.err.println("Error: Filename expected after -uf");
270
                        System.err.println(helpMsg);
271
                        System.exit(1);
272
                    }
273
                    System.out.println("FPGA configuration time: " + ztex.configureFpga( args[i], forceUpload, bs ) + " ms");
274
                }
275
                else if ( args[i].equals("-sf") ) {
276
                    i++;
277
                    int fn=-1;
278
                    try {
279
                        if (i>=args.length)
280
                            throw new Exception();
281
                        fn = Integer.parseInt( args[i] );
282
                    }
283
                    catch (Exception e) {
284
                        System.err.println("Error: Number expected after -sf");
285
                        System.err.println(helpMsg);
286
                        System.exit(1);
287
                    }
288
                    if ( fn >= 0 ) {
289
                        ztex.selectFpga(fn);
290
                    }
291
                }
292
                else if ( args[i].equals("-re") ) {
293
                    ztex.eepromDisable();
294
                }
295
                else if ( args[i].equals("-ue") ) {
296
                    i++;
297
                    if ( i >= args.length ) {
298
                        System.err.println("Error: Filename expected after -ue");
299
                        System.err.println(helpMsg);
300
                        System.exit(1);
301
                    }
302
                    ZtexIhxFile1 ihxFile = new ZtexIhxFile1( args[i] );
303
                    if ( snString != null )
304
                        ihxFile.setSnString(snString);
305
                    System.out.println("Firmware to EEPROM upload time: " + ztex.eepromUpload( ihxFile, forceUpload ) + " ms");
306
                }
307
                else if ( args[i].equals("-rm") ) {
308
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
309
                    ztex.flashResetBitstream();
310
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
311
                }
312
                else if ( args[i].equals("-um") ) {
313
                    i++;
314
                    if ( i >= args.length ) {
315
                        System.err.println("Error: Filename expected after -um");
316
                        System.err.println(helpMsg);
317
                        System.exit(1);
318
                    }
319
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
320
                    System.out.println("FPGA configuration time: " + ztex.flashUploadBitstream( args[i], bs ) + " ms");
321
                    System.out.println("First free sector: " + ztex.flashFirstFreeSector() );
322
                }
323
                else if ( args[i].equals("-uxf") ) {
324
                    i++;
325
                    if ( i >= args.length ) {
326
                        System.err.println("Error: Filename expected after -uxf");
327
                        System.err.println(helpMsg);
328
                        System.exit(1);
329
                    }
330
                    System.out.println("Firmware to ATxmega Flash upload time: " + ztex.xmegaWriteFirmware( new IhxFile(args[i]) ) + " ms");
331
                }
332
                else if ( args[i].equals("-uxe") ) {
333
                    i++;
334
                    if ( i >= args.length ) {
335
                        System.err.println("Error: Filename expected after -uxe");
336
                        System.err.println(helpMsg);
337
                        System.exit(1);
338
                    }
339
                    System.out.println("Firmware to ATxmega Flash upload time: " + ztex.xmegaWriteEeprom( new IhxFile(args[i]) ) + " ms");
340
                }
341
                else if ( args[i].equals("-rxf") ) {
342
                    i++;
343
                    int j = 0;
344
                    try {
345
                        if (i>=args.length)
346
                            throw new Exception();
347
                        j = Integer.parseInt( args[i] );
348
                    }
349
                    catch (Exception e) {
350
                        System.err.println("Error: Index number expected after -rxf");
351
                        System.err.println(helpMsg);
352
                        System.exit(1);
353
                    }
354
                    System.out.println("Fuse " + j + ": 0b" + Integer.toBinaryString(256 | ztex.xmegaFuseRead ( j )).substring(1));
355
                }
356
                else if ( args[i].equals("-wxf") ) {
357
                    i++;
358
                    int j=0, k=0, l=0;
359
                    try {
360
                        if (i>=args.length)
361
                            throw new Exception();
362
                        j = Integer.parseInt( args[i] );
363
                    }
364
                    catch (Exception e) {
365
                        System.err.println("Error: Index number expected after -wxf");
366
                        System.err.println(helpMsg);
367
                        System.exit(1);
368
                    }
369
                    i++;
370
                    try {
371
                        if (i>=args.length)
372
                            throw new Exception();
373
                        k = Integer.parseInt( args[i] );
374
                    }
375
                    catch (Exception e) {
376
                        System.err.println("Error: Bitmask expected after -wxf <index>");
377
                        System.err.println(helpMsg);
378
                        System.exit(1);
379
                    }
380
                    i++;
381
                    try {
382
                        if (i>=args.length)
383
                            throw new Exception();
384
                        l = Integer.parseInt( args[i] );
385
                    }
386
                    catch (Exception e) {
387
                        System.err.println("Error: Value expected after -wxf <index> <bitmask>");
388
                        System.err.println(helpMsg);
389
                        System.exit(1);
390
                    }
391
                    ztex.xmegaFuseWrite( j, (ztex.xmegaFuseRead(j) & ~k) | l );
392
                }
393
            }
394
        }
395
        catch (Exception e) {
396
            System.out.println("Error: "+e.getLocalizedMessage() );
397
        }
398
   }
399
 
400
}

powered by: WebSVN 2.1.0

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