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

Subversion Repositories or1k_soc_on_altera_embedded_dev_kit

[/] [or1k_soc_on_altera_embedded_dev_kit/] [trunk/] [soc/] [sw/] [adv_jtag_bridge/] [cable_usbblaster.c] - Blame information for rev 12

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

Line No. Rev Author Line
1 12 xianfeng
/* 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
#include <sys/time.h>
25
#include <time.h>
26
 
27
#ifdef __SUPPORT_FTDI_CABLES__
28
#include <ftdi.h>
29
#endif
30
 
31
#include "usb.h"  // libusb header
32
#include "cable_common.h"
33
#include "errcodes.h"
34
 
35
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
36
 
37
// USB constants for the USB Blaster
38
// Valid endpoints: 0x81, 0x02, 0x06, 0x88
39
#define EP2        0x02
40
#define EP1        0x81
41
#define ALTERA_VID 0x09FB
42
#define ALTERA_PID 0x6001
43
 
44
//#define USB_TIMEOUT 500
45
#define USB_TIMEOUT 10000
46
 
47
 
48
// Bit meanings in the command byte sent to the USB-Blaster
49
#define USBBLASTER_CMD_TCK 0x01
50
#define USBBLASTER_CMD_TMS 0x02
51
#define USBBLASTER_CMD_nCE 0x04  /* should be left low */
52
#define USBBLASTER_CMD_nCS 0x08  /* must be set for byte-shift mode reads to work */
53
#define USBBLASTER_CMD_TDI 0x10
54
#define USBBLASTER_CMD_OE  0x20  /* appears necessary to set it to make everything work */
55
#define USBBLASTER_CMD_READ 0x40
56
#define USBBLASTER_CMD_BYTESHIFT 0x80
57
 
58
 
59
static struct usb_device *usbblaster_device;
60
 
61
static char *data_out_scratchpad = NULL;
62
static int data_out_scratchpad_size = 0;
63
static char *data_in_scratchpad = NULL;
64
static int data_in_scratchpad_size = 0;
65
 
66
#ifdef __SUPPORT_FTDI_CABLES__
67
static struct ftdi_context ftdic;
68
#endif
69
 
70
 
71
///////////////////////////////////////////////////////////////////////////////
72
/*-------------------------------------[ USB Blaster specific functions ]---*/
73
/////////////////////////////////////////////////////////////////////////////
74
 
75
 
76
static int usbblaster_start_interface(struct usb_dev_handle *xpcu)
77
{
78
  // Need to send a VENDOR request OUT, request = GET_STATUS
79
  // Other parameters are ignored
80
  if(usb_control_msg(xpcu, (USB_ENDPOINT_OUT | USB_TYPE_VENDOR), USB_REQ_GET_STATUS,
81
                     0, 0, NULL, 0, 1000)<0)
82
    {
83
      perror("usb_control_msg(start interface)");
84
      return APP_ERR_USB;
85
    }
86
 
87
  return APP_ERR_NONE;
88
}
89
 
90
 
91
static int usbblaster_read_firmware_version(struct usb_dev_handle *xpcu, uint16_t *buf)
92
{
93
  if(usb_control_msg(xpcu, 0xC0, 0x90, 0, 3, (char*)buf, 2, USB_TIMEOUT)<0)
94
    {
95
      perror("usb_control_msg(0x90.0) (read_firmware_version)");
96
      return APP_ERR_USB;
97
    }
98
 
99
  // Swap endian
100
  *buf = htons(*buf);
101
  //*buf = (*buf << 8) | (*buf >> 8);
102
 
103
  return APP_ERR_NONE;
104
}
105
 
106
 
107
 
