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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 nyawn
/* cable_ft2232.c - FT2232 based cable driver for the Advanced JTAG Bridge
2
   Copyright (C) 2008 Arnim Laeuger, arniml@opencores.org
3
   Copyright (C) 2009 José Ignacio Villar, jose@dte.us.es
4
 
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 2 of the License, or
8
   (at your option) any later version.
9
 
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, write to the Free Software
17
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18
 
19
 
20
#include <time.h>
21
#include <sys/time.h>
22
#include <fcntl.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <stdlib.h>
26
#include <stdint.h>
27
#include <unistd.h>
28
#include <ftdi.h>
29 55 nyawn
 
30 21 nyawn
#include "cable_ft2232.h"
31
#include "errcodes.h"
32
int debug = 0;
33
 
34 55 nyawn
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
35
 
36
jtag_cable_t ft2232_cable_driver = {
37
    .name = "ft2232",
38
    .inout_func = NULL,
39
    .out_func = NULL,
40
    .init_func = cable_ftdi_init,
41
    .opt_func = cable_ftdi_opt,
42
    .bit_out_func = cable_ftdi_write_bit,
43
    .bit_inout_func = cable_ftdi_read_write_bit,
44
    .stream_out_func = cable_ftdi_write_stream,
45
    .stream_inout_func = cable_ftdi_read_stream,
46
    .flush_func = cable_ftdi_flush,
47 58 nyawn
    .opts = "p:v:",
48
    .help = "-p [PID] Alteranate PID for USB device (hex value)\n\t-v [VID] Alternate VID for USB device (hex value)\n",
49 55 nyawn
};
50
 
51
usbconn_t * usbconn_ftdi_connect();
52
int my_ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
53
char *my_ftdi_get_error_string (struct ftdi_context *ftdi);
54
int my_ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
55
int my_ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char* description, const char* serial);
56
void my_ftdi_deinit(struct ftdi_context *ftdi);
57
int my_ftdi_usb_purge_buffers(struct ftdi_context *ftdi);
58
int my_ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi);
59
int my_ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi);
60
int my_ftdi_usb_reset(struct ftdi_context *ftdi);
61
int my_ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency);
62
int my_ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate);
63
int my_ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
64
int my_ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize);
65
int my_ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable);
66
int my_ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable);
67
int my_ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
68
int my_ftdi_usb_close(struct ftdi_context *ftdi);
69
 
70 21 nyawn
static int usbconn_ftdi_common_open( usbconn_t *conn);
71
static void usbconn_ftdi_free( usbconn_t *conn );
72
static int seq_purge(struct ftdi_context *ftdic, int purge_rx, int purge_tx);
73
static int seq_reset(struct ftdi_context *ftdic);
74
static int usbconn_ftdi_flush( ftdi_param_t *params );
75
static int usbconn_ftdi_read( usbconn_t *conn, uint8_t *buf, int len );
76
static int usbconn_ftdi_write( usbconn_t *conn, uint8_t *buf, int len, int recv );
77
static int usbconn_ftdi_mpsse_open( usbconn_t *conn );
78
static int usbconn_ftdi_close(usbconn_t *conn);
79
 
80
usbconn_driver_t usbconn_ft2232_mpsse_driver = {
81
        "ftdi-mpsse",
82
        usbconn_ftdi_connect,
83
        usbconn_ftdi_free,
84
        usbconn_ftdi_mpsse_open,
85
        usbconn_ftdi_close,
86
        usbconn_ftdi_read,
87
        usbconn_ftdi_write
88
};
89
 
90
usbconn_cable_t usbconn_ft2232_mpsse_CableID2= {
91
  "CableID2",         /* cable name */
92
  "CableID2",         /* string pattern, not used */
93
  "ftdi-mpsse",       /* default usbconn driver */
94
  0x0403,             /* VID */
95
  0x6010              /* PID */
96
};
97
 
98
static usbconn_t *ft2232_device;
99
 
100
 
101
 
102
/// ----------------------------------------------------------------------------------------------
103
/// libftdi wrappers for debugging purposes.
104
/// ----------------------------------------------------------------------------------------------
105
 
106
void print_buffer(unsigned char *buf, int size) {
107
        int i=0;
108
        for(i=0; i<size; i++)
109
                printf("[MYDBG]\tBUFFER[%d] = %02x\n", i, buf[i]);
110
 
111
}
112
 
113
int my_ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
114
        debug("[MYDBG] ftdi_write_data(ftdi, buf=BUFFER[%d], size=%d);\n", size, size);
115
        if(debug > 1) print_buffer(buf, size);
116
        return ftdi_write_data(ftdi, buf, size);
117
}
118
 
119
char *my_ftdi_get_error_string (struct ftdi_context *ftdi) {
120
        debug("[MYDBG] ftdi_get_error_string(ftdi);\n");
121
        return ftdi_get_error_string (ftdi);
122
}
123
 
124
int my_ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
125
        int ret = 0;
126
        debug("[MYDBG] ftdi_read_data(ftdi, buf=BUFFER[%d], size=%d);\n", size, size);
