| 1 |
2 |
ZTEX |
/*!
|
| 2 |
|
|
Java Driver API for the ZTEX Firmware Kit
|
| 3 |
|
|
Copyright (C) 2009-2010 ZTEX e.K.
|
| 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 |
|
|
Functions for USB devices with ZTEX descriptor 1, Interface 1
|
| 21 |
|
|
Interface capabilities and vendor requests (VR) / commands (VC):
|
| 22 |
|
|
0.0 : EEPROM support
|
| 23 |
|
|
VR 0x38 : read from EEPROM
|
| 24 |
|
|
Returns:
|
| 25 |
|
|
EEPROM data
|
| 26 |
|
|
VC 0x39 : write to EEPROM
|
| 27 |
|
|
VR 0x3a : EEPROM state
|
| 28 |
|
|
Returns:
|
| 29 |
|
|
Offs Description
|
| 30 |
|
|
0-1 bytes written
|
| 31 |
|
|
2 checksum
|
| 32 |
|
|
3 0:idle, 1:busy or error
|
| 33 |
|
|
|
| 34 |
|
|
0.1 : FPGA Configuration
|
| 35 |
|
|
VR 0x30 : get FPGA state
|
| 36 |
|
|
Returns:
|
| 37 |
|
|
Offs Description
|
| 38 |
|
|
|
| 39 |
|
|
1 checksum
|
| 40 |
|
|
2-5 transferred bytes
|
| 41 |
|
|
6 INIT_B state
|
| 42 |
|
|
VC 0x31 : reset FPGA
|
| 43 |
|
|
VC 0x32 : send FPGA configuration data (Bitstream)
|
| 44 |
|
|
|
| 45 |
|
|
0.2 : Flash memory support
|
| 46 |
3 |
ZTEX |
VR 0x40 : read Flash state
|
| 47 |
2 |
ZTEX |
Returns:
|
| 48 |
|
|
Offs Description
|
| 49 |
|
|
|
| 50 |
|
|
1-2 Sector size
|
| 51 |
|
|
3-6 Number of sectors
|
| 52 |
|
|
7 Error code
|
| 53 |
|
|
VR 0x41 : read from Flash
|
| 54 |
|
|
VR 0x42 : write to Flash
|
| 55 |
3 |
ZTEX |
0.3 : Debug helper support
|
| 56 |
|
|
VR 0x28 : read debug data
|
| 57 |
|
|
Returns:
|
| 58 |
|
|
Offs Description
|
| 59 |
|
|
0-1 number of messages
|
| 60 |
|
|
2 stack size in messages
|
| 61 |
|
|
3 message size in bytes
|
| 62 |
|
|
>=4 message stack
|
| 63 |
2 |
ZTEX |
*/
|
| 64 |
|
|
package ztex;
|
| 65 |
|
|
|
| 66 |
|
|
import java.io.*;
|
| 67 |
|
|
import java.util.*;
|
| 68 |
|
|
|
| 69 |
|
|
import ch.ntb.usb.*;
|
| 70 |
|
|
|
| 71 |
|
|
/**
|
| 72 |
|
|
* This class implements the communication protocol of the interface version 1 for the interaction with the ZTEX firmware.
|
| 73 |
|
|
* <p>
|
| 74 |
|
|
* The features supported by this interface can be accessed via vendor commands and vendor requests via Endpoint 0.
|
| 75 |
|
|
* Each feature can be enabled or disabled by the firmware and also depends from the hardware.
|
| 76 |
|
|
* The presence of a feature is indicated by a 1 in the corresponding feature bit of the ZTEX descriptor 1, see {@link ZtexDevice1}.
|
| 77 |
|
|
* The following table gives an overview about the features
|
| 78 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=10>
|
| 79 |
|
|
* <tr>
|
| 80 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Capability bit</b></td>
|
| 81 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 82 |
|
|
* </tr>
|
| 83 |
|
|
* <tr>
|
| 84 |
|
|
* <td bgcolor="#ffffff" valign="top">0.0</td>
|
| 85 |
|
|
* <td bgcolor="#ffffff" valign="top" colspan=2>
|
| 86 |
|
|
* EEPROM support<p>
|
| 87 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=6>
|
| 88 |
|
|
* <tr>
|
| 89 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Vendor request (VR)<br> or command (VC)</b></td>
|
| 90 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 91 |
|
|
* </tr>
|
| 92 |
|
|
* <tr>
|
| 93 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x38</td>
|
| 94 |
|
|
* <td bgcolor="#ffffff" valign="top">Read from EEPROM</td>
|
| 95 |
|
|
* </tr>
|
| 96 |
|
|
* <tr>
|
| 97 |
|
|
* <td bgcolor="#ffffff" valign="top">VC 0x39</td>
|
| 98 |
|
|
* <td bgcolor="#ffffff" valign="top">Write to EEPROM</td>
|
| 99 |
|
|
* </tr>
|
| 100 |
|
|
* <tr>
|
| 101 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x3a</td>
|
| 102 |
|
|
* <td bgcolor="#ffffff" valign="top">Get EEPROM state. Returns:
|
| 103 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=4>
|
| 104 |
|
|
* <tr>
|
| 105 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Bytes</b></td>
|
| 106 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 107 |
|
|
* </tr>
|
| 108 |
|
|
* <tr>
|
| 109 |
|
|
* <td bgcolor="#ffffff" valign="top">0-1</td>
|
| 110 |
|
|
* <td bgcolor="#ffffff" valign="top">Number of bytes written.</td>
|
| 111 |
|
|
* </tr>
|
| 112 |
|
|
* <tr>
|
| 113 |
|
|
* <td bgcolor="#ffffff" valign="top">2</td>
|
| 114 |
|
|
* <td bgcolor="#ffffff" valign="top">Checksum</td>
|
| 115 |
|
|
* </tr>
|
| 116 |
|
|
* <tr>
|
| 117 |
|
|
* <td bgcolor="#ffffff" valign="top">3</td>
|
| 118 |
|
|
* <td bgcolor="#ffffff" valign="top">0:idle, 1:busy or error</td>
|
| 119 |
|
|
* </tr>
|
| 120 |
|
|
* </table>
|
| 121 |
|
|
* </td>
|
| 122 |
|
|
* </tr>
|
| 123 |
|
|
* </table>
|
| 124 |
|
|
* </td>
|
| 125 |
|
|
* </tr>
|
| 126 |
|
|
* <tr>
|
| 127 |
|
|
* <td bgcolor="#ffffff" valign="top">0.1</td>
|
| 128 |
|
|
* <td bgcolor="#ffffff" valign="top" colspan=2>
|
| 129 |
|
|
* FPGA Configuration<p>
|
| 130 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=6>
|
| 131 |
|
|
* <tr>
|
| 132 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Vendor request (VR)<br> or command (VC)</b></td>
|
| 133 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 134 |
|
|
* </tr>
|
| 135 |
|
|
* <tr>
|
| 136 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x30</td>
|
| 137 |
|
|
* <td bgcolor="#ffffff" valign="top">Get FPGA state. Returns:
|
| 138 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=4>
|
| 139 |
|
|
* <tr>
|
| 140 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Bytes</b></td>
|
| 141 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 142 |
|
|
* </tr>
|
| 143 |
|
|
* <tr>
|
| 144 |
|
|
* <td bgcolor="#ffffff" valign="top">0</td>
|
| 145 |
|
|
* <td bgcolor="#ffffff" valign="top">1: unconfigured, 0:configured</td>
|
| 146 |
|
|
* </tr>
|
| 147 |
|
|
* <tr>
|
| 148 |
|
|
* <td bgcolor="#ffffff" valign="top">1</td>
|
| 149 |
|
|
* <td bgcolor="#ffffff" valign="top">Checksum</td>
|
| 150 |
|
|
* </tr>
|
| 151 |
|
|
* <tr>
|
| 152 |
|
|
* <td bgcolor="#ffffff" valign="top">2-5</td>
|
| 153 |
|
|
* <td bgcolor="#ffffff" valign="top">Number of bytes transferred.</td>
|
| 154 |
|
|
* </tr>
|
| 155 |
|
|
* <tr>
|
| 156 |
|
|
* <td bgcolor="#ffffff" valign="top">6</td>
|
| 157 |
|
|
* <td bgcolor="#ffffff" valign="top">INIT_B states (Must be 222).</td>
|
| 158 |
|
|
* </tr>
|
| 159 |
|
|
* </table>
|
| 160 |
|
|
* </td>
|
| 161 |
|
|
* </tr>
|
| 162 |
|
|
* <tr>
|
| 163 |
|
|
* <td bgcolor="#ffffff" valign="top">VC 0x31</td>
|
| 164 |
|
|
* <td bgcolor="#ffffff" valign="top">Reset FPGA</td>
|
| 165 |
|
|
* </tr>
|
| 166 |
|
|
* <tr>
|
| 167 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x32</td>
|
| 168 |
|
|
* <td bgcolor="#ffffff" valign="top">Send Bitstream</td>
|
| 169 |
|
|
* </tr>
|
| 170 |
|
|
* </table>
|
| 171 |
|
|
* </td>
|
| 172 |
|
|
* </tr>
|
| 173 |
|
|
* <tr>
|
| 174 |
|
|
* <td bgcolor="#ffffff" valign="top">0.2</td>
|
| 175 |
|
|
* <td bgcolor="#ffffff" valign="top" colspan=2>
|
| 176 |
|
|
* Flash memory support<p>
|
| 177 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=6>
|
| 178 |
|
|
* <tr>
|
| 179 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Vendor request (VR)<br> or command (VC)</b></td>
|
| 180 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 181 |
|
|
* </tr>
|
| 182 |
|
|
* <tr>
|
| 183 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x40</td>
|
| 184 |
|
|
* <td bgcolor="#ffffff" valign="top">Get Flash state. Returns:
|
| 185 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=4>
|
| 186 |
|
|
* <tr>
|
| 187 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Bytes</b></td>
|
| 188 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 189 |
|
|
* </tr>
|
| 190 |
|
|
* <tr>
|
| 191 |
|
|
* <td bgcolor="#ffffff" valign="top">0</td>
|
| 192 |
|
|
* <td bgcolor="#ffffff" valign="top">1:enabled, 0:disabled</td>
|
| 193 |
|
|
* </tr>
|
| 194 |
|
|
* <tr>
|
| 195 |
|
|
* <td bgcolor="#ffffff" valign="top">1-2</td>
|
| 196 |
|
|
* <td bgcolor="#ffffff" valign="top">Sector size</td>
|
| 197 |
|
|
* </tr>
|
| 198 |
|
|
* <tr>
|
| 199 |
|
|
* <td bgcolor="#ffffff" valign="top">3-6</td>
|
| 200 |
|
|
* <td bgcolor="#ffffff" valign="top">Number of sectors</td>
|
| 201 |
|
|
* </tr>
|
| 202 |
|
|
* <tr>
|
| 203 |
|
|
* <td bgcolor="#ffffff" valign="top">7</td>
|
| 204 |
|
|
* <td bgcolor="#ffffff" valign="top">Error code</td>
|
| 205 |
|
|
* </tr>
|
| 206 |
|
|
* </table>
|
| 207 |
|
|
* </td>
|
| 208 |
|
|
* </tr>
|
| 209 |
|
|
* <tr>
|
| 210 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x41</td>
|
| 211 |
|
|
* <td bgcolor="#ffffff" valign="top">Read one sector from Flash</td>
|
| 212 |
|
|
* </tr>
|
| 213 |
|
|
* <tr>
|
| 214 |
|
|
* <td bgcolor="#ffffff" valign="top">VC 0x42</td>
|
| 215 |
|
|
* <td bgcolor="#ffffff" valign="top">Write one sector to Flash</td>
|
| 216 |
|
|
* </tr>
|
| 217 |
|
|
* </table>
|
| 218 |
|
|
* </td>
|
| 219 |
|
|
* </tr>
|
| 220 |
3 |
ZTEX |
* <tr>
|
| 221 |
|
|
* <td bgcolor="#ffffff" valign="top">0.3</td>
|
| 222 |
|
|
* <td bgcolor="#ffffff" valign="top" colspan=2>
|
| 223 |
|
|
* Debug helper support<p>
|
| 224 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=6>
|
| 225 |
|
|
* <tr>
|
| 226 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Vendor request (VR)<br> or command (VC)</b></td>
|
| 227 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 228 |
|
|
* </tr>
|
| 229 |
|
|
* <tr>
|
| 230 |
|
|
* <td bgcolor="#ffffff" valign="top">VR 0x28</td>
|
| 231 |
|
|
* <td bgcolor="#ffffff" valign="top">Get debug data. Returns:
|
| 232 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=4>
|
| 233 |
|
|
* <tr>
|
| 234 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Bytes</b></td>
|
| 235 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 236 |
|
|
* </tr>
|
| 237 |
|
|
* <tr>
|
| 238 |
|
|
* <td bgcolor="#ffffff" valign="top">0-1</td>
|
| 239 |
|
|
* <td bgcolor="#ffffff" valign="top">Number of the last message</td>
|
| 240 |
|
|
* </tr>
|
| 241 |
|
|
* <tr>
|
| 242 |
|
|
* <td bgcolor="#ffffff" valign="top">2</td>
|
| 243 |
|
|
* <td bgcolor="#ffffff" valign="top">Stack size in messages</td>
|
| 244 |
|
|
* </tr>
|
| 245 |
|
|
* <tr>
|
| 246 |
|
|
* <td bgcolor="#ffffff" valign="top">3</td>
|
| 247 |
|
|
* <td bgcolor="#ffffff" valign="top">Message size in bytes</td>
|
| 248 |
|
|
* </tr>
|
| 249 |
|
|
* <tr>
|
| 250 |
|
|
* <td bgcolor="#ffffff" valign="top">≥4</td>
|
| 251 |
|
|
* <td bgcolor="#ffffff" valign="top">Message stack</td>
|
| 252 |
|
|
* </tr>
|
| 253 |
|
|
* </table>
|
| 254 |
|
|
* </td>
|
| 255 |
|
|
* </tr>
|
| 256 |
|
|
* </table>
|
| 257 |
|
|
* </td>
|
| 258 |
|
|
* </tr>
|
| 259 |
2 |
ZTEX |
* </table>
|
| 260 |
|
|
* @see ZtexDevice1
|
| 261 |
|
|
* @see Ztex1
|
| 262 |
|
|
*/
|
| 263 |
|
|
|
| 264 |
|
|
public class Ztex1v1 extends Ztex1 {
|
| 265 |
3 |
ZTEX |
/** * Capability index for EEPROM support. */
|
| 266 |
|
|
public static final int CAPABILITY_EEPROM = 0;
|
| 267 |
|
|
/** * Capability index for FPGA configuration support. */
|
| 268 |
|
|
public static final int CAPABILITY_FPGA = 1;
|
| 269 |
|
|
/** * Capability index for FLASH memory support. */
|
| 270 |
|
|
public static final int CAPABILITY_FLASH = 2;
|
| 271 |
|
|
/** * Capability index for DEBUG helper support. */
|
| 272 |
|
|
public static final int CAPABILITY_DEBUG = 3;
|
| 273 |
|
|
/** * Capability index for AVR XMEGA support. */
|
| 274 |
|
|
public static final int CAPABILITY_XMEGA = 4;
|
| 275 |
|
|
|
| 276 |
2 |
ZTEX |
/** * The names of the capabilities */
|
| 277 |
|
|
public static final String capabilityStrings[] = {
|
| 278 |
|
|
"EEPROM read/write" ,
|
| 279 |
|
|
"FPGA configuration" ,
|
| 280 |
3 |
ZTEX |
"Flash memory support",
|
| 281 |
|
|
"Debug helper",
|
| 282 |
|
|
"XMEGA support"
|
| 283 |
2 |
ZTEX |
};
|
| 284 |
|
|
|
| 285 |
|
|
private boolean fpgaConfigured = false;
|
| 286 |
|
|
private int fpgaChecksum = 0;
|
| 287 |
|
|
private int fpgaBytes = 0;
|
| 288 |
|
|
private int fpgaInitB = 0;
|
| 289 |
|
|
private int fpgaFlashResult = 255;
|
| 290 |
|
|
private boolean fpgaFlashBitSwap = false;
|
| 291 |
|
|
|
| 292 |
|
|
/** * Number of bytes written to EEPROM. (Obtained by {@link #eepromState()}.) */
|
| 293 |
|
|
public int eepromBytes = 0;
|
| 294 |
|
|
/** * Checksum of the last EEPROM transfer. (Obtained by {@link #eepromState()}.) */
|
| 295 |
|
|
public int eepromChecksum = 0;
|
| 296 |
|
|
|
| 297 |
|
|
private int flashEnabled = -1;
|
| 298 |
|
|
private int flashSectorSize = -1;
|
| 299 |
|
|
private int flashSectors = -1;
|
| 300 |
|
|
|
| 301 |
|
|
/** * Last Flash error code obtained by {@link #flashState()}. See FLASH_EC_* for possible error codes. */
|
| 302 |
|
|
public int flashEC = 0;
|
| 303 |
|
|
/** * Means no error. */
|
| 304 |
|
|
public static final int FLASH_EC_NO_ERROR = 0;
|
| 305 |
|
|
/** * Signals an error while attempting to execute a command. */
|
| 306 |
|
|
public static final int FLASH_EC_CMD_ERROR = 1;
|
| 307 |
|
|
/** * Signals that a timeout occurred. */
|
| 308 |
|
|
public static final int FLASH_EC_TIMEOUT = 2;
|
| 309 |
|
|
/** * Signals that Flash memory it busy. */
|
| 310 |
|
|
public static final int FLASH_EC_BUSY = 3;
|
| 311 |
|
|
/** * Signals that another Flash operation is pending. */
|
| 312 |
|
|
public static final int FLASH_EC_PENDING = 4;
|
| 313 |
|
|
/** * Signals an error while attempting to read from Flash. */
|
| 314 |
|
|
public static final int FLASH_EC_READ_ERROR = 5;
|
| 315 |
|
|
/** * Signals an error while attempting to write to Flash. */
|
| 316 |
|
|
public static final int FLASH_EC_WRITE_ERROR = 6;
|
| 317 |
|
|
/** * Signals the the installed Flash memeory is not supported. */
|
| 318 |
|
|
public static final int FLASH_EC_NOTSUPPORTED = 7;
|
| 319 |
3 |
ZTEX |
|
| 320 |
|
|
private int debugStackSize = -1;
|
| 321 |
|
|
private int debugMsgSize = -1;
|
| 322 |
|
|
private int debugLastMsg = 0;
|
| 323 |
|
|
/** * Is set by {@link #debugReadMessages(boolean,byte[])} and conains the number of new messages. */
|
| 324 |
|
|
public int debugNewMessages = 0;
|
| 325 |
2 |
ZTEX |
|
| 326 |
|
|
// ******* Ztex1v1 *************************************************************
|
| 327 |
|
|
/**
|
| 328 |
|
|
* Constructs an instance from a given device.
|
| 329 |
|
|
* @param pDev The given device.
|
| 330 |
|
|
* @throws UsbException if an communication error occurred.
|
| 331 |
|
|
*/
|
| 332 |
|
|
public Ztex1v1 ( ZtexDevice1 pDev ) throws UsbException {
|
| 333 |
|
|
super ( pDev );
|
| 334 |
|
|
}
|
| 335 |
|
|
|
| 336 |
|
|
// ******* valid ***************************************************************
|
| 337 |
|
|
/**
|
| 338 |
|
|
* Returns true if ZTEX interface 1 is available.
|
| 339 |
|
|
* @return true if ZTEX interface 1 is available.
|
| 340 |
|
|
*/
|
| 341 |
|
|
public boolean valid ( ) {
|
| 342 |
|
|
return dev().valid() && dev().interfaceVersion()==1;
|
| 343 |
|
|
}
|
| 344 |
|
|
|
| 345 |
|
|
/**
|
| 346 |
|
|
* Returns true if ZTEX interface 1 and capability i.j are available.
|
| 347 |
|
|
* @param i byte index of the capability
|
| 348 |
|
|
* @param j bit index of the capability
|
| 349 |
|
|
* @return true if ZTEX interface 1 and capability i.j are available.
|
| 350 |
|
|
*/
|
| 351 |
|
|
public boolean valid ( int i, int j) {
|
| 352 |
|
|
return dev().valid() && dev().interfaceVersion()==1 && dev().interfaceCapabilities(i,j);
|
| 353 |
|
|
}
|
| 354 |
|
|
|
| 355 |
|
|
// ******* compatible **********************************************************
|
| 356 |
|
|
/**
|
| 357 |
|
|
* Checks whether the given product ID is compatible to the device corresponding to this class and whether interface 1 is supported.<br>
|
| 358 |
|
|
* The given product ID is compatible
|
| 359 |
|
|
* <pre>if ( this.productId(0)==0 || productId0<=0 || this.productId(0)==productId0 ) &&
|
| 360 |
|
|
( this.productId(0)==0 || productId1<=0 || this.productId(1)==productId1 ) &&
|
| 361 |
|
|
( this.productId(2)==0 || productId2<=0 || this.productId(2)==productId2 ) &&
|
| 362 |
|
|
( this.productId(3)==0 || productId3<=0 || this.productId(3)==productId3 ) </pre>
|
| 363 |
|
|
* @param productId0 Byte 0 of the given product ID
|
| 364 |
|
|
* @param productId1 Byte 1 of the given product ID
|
| 365 |
|
|
* @param productId2 Byte 2 of the given product ID
|
| 366 |
|
|
* @param productId3 Byte 3 of the given product ID
|
| 367 |
|
|
* @return true if the given product ID is compatible and interface 1 is supported.
|
| 368 |
|
|
*/
|
| 369 |
|
|
public boolean compatible ( int productId0, int productId1, int productId2, int productId3 ) {
|
| 370 |
|
|
return dev().valid() && dev().compatible ( productId0, productId1, productId2, productId3 ) && dev().interfaceVersion()==1;
|
| 371 |
|
|
}
|
| 372 |
|
|
|
| 373 |
|
|
// ******* checkValid **********************************************************
|
| 374 |
|
|
/**
|
| 375 |
|
|
* Checks whether ZTEX descriptor 1 is available and interface 1 is supported.
|
| 376 |
|
|
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available or interface 1 is not supported.
|
| 377 |
|
|
*/
|
| 378 |
|
|
public void checkValid () throws InvalidFirmwareException {
|
| 379 |
|
|
super.checkValid();
|
| 380 |
|
|
if ( dev().interfaceVersion() != 1 )
|
| 381 |
|
|
throw new InvalidFirmwareException(this, "Wrong interface: " + dev().interfaceVersion() + ", expected: 1" );
|
| 382 |
|
|
}
|
| 383 |
|
|
|
| 384 |
|
|
// ******* checkCapability *****************************************************
|
| 385 |
|
|
/**
|
| 386 |
|
|
* Checks whether ZTEX descriptor 1 is available and interface 1 and a given capability are supported.
|
| 387 |
|
|
* @param i byte index of the capability
|
| 388 |
|
|
* @param j bit index of the capability
|
| 389 |
|
|
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available or interface 1 is not supported.
|
| 390 |
|
|
* @throws CapabilityException if the given capability is not supported.
|
| 391 |
|
|
*/
|
| 392 |
|
|
public void checkCapability ( int i, int j ) throws InvalidFirmwareException, CapabilityException {
|
| 393 |
|
|
checkValid();
|
| 394 |
|
|
if ( ! dev().interfaceCapabilities(i,j) ) {
|
| 395 |
|
|
int k = i*8 + j;
|
| 396 |
|
|
if ( k>=0 && k<capabilityStrings.length )
|
| 397 |
|
|
throw new CapabilityException( this, ( k>=0 && k<=capabilityStrings.length ) ? capabilityStrings[k] : ("Capabilty " + i + "," + j) );
|
| 398 |
|
|
}
|
| 399 |
|
|
}
|
| 400 |
|
|
|
| 401 |
3 |
ZTEX |
/**
|
| 402 |
|
|
* Checks whether ZTEX descriptor 1 is available and interface 1 and a given capability are supported.
|
| 403 |
|
|
* @param i capability index (0..47)
|
| 404 |
|
|
* @throws InvalidFirmwareException if ZTEX descriptor 1 is not available or interface 1 is not supported.
|
| 405 |
|
|
* @throws CapabilityException if the given capability is not supported.
|
| 406 |
|
|
*/
|
| 407 |
|
|
public void checkCapability ( int i ) throws InvalidFirmwareException, CapabilityException {
|
| 408 |
|
|
checkCapability(i/8, i%8);
|
| 409 |
|
|
}
|
| 410 |
|
|
|
| 411 |
2 |
ZTEX |
// ******* checkCompatible *****************************************************
|
| 412 |
|
|
/**
|
| 413 |
|
|
* Checks whether the given product ID is compatible to the device corresponding to this class and whether interface 1 is supported.
|
| 414 |
|
|
* See {@link #compatible(int,int,int,int)}.
|
| 415 |
|
|
* @param productId0 Byte 0 of the given product ID
|
| 416 |
|
|
* @param productId1 Byte 1 of the given product ID
|
| 417 |
|
|
* @param productId2 Byte 2 of the given product ID
|
| 418 |
|
|
* @param productId3 Byte 3 of the given product ID
|
| 419 |
|
|
* @throws InvalidFirmwareException if the given product ID is not compatible or interface 1 is not supported.
|
| 420 |
|
|
*/
|
| 421 |
|
|
public void checkCompatible ( int productId0, int productId1, int productId2, int productId3 ) throws InvalidFirmwareException {
|
| 422 |
|
|
checkValid();
|
| 423 |
|
|
if ( ! dev().compatible ( productId0, productId1, productId2, productId3 ) )
|
| 424 |
|
|
throw new InvalidFirmwareException(this, "Incompatible Product ID");
|
| 425 |
|
|
}
|
| 426 |
|
|
|
| 427 |
|
|
// ******* getFpgaState ********************************************************
|
| 428 |
|
|
private void getFpgaState () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 429 |
|
|
byte[] buffer = new byte[9];
|
| 430 |
3 |
ZTEX |
checkCapability(CAPABILITY_FPGA);
|
| 431 |
2 |
ZTEX |
vendorRequest2(0x30, "getFpgaState", buffer, 9);
|
| 432 |
|
|
fpgaConfigured = buffer[0] == 0;
|
| 433 |
|
|
fpgaChecksum = buffer[1] & 0xff;
|
| 434 |
|
|
fpgaBytes = ((buffer[5] & 0xff)<<24) | ((buffer[4] & 0xff)<<16) | ((buffer[3] & 0xff)<<8) | (buffer[2] & 0xff);
|
| 435 |
|
|
fpgaInitB = buffer[6] & 0xff;
|
| 436 |
|
|
fpgaFlashResult = buffer[7];
|
| 437 |
|
|
fpgaFlashBitSwap = buffer[8] != 0;
|
| 438 |
|
|
}
|
| 439 |
|
|
|
| 440 |
|
|
// ******* printFpgaState ******************************************************
|
| 441 |
|
|
/**
|
| 442 |
|
|
* Prints out the FPGA state.
|
| 443 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 444 |
|
|
* @throws UsbException if a communication error occurs.
|
| 445 |
|
|
* @throws CapabilityException if FPGA configuration is not supported by the firmware.
|
| 446 |
|
|
*/
|
| 447 |
|
|
public void printFpgaState () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 448 |
|
|
getFpgaState();
|
| 449 |
|
|
System.out.println( "size=" + fpgaBytes + "; checksum=" + fpgaChecksum + "; INIT_B_HIST=" + fpgaInitB +" (should be 222); flash_configuration_result=" + fpgaFlashResult );
|
| 450 |
|
|
}
|
| 451 |
|
|
|
| 452 |
|
|
// ******* getFpgaConfiguration ************************************************
|
| 453 |
|
|
/**
|
| 454 |
|
|
* Returns true if the FPGA is configured.
|
| 455 |
|
|
* @return true if the FPGA is configured.
|
| 456 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 457 |
|
|
* @throws UsbException if a communication error occurs.
|
| 458 |
|
|
* @throws CapabilityException if FPGA configuration is not supported by the firmware.
|
| 459 |
|
|
*/
|
| 460 |
|
|
public boolean getFpgaConfiguration () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 461 |
|
|
getFpgaState ();
|
| 462 |
|
|
return fpgaConfigured;
|
| 463 |
|
|
}
|
| 464 |
|
|
|
| 465 |
|
|
// ******* getFpgaConfigurationStr *********************************************
|
| 466 |
|
|
/**
|
| 467 |
|
|
* Returns a string that indicates the FPGA configuration status.
|
| 468 |
|
|
* @return a string that indicates the FPGA configuration status.
|
| 469 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 470 |
|
|
* @throws UsbException if a communication error occurs.
|
| 471 |
|
|
* @throws CapabilityException if FPGA configuration is not supported by the firmware.
|
| 472 |
|
|
*/
|
| 473 |
|
|
public String getFpgaConfigurationStr () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 474 |
|
|
getFpgaState ();
|
| 475 |
|
|
return fpgaConfigured ? "FPGA configured" : "FPGA unconfigured";
|
| 476 |
|
|
}
|
| 477 |
|
|
|
| 478 |
|
|
// ******* resetFGPA ***********************************************************
|
| 479 |
|
|
/**
|
| 480 |
|
|
* Resets the FPGA.
|
| 481 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 482 |
|
|
* @throws UsbException if a communication error occurs.
|
| 483 |
|
|
* @throws CapabilityException if FPGA configuration is not supported by the firmware.
|
| 484 |
|
|
*/
|
| 485 |
|
|
public void resetFpga () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 486 |
3 |
ZTEX |
checkCapability(CAPABILITY_FPGA);
|
| 487 |
2 |
ZTEX |
vendorCommand(0x31, "resetFpga" );
|
| 488 |
|
|
}
|
| 489 |
|
|
|
| 490 |
3 |
ZTEX |
|
| 491 |
|
|
// ******* detectBitstreamBitOrder *********************************************
|
| 492 |
|
|
private int detectBitstreamBitOrder ( byte[] buf ) {
|
| 493 |
|
|
for ( int i=0; i<buf.length-3; i++ ) {
|
| 494 |
|
|
if ( ((buf[i] & 255)==0xaa) && ((buf[i+1] & 255)==0x99) && ((buf[i+2] & 255)==0x55) && ((buf[i+3] & 255)==0x66) )
|
| 495 |
|
|
return 1;
|
| 496 |
|
|
if ( ((buf[i] & 255)==0x55) && ((buf[i+1] & 255)==0x99) && ((buf[i+2] & 255)==0xaa) && ((buf[i+3] & 255)==0x66) )
|
| 497 |
|
|
return 0;
|
| 498 |
|
|
}
|
| 499 |
|
|
System.err.println("Warning: Unable to determine bitstream bit order: no signature found");
|
| 500 |
|
|
return 0;
|
| 501 |
|
|
}
|
| 502 |
|
|
|
| 503 |
|
|
// ******* swapBits ************************************************************
|
| 504 |
|
|
private void swapBits ( byte[][] buf, int size ) {
|
| 505 |
|
|
int j=0, k=0;
|
| 506 |
|
|
for (int i=0; i<size; i++ ) {
|
| 507 |
|
|
while ( k >= buf[j].length ) {
|
| 508 |
|
|
j++;
|
| 509 |
|
|
k=0;
|
| 510 |
|
|
}
|
| 511 |
|
|
byte b = buf[j][k];
|
| 512 |
|
|
buf[j][k] = (byte) ( ((b & 128) >> 7) |
|
| 513 |
|
|
((b & 64) >> 5) |
|
| 514 |
|
|
((b & 32) >> 3) |
|
| 515 |
|
|
((b & 16) >> 1) |
|
| 516 |
|
|
((b & 8) << 1) |
|
| 517 |
|
|
((b & 4) << 3) |
|
| 518 |
|
|
((b & 2) << 5) |
|
| 519 |
|
|
((b & 1) << 7) );
|
| 520 |
|
|
k++;
|
| 521 |
|
|
}
|
| 522 |
|
|
}
|
| 523 |
|
|
|
| 524 |
2 |
ZTEX |
// ******* configureFpga *******************************************************
|
| 525 |
|
|
// returns configuration time in ms
|
| 526 |
|
|
/**
|
| 527 |
|
|
* Upload a Bitstream to the FPGA.
|
| 528 |
|
|
* @param fwFileName The file name of the Bitstream. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
| 529 |
|
|
* @param force If set to true existing configurations will be overwritten. (By default an {@link AlreadyConfiguredException} is thrown).
|
| 530 |
3 |
ZTEX |
* @param bs 0: disable bit swapping, 1: enable bit swapping, all other values: automatic detection of bit order.
|
| 531 |
2 |
ZTEX |
* @throws BitstreamReadException if an error occurred while attempting to read the Bitstream.
|
| 532 |
|
|
* @throws BitstreamUploadException if an error occurred while attempting to upload the Bitstream.
|
| 533 |
|
|
* @throws AlreadyConfiguredException if the FPGA is already configured.
|
| 534 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 535 |
|
|
* @throws UsbException if a communication error occurs.
|
| 536 |
|
|
* @throws CapabilityException if FPGA configuration is not supported by the firmware.
|
| 537 |
|
|
*/
|
| 538 |
3 |
ZTEX |
public long configureFpga ( String fwFileName, boolean force, int bs ) throws BitstreamReadException, UsbException, BitstreamUploadException, AlreadyConfiguredException, InvalidFirmwareException, CapabilityException {
|
| 539 |
2 |
ZTEX |
final int transactionBytes = certainWorkarounds ? 256 : 2048;
|
| 540 |
|
|
long t0 = 0;
|
| 541 |
|
|
|
| 542 |
3 |
ZTEX |
checkCapability(CAPABILITY_FPGA);
|
| 543 |
2 |
ZTEX |
|
| 544 |
|
|
if ( !force && getFpgaConfiguration() )
|
| 545 |
|
|
throw new AlreadyConfiguredException();
|
| 546 |
|
|
|
| 547 |
|
|
// read the Bitstream file
|
| 548 |
3 |
ZTEX |
byte[][] buffer = new byte[16*1024*1024/transactionBytes][];
|
| 549 |
2 |
ZTEX |
int size = 0;
|
| 550 |
|
|
try {
|
| 551 |
|
|
InputStream inputStream = JInputStream.getInputStream( fwFileName );
|
| 552 |
|
|
int j = transactionBytes;
|
| 553 |
|
|
for ( int i=0; i<buffer.length && j==transactionBytes; i++ ) {
|
| 554 |
|
|
buffer[i] = new byte[transactionBytes];
|
| 555 |
|
|
j = inputStream.read( buffer[i] );
|
| 556 |
|
|
if ( j < 0 )
|
| 557 |
|
|
j = 0;
|
| 558 |
|
|
if ( j < transactionBytes && j % 64 == 0 ) // ensures size % 64 != 0
|
| 559 |
|
|
j+=1;
|
| 560 |
|
|
size += j;
|
| 561 |
|
|
}
|
| 562 |
|
|
|
| 563 |
|
|
try {
|
| 564 |
|
|
inputStream.close();
|
| 565 |
|
|
}
|
| 566 |
|
|
catch ( Exception e ) {
|
| 567 |
|
|
System.err.println( "Warning: Error closing file " + fwFileName + ": " + e.getLocalizedMessage() );
|
| 568 |
|
|
}
|
| 569 |
|
|
}
|
| 570 |
|
|
catch (IOException e) {
|
| 571 |
|
|
throw new BitstreamReadException(e.getLocalizedMessage());
|
| 572 |
|
|
}
|
| 573 |
|
|
if ( size < 64 || size % 64 == 0 )
|
| 574 |
|
|
throw new BitstreamReadException("Invalid file size: " + size );
|
| 575 |
3 |
ZTEX |
|
| 576 |
|
|
// detect bitstream bit order and swap bits if necessary
|
| 577 |
|
|
if ( bs<0 || bs>1 )
|
| 578 |
|
|
bs = detectBitstreamBitOrder ( buffer[0] );
|
| 579 |
|
|
if ( bs == 1 )
|
| 580 |
|
|
swapBits(buffer,size);
|
| 581 |
2 |
ZTEX |
|
| 582 |
|
|
// upload the Bitstream file
|
| 583 |
|
|
for ( int tries=10; tries>0; tries-- ) {
|
| 584 |
|
|
|
| 585 |
|
|
resetFpga();
|
| 586 |
|
|
|
| 587 |
|
|
try {
|
| 588 |
|
|
t0 = -new Date().getTime();
|
| 589 |
|
|
int cs = 0;
|
| 590 |
3 |
ZTEX |
bs = 0;
|
| 591 |
2 |
ZTEX |
|
| 592 |
|
|
for ( int i=0; i<buffer.length && i*transactionBytes < size; i++ ) {
|
| 593 |
|
|
int j = size-i*transactionBytes;
|
| 594 |
|
|
if (j>transactionBytes)
|
| 595 |
|
|
j = transactionBytes;
|
| 596 |
|
|
vendorCommand2(0x32, "sendFpgaData", 0,0, buffer[i], j);
|
| 597 |
|
|
|
| 598 |
|
|
bs+=j;
|
| 599 |
|
|
for ( int k=0; k<buffer[i].length; k++ )
|
| 600 |
|
|
cs = ( cs + (buffer[i][k] & 0xff) ) & 0xff;
|
| 601 |
|
|
}
|
| 602 |
|
|
|
| 603 |
|
|
getFpgaState();
|
| 604 |
|
|
// System.err.println("fpgaConfigred=" + fpgaConfigured + " fpgaBytes="+fpgaBytes + " ("+bs+") fpgaChecksum="+fpgaChecksum + " ("+cs+") fpgaInitB="+fpgaInitB );
|
| 605 |
|
|
if ( ! fpgaConfigured ) {
|
| 606 |
|
|
throw new BitstreamUploadException( "FPGA configuration failed: DONE pin does not go high (size=" + fpgaBytes + " , " + (bs - fpgaBytes) + " bytes went lost; checksum="
|
| 607 |
|
|
+ fpgaChecksum + " , should be " + cs + "; INIT_B_HIST=" + fpgaInitB +", should be 222)" );
|
| 608 |
|
|
}
|
| 609 |
|
|
// System.out.println( "FPGA configuration: size=" + fpgaBytes + " , " + (bs - fpgaBytes) + " bytes went lost; checksum=" + fpgaChecksum + " , should be " + cs + "; INIT_B_HIST=" + fpgaInitB +", should be 222" );
|
| 610 |
|
|
// if ( fpgaInitB != 222 )
|
| 611 |
|
|
// System.err.println ( "Warning: FPGA configuration may have failed: DONE pin has gone high but INIT_B states are wrong: " + fpgaInitB +", should be 222");
|
| 612 |
|
|
|
| 613 |
|
|
tries = 0;
|
| 614 |
|
|
t0 += new Date().getTime();
|
| 615 |
|
|
|
| 616 |
|
|
}
|
| 617 |
|
|
catch ( BitstreamUploadException e ) {
|
| 618 |
|
|
if ( tries>1 )
|
| 619 |
|
|
System.err.println("Warning: " + e.getLocalizedMessage() +": Retrying it ...");
|
| 620 |
|
|
else
|
| 621 |
|
|
throw e;
|
| 622 |
|
|
}
|
| 623 |
|
|
}
|
| 624 |
|
|
|
| 625 |
|
|
try {
|
| 626 |
|
|
Thread.sleep( 200 );
|
| 627 |
|
|
}
|
| 628 |
|
|
catch ( InterruptedException e) {
|
| 629 |
|
|
}
|
| 630 |
|
|
|
| 631 |
|
|
return t0;
|
| 632 |
|
|
}
|
| 633 |
|
|
|
| 634 |
3 |
ZTEX |
/**
|
| 635 |
|
|
* Upload a Bitstream to the FPGA.
|
| 636 |
|
|
* @param fwFileName The file name of the Bitstream. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
| 637 |
|
|
* @param force If set to true existing configurations will be overwritten. (By default an {@link AlreadyConfiguredException} is thrown).
|
| 638 |
|
|
* @throws BitstreamReadException if an error occurred while attempting to read the Bitstream.
|
| 639 |
|
|
* @throws BitstreamUploadException if an error occurred while attempting to upload the Bitstream.
|
| 640 |
|
|
* @throws AlreadyConfiguredException if the FPGA is already configured.
|
| 641 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 642 |
|
|
* @throws UsbException if a communication error occurs.
|
| 643 |
|
|
* @throws CapabilityException if FPGA configuration is not supported by the firmware.
|
| 644 |
|
|
*/
|
| 645 |
|
|
public long configureFpga ( String fwFileName, boolean force ) throws BitstreamReadException, UsbException, BitstreamUploadException, AlreadyConfiguredException, InvalidFirmwareException, CapabilityException {
|
| 646 |
|
|
return configureFpga(fwFileName, force, -1);
|
| 647 |
|
|
}
|
| 648 |
|
|
|
| 649 |
2 |
ZTEX |
// ******* eepromState *********************************************************
|
| 650 |
|
|
// returns true if EEPROM is ready
|
| 651 |
|
|
/**
|
| 652 |
|
|
* Reads the current EEPROM status.
|
| 653 |
|
|
* This method also sets the varibles {@link #eepromBytes} and {@link #eepromChecksum}.
|
| 654 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 655 |
|
|
* @throws UsbException if a communication error occurs.
|
| 656 |
|
|
* @throws CapabilityException if EEPROM access is not supported by the firmware.
|
| 657 |
|
|
* @return true if EEPROM is ready.
|
| 658 |
|
|
*/
|
| 659 |
|
|
public boolean eepromState ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 660 |
|
|
byte[] buf = new byte[4];
|
| 661 |
3 |
ZTEX |
checkCapability(CAPABILITY_EEPROM);
|
| 662 |
2 |
ZTEX |
vendorRequest2(0x3A, "EEPROM State", 0, 0, buf, 4);
|
| 663 |
|
|
eepromBytes = (buf[0] & 255) | (buf[1] & 255)<<8;
|
| 664 |
|
|
eepromChecksum = buf[2] & 255;
|
| 665 |
|
|
return buf[3] == 0;
|
| 666 |
|
|
}
|
| 667 |
|
|
|
| 668 |
|
|
// ******* eepromWrite *********************************************************
|
| 669 |
|
|
/**
|
| 670 |
|
|
* Writes data to the EEPROM.
|
| 671 |
|
|
* @param addr The destination address of the EEPROM.
|
| 672 |
|
|
* @param buf The data.
|
| 673 |
|
|
* @param length The amount of bytes to be sent.
|
| 674 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 675 |
|
|
* @throws UsbException if a communication error occurs.
|
| 676 |
|
|
* @throws CapabilityException if EEPROM access is not supported by the firmware.
|
| 677 |
|
|
*/
|
| 678 |
|
|
public void eepromWrite ( int addr, byte[] buf, int length ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 679 |
3 |
ZTEX |
checkCapability(CAPABILITY_EEPROM);
|
| 680 |
2 |
ZTEX |
vendorCommand2( 0x39, "EEPROM Write", addr, 0, buf, length );
|
| 681 |
|
|
try {
|
| 682 |
|
|
Thread.sleep( 10 );
|
| 683 |
|
|
}
|
| 684 |
|
|
catch ( InterruptedException e) {
|
| 685 |
|
|
}
|
| 686 |
|
|
}
|
| 687 |
|
|
|
| 688 |
|
|
// ******* eepromRead **********************************************************
|
| 689 |
|
|
/**
|
| 690 |
|
|
* Reads data from the EEPROM.
|
| 691 |
|
|
* @param addr The source address of the EEPROM.
|
| 692 |
|
|
* @param buf A buffer for the storage of the data.
|
| 693 |
|
|
* @param length The amount of bytes to be read.
|
| 694 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 695 |
|
|
* @throws UsbException if a communication error occurs.
|
| 696 |
|
|
* @throws CapabilityException if EEPROM access is not supported by the firmware.
|
| 697 |
|
|
*/
|
| 698 |
|
|
public void eepromRead ( int addr, byte[] buf, int length ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 699 |
3 |
ZTEX |
checkCapability(CAPABILITY_EEPROM);
|
| 700 |
2 |
ZTEX |
vendorRequest2( 0x38, "EEPROM Read", addr, 0, buf, length ); // sometimes a little bit slow
|
| 701 |
|
|
try {
|
| 702 |
|
|
Thread.sleep( 10 );
|
| 703 |
|
|
}
|
| 704 |
|
|
catch ( InterruptedException e) {
|
| 705 |
|
|
}
|
| 706 |
|
|
}
|
| 707 |
|
|
|
| 708 |
|
|
// ******* eepromUpload ********************************************************
|
| 709 |
|
|
// returns upload time in ms
|
| 710 |
|
|
/**
|
| 711 |
|
|
* Upload the firmware to the EEPROM.
|
| 712 |
|
|
* In order to start the uploaded firmware the device must be reset.
|
| 713 |
|
|
* @param ihxFileName The file name of the firmware image in ihx format. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
| 714 |
|
|
* @param force Skips the compatibility check if true.
|
| 715 |
|
|
* @throws IncompatibleFirmwareException if the given firmware is not compatible to the installed one, see {@link #compatible(int,int,int,int)} (Upload can be enforced using the <tt>force</tt> parameter.)
|
| 716 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 717 |
|
|
* @throws CapabilityException if EEPROM access is not supported by the firmware.
|
| 718 |
|
|
* @throws FirmwareUploadException if an error occurred while attempting to upload the firmware.
|
| 719 |
|
|
*/
|
| 720 |
|
|
public long eepromUpload ( String ihxFileName, boolean force ) throws IncompatibleFirmwareException, FirmwareUploadException, InvalidFirmwareException, CapabilityException {
|
| 721 |
|
|
final int pagesMax = 256;
|
| 722 |
|
|
final int pageSize = 256;
|
| 723 |
|
|
int pages = 0;
|
| 724 |
|
|
byte[][] buffer = new byte[pagesMax][];
|
| 725 |
|
|
|
| 726 |
3 |
ZTEX |
checkCapability(CAPABILITY_EEPROM);
|
| 727 |
2 |
ZTEX |
|
| 728 |
|
|
// load the ihx file
|
| 729 |
|
|
ZtexIhxFile1 ihxFile;
|
| 730 |
|
|
try {
|
| 731 |
|
|
ihxFile = new ZtexIhxFile1( ihxFileName );
|
| 732 |
|
|
}
|
| 733 |
|
|
catch ( IOException e ) {
|
| 734 |
|
|
throw new FirmwareUploadException( e.getLocalizedMessage() );
|
| 735 |
|
|
}
|
| 736 |
|
|
catch ( IhxFileDamagedException e ) {
|
| 737 |
|
|
throw new FirmwareUploadException( e.getLocalizedMessage() );
|
| 738 |
|
|
}
|
| 739 |
|
|
// ihxFile.dataInfo(System.out);
|
| 740 |
|
|
// System.out.println(ihxFile);
|
| 741 |
|
|
|
| 742 |
|
|
// check for compatibility
|
| 743 |
|
|
if ( ! force && dev().valid() ) {
|
| 744 |
|
|
if ( ihxFile.interfaceVersion() != 1 )
|
| 745 |
|
|
throw new IncompatibleFirmwareException("Wrong interface version: Expected 1, got " + ihxFile.interfaceVersion() );
|
| 746 |
|
|
|
| 747 |
|
|
if ( ! dev().compatible ( ihxFile.productId(0), ihxFile.productId(1), ihxFile.productId(2), ihxFile.productId(3) ) )
|
| 748 |
|
|
throw new IncompatibleFirmwareException("Incompatible productId's: Current firmware: " + ZtexDevice1.byteArrayString(dev().productId())
|
| 749 |
|
|
+ " Ihx File: " + ZtexDevice1.byteArrayString(ihxFile.productId()) );
|
| 750 |
|
|
}
|
| 751 |
|
|
|
| 752 |
|
|
Usb_Device_Descriptor dd = dev().dev().getDescriptor();
|
| 753 |
|
|
int vid = dd.getIdVendor() & 65535;
|
| 754 |
|
|
int pid = dd.getIdProduct() & 65535;
|
| 755 |
|
|
|
| 756 |
|
|
buffer[0] = new byte[pageSize];
|
| 757 |
|
|
buffer[0][0] = (byte) 0xc2;
|
| 758 |
|
|
buffer[0][1] = (byte) (vid & 255);
|
| 759 |
|
|
buffer[0][2] = (byte) ((vid >> 8) & 255);
|
| 760 |
|
|
buffer[0][3] = (byte) (pid & 255);
|
| 761 |
|
|
buffer[0][4] = (byte) ((pid >> 8) & 255);
|
| 762 |
|
|
buffer[0][5] = 0;
|
| 763 |
|
|
buffer[0][6] = 0;
|
| 764 |
|
|
buffer[0][7] = 0;
|
| 765 |
|
|
|
| 766 |
|
|
int ptr = 8, i = 0;
|
| 767 |
|
|
|
| 768 |
|
|
while ( i < ihxFile.ihxData.length ) {
|
| 769 |
|
|
if ( ihxFile.ihxData[i]>=0 && ihxFile.ihxData[i]<256 ) { // new data block
|
| 770 |
|
|
int j = 1;
|
| 771 |
|
|
while ( i+j<ihxFile.ihxData.length && ihxFile.ihxData[i+j]>=0 && ihxFile.ihxData[i+j]<255 )
|
| 772 |
|
|
j++;
|
| 773 |
|
|
|
| 774 |
|
|
for (int k=ptr/pageSize + 1; k < (ptr+j+4)/pageSize + 1; k++ ) // also considers 5 bytes for the last data block
|
| 775 |
|
|
buffer[k] = new byte[pageSize];
|
| 776 |
|
|
|
| 777 |
|
|
buffer[(ptr+0)/pageSize][(ptr+0) % pageSize] = (byte) ((j >> 8) & 255);
|
| 778 |
|
|
buffer[(ptr+1)/pageSize][(ptr+1) % pageSize] = (byte) (j & 255); // length
|
| 779 |
|
|
buffer[(ptr+2)/pageSize][(ptr+2) % pageSize] = (byte) ((i >> 8) & 255);
|
| 780 |
|
|
buffer[(ptr+3)/pageSize][(ptr+3) % pageSize] = (byte) (i & 255); // address
|
| 781 |
|
|
ptr+=4;
|
| 782 |
|
|
for ( int k=0; k<j; k++ ) // data
|
| 783 |
|
|
buffer[(ptr+k)/pageSize][(ptr+k) % pageSize] = (byte) ihxFile.ihxData[i+k];
|
| 784 |
|
|
ptr+=j;
|
| 785 |
|
|
i+=j;
|
| 786 |
|
|
}
|
| 787 |
|
|
else {
|
| 788 |
|
|
i+=1;
|
| 789 |
|
|
}
|
| 790 |
|
|
}
|
| 791 |
|
|
|
| 792 |
|
|
buffer[(ptr+0)/pageSize][(ptr+0) % pageSize] = (byte) 0x80; // last data block
|
| 793 |
|
|
buffer[(ptr+1)/pageSize][(ptr+1) % pageSize] = (byte) 0x01;
|
| 794 |
|
|
buffer[(ptr+2)/pageSize][(ptr+2) % pageSize] = (byte) 0xe6;
|
| 795 |
|
|
buffer[(ptr+3)/pageSize][(ptr+3) % pageSize] = (byte) 0x00;
|
| 796 |
|
|
buffer[(ptr+3)/pageSize][(ptr+4) % pageSize] = (byte) 0x00;
|
| 797 |
|
|
ptr+=5;
|
| 798 |
|
|
|
| 799 |
|
|
|
| 800 |
|
|
long t0 = new Date().getTime();
|
| 801 |
|
|
byte[] rbuf = new byte[pageSize];
|
| 802 |
|
|
|
| 803 |
|
|
for ( i=(ptr-1)/pageSize; i>=0; i-- ) {
|
| 804 |
|
|
|
| 805 |
|
|
int k = (i+1)*pageSize < ptr ? pageSize : ptr-i*pageSize;
|
| 806 |
|
|
int cs = 0;
|
| 807 |
|
|
for (int j=0; j<k; j++ ) {
|
| 808 |
|
|
cs = ( cs + (buffer[i][j] & 255) ) & 255;
|
| 809 |
|
|
}
|
| 810 |
|
|
|
| 811 |
|
|
for ( int tries=3; tries>0; tries-- ) {
|
| 812 |
|
|
try {
|
| 813 |
|
|
eepromWrite(i*pageSize, buffer[i], k);
|
| 814 |
|
|
eepromState();
|
| 815 |
|
|
if ( eepromBytes!=k )
|
| 816 |
|
|
throw new FirmwareUploadException("Error writing data to EEPROM: Wrote " + eepromBytes + " bytes instead of " + k + " bytes" );
|
| 817 |
|
|
if ( eepromChecksum!=cs )
|
| 818 |
|
|
throw new FirmwareUploadException("Error writing data to EEPROM: Checksum error");
|
| 819 |
|
|
|
| 820 |
|
|
eepromRead(i*pageSize, rbuf, k);
|
| 821 |
|
|
for (int j=0; j<k; j++ ) {
|
| 822 |
|
|
if ( rbuf[j] != buffer[i][j] )
|
| 823 |
|
|
throw new FirmwareUploadException("Error writing data to EEPROM: Verification failed");
|
| 824 |
|
|
}
|
| 825 |
|
|
tries = 0;
|
| 826 |
|
|
}
|
| 827 |
|
|
catch ( Exception e ) {
|
| 828 |
|
|
if ( tries > 1 ) {
|
| 829 |
|
|
System.err.println("Warning: " + e.getLocalizedMessage() +": Retrying it ...");
|
| 830 |
|
|
}
|
| 831 |
|
|
else {
|
| 832 |
|
|
throw new FirmwareUploadException(e.getLocalizedMessage());
|
| 833 |
|
|
}
|
| 834 |
|
|
}
|
| 835 |
|
|
}
|
| 836 |
|
|
}
|
| 837 |
|
|
|
| 838 |
|
|
return new Date().getTime() - t0;
|
| 839 |
|
|
}
|
| 840 |
|
|
|
| 841 |
|
|
|
| 842 |
|
|
// ******* eepromDisable ********************************************************
|
| 843 |
|
|
/**
|
| 844 |
|
|
* Disables the firmware stored in the EEPROM.
|
| 845 |
|
|
* This is achived by writing a "0" to the address 0 of the EEPROM.
|
| 846 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 847 |
|
|
* @throws CapabilityException if EEPROM access is not supported by the firmware.
|
| 848 |
|
|
* @throws FirmwareUploadException if an error occurred while attempting to disable the firmware.
|
| 849 |
|
|
*/
|
| 850 |
|
|
public void eepromDisable ( ) throws FirmwareUploadException, InvalidFirmwareException, CapabilityException {
|
| 851 |
|
|
byte[] buf = { 0 };
|
| 852 |
|
|
|
| 853 |
|
|
for ( int tries=3; tries>0; tries-- ) {
|
| 854 |
|
|
try {
|
| 855 |
|
|
eepromWrite(0, buf, 1);
|
| 856 |
|
|
|
| 857 |
|
|
eepromRead(0, buf, 1);
|
| 858 |
|
|
if ( buf[0] != 0 )
|
| 859 |
|
|
throw new FirmwareUploadException("Error disabeling EEPROM firmware: Verification failed");
|
| 860 |
|
|
tries = 0;
|
| 861 |
|
|
|
| 862 |
|
|
}
|
| 863 |
|
|
catch ( Exception e ) {
|
| 864 |
|
|
if ( tries > 1 ) {
|
| 865 |
|
|
System.err.println("Warning: " + e.getLocalizedMessage() +": Retrying it ...");
|
| 866 |
|
|
}
|
| 867 |
|
|
else {
|
| 868 |
|
|
throw new FirmwareUploadException(e.getLocalizedMessage());
|
| 869 |
|
|
}
|
| 870 |
|
|
}
|
| 871 |
|
|
}
|
| 872 |
|
|
}
|
| 873 |
|
|
|
| 874 |
|
|
// ******* flashStrError *******************************************************
|
| 875 |
|
|
/**
|
| 876 |
|
|
* Converts a given error code into a String.
|
| 877 |
|
|
* @param errNum The error code.
|
| 878 |
|
|
* @return an error message.
|
| 879 |
|
|
*/
|
| 880 |
|
|
public static String flashStrError ( int errNum ) {
|
| 881 |
|
|
switch ( errNum ) {
|
| 882 |
|
|
case FLASH_EC_NO_ERROR:
|
| 883 |
|
|
return "USB error: " + LibusbJava.usb_strerror();
|
| 884 |
|
|
case FLASH_EC_CMD_ERROR:
|
| 885 |
|
|
return "Command error";
|
| 886 |
|
|
case FLASH_EC_TIMEOUT:
|
| 887 |
|
|
return "Timeout error";
|
| 888 |
|
|
case FLASH_EC_BUSY:
|
| 889 |
|
|
return "Busy";
|
| 890 |
|
|
case FLASH_EC_PENDING:
|
| 891 |
|
|
return "Another operation is pending";
|
| 892 |
|
|
case FLASH_EC_READ_ERROR:
|
| 893 |
|
|
return "Read error";
|
| 894 |
|
|
case FLASH_EC_WRITE_ERROR:
|
| 895 |
|
|
return "Write error";
|
| 896 |
|
|
case FLASH_EC_NOTSUPPORTED:
|
| 897 |
|
|
return "Not supported";
|
| 898 |
|
|
}
|
| 899 |
|
|
return "Error " + errNum;
|
| 900 |
|
|
}
|
| 901 |
|
|
|
| 902 |
|
|
/**
|
| 903 |
|
|
* Gets the last Flash error from the device.
|
| 904 |
|
|
* @return an error message.
|
| 905 |
|
|
*/
|
| 906 |
|
|
public String flashStrError ( ) {
|
| 907 |
|
|
try {
|
| 908 |
|
|
return flashStrError( getFlashEC() );
|
| 909 |
|
|
}
|
| 910 |
|
|
catch ( Exception e ) {
|
| 911 |
|
|
return "Unknown error (Error receiving errorcode: "+e.getLocalizedMessage() +")";
|
| 912 |
|
|
}
|
| 913 |
|
|
}
|
| 914 |
|
|
|
| 915 |
|
|
// ******* flashState **********************************************************
|
| 916 |
|
|
/**
|
| 917 |
|
|
* Reads the the Flash memory status and information.
|
| 918 |
|
|
* This method also sets the variables {@link #flashEnabled}, {@link #flashSectorSize} and {@link #flashSectors}.
|
| 919 |
|
|
* @return true if Flash memory is installed.
|
| 920 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 921 |
|
|
* @throws UsbException if a communication error occurs.
|
| 922 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 923 |
|
|
*/
|
| 924 |
|
|
public boolean flashState () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 925 |
|
|
byte[] buf = new byte[8];
|
| 926 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 927 |
2 |
ZTEX |
|
| 928 |
|
|
// device may be busy due to initialization, we try it up to up to 4s
|
| 929 |
|
|
vendorRequest2(0x40, "Flash State", 0, 0, buf, 8);
|
| 930 |
|
|
flashEC = buf[7] & 255;
|
| 931 |
|
|
int tries=20;
|
| 932 |
|
|
while ( flashEC==FLASH_EC_BUSY && tries>0 )
|
| 933 |
|
|
{
|
| 934 |
|
|
try {
|
| 935 |
|
|
Thread.sleep( 200 );
|
| 936 |
|
|
}
|
| 937 |
|
|
catch ( InterruptedException e) {
|
| 938 |
|
|
}
|
| 939 |
|
|
tries-=1;
|
| 940 |
|
|
vendorRequest2(0x40, "Flash State", 0, 0, buf, 8);
|
| 941 |
|
|
flashEC = buf[7] & 255;
|
| 942 |
|
|
}
|
| 943 |
|
|
flashEnabled = buf[0] & 255;
|
| 944 |
|
|
flashSectorSize = flashEnabled == 1 ? ((buf[2] & 255) << 8) | (buf[1] & 255) : 0;
|
| 945 |
|
|
flashSectors = flashEnabled == 1 ? ((buf[6] & 255) << 24) | ((buf[5] & 255) << 16) | ((buf[4] & 255) << 8) | (buf[3] & 255) : 0;
|
| 946 |
|
|
return flashEnabled == 1;
|
| 947 |
|
|
}
|
| 948 |
|
|
|
| 949 |
|
|
// ******* getFlashEC **********************************************************
|
| 950 |
|
|
// reads the current error code
|
| 951 |
|
|
/**
|
| 952 |
|
|
* Gets the last Flash error from the device.
|
| 953 |
3 |
ZTEX |
* @return The last error code.
|
| 954 |
2 |
ZTEX |
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 955 |
|
|
* @throws UsbException if a communication error occurs.
|
| 956 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 957 |
|
|
*/
|
| 958 |
|
|
public int getFlashEC () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 959 |
|
|
byte[] buf = new byte[8];
|
| 960 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 961 |
2 |
ZTEX |
vendorRequest2(0x40, "Flash State", 0, 0, buf, 8);
|
| 962 |
|
|
flashEC = buf[7] & 255;
|
| 963 |
|
|
return flashEC;
|
| 964 |
|
|
}
|
| 965 |
|
|
|
| 966 |
|
|
// ******* flashReadSector ****************************************************
|
| 967 |
|
|
// read exactly one sector
|
| 968 |
|
|
/**
|
| 969 |
|
|
* Reads one sector from the Flash.
|
| 970 |
|
|
* @param sector The sector number to be read.
|
| 971 |
|
|
* @param buf A buffer for the storage of the data.
|
| 972 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 973 |
|
|
* @throws UsbException if a communication error occurs.
|
| 974 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 975 |
|
|
*/
|
| 976 |
|
|
public void flashReadSector ( int sector, byte[] buf ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 977 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 978 |
2 |
ZTEX |
if ( ! flashEnabled() )
|
| 979 |
|
|
throw new CapabilityException(this, "No Flash memory installed or");
|
| 980 |
|
|
|
| 981 |
|
|
try {
|
| 982 |
|
|
vendorRequest2( 0x41, "Flash Read", sector, sector >> 16, buf, flashSectorSize );
|
| 983 |
|
|
}
|
| 984 |
|
|
catch ( UsbException e ) {
|
| 985 |
|
|
throw new UsbException( dev().dev(), "Flash Read: " + flashStrError() );
|
| 986 |
|
|
}
|
| 987 |
|
|
}
|
| 988 |
|
|
|
| 989 |
|
|
// ******* flashWriteSector ***************************************************
|
| 990 |
|
|
// write exactly one sector
|
| 991 |
|
|
/**
|
| 992 |
|
|
* Writes one sector to the Flash.
|
| 993 |
|
|
* @param sector The sector number to be written.
|
| 994 |
|
|
* @param buf The data.
|
| 995 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 996 |
|
|
* @throws UsbException if a communication error occurs.
|
| 997 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 998 |
|
|
*/
|
| 999 |
|
|
public void flashWriteSector ( int sector, byte[] buf ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1000 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 1001 |
2 |
ZTEX |
if ( ! flashEnabled() )
|
| 1002 |
|
|
throw new CapabilityException(this, "No Flash memory installed or");
|
| 1003 |
|
|
|
| 1004 |
|
|
try {
|
| 1005 |
|
|
vendorCommand2( 0x42, "Flash Write", sector, sector >> 16, buf, flashSectorSize );
|
| 1006 |
|
|
}
|
| 1007 |
|
|
catch ( UsbException e ) {
|
| 1008 |
|
|
throw new UsbException( dev().dev(), "Flash Write: " + flashStrError() );
|
| 1009 |
|
|
}
|
| 1010 |
|
|
}
|
| 1011 |
|
|
|
| 1012 |
|
|
// ******* flashEnabled ********************************************************
|
| 1013 |
|
|
// returns enabled / disabled state
|
| 1014 |
|
|
/**
|
| 1015 |
|
|
* Returns true if Flash memory is installed.
|
| 1016 |
|
|
* @return true if Flash memory is installed.
|
| 1017 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1018 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1019 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 1020 |
|
|
*/
|
| 1021 |
|
|
public boolean flashEnabled () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1022 |
|
|
if ( flashEnabled < 0 ) // init variable
|
| 1023 |
|
|
flashState();
|
| 1024 |
|
|
return flashEnabled == 1;
|
| 1025 |
|
|
}
|
| 1026 |
|
|
|
| 1027 |
|
|
// ******* flashSectorSize *****************************************************
|
| 1028 |
|
|
// returns sector size of Flash memory, if available
|
| 1029 |
|
|
/**
|
| 1030 |
|
|
* Returns the sector size of the Flash memory or 0, if no flash memory is installed.
|
| 1031 |
|
|
* If required, the sector size is determined form the device first.
|
| 1032 |
|
|
* @return the sector size of the Flash memory.
|
| 1033 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1034 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1035 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 1036 |
|
|
*/
|
| 1037 |
|
|
public int flashSectorSize () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1038 |
|
|
if ( flashSectorSize < 0 ) // init variable
|
| 1039 |
|
|
flashState();
|
| 1040 |
|
|
return flashSectorSize;
|
| 1041 |
|
|
}
|
| 1042 |
|
|
|
| 1043 |
|
|
// ******* flashSectors ********************************************************
|
| 1044 |
|
|
// returns number of sectors of Flash memory, if available
|
| 1045 |
|
|
/**
|
| 1046 |
|
|
* Returns the number of sectors of the Flash memory or 0, if no Flash memory is installed.
|
| 1047 |
|
|
* If required, the number of sectors is determined form the device first.
|
| 1048 |
|
|
* @return the number of sectors of the Flash memory.
|
| 1049 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1050 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1051 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 1052 |
|
|
*/
|
| 1053 |
|
|
public int flashSectors () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1054 |
|
|
if ( flashSectors < 0 ) // init variable
|
| 1055 |
|
|
flashState();
|
| 1056 |
|
|
return flashSectors;
|
| 1057 |
|
|
}
|
| 1058 |
|
|
|
| 1059 |
|
|
// ******* flashSize ***********************************************************
|
| 1060 |
|
|
// returns size of Flash memory, if available
|
| 1061 |
|
|
/**
|
| 1062 |
|
|
* Returns the size of Flash memory or 0, if no Flash memory is installed.
|
| 1063 |
|
|
* If required, the Flash size is determined form the device first.
|
| 1064 |
|
|
* @return the size of Flash memory.
|
| 1065 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1066 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1067 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 1068 |
|
|
*/
|
| 1069 |
|
|
public long flashSize () throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1070 |
|
|
return flashSectorSize() * (long)flashSectors();
|
| 1071 |
|
|
}
|
| 1072 |
|
|
|
| 1073 |
|
|
// ******* printMmcState *******************************************************
|
| 1074 |
|
|
// returns true if Flash is available
|
| 1075 |
|
|
/**
|
| 1076 |
|
|
* Prints out some debug information about *SD/MMC Flash cards in SPI mode.<br>
|
| 1077 |
|
|
* <b>Only use this method if such kind of Flash is installed.</b>
|
| 1078 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1079 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1080 |
|
|
* @throws CapabilityException if Flash memory access is not supported by the firmware.
|
| 1081 |
|
|
*/
|
| 1082 |
|
|
public boolean printMmcState ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1083 |
|
|
byte[] buf = new byte[23];
|
| 1084 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 1085 |
2 |
ZTEX |
vendorRequest2(0x43, "MMC State", 0, 0, buf, 23);
|
| 1086 |
|
|
System.out.println("status=" + Integer.toBinaryString(256+(buf[0] & 255)).substring(1) + "." + Integer.toBinaryString(256+(buf[1] & 255)).substring(1) +
|
| 1087 |
|
|
" lastCmd=" + buf[3] +
|
| 1088 |
|
|
" lastCmdResponse=" + Integer.toBinaryString(256+(buf[4] & 255)).substring(1) +
|
| 1089 |
|
|
" ec=" + buf[2] +
|
| 1090 |
|
|
" BUSY=" + buf[22] +
|
| 1091 |
|
|
" SDHC=" + buf[5] +
|
| 1092 |
|
|
" buf=" + (buf[6] & 255)+" "+(buf[7] & 255)+" "+(buf[8] & 255)+" "+(buf[9] & 255)+" "+(buf[10] & 255)+" "+(buf[11] & 255)+" "+(buf[12] & 255)); // +" "+(buf[13] & 255)+" "+(buf[14] & 255)+" "+(buf[15] & 255)+" "+(buf[16] & 255)+" "+(buf[17] & 255));
|
| 1093 |
|
|
|
| 1094 |
|
|
return flashEnabled == 1;
|
| 1095 |
|
|
}
|
| 1096 |
|
|
|
| 1097 |
|
|
// ******* flashUploadBitstream ************************************************
|
| 1098 |
|
|
/*
|
| 1099 |
|
|
Returns configuration time in ms.
|
| 1100 |
|
|
The format of the boot sector (sector 0 of the Flash memory) is
|
| 1101 |
|
|
0..7
|
| 1102 |
|
|
8..9 Number of sectors, or 0 is disabled
|
| 1103 |
|
|
10..11 Number of bytes in the last sector, i.e. th total size of Bitstream is ((bs[8] | (bs[9]<<8) - 1) * flash_sector_size + ((bs[10] | (bs[11]<<8))
|
| 1104 |
|
|
*/
|
| 1105 |
|
|
/**
|
| 1106 |
|
|
* Uploads a Bitstream to the Flash.
|
| 1107 |
|
|
* This allows the firmware to load the Bitstream from Flash. Together with installation of the firmware in EEPROM
|
| 1108 |
|
|
* it is possible to construct fully autonomous devices.
|
| 1109 |
|
|
* <p>
|
| 1110 |
|
|
* Information about the bitstream is stored in sector 0.
|
| 1111 |
|
|
* This so called boot sector has the following format:
|
| 1112 |
|
|
* <table bgcolor="#404040" cellspacing=1 cellpadding=4>
|
| 1113 |
|
|
* <tr>
|
| 1114 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Bytes</b></td>
|
| 1115 |
|
|
* <td bgcolor="#d0d0d0" valign="bottom"><b>Description</b></td>
|
| 1116 |
|
|
* </tr>
|
| 1117 |
|
|
* <tr>
|
| 1118 |
|
|
* <td bgcolor="#ffffff" valign="top">0..7</td>
|
| 1119 |
|
|
* <td bgcolor="#ffffff" valign="top">ID, must be "ZTEXBS",1,1</td>
|
| 1120 |
|
|
* </tr>
|
| 1121 |
|
|
* <tr>
|
| 1122 |
|
|
* <td bgcolor="#ffffff" valign="top">8..9</td>
|
| 1123 |
|
|
* <td bgcolor="#ffffff" valign="top">The number of sectors used to store the Bitstream. 0 means no Bitstream.</td>
|
| 1124 |
|
|
* </tr>
|
| 1125 |
|
|
* <tr>
|
| 1126 |
|
|
* <td bgcolor="#ffffff" valign="top">10..11</td>
|
| 1127 |
|
|
* <td bgcolor="#ffffff" valign="top">The number of bytes in the last sector.</td>
|
| 1128 |
|
|
* </tr>
|
| 1129 |
|
|
* <tr>
|
| 1130 |
|
|
* <td bgcolor="#ffffff" valign="top">12..sectorSize-1</td>
|
| 1131 |
|
|
* <td bgcolor="#ffffff" valign="top">This data is reserved for future use and preserved by this method.</td>
|
| 1132 |
|
|
* </tr>
|
| 1133 |
|
|
* </table>
|
| 1134 |
|
|
* <p>
|
| 1135 |
|
|
* The total size of the Bitstream is computed as ((bs[8] | (bs[9]<<8) - 1) * flash_sector_size + ((bs[10] | (bs[11]<<8))
|
| 1136 |
|
|
* where bs[i] denotes byte i of the boot sector.
|
| 1137 |
|
|
* <p>
|
| 1138 |
|
|
* The first sector of the Bitstream is sector 1.
|
| 1139 |
|
|
* @param fwFileName The file name of the Bitstream. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
| 1140 |
3 |
ZTEX |
* @param bs 0: disable bit swapping, 1: enable bit swapping, all other values: automatic detection of bit order.
|
| 1141 |
2 |
ZTEX |
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1142 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1143 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1144 |
|
|
* @throws BitstreamReadException if an error occurred while attempting to read the Bitstream.
|
| 1145 |
|
|
*/
|
| 1146 |
3 |
ZTEX |
public long flashUploadBitstream ( String fwFileName, int bs ) throws BitstreamReadException, UsbException, InvalidFirmwareException, CapabilityException {
|
| 1147 |
|
|
checkCapability(CAPABILITY_FPGA);
|
| 1148 |
|
|
checkCapability(CAPABILITY_FLASH);
|
| 1149 |
2 |
ZTEX |
if ( ! flashEnabled() )
|
| 1150 |
|
|
throw new CapabilityException(this, "No Flash memory installed or");
|
| 1151 |
|
|
getFpgaState();
|
| 1152 |
|
|
|
| 1153 |
|
|
// read the Bitstream file
|
| 1154 |
3 |
ZTEX |
byte[][] buffer = new byte[32768][];
|
| 1155 |
2 |
ZTEX |
int i,j;
|
| 1156 |
|
|
try {
|
| 1157 |
|
|
InputStream inputStream = JInputStream.getInputStream( fwFileName );
|
| 1158 |
|
|
j = flashSectorSize;
|
| 1159 |
|
|
for ( i=0; i<buffer.length && j==flashSectorSize; i++ ) {
|
| 1160 |
|
|
buffer[i] = new byte[flashSectorSize];
|
| 1161 |
|
|
j = inputStream.read( buffer[i] );
|
| 1162 |
|
|
if ( j < 0 )
|
| 1163 |
|
|
j = 0;
|
| 1164 |
|
|
}
|
| 1165 |
|
|
|
| 1166 |
|
|
try {
|
| 1167 |
|
|
inputStream.close();
|
| 1168 |
|
|
}
|
| 1169 |
|
|
catch ( Exception e ) {
|
| 1170 |
|
|
System.err.println( "Warning: Error closing file " + fwFileName + ": " + e.getLocalizedMessage() );
|
| 1171 |
|
|
}
|
| 1172 |
|
|
}
|
| 1173 |
|
|
catch (IOException e) {
|
| 1174 |
|
|
throw new BitstreamReadException(e.getLocalizedMessage());
|
| 1175 |
|
|
}
|
| 1176 |
|
|
|
| 1177 |
3 |
ZTEX |
// detect bitstream bit order and swap bits if necessary
|
| 1178 |
|
|
if ( bs<0 || bs>1 )
|
| 1179 |
|
|
bs = detectBitstreamBitOrder(buffer[0]);
|
| 1180 |
|
|
if ( fpgaFlashBitSwap != (bs==1) )
|
| 1181 |
|
|
swapBits( buffer, flashSectorSize*i );
|
| 1182 |
|
|
|
| 1183 |
2 |
ZTEX |
// upload the Bitstream file
|
| 1184 |
|
|
byte[] sector = new byte[flashSectorSize];
|
| 1185 |
|
|
byte[] ID = new String("ZTEXBS").getBytes();
|
| 1186 |
|
|
|
| 1187 |
|
|
flashReadSector(0,sector); // read the boot sector (only the first 16 bytes are overwritten)
|
| 1188 |
|
|
for (int k=0; k<6; k++)
|
| 1189 |
|
|
sector[k]=ID[k];
|
| 1190 |
|
|
sector[6] = 1;
|
| 1191 |
|
|
sector[7] = 1;
|
| 1192 |
|
|
sector[8] = (byte) (i & 255);
|
| 1193 |
|
|
sector[9] = (byte) ((i>>8) & 255);
|
| 1194 |
|
|
sector[10] = (byte) (j & 255);
|
| 1195 |
|
|
sector[11] = (byte) ((j>>8) & 255);
|
| 1196 |
|
|
long t0 = new Date().getTime();
|
| 1197 |
|
|
flashWriteSector(0,sector); // write the boot sector
|
| 1198 |
|
|
for (int k=0; k<i; k++)
|
| 1199 |
|
|
flashWriteSector(k+1,buffer[k]); // write the Bitstream sectors
|
| 1200 |
|
|
|
| 1201 |
|
|
return new Date().getTime() - t0;
|
| 1202 |
|
|
}
|
| 1203 |
|
|
|
| 1204 |
3 |
ZTEX |
/**
|
| 1205 |
|
|
* Uploads a Bitstream to the Flash.
|
| 1206 |
|
|
* This allows the firmware to load the Bitstream from Flash. Together with installation of the firmware in EEPROM
|
| 1207 |
|
|
* it is possible to construct fully autonomous devices.
|
| 1208 |
|
|
* See {@link #flashUploadBitstream(String,int)} for further details.
|
| 1209 |
|
|
* @param fwFileName The file name of the Bitstream. The file can be a regular file or a system resource (e.g. a file from the current jar archive).
|
| 1210 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1211 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1212 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1213 |
|
|
* @throws BitstreamReadException if an error occurred while attempting to read the Bitstream.
|
| 1214 |
|
|
*/
|
| 1215 |
|
|
public long flashUploadBitstream ( String fwFileName ) throws BitstreamReadException, UsbException, InvalidFirmwareException, CapabilityException {
|
| 1216 |
|
|
return flashUploadBitstream(fwFileName, -1);
|
| 1217 |
|
|
}
|
| 1218 |
|
|
|
| 1219 |
2 |
ZTEX |
// ******* flashResetBitstream *************************************************
|
| 1220 |
|
|
// Clears a Bitstream from the Flash.
|
| 1221 |
|
|
/**
|
| 1222 |
|
|
* Clears a Bitstream from the Flash.
|
| 1223 |
|
|
* This is achieved by writing 0 to bytes 8..9 of the boot sector, see {@link #flashUploadBitstream(String)}.
|
| 1224 |
|
|
* If no boot sector is installed the method returns without any write action.
|
| 1225 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1226 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1227 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1228 |
|
|
*/
|
| 1229 |
|
|
public void flashResetBitstream ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1230 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 1231 |
2 |
ZTEX |
if ( ! flashEnabled() )
|
| 1232 |
|
|
throw new CapabilityException(this, "No Flash memory installed or");
|
| 1233 |
|
|
byte[] sector = new byte[flashSectorSize];
|
| 1234 |
|
|
byte[] ID = new String("ZTEXBS").getBytes();
|
| 1235 |
|
|
|
| 1236 |
|
|
flashReadSector(0,sector); // read the boot sector
|
| 1237 |
|
|
for (int k=0; k<6; k++)
|
| 1238 |
|
|
if ( sector[k] != ID[k] )
|
| 1239 |
|
|
return;
|
| 1240 |
|
|
if (sector[6]!=1 || sector[7]!=1 )
|
| 1241 |
|
|
return;
|
| 1242 |
|
|
sector[8] = 0;
|
| 1243 |
|
|
sector[9] = 0;
|
| 1244 |
|
|
flashWriteSector(0,sector); // write the boot sector
|
| 1245 |
|
|
}
|
| 1246 |
|
|
|
| 1247 |
|
|
// ******* flashFirstFreeSector ************************************************
|
| 1248 |
|
|
// Returns the first free sector of the Flash memory, i.e. the first sector behind the Bitstream
|
| 1249 |
|
|
/**
|
| 1250 |
|
|
* Returns the first free sector of the Flash memory.
|
| 1251 |
|
|
* This is the first sector behind the Bitstream, or 0 if no boot sector is installed (or 1 if a boot sector but no Bitstream is installed).
|
| 1252 |
|
|
* @return the first free sector of the Flash memory.
|
| 1253 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1254 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1255 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1256 |
|
|
*/
|
| 1257 |
|
|
public int flashFirstFreeSector ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1258 |
3 |
ZTEX |
checkCapability(CAPABILITY_FLASH);
|
| 1259 |
2 |
ZTEX |
if ( ! flashEnabled() )
|
| 1260 |
|
|
throw new CapabilityException(this, "No Flash memory installed or");
|
| 1261 |
|
|
|
| 1262 |
|
|
byte[] sector = new byte[flashSectorSize];
|
| 1263 |
|
|
byte[] ID = new String("ZTEXBS").getBytes();
|
| 1264 |
|
|
|
| 1265 |
|
|
flashReadSector(0,sector); // read the boot sector
|
| 1266 |
|
|
for (int k=0; k<6; k++)
|
| 1267 |
|
|
if ( sector[k] != ID[k] )
|
| 1268 |
|
|
return 0;
|
| 1269 |
|
|
if (sector[6]!=1 || sector[7]!=1 )
|
| 1270 |
|
|
return 0;
|
| 1271 |
|
|
return (sector[8] & 255) + ((sector[9] & 255) << 8) + 1;
|
| 1272 |
|
|
}
|
| 1273 |
|
|
|
| 1274 |
3 |
ZTEX |
|
| 1275 |
|
|
// ******* debugStackSize ******************************************************
|
| 1276 |
|
|
/**
|
| 1277 |
|
|
* Returns the size of message stack in messages.
|
| 1278 |
|
|
* @return the size of message stack in messages.
|
| 1279 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1280 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1281 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1282 |
|
|
*/
|
| 1283 |
|
|
public int debugStackSize ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1284 |
|
|
checkCapability(CAPABILITY_DEBUG);
|
| 1285 |
|
|
if ( debugStackSize<=0 || debugMsgSize<=0 ) {
|
| 1286 |
|
|
byte[] buf = new byte[7];
|
| 1287 |
|
|
vendorRequest2(0x28, "Read debug data", 0, 0, buf, 4);
|
| 1288 |
|
|
debugStackSize = buf[2] & 255;
|
| 1289 |
|
|
debugMsgSize = buf[3] & 255;
|
| 1290 |
|
|
}
|
| 1291 |
|
|
return debugStackSize;
|
| 1292 |
|
|
}
|
| 1293 |
|
|
|
| 1294 |
|
|
// ******* debugMsgSize ********************************************************
|
| 1295 |
|
|
/**
|
| 1296 |
|
|
* Returns the size of messages in bytes.
|
| 1297 |
|
|
* @return the size of messages in bytes.
|
| 1298 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1299 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1300 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1301 |
|
|
*/
|
| 1302 |
|
|
public int debugMsgSize ( ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1303 |
|
|
checkCapability(CAPABILITY_DEBUG);
|
| 1304 |
|
|
if ( debugMsgSize<=0 )
|
| 1305 |
|
|
debugStackSize();
|
| 1306 |
|
|
|
| 1307 |
|
|
return debugMsgSize;
|
| 1308 |
|
|
}
|
| 1309 |
|
|
|
| 1310 |
|
|
// ******* debugLastMsg ********************************************************
|
| 1311 |
|
|
/**
|
| 1312 |
|
|
* Returns the number of the last message read out by {@link #debugReadMessages(boolean,byte[])}
|
| 1313 |
|
|
* @return the number of the last message read out by {@link #debugReadMessages(boolean,byte[])}
|
| 1314 |
|
|
*/
|
| 1315 |
|
|
public final int debugLastMsg ( ) {
|
| 1316 |
|
|
return debugLastMsg;
|
| 1317 |
|
|
}
|
| 1318 |
|
|
|
| 1319 |
|
|
// ******* debugReadMessages ***************************************************
|
| 1320 |
|
|
/**
|
| 1321 |
|
|
* Reads debug messages from message stack.
|
| 1322 |
|
|
* The number of messages stored in buf is returned. The total number of new messages is stored in {@link #debugNewMessages}.
|
| 1323 |
|
|
* The number of the latest message is returned by {@link #debugLastMsg()}.
|
| 1324 |
|
|
* @param all If true, all messages from stack are written to buf. If it is false, only the new messages are written to buf.
|
| 1325 |
|
|
* @param buf The buffer to store the messages.
|
| 1326 |
|
|
* @return the size of messages stored in buffer.
|
| 1327 |
|
|
* @throws InvalidFirmwareException if interface 1 is not supported.
|
| 1328 |
|
|
* @throws UsbException if a communication error occurs.
|
| 1329 |
|
|
* @throws CapabilityException if Flash memory access is not possible.
|
| 1330 |
|
|
*/
|
| 1331 |
|
|
public int debugReadMessages ( boolean all, byte[] buf ) throws UsbException, InvalidFirmwareException, CapabilityException {
|
| 1332 |
|
|
checkCapability(CAPABILITY_DEBUG);
|
| 1333 |
|
|
byte buf2[] = new byte[ debugStackSize()*debugMsgSize() + 4 ];
|
| 1334 |
|
|
vendorRequest2(0x28, "Read debug data", 0, 0, buf2, buf2.length);
|
| 1335 |
|
|
int lm = (buf2[0] & 255) | ((buf2[1] & 255) << 8);
|
| 1336 |
|
|
debugNewMessages = lm - debugLastMsg;
|
| 1337 |
|
|
|
| 1338 |
|
|
int r = Math.min( Math.min( buf.length/debugMsgSize() , debugStackSize ), lm);
|
| 1339 |
|
|
if ( !all ) r = Math.min(r,debugNewMessages);
|
| 1340 |
|
|
for (int i = 0; i<r; i++) {
|
| 1341 |
|
|
int k=(lm-r+i) % debugStackSize;
|
| 1342 |
|
|
for (int j=0; j<debugMsgSize; j++ )
|
| 1343 |
|
|
buf[i*debugMsgSize+j] = buf2[k*debugMsgSize+j+4];
|
| 1344 |
|
|
}
|
| 1345 |
|
|
|
| 1346 |
|
|
debugLastMsg = lm;
|
| 1347 |
|
|
return r;
|
| 1348 |
|
|
}
|
| 1349 |
|
|
|
| 1350 |
2 |
ZTEX |
// ******* toString ************************************************************
|
| 1351 |
|
|
/**
|
| 1352 |
|
|
* Returns a lot of useful information about the corresponding device.
|
| 1353 |
|
|
* @return a lot of useful information about the corresponding device.
|
| 1354 |
|
|
*/
|
| 1355 |
|
|
public String toString () {
|
| 1356 |
|
|
String str = dev().toString();
|
| 1357 |
|
|
try {
|
| 1358 |
|
|
str += "\n " + getFpgaConfigurationStr();
|
| 1359 |
|
|
}
|
| 1360 |
|
|
catch ( Exception e ) {
|
| 1361 |
|
|
}
|
| 1362 |
|
|
return str;
|
| 1363 |
|
|
}
|
| 1364 |
|
|
|
| 1365 |
|
|
// ******* capabilityInfo ******************************************************
|
| 1366 |
|
|
/**
|
| 1367 |
|
|
* Creates a String with capability information.
|
| 1368 |
|
|
* @param pf A separator between the single capabilities, e.g. ", "
|
| 1369 |
|
|
* @return a string of the supported capabilities.
|
| 1370 |
|
|
*/
|
| 1371 |
|
|
public String capabilityInfo ( String pf ) {
|
| 1372 |
|
|
String str = "";
|
| 1373 |
|
|
for ( int i=0; i<6; i++ )
|
| 1374 |
|
|
for (int j=0; j<8; j++ )
|
| 1375 |
|
|
if ( dev().interfaceCapabilities(i,j) ) {
|
| 1376 |
|
|
if ( ! str.equals("") )
|
| 1377 |
|
|
str+=pf;
|
| 1378 |
|
|
if (i*8+j < capabilityStrings.length)
|
| 1379 |
|
|
str+=capabilityStrings[i*8+j];
|
| 1380 |
|
|
else
|
| 1381 |
|
|
str+=i+"."+j;
|
| 1382 |
|
|
}
|
| 1383 |
|
|
return str;
|
| 1384 |
|
|
}
|
| 1385 |
|
|
}
|
| 1386 |
|
|
|