108
static int usbblaster_enumerate_bus(void)
109
{
110
  int             flag;  // for USB bus scanning stop condition
111
  struct usb_bus *bus;   // pointer on the USB bus
112
 
113
  // board detection
114
  usb_init();
115
  usb_find_busses();
116
  usb_find_devices();
117
 
118
  flag = 0;
119
 
120
  for (bus = usb_get_busses(); bus; bus = bus->next)
121
  {
122
    for (usbblaster_device = bus->devices; usbblaster_device; usbblaster_device = usbblaster_device->next)
123
    {
124
      if (usbblaster_device->descriptor.idVendor  == ALTERA_VID &&
125
          usbblaster_device->descriptor.idProduct == ALTERA_PID)
126
      {
127
              flag = 1;
128
              fprintf(stderr, "Found Altera USB-Blaster\n");
129
              return APP_ERR_NONE;
130
      }
131
    }
132
    if (flag)
133
      break;
134
  }
135
 
136
  fprintf(stderr, "Failed to find USB-Blaster\n");
137
  return APP_ERR_CABLENOTFOUND;
138
}
139
 
140
 
141
int cable_usbblaster_init(){
142
  int err = APP_ERR_NONE;
143
 
144
  // Process to reset the usb blaster
145
  if(err |= usbblaster_enumerate_bus()) {
146
    return err;
147
  }
148
 
149
  usb_dev_handle *h_device = usb_open(usbblaster_device);
150
 
151
  if(h_device == NULL)
152
    {
153
      fprintf(stderr, "Init failed to open USB device for reset\n");
154
      return APP_ERR_USB;
155
    }
156
 
157
  if(usb_reset(h_device) != APP_ERR_NONE)
158
    fprintf(stderr, "Failed to reset USB Blaster\n");
159
 
160
  usb_close(h_device);
161
 
162
  // Wait for reset!!!
163
  sleep(1);
164
 
165
  // Do device initialization
166
  if(err |= usbblaster_enumerate_bus())
167
    return err;
168
 
169
  h_device = usb_open(usbblaster_device);
170
  if(h_device == NULL)
171
    {
172
      fprintf(stderr, "Init failed to open USB device for initialization\n");
173
      return APP_ERR_USB;
174
    }
175
 
176
  // set the configuration
177
  if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue))
178
    {
179
      usb_close(h_device);
180
      fprintf(stderr, "USB-reset failed to set configuration\n");
181
      return APP_ERR_NONE;
182
    }
183
 
184
  while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
185
 
186
  //usb_clear_halt(h_device, EP1);
187
  //usb_clear_halt(h_device, EP2);
188
 
189
  // IMPORTANT:  DO NOT SEND A REQUEST TYPE "CLASS" OR TYPE "RESERVED".  This may stall the EP.
190
 
191
  // Some clones need this before they will start processing IN/OUT requests
192
  if(usbblaster_start_interface(h_device) != APP_ERR_NONE)
193
    fprintf(stderr, "Failed to start remote interface\n");
194
 
195
  uint16_t buf;
196
  if(err |= usbblaster_read_firmware_version(h_device, &buf))
197
    {
198
      usb_close(h_device);
199
      fprintf(stderr, "Failed to read firmware version\n");
200
      return err;
201
    }
202
  else
203
    {
204
      printf("firmware version = 0x%04X (%u)\n", buf, buf);
205
    }
206
 
207
 
208
  // USB blaster is expecting us to read 2 bytes, which are useless to us...
209
  char ret[2];
210
  int rv = usb_bulk_read(h_device, EP1, ret, 2, USB_TIMEOUT);
211
  if (rv < 0){  // But if we fail, who cares?
212
    fprintf(stderr, "\nWarning: Failed to read post-init bytes from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
213
  }
214
 
215
  if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
216
    usb_close(h_device);
217
    fprintf(stderr, "USB-out failed to release interface\n");
218
    return APP_ERR_USB;
219
  }
220
 
221
  usb_close(h_device);
222
 
223
  data_out_scratchpad = (char *) malloc(64);
224
  data_out_scratchpad_size = 64;
225
  data_in_scratchpad = (char *) malloc(64);
226
  data_in_scratchpad_size = 64;
227
 
228
  return APP_ERR_NONE;
229
}
230
 
231
 