127
        ret = ftdi_read_data(ftdi, buf, size);
128
        if(debug) print_buffer(buf, size);
129
        return ret;
130
}
131
 
132
int my_ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product, const char* description, const char* serial) {
133
        debug("[MYDBG] ftdi_usb_open_desc(ftdi, vendor=%d, product=%d, description=DESCRIPTION, serial=SERIAL);\n", vendor, product);
134
        return ftdi_usb_open_desc(ftdi, vendor, product, description, serial);
135
}
136
 
137
void my_ftdi_deinit(struct ftdi_context *ftdi) {
138
        debug("[MYDBG] ftdi_deinit(ftdi);\n");
139
        ftdi_deinit(ftdi);
140
}
141
 
142
int my_ftdi_usb_purge_buffers(struct ftdi_context *ftdi) {
143
        debug("[MYDBG] ftdi_usb_purge_buffers(ftdi);\n");
144
        return ftdi_usb_purge_buffers(ftdi);
145
}
146
 
147
int my_ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi) {
148
        debug("[MYDBG] ftdi_usb_purge_rx_buffer(ftdi);\n");
149
        return ftdi_usb_purge_rx_buffer(ftdi);
150
}
151
 
152
int my_ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi) {
153
        debug("[MYDBG] ftdi_usb_purge_tx_buffer(ftdi);\n");
154
        return ftdi_usb_purge_tx_buffer(ftdi);
155
}
156
 
157
int my_ftdi_usb_reset(struct ftdi_context *ftdi) {
158
        debug("[MYDBG] ftdi_usb_reset(ftdi);\n");
159
        return ftdi_usb_reset(ftdi);
160
}
161
 
162
int my_ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
163
        debug("[MYDBG] ftdi_set_latency_timer(ftdi, latency=0x%02x);\n", latency);
164
        return ftdi_set_latency_timer(ftdi, latency);
165
}
166
 
167
int my_ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
168
        debug("[MYDBG] ftdi_set_baudrate(ftdi, baudrate=%d);\n", baudrate);
169
        return ftdi_set_baudrate(ftdi, baudrate);
170
}
171
 
172
int my_ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
173
        debug("[MYDBG] ftdi_read_data_set_chunksize(ftdi, chunksize=%u);\n", chunksize);
174
        return ftdi_read_data_set_chunksize(ftdi, chunksize);
175
}
176
 
177
int my_ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
178
        debug("[MYDBG] ftdi_write_data_set_chunksize(ftdi, chunksize=%u);\n", chunksize);
179
        return ftdi_write_data_set_chunksize(ftdi, chunksize);
180
}
181
 
182
int my_ftdi_set_event_char(struct ftdi_context *ftdi, unsigned char eventch, unsigned char enable) {
183
        debug("[MYDBG] ftdi_set_event_char(ftdi, eventch=0x%02x, enable=0x%02x);\n", eventch, enable);
184
        return ftdi_set_event_char(ftdi, eventch, enable);
185
}
186
 
187
int my_ftdi_set_error_char(struct ftdi_context *ftdi, unsigned char errorch, unsigned char enable) {
188
        debug("[MYDBG] ftdi_set_error_char(ftdi, errorch=0x%02x, enable=0x%02x);\n", errorch, enable);
189
        return ftdi_set_error_char(ftdi, errorch, enable);
190
}
191
 
192
int my_ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode) {
193
        debug("[MYDBG] ftdi_set_bitmode(ftdi, bitmask=0x%02x, mode=0x%02x);\n", bitmask, mode);
194
        return ftdi_set_bitmode(ftdi, bitmask, mode);
195
}
196
 
197
int my_ftdi_usb_close(struct ftdi_context *ftdi) {
198
        debug("[MYDBG] ftdi_usb_close(ftdi);\n");
199
        return ftdi_usb_close(ftdi);
200
}
201
 
202
 
203
 
204
/// ----------------------------------------------------------------------------------------------
205
/// USBconn FTDI MPSSE subsystem
206
/// ----------------------------------------------------------------------------------------------
207
 
208
 
209
static int usbconn_ftdi_common_open(usbconn_t *conn) {
210
        ftdi_param_t *params = conn->params;
211
        struct ftdi_context * ftdic = params->ftdic;
212
        int error;
213
 
214
        printf("Initializing USB device\n");
215
 
216
        if ((error = my_ftdi_usb_open_desc(ftdic, conn->cable->vid, conn->cable->pid, NULL, NULL))) {
217
                if      (error == -1) printf("usb_find_busses() failed\n");
218
                else if (error == -2) printf("usb_find_devices() failed\n");
219 58 nyawn
                else if (error == -3) printf("usb device not found with VID 0x%0X, PID 0x%0X\n", conn->cable->vid, conn->cable->pid);
220 21 nyawn
                else if (error == -4) printf("unable to open device\n");
221
                else if (error == -5) printf("unable to claim device\n");
222
                else if (error == -6) printf("reset failed\n");
223
                else if (error == -7) printf("set baudrate failed\n");
224
                else if (error == -8) printf("get product description failed\n");
225
                else if (error == -9) printf("get serial number failed\n");
226
                else if (error == -10) printf("unable to close device\n");
227
 
228
                my_ftdi_deinit(ftdic);
229
                ftdic = NULL;
230
 
231
                printf("Can't open FTDI usb device\n");
232
                return(-1);
233
        }
234
 
235
        return 0;
236
}
237
 
