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_ft245.c] - Blame information for rev 58

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 55 nyawn
/* cable_usbblaster_ftdi.c - Alternate, libFTDI-based Altera USB Blaster driver
2
   for the Advanced JTAG Bridge.  Originally by Xianfeng Zheng.
3
   Copyright (C) 2009 Xianfeng Zeng
4
                 2009 - 2010 Nathan Yawn, nathan.yawn@opencores.org
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 as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
 
21
#include <stdio.h>
22
#include <sys/types.h>
23
#include <unistd.h>  // for usleep()
24
#include <stdlib.h>  // for sleep()
25
#include <arpa/inet.h> // for htons()
26
#include <sys/time.h>
27
#include <time.h>
28
#include <string.h>
29
 
30
#include "ftdi.h"  // libftdi header
31
 
32
#include "cable_ft245.h"
33
#include "errcodes.h"
34
 
35
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
36
 
37
jtag_cable_t ft245_cable_driver = {
38
    .name = "ft245",
39
    .inout_func = cable_ft245_inout,
40
    .out_func = cable_ft245_out,
41
    .init_func =cable_ft245_init ,
42
    .opt_func = cable_ft245_opt,
43
    .bit_out_func = cable_common_write_bit,
44
    .bit_inout_func = cable_common_read_write_bit,
45
#if 1
46
    .stream_out_func = cable_ft245_write_stream ,
47
    .stream_inout_func = cable_ft245_read_stream,
48
#else
49
    .stream_out_func = cable_common_write_stream ,
50
    .stream_inout_func = cable_common_read_stream,
51
#endif
52
    .flush_func = NULL,
53 58 nyawn
    .opts = "p:v:",
54
    .help = "-p [PID] Alteranate PID for USB device (hex value)\n\t-v [VID] Alternate VID for USB device (hex value)\n",
55 55 nyawn
};
56
 
57
// USBBlaster has a max. single transaction of 63 bytes.  We assume
58
// the FT245 has the same limit.
59
// So, size the max read and write to create 64-byte USB packets
60
#define USBBLASTER_MAX_WRITE 63
61
static uint8_t data_out_scratchpad[USBBLASTER_MAX_WRITE+1];
62
#define USBBLASTER_MAX_READ  62
63
static uint8_t data_in_scratchpad[USBBLASTER_MAX_READ+2];
64
 
65 58 nyawn
// USB constants for the USB Blaster, can be changed on the command line
66
static uint32_t ALTERA_VID = 0X09FB;
67
static uint32_t ALTERA_PID = 0x6001;
68 55 nyawn
 
69 58 nyawn
 
70 55 nyawn
static struct ftdi_context ftdic;
71
 
72
///////////////////////////////////////////////////////////////////////////////
73
/*-------------------------------------[ USB Blaster specific functions ]---*/
74
/////////////////////////////////////////////////////////////////////////////
75
 
76
//
77
// libusb does not work with my 3C25, but it works with libfdti.
78
// Following code is ported from http://www.ixo.de/info/usb_jtag/
79
// ZXF, 2009-10-22
80
//
81
 
82
int usb_blaster_buf_write(uint8_t *buf, int size, uint32_t* bytes_written)
83
{
84
        int retval;
85
 
86
        debug("ft245 usb_blaster_buf_write %02X (%d)\n", buf[0], size);
87
 
88
        if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0) {
89
                *bytes_written = 0;
90
                printf("ftdi_write_data: %s\n", ftdi_get_error_string(&ftdic));
91
                return -1;
92
        } else {
93
                *bytes_written = retval;
94
                return 0;
95
        }
96
}
97
 
98
int usb_blaster_buf_read(uint8_t* buf, int size, uint32_t* bytes_read)
99
{
100
        int retval;
101
        int timeout = 100;
102
        *bytes_read = 0;
103
 
104
        while ((*bytes_read < size) && timeout--) {
105
                if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0) {
106
                        *bytes_read = 0;
107
                        printf("ftdi_read_data: %s\n", ftdi_get_error_string(&ftdic));
108
                        return -1;
109
                }
110
                *bytes_read += retval;
111
        }
112
 