232
int cable_usbblaster_out(uint8_t value)
233
{
234
  int             rv;                  // to catch return values of functions
235
  usb_dev_handle *h_device;            // handle on the ubs device
236
  char out;
237
  int err = APP_ERR_NONE;
238
 
239
  // open the device
240
  h_device = usb_open(usbblaster_device);
241
  if (h_device == NULL){
242
    usb_close(h_device);
243
    fprintf(stderr, "USB-out failed to open device\n");
244
    return APP_ERR_USB;
245
  }
246
 
247
  // set the configuration
248
  if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue))
249
    {
250
      usb_close(h_device);
251
      fprintf(stderr, "USB-out failed to set configuration\n");
252
      return APP_ERR_USB;
253
    }
254
 
255
  // wait until device is ready
256
  while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
257
 
258
  out = (USBBLASTER_CMD_OE | USBBLASTER_CMD_nCS);  // Set output enable (appears necessary) and nCS (necessary for byte-shift reads)
259
 
260
  // Translate to USB blaster protocol
261
  // USB-Blaster has no TRST pin
262
  if(value & TCLK_BIT)
263
    out |= USBBLASTER_CMD_TCK;
264
  if(value & TDI_BIT)
265
    out |= USBBLASTER_CMD_TDI;
266
  if(value & TMS_BIT)
267
    out |= USBBLASTER_CMD_TMS;
268
 
269
 
270
  rv = usb_bulk_write(h_device, EP2, &out, 1, USB_TIMEOUT);
271
  if (rv != 1){
272
    fprintf(stderr, "\nFailed to write to the FIFO (rv = %d):\n%s", rv, usb_strerror());
273
    err |= APP_ERR_USB;
274
  }
275
 
276
  // release the interface cleanly
277
  if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
278
    fprintf(stderr, "Warning: failed to release usb interface after write\n");
279
    err |= APP_ERR_USB;
280
  }
281
 
282
  // close the device
283
  usb_close(h_device);
284
  return err;
285
}
286
 
287
 
288
int cable_usbblaster_inout(uint8_t value, uint8_t *in_bit)
289
{
290
  int             rv;                  // to catch return values of functions
291
  usb_dev_handle *h_device;            // handle on the usb device
292
  char ret[3] = {0,0,0};               // Two useless bytes (0x31,0x60) always precede the useful byte
293
  char out;
294
 
295
  out = (USBBLASTER_CMD_OE | USBBLASTER_CMD_nCS);  // Set output enable (?) and nCS (necessary for byte-shift reads)
296
  out |=  USBBLASTER_CMD_READ;
297
 
298
  // Translate to USB blaster protocol
299
  // USB-Blaster has no TRST pin
300
  if(value & TCLK_BIT)
301
    out |= USBBLASTER_CMD_TCK;
302
  if(value & TDI_BIT)
303
    out |= USBBLASTER_CMD_TDI;
304
  if(value & TMS_BIT)
305
    out |= USBBLASTER_CMD_TMS;
306
 
307
 
308
  // open the device
309
  h_device = usb_open(usbblaster_device);
310
  if (h_device == NULL){
311
    return APP_ERR_USB;
312
  }
313
 
314
  // set the configuration
315
  if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue)){
316
    usb_close(h_device);
317
    return APP_ERR_USB;
318
  }
319
 
320
  // wait until device is ready
321
  while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
322
 
323
  // Send a read request
324
  rv = usb_bulk_write(h_device, EP2, &out, 1, USB_TIMEOUT);
325
  if (rv != 1){
326
    fprintf(stderr, "\nFailed to write a read request to the EP2 FIFO:\n%s", usb_strerror());
327
    usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
328
    usb_close(h_device);
329
    return APP_ERR_USB;
330
  }
331
 
332
 
333
  // receive the response
334
  // Sometimes, we do a read but just get the useless 0x31,0x60 chars...
335
  // retry until we get a 3rd byte (with real data), for a reasonable number of retries.
336
  int retries = 0;