238
static int seq_purge(struct ftdi_context *ftdic, int purge_rx, int purge_tx) {
239
        int r = 0;
240
        unsigned char buf;
241
 
242
        if ((r = my_ftdi_usb_purge_buffers( ftdic )) < 0)
243
                printf("my_ftdi_usb_purge_buffers() failed\n");
244
        if (r >= 0) if ((r = my_ftdi_read_data( ftdic, &buf, 1 )) < 0)
245
                printf("my_ftdi_read_data() failed\n");
246
 
247
        return r < 0 ? -1 : 0;
248
}
249
 
250
static int seq_reset(struct ftdi_context *ftdic) {
251
 
252
        if (my_ftdi_usb_reset( ftdic ) < 0) {
253
                printf("my_ftdi_usb_reset() failed\n");
254
                return -1;
255
        }
256
 
257
        if(seq_purge(ftdic, 1, 1) < 0)
258
                return -1;
259
 
260
        return 0;
261
}
262
 
263
static int usbconn_ftdi_flush( ftdi_param_t *params )
264
        {
265
        int xferred;
266
        int recvd = 0;
267
 
268
        if (!params->ftdic)
269
                return -1;
270
 
271
        if (params->send_buffered == 0)
272
                return 0;
273
 
274
        if ((xferred = my_ftdi_write_data( params->ftdic, params->send_buf, params->send_buffered )) < 0)
275
                printf("my_ftdi_write_data() failed\n");
276
 
277
        if (xferred < params->send_buffered) {
278
                printf("Written fewer bytes than requested.\n");
279
                return -1;
280
        }
281
 
282
        params->send_buffered = 0;
283
 
284
        /* now read all scheduled receive bytes */
285
        if (params->to_recv) {
286
                if (params->recv_write_idx + params->to_recv > params->recv_buf_len) {
287
                        /* extend receive buffer */
288
                        params->recv_buf_len = params->recv_write_idx + params->to_recv;
289
                        if (params->recv_buf)
290
                                params->recv_buf = (uint8_t *)realloc( params->recv_buf, params->recv_buf_len );
291
                }
292
 
293
                if (!params->recv_buf) {
294
                        printf("Receive buffer does not exist.\n");
295
                        return -1;
296
                }
297
 
298
                while (recvd == 0)
299
                        if ((recvd = my_ftdi_read_data( params->ftdic, &(params->recv_buf[params->recv_write_idx]), params->to_recv )) < 0)
300
                                printf("Error from my_ftdi_read_data()\n");
301
 
302
                if (recvd < params->to_recv)
303
                        printf("Received less bytes than requested.\n");
304
 
305
                params->to_recv -= recvd;
306
                params->recv_write_idx += recvd;
307
        }
308
 
309
        debug("[MYDBG] FLUSHING xferred=%u\n", xferred);
310
        return xferred < 0 ? -1 : xferred;
311
}
312
 
313
static int usbconn_ftdi_read( usbconn_t *conn, uint8_t *buf, int len ) {
314
        ftdi_param_t *params = conn->params;
315
        int cpy_len;
316
        int recvd = 0;
317
 
318
        if (!params->ftdic)
319
                return -1;
320
 
321
        /* flush send buffer to get all scheduled receive bytes */
322
        if (usbconn_ftdi_flush( params ) < 0)
323
                return -1;
324
 
325
        if (len == 0)
326
                return 0;
327
 
328
        /* check for number of remaining bytes in receive buffer */
329
        cpy_len = params->recv_write_idx - params->recv_read_idx;
330
        if (cpy_len > len)
331
                cpy_len = len;
332
        len -= cpy_len;
333
 
334
        if (cpy_len > 0) {
335
                /* get data from the receive buffer */
336
                memcpy( buf, &(params->recv_buf[params->recv_read_idx]), cpy_len );
337
                params->recv_read_idx += cpy_len;
338
                if (params->recv_read_idx == params->recv_write_idx)
339
                        params->recv_read_idx = params->recv_write_idx = 0;
340
        }
341
 
342
        if (len > 0) {
343
                /* need to get more data directly from the device */
344
                while (recvd == 0)
345
                        if ((recvd = my_ftdi_read_data( params->ftdic, &(buf[cpy_len]), len )) < 0)
346
                                printf("Error from my_ftdi_read_data()\n");
347
        }
348
        debug("[MYDBG] READ cpy_len=%u ; len=%u\n", cpy_len, len);
349
        return recvd < 0 ? -1 : cpy_len + len;
350
}
351
 
