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

Subversion Repositories usb_fpga_2_16

[/] [usb_fpga_2_16/] [trunk/] [java/] [ztex/] [ConfigData.java] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2 3 ZTEX
   Java host software API of ZTEX SDK
3
   Copyright (C) 2009-2014 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
package ztex;
20
 
21
import ch.ntb.usb.*;
22
 
23
/**
24
  * This class represents the configuration data space of ZTEX FPGA Boards that support it.
25
  * The internal format is
26
  * <pre>
27
  * &lt;Address&gt; &lt;Description&gt;
28
  * 0..2      Signature "CD0"
29
  * 3         Kind of FPGA Board, see {@link #boardNames}, e.g. 2 for "ZTEX USB-FPGA Module",
30
  * 4         FPGA Board series, e.g. 2
31
  * 5         FPGA Board number (number behind the dot), e.g. 16
32
  * 6..7      FPGA Board variant (letter), e.g. "b"
33
  * 8..9      FPGA, see {@link #fpgas}, e.g. 12 for X7A200T
34
  * 10        FPGA package, see {@link #packages}, e.g. 3 for FBG484
35
  * 11..13    Speed grade + temperature range, e.g. "2C"
36
  * 14        RAM size, format is ( n & 0xf0 ) << ( (n & 0xf) + 16 ) bytes
37
  * 15        RAM type, see {@link #ramTypes}
38
  * 16..25    Serial number, overwrites SN_STRING of the ZTEX descriptor.
39
  *           If it is equal to "0000000000" (default) it is replaced by the unique MAC address.
40
  * 26..27    Actual size of Bitstream in 4K sectors; 0 means Bitstream disabled (default)
41
  * 28..29    Maximum size of Bitstream in 4K sectors; 0 means that either no Flash
42
  *           is present or that this information is stored in Flash (exchangeable media)
43
  * 30..79    Reserved
44
  * 80..127   48 bytes user space
45
  * </pre>
46
  */
47
 