113
        debug("ft245 usb_blaster_buf_read %02X (%d)\n", buf[0], *bytes_read);
114
 
115
        return 0;
116
}
117
 
118
/* The following code doesn't fully utilize the possibilities of the USB-Blaster. It
119
 * writes one byte per JTAG pin state change at a time; it doesn't even try to buffer
120
 * data up to the maximum packet size of 64 bytes.
121
 *
122
 * The USB-Blaster offers a byte-shift mode to transmit up to 504 data bits
123
 * (bidirectional) in a single USB packet. A header byte has to be sent as the first
124
 * byte in a packet with the following meaning:
125
 *
126
 *   Bit 7 (0x80): Must be set to indicate byte-shift mode.
127
 *   Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
128
 *   Bit 5..0:     Define the number N of following bytes
129
 *
130
 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was set,
131
 * it will afterwards return N bytes with TDO data read while clocking out the TDI data.
132
 * LSB of the first byte after the header byte will appear first on TDI.
133
 */
134
 
135
/* Simple bit banging mode:
136
 *
137
 *   Bit 7 (0x80): Must be zero (see byte-shift mode above)
138
 *   Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO in return.
139
 *   Bit 5 (0x20): Unknown; for now, set to one.
140
 *   Bit 4 (0x10): TDI Output.
141
 *   Bit 3 (0x08): Unknown; for now, set to one.
142
 *   Bit 2 (0x04): Unknown; for now, set to one.
143
 *   Bit 1 (0x02): TMS Output.
144
 *   Bit 0 (0x01): TCK Output.
145
 *
146
 * For transmitting a single data bit, you need to write two bytes. Up to 64 bytes can be
147
 * combined in a single USB packet (but this is not done in the code below). It isn't
148
 * possible to read a data without transmitting data.
149
 */
150
 
151
#define FTDI_TCK    0
152
#define FTDI_TMS    1
153
#define FTDI_TDI    4
154
#define FTDI_READ   6
155
#define FTDI_SHMODE 7
156
#define FTDI_OTHERS ((1<<2)|(1<<3)|(1<<5))
157
 
158
void usb_blaster_write(int tck, int tms, int tdi)
159
{
160
        uint8_t buf[1];
161
        uint32_t count;
162
 
163
        debug("---- usb_blaster_write(%d,%d,%d)\n", tck,tms,tdi);
164
 
165
        buf[0] = FTDI_OTHERS | (tck?(1<<FTDI_TCK):0) | (tms?(1<<FTDI_TMS):0) | (tdi?(1<<FTDI_TDI):0);
166
        usb_blaster_buf_write(buf, 1, &count);
167
}
168
 
169
int usb_blaster_write_read(int tck, int tms, int tdi)
170
{
171
        uint8_t buf[1];
172
        uint32_t count;
173
 
174
        debug("++++ usb_blaster_write_read(%d,%d,%d)\n", tck,tms,tdi);
175
 
176
        buf[0] = FTDI_OTHERS | (tck?(1<<FTDI_TCK):0) | (tms?(1<<FTDI_TMS):0) | (tdi?(1<<FTDI_TDI):0) | (1<<FTDI_READ);
177
        usb_blaster_buf_write(buf, 1, &count);
178
        usb_blaster_buf_read(buf, 1, &count);
179
        return (buf[0]&1);
180
}
181
 
182
int usb_blaster_speed(int speed)
183
{
184
        if(ftdi_set_baudrate(&ftdic, speed)<0) {
185
                printf("Can't set baud rate to max: %s\n", ftdi_get_error_string(&ftdic));
186
                return -1;
187
        }
188
 
189
        return 0;
190
}
191
 
192
int ftdi_usb_blaster_quit(void)
193
{
194
        ftdi_usb_close(&ftdic);
195
        ftdi_deinit(&ftdic);
196
 
197
        return 0;
198
}
199
 
200
// ---------- adv_jtag_bridge interface functions ------------------
201
// The stream functions below *do* use the full potential of the FT245
202
// USB-Blaster.  Up 63 bytes can be written in a single USB transaction,
203
// and 62 can be read back.  It's possible that libFTDI can handle arbitrary-
204
// sized writes, but we break up reads and writes into single-transaction
205
// chunks here.
206
//
207
// The usbblaster transfers the bits in the stream in the following order:
208
// bit 0 of the first byte received ... bit 7 of the first byte received
209
// bit 0 of second byte received ... etc.
210
int cable_ft245_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
211
  int             rv;                  // to catch return values of functions