352
static int usbconn_ftdi_write( usbconn_t *conn, uint8_t *buf, int len, int recv ) {
353
 
354
        ftdi_param_t *params = conn->params;
355
        int xferred = 0;
356
 
357
        if (!params->ftdic)
358
                return -1;
359
 
360
        /* this write function will try to buffer write data
361
           buffering will be ceased and a flush triggered in two cases. */
362
 
363
        /* Case A: max number of scheduled receive bytes will be exceeded
364
           with this write
365
           Case B: max number of scheduled send bytes has been reached */
366
        if ((params->to_recv + recv > FTDI_MAXRECV) || ((params->send_buffered > FTDX_MAXSEND) && (params->to_recv == 0)))
367
                xferred = usbconn_ftdi_flush(params);
368
 
369
        if (xferred < 0)
370
                return -1;
371
 
372
        /* now buffer this write */
373
        if (params->send_buffered + len > params->send_buf_len) {
374
                params->send_buf_len = params->send_buffered + len;
375
                if (params->send_buf)
376
                        params->send_buf = (uint8_t *)realloc( params->send_buf, params->send_buf_len);
377
        }
378
 
379
        if (params->send_buf) {
380
                memcpy( &(params->send_buf[params->send_buffered]), buf, len );
381
                params->send_buffered += len;
382
                if (recv > 0)
383
                        params->to_recv += recv;
384
 
385
                if (recv < 0) {
386
                        /* immediate write requested, so flush the buffered data */
387
                        xferred = usbconn_ftdi_flush( params );
388
                }
389
 
390
                debug("[MYDBG] WRITE inmediate=%s ; xferred=%u ; len=%u\n", ((recv < 0) ? "TRUE" : "FALSE"), xferred, len);
391
                return xferred < 0 ? -1 : len;
392
        }
393
        else {
394
                printf("Send buffer does not exist.\n");
395
                return -1;
396
        }
397
}
398
 
399
static int usbconn_ftdi_mpsse_open( usbconn_t *conn ) {
400
        ftdi_param_t *params = conn->params;
401
        struct ftdi_context *ftdic = params->ftdic;
402
 
403
        int r = 0;
404
 
405
        if (usbconn_ftdi_common_open(conn) < 0) {
406
                printf("Connection failed\n");
407
                return -1;
408
        }
409
 
410
        /* This sequence might seem weird and containing superfluous stuff.
411
           However, it's built after the description of JTAG_InitDevice
412
           Ref. FTCJTAGPG10.pdf
413
           Intermittent problems will occur when certain steps are skipped. */
414
 
415
        r = seq_reset( ftdic );
416
        if (r >= 0)
417
                r = seq_purge( ftdic, 1, 0 );
418
 
419
        if (r >= 0)
420
                if ((r = my_ftdi_write_data_set_chunksize( ftdic, FTDX_MAXSEND_MPSSE )) < 0)
421
                        puts( my_ftdi_get_error_string( ftdic ) );
422
 
423
        if (r >= 0)
424
                if ((r = my_ftdi_read_data_set_chunksize( ftdic, FTDX_MAXSEND_MPSSE )) < 0)
425
                        puts( my_ftdi_get_error_string( ftdic ) );
426
 
427
        /* set a reasonable latency timer value
428
           if this value is too low then the chip will send intermediate result data
429
           in short packets (suboptimal performance) */
430
        if (r >= 0)
431
                if ((r = my_ftdi_set_latency_timer( ftdic, 16 )) < 0)
432
                        printf("my_ftdi_set_latency_timer() failed\n");
433
 
434
        if (r >= 0)
435
                if ((r = my_ftdi_set_bitmode( ftdic, 0x0b, BITMODE_MPSSE )) < 0)
436
                        printf("my_ftdi_set_bitmode() failed\n");
437
 
438
        if (r >= 0)
439
                if ((r = my_ftdi_usb_reset( ftdic )) < 0)
440
                        printf("my_ftdi_usb_reset() failed\n");
441
 
442
        if (r >= 0)
443
                r = seq_purge( ftdic, 1, 0 );
444
 
445
        /* set TCK Divisor */
446
        if (r >= 0) {
447
                uint8_t buf[3] = {TCK_DIVISOR, 0x00, 0x00};
448
                r = usbconn_ftdi_write( conn, buf, 3, 0 );
449
        }
450
 
451
        /* switch off loopback */
452
        if (r >= 0) {
453
                uint8_t buf[1] = {LOOPBACK_END};
454
                r = usbconn_ftdi_write( conn, buf, 1, 0 );
455
        }
456
 
457
        if (r >= 0)
458
                r = usbconn_ftdi_read( conn, NULL, 0 );
459
 
460
        if (r >= 0)
461
                if ((r = my_ftdi_usb_reset( ftdic )) < 0)
462
                        printf("my_ftdi_usb_reset() failed\n");
463
 
464
        if (r >= 0)
465
                r = seq_purge( ftdic, 1, 0 );
466
 
467
        if (r < 0) {
468
                ftdi_usb_close( ftdic );
469
                ftdi_deinit( ftdic );
470
                /* mark ftdi layer as not initialized */
471
                params->ftdic = NULL;
472
        }
473
 
474
        return r < 0 ? -1 : 0;
475
}
476
 