337
  do {
338
    rv = usb_bulk_read(h_device, EP1, ret, 3, USB_TIMEOUT);
339
    if (rv < 0){
340
      fprintf(stderr, "\nFailed to read from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
341
      usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
342
      usb_close(h_device);
343
      return APP_ERR_USB;
344
    }
345
 
346
    // fprintf(stderr, "Read %i bytes: 0x%X, 0x%X, 0x%X\n", rv, ret[0], ret[1], ret[2]);
347
    retries++;
348
  }
349
  while((rv < 3) && (retries < 20));
350
 
351
 
352
  // release the interface cleanly
353
  if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
354
    fprintf(stderr, "Warning: failed to release USB interface after read\n");
355
    usb_close(h_device);
356
    return APP_ERR_USB;
357
  }
358
 
359
  // close the device
360
  usb_close(h_device);
361
 
362
  *in_bit = (ret[2] & 0x01); /* TDO is bit 0.  USB-Blaster may also set bit 1. */
363
  return APP_ERR_NONE;
364
}
365
 
366
 
367
// The usbblaster transfers the bits in the stream in the following order:
368
// bit 0 of the first byte received ... bit 7 of the first byte received
369
// bit 0 of second byte received ... etc.
370
int cable_usbblaster_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
371
  int             rv;                  // to catch return values of functions
372
  usb_dev_handle *h_device;            // handle on the ubs device
373
  unsigned int bytes_to_transfer, leftover_bit_length;
374
  uint32_t leftover_bits;
375
  unsigned char i;
376
  int err = APP_ERR_NONE;
377
 
378
  //printf("cable_usbblaster_write_stream(0x%X, %d, %i)\n", stream, len, set_last_bit);
379
 
380
  // This routine must transfer at least 8 bits.  Additionally, TMS (the last bit)
381
  // cannot be set by 'byte shift mode'.  So we need at least 8 bits to transfer,
382
  // plus one bit to send along with TMS.
383
  bytes_to_transfer = len_bits / 8;
384
  leftover_bit_length = len_bits - (bytes_to_transfer * 8);
385
 
386
  if((!leftover_bit_length) && set_last_bit) {
387
    bytes_to_transfer -= 1;
388
    leftover_bit_length += 8;
389
  }
390
 
391
  //printf("bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
392
 
393
  // Not enough bits for high-speed transfer. bit-bang.
394
  if(bytes_to_transfer == 0) {
395
    return cable_common_write_stream(stream, len_bits, set_last_bit);
396
  }
397
 
398
  // Bitbang functions leave clock high.  USBBlaster assumes clock low at the start of a burst.
399
  // Lower the clock.
400
  err |= cable_usbblaster_out(0);
401
 
402
  // Set leftover bits
403
  leftover_bits = (stream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
404
 
405
  //printf("leftover_bits: 0x%X, LSB_first_xfer = %d\n", leftover_bits, LSB_first_xfer);
406
 
407
  // open the device
408
  h_device = usb_open(usbblaster_device);
409
  if (h_device == NULL){
410
    usb_close(h_device);
411
    fprintf(stderr, "USBBlaster_write_stream failed to open device\n");
412
    return APP_ERR_USB;
413
  }
414
 
415
  // set the configuration
416
  if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue))
417
  {
418
    usb_close(h_device);
419
    fprintf(stderr, "USBBlaster_write_stream failed to set configuration\n");
420
    return APP_ERR_USB;
421
  }
422
 
423
  // wait until device is ready
424
  while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
425
 
426
 
427
  // Copy stream into out.  Not pretty, but better than changing the interface to the upper layers;
428
  // 32 bits are easier to work with than 8 bits in upper layers.
429
  if(data_out_scratchpad_size < (bytes_to_transfer+1)) {
430
    free(data_out_scratchpad);
431
    data_out_scratchpad = (char *) malloc(bytes_to_transfer+1);  // free/malloc instead of realloc will save copy time
432
    if(data_out_scratchpad == NULL) {
433
      usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
434
      usb_close(h_device);
435
      return APP_ERR_MALLOC;
436
    }
437
    data_out_scratchpad_size = bytes_to_transfer+1;
438
  }
439
 
440
  data_out_scratchpad[0] = USBBLASTER_CMD_BYTESHIFT | (bytes_to_transfer & 0x3F);
441
  for(i = 0; i < bytes_to_transfer; i++) {
442
    data_out_scratchpad[i+1] = (stream[i>>2] >> (8*(i&0x3))) & 0xFF;
443
  }