212
  uint32_t count;
213
  unsigned int bytes_to_transfer, leftover_bit_length;
214
  uint32_t leftover_bits;
215
  int bytes_remaining;
216
  char *xfer_ptr;
217
  int err = APP_ERR_NONE;
218
 
219
  debug("cable_ft245_write_stream(0x%X, %d, %i)\n", stream, len, set_last_bit);
220
 
221
  // This routine must transfer at least 8 bits.  Additionally, TMS (the last bit)
222
  // cannot be set by 'byte shift mode'.  So we need at least 8 bits to transfer,
223
  // plus one bit to send along with TMS.
224
  bytes_to_transfer = len_bits / 8;
225
  leftover_bit_length = len_bits - (bytes_to_transfer * 8);
226
 
227
  if((!leftover_bit_length) && set_last_bit) {
228
    bytes_to_transfer -= 1;
229
    leftover_bit_length += 8;
230
  }
231
 
232
  debug("bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
233
 
234
  // Not enough bits for high-speed transfer. bit-bang.
235
  if(bytes_to_transfer == 0) {
236
    return cable_common_write_stream(stream, len_bits, set_last_bit);
237
  }
238
 
239
  // Bitbang functions leave clock high.  USBBlaster assumes clock low at the start of a burst.
240
  err |= cable_ft245_out(0);  // Lower the clock.
241
 
242
  // Set leftover bits
243
  leftover_bits = (stream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
244
 
245
  debug("leftover_bits: 0x%X, LSB_first_xfer = %d\n", leftover_bits, LSB_first_xfer);
246
 
247
  bytes_remaining = bytes_to_transfer;
248
  xfer_ptr = (char *) stream;
249
  while(bytes_remaining > 0)
250
    {
251
      int bytes_this_xfer = (bytes_remaining > USBBLASTER_MAX_WRITE) ? USBBLASTER_MAX_WRITE:bytes_remaining;
252
 
253
      data_out_scratchpad[0] = (1<<FTDI_SHMODE) | (bytes_this_xfer & 0x3F);
254
      memcpy(&data_out_scratchpad[1], xfer_ptr, bytes_this_xfer);
255
 
256
      /* printf("Data packet: ");
257
         for(i = 0; i <= bytes_to_transfer; i++)
258
         printf("0x%X ", out[i]);
259
         printf("\n"); */
260
 
261
      rv = usb_blaster_buf_write(data_out_scratchpad, bytes_this_xfer+1, &count);
262
      if (count != (bytes_this_xfer+1)){
263
        fprintf(stderr, "\nFailed to write to the FIFO (count = %d)", rv);
264
        err |= APP_ERR_USB;
265
        break;
266
      }
267
 
268
      bytes_remaining -= bytes_this_xfer;
269
      xfer_ptr += bytes_this_xfer;
270
    }
271
 
272
  // if we have a number of bits not divisible by 8, or we need to set TMS...
273
  if(leftover_bit_length != 0) {
274
    //printf("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
275
    return cable_common_write_stream(&leftover_bits, leftover_bit_length, set_last_bit);
276
  }
277
 
278
  return err;
279
}
280
 
281
 
282
int cable_ft245_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
283
  int             rv;                  // to catch return values of functions
284
  uint32_t count;
285
  unsigned int bytes_received = 0, total_bytes_received = 0;
286
  unsigned int bytes_to_transfer, leftover_bit_length;
287
  uint32_t leftover_bits, leftovers_received = 0;
288
  unsigned char i;
289
  int bytes_remaining;
290
  char *xfer_ptr;
291
  int retval = APP_ERR_NONE;
292
 
293
  debug("cable_usbblaster_read_stream(0x%X, %d, %i)\n", outstream[0], len_bits, set_last_bit);
294
 
295
  // This routine must transfer at least 8 bits.  Additionally, TMS (the last bit)
296
  // cannot be set by 'byte shift mode'.  So we need at least 8 bits to transfer,
297
  // plus one bit to send along with TMS.
298
  bytes_to_transfer = len_bits / 8;
299
  leftover_bit_length = len_bits - (bytes_to_transfer * 8);
300
 
301
  if((!leftover_bit_length) && set_last_bit) {
302
    bytes_to_transfer -= 1;
303
    leftover_bit_length += 8;
304
  }
305
 
306
  debug("RD bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
307
 
308
  // Not enough bits for high-speed transfer. bit-bang.
309
  if(bytes_to_transfer == 0) {
310
    return cable_common_read_stream(outstream, instream, len_bits, set_last_bit);
311
  }
312
 
313
  // Bitbang functions leave clock high.  USBBlaster assumes clock low at the start of a burst.
314
  // Lower the clock.
315
  retval |= cable_ft245_out(0);
316
 
317
  // Zero the input, since we add new data by logical-OR
318
  for(i = 0; i < (len_bits/32); i++)  instream[i] = 0;
319
  if(len_bits % 32)                   instream[i] = 0;
320
 
321
  // Set leftover bits
322
  leftover_bits = (outstream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
323
  debug("leftover_bits: 0x%X\n", leftover_bits);
324
 
325
  // Transfer the data.  USBBlaster has a max transfer size of 64 bytes.
326
  bytes_remaining = bytes_to_transfer;
327
  xfer_ptr = (char *) outstream;
328
  total_bytes_received = 0;
329
  while(bytes_remaining > 0)
330
    {
331
      int bytes_this_xfer = (bytes_remaining > USBBLASTER_MAX_READ) ? USBBLASTER_MAX_READ:bytes_remaining;
332
      data_out_scratchpad[0] = (1<<FTDI_SHMODE) | (1<<FTDI_READ) | (bytes_this_xfer & 0x3F);
333
      memcpy(&data_out_scratchpad[1], xfer_ptr, bytes_this_xfer);
334
 
335
      /* debug("Data packet: ");
336
         for(i = 0; i <= bytes_to_transfer; i++) debug("0x%X ", data_out_scratchpad[i]);
337
         debug("\n"); */
338
 
339
      rv = usb_blaster_buf_write(data_out_scratchpad, bytes_this_xfer+1, &count);
340
      if (count != (bytes_this_xfer+1)){
341
        fprintf(stderr, "\nFailed to write to the EP2 FIFO (count = %d)\n", count);
342
        return APP_ERR_USB;
343
      }
344
 
345
      // receive the response
346
      // libFTDI removes the excess 0x31 0x60 chars for us.
347
      int retries = 0;
348
      bytes_received = 0;
349
      do {
350
        debug("stream read, bytes_this_xfer = %i, bytes_received = %i\n", bytes_this_xfer, bytes_received);
351
        rv = usb_blaster_buf_read(data_in_scratchpad, (bytes_this_xfer-bytes_received), &count);
352
        if (rv < 0){
353
          fprintf(stderr, "\nFailed to read stream from the EP1 FIFO (%i)\n", rv);
354
          return APP_ERR_USB;
355
        }
356
 
357
        /* debug("Read %i bytes: ", rv);
358
           for(i = 0; i < rv; i++)
359
           debug("0x%X ", data_in_scratchpad[i]);
360
           debug("\n"); */
361
 
362
        if(count > 0) retries = 0;
363
        else retries++;
364
 
365
        /* Put the received bytes into the return stream.  Works for either endian. */
366
        for(i = 0; i < count; i++) {
367
          // Do size/type promotion before shift.  Must cast to unsigned, else the value may be
368
          // sign-extended through the upper 16 bits of the uint32_t.
369
          uint32_t tmp = (unsigned char) data_in_scratchpad[i];
370
          instream[(total_bytes_received+i)>>2] |= (tmp << ((i & 0x3)*8));
371
        }
372
 
373
        bytes_received += count;
374
        total_bytes_received += count;
375
      }
376
      while((bytes_received < bytes_this_xfer) && (retries < 15));
377
 
378
      bytes_remaining -= bytes_this_xfer;
379
      xfer_ptr += bytes_this_xfer;
380
    }
381
 
382
  // if we have a number of bits not divisible by 8
383
  if(leftover_bit_length != 0) {
384
    debug("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
385
    retval |= cable_common_read_stream(&leftover_bits, &leftovers_received, leftover_bit_length, set_last_bit);
386
    instream[bytes_to_transfer>>2] |= (leftovers_received & 0xFF) << (8*(bytes_to_transfer & 0x3));
387
  }
388
 
389
  return retval;
390
}
391
 
392
 
393
int cable_ft245_out(uint8_t value)
394
{
395
        int    tck = 0;
396
        int    tms = 0;
397
        int    tdi = 0;
398
 
399
        // Translate to USB blaster protocol
400
        // USB-Blaster has no TRST pin
401
        if(value & TCLK_BIT)
402
                tck = 1;
403
        if(value & TDI_BIT)
404
                tdi = 1;;
405
        if(value & TMS_BIT)
406
                tms = 1;
407
 
408
        usb_blaster_write(tck, tms, tdi);
409
 
410
        return 0;
411
}
412
 
413
int cable_ft245_inout(uint8_t value, uint8_t *in_bit)
414
{
415
        int    tck = 0;
416
        int    tms = 0;
417
        int    tdi = 0;
418
 
419
        // Translate to USB blaster protocol
420
        // USB-Blaster has no TRST pin
421
        if(value & TCLK_BIT)
422
                tck = 1;
423
        if(value & TDI_BIT)
424
                tdi = 1;
425
        if(value & TMS_BIT)
426
                tms = 1;
427
 
428
        *in_bit = usb_blaster_write_read(tck, tms, tdi);
429
 
430
        return 0;
431
}
432
 
433
int cable_ft245_init(void)
434
{
435
        uint8_t  latency_timer;
436
 
437
        printf("'usb_blaster' interface using libftdi\n");
438
 
439
        if (ftdi_init(&ftdic) < 0) {
440
                printf("ftdi_init failed!");
441
                return -1;
442
        }
443
 
444
        /* context, vendor id, product id */
445
        if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_PID) < 0) {
446 58 nyawn
                printf("unable to open ftdi device with VID 0x%0X, PID 0x%0X: %s\n", ALTERA_VID, ALTERA_PID, ftdic.error_str);
447 55 nyawn
                return -1;
448
        }
449
 
450
        if (ftdi_usb_reset(&ftdic) < 0) {
451
                printf("unable to reset ftdi device\n");
452
                return -1;
453
        }
454
 
455
        if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
456
                printf("unable to set latency timer\n");
457
                return -1;
458
        }
459
 
460
        if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
461
                printf("unable to get latency timer\n");
462
                return -1;
463
        } else {
464
                printf("current latency timer: %i\n", latency_timer);
465
        }