477
static int usbconn_ftdi_close(usbconn_t *conn) {
478
        ftdi_param_t *params = conn->params;
479
 
480
        if (params->ftdic) {
481
                my_ftdi_usb_close(params->ftdic);
482
                my_ftdi_deinit(params->ftdic);
483
                params->ftdic = NULL;
484
        }
485
 
486
        return 0;
487
}
488
 
489
static void usbconn_ftdi_free( usbconn_t *conn )
490
{
491
  ftdi_param_t *params = conn->params;
492
 
493
  if (params->send_buf) free( params->send_buf );
494
  if (params->recv_buf) free( params->recv_buf );
495
  if (params->ftdic)    free( params->ftdic );
496
  if (params->serial)   free( params->serial );
497
 
498
  free( conn->params );
499
  free( conn );
500
}
501
 
502
usbconn_t * usbconn_ftdi_connect() {
503
 
504
        usbconn_t *conn            = malloc( sizeof( usbconn_t ) );
505
        ftdi_param_t *params       = malloc( sizeof( ftdi_param_t ) );
506
        struct ftdi_context *ftdic = malloc( sizeof( struct ftdi_context ) );
507
 
508
        if (params) {
509
                params->send_buf_len   = FTDX_MAXSEND;
510
                params->send_buffered  = 0;
511
                params->send_buf       = (uint8_t *) malloc( params->send_buf_len );
512
                params->recv_buf_len   = FTDI_MAXRECV;
513
                params->to_recv        = 0;
514
                params->recv_write_idx = 0;
515
                params->recv_read_idx  = 0;
516
                params->recv_buf       = (uint8_t *) malloc( params->recv_buf_len );
517
        }
518
 
519
        if (!conn || !params || !ftdic || !params->send_buf || !params->recv_buf) {
520
                printf("Can't allocate memory for ftdi context structures\n");
521
 
522
                if (conn) free( conn );
523
                if (params) free( params );
524
                if (ftdic) free( ftdic );
525
                if (params->send_buf) free( params->send_buf );
526
                if (params->recv_buf) free( params->recv_buf );
527
                return NULL;
528
        }
529
 
530
        conn->driver = &usbconn_ft2232_mpsse_driver;
531
        conn->cable  = &usbconn_ft2232_mpsse_CableID2;
532
 
533
        ftdi_init( ftdic );
534
        params->ftdic  = ftdic;
535
        params->pid    = conn->cable->pid;
536
        params->vid    = conn->cable->vid;
537
        params->serial = NULL;
538
 
539
        conn->params = params;
540
 
541
        printf("Structs successfully initialized\n");
542
 
543
        /* do a test open with the specified cable paramters,
544
           alternatively we could use libusb to detect the presence of the
545
           specified USB device         */
546
        if (usbconn_ftdi_common_open(conn) != 0) {
547
                printf("Connection failed\n");
548
                usbconn_ftdi_free(conn);
549
                printf("Freeing structures.\n");
550
                return NULL;
551
        }
552
 
553
        my_ftdi_usb_close( ftdic );
554
 
555
        printf("Connected to libftdi driver.\n");
556
 
557
        return conn;
558
}
559
 
560
 
561
 
562
/// ----------------------------------------------------------------------------------------------
563
/// High level functions to generate Tx/Rx commands
564
/// ----------------------------------------------------------------------------------------------
565
 
566
int cable_ft2232_write_bytes(usbconn_t *conn, unsigned char *buf, int len, int postread) {
567
 
568
        int cur_command_size;
569
        int max_command_size;
570
        int cur_chunk_len;
571
        int recv;
572
        int xferred;
573
        int i;
574
        unsigned char *mybuf;
575
 
576
        if(len == 0)
577
                return 0;
578
        debug("write_bytes(length=%d, postread=%s)\n", len, ((postread > 0) ? "TRUE" : "FALSE"));
579
        recv = 0;
580
        max_command_size = min(len, 65536)+3;
581
        mybuf = (unsigned char *) malloc( max_command_size );
582
 
583
        /// Command OPCODE: write bytes
584
        mybuf[0] = MPSSE_DO_WRITE | MPSSE_LSB | MPSSE_WRITE_NEG;
585
        if(postread) // if postread is enabled it will buffer incoming bytes
586
                mybuf[0] = mybuf[0] | MPSSE_DO_READ;
587
 
588
        // We divide the transmitting stream of bytes in chunks with a maximun length of 65536 bytes each.
589
        while(len > 0) {
590
                cur_chunk_len = min(len, 65536);
591
                len = len - cur_chunk_len;
592
                cur_command_size = cur_chunk_len + 3;
593
 
594
                /// Low and High bytes of the length field
595
                mybuf[1] = (unsigned char) ( cur_chunk_len - 1);
596
                mybuf[2] = (unsigned char) ((cur_chunk_len - 1) >> 8);
597
 
598
                debug("\tOPCODE:  0x%x\n", mybuf[0]);
599
                debug("\tLENGTL:  0x%02x\n", mybuf[1]);
600
                debug("\tLENGTH:  0x%02x\n", mybuf[2]);
601
 
602
                /// The rest of the command is filled with the bytes that will be transferred
603
                memcpy(&(mybuf[3]), buf, cur_chunk_len );
604
                buf = buf + cur_chunk_len;
605
                for(i = 0; i< cur_chunk_len; i++)
606
                        if(debug>1) debug("\tBYTE%3d: 0x%02x\n", i, mybuf[3+i]);
607
 
608
                /// Finally we can ransmit the command
609
                xferred = usbconn_ftdi_write( conn, mybuf, cur_command_size, (postread ? cur_chunk_len : 0) );
610
                if(xferred != cur_command_size)
611
                        return -1;
612
 
613
                // If OK, the update the number of incoming bytes that are being buffered for a posterior read
614
                if(postread)
615
                        recv = recv + cur_chunk_len;
616
        }
617
        debug("\tPOSTREAD: %u bytes\n", recv);
618
 
619
        // Returns the number of buffered incoming bytes
620
        return recv;
621
}
622
 