444
 
445
 
446
  /*
447
    printf("Data packet: ");
448
    for(i = 0; i <= bytes_to_transfer; i++)
449
    printf("0x%X ", out[i]);
450
    printf("\n");
451
  */
452
 
453
  rv = usb_bulk_write(h_device, EP2, data_out_scratchpad, bytes_to_transfer+1, USB_TIMEOUT);
454
  if (rv != (bytes_to_transfer+1)){
455
    fprintf(stderr, "\nFailed to write to the FIFO (rv = %d):\n%s", rv, usb_strerror());
456
    err |= APP_ERR_USB;
457
  }
458
 
459
  // release the interface cleanly
460
  if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
461
        fprintf(stderr, "Warning: failed to release usb interface after stream write\n");
462
  }
463
 
464
  // close the device
465
  usb_close(h_device);
466
 
467
  // if we have a number of bits not divisible by 8, or we need to set TMS...
468
  if(leftover_bit_length != 0) {
469
    //printf("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
470
    return cable_common_write_stream(&leftover_bits, leftover_bit_length, set_last_bit);
471
  }
472
 
473
  return err;
474
}
475
 
476
 
477
int cable_usbblaster_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
478
  int             rv;                  // to catch return values of functions
479
  usb_dev_handle *h_device;            // handle on the ubs device
480
  unsigned int bytes_received = 0;
481
  unsigned int bytes_to_transfer, leftover_bit_length;
482
  uint32_t leftover_bits, leftovers_received = 0;
483
  unsigned char i;
484
  int retval = APP_ERR_NONE;
485
 
486
  debug("cable_usbblaster_read_stream(0x%X, %d, %i)\n", outstream[0], len_bits, set_last_bit);
487
 
488
  // This routine must transfer at least 8 bits.  Additionally, TMS (the last bit)
489
  // cannot be set by 'byte shift mode'.  So we need at least 8 bits to transfer,
490
  // plus one bit to send along with TMS.
491
  bytes_to_transfer = len_bits / 8;
492
  leftover_bit_length = len_bits - (bytes_to_transfer * 8);
493
 
494
  if((!leftover_bit_length) && set_last_bit) {
495
    bytes_to_transfer -= 1;
496
    leftover_bit_length += 8;
497
  }
498
 
499
  //printf("RD bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
500
 
501
  // Not enough bits for high-speed transfer. bit-bang.
502
  if(bytes_to_transfer == 0) {
503
    return cable_common_read_stream(outstream, instream, len_bits, set_last_bit);
504
    //retval |= cable_common_read_stream(&leftover_bits, &leftovers_received, leftover_bit_length, set_last_bit);
505
  }
506
 
507
  // Bitbang functions leave clock high.  USBBlaster assumes clock low at the start of a burst.
508
  // Lower the clock.
509
  retval |= cable_usbblaster_out(0);
510
 
511
  // Zero the input, since we add new data by logical-OR
512
  for(i = 0; i < (len_bits/32); i++)
513
    instream[i] = 0;
514
  if(len_bits % 32)
515
    instream[i] = 0;
516
 
517
  // Set leftover bits
518
  leftover_bits = (outstream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
519
  debug("leftover_bits: 0x%X\n", leftover_bits);
520
 
521
  // open the device
522
  h_device = usb_open(usbblaster_device);
523
  if (h_device == NULL){
524
        usb_close(h_device);
525
        fprintf(stderr, "USBBlaster_read_stream failed to open device\n");
526
        return APP_ERR_USB;
527
  }
528
 
529
  // set the configuration
530
  if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue))
531
  {
532
        usb_close(h_device);
533
        fprintf(stderr, "USBBlaster_read_stream failed to set configuration\n");
534
        return APP_ERR_USB;
535
  }
536
 
537
  // wait until device is ready
538
  while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
539
 
540
 
541
 
542
  // Copy stream into out.  Not pretty, but better than changing the interface to the upper layers;
543
  // 32 bits are easier to work with than 8 bits in upper layers.
