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 55

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

powered by: WebSVN 2.1.0

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