623
int cable_ft2232_write_bits(usbconn_t *conn, unsigned char *buf, int len, int postread, int with_tms)
624
{
625
        int max_command_size;
626
        int max_chunk_len;
627
        int cur_chunk_len;
628
        int recv;
629
        int xferred;
630
        int i;
631
        unsigned char *mybuf;
632
 
633
        if(len == 0)
634
                return 0;
635
 
636
        max_command_size = 3;
637
        mybuf = (unsigned char *) malloc( max_command_size );
638
 
639
        if(!with_tms) {
640
                /// Command OPCODE: write bits (can write up to 8 bits in a single command)
641
                max_chunk_len = 8;
642
                mybuf[0] = MPSSE_DO_WRITE | MPSSE_LSB | MPSSE_WRITE_NEG | MPSSE_BITMODE;
643
        }
644
        else {
645
                /// Command OPCODE: 0x4B write bit with tms (can write up to 1 bits in a single command)
646
                max_chunk_len = 1;
647
                mybuf[0] = MPSSE_WRITE_TMS|MPSSE_LSB|MPSSE_BITMODE|MPSSE_WRITE_NEG;
648
        }
649
 
650
        if(postread) // (OPCODE += 0x20) if postread is enabled it will buffer incoming bits
651
                mybuf[0] = mybuf[0] | MPSSE_DO_READ;
652
 
653
        // We divide the transmitting stream of bytes in chunks with a maximun length of max_chunk_len bits each.
654
        i=0;
655
        recv = 0;
656
        while(len > 0) {
657
                cur_chunk_len = min(len, max_chunk_len);
658
                len = len - cur_chunk_len;
659
 
660
                /// Bits length field
661
                mybuf[1] = (unsigned char) ( cur_chunk_len - 1);
662
 
663
                debug("\tOPCODE:  0x%x\n", mybuf[0]);
664
                debug("\tLENGTH:  0x%02x\n", mybuf[1]);
665
 
666
                if(!with_tms) {
667
                        /// The last byte of the command is filled with the bits that will be transferred
668
                        debug("\tDATA[%d]  0x%02x\n", (i/8), buf[i/8]);
669
                        mybuf[2] = buf[i/8];
670
                        i=i+8;
671
                }
672
                else {
673
                        //TODO: seleccionar el bit a transmitir
674
                        mybuf[2] = 0x01 | ((buf[(i/8)] >> (i%8)) << 7);
675
                        i++;
676
                }
677
 
678
                debug("\tBYTE%3d: 0x%02x\n", i, mybuf[2]);
679
 
680
                /// Finally we can transmmit the command
681
                xferred = usbconn_ftdi_write( conn, mybuf, max_command_size, (postread ? 1 : 0) );
682
                if(xferred != max_command_size)
683
                        return -1;
684
 
685
                // If OK, the update the number of incoming bytes that are being buffered for a posterior read
686
                if(postread)
687
                        recv = recv + 1;
688
        }
689
        debug("\tPOSTREAD: %u bytes\n", recv);
690
 
691
        return recv;
692
}
693
 
