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

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [java/] [FWLoader.java] - Blame information for rev 9

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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