544
  if(data_out_scratchpad_size < (bytes_to_transfer+1)) {
545
    free(data_out_scratchpad);
546
    data_out_scratchpad = (char *) malloc(bytes_to_transfer+1);  // free/malloc instead of realloc will save copy time
547
    if(data_out_scratchpad == NULL) {
548
      usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
549
      usb_close(h_device);
550
      return APP_ERR_MALLOC;
551
    }
552
    data_out_scratchpad_size = bytes_to_transfer+1;
553
  }
554
 
555
  data_out_scratchpad[0] = USBBLASTER_CMD_BYTESHIFT | USBBLASTER_CMD_READ | (bytes_to_transfer & 0x3F);  // Set command byte
556
  for(i = 0; i < bytes_to_transfer; i++) {
557
    data_out_scratchpad[i+1] = (outstream[i>>2] >> (8*(i&0x3))) & 0xFF;
558
  }
559
 
560
  /*
561
  debug("Data packet: ");
562
  for(i = 0; i <= bytes_to_transfer; i++)
563
    debug("0x%X ", data_out_scratchpad[i]);
564
  debug("\n");
565
  */
566
 
567
  rv = usb_bulk_write(h_device, EP2, data_out_scratchpad, bytes_to_transfer+1, USB_TIMEOUT);
568
  if (rv != (bytes_to_transfer+1)){
569
    fprintf(stderr, "\nFailed to write to the EP2 FIFO (rv = %d):\n%s", rv, usb_strerror());
570
    usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
571
    usb_close(h_device);
572
    return APP_ERR_USB;
573
  }
574
 
575
 
576
  // Make sure we have a big-enough buffer to hold the incoming data
577
  if(data_in_scratchpad_size < (bytes_to_transfer+2)) {
578
    free(data_in_scratchpad);
579
    data_in_scratchpad = (char *) malloc(bytes_to_transfer+2);  // free/malloc instead of realloc will save copy time
580
    if(data_in_scratchpad == NULL) {
581
      usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
582
      usb_close(h_device);
583
      return APP_ERR_MALLOC;
584
    }
585
    data_in_scratchpad_size = (bytes_to_transfer+2);
586
  }
587
 
588
  // receive the response
589
  // Sometimes, we do a read but just get the useless 0x31,0x60 chars...
590
  // retry until we get at least 3 bytes (with real data), for a reasonable number of retries.
591
  int retries = 0;
592
  bytes_received = 0;
593
  do {
594
    rv = usb_bulk_read(h_device, EP1, data_in_scratchpad, (bytes_to_transfer-bytes_received)+2, USB_TIMEOUT);
595
    if (rv < 0){
596
      fprintf(stderr, "\nFailed to read stream from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
597
      usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber);
598
      usb_close(h_device);
599
      return APP_ERR_USB;
600
    }
601
 
602
    /*
603
    debug("Read %i bytes: ", rv);
604
    for(i = 0; i < rv; i++)
605
      debug("0x%X ", data_in_scratchpad[i]);
606
    debug("\n");
607
    */
608
 
609
    if(rv > 2) retries = 0;
610
    else retries++;
611
 
612
    /* Put the received bytes into the return stream.  */
613
    for(i = 0; i < (rv-2); i++) {
614
      // Do size/type promotion before shift.  Must cast to unsigned, else the value may be
615
      // sign-extended through the upper 16 bits of the uint32_t.
616
      uint32_t tmp = (unsigned char) data_in_scratchpad[2+i];
617
      instream[(bytes_received+i)>>2] |= (tmp << ((i & 0x3)*8));
618
    }
619
 
620
    bytes_received += (rv-2);
621
  }
622
  while((bytes_received < bytes_to_transfer) && (retries < 15));
623
 
624
 
625
  // release the interface cleanly
626
  if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
627
        fprintf(stderr, "Warning: failed to release usb interface after stream read\n");
628
  }
629
 
630
  // close the device
631
  usb_close(h_device);
632
 
633
  // if we have a number of bits not divisible by 8