694
int cable_ft2232_read_packed_bits(usbconn_t *conn, uint8_t *buf, int packet_len, int bits_per_packet, int offset)
695
{
696
        unsigned char *mybuf;
697
        unsigned char dst_mask;
698
        unsigned char src_mask;
699
        int row_offset;
700
        int dst_row;
701
        int dst_col;
702
        int src_row;
703
        int src_col;
704
        int i;
705
        int r;
706
 
707
        if(packet_len == 0 || bits_per_packet == 0)
708
                return 0;
709
 
710
        mybuf = (unsigned char *) malloc( packet_len );
711
        if((r=usbconn_ftdi_read( conn, mybuf, packet_len )) < 0) {
712
                debug("Read failed\n");
713
                return -1;
714
        }
715
 
716
        if(bits_per_packet < 8) {
717
                for(i=0; i < packet_len; i++){ // rotate bits to the left side
718
//                      debug("[MYDBG] unaligned bits[%d]=%02x\n", i, mybuf[i]);                        
719
                        mybuf[i] = (mybuf[i] >> (8-bits_per_packet));
720
//                      debug("[MYDBG]   aligned bits[%d]=%02x\n", i, mybuf[i]);
721
                }
722
                for(i=offset; i < (packet_len*bits_per_packet+offset); i++) {
723
                        dst_row = i / 8;
724
                        dst_col = i % 8;
725
                        src_row = (i-offset) / bits_per_packet;
726
                        src_col = (i-offset) % bits_per_packet;
727
                        dst_mask = ~(1 << dst_col);
728
                        src_mask = (1 << src_col);
729
//                      debug("[MYDBG] i=%4d dst[%3d][%3d] dst_mask=%02x dst_val=%02x dst_masked=%02x\n", i, dst_row, dst_col, dst_mask, buf[dst_row], (buf[dst_row] & dst_mask));
730
//                      debug("[MYDBG] i=%4d src[%3d][%3d] src_mask=%02x src_val=%02x src_masked=%02x\n", i, src_row, src_col, src_mask, mybuf[src_row], (mybuf[src_row] & src_mask));
731
                        if(dst_col >= src_col)
732
                                buf[dst_row] = (buf[dst_row] & dst_mask) | ((mybuf[src_row] & src_mask) << (dst_col - src_col));
733
                        else
734
                                buf[dst_row] = (buf[dst_row] & dst_mask) | ((mybuf[src_row] & src_mask) >> (dst_col - src_col));
735
                }
736
 
737
        }
738
        else if(bits_per_packet == 8){
739
                row_offset = offset / 8;
740
//              debug("[MYDBG] Row offset=%d\n", row_offset);
741
                memcpy( &(buf[row_offset]), mybuf, packet_len);
742
        }
743
        else {
744
                return -1;
745
        }
746
 
747
//      debug("read_bits()-> %x\n", *buf);
748
        return ((r < 1) ? -1 : 0);
749
}
750
 
751
int cable_ft2232_write_stream(usbconn_t *conn, unsigned char *buf, int len, int postread, int with_tms) {
752
        int len_bytes;
753
        int len_bits;
754
        int len_tms_bits;
755
        unsigned char mybuf;
756
 
757
        len_tms_bits = ((with_tms) ? 1 : 0);
758
        len_bytes = ((len -len_tms_bits) / 8);
759
        len_bits  = ((len -len_tms_bits) % 8);
760
 
761
        debug("[MYDBG] cable_ft2232_write_stream(len=%d postread=%d tms=%d) = %d bytes %dbits %dtms_bits\n", len, postread, with_tms, len_bytes, len_bits, len_tms_bits);
762
 
763
        if(len_bytes > 0)
764
                cable_ft2232_write_bytes(conn, buf, len_bytes, postread);
765
 
766
        if(len_bits > 0)
767
                cable_ft2232_write_bits(conn, &(buf[len_bytes]), len_bits, postread, 0);
768
 
769
        if(len_tms_bits > 0) {
770
                mybuf = (buf[len_bytes] >> len_bits);
771
                cable_ft2232_write_bits(conn, &mybuf, 1, postread, 1);
772
        }
773
 
774
        return 0;
775
}
776
 
777
int cable_ft2232_read_stream(usbconn_t *conn, unsigned char *buf, int len, int with_tms) {
778
        int len_bytes;
779
        int len_bits;
780
        int len_tms_bits;
781
 
782
        len_tms_bits = ((with_tms) ? 1 : 0);
783
        len_bytes = ((len -len_tms_bits) / 8);
784
        len_bits  = ((len -len_tms_bits) % 8);
785
 
786
        debug("[MYDBG] cable_ft2232_read_stream(len=%d tms=%d) = %d bytes %dbits %dtms_bits\n", len, with_tms, len_bytes, len_bits, len_tms_bits);
787
 
788
        if(len_bytes > 0)
789
                cable_ft2232_read_packed_bits(conn, buf, len_bytes, 8, 0);
790
 
791
        if(len_bits > 0)
792
                cable_ft2232_read_packed_bits(conn, buf, 1, len_bits, (len_bytes * 8));
793
 
794
        if(len_tms_bits > 0)
795
                cable_ft2232_read_packed_bits(conn, buf, 1, 1, (len_bits + (len_bytes * 8)));
796
 
797
        return 0;
798
}
799
 
800
 
801
 
802
/// ----------------------------------------------------------------------------------------------
803
/// Advanced Jtag debugger driver interface.
804
/// ----------------------------------------------------------------------------------------------
805
 
806 55 nyawn
jtag_cable_t *cable_ftdi_get_driver(void)
807
{
808
  return &ft2232_cable_driver;
809
}
810
 