466
 
467
        ftdi_disable_bitbang(&ftdic);
468
 
469
        usb_blaster_speed(300000);
470
 
471
        return 0;
472
}
473
 
474
jtag_cable_t *cable_ft245_get_driver(void)
475
{
476
  return &ft245_cable_driver;
477
}
478
 
479
 
480
int cable_ft245_opt(int c, char *str)
481
{
482 58 nyawn
  uint32_t newvid;
483
  uint32_t newpid;
484
 
485
  switch(c) {
486
  case 'p':
487
    if(!sscanf(str, "%x", &newpid)) {
488
      fprintf(stderr, "p parameter must have a hex number as parameter\n");
489
      return APP_ERR_BAD_PARAM;
490
    }
491
    else {
492
      ALTERA_PID = newpid;
493
    }
494
    break;
495
 
496
  case 'v':
497
    if(!sscanf(str, "%x", &newvid)) {
498
      fprintf(stderr, "v parameter must have a hex number as parameter\n");
499
      return APP_ERR_BAD_PARAM;
500
    }
501
    else {
502
      ALTERA_VID = newvid;
503
    }
504
    break;
505
 
506
  default:
507
    fprintf(stderr, "Unknown parameter '%c'\n", c);
508
    return APP_ERR_BAD_PARAM;
509
  }
510
  return APP_ERR_NONE;
511 55 nyawn
}
512
 
513
 

powered by: WebSVN 2.1.0

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