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_ftdi.c] - Blame information for rev 32

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 32 nyawn
/* cable_usbblaster_ftdi.c - Alternate, libFTDI-basede 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
 
29
#include "ftdi.h"  // libftdi header
30
 
31
#include "cable_common.h"
32
#include "errcodes.h"
33
 
34
#warning Compiling alternate (FTDI-based) USB-Blaster driver -- LOW SPEED!
35
 
36
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
37
 
38
// USB constants for the USB Blaster
39
#define ALTERA_VID 0x09FB
40
#define ALTERA_PID 0x6001
41
 
42
static struct ftdi_context ftdic;
43
 
44
///////////////////////////////////////////////////////////////////////////////
45
/*-------------------------------------[ USB Blaster specific functions ]---*/
46
/////////////////////////////////////////////////////////////////////////////
47
 
48
//
49
// libusb does not work with my 3C25, but it works with libfdti.
50
// Following code is ported from http://www.ixo.de/info/usb_jtag/
51
// ZXF, 2009-10-22
52
//
53
 
54
int usb_blaster_buf_write(uint8_t *buf, int size, uint32_t* bytes_written)
55
{
56
        int retval;
57
 
58
        debug("usb_blaster_buf_write %02X (%d)\n", buf[0], size);
59
 
60
        if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0) {
61
                *bytes_written = 0;
62
                printf("ftdi_write_data: %s\n", ftdi_get_error_string(&ftdic));
63
                return -1;
64
        } else {
65
                *bytes_written = retval;
66
                return 0;
67
        }
68
}
69
 
70
int usb_blaster_buf_read(uint8_t* buf, int size, uint32_t* bytes_read)
71
{
72
        int retval;
73
        int timeout = 100;
74
        *bytes_read = 0;
75
 
76
        while ((*bytes_read < size) && timeout--) {
77
                if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0) {
78
                        *bytes_read = 0;
79
                        printf("ftdi_read_data: %s\n", ftdi_get_error_string(&ftdic));
80
                        return -1;
81
                }
82
                *bytes_read += retval;
83
        }
84
 
85
        debug("usb_blaster_buf_read %02X (%d)\n", buf[0], *bytes_read);
86
 
87
        return 0;
88
}
89
 
90
/* The following code doesn't fully utilize the possibilities of the USB-Blaster. It
91
 * writes one byte per JTAG pin state change at a time; it doesn't even try to buffer
92
 * data up to the maximum packet size of 64 bytes.
93
 *
94
 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data bits
95
 * (bidirectional) in a single USB packet. A header byte has to be sent as the first
96
 * byte in a packet with the following meaning:
97
 *
98
 *   Bit 7 (0x80): Must be set to indicate byte-shift mode.
99
 *   Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
100
 *   Bit 5..0:     Define the number N of following bytes
101
 *
102
 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was set,
103
 * it will afterwards return N bytes with TDO data read while clocking out the TDI data.
104
 * LSB of the first byte after the header byte will appear first on TDI.
105
 */
106
 
107
/* Simple bit banging mode:
108
 *
109
 *   Bit 7 (0x80): Must be zero (see byte-shift mode above)
110
 *   Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO in return.
111
 *   Bit 5 (0x20): Unknown; for now, set to one.
112
 *   Bit 4 (0x10): TDI Output.
113
 *   Bit 3 (0x08): Unknown; for now, set to one.
114
 *   Bit 2 (0x04): Unknown; for now, set to one.
115
 *   Bit 1 (0x02): TMS Output.
116
 *   Bit 0 (0x01): TCK Output.
117
 *
118
 * For transmitting a single data bit, you need to write two bytes. Up to 64 bytes can be
119
 * combined in a single USB packet (but this is not done in the code below). It isn't
120
 * possible to read a data without transmitting data.
121
 */
122
 
123
#define FTDI_TCK    0
124
#define FTDI_TMS    1
125
#define FTDI_TDI    4
126
#define FTDI_READ   6
127
#define FTDI_SHMODE 7
128
#define FTDI_OTHERS ((1<<2)|(1<<3)|(1<<5))
129
 
130
void usb_blaster_write(int tck, int tms, int tdi)
131
{
132
        uint8_t buf[1];
133
        uint32_t count;
134
 
135
        debug("---- usb_blaster_write(%d,%d,%d)\n", tck,tms,tdi);
136
 
137
        buf[0] = FTDI_OTHERS | (tck?(1<<FTDI_TCK):0) | (tms?(1<<FTDI_TMS):0) | (tdi?(1<<FTDI_TDI):0);
138
        usb_blaster_buf_write(buf, 1, &count);
139
}
140
 
141
int usb_blaster_write_read(int tck, int tms, int tdi)
142
{
143
        uint8_t buf[1];
144
        uint32_t count;
145
 
146
        debug("++++ usb_blaster_write_read(%d,%d,%d)\n", tck,tms,tdi);
147
 
148
        buf[0] = FTDI_OTHERS | (tck?(1<<FTDI_TCK):0) | (tms?(1<<FTDI_TMS):0) | (tdi?(1<<FTDI_TDI):0) | (1<<FTDI_READ);
149
        usb_blaster_buf_write(buf, 1, &count);
150
        usb_blaster_buf_read(buf, 1, &count);
151
        return (buf[0]&1);
152
}
153
 