634
  if(leftover_bit_length != 0) {
635
    debug("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
636
    retval |= cable_common_read_stream(&leftover_bits, &leftovers_received, leftover_bit_length, set_last_bit);
637
    instream[bytes_to_transfer>>2] |= (leftovers_received & 0xFF) << (8*(bytes_to_transfer & 0x3));
638
  }
639
 
640
  return retval;
641
}
642
 
643
 
644
int cable_usbblaster_opt(int c, char *str)
645
{
646
  fprintf(stderr, "Unknown parameter '%c'\n", c);
647
  return APP_ERR_BAD_PARAM;
648
}
649
 
650
 
651
//
652
// libusb does not work with my 3C25, but it works with libfdti.
653
// Following code is ported from http://www.ixo.de/info/usb_jtag/
654
// ZXF, 2009-10-22
655
//
656
#ifdef __SUPPORT_FTDI_CABLES__
657
 
658
int usb_blaster_buf_write(uint8_t *buf, int size, uint32_t* bytes_written)
659
{
660
        int retval;
661
 
662
        debug("usb_blaster_buf_write %02X (%d)\n", buf[0], size);
663
 
664
        if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0) {
665
                *bytes_written = 0;
666
                printf("ftdi_write_data: %s\n", ftdi_get_error_string(&ftdic));
667
                return -1;
668
        } else {
669
                *bytes_written = retval;
670
                return 0;
671
        }
672
}
673
 
674
int usb_blaster_buf_read(uint8_t* buf, int size, uint32_t* bytes_read)
675
{
676
        int retval;
677
        int timeout = 100;
678
        *bytes_read = 0;
679
 
680
        while ((*bytes_read < size) && timeout--) {
681
                if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0) {
682
                        *bytes_read = 0;
683
                        printf("ftdi_read_data: %s\n", ftdi_get_error_string(&ftdic));
684
                        return -1;
685
                }
686
                *bytes_read += retval;
687
        }
688
 
689
        debug("usb_blaster_buf_read %02X (%d)\n", buf[0], *bytes_read);
690
 
691
        return 0;
692
}
693
 
694
/* The following code doesn't fully utilize the possibilities of the USB-Blaster. It
695
 * writes one byte per JTAG pin state change at a time; it doesn't even try to buffer
696
 * data up to the maximum packet size of 64 bytes.
697
 *
698
 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data bits
699
 * (bidirectional) in a single USB packet. A header byte has to be sent as the first
700
 * byte in a packet with the following meaning:
701
 *
702
 *   Bit 7 (0x80): Must be set to indicate byte-shift mode.
703
 *   Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
704
 *   Bit 5..0:     Define the number N of following bytes
705
 *
706
 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was set,
707
 * it will afterwards return N bytes with TDO data read while clocking out the TDI data.
708
 * LSB of the first byte after the header byte will appear first on TDI.
709
 */
710
 
711
/* Simple bit banging mode:
712
 *
713
 *   Bit 7 (0x80): Must be zero (see byte-shift mode above)
714
 *   Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO in return.
715
 *   Bit 5 (0x20): Unknown; for now, set to one.
716
 *   Bit 4 (0x10): TDI Output.
717
 *   Bit 3 (0x08): Unknown; for now, set to one.
718
 *   Bit 2 (0x04): Unknown; for now, set to one.
719
 *   Bit 1 (0x02): TMS Output.
720
 *   Bit 0 (0x01): TCK Output.
721
 *
722
 * For transmitting a single data bit, you need to write two bytes. Up to 64 bytes can be
723
 * combined in a single USB packet (but this is not done in the code below). It isn't
724
 * possible to read a data without transmitting data.
725
 */
726
 
727
#define FTDI_TCK    0
728
#define FTDI_TMS    1
729
#define FTDI_TDI    4
730
#define FTDI_READ   6
731
#define FTDI_SHMODE 7
732
#define FTDI_OTHERS ((1<<2)|(1<<3)|(1<<5))
733
 
734
void usb_blaster_write(int tck, int tms, int tdi)
735
{
736
        uint8_t buf[1];
737
        uint32_t count;
738
 
739
        debug("---- usb_blaster_write(%d,%d,%d)\n", tck,tms,tdi);
740
 
741
        buf[0] = FTDI_OTHERS | (tck?(1<<FTDI_TCK):0) | (tms?(1<<FTDI_TMS):0) | (tdi?(1<<FTDI_TDI):0);
742
        usb_blaster_buf_write(buf, 1, &count);
743
}
744
 
