1 |
4 |
nyawn |
/* cable_usbblaster.c - Altera USB Blaster driver for the Advanced JTAG Bridge
|
2 |
32 |
nyawn |
Copyright (C) 2008 - 2010 Nathan Yawn, nathan.yawn@opencores.org
|
3 |
4 |
nyawn |
|
4 |
|
|
This program is free software; you can redistribute it and/or modify
|
5 |
|
|
it under the terms of the GNU General Public License as published by
|
6 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
7 |
|
|
(at your option) any later version.
|
8 |
|
|
|
9 |
|
|
This program is distributed in the hope that it will be useful,
|
10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
|
GNU General Public License for more details.
|
13 |
|
|
|
14 |
|
|
You should have received a copy of the GNU General Public License
|
15 |
|
|
along with this program; if not, write to the Free Software
|
16 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
#include <stdio.h>
|
20 |
|
|
#include <sys/types.h>
|
21 |
|
|
#include <unistd.h> // for usleep()
|
22 |
|
|
#include <stdlib.h> // for sleep()
|
23 |
|
|
#include <arpa/inet.h> // for htons()
|
24 |
32 |
nyawn |
#include <string.h> // for memcpy()
|
25 |
4 |
nyawn |
|
26 |
|
|
#include "usb.h" // libusb header
|
27 |
55 |
nyawn |
|
28 |
|
|
#include "cable_usbblaster.h"
|
29 |
59 |
nyawn |
#include "utilities.h"
|
30 |
4 |
nyawn |
#include "errcodes.h"
|
31 |
|
|
|
32 |
|
|
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
|
33 |
|
|
|
34 |
55 |
nyawn |
jtag_cable_t usbblaster_cable_driver = {
|
35 |
|
|
.name = "usbblaster",
|
36 |
|
|
.inout_func = cable_usbblaster_inout,
|
37 |
|
|
.out_func = cable_usbblaster_out,
|
38 |
59 |
nyawn |
.init_func = cable_usbblaster_init ,
|
39 |
55 |
nyawn |
.opt_func = cable_usbblaster_opt,
|
40 |
|
|
.bit_out_func = cable_common_write_bit,
|
41 |
|
|
.bit_inout_func = cable_common_read_write_bit,
|
42 |
|
|
.stream_out_func = cable_usbblaster_write_stream,
|
43 |
|
|
.stream_inout_func = cable_usbblaster_read_stream,
|
44 |
|
|
.flush_func = NULL,
|
45 |
58 |
nyawn |
.opts = "p:v:",
|
46 |
|
|
.help = "-p [PID] Alteranate PID for USB device (hex value)\n\t-v [VID] Alternate VID for USB device (hex value)\n",
|
47 |
55 |
nyawn |
};
|
48 |
|
|
|
49 |
4 |
nyawn |
// USB constants for the USB Blaster
|
50 |
|
|
// Valid endpoints: 0x81, 0x02, 0x06, 0x88
|
51 |
|
|
#define EP2 0x02
|
52 |
|
|
#define EP1 0x81
|
53 |
|
|
|
54 |
58 |
nyawn |
// These are the Altera defaults, but can be changed on the command line
|
55 |
|
|
static uint32_t ALTERA_VID = 0x09FB;
|
56 |
|
|
static uint32_t ALTERA_PID = 0x6001;
|
57 |
|
|
|
58 |
4 |
nyawn |
//#define USB_TIMEOUT 500
|
59 |
|
|
#define USB_TIMEOUT 10000
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
// Bit meanings in the command byte sent to the USB-Blaster
|
63 |
|
|
#define USBBLASTER_CMD_TCK 0x01
|
64 |
|
|
#define USBBLASTER_CMD_TMS 0x02
|
65 |
|
|
#define USBBLASTER_CMD_nCE 0x04 /* should be left low */
|
66 |
|
|
#define USBBLASTER_CMD_nCS 0x08 /* must be set for byte-shift mode reads to work */
|
67 |
|
|
#define USBBLASTER_CMD_TDI 0x10
|
68 |
|
|
#define USBBLASTER_CMD_OE 0x20 /* appears necessary to set it to make everything work */
|
69 |
|
|
#define USBBLASTER_CMD_READ 0x40
|
70 |
|
|
#define USBBLASTER_CMD_BYTESHIFT 0x80
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
static struct usb_device *usbblaster_device;
|
74 |
32 |
nyawn |
static usb_dev_handle *h_device;
|
75 |
4 |
nyawn |
|
76 |
32 |
nyawn |
// libusb seems to give an error if we ask for a transfer larger than 64 bytes
|
77 |
|
|
// USBBlaster also has a max. single transaction of 63 bytes.
|
78 |
|
|
// So, size the max read and write to create 64-byte USB packets (reads have 2 useless bytes prepended)
|
79 |
|
|
#define USBBLASTER_MAX_WRITE 63
|
80 |
|
|
static char data_out_scratchpad[USBBLASTER_MAX_WRITE+1];
|
81 |
|
|
#define USBBLASTER_MAX_READ 62
|
82 |
|
|
static char data_in_scratchpad[USBBLASTER_MAX_READ+2];
|
83 |
4 |
nyawn |
|
84 |
59 |
nyawn |
// Flag used to avoid unnecessary transfers
|
85 |
|
|
// Since clock is lowered if high (and never the other way around),
|
86 |
|
|
// 1 is a safe default value.
|
87 |
|
|
static unsigned char clock_is_high = 1;
|
88 |
|
|
|
89 |
32 |
nyawn |
int cable_usbblaster_open_cable(void);
|
90 |
|
|
void cable_usbblaster_close_cable(void);
|
91 |
|
|
//int cable_usbblaster_reopen_cable(void);
|
92 |
|
|
|
93 |
4 |
nyawn |
///////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
/*-------------------------------------[ USB Blaster specific functions ]---*/
|
95 |
|
|
/////////////////////////////////////////////////////////////////////////////
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
static int usbblaster_start_interface(struct usb_dev_handle *xpcu)
|
99 |
|
|
{
|
100 |
|
|
// Need to send a VENDOR request OUT, request = GET_STATUS
|
101 |
|
|
// Other parameters are ignored
|
102 |
|
|
if(usb_control_msg(xpcu, (USB_ENDPOINT_OUT | USB_TYPE_VENDOR), USB_REQ_GET_STATUS,
|
103 |
|
|
0, 0, NULL, 0, 1000)<0)
|
104 |
|
|
{
|
105 |
|
|
perror("usb_control_msg(start interface)");
|
106 |
|
|
return APP_ERR_USB;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
return APP_ERR_NONE;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
static int usbblaster_read_firmware_version(struct usb_dev_handle *xpcu, uint16_t *buf)
|
114 |
|
|
{
|
115 |
|
|
if(usb_control_msg(xpcu, 0xC0, 0x90, 0, 3, (char*)buf, 2, USB_TIMEOUT)<0)
|
116 |
|
|
{
|
117 |
|
|
perror("usb_control_msg(0x90.0) (read_firmware_version)");
|
118 |
|
|
return APP_ERR_USB;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
// Swap endian
|
122 |
|
|
*buf = htons(*buf);
|
123 |
|
|
//*buf = (*buf << 8) | (*buf >> 8);
|
124 |
|
|
|
125 |
|
|
return APP_ERR_NONE;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
static int usbblaster_enumerate_bus(void)
|
131 |
|
|
{
|
132 |
|
|
int flag; // for USB bus scanning stop condition
|
133 |
|
|
struct usb_bus *bus; // pointer on the USB bus
|
134 |
|
|
|
135 |
|
|
// board detection
|
136 |
|
|
usb_init();
|
137 |
|
|
usb_find_busses();
|
138 |
|
|
usb_find_devices();
|
139 |
|
|
|
140 |
|
|
flag = 0;
|
141 |
|
|
|
142 |
|
|
for (bus = usb_get_busses(); bus; bus = bus->next)
|
143 |
|
|
{
|
144 |
|
|
for (usbblaster_device = bus->devices; usbblaster_device; usbblaster_device = usbblaster_device->next)
|
145 |
|
|
{
|
146 |
|
|
if (usbblaster_device->descriptor.idVendor == ALTERA_VID &&
|
147 |
|
|
usbblaster_device->descriptor.idProduct == ALTERA_PID)
|
148 |
|
|
{
|
149 |
|
|
flag = 1;
|
150 |
|
|
fprintf(stderr, "Found Altera USB-Blaster\n");
|
151 |
|
|
return APP_ERR_NONE;
|
152 |
|
|
}
|
153 |
|
|
}
|
154 |
|
|
if (flag)
|
155 |
|
|
break;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
58 |
nyawn |
fprintf(stderr, "Failed to find USB-Blaster with VID 0x%0X, PID 0x%0X\n", ALTERA_VID, ALTERA_PID);
|
159 |
4 |
nyawn |
return APP_ERR_CABLENOTFOUND;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
int cable_usbblaster_init(){
|
164 |
|
|
int err = APP_ERR_NONE;
|
165 |
|
|
|
166 |
|
|
// Process to reset the usb blaster
|
167 |
|
|
if(err |= usbblaster_enumerate_bus()) {
|
168 |
|
|
return err;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
32 |
nyawn |
h_device = usb_open(usbblaster_device);
|
172 |
4 |
nyawn |
|
173 |
|
|
if(h_device == NULL)
|
174 |
|
|
{
|
175 |
|
|
fprintf(stderr, "Init failed to open USB device for reset\n");
|
176 |
|
|
return APP_ERR_USB;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
if(usb_reset(h_device) != APP_ERR_NONE)
|
180 |
|
|
fprintf(stderr, "Failed to reset USB Blaster\n");
|
181 |
|
|
|
182 |
|
|
usb_close(h_device);
|
183 |
|
|
|
184 |
|
|
// Wait for reset!!!
|
185 |
|
|
sleep(1);
|
186 |
|
|
|
187 |
|
|
// Do device initialization
|
188 |
|
|
if(err |= usbblaster_enumerate_bus())
|
189 |
|
|
return err;
|
190 |
|
|
|
191 |
32 |
nyawn |
err = cable_usbblaster_open_cable();
|
192 |
|
|
if(err != APP_ERR_NONE) return err;
|
193 |
4 |
nyawn |
|
194 |
|
|
//usb_clear_halt(h_device, EP1);
|
195 |
|
|
//usb_clear_halt(h_device, EP2);
|
196 |
|
|
|
197 |
|
|
// IMPORTANT: DO NOT SEND A REQUEST TYPE "CLASS" OR TYPE "RESERVED". This may stall the EP.
|
198 |
|
|
|
199 |
|
|
// Some clones need this before they will start processing IN/OUT requests
|
200 |
|
|
if(usbblaster_start_interface(h_device) != APP_ERR_NONE)
|
201 |
|
|
fprintf(stderr, "Failed to start remote interface\n");
|
202 |
|
|
|
203 |
|
|
uint16_t buf;
|
204 |
|
|
if(err |= usbblaster_read_firmware_version(h_device, &buf))
|
205 |
|
|
{
|
206 |
|
|
usb_close(h_device);
|
207 |
|
|
fprintf(stderr, "Failed to read firmware version\n");
|
208 |
|
|
return err;
|
209 |
|
|
}
|
210 |
|
|
else
|
211 |
|
|
{
|
212 |
|
|
printf("firmware version = 0x%04X (%u)\n", buf, buf);
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
// USB blaster is expecting us to read 2 bytes, which are useless to us...
|
217 |
|
|
char ret[2];
|
218 |
|
|
int rv = usb_bulk_read(h_device, EP1, ret, 2, USB_TIMEOUT);
|
219 |
|
|
if (rv < 0){ // But if we fail, who cares?
|
220 |
|
|
fprintf(stderr, "\nWarning: Failed to read post-init bytes from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
|
221 |
|
|
}
|
222 |
32 |
nyawn |
|
223 |
4 |
nyawn |
return APP_ERR_NONE;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
int cable_usbblaster_out(uint8_t value)
|
228 |
|
|
{
|
229 |
|
|
int rv; // to catch return values of functions
|
230 |
14 |
nyawn |
char out;
|
231 |
4 |
nyawn |
int err = APP_ERR_NONE;
|
232 |
|
|
|
233 |
32 |
nyawn |
// open the device, if necessary
|
234 |
|
|
if(h_device == NULL) {
|
235 |
|
|
rv = cable_usbblaster_open_cable();
|
236 |
|
|
if(rv != APP_ERR_NONE) return rv;
|
237 |
4 |
nyawn |
}
|
238 |
|
|
|
239 |
|
|
out = (USBBLASTER_CMD_OE | USBBLASTER_CMD_nCS); // Set output enable (appears necessary) and nCS (necessary for byte-shift reads)
|
240 |
|
|
|
241 |
|
|
// Translate to USB blaster protocol
|
242 |
|
|
// USB-Blaster has no TRST pin
|
243 |
|
|
if(value & TCLK_BIT)
|
244 |
|
|
out |= USBBLASTER_CMD_TCK;
|
245 |
|
|
if(value & TDI_BIT)
|
246 |
|
|
out |= USBBLASTER_CMD_TDI;
|
247 |
|
|
if(value & TMS_BIT)
|
248 |
|
|
out |= USBBLASTER_CMD_TMS;
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
rv = usb_bulk_write(h_device, EP2, &out, 1, USB_TIMEOUT);
|
252 |
|
|
if (rv != 1){
|
253 |
|
|
fprintf(stderr, "\nFailed to write to the FIFO (rv = %d):\n%s", rv, usb_strerror());
|
254 |
|
|
err |= APP_ERR_USB;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
59 |
nyawn |
if(value & TCLK_BIT)
|
258 |
|
|
{
|
259 |
|
|
clock_is_high = 1;
|
260 |
|
|
}
|
261 |
|
|
else
|
262 |
|
|
{
|
263 |
|
|
clock_is_high = 0;
|
264 |
|
|
}
|
265 |
|
|
|
266 |
4 |
nyawn |
return err;
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
|
270 |
|
|
int cable_usbblaster_inout(uint8_t value, uint8_t *in_bit)
|
271 |
|
|
{
|
272 |
|
|
int rv; // to catch return values of functions
|
273 |
|
|
char ret[3] = {0,0,0}; // Two useless bytes (0x31,0x60) always precede the useful byte
|
274 |
14 |
nyawn |
char out;
|
275 |
4 |
nyawn |
|
276 |
|
|
out = (USBBLASTER_CMD_OE | USBBLASTER_CMD_nCS); // Set output enable (?) and nCS (necessary for byte-shift reads)
|
277 |
|
|
out |= USBBLASTER_CMD_READ;
|
278 |
|
|
|
279 |
|
|
// Translate to USB blaster protocol
|
280 |
|
|
// USB-Blaster has no TRST pin
|
281 |
|
|
if(value & TCLK_BIT)
|
282 |
|
|
out |= USBBLASTER_CMD_TCK;
|
283 |
|
|
if(value & TDI_BIT)
|
284 |
|
|
out |= USBBLASTER_CMD_TDI;
|
285 |
|
|
if(value & TMS_BIT)
|
286 |
|
|
out |= USBBLASTER_CMD_TMS;
|
287 |
|
|
|
288 |
32 |
nyawn |
// open the device, if necessary
|
289 |
|
|
if(h_device == NULL) {
|
290 |
|
|
rv = cable_usbblaster_open_cable();
|
291 |
|
|
if(rv != APP_ERR_NONE) return rv;
|
292 |
4 |
nyawn |
}
|
293 |
|
|
|
294 |
|
|
// Send a read request
|
295 |
|
|
rv = usb_bulk_write(h_device, EP2, &out, 1, USB_TIMEOUT);
|
296 |
|
|
if (rv != 1){
|
297 |
|
|
fprintf(stderr, "\nFailed to write a read request to the EP2 FIFO:\n%s", usb_strerror());
|
298 |
32 |
nyawn |
cable_usbblaster_close_cable();
|
299 |
4 |
nyawn |
return APP_ERR_USB;
|
300 |
|
|
}
|
301 |
|
|
|
302 |
59 |
nyawn |
if(value & TCLK_BIT)
|
303 |
|
|
{
|
304 |
|
|
clock_is_high = 1;
|
305 |
|
|
}
|
306 |
|
|
else
|
307 |
|
|
{
|
308 |
|
|
clock_is_high = 0;
|
309 |
|
|
}
|
310 |
4 |
nyawn |
|
311 |
|
|
// receive the response
|
312 |
|
|
// Sometimes, we do a read but just get the useless 0x31,0x60 chars...
|
313 |
|
|
// retry until we get a 3rd byte (with real data), for a reasonable number of retries.
|
314 |
|
|
int retries = 0;
|
315 |
|
|
do {
|
316 |
|
|
rv = usb_bulk_read(h_device, EP1, ret, 3, USB_TIMEOUT);
|
317 |
|
|
if (rv < 0){
|
318 |
|
|
fprintf(stderr, "\nFailed to read from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
|
319 |
32 |
nyawn |
cable_usbblaster_close_cable();
|
320 |
4 |
nyawn |
return APP_ERR_USB;
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
// fprintf(stderr, "Read %i bytes: 0x%X, 0x%X, 0x%X\n", rv, ret[0], ret[1], ret[2]);
|
324 |
|
|
retries++;
|
325 |
|
|
}
|
326 |
|
|
while((rv < 3) && (retries < 20));
|
327 |
|
|
|
328 |
|
|
*in_bit = (ret[2] & 0x01); /* TDO is bit 0. USB-Blaster may also set bit 1. */
|
329 |
|
|
return APP_ERR_NONE;
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
// The usbblaster transfers the bits in the stream in the following order:
|
334 |
|
|
// bit 0 of the first byte received ... bit 7 of the first byte received
|
335 |
|
|
// bit 0 of second byte received ... etc.
|
336 |
|
|
int cable_usbblaster_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
|
337 |
|
|
int rv; // to catch return values of functions
|
338 |
|
|
unsigned int bytes_to_transfer, leftover_bit_length;
|
339 |
|
|
uint32_t leftover_bits;
|
340 |
32 |
nyawn |
int bytes_remaining;
|
341 |
|
|
char *xfer_ptr;
|
342 |
4 |
nyawn |
int err = APP_ERR_NONE;
|
343 |
|
|
|
344 |
32 |
nyawn |
debug("cable_usbblaster_write_stream(0x%X, %d, %i)\n", stream, len, set_last_bit);
|
345 |
4 |
nyawn |
|
346 |
|
|
// This routine must transfer at least 8 bits. Additionally, TMS (the last bit)
|
347 |
|
|
// cannot be set by 'byte shift mode'. So we need at least 8 bits to transfer,
|
348 |
|
|
// plus one bit to send along with TMS.
|
349 |
|
|
bytes_to_transfer = len_bits / 8;
|
350 |
|
|
leftover_bit_length = len_bits - (bytes_to_transfer * 8);
|
351 |
|
|
|
352 |
|
|
if((!leftover_bit_length) && set_last_bit) {
|
353 |
|
|
bytes_to_transfer -= 1;
|
354 |
|
|
leftover_bit_length += 8;
|
355 |
|
|
}
|
356 |
|
|
|
357 |
32 |
nyawn |
debug("bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
|
358 |
4 |
nyawn |
|
359 |
|
|
// Not enough bits for high-speed transfer. bit-bang.
|
360 |
|
|
if(bytes_to_transfer == 0) {
|
361 |
|
|
return cable_common_write_stream(stream, len_bits, set_last_bit);
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
// Bitbang functions leave clock high. USBBlaster assumes clock low at the start of a burst.
|
365 |
59 |
nyawn |
if(clock_is_high)
|
366 |
|
|
{
|
367 |
|
|
err |= cable_usbblaster_out(0); // Lower the clock.
|
368 |
|
|
}
|
369 |
4 |
nyawn |
|
370 |
|
|
// Set leftover bits
|
371 |
|
|
leftover_bits = (stream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
|
372 |
|
|
|
373 |
32 |
nyawn |
debug("leftover_bits: 0x%X, LSB_first_xfer = %d\n", leftover_bits, LSB_first_xfer);
|
374 |
4 |
nyawn |
|
375 |
32 |
nyawn |
// open the device, if necessary
|
376 |
|
|
if(h_device == NULL) {
|
377 |
|
|
rv = cable_usbblaster_open_cable();
|
378 |
|
|
if(rv != APP_ERR_NONE) return rv;
|
379 |
4 |
nyawn |
}
|
380 |
|
|
|
381 |
32 |
nyawn |
bytes_remaining = bytes_to_transfer;
|
382 |
|
|
xfer_ptr = (char *) stream;
|
383 |
|
|
while(bytes_remaining > 0)
|
384 |
|
|
{
|
385 |
|
|
int bytes_this_xfer = (bytes_remaining > USBBLASTER_MAX_WRITE) ? USBBLASTER_MAX_WRITE:bytes_remaining;
|
386 |
4 |
nyawn |
|
387 |
32 |
nyawn |
data_out_scratchpad[0] = USBBLASTER_CMD_BYTESHIFT | (bytes_this_xfer & 0x3F);
|
388 |
|
|
memcpy(&data_out_scratchpad[1], xfer_ptr, bytes_this_xfer);
|
389 |
|
|
|
390 |
|
|
/* printf("Data packet: ");
|
391 |
|
|
for(i = 0; i <= bytes_to_transfer; i++)
|
392 |
|
|
printf("0x%X ", out[i]);
|
393 |
|
|
printf("\n"); */
|
394 |
|
|
|
395 |
|
|
rv = usb_bulk_write(h_device, EP2, data_out_scratchpad, bytes_this_xfer+1, USB_TIMEOUT);
|
396 |
|
|
if (rv != (bytes_this_xfer+1)){
|
397 |
|
|
fprintf(stderr, "\nFailed to write to the FIFO (rv = %d):\n%s", rv, usb_strerror());
|
398 |
|
|
err |= APP_ERR_USB;
|
399 |
|
|
break;
|
400 |
|
|
}
|
401 |
4 |
nyawn |
|
402 |
32 |
nyawn |
bytes_remaining -= bytes_this_xfer;
|
403 |
|
|
xfer_ptr += bytes_this_xfer;
|
404 |
4 |
nyawn |
}
|
405 |
|
|
|
406 |
59 |
nyawn |
clock_is_high = 0;
|
407 |
|
|
|
408 |
4 |
nyawn |
// if we have a number of bits not divisible by 8, or we need to set TMS...
|
409 |
|
|
if(leftover_bit_length != 0) {
|
410 |
|
|
//printf("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
|
411 |
|
|
return cable_common_write_stream(&leftover_bits, leftover_bit_length, set_last_bit);
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
return err;
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
|
418 |
|
|
int cable_usbblaster_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
|
419 |
|
|
int rv; // to catch return values of functions
|
420 |
32 |
nyawn |
unsigned int bytes_received = 0, total_bytes_received = 0;
|
421 |
4 |
nyawn |
unsigned int bytes_to_transfer, leftover_bit_length;
|
422 |
|
|
uint32_t leftover_bits, leftovers_received = 0;
|
423 |
32 |
nyawn |
unsigned char i;
|
424 |
|
|
int bytes_remaining;
|
425 |
|
|
char *xfer_ptr;
|
426 |
4 |
nyawn |
int retval = APP_ERR_NONE;
|
427 |
|
|
|
428 |
|
|
debug("cable_usbblaster_read_stream(0x%X, %d, %i)\n", outstream[0], len_bits, set_last_bit);
|
429 |
|
|
|
430 |
|
|
// This routine must transfer at least 8 bits. Additionally, TMS (the last bit)
|
431 |
|
|
// cannot be set by 'byte shift mode'. So we need at least 8 bits to transfer,
|
432 |
|
|
// plus one bit to send along with TMS.
|
433 |
|
|
bytes_to_transfer = len_bits / 8;
|
434 |
|
|
leftover_bit_length = len_bits - (bytes_to_transfer * 8);
|
435 |
|
|
|
436 |
|
|
if((!leftover_bit_length) && set_last_bit) {
|
437 |
|
|
bytes_to_transfer -= 1;
|
438 |
|
|
leftover_bit_length += 8;
|
439 |
|
|
}
|
440 |
|
|
|
441 |
32 |
nyawn |
debug("RD bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
|
442 |
4 |
nyawn |
|
443 |
|
|
// Not enough bits for high-speed transfer. bit-bang.
|
444 |
|
|
if(bytes_to_transfer == 0) {
|
445 |
|
|
return cable_common_read_stream(outstream, instream, len_bits, set_last_bit);
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
// Bitbang functions leave clock high. USBBlaster assumes clock low at the start of a burst.
|
449 |
59 |
nyawn |
if(clock_is_high)
|
450 |
|
|
{
|
451 |
|
|
// Lower the clock.
|
452 |
|
|
retval |= cable_usbblaster_out(0);
|
453 |
|
|
}
|
454 |
4 |
nyawn |
|
455 |
|
|
// Zero the input, since we add new data by logical-OR
|
456 |
32 |
nyawn |
for(i = 0; i < (len_bits/32); i++) instream[i] = 0;
|
457 |
|
|
if(len_bits % 32) instream[i] = 0;
|
458 |
4 |
nyawn |
|
459 |
|
|
// Set leftover bits
|
460 |
|
|
leftover_bits = (outstream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
|
461 |
|
|
debug("leftover_bits: 0x%X\n", leftover_bits);
|
462 |
|
|
|
463 |
32 |
nyawn |
// open the device, if necessary
|
464 |
|
|
if(h_device == NULL) {
|
465 |
|
|
rv = cable_usbblaster_open_cable();
|
466 |
|
|
if(rv != APP_ERR_NONE) return rv;
|
467 |
4 |
nyawn |
}
|
468 |
|
|
|
469 |
32 |
nyawn |
// Transfer the data. USBBlaster has a max transfer size of 64 bytes.
|
470 |
|
|
bytes_remaining = bytes_to_transfer;
|
471 |
|
|
xfer_ptr = (char *) outstream;
|
472 |
|
|
total_bytes_received = 0;
|
473 |
|
|
while(bytes_remaining > 0)
|
474 |
|
|
{
|
475 |
|
|
int bytes_this_xfer = (bytes_remaining > USBBLASTER_MAX_READ) ? USBBLASTER_MAX_READ:bytes_remaining;
|
476 |
|
|
data_out_scratchpad[0] = USBBLASTER_CMD_BYTESHIFT | USBBLASTER_CMD_READ | (bytes_this_xfer & 0x3F);
|
477 |
|
|
memcpy(&data_out_scratchpad[1], xfer_ptr, bytes_this_xfer);
|
478 |
4 |
nyawn |
|
479 |
32 |
nyawn |
/* debug("Data packet: ");
|
480 |
|
|
for(i = 0; i <= bytes_to_transfer; i++) debug("0x%X ", data_out_scratchpad[i]);
|
481 |
|
|
debug("\n"); */
|
482 |
4 |
nyawn |
|
483 |
32 |
nyawn |
rv = usb_bulk_write(h_device, EP2, data_out_scratchpad, bytes_this_xfer+1, USB_TIMEOUT);
|
484 |
|
|
if (rv != (bytes_this_xfer+1)){
|
485 |
|
|
fprintf(stderr, "\nFailed to write to the EP2 FIFO (rv = %d):\n%s", rv, usb_strerror());
|
486 |
|
|
cable_usbblaster_close_cable();
|
487 |
|
|
return APP_ERR_USB;
|
488 |
|
|
}
|
489 |
4 |
nyawn |
|
490 |
32 |
nyawn |
// receive the response
|
491 |
|
|
// Sometimes, we do a read but just get the useless 0x31,0x60 chars...
|
492 |
|
|
// retry until we get at least 3 bytes (with real data), for a reasonable number of retries.
|
493 |
|
|
int retries = 0;
|
494 |
|
|
bytes_received = 0;
|
495 |
|
|
do {
|
496 |
|
|
debug("stream read, bytes_this_xfer = %i, bytes_received = %i\n", bytes_this_xfer, bytes_received);
|
497 |
|
|
rv = usb_bulk_read(h_device, EP1, data_in_scratchpad, (bytes_this_xfer-bytes_received)+2, USB_TIMEOUT);
|
498 |
|
|
if (rv < 0){
|
499 |
|
|
fprintf(stderr, "\nFailed to read stream from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
|
500 |
|
|
cable_usbblaster_close_cable();
|
501 |
|
|
return APP_ERR_USB;
|
502 |
|
|
}
|
503 |
4 |
nyawn |
|
504 |
32 |
nyawn |
/* debug("Read %i bytes: ", rv);
|
505 |
|
|
for(i = 0; i < rv; i++)
|
506 |
|
|
debug("0x%X ", data_in_scratchpad[i]);
|
507 |
|
|
debug("\n"); */
|
508 |
|
|
|
509 |
|
|
if(rv > 2) retries = 0;
|
510 |
|
|
else retries++;
|
511 |
|
|
|
512 |
|
|
/* Put the received bytes into the return stream. Works for either endian. */
|
513 |
|
|
for(i = 0; i < (rv-2); i++) {
|
514 |
|
|
// Do size/type promotion before shift. Must cast to unsigned, else the value may be
|
515 |
|
|
// sign-extended through the upper 16 bits of the uint32_t.
|
516 |
|
|
uint32_t tmp = (unsigned char) data_in_scratchpad[2+i];
|
517 |
|
|
instream[(total_bytes_received+i)>>2] |= (tmp << ((i & 0x3)*8));
|
518 |
|
|
}
|
519 |
4 |
nyawn |
|
520 |
32 |
nyawn |
bytes_received += (rv-2);
|
521 |
|
|
total_bytes_received += (rv-2);
|
522 |
|
|
}
|
523 |
|
|
while((bytes_received < bytes_this_xfer) && (retries < 15));
|
524 |
4 |
nyawn |
|
525 |
32 |
nyawn |
bytes_remaining -= bytes_this_xfer;
|
526 |
|
|
xfer_ptr += bytes_this_xfer;
|
527 |
4 |
nyawn |
}
|
528 |
|
|
|
529 |
59 |
nyawn |
clock_is_high = 0;
|
530 |
|
|
|
531 |
4 |
nyawn |
// if we have a number of bits not divisible by 8
|
532 |
|
|
if(leftover_bit_length != 0) {
|
533 |
|
|
debug("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
|
534 |
|
|
retval |= cable_common_read_stream(&leftover_bits, &leftovers_received, leftover_bit_length, set_last_bit);
|
535 |
|
|
instream[bytes_to_transfer>>2] |= (leftovers_received & 0xFF) << (8*(bytes_to_transfer & 0x3));
|
536 |
|
|
}
|
537 |
|
|
|
538 |
|
|
return retval;
|
539 |
|
|
}
|
540 |
|
|
|
541 |
55 |
nyawn |
jtag_cable_t *cable_usbblaster_get_driver(void)
|
542 |
|
|
{
|
543 |
|
|
return &usbblaster_cable_driver;
|
544 |
|
|
}
|
545 |
4 |
nyawn |
|
546 |
|
|
int cable_usbblaster_opt(int c, char *str)
|
547 |
|
|
{
|
548 |
58 |
nyawn |
uint32_t newvid;
|
549 |
|
|
uint32_t newpid;
|
550 |
|
|
|
551 |
|
|
switch(c) {
|
552 |
|
|
case 'p':
|
553 |
|
|
if(!sscanf(str, "%x", &newpid)) {
|
554 |
|
|
fprintf(stderr, "p parameter must have a hex number as parameter\n");
|
555 |
|
|
return APP_ERR_BAD_PARAM;
|
556 |
|
|
}
|
557 |
|
|
else {
|
558 |
|
|
ALTERA_PID = newpid;
|
559 |
|
|
}
|
560 |
|
|
break;
|
561 |
|
|
|
562 |
|
|
case 'v':
|
563 |
|
|
if(!sscanf(str, "%x", &newvid)) {
|
564 |
|
|
fprintf(stderr, "v parameter must have a hex number as parameter\n");
|
565 |
|
|
return APP_ERR_BAD_PARAM;
|
566 |
|
|
}
|
567 |
|
|
else {
|
568 |
|
|
ALTERA_VID = newvid;
|
569 |
|
|
}
|
570 |
|
|
break;
|
571 |
|
|
|
572 |
|
|
default:
|
573 |
|
|
fprintf(stderr, "Unknown parameter '%c'\n", c);
|
574 |
|
|
return APP_ERR_BAD_PARAM;
|
575 |
|
|
}
|
576 |
|
|
return APP_ERR_NONE;
|
577 |
4 |
nyawn |
}
|
578 |
|
|
|
579 |
|
|
|
580 |
32 |
nyawn |
int cable_usbblaster_open_cable(void)
|
581 |
|
|
{
|
582 |
59 |
nyawn |
int if_not_claimed = 1;
|
583 |
|
|
timeout_timer timer;
|
584 |
|
|
|
585 |
32 |
nyawn |
// open the device
|
586 |
|
|
h_device = usb_open(usbblaster_device);
|
587 |
|
|
if (h_device == NULL){
|
588 |
|
|
fprintf(stderr, "USBBlaster driver failed to open device\n");
|
589 |
|
|
return APP_ERR_USB;
|
590 |
|
|
}
|
591 |
|
|
|
592 |
|
|
// set the configuration
|
593 |
|
|
if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue))
|
594 |
|
|
{
|
595 |
|
|
usb_close(h_device);
|
596 |
|
|
h_device = NULL;
|
597 |
|
|
fprintf(stderr, "USBBlaster driver failed to set configuration\n");
|
598 |
|
|
return APP_ERR_USB;
|
599 |
|
|
}
|
600 |
|
|
|
601 |
|
|
// wait until device is ready
|
602 |
59 |
nyawn |
if ( create_timer(&timer) )
|
603 |
|
|
{
|
604 |
|
|
fprintf(stderr, "Failed to create timer\n");
|
605 |
|
|
// fall back to infinite wait
|
606 |
|
|
while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
|
607 |
|
|
}
|
608 |
|
|
else
|
609 |
|
|
{
|
610 |
|
|
while (if_not_claimed && !timedout(&timer) )
|
611 |
|
|
if_not_claimed = usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
|
612 |
32 |
nyawn |
|
613 |
59 |
nyawn |
if ( timedout(&timer) )
|
614 |
|
|
{
|
615 |
|
|
fprintf(stderr, "Claiming interface timed out...\n");
|
616 |
|
|
return APP_ERR_USB;
|
617 |
|
|
}
|
618 |
|
|
}
|
619 |
|
|
|
620 |
32 |
nyawn |
return APP_ERR_NONE;
|
621 |
|
|
}
|
622 |
|
|
|
623 |
|
|
|
624 |
|
|
void cable_usbblaster_close_cable(void)
|
625 |
|
|
{
|
626 |
|
|
if(h_device != NULL) {
|
627 |
|
|
// release the interface cleanly
|
628 |
|
|
if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
|
629 |
|
|
fprintf(stderr, "Warning: failed to release usb interface\n");
|
630 |
|
|
}
|
631 |
|
|
|
632 |
|
|
// close the device
|
633 |
|
|
usb_close(h_device);
|
634 |
|
|
h_device = NULL;
|
635 |
|
|
}
|
636 |
|
|
|
637 |
|
|
return;
|
638 |
|
|
}
|
639 |
|
|
|
640 |
|
|
/*
|
641 |
|
|
int cable_usbblaster_reopen_cable(void)
|
642 |
|
|
{
|
643 |
|
|
cable_usbblaster_close_cable();
|
644 |
|
|
return cable_usbblaster_open_cable();
|
645 |
|
|
}
|
646 |
|
|
*/
|