154
int cable_usbblaster_out(uint8_t value)
155
{
156
        int    tck = 0;
157
        int    tms = 0;
158
        int    tdi = 0;
159
 
160
        // Translate to USB blaster protocol
161
        // USB-Blaster has no TRST pin
162
        if(value & TCLK_BIT)
163
                tck = 1;
164
        if(value & TDI_BIT)
165
                tdi = 1;;
166
        if(value & TMS_BIT)
167
                tms = 1;
168
 
169
        usb_blaster_write(tck, tms, tdi);
170
 
171
        return 0;
172
}
173
 
174
int cable_usbblaster_inout(uint8_t value, uint8_t *in_bit)
175
{
176
        int    tck = 0;
177
        int    tms = 0;
178
        int    tdi = 0;
179
 
180
        // Translate to USB blaster protocol
181
        // USB-Blaster has no TRST pin
182
        if(value & TCLK_BIT)
183
                tck = 1;
184
        if(value & TDI_BIT)
185
                tdi = 1;
186
        if(value & TMS_BIT)
187
                tms = 1;
188
 
189
        *in_bit = usb_blaster_write_read(tck, tms, tdi);
190
 
191
        return 0;
192
}
193
 
194
int usb_blaster_speed(int speed)
195
{
196
        if(ftdi_set_baudrate(&ftdic, speed)<0) {
197
                printf("Can't set baud rate to max: %s\n", ftdi_get_error_string(&ftdic));
198
                return -1;
199
        }
200
 
201
        return 0;
202
}
203
 
204
int cable_usbblaster_init(void)
205
{
206
        uint8_t  latency_timer;
207
 
208
        printf("'usb_blaster' interface using libftdi\n");
209
 
210
        if (ftdi_init(&ftdic) < 0) {
211
                printf("ftdi_init failed!");
212
                return -1;
213
        }
214
 
215
        /* context, vendor id, product id */
216
        if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_PID) < 0) {
217
                printf("unable to open ftdi device: %s\n", ftdic.error_str);
218
                return -1;
219
        }
220
 
221
        if (ftdi_usb_reset(&ftdic) < 0) {
222
                printf("unable to reset ftdi device\n");
223
                return -1;
224
        }
225
 
226
        if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
227
                printf("unable to set latency timer\n");
228
                return -1;
229
        }
230
 
231
        if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
232
                printf("unable to get latency timer\n");
233
                return -1;
234
        } else {
235
                printf("current latency timer: %i\n", latency_timer);
236
        }
237
 
238
        ftdi_disable_bitbang(&ftdic);
239
 
240
        usb_blaster_speed(300000);
241
 
242
        return 0;
243
}
244
 
245
int ftdi_usb_blaster_quit(void)
246
{
247
        ftdi_usb_close(&ftdic);
248
        ftdi_deinit(&ftdic);
249
 
250
        return 0;
251
}
252
 
253
 
254
/* Puts bitstream via bit-bang.  Copied directly from cable_common.c, for function-level compatibility
255
 * with the standard / high-speed usbblaster driver.
256
 */
257
int cable_usbblaster_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
258
  int i;
259
  int index = 0;
260
  int bits_this_index = 0;
261
  uint8_t out;
262
  int err = APP_ERR_NONE;
263
 
264
  debug("writeSrrm%d(", len_bits);
265
  for(i = 0; i < len_bits - 1; i++) {
266
    out = (stream[index] >> bits_this_index) & 1;
267
    err |= cable_write_bit(out);
268
    debug("%i", out);
269
    bits_this_index++;
270
    if(bits_this_index >= 32) {
271
      index++;
272
      bits_this_index = 0;
273
    }
274
  }
275
 
276
  out = (stream[index] >>(len_bits - 1)) & 0x1;
277
  if(set_last_bit) out |= TMS;
278
  err |= cable_write_bit(out);
279
  debug("%i)\n", out);
280
  return err;
281
}
282
 
283
/* Gets bitstream via bit-bang.  Copied directly from cable_common.c, for function-level compatibility
284
 * with the standard / high-speed usbblaster driver.
285
 */
286
int cable_usbblaster_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
287
  int i;
288
  int index = 0;
289
  int bits_this_index = 0;
290
  uint8_t inval, outval;
291
  int err = APP_ERR_NONE;
292
 
293
  instream[0] = 0;
294
 
295
  debug("readStrm%d(", len_bits);
296
  for(i = 0; i < (len_bits - 1); i++) {
297
    outval = (outstream[index] >> bits_this_index) & 0x1;
298
    err |= cable_read_write_bit(outval, &inval);
299
    debug("%i", inval);
300
    instream[index] |= (inval << bits_this_index);
301
    bits_this_index++;
302
    if(bits_this_index >= 32) {
303
      index++;
304
      bits_this_index = 0;
305
      instream[index] = 0;  // It's safe to do this, because there's always at least one more bit
306
    }
307
  }
308
 
309
  if (set_last_bit)
310
    outval = ((outstream[index] >> (len_bits - 1)) & 1) | TMS;
311
  else
312
    outval = (outstream[index] >> (len_bits - 1)) & 1;
313
 
314
  err |= cable_read_write_bit(outval, &inval);
315
  debug("%i", inval);
316
  instream[index] |= (inval << bits_this_index);
317
 
318
  debug(") = 0x%lX\n", instream[0]);
319
 
320
  return err;
321
}
322
 
323
 
324
 
325
 
326
int cable_usbblaster_opt(int c, char *str)
327
{
328
  fprintf(stderr, "Unknown parameter '%c'\n", c);
329
  return APP_ERR_BAD_PARAM;
330
}
331
 
332
 

powered by: WebSVN 2.1.0

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