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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Software/] [adv_jtag_bridge/] [cable_usbblaster.c] - Blame information for rev 14

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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