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 8

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

powered by: WebSVN 2.1.0

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