48
public class ConfigData {
49
 
50
/**
51
  * Kinds of FPGA Boards.
52
  * It's defined as
53
  * <pre>{@code
54
public static final String boardNames[] = {
55
    "(unknown)" ,             // 0
56
    "ZTEX FPGA Module" ,      // 1
57
    "ZTEX USB-FPGA Module"    // 2
58
};}</pre>
59
  **/
60
    public static final String boardNames[] = {
61
        "(unknown)" ,             // 0
62
        "ZTEX FPGA Module" ,      // 1
63
        "ZTEX USB-FPGA Module"    // 2
64
    };
65
 
66
/**
67
  * FPGA's used on ZTEX FPGA Boards.
68
  * It's defined as
69
  * <pre>{@code
70
public static final String fpgas[] = {
71
        "(unknown)" ,  // 0
72
        "XC6SLX9" ,    // 1
73
        "XC6SLX16" ,   // 2
74
        "XC6SLX25" ,   // 3
75
        "XC6SLX45" ,   // 4
76
        "XC6SLX75" ,   // 5
77
        "XC6SLX100" ,  // 6
78
        "XC6SLX150" ,  // 7
79
        "XC7A35T",     // 8
80
        "XC7A50T",     // 9
81
        "XC7A75T",     // 10
82
        "XC7A100T",    // 11
83
        "XC7A200T",    // 12
84
        "Quad-XC6SLX150"  // 13
85
};}</pre>
86
  **/
87
    public static final String fpgas[] = {
88
        "(unknown)" ,  // 0
89
        "XC6SLX9" ,    // 1
90
        "XC6SLX16" ,   // 2
91
        "XC6SLX25" ,   // 3
92
        "XC6SLX45" ,   // 4
93
        "XC6SLX75" ,   // 5
94
        "XC6SLX100" ,  // 6
95
        "XC6SLX150" ,  // 7
96
        "XC7A35T",     // 8
97
        "XC7A50T",     // 9
98
        "XC7A75T",     // 10
99
        "XC7A100T",    // 11
100
        "XC7A200T",    // 12
101
        "Quad-XC6SLX150"  // 13
102
    };
103
 
104
/** * FPGA packages used on ZTEX FPGA boards.
105
  * It's defined as
106
  * <pre>{@code
107
public static final String packages[] = {
108
        "(unknown)",  // 0
109
        "FTG256" ,    // 1  256 balls, 1.0mm
110
        "CSG324" ,    // 2  324 balls, 0.8mm
111
        "CSG484" ,    // 3  484 balls, 0.8mm
112
        "FBG484"      // 4  484 balls, 1.0mm
113
};}</pre>
114
  **/
115
    public static final String packages[] = {
116
        "(unknown)",  // 0
117
        "FTG256" ,    // 1  256 balls, 1.0mm
118
        "CSG324" ,    // 2  324 balls, 0.8mm
119
        "CSG484" ,    // 3  484 balls, 0.8mm
120
        "FBG484"      // 4  484 balls, 1.0mm
121
    };
122
 
123
/** * RAM types and speed used on ZTEX FPGA boards.
124
  * It's defined as
125
  * <pre>{@code
126
public static final String ramTypes[] = {
127
        "(unknown)",        // 0
128
        "DDR-200 SDRAM",    // 1
129
        "DDR-266 SDRAM",    // 2
130
        "DDR-333 SDRAM",    // 3
131
        "DDR-400 SDRAM",    // 4
132
        "DDR2-400 SDRAM",   // 5
133
        "DDR2-533 SDRAM",   // 6
134
        "DDR2-667 SDRAM",   // 7
135
        "DDR2-800 SDRAM",   // 8
136
        "DDR2-1066 SDRAM"   // 9
137
};}</pre>
138
  **/
139
    public static final String ramTypes[] = {
140
        "(unknown)",        // 0
141
        "DDR-200 SDRAM",    // 1
142
        "DDR-266 SDRAM",    // 2
143
        "DDR-333 SDRAM",    // 3
144
        "DDR-400 SDRAM",    // 4
145
        "DDR2-400 SDRAM",   // 5
146
        "DDR2-533 SDRAM",   // 6
147
        "DDR2-667 SDRAM",   // 7
148
        "DDR2-800 SDRAM",   // 8
149
        "DDR2-1066 SDRAM",  // 9
150
        "DDR3-800 SDRAM",   // 10
151
        "DDR3-1066 SDRAM"   // 11
152
    };
153
 
154
    private byte[] data = new byte[128];  // data buffer
155
    private Ztex1v1 ztex = null;
156
 
157
 
158
/**
159
 * Constructs an empty instance.
160
 */
161
   public ConfigData() {
162
        data[0] = 67;
163
        data[1] = 68;
164
        data[2] = 48;
165
        for ( int i=3; i<128; i++)
166
            data[i] = 0;
167
        for ( int i=16; i<26; i++)
168
            data[i] = 48;
169
   }
170
 
171
/**
172
  * Constructs an instance and connects it with z. Also see {@link #connect(Ztex1v1)}.
173
  * @param z The ztex device to connect with.
174
  * @throws InvalidFirmwareException if interface 1 is not supported.
175
  * @throws UsbException If a communication error occurs.
176
  * @throws CapabilityException If no MAC-EEPROM support is present.
177
  */
178
   public ConfigData( Ztex1v1 z ) throws InvalidFirmwareException, UsbException, CapabilityException {
179
        this();
180
        connect(z);
181
   }
182
 
183
 
184
/**
185
  * Reads the configuration data (if existent) from a device and connects it to this instance.
186
  * After this user defined settings (e.g. serial number, bitstream size) are
187
  * stored on device immediately after they are altered.
188
  * @param z The ztex device to connect with.
189
  * @return True if configuration data could be read.
190
  * @throws InvalidFirmwareException If interface 1 is not supported.
191
  * @throws UsbException If a communication error occurs.
192
  * @throws CapabilityException If no MAC-EEPROM support is present.
193
  */
194
   public boolean connect( Ztex1v1 z ) throws InvalidFirmwareException, UsbException, CapabilityException {
195
        ztex = z;
196
        if ( ztex == null ) return false;
197
 
198
        byte[] buf = new byte[128];
199
        ztex.macEepromRead(0,buf,128);
200
        if ( buf[0]==67 && buf[1]==68 && buf[2]==48 ) {
201
            for ( int i=3; i<128; i++)
202
                data[i] = buf[i];
203
            return true;
204
        }
205
        return false;
206
   }
207
 
208
 
209
/**
210
  * Disconnects the currently connected device.
211
  * After this modified settings are not stored on device anymore.
212
  * @return True if a device was connected.
213
  */
214
   public boolean disconnect() {
215
        if ( ztex == null ) return false;
216
        ztex = null;
217
        return true;
218
    }
219
 
220
 
221
/**
222
  * Returns a copy of the configuration data array.
223
  * @return A copy of the configuration data array.
224
  */
225
   public byte[] data () {
226
        byte[] buf = new byte[128];
227
        for ( int i=0; i<128; i++)
228
            buf[i] = data[i];
229
        return buf;
230
   }
231
 
232
 
233
/**
234
  * Returns a string of an array of strings including range check.
235
  */
236
   private String stringOfArray (String[] a, int i) {
237
        if ( i > a.length || i < 0 ) i = 0;
238
        return a[i];
239
    }
240
 
241
 
242
/**
243
  * Finds a string from array.
244
  */
245
   private int findFromArray ( String[] a, String s) {
246
        int i = 0;
247
        while ( i < a.length && !a[i].equals(s) ) i++;
248
        if ( i >= a.length ) {
249
            System.err.print("Invalid value: `" + s + "'. Possible values: `" + a[1] + "'");
250
            for (int j=2; j<a.length; j++ )
251
                System.out.print(", `" + a[j] + "'");
252
            System.out.println();
253
            i = 0;
254
        }
255
        return i;
256
    }
257
 
258
 
259
/**
260
  * Returns a string from data.
261
  */
262
   private String stringFromData (int start, int maxlen) {
263
        int i = 0;
264
        while ( i < maxlen && data[start+i] != 0 ) i++;
265
        return new String(data,start,i);
266
    }
267
 
268
 
269
/**
270
  * send data
271
  * returns true if data was sent
272
  */
273
   private boolean sendData ( int start, int len) throws InvalidFirmwareException, UsbException, CapabilityException {
274
        if ( ztex == null ) return false;
275
        if ( start < 0 ) start = 0;
276
        if ( len > 128-start ) len = 128-start;
277
        if ( len <= 0 ) return false;
278
        byte[] buf = new byte[len];
279
        for ( int i=0; i<len; i++ )
280
            buf[i] = data[start+i];
281
        ConfigData c = ztex.config;
282
        ztex.config = null;
283
        ztex.macEepromWrite(start,buf,len);
284
        ztex.config = c;
285
        return true;
286
    }
287
 
288
/**
289
  * Convert string to data.
290
  */
291
   private void stringToData (String s, int start, int maxlen) {
292
        byte buf[] = s.getBytes();
293
        for ( int i=0; i<maxlen; i++ ) {
294
            data[start+i] = i<buf.length ? buf[i] : 0;
295
        }
296
    }
297
 
298
/**
299
  * Returns the name of the FPGA Board.
300
  * @return The name of the FPGA Board.
301
  */
302
   public String getName () {
303
        return stringOfArray(boardNames,data[3]) + " " + data[4] + "." + data[5] + stringFromData(6,2);
304
   }
305
 
306
/**
307
  * Sets the name of the FPGA Board.
308
  * Example: <pre>setName("ZTEX USB-FPGA Module", 2, 16, "b");  // denotes "ZTEX USB-FPGA Module 2.16b"</pre>
309
  * This setting is not transferred to the FPGA Board because is should not be altered by the user.
310
  * @param kind Kind of FPGA Board, see {@link #boardNames} for possible values, e.g. "ZTEX USB-FPGA Module"
311
  * @param series FPGA Board series, e.g. 2
312
  * @param number FPGA Board number (number behind the dot), e.g. 16
313
  * @param variant FPGA Board variant (letter), e.g. "b"
314
 */
315
   public void setName ( String kind, int series, int number, String variant) {
316
        data[3] = (byte) findFromArray(boardNames, kind);
317
        data[4] = (byte) (series & 255);
318
        data[5] = (byte) (number & 255);
319
        stringToData(variant,6,2);
320
   }
321
 
322
/**
323
  * Returns FPGA information.
324
  * Notation of the result is &lt;name&gt;-&lt;package&gt;-&lt;speed grade and temperature range&gt;, e.g. XC7A200T-FBG484-2C.
325
  * @return FPGA Information.
326
  */
327
   public String getFpga () {
328
        return stringOfArray(fpgas, (data[8] & 255) | ((data[9] & 255) << 8)) + "-" + stringOfArray(packages, data[10]) + "-" + stringFromData(11,3);
329
   }
330
 
331
/**
332
  * Sets FPGA information.
333
  * Example: <pre>setFpga("XC7A200T", "FBG484", "2C");   // denotes Xilinx part number XC7A200T-2FBG484C</pre>
334
  * This setting is not transferred to the FPGA Board because is should not be altered by the user.
335
  * @param name Name of the FPGA, see {@link #fpgas} for possible values, e.g. "XC7A200T"
336
  * @param pckg FPGA package, see {@link #packages} for possible values, e.g. "FBG484"
337
  * @param sg Speed grade and temperature range, e.g. "2C"
338
 */
339
   public void setFpga ( String name, String pckg, String sg) {
340
        int i = findFromArray(fpgas, name);
341
        data[8] = (byte) (i & 255);
342
        data[9] = (byte) ((i>>8) & 255);
343
        data[10] = (byte) findFromArray(packages, pckg);
344
        stringToData(sg,11,3);
345
   }
346
 
347
/**
348
  * Returns RAM type and speed.
349
  * @return FPGA Information.
350
  */
351
   public String getRamType () {
352
        return stringOfArray(ramTypes, (data[15] & 255));
353
   }
354
 
355
/**
356
  * Returns RAM size in bytes.
357
  * @return RAM size in bytes.
358
  */
359
   public int getRamSize () {
360
        return (data[14] & 0xf0) << ( (data[14] & 0xf) + 16 );
361
   }
362
 
363
/**
364
  * Sets RAM information.
365
  * Example: <pre>setRam(128, "DDR2-800 SDRAM");   // sets RAM info to 128 MB DDR2-800 SDRAM</pre>
366
  * This setting is not transferred to the FPGA Board because is should not be altered by the user.
367
  * @param size RAM size in MBytes, e.g. 128
368
  * @param type RAM type and speed, see {@link #ramTypes} for possible values, e.g. "DDR2-800 SDRAM"
369
 */
370
   public void setRam ( int size, String type) {
371
        if (size<0 || size>480) {
372
            System.err.println("Warning: Invalid RAM size: `" + size + "'. Possible values are 0 to 480.");
373
            size = 0;
374
        }
375
        int i=0;
376
        while (size >= 16) {
377
            i++;
378
            size = size >> 1;
379
        }
380
        data[14] = (byte) ((size << 4) | (i & 15));
381
        data[15] = (byte) findFromArray(ramTypes, type);
382
   }
383
 
384
/**
385
  * Returns maximum size of bitstream in bytes.
386
  * This is the amount of flash which should be reserved for the bitstream.
387
  * @return Maximum size of bitstream in bytes sectors.
388
  */
389
   public int getMaxBitstreamSize () {
390
        return ( (data[28] & 255) | ((data[29] & 255) << 8) ) * 4096;
391
   }
392
 
393
/**
394
  * Sets the maximum size of bitstream in 4 KByte sectors.
395
  * This setting is not transferred to the FPGA Board because is should not be altered by the user.
396
  * @param size4k Maximum size of bitstream in 4 KByte sectors. E.g. a value of 256 reserves 1 MByte for the bitstream.
397
 */
398
   public void setMaxBitstreamSize ( int size4k ) {
399
        data[28] = (byte) (size4k & 255);
400
        data[29] = (byte) ((size4k>> 8) & 255);
401
   }
402
 
403
/**
404
  * Returns actual size of bitstream in bytes sectors.
405
  * 0 means that no bitstream is stored. The value is rounded up to a multiples of 4096.
406
  * @return Actual size of bitstream in byte sectors.
407
  */
408
   public int getBitstreamSize () {
409
        return ( (data[26] & 255) | ((data[27] & 255) << 8) ) * 4096;
410
   }
411
 
412
/**
413
  * Sets the actual size of bitstream in bytes. The value is rounded up to a multiple of 4096.
414
  * If a device is connected, this setting is transferred to the FPGA Board.
415
  * A warning is printed if bitstream size is larger then the reserved size (see {@link #getMaxBitstreamSize()}).
416
  * @param size Actual size of bitstream in bytes.
417
  * @return True if a device is connected and setting was send.
418
  * @throws InvalidFirmwareException If interface 1 is not supported.
419
  * @throws UsbException If a communication error occurs.
420
  * @throws CapabilityException If no MAC-EEPROM support is present.
421
 */
422
   public boolean setBitstreamSize ( int size ) throws InvalidFirmwareException, UsbException, CapabilityException {
423
        if ( size < 0 ) size = 0;
424
        size = (size + 4095) >> 12;
425
        int i = (data[28] & 255) | ((data[29] & 255) << 8);
426
        if ( size > i )  System.err.println("Warning: Bitstream size of " + size + " 4K sectors larger than reserved memory of " + i + " 4K sectors");
427
        data[26] = (byte) (size & 255);
428
        data[27] = (byte) ((size>> 8) & 255);
429
        return sendData(26,2);
430
   }
431
 
432
/**
433
  * Returns the serial number. This is not necessarily the serial number
434
  * returned by the FPGA board according to the USB specification, see {@link #setSN(String)}
435
  * @return Serial number as stored in the configuration data space.
436
  */
437
   public String getSN () {
438
        return stringFromData(16,10);
439
   }
440
 
441
/**
442
  * Sets the serial number.
443
  * During start-up the firmware overwrites SN_STRING from the ZTEX descriptor (see {@link ZtexDevice1}) by this value.
444
  * If it is equal to "0000000000" (default) it is replaced by the unique MAC address. <p>
445
  * This setting is transferred to the FPGA Board.
446
  * Change takes effect after the next restart of the firmware.
447
  * @param sn Serial number string. Only the first 10 characters are considered.
448
  * @return True if a device is connected and setting was send.
449
  * @throws InvalidFirmwareException If interface 1 is not supported.
450
  * @throws UsbException If a communication error occurs.
451
  * @throws CapabilityException If no MAC-EEPROM support is present.
452
 */
453
   public boolean setSN ( String sn ) throws InvalidFirmwareException, UsbException, CapabilityException {
454
        stringToData(sn,16,10);
455
        return sendData(16,10);
456
   }
457
 
458
/**
459
  * Returns user data at index i.
460
  * @param i the index. Valid values are 0 to 47.
461
  * @return User data.
462
  * @throws IndexOutOfBoundsException If i is smaller than 0 or greater than 47.
463
  */
464
   public byte getUserData (int i) {
465
        if ( i<0 || i>47 ) throw new IndexOutOfBoundsException ( "Invalid index: " + i + ". Valid range is 0 to 47.");
466
        return data[80+i];
467
   }
468
 
469
/**
470
  * Sets user data at index i to value v. Use the method {@link #getMaxBitstreamSize()}
471
  * to transfer the data to the FPGA Board.
472
  * @param i The index. Valid values are 0 to 47.
473
  * @param v The value.
474
  * @throws IndexOutOfBoundsException If i is smaller than 0 or greater than 47.
475
  */
476
   public void setUserData (int i, byte v) throws IndexOutOfBoundsException {
477
        if ( i<0 || i>47 ) throw new IndexOutOfBoundsException ( "Invalid index: " + i + ". Valid range is 0 to 47.");
478
        data[80+i] = v;
479
   }
480
 
481
/**
482
  * Sends the user data to the FPGA Board.
483
  * @return True if a device is connected and data could be send.
484
  * @throws InvalidFirmwareException If interface 1 is not supported.
485
  * @throws UsbException If a communication error occurs.
486
  * @throws CapabilityException If no MAC-EEPROM support is present.
487
  */
488
   public boolean sendtUserData () throws InvalidFirmwareException, UsbException, CapabilityException {
489
        return sendData(80,48);
490
   }
491
}

powered by: WebSVN 2.1.0

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