1 |
2 |
ZTEX |
/*!
|
2 |
|
|
flashdemo -- demo for Flash memory access from firmware and host software for ZTEX USB-FPGA Module 1.15
|
3 |
|
|
Copyright (C) 2009-2011 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 |
|
|
import java.io.*;
|
20 |
|
|
import java.util.*;
|
21 |
|
|
|
22 |
|
|
import ch.ntb.usb.*;
|
23 |
|
|
|
24 |
|
|
import ztex.*;
|
25 |
|
|
|
26 |
|
|
// *****************************************************************************
|
27 |
|
|
// ******* ParameterException **************************************************
|
28 |
|
|
// *****************************************************************************
|
29 |
|
|
// Exception the prints a help message
|
30 |
|
|
class ParameterException extends Exception {
|
31 |
|
|
public final static String helpMsg = new String (
|
32 |
|
|
"Parameters:\n"+
|
33 |
|
|
" -d <number> Device Number (default: 0)\n" +
|
34 |
|
|
" -p Print bus info\n" +
|
35 |
|
|
" -f Force upload Firmware to RAM\n" +
|
36 |
|
|
// " -va Upload configuration data for USB-FPGA Modules 2.16a\n" +
|
37 |
|
|
" -vb Upload configuration data for USB-FPGA Modules 2.16b\n" +
|
38 |
|
|
" -c Clear settings from configuration data\n" +
|
39 |
|
|
" -ue Upload Firmware to EEPROM\n" +
|
40 |
|
|
" -re Reset EEPROM Firmware\n" +
|
41 |
|
|
" -r Reset device after uploading\n" +
|
42 |
|
|
" -h This help" );
|
43 |
|
|
|
44 |
|
|
public ParameterException (String msg) {
|
45 |
|
|
super( msg + "\n" + helpMsg );
|
46 |
|
|
}
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
// *****************************************************************************
|
50 |
|
|
// ******* Default *************************************************************
|
51 |
|
|
// *****************************************************************************
|
52 |
|
|
class Default {
|
53 |
|
|
|
54 |
|
|
// ******* main ****************************************************************
|
55 |
|
|
public static void main (String args[]) {
|
56 |
|
|
|
57 |
|
|
int devNum = 0;
|
58 |
|
|
boolean force = false;
|
59 |
|
|
boolean clear = false;
|
60 |
|
|
boolean reset = false;
|
61 |
|
|
int variant = 0;
|
62 |
|
|
|
63 |
|
|
try {
|
64 |
|
|
// init USB stuff
|
65 |
|
|
LibusbJava.usb_init();
|
66 |
|
|
|
67 |
|
|
// scan the USB bus
|
68 |
|
|
ZtexScanBus1 bus = new ZtexScanBus1( ZtexDevice1.ztexVendorId, ZtexDevice1.ztexProductId, true, false, 1);
|
69 |
|
|
if ( bus.numberOfDevices() <= 0) {
|
70 |
|
|
System.err.println("No devices found");
|
71 |
|
|
System.exit(0);
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
// scan the command line arguments
|
75 |
|
|
for (int i=0; i<args.length; i++ ) {
|
76 |
|
|
if ( args[i].equals("-d") ) {
|
77 |
|
|
i++;
|
78 |
|
|
try {
|
79 |
|
|
if (i>=args.length) throw new Exception();
|
80 |
|
|
devNum = Integer.parseInt( args[i] );
|
81 |
|
|
}
|
82 |
|
|
catch (Exception e) {
|
83 |
|
|
throw new ParameterException("Device number expected after -d");
|
84 |
|
|
}
|
85 |
|
|
}
|
86 |
|
|
else if ( args[i].equals("-p") ) {
|
87 |
|
|
bus.printBus(System.out);
|
88 |
|
|
System.exit(0);
|
89 |
|
|
}
|
90 |
|
|
else if ( args[i].equals("-f") ) {
|
91 |
|
|
force = true;
|
92 |
|
|
}
|
93 |
|
|
else if ( args[i].equals("-va") ) {
|
94 |
|
|
variant = 1;
|
95 |
|
|
}
|
96 |
|
|
else if ( args[i].equals("-vb") ) {
|
97 |
|
|
variant = 2;
|
98 |
|
|
}
|
99 |
|
|
else if ( args[i].equals("-c") ) {
|
100 |
|
|
clear = true;
|
101 |
|
|
}
|
102 |
|
|
else if ( args[i].equals("-r") ) {
|
103 |
|
|
reset = true;
|
104 |
|
|
}
|
105 |
|
|
else if ( args[i].equals("-h") ) {
|
106 |
|
|
System.err.println(ParameterException.helpMsg);
|
107 |
|
|
System.exit(0);
|
108 |
|
|
}
|
109 |
|
|
else if ( !args[i].equals("-re") && !args[i].equals("-ue") )
|
110 |
|
|
throw new ParameterException("Invalid Parameter: "+args[i]);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
// create the main class
|
114 |
|
|
Ztex1v1 ztex = new Ztex1v1 ( bus.device(devNum) );
|
115 |
|
|
|
116 |
|
|
// upload the firmware if necessary
|
117 |
|
|
if ( force || ! ztex.valid() || ! ztex.InterfaceCapabilities(ztex.CAPABILITY_EEPROM) || ! ztex.InterfaceCapabilities(ztex.CAPABILITY_MAC_EEPROM) ) {
|
118 |
|
|
System.out.println("Firmware upload time: " + ztex.uploadFirmware( "default.ihx", force ) + " ms");
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
for (int i=0; i<args.length; i++ ) {
|
122 |
|
|
if ( args[i].equals("-re") ) {
|
123 |
|
|
ztex.eepromDisable();
|
124 |
|
|
}
|
125 |
|
|
else if ( args[i].equals("-ue") ) {
|
126 |
|
|
System.out.println("Firmware to EEPROM upload time: " + ztex.eepromUpload( "default.ihx", force ) + " ms");
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
// if ( ztex.config!=null ) System.out.println(ztex.config.getName());
|
131 |
|
|
|
132 |
|
|
// generate and upload config data
|
133 |
|
|
if ( variant > 0 )
|
134 |
|
|
{
|
135 |
|
|
ConfigData config = new ConfigData();
|
136 |
|
|
if ( ! clear ) {
|
137 |
|
|
if ( config.connect(ztex) )
|
138 |
|
|
System.out.println("Reading configuration data.");
|
139 |
|
|
config.disconnect();
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
// System.out.println("ud[33]="+config.getUserData(33));
|
143 |
|
|
// config.setUserData(33, (byte) (config.getUserData(33)+1) );
|
144 |
|
|
|
145 |
|
|
if ( variant == 1 ) {
|
146 |
|
|
config.setName("ZTEX USB-FPGA Module", 2, 16, "a");
|
147 |
|
|
config.setFpga("XC7A100T", "FBG484", "2C");
|
148 |
|
|
config.setMaxBitstreamSize(1130);
|
149 |
|
|
}
|
150 |
|
|
else {
|
151 |
|
|
config.setName("ZTEX USB-FPGA Module", 2, 16, "b");
|
152 |
|
|
config.setFpga("XC7A200T", "FBG484", "2C");
|
153 |
|
|
config.setMaxBitstreamSize(2880);
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
System.out.println("Writing configuration data.");
|
157 |
|
|
ztex.config=null;
|
158 |
|
|
ztex.macEepromWrite(0, config.data(), 128);
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
if ( reset ) ztex.resetEzUsb();
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
}
|
165 |
|
|
catch (Exception e) {
|
166 |
|
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
167 |
|
|
}
|
168 |
|
|
catch (Error e) {
|
169 |
|
|
System.out.println("Error: "+e.getLocalizedMessage() );
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
}
|