745
int usb_blaster_write_read(int tck, int tms, int tdi)
746
{
747
        uint8_t buf[1];
748
        uint32_t count;
749
 
750
        debug("++++ usb_blaster_write_read(%d,%d,%d)\n", tck,tms,tdi);
751
 
752
        buf[0] = FTDI_OTHERS | (tck?(1<<FTDI_TCK):0) | (tms?(1<<FTDI_TMS):0) | (tdi?(1<<FTDI_TDI):0) | (1<<FTDI_READ);
753
        usb_blaster_buf_write(buf, 1, &count);
754
        usb_blaster_buf_read(buf, 1, &count);
755
        return (buf[0]&1);
756
}
757
 
758
int ftdi_usb_blaster_out(uint8_t value)
759
{
760
        int    tck = 0;
761
        int    tms = 0;
762
        int    tdi = 0;
763
 
764
        // Translate to USB blaster protocol
765
        // USB-Blaster has no TRST pin
766
        if(value & TCLK_BIT)
767
                tck = 1;
768
        if(value & TDI_BIT)
769
                tdi = 1;;
770
        if(value & TMS_BIT)
771
                tms = 1;
772
 
773
        usb_blaster_write(tck, tms, tdi);
774
 
775
        return 0;
776
}
777
 
778
int ftdi_usb_blaster_inout(uint8_t value, uint8_t *in_bit)
779
{
780
        int    tck = 0;
781
        int    tms = 0;
782
        int    tdi = 0;
783
 
784
        // Translate to USB blaster protocol
785
        // USB-Blaster has no TRST pin
786
        if(value & TCLK_BIT)
787
                tck = 1;
788
        if(value & TDI_BIT)
789
                tdi = 1;
790
        if(value & TMS_BIT)
791
                tms = 1;
792
 
793
        *in_bit = usb_blaster_write_read(tck, tms, tdi);
794
 
795
        return 0;
796
}
797
 
798
int usb_blaster_speed(int speed)
799
{
800
        if(ftdi_set_baudrate(&ftdic, speed)<0) {
801
                printf("Can't set baud rate to max: %s\n", ftdi_get_error_string(&ftdic));
802
                return -1;
803
        }
804
 
805
        return 0;
806
}
807
 
808
int ftdi_usb_blaster_init(void)
809
{
810
        uint8_t latency_timer;
811
        int     speed = 300000;
812
 
813
        printf("'usb_blaster' interface using libftdi.\n");
814
        printf("Ported by Xianfeng (xianfeng.zeng@gmail.com), 2009\n");
815
 
816
        if (ftdi_init(&ftdic) < 0) {
817
                printf("ftdi_init failed!");
818
                return -1;
819
        }
820
 
821
        /* context, vendor id, product id */
822
        if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_PID) < 0) {
823
                printf("unable to open ftdi device: %s\n", ftdic.error_str);
824
                return -1;
825
        }
826
 
827
        if (ftdi_usb_reset(&ftdic) < 0) {
828
                printf("unable to reset ftdi device\n");
829
                return -1;
830
        }
831
 
832
        if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
833
                printf("unable to set latency timer\n");
834
                return -1;
835
        }
836
 
837
        if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
838
                printf("unable to get latency timer\n");
839
                return -1;
840
        } else {
841
                printf("current latency timer: %i\n", latency_timer);
842
        }
843
 
844
        ftdi_disable_bitbang(&ftdic);
845
 
846
        printf("Baudrate:%d\n", speed);
847
        usb_blaster_speed(speed);
848
 
849
        return 0;
850
}
851
 
852
int ftdi_usb_blaster_quit(void)
853
{
854
        ftdi_usb_close(&ftdic);
855
        ftdi_deinit(&ftdic);
856
 
857
        return 0;
858
}
859
#endif
860
 

powered by: WebSVN 2.1.0

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