811 21 nyawn
int cable_ftdi_init() {
812
        int err = APP_ERR_NONE;
813
        int res = 0;
814
        unsigned char  *buf = malloc(10);
815
 
816
        ft2232_device = usbconn_ftdi_connect();
817
 
818
        if((res = usbconn_ftdi_mpsse_open(ft2232_device)) != 0)
819
                err |= APP_ERR_USB;
820
        printf("Open MPSSE mode returned: %s\n", ((res != 0) ? "FAIL" : "OK") );
821
 
822
        ftdi_param_t *params = ft2232_device->params;
823
        //struct ftdi_context * ftdic = params->ftdic;
824
 
825 58 nyawn
        buf[0]= SET_BITS_LOW;  // Set value & direction of ADBUS lines
826
        buf[1]= 0x00;          // values
827
        buf[2]= 0x1b;          // direction (1 == output)
828 21 nyawn
        buf[3]= TCK_DIVISOR;
829
        buf[4]= 0x01;
830
        buf[5]= 0x00;
831
        buf[6]= SET_BITS_HIGH;
832
        buf[7]= ~0x04;
833
        buf[8]= 0x04;
834
        buf[9]= SEND_IMMEDIATE;
835
        if(usbconn_ftdi_write( ft2232_device , buf, 10, 0) != 10) {
836
                err |= APP_ERR_USB;
837
                printf("Initial write failed\n");
838
        }
839
 
840
        usbconn_ftdi_flush( params );
841
 
842
        return err;
843
}
844
 
845
int cable_ftdi_close() {
846
        usbconn_ftdi_close(ft2232_device);
847
        usbconn_ftdi_free(ft2232_device);
848
 
849
        return APP_ERR_NONE;
850
}
851
 
852
int cable_ftdi_flush() {
853
        ftdi_param_t *params = ft2232_device->params;
854
        usbconn_ftdi_flush( params );
855
 
856
        return APP_ERR_NONE;
857
}
858
 
859
int cable_ftdi_write_bit(uint8_t packet) {
860
        int err = APP_ERR_NONE;
861
        unsigned char buf;
862
        int tms;
863
 
864
        buf = ((packet & TDO) ? 0x01 : 0x00);
865
        tms = ((packet & TMS) ? 1 : 0);
866
 
867
        if(cable_ft2232_write_stream(ft2232_device, &buf, 1, 0, tms) < 0)
868
                err |= APP_ERR_COMM;
869
 
870
        cable_ftdi_flush();
871
 
872
        return err;
873
 
874
}
875
 
876
int cable_ftdi_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
877
 
878
        int err = APP_ERR_NONE;
879
        unsigned char buf;
880
        int tms;
881
 
882
        buf = ((packet_out & TDO) ? 0x01 : 0x00);
883
        tms = ((packet_out & TMS) ? 1 : 0);
884
 
885
        if(cable_ft2232_write_stream(ft2232_device, &buf, 1, 1, tms) < 0)
886
                err = APP_ERR_COMM;
887
 
888
        if(cable_ft2232_read_stream(ft2232_device, ((unsigned char *)bit_in), 1, tms) < 0)
889
                err = APP_ERR_COMM;
890
 
891
        return err;
892
}
893
 
894
int cable_ftdi_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
895
        int err = APP_ERR_NONE;
896
 
897
        if(cable_ft2232_write_stream(ft2232_device, ((unsigned char *)stream), len_bits, 0, set_last_bit) < 0)
898
                err |= APP_ERR_COMM;
899
 
900
        cable_ftdi_flush();
901
 
902
        return err;
903
}
904
 
905
int cable_ftdi_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
906
        int err = APP_ERR_NONE;
907
        if(cable_ft2232_write_stream(ft2232_device, ((unsigned char *)outstream), len_bits, 1, set_last_bit) < 0)
908
                err |= APP_ERR_COMM;
909
        if(cable_ft2232_read_stream(ft2232_device, ((unsigned char *)instream), len_bits, set_last_bit) < 0)
910
                err |= APP_ERR_COMM;
911
 
912
        return err;
913
}
914
 
915
int cable_ftdi_opt(int c, char *str) {
916 58 nyawn
  uint32_t newvid;
917
  uint32_t newpid;
918
 
919
  switch(c) {
920
  case 'p':
921
    if(!sscanf(str, "%x", &newpid)) {
922
      fprintf(stderr, "p parameter must have a hex number as parameter\n");
923
      return APP_ERR_BAD_PARAM;
924
    }
925
    else {
926
      usbconn_ft2232_mpsse_CableID2.pid = newpid;
927
    }
928
    break;
929
 
930
  case 'v':
931
    if(!sscanf(str, "%x", &newvid)) {
932
      fprintf(stderr, "v parameter must have a hex number as parameter\n");
933
      return APP_ERR_BAD_PARAM;
934
    }
935
    else {
936
      usbconn_ft2232_mpsse_CableID2.vid = newvid;
937
    }
938
    break;
939
 
940
  default:
941
    fprintf(stderr, "Unknown parameter '%c'\n", c);
942
    return APP_ERR_BAD_PARAM;
943
  }
944
  return APP_ERR_NONE;
945 21 nyawn
}
946
 
947
/// ----------------------------------------------------------------------------------------------
948
 

powered by: WebSVN 2.1.0

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