Zach Welch <zw@superlucidity.net> fix ft2232 usb io format warning
[openocd.git] / src / jtag / ft2232.c
1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24
25 /* This code uses information contained in the MPSSE specification which was
26 * found here:
27 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
28 * Hereafter this is called the "MPSSE Spec".
29 */
30
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #if IS_CYGWIN == 1
37 #include "windows.h"
38 #endif
39
40 #include "replacements.h"
41
42 /* project specific includes */
43 #include "log.h"
44 #include "types.h"
45 #include "jtag.h"
46 #include "configuration.h"
47 #include "time_support.h"
48
49 /* system includes */
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53
54 /* FT2232 access library includes */
55 #if BUILD_FT2232_FTD2XX == 1
56 #include <ftd2xx.h>
57 #elif BUILD_FT2232_LIBFTDI == 1
58 #include <ftdi.h>
59 #endif
60
61 static int ft2232_execute_queue(void);
62
63 static int ft2232_speed(int speed);
64 static int ft2232_speed_div(int speed, int* khz);
65 static int ft2232_khz(int khz, int* jtag_speed);
66 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
67 static int ft2232_init(void);
68 static int ft2232_quit(void);
69
70 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
71 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
72 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
73 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
74 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
75
76
77 /**
78 * Function ft2232_stableclocks
79 * will send out \a num_cycles on the TCK line while the TAP(s)
80 * are in a stable state. Calling code must ensure that current state is
81 * stable, that verification is not done in here.
82 * @param num_cycles is the count of clocks cycles to send.
83 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
84 */
85 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
86
87
88 static char * ft2232_device_desc_A = NULL;
89 static char* ft2232_device_desc = NULL;
90 static char* ft2232_serial = NULL;
91 static char* ft2232_layout = NULL;
92 static unsigned char ft2232_latency = 2;
93
94 #define MAX_USB_IDS 8
95 /* vid = pid = 0 marks the end of the list */
96 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
97 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
98
99 typedef struct ft2232_layout_s
100 {
101 char* name;
102 int (*init)(void);
103 void (*reset)(int trst, int srst);
104 void (*blink)(void);
105 } ft2232_layout_t;
106
107 /* init procedures for supported layouts */
108 static int usbjtag_init(void);
109 static int jtagkey_init(void);
110 static int olimex_jtag_init(void);
111 static int flyswatter_init(void);
112 static int turtle_init(void);
113 static int comstick_init(void);
114 static int stm32stick_init(void);
115 static int axm0432_jtag_init(void);
116 static int sheevaplug_init(void);
117
118 /* reset procedures for supported layouts */
119 static void usbjtag_reset(int trst, int srst);
120 static void jtagkey_reset(int trst, int srst);
121 static void olimex_jtag_reset(int trst, int srst);
122 static void flyswatter_reset(int trst, int srst);
123 static void turtle_reset(int trst, int srst);
124 static void comstick_reset(int trst, int srst);
125 static void stm32stick_reset(int trst, int srst);
126 static void axm0432_jtag_reset(int trst, int srst);
127 static void sheevaplug_reset(int trst, int srst);
128
129 /* blink procedures for layouts that support a blinking led */
130 static void olimex_jtag_blink(void);
131 static void flyswatter_jtag_blink(void);
132 static void turtle_jtag_blink(void);
133
134 ft2232_layout_t ft2232_layouts[] =
135 {
136 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
137 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
138 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
139 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
140 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
141 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
142 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
143 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
144 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
145 { "comstick", comstick_init, comstick_reset, NULL },
146 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
147 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
148 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
149 { NULL, NULL, NULL, NULL },
150 };
151
152 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
153
154 static ft2232_layout_t* layout;
155 static u8 low_output = 0x0;
156 static u8 low_direction = 0x0;
157 static u8 high_output = 0x0;
158 static u8 high_direction = 0x0;
159
160 #if BUILD_FT2232_FTD2XX == 1
161 static FT_HANDLE ftdih = NULL;
162 #elif BUILD_FT2232_LIBFTDI == 1
163 static struct ftdi_context ftdic;
164 #endif
165
166
167 static jtag_command_t* first_unsent; /* next command that has to be sent */
168 static int require_send;
169
170 static u8* ft2232_buffer = NULL;
171 static int ft2232_buffer_size = 0;
172 static int ft2232_read_pointer = 0;
173 static int ft2232_expect_read = 0;
174
175 #define FT2232_BUFFER_SIZE 131072
176 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
177 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
178
179 jtag_interface_t ft2232_interface =
180 {
181 .name = "ft2232",
182 .execute_queue = ft2232_execute_queue,
183 .speed = ft2232_speed,
184 .speed_div = ft2232_speed_div,
185 .khz = ft2232_khz,
186 .register_commands = ft2232_register_commands,
187 .init = ft2232_init,
188 .quit = ft2232_quit,
189 };
190
191 static int ft2232_write(u8* buf, int size, u32* bytes_written)
192 {
193 #if BUILD_FT2232_FTD2XX == 1
194 FT_STATUS status;
195 DWORD dw_bytes_written;
196 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
197 {
198 *bytes_written = dw_bytes_written;
199 LOG_ERROR("FT_Write returned: %lu", status);
200 return ERROR_JTAG_DEVICE_ERROR;
201 }
202 else
203 {
204 *bytes_written = dw_bytes_written;
205 return ERROR_OK;
206 }
207 #elif BUILD_FT2232_LIBFTDI == 1
208 int retval;
209 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
210 {
211 *bytes_written = 0;
212 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
213 return ERROR_JTAG_DEVICE_ERROR;
214 }
215 else
216 {
217 *bytes_written = retval;
218 return ERROR_OK;
219 }
220 #endif
221 }
222
223
224 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
225 {
226 #if BUILD_FT2232_FTD2XX == 1
227 DWORD dw_bytes_read;
228 FT_STATUS status;
229 int timeout = 5;
230 *bytes_read = 0;
231
232 while ( (*bytes_read < size) && timeout-- )
233 {
234 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
235 *bytes_read, &dw_bytes_read) ) != FT_OK )
236 {
237 *bytes_read = 0;
238 LOG_ERROR("FT_Read returned: %lu", status);
239 return ERROR_JTAG_DEVICE_ERROR;
240 }
241 *bytes_read += dw_bytes_read;
242 }
243
244 #elif BUILD_FT2232_LIBFTDI == 1
245 int retval;
246 int timeout = 100;
247 *bytes_read = 0;
248
249 while ( (*bytes_read < size) && timeout-- )
250 {
251 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
252 {
253 *bytes_read = 0;
254 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
255 return ERROR_JTAG_DEVICE_ERROR;
256 }
257 *bytes_read += retval;
258 }
259
260 #endif
261
262 if (*bytes_read < size)
263 {
264 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
265 return ERROR_JTAG_DEVICE_ERROR;
266 }
267
268 return ERROR_OK;
269 }
270
271
272 static int ft2232_speed(int speed)
273 {
274 u8 buf[3];
275 int retval;
276 u32 bytes_written;
277
278 buf[0] = 0x86; /* command "set divisor" */
279 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
280 buf[2] = (speed >> 8) & 0xff; /* valueH */
281
282 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
283 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
284 {
285 LOG_ERROR("couldn't set FT2232 TCK speed");
286 return retval;
287 }
288
289 return ERROR_OK;
290 }
291
292
293 static int ft2232_speed_div(int speed, int* khz)
294 {
295 /* Take a look in the FT2232 manual,
296 * AN2232C-01 Command Processor for
297 * MPSSE and MCU Host Bus. Chapter 3.8 */
298
299 *khz = 6000 / (1 + speed);
300
301 return ERROR_OK;
302 }
303
304
305 static int ft2232_khz(int khz, int* jtag_speed)
306 {
307 if (khz==0)
308 {
309 LOG_ERROR("RCLK not supported");
310 return ERROR_FAIL;
311 }
312
313 /* Take a look in the FT2232 manual,
314 * AN2232C-01 Command Processor for
315 * MPSSE and MCU Host Bus. Chapter 3.8
316 *
317 * We will calc here with a multiplier
318 * of 10 for better rounding later. */
319
320 /* Calc speed, (6000 / khz) - 1 */
321 /* Use 65000 for better rounding */
322 *jtag_speed = (60000 / khz) - 10;
323
324 /* Add 0.9 for rounding */
325 *jtag_speed += 9;
326
327 /* Calc real speed */
328 *jtag_speed = *jtag_speed / 10;
329
330 /* Check if speed is greater than 0 */
331 if (*jtag_speed < 0)
332 {
333 *jtag_speed = 0;
334 }
335
336 /* Check max value */
337 if (*jtag_speed > 0xFFFF)
338 {
339 *jtag_speed = 0xFFFF;
340 }
341
342 return ERROR_OK;
343 }
344
345
346 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
347 {
348 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
349 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
350 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
351 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
352 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
353 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
354 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
355 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
356 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
357 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
358 return ERROR_OK;
359 }
360
361
362 void ft2232_end_state(tap_state_t state)
363 {
364 if (tap_is_state_stable(state))
365 tap_set_end_state(state);
366 else
367 {
368 LOG_ERROR("BUG: %i is not a valid end state", state);
369 exit(-1);
370 }
371 }
372
373
374 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
375 {
376 int num_bytes = (scan_size + 7) / 8;
377 int bits_left = scan_size;
378 int cur_byte = 0;
379
380 while (num_bytes-- > 1)
381 {
382 buffer[cur_byte] = BUFFER_READ;
383 cur_byte++;
384 bits_left -= 8;
385 }
386
387 buffer[cur_byte] = 0x0;
388
389 if (bits_left > 1)
390 {
391 buffer[cur_byte] = BUFFER_READ >> 1;
392 }
393
394 buffer[cur_byte] = ( buffer[cur_byte] | ( (BUFFER_READ & 0x02) << 6 ) ) >> (8 - bits_left);
395 }
396
397
398 static void ft2232_debug_dump_buffer(void)
399 {
400 int i;
401 char line[256];
402 char* line_p = line;
403
404 for (i = 0; i < ft2232_buffer_size; i++)
405 {
406 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
407 if (i % 16 == 15)
408 {
409 LOG_DEBUG("%s", line);
410 line_p = line;
411 }
412 }
413
414 if (line_p != line)
415 LOG_DEBUG("%s", line);
416 }
417
418
419 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
420 {
421 jtag_command_t* cmd;
422 u8* buffer;
423 int scan_size;
424 enum scan_type type;
425 int retval;
426 u32 bytes_written;
427 u32 bytes_read;
428
429 #ifdef _DEBUG_USB_IO_
430 struct timeval start, inter, inter2, end;
431 struct timeval d_inter, d_inter2, d_end;
432 #endif
433
434 #ifdef _DEBUG_USB_COMMS_
435 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
436 ft2232_debug_dump_buffer();
437 #endif
438
439 #ifdef _DEBUG_USB_IO_
440 gettimeofday(&start, NULL);
441 #endif
442
443 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
444 {
445 LOG_ERROR("couldn't write MPSSE commands to FT2232");
446 return retval;
447 }
448
449 #ifdef _DEBUG_USB_IO_
450 gettimeofday(&inter, NULL);
451 #endif
452
453 if (ft2232_expect_read)
454 {
455 int timeout = 100;
456 ft2232_buffer_size = 0;
457
458 #ifdef _DEBUG_USB_IO_
459 gettimeofday(&inter2, NULL);
460 #endif
461
462 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
463 {
464 LOG_ERROR("couldn't read from FT2232");
465 return retval;
466 }
467
468 #ifdef _DEBUG_USB_IO_
469 gettimeofday(&end, NULL);
470
471 timeval_subtract(&d_inter, &inter, &start);
472 timeval_subtract(&d_inter2, &inter2, &start);
473 timeval_subtract(&d_end, &end, &start);
474
475 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
476 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
477 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
478 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
479 #endif
480
481 ft2232_buffer_size = bytes_read;
482
483 if (ft2232_expect_read != ft2232_buffer_size)
484 {
485 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
486 ft2232_buffer_size,
487 100 - timeout);
488 ft2232_debug_dump_buffer();
489
490 exit(-1);
491 }
492
493 #ifdef _DEBUG_USB_COMMS_
494 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
495 ft2232_debug_dump_buffer();
496 #endif
497 }
498
499 ft2232_expect_read = 0;
500 ft2232_read_pointer = 0;
501
502 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
503 * that wasn't handled by a caller-provided error handler
504 */
505 retval = ERROR_OK;
506
507 cmd = first;
508 while (cmd != last)
509 {
510 switch (cmd->type)
511 {
512 case JTAG_SCAN:
513 type = jtag_scan_type(cmd->cmd.scan);
514 if (type != SCAN_OUT)
515 {
516 scan_size = jtag_scan_size(cmd->cmd.scan);
517 buffer = calloc(CEIL(scan_size, 8), 1);
518 ft2232_read_scan(type, buffer, scan_size);
519 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
520 retval = ERROR_JTAG_QUEUE_FAILED;
521 free(buffer);
522 }
523 break;
524
525 default:
526 break;
527 }
528
529 cmd = cmd->next;
530 }
531
532 ft2232_buffer_size = 0;
533
534 return retval;
535 }
536
537
538 static void ft2232_add_pathmove(pathmove_command_t* cmd)
539 {
540 int num_states = cmd->num_states;
541 int state_count = 0;
542
543 while (num_states)
544 {
545 u8 tms_byte = 0; /* zero this on each MPSSE batch */
546
547 int bit_count = 0;
548
549 int num_states_batch = num_states > 7 ? 7 : num_states;
550
551 /* command "Clock Data to TMS/CS Pin (no Read)" */
552 BUFFER_ADD = 0x4b;
553
554 /* number of states remaining */
555 BUFFER_ADD = num_states_batch - 1;
556
557 while (num_states_batch--)
558 {
559 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
560 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
561 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
562 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
563 else
564 {
565 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
566 tap_get_state() ), tap_state_name(cmd->path[state_count]) );
567 exit(-1);
568 }
569
570 tap_set_state(cmd->path[state_count]);
571 state_count++;
572 num_states--;
573 }
574
575 BUFFER_ADD = tms_byte;
576 }
577
578 tap_set_end_state(tap_get_state());
579 }
580
581
582 void ft2232_add_scan(int ir_scan, enum scan_type type, u8* buffer, int scan_size)
583 {
584 int num_bytes = (scan_size + 7) / 8;
585 int bits_left = scan_size;
586 int cur_byte = 0;
587 int last_bit;
588
589 if ( !( ( !ir_scan && (tap_get_state() == TAP_DRSHIFT) )
590 || ( ir_scan && (tap_get_state() == TAP_IRSHIFT) ) ) )
591 {
592 /* command "Clock Data to TMS/CS Pin (no Read)" */
593 BUFFER_ADD = 0x4b;
594
595 BUFFER_ADD = 0x6; /* scan 7 bits */
596
597 /* TMS data bits */
598 if (ir_scan)
599 {
600 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IRSHIFT);
601 tap_set_state(TAP_IRSHIFT);
602 }
603 else
604 {
605 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
606 tap_set_state(TAP_DRSHIFT);
607 }
608 /* LOG_DEBUG("added TMS scan (no read)"); */
609 }
610
611 /* add command for complete bytes */
612 while (num_bytes > 1)
613 {
614 int thisrun_bytes;
615 if (type == SCAN_IO)
616 {
617 /* Clock Data Bytes In and Out LSB First */
618 BUFFER_ADD = 0x39;
619 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
620 }
621 else if (type == SCAN_OUT)
622 {
623 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
624 BUFFER_ADD = 0x19;
625 /* LOG_DEBUG("added TDI bytes (o)"); */
626 }
627 else if (type == SCAN_IN)
628 {
629 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
630 BUFFER_ADD = 0x28;
631 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
632 }
633
634 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
635 num_bytes -= thisrun_bytes;
636 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
637 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
638
639 if (type != SCAN_IN)
640 {
641 /* add complete bytes */
642 while (thisrun_bytes-- > 0)
643 {
644 BUFFER_ADD = buffer[cur_byte];
645 cur_byte++;
646 bits_left -= 8;
647 }
648 }
649 else /* (type == SCAN_IN) */
650 {
651 bits_left -= 8 * (thisrun_bytes);
652 }
653 }
654
655 /* the most signifcant bit is scanned during TAP movement */
656 if (type != SCAN_IN)
657 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
658 else
659 last_bit = 0;
660
661 /* process remaining bits but the last one */
662 if (bits_left > 1)
663 {
664 if (type == SCAN_IO)
665 {
666 /* Clock Data Bits In and Out LSB First */
667 BUFFER_ADD = 0x3b;
668 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
669 }
670 else if (type == SCAN_OUT)
671 {
672 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
673 BUFFER_ADD = 0x1b;
674 /* LOG_DEBUG("added TDI bits (o)"); */
675 }
676 else if (type == SCAN_IN)
677 {
678 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
679 BUFFER_ADD = 0x2a;
680 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
681 }
682 BUFFER_ADD = bits_left - 2;
683 if (type != SCAN_IN)
684 BUFFER_ADD = buffer[cur_byte];
685 }
686
687 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
688 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
689 {
690 if (type == SCAN_IO)
691 {
692 /* Clock Data Bits In and Out LSB First */
693 BUFFER_ADD = 0x3b;
694 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
695 }
696 else if (type == SCAN_OUT)
697 {
698 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
699 BUFFER_ADD = 0x1b;
700 /* LOG_DEBUG("added TDI bits (o)"); */
701 }
702 else if (type == SCAN_IN)
703 {
704 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
705 BUFFER_ADD = 0x2a;
706 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
707 }
708 BUFFER_ADD = 0x0;
709 BUFFER_ADD = last_bit;
710 }
711 else
712 {
713 /* move from Shift-IR/DR to end state */
714 if (type != SCAN_OUT)
715 {
716 /* Clock Data to TMS/CS Pin with Read */
717 BUFFER_ADD = 0x6b;
718 /* LOG_DEBUG("added TMS scan (read)"); */
719 }
720 else
721 {
722 /* Clock Data to TMS/CS Pin (no Read) */
723 BUFFER_ADD = 0x4b;
724 /* LOG_DEBUG("added TMS scan (no read)"); */
725 }
726 BUFFER_ADD = 0x6; /* scan 7 bits */
727
728 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
729 tap_set_state( tap_get_end_state() );
730 }
731 }
732
733
734 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
735 {
736 int num_bytes = (scan_size + 7) / 8;
737 int bits_left = scan_size;
738 int cur_byte = 0;
739 int last_bit;
740 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
741 u8* receive_pointer = receive_buffer;
742 u32 bytes_written;
743 u32 bytes_read;
744 int retval;
745 int thisrun_read = 0;
746
747 if (cmd->ir_scan)
748 {
749 LOG_ERROR("BUG: large IR scans are not supported");
750 exit(-1);
751 }
752
753 if (tap_get_state() != TAP_DRSHIFT)
754 {
755 /* command "Clock Data to TMS/CS Pin (no Read)" */
756 BUFFER_ADD = 0x4b;
757
758 BUFFER_ADD = 0x6; /* scan 7 bits */
759
760 /* TMS data bits */
761 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
762 tap_set_state(TAP_DRSHIFT);
763 }
764
765 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
766 {
767 LOG_ERROR("couldn't write MPSSE commands to FT2232");
768 exit(-1);
769 }
770 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
771 ft2232_buffer_size = 0;
772
773 /* add command for complete bytes */
774 while (num_bytes > 1)
775 {
776 int thisrun_bytes;
777
778 if (type == SCAN_IO)
779 {
780 /* Clock Data Bytes In and Out LSB First */
781 BUFFER_ADD = 0x39;
782 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
783 }
784 else if (type == SCAN_OUT)
785 {
786 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
787 BUFFER_ADD = 0x19;
788 /* LOG_DEBUG("added TDI bytes (o)"); */
789 }
790 else if (type == SCAN_IN)
791 {
792 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
793 BUFFER_ADD = 0x28;
794 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
795 }
796
797 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
798 thisrun_read = thisrun_bytes;
799 num_bytes -= thisrun_bytes;
800 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
801 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
802
803 if (type != SCAN_IN)
804 {
805 /* add complete bytes */
806 while (thisrun_bytes-- > 0)
807 {
808 BUFFER_ADD = buffer[cur_byte];
809 cur_byte++;
810 bits_left -= 8;
811 }
812 }
813 else /* (type == SCAN_IN) */
814 {
815 bits_left -= 8 * (thisrun_bytes);
816 }
817
818 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
819 {
820 LOG_ERROR("couldn't write MPSSE commands to FT2232");
821 exit(-1);
822 }
823 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
824 ft2232_buffer_size = 0;
825
826 if (type != SCAN_OUT)
827 {
828 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
829 {
830 LOG_ERROR("couldn't read from FT2232");
831 exit(-1);
832 }
833 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
834 receive_pointer += bytes_read;
835 }
836 }
837
838 thisrun_read = 0;
839
840 /* the most signifcant bit is scanned during TAP movement */
841 if (type != SCAN_IN)
842 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
843 else
844 last_bit = 0;
845
846 /* process remaining bits but the last one */
847 if (bits_left > 1)
848 {
849 if (type == SCAN_IO)
850 {
851 /* Clock Data Bits In and Out LSB First */
852 BUFFER_ADD = 0x3b;
853 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
854 }
855 else if (type == SCAN_OUT)
856 {
857 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
858 BUFFER_ADD = 0x1b;
859 /* LOG_DEBUG("added TDI bits (o)"); */
860 }
861 else if (type == SCAN_IN)
862 {
863 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
864 BUFFER_ADD = 0x2a;
865 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
866 }
867 BUFFER_ADD = bits_left - 2;
868 if (type != SCAN_IN)
869 BUFFER_ADD = buffer[cur_byte];
870
871 if (type != SCAN_OUT)
872 thisrun_read += 2;
873 }
874
875 if (tap_get_end_state() == TAP_DRSHIFT)
876 {
877 if (type == SCAN_IO)
878 {
879 /* Clock Data Bits In and Out LSB First */
880 BUFFER_ADD = 0x3b;
881 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
882 }
883 else if (type == SCAN_OUT)
884 {
885 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
886 BUFFER_ADD = 0x1b;
887 /* LOG_DEBUG("added TDI bits (o)"); */
888 }
889 else if (type == SCAN_IN)
890 {
891 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
892 BUFFER_ADD = 0x2a;
893 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
894 }
895 BUFFER_ADD = 0x0;
896 BUFFER_ADD = last_bit;
897 }
898 else
899 {
900 /* move from Shift-IR/DR to end state */
901 if (type != SCAN_OUT)
902 {
903 /* Clock Data to TMS/CS Pin with Read */
904 BUFFER_ADD = 0x6b;
905 /* LOG_DEBUG("added TMS scan (read)"); */
906 }
907 else
908 {
909 /* Clock Data to TMS/CS Pin (no Read) */
910 BUFFER_ADD = 0x4b;
911 /* LOG_DEBUG("added TMS scan (no read)"); */
912 }
913 BUFFER_ADD = 0x6;
914 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
915 tap_set_state( tap_get_end_state() );
916 }
917
918 if (type != SCAN_OUT)
919 thisrun_read += 1;
920
921 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
922 {
923 LOG_ERROR("couldn't write MPSSE commands to FT2232");
924 exit(-1);
925 }
926 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
927 ft2232_buffer_size = 0;
928
929 if (type != SCAN_OUT)
930 {
931 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
932 {
933 LOG_ERROR("couldn't read from FT2232");
934 exit(-1);
935 }
936 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
937 receive_pointer += bytes_read;
938 }
939
940 return ERROR_OK;
941 }
942
943
944 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
945 {
946 int predicted_size = 3;
947 int num_bytes = (scan_size - 1) / 8;
948
949 if (tap_get_state() != TAP_DRSHIFT)
950 predicted_size += 3;
951
952 if (type == SCAN_IN) /* only from device to host */
953 {
954 /* complete bytes */
955 predicted_size += CEIL(num_bytes, 65536) * 3;
956 /* remaining bits - 1 (up to 7) */
957 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
958 }
959 else /* host to device, or bidirectional */
960 {
961 /* complete bytes */
962 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
963 /* remaining bits -1 (up to 7) */
964 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
965 }
966
967 return predicted_size;
968 }
969
970
971 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
972 {
973 int predicted_size = 0;
974
975 if (type != SCAN_OUT)
976 {
977 /* complete bytes */
978 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
979
980 /* remaining bits - 1 */
981 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
982
983 /* last bit (from TMS scan) */
984 predicted_size += 1;
985 }
986
987 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
988
989 return predicted_size;
990 }
991
992
993 static void usbjtag_reset(int trst, int srst)
994 {
995 if (trst == 1)
996 {
997 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
998 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
999 else
1000 low_output &= ~nTRST; /* switch output low */
1001 }
1002 else if (trst == 0)
1003 {
1004 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1005 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1006 else
1007 low_output |= nTRST; /* switch output high */
1008 }
1009
1010 if (srst == 1)
1011 {
1012 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1013 low_output &= ~nSRST; /* switch output low */
1014 else
1015 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1016 }
1017 else if (srst == 0)
1018 {
1019 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1020 low_output |= nSRST; /* switch output high */
1021 else
1022 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1023 }
1024
1025 /* command "set data bits low byte" */
1026 BUFFER_ADD = 0x80;
1027 BUFFER_ADD = low_output;
1028 BUFFER_ADD = low_direction;
1029 }
1030
1031
1032 static void jtagkey_reset(int trst, int srst)
1033 {
1034 if (trst == 1)
1035 {
1036 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1037 high_output &= ~nTRSTnOE;
1038 else
1039 high_output &= ~nTRST;
1040 }
1041 else if (trst == 0)
1042 {
1043 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1044 high_output |= nTRSTnOE;
1045 else
1046 high_output |= nTRST;
1047 }
1048
1049 if (srst == 1)
1050 {
1051 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1052 high_output &= ~nSRST;
1053 else
1054 high_output &= ~nSRSTnOE;
1055 }
1056 else if (srst == 0)
1057 {
1058 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1059 high_output |= nSRST;
1060 else
1061 high_output |= nSRSTnOE;
1062 }
1063
1064 /* command "set data bits high byte" */
1065 BUFFER_ADD = 0x82;
1066 BUFFER_ADD = high_output;
1067 BUFFER_ADD = high_direction;
1068 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1069 high_direction);
1070 }
1071
1072
1073 static void olimex_jtag_reset(int trst, int srst)
1074 {
1075 if (trst == 1)
1076 {
1077 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1078 high_output &= ~nTRSTnOE;
1079 else
1080 high_output &= ~nTRST;
1081 }
1082 else if (trst == 0)
1083 {
1084 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1085 high_output |= nTRSTnOE;
1086 else
1087 high_output |= nTRST;
1088 }
1089
1090 if (srst == 1)
1091 {
1092 high_output |= nSRST;
1093 }
1094 else if (srst == 0)
1095 {
1096 high_output &= ~nSRST;
1097 }
1098
1099 /* command "set data bits high byte" */
1100 BUFFER_ADD = 0x82;
1101 BUFFER_ADD = high_output;
1102 BUFFER_ADD = high_direction;
1103 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1104 high_direction);
1105 }
1106
1107
1108 static void axm0432_jtag_reset(int trst, int srst)
1109 {
1110 if (trst == 1)
1111 {
1112 tap_set_state(TAP_RESET);
1113 high_output &= ~nTRST;
1114 }
1115 else if (trst == 0)
1116 {
1117 high_output |= nTRST;
1118 }
1119
1120 if (srst == 1)
1121 {
1122 high_output &= ~nSRST;
1123 }
1124 else if (srst == 0)
1125 {
1126 high_output |= nSRST;
1127 }
1128
1129 /* command "set data bits low byte" */
1130 BUFFER_ADD = 0x82;
1131 BUFFER_ADD = high_output;
1132 BUFFER_ADD = high_direction;
1133 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1134 high_direction);
1135 }
1136
1137
1138 static void flyswatter_reset(int trst, int srst)
1139 {
1140 if (trst == 1)
1141 {
1142 low_output &= ~nTRST;
1143 }
1144 else if (trst == 0)
1145 {
1146 low_output |= nTRST;
1147 }
1148
1149 if (srst == 1)
1150 {
1151 low_output |= nSRST;
1152 }
1153 else if (srst == 0)
1154 {
1155 low_output &= ~nSRST;
1156 }
1157
1158 /* command "set data bits low byte" */
1159 BUFFER_ADD = 0x80;
1160 BUFFER_ADD = low_output;
1161 BUFFER_ADD = low_direction;
1162 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1163 }
1164
1165
1166 static void turtle_reset(int trst, int srst)
1167 {
1168 trst = trst;
1169
1170 if (srst == 1)
1171 {
1172 low_output |= nSRST;
1173 }
1174 else if (srst == 0)
1175 {
1176 low_output &= ~nSRST;
1177 }
1178
1179 /* command "set data bits low byte" */
1180 BUFFER_ADD = 0x80;
1181 BUFFER_ADD = low_output;
1182 BUFFER_ADD = low_direction;
1183 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1184 }
1185
1186
1187 static void comstick_reset(int trst, int srst)
1188 {
1189 if (trst == 1)
1190 {
1191 high_output &= ~nTRST;
1192 }
1193 else if (trst == 0)
1194 {
1195 high_output |= nTRST;
1196 }
1197
1198 if (srst == 1)
1199 {
1200 high_output &= ~nSRST;
1201 }
1202 else if (srst == 0)
1203 {
1204 high_output |= nSRST;
1205 }
1206
1207 /* command "set data bits high byte" */
1208 BUFFER_ADD = 0x82;
1209 BUFFER_ADD = high_output;
1210 BUFFER_ADD = high_direction;
1211 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1212 high_direction);
1213 }
1214
1215
1216 static void stm32stick_reset(int trst, int srst)
1217 {
1218 if (trst == 1)
1219 {
1220 high_output &= ~nTRST;
1221 }
1222 else if (trst == 0)
1223 {
1224 high_output |= nTRST;
1225 }
1226
1227 if (srst == 1)
1228 {
1229 low_output &= ~nSRST;
1230 }
1231 else if (srst == 0)
1232 {
1233 low_output |= nSRST;
1234 }
1235
1236 /* command "set data bits low byte" */
1237 BUFFER_ADD = 0x80;
1238 BUFFER_ADD = low_output;
1239 BUFFER_ADD = low_direction;
1240
1241 /* command "set data bits high byte" */
1242 BUFFER_ADD = 0x82;
1243 BUFFER_ADD = high_output;
1244 BUFFER_ADD = high_direction;
1245 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1246 high_direction);
1247 }
1248
1249
1250
1251 static void sheevaplug_reset(int trst, int srst)
1252 {
1253 if (trst == 1)
1254 high_output &= ~nTRST;
1255 else if (trst == 0)
1256 high_output |= nTRST;
1257
1258 if (srst == 1)
1259 high_output &= ~nSRSTnOE;
1260 else if (srst == 0)
1261 high_output |= nSRSTnOE;
1262
1263 /* command "set data bits high byte" */
1264 BUFFER_ADD = 0x82;
1265 BUFFER_ADD = high_output;
1266 BUFFER_ADD = high_direction;
1267 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1268 }
1269
1270 static int ft2232_execute_queue()
1271 {
1272 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1273 u8* buffer;
1274 int scan_size; /* size of IR or DR scan */
1275 enum scan_type type;
1276 int i;
1277 int predicted_size = 0;
1278 int retval;
1279
1280 first_unsent = cmd; /* next command that has to be sent */
1281 require_send = 0;
1282
1283 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1284 * that wasn't handled by a caller-provided error handler
1285 */
1286 retval = ERROR_OK;
1287
1288 ft2232_buffer_size = 0;
1289 ft2232_expect_read = 0;
1290
1291 /* blink, if the current layout has that feature */
1292 if (layout->blink)
1293 layout->blink();
1294
1295 while (cmd)
1296 {
1297 switch (cmd->type)
1298 {
1299 case JTAG_END_STATE:
1300 if (cmd->cmd.end_state->end_state != TAP_INVALID)
1301 ft2232_end_state(cmd->cmd.end_state->end_state);
1302 break;
1303
1304 case JTAG_RESET:
1305 /* only send the maximum buffer size that FT2232C can handle */
1306 predicted_size = 3;
1307 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1308 {
1309 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1310 retval = ERROR_JTAG_QUEUE_FAILED;
1311 require_send = 0;
1312 first_unsent = cmd;
1313 }
1314
1315 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1316 {
1317 tap_set_state(TAP_RESET);
1318 }
1319 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1320 require_send = 1;
1321
1322 #ifdef _DEBUG_JTAG_IO_
1323 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1324 #endif
1325 break;
1326
1327 case JTAG_RUNTEST:
1328 /* only send the maximum buffer size that FT2232C can handle */
1329 predicted_size = 0;
1330 if (tap_get_state() != TAP_IDLE)
1331 predicted_size += 3;
1332 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1333 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1334 predicted_size += 3;
1335 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1336 predicted_size += 3;
1337 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1338 {
1339 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1340 retval = ERROR_JTAG_QUEUE_FAILED;
1341 require_send = 0;
1342 first_unsent = cmd;
1343 }
1344 if (tap_get_state() != TAP_IDLE)
1345 {
1346 /* command "Clock Data to TMS/CS Pin (no Read)" */
1347 BUFFER_ADD = 0x4b;
1348 BUFFER_ADD = 0x6; /* scan 7 bits */
1349
1350 /* TMS data bits */
1351 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IDLE);
1352 tap_set_state(TAP_IDLE);
1353 require_send = 1;
1354 }
1355 i = cmd->cmd.runtest->num_cycles;
1356 while (i > 0)
1357 {
1358 /* command "Clock Data to TMS/CS Pin (no Read)" */
1359 BUFFER_ADD = 0x4b;
1360
1361 /* scan 7 bits */
1362 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1363
1364 /* TMS data bits */
1365 BUFFER_ADD = 0x0;
1366 tap_set_state(TAP_IDLE);
1367 i -= (i > 7) ? 7 : i;
1368 /* LOG_DEBUG("added TMS scan (no read)"); */
1369 }
1370
1371 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1372 ft2232_end_state(cmd->cmd.runtest->end_state);
1373
1374 if ( tap_get_state() != tap_get_end_state() )
1375 {
1376 /* command "Clock Data to TMS/CS Pin (no Read)" */
1377 BUFFER_ADD = 0x4b;
1378 /* scan 7 bit */
1379 BUFFER_ADD = 0x6;
1380 /* TMS data bits */
1381 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1382 tap_set_state( tap_get_end_state() );
1383 /* LOG_DEBUG("added TMS scan (no read)"); */
1384 }
1385 require_send = 1;
1386 #ifdef _DEBUG_JTAG_IO_
1387 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1388 #endif
1389 break;
1390
1391 case JTAG_STATEMOVE:
1392 /* only send the maximum buffer size that FT2232C can handle */
1393 predicted_size = 3;
1394 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1395 {
1396 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1397 retval = ERROR_JTAG_QUEUE_FAILED;
1398 require_send = 0;
1399 first_unsent = cmd;
1400 }
1401 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1402 ft2232_end_state(cmd->cmd.statemove->end_state);
1403
1404 /* command "Clock Data to TMS/CS Pin (no Read)" */
1405 BUFFER_ADD = 0x4b;
1406
1407 BUFFER_ADD = 0x6; /* scan 7 bits */
1408
1409 /* TMS data bits */
1410 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1411 /* LOG_DEBUG("added TMS scan (no read)"); */
1412 tap_set_state( tap_get_end_state() );
1413 require_send = 1;
1414 #ifdef _DEBUG_JTAG_IO_
1415 LOG_DEBUG( "statemove: %s", tap_state_name( tap_get_end_state() ) );
1416 #endif
1417 break;
1418
1419 case JTAG_PATHMOVE:
1420 /* only send the maximum buffer size that FT2232C can handle */
1421 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1422 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1423 {
1424 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1425 retval = ERROR_JTAG_QUEUE_FAILED;
1426 require_send = 0;
1427 first_unsent = cmd;
1428 }
1429 ft2232_add_pathmove(cmd->cmd.pathmove);
1430 require_send = 1;
1431 #ifdef _DEBUG_JTAG_IO_
1432 LOG_DEBUG( "pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1433 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]) );
1434 #endif
1435 break;
1436
1437 case JTAG_SCAN:
1438 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1439 type = jtag_scan_type(cmd->cmd.scan);
1440 predicted_size = ft2232_predict_scan_out(scan_size, type);
1441 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1442 {
1443 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1444 /* unsent commands before this */
1445 if (first_unsent != cmd)
1446 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1447 retval = ERROR_JTAG_QUEUE_FAILED;
1448
1449 /* current command */
1450 if (cmd->cmd.scan->end_state != TAP_INVALID)
1451 ft2232_end_state(cmd->cmd.scan->end_state);
1452 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1453 require_send = 0;
1454 first_unsent = cmd->next;
1455 if (buffer)
1456 free(buffer);
1457 break;
1458 }
1459 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1460 {
1461 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1462 first_unsent,
1463 cmd);
1464 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1465 retval = ERROR_JTAG_QUEUE_FAILED;
1466 require_send = 0;
1467 first_unsent = cmd;
1468 }
1469 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1470 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1471 if (cmd->cmd.scan->end_state != TAP_INVALID)
1472 ft2232_end_state(cmd->cmd.scan->end_state);
1473 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1474 require_send = 1;
1475 if (buffer)
1476 free(buffer);
1477 #ifdef _DEBUG_JTAG_IO_
1478 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1479 tap_state_name( tap_get_end_state() ) );
1480 #endif
1481 break;
1482
1483 case JTAG_SLEEP:
1484 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1485 retval = ERROR_JTAG_QUEUE_FAILED;
1486 first_unsent = cmd->next;
1487 jtag_sleep(cmd->cmd.sleep->us);
1488 #ifdef _DEBUG_JTAG_IO_
1489 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1490 #endif
1491 break;
1492
1493 case JTAG_STABLECLOCKS:
1494
1495 /* this is only allowed while in a stable state. A check for a stable
1496 * state was done in jtag_add_clocks()
1497 */
1498 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1499 retval = ERROR_JTAG_QUEUE_FAILED;
1500 #ifdef _DEBUG_JTAG_IO_
1501 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1502 #endif
1503 break;
1504
1505 default:
1506 LOG_ERROR("BUG: unknown JTAG command type encountered");
1507 exit(-1);
1508 }
1509
1510 cmd = cmd->next;
1511 }
1512
1513 if (require_send > 0)
1514 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1515 retval = ERROR_JTAG_QUEUE_FAILED;
1516
1517 return retval;
1518 }
1519
1520
1521 #if BUILD_FT2232_FTD2XX == 1
1522 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1523 {
1524 FT_STATUS status;
1525 DWORD openex_flags = 0;
1526 char* openex_string = NULL;
1527 u8 latency_timer;
1528
1529 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1530
1531 #if IS_WIN32 == 0
1532 /* Add non-standard Vid/Pid to the linux driver */
1533 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1534 {
1535 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1536 }
1537 #endif
1538
1539 if (ft2232_device_desc && ft2232_serial)
1540 {
1541 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1542 ft2232_device_desc = NULL;
1543 }
1544
1545 if (ft2232_device_desc)
1546 {
1547 openex_string = ft2232_device_desc;
1548 openex_flags = FT_OPEN_BY_DESCRIPTION;
1549 }
1550 else if (ft2232_serial)
1551 {
1552 openex_string = ft2232_serial;
1553 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1554 }
1555 else
1556 {
1557 LOG_ERROR("neither device description nor serial number specified");
1558 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1559
1560 return ERROR_JTAG_INIT_FAILED;
1561 }
1562
1563 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1564 if( status != FT_OK ){
1565 // under Win32, the FTD2XX driver appends an "A" to the end
1566 // of the description, if we tried by the desc, then
1567 // try by the alternate "A" description.
1568 if( openex_string == ft2232_device_desc ){
1569 // Try the alternate method.
1570 openex_string = ft2232_device_desc_A;
1571 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1572 if( status == FT_OK ){
1573 // yea, the "alternate" method worked!
1574 } else {
1575 // drat, give the user a meaningfull message.
1576 // telling the use we tried *BOTH* methods.
1577 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1578 ft2232_device_desc,
1579 ft2232_device_desc_A );
1580 }
1581 }
1582 }
1583
1584 if ( status != FT_OK )
1585 {
1586 DWORD num_devices;
1587
1588 if (more)
1589 {
1590 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1591 *try_more = 1;
1592 return ERROR_JTAG_INIT_FAILED;
1593 }
1594 LOG_ERROR("unable to open ftdi device: %lu", status);
1595 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1596 if (status == FT_OK)
1597 {
1598 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1599 u32 i;
1600
1601 for (i = 0; i < num_devices; i++)
1602 desc_array[i] = malloc(64);
1603
1604 desc_array[num_devices] = NULL;
1605
1606 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1607
1608 if (status == FT_OK)
1609 {
1610 LOG_ERROR("ListDevices: %lu\n", num_devices);
1611 for (i = 0; i < num_devices; i++)
1612 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1613 }
1614
1615 for (i = 0; i < num_devices; i++)
1616 free(desc_array[i]);
1617
1618 free(desc_array);
1619 }
1620 else
1621 {
1622 LOG_ERROR("ListDevices: NONE\n");
1623 }
1624 return ERROR_JTAG_INIT_FAILED;
1625 }
1626
1627 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1628 {
1629 LOG_ERROR("unable to set latency timer: %lu", status);
1630 return ERROR_JTAG_INIT_FAILED;
1631 }
1632
1633 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1634 {
1635 LOG_ERROR("unable to get latency timer: %lu", status);
1636 return ERROR_JTAG_INIT_FAILED;
1637 }
1638 else
1639 {
1640 LOG_DEBUG("current latency timer: %i", latency_timer);
1641 }
1642
1643 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1644 {
1645 LOG_ERROR("unable to set timeouts: %lu", status);
1646 return ERROR_JTAG_INIT_FAILED;
1647 }
1648
1649 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1650 {
1651 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1652 return ERROR_JTAG_INIT_FAILED;
1653 }
1654
1655 return ERROR_OK;
1656 }
1657
1658
1659 static int ft2232_purge_ftd2xx(void)
1660 {
1661 FT_STATUS status;
1662
1663 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1664 {
1665 LOG_ERROR("error purging ftd2xx device: %lu", status);
1666 return ERROR_JTAG_INIT_FAILED;
1667 }
1668
1669 return ERROR_OK;
1670 }
1671
1672
1673 #endif /* BUILD_FT2232_FTD2XX == 1 */
1674
1675 #if BUILD_FT2232_LIBFTDI == 1
1676 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1677 {
1678 u8 latency_timer;
1679
1680 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1681 ft2232_layout, vid, pid);
1682
1683 if (ftdi_init(&ftdic) < 0)
1684 return ERROR_JTAG_INIT_FAILED;
1685
1686 /* context, vendor id, product id */
1687 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1688 ft2232_serial) < 0)
1689 {
1690 if (more)
1691 LOG_WARNING("unable to open ftdi device (trying more): %s",
1692 ftdic.error_str);
1693 else
1694 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1695 *try_more = 1;
1696 return ERROR_JTAG_INIT_FAILED;
1697 }
1698
1699 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1700 {
1701 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1702 return ERROR_JTAG_INIT_FAILED;
1703 }
1704
1705 if (ftdi_usb_reset(&ftdic) < 0)
1706 {
1707 LOG_ERROR("unable to reset ftdi device");
1708 return ERROR_JTAG_INIT_FAILED;
1709 }
1710
1711 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1712 {
1713 LOG_ERROR("unable to set latency timer");
1714 return ERROR_JTAG_INIT_FAILED;
1715 }
1716
1717 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1718 {
1719 LOG_ERROR("unable to get latency timer");
1720 return ERROR_JTAG_INIT_FAILED;
1721 }
1722 else
1723 {
1724 LOG_DEBUG("current latency timer: %i", latency_timer);
1725 }
1726
1727 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1728
1729 return ERROR_OK;
1730 }
1731
1732
1733 static int ft2232_purge_libftdi(void)
1734 {
1735 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1736 {
1737 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1738 return ERROR_JTAG_INIT_FAILED;
1739 }
1740
1741 return ERROR_OK;
1742 }
1743
1744
1745 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1746
1747 static int ft2232_init(void)
1748 {
1749 u8 buf[1];
1750 int retval;
1751 u32 bytes_written;
1752 ft2232_layout_t* cur_layout = ft2232_layouts;
1753 int i;
1754
1755 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1756 {
1757 ft2232_layout = "usbjtag";
1758 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1759 }
1760
1761 while (cur_layout->name)
1762 {
1763 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1764 {
1765 layout = cur_layout;
1766 break;
1767 }
1768 cur_layout++;
1769 }
1770
1771 if (!layout)
1772 {
1773 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1774 return ERROR_JTAG_INIT_FAILED;
1775 }
1776
1777 for (i = 0; 1; i++)
1778 {
1779 /*
1780 * "more indicates that there are more IDs to try, so we should
1781 * not print an error for an ID mismatch (but for anything
1782 * else, we should).
1783 *
1784 * try_more indicates that the error code returned indicates an
1785 * ID mismatch (and nothing else) and that we should proceeed
1786 * with the next ID pair.
1787 */
1788 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
1789 int try_more = 0;
1790
1791 #if BUILD_FT2232_FTD2XX == 1
1792 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1793 more, &try_more);
1794 #elif BUILD_FT2232_LIBFTDI == 1
1795 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1796 more, &try_more);
1797 #endif
1798 if (retval >= 0)
1799 break;
1800 if (!more || !try_more)
1801 return retval;
1802 }
1803
1804 ft2232_buffer_size = 0;
1805 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1806
1807 if (layout->init() != ERROR_OK)
1808 return ERROR_JTAG_INIT_FAILED;
1809
1810 ft2232_speed(jtag_speed);
1811
1812 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1813 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
1814 {
1815 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1816 return ERROR_JTAG_INIT_FAILED;
1817 }
1818
1819 #if BUILD_FT2232_FTD2XX == 1
1820 return ft2232_purge_ftd2xx();
1821 #elif BUILD_FT2232_LIBFTDI == 1
1822 return ft2232_purge_libftdi();
1823 #endif
1824
1825 return ERROR_OK;
1826 }
1827
1828
1829 static int usbjtag_init(void)
1830 {
1831 u8 buf[3];
1832 u32 bytes_written;
1833
1834 low_output = 0x08;
1835 low_direction = 0x0b;
1836
1837 if (strcmp(ft2232_layout, "usbjtag") == 0)
1838 {
1839 nTRST = 0x10;
1840 nTRSTnOE = 0x10;
1841 nSRST = 0x40;
1842 nSRSTnOE = 0x40;
1843 }
1844 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1845 {
1846 nTRST = 0x10;
1847 nTRSTnOE = 0x10;
1848 nSRST = 0x20;
1849 nSRSTnOE = 0x20;
1850 }
1851 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1852 {
1853 nTRST = 0x0;
1854 nTRSTnOE = 0x00;
1855 nSRST = 0x20;
1856 nSRSTnOE = 0x20;
1857 low_output = 0x88;
1858 low_direction = 0x8b;
1859 }
1860 else
1861 {
1862 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1863 return ERROR_JTAG_INIT_FAILED;
1864 }
1865
1866 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1867 {
1868 low_direction &= ~nTRSTnOE; /* nTRST input */
1869 low_output &= ~nTRST; /* nTRST = 0 */
1870 }
1871 else
1872 {
1873 low_direction |= nTRSTnOE; /* nTRST output */
1874 low_output |= nTRST; /* nTRST = 1 */
1875 }
1876
1877 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1878 {
1879 low_direction |= nSRSTnOE; /* nSRST output */
1880 low_output |= nSRST; /* nSRST = 1 */
1881 }
1882 else
1883 {
1884 low_direction &= ~nSRSTnOE; /* nSRST input */
1885 low_output &= ~nSRST; /* nSRST = 0 */
1886 }
1887
1888 /* initialize low byte for jtag */
1889 buf[0] = 0x80; /* command "set data bits low byte" */
1890 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1891 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1892 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1893
1894 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1895 {
1896 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1897 return ERROR_JTAG_INIT_FAILED;
1898 }
1899
1900 return ERROR_OK;
1901 }
1902
1903
1904 static int axm0432_jtag_init(void)
1905 {
1906 u8 buf[3];
1907 u32 bytes_written;
1908
1909 low_output = 0x08;
1910 low_direction = 0x2b;
1911
1912 /* initialize low byte for jtag */
1913 buf[0] = 0x80; /* command "set data bits low byte" */
1914 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1915 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1916 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1917
1918 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1919 {
1920 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1921 return ERROR_JTAG_INIT_FAILED;
1922 }
1923
1924 if (strcmp(layout->name, "axm0432_jtag") == 0)
1925 {
1926 nTRST = 0x08;
1927 nTRSTnOE = 0x0; /* No output enable for TRST*/
1928 nSRST = 0x04;
1929 nSRSTnOE = 0x0; /* No output enable for SRST*/
1930 }
1931 else
1932 {
1933 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1934 exit(-1);
1935 }
1936
1937 high_output = 0x0;
1938 high_direction = 0x0c;
1939
1940 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1941 {
1942 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1943 }
1944 else
1945 {
1946 high_output |= nTRST;
1947 }
1948
1949 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1950 {
1951 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1952 }
1953 else
1954 {
1955 high_output |= nSRST;
1956 }
1957
1958 /* initialize high port */
1959 buf[0] = 0x82; /* command "set data bits high byte" */
1960 buf[1] = high_output; /* value */
1961 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1962 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1963
1964 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1965 {
1966 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1967 return ERROR_JTAG_INIT_FAILED;
1968 }
1969
1970 return ERROR_OK;
1971 }
1972
1973
1974 static int jtagkey_init(void)
1975 {
1976 u8 buf[3];
1977 u32 bytes_written;
1978
1979 low_output = 0x08;
1980 low_direction = 0x1b;
1981
1982 /* initialize low byte for jtag */
1983 buf[0] = 0x80; /* command "set data bits low byte" */
1984 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1985 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1986 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1987
1988 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1989 {
1990 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1991 return ERROR_JTAG_INIT_FAILED;
1992 }
1993
1994 if (strcmp(layout->name, "jtagkey") == 0)
1995 {
1996 nTRST = 0x01;
1997 nTRSTnOE = 0x4;
1998 nSRST = 0x02;
1999 nSRSTnOE = 0x08;
2000 }
2001 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2002 || (strcmp(layout->name, "oocdlink") == 0) )
2003 {
2004 nTRST = 0x02;
2005 nTRSTnOE = 0x1;
2006 nSRST = 0x08;
2007 nSRSTnOE = 0x04;
2008 }
2009 else
2010 {
2011 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2012 exit(-1);
2013 }
2014
2015 high_output = 0x0;
2016 high_direction = 0x0f;
2017
2018 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2019 {
2020 high_output |= nTRSTnOE;
2021 high_output &= ~nTRST;
2022 }
2023 else
2024 {
2025 high_output &= ~nTRSTnOE;
2026 high_output |= nTRST;
2027 }
2028
2029 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2030 {
2031 high_output &= ~nSRSTnOE;
2032 high_output |= nSRST;
2033 }
2034 else
2035 {
2036 high_output |= nSRSTnOE;
2037 high_output &= ~nSRST;
2038 }
2039
2040 /* initialize high port */
2041 buf[0] = 0x82; /* command "set data bits high byte" */
2042 buf[1] = high_output; /* value */
2043 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2044 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2045
2046 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2047 {
2048 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2049 return ERROR_JTAG_INIT_FAILED;
2050 }
2051
2052 return ERROR_OK;
2053 }
2054
2055
2056 static int olimex_jtag_init(void)
2057 {
2058 u8 buf[3];
2059 u32 bytes_written;
2060
2061 low_output = 0x08;
2062 low_direction = 0x1b;
2063
2064 /* initialize low byte for jtag */
2065 buf[0] = 0x80; /* command "set data bits low byte" */
2066 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2067 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2068 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2069
2070 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2071 {
2072 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2073 return ERROR_JTAG_INIT_FAILED;
2074 }
2075
2076 nTRST = 0x01;
2077 nTRSTnOE = 0x4;
2078 nSRST = 0x02;
2079 nSRSTnOE = 0x00; /* no output enable for nSRST */
2080
2081 high_output = 0x0;
2082 high_direction = 0x0f;
2083
2084 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2085 {
2086 high_output |= nTRSTnOE;
2087 high_output &= ~nTRST;
2088 }
2089 else
2090 {
2091 high_output &= ~nTRSTnOE;
2092 high_output |= nTRST;
2093 }
2094
2095 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2096 {
2097 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2098 }
2099 else
2100 {
2101 high_output &= ~nSRST;
2102 }
2103
2104 /* turn red LED on */
2105 high_output |= 0x08;
2106
2107 /* initialize high port */
2108 buf[0] = 0x82; /* command "set data bits high byte" */
2109 buf[1] = high_output; /* value */
2110 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2111 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2112
2113 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2114 {
2115 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2116 return ERROR_JTAG_INIT_FAILED;
2117 }
2118
2119 return ERROR_OK;
2120 }
2121
2122
2123 static int flyswatter_init(void)
2124 {
2125 u8 buf[3];
2126 u32 bytes_written;
2127
2128 low_output = 0x18;
2129 low_direction = 0xfb;
2130
2131 /* initialize low byte for jtag */
2132 buf[0] = 0x80; /* command "set data bits low byte" */
2133 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2134 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2135 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2136
2137 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2138 {
2139 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2140 return ERROR_JTAG_INIT_FAILED;
2141 }
2142
2143 nTRST = 0x10;
2144 nTRSTnOE = 0x0; /* not output enable for nTRST */
2145 nSRST = 0x20;
2146 nSRSTnOE = 0x00; /* no output enable for nSRST */
2147
2148 high_output = 0x00;
2149 high_direction = 0x0c;
2150
2151 /* turn red LED3 on, LED2 off */
2152 high_output |= 0x08;
2153
2154 /* initialize high port */
2155 buf[0] = 0x82; /* command "set data bits high byte" */
2156 buf[1] = high_output; /* value */
2157 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2158 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2159
2160 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2161 {
2162 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2163 return ERROR_JTAG_INIT_FAILED;
2164 }
2165
2166 return ERROR_OK;
2167 }
2168
2169
2170 static int turtle_init(void)
2171 {
2172 u8 buf[3];
2173 u32 bytes_written;
2174
2175 low_output = 0x08;
2176 low_direction = 0x5b;
2177
2178 /* initialize low byte for jtag */
2179 buf[0] = 0x80; /* command "set data bits low byte" */
2180 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2181 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2182 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2183
2184 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2185 {
2186 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2187 return ERROR_JTAG_INIT_FAILED;
2188 }
2189
2190 nSRST = 0x40;
2191
2192 high_output = 0x00;
2193 high_direction = 0x0C;
2194
2195 /* initialize high port */
2196 buf[0] = 0x82; /* command "set data bits high byte" */
2197 buf[1] = high_output;
2198 buf[2] = high_direction;
2199 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2200
2201 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2202 {
2203 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2204 return ERROR_JTAG_INIT_FAILED;
2205 }
2206
2207 return ERROR_OK;
2208 }
2209
2210
2211 static int comstick_init(void)
2212 {
2213 u8 buf[3];
2214 u32 bytes_written;
2215
2216 low_output = 0x08;
2217 low_direction = 0x0b;
2218
2219 /* initialize low byte for jtag */
2220 buf[0] = 0x80; /* command "set data bits low byte" */
2221 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2222 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2223 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2224
2225 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2226 {
2227 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2228 return ERROR_JTAG_INIT_FAILED;
2229 }
2230
2231 nTRST = 0x01;
2232 nTRSTnOE = 0x00; /* no output enable for nTRST */
2233 nSRST = 0x02;
2234 nSRSTnOE = 0x00; /* no output enable for nSRST */
2235
2236 high_output = 0x03;
2237 high_direction = 0x03;
2238
2239 /* initialize high port */
2240 buf[0] = 0x82; /* command "set data bits high byte" */
2241 buf[1] = high_output;
2242 buf[2] = high_direction;
2243 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2244
2245 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2246 {
2247 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2248 return ERROR_JTAG_INIT_FAILED;
2249 }
2250
2251 return ERROR_OK;
2252 }
2253
2254
2255 static int stm32stick_init(void)
2256 {
2257 u8 buf[3];
2258 u32 bytes_written;
2259
2260 low_output = 0x88;
2261 low_direction = 0x8b;
2262
2263 /* initialize low byte for jtag */
2264 buf[0] = 0x80; /* command "set data bits low byte" */
2265 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2266 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2267 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2268
2269 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2270 {
2271 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2272 return ERROR_JTAG_INIT_FAILED;
2273 }
2274
2275 nTRST = 0x01;
2276 nTRSTnOE = 0x00; /* no output enable for nTRST */
2277 nSRST = 0x80;
2278 nSRSTnOE = 0x00; /* no output enable for nSRST */
2279
2280 high_output = 0x01;
2281 high_direction = 0x03;
2282
2283 /* initialize high port */
2284 buf[0] = 0x82; /* command "set data bits high byte" */
2285 buf[1] = high_output;
2286 buf[2] = high_direction;
2287 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2288
2289 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2290 {
2291 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2292 return ERROR_JTAG_INIT_FAILED;
2293 }
2294
2295 return ERROR_OK;
2296 }
2297
2298
2299 static int sheevaplug_init(void)
2300 {
2301 u8 buf[3];
2302 u32 bytes_written;
2303
2304 low_output = 0x08;
2305 low_direction = 0x1b;
2306
2307 /* initialize low byte for jtag */
2308 buf[0] = 0x80; /* command "set data bits low byte" */
2309 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2310 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2311 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2312
2313 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2314 {
2315 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2316 return ERROR_JTAG_INIT_FAILED;
2317 }
2318
2319 nTRSTnOE = 0x1;
2320 nTRST = 0x02;
2321 nSRSTnOE = 0x4;
2322 nSRST = 0x08;
2323
2324 high_output = 0x0;
2325 high_direction = 0x0f;
2326
2327 /* nTRST is always push-pull */
2328 high_output &= ~nTRSTnOE;
2329 high_output |= nTRST;
2330
2331 /* nSRST is always open-drain */
2332 high_output |= nSRSTnOE;
2333 high_output &= ~nSRST;
2334
2335 /* initialize high port */
2336 buf[0] = 0x82; /* command "set data bits high byte" */
2337 buf[1] = high_output; /* value */
2338 buf[2] = high_direction; /* all outputs - xRST */
2339 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2340
2341 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2342 {
2343 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2344 return ERROR_JTAG_INIT_FAILED;
2345 }
2346
2347 return ERROR_OK;
2348 }
2349
2350 static void olimex_jtag_blink(void)
2351 {
2352 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2353 * ACBUS3 is bit 3 of the GPIOH port
2354 */
2355 if (high_output & 0x08)
2356 {
2357 /* set port pin high */
2358 high_output &= 0x07;
2359 }
2360 else
2361 {
2362 /* set port pin low */
2363 high_output |= 0x08;
2364 }
2365
2366 BUFFER_ADD = 0x82;
2367 BUFFER_ADD = high_output;
2368 BUFFER_ADD = high_direction;
2369 }
2370
2371
2372 static void flyswatter_jtag_blink(void)
2373 {
2374 /*
2375 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2376 */
2377 high_output ^= 0x0c;
2378
2379 BUFFER_ADD = 0x82;
2380 BUFFER_ADD = high_output;
2381 BUFFER_ADD = high_direction;
2382 }
2383
2384
2385 static void turtle_jtag_blink(void)
2386 {
2387 /*
2388 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2389 */
2390 if (high_output & 0x08)
2391 {
2392 high_output = 0x04;
2393 }
2394 else
2395 {
2396 high_output = 0x08;
2397 }
2398
2399 BUFFER_ADD = 0x82;
2400 BUFFER_ADD = high_output;
2401 BUFFER_ADD = high_direction;
2402 }
2403
2404
2405 static int ft2232_quit(void)
2406 {
2407 #if BUILD_FT2232_FTD2XX == 1
2408 FT_STATUS status;
2409
2410 status = FT_Close(ftdih);
2411 #elif BUILD_FT2232_LIBFTDI == 1
2412 ftdi_disable_bitbang(&ftdic);
2413
2414 ftdi_usb_close(&ftdic);
2415
2416 ftdi_deinit(&ftdic);
2417 #endif
2418
2419 free(ft2232_buffer);
2420 ft2232_buffer = NULL;
2421
2422 return ERROR_OK;
2423 }
2424
2425
2426 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2427 {
2428 char *cp;
2429 char buf[200];
2430 if (argc == 1)
2431 {
2432 ft2232_device_desc = strdup(args[0]);
2433 cp = strchr( ft2232_device_desc, 0 );
2434 // under Win32, the FTD2XX driver appends an "A" to the end
2435 // of the description, this examines the given desc
2436 // and creates the 'missing' _A or non_A variable.
2437 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2438 // it was, so make this the "A" version.
2439 ft2232_device_desc_A = ft2232_device_desc;
2440 // and *CREATE* the non-A version.
2441 strcpy( buf, ft2232_device_desc );
2442 cp = strchr( buf, 0 );
2443 cp[-2] = 0;
2444 ft2232_device_desc = strdup( buf );
2445 } else {
2446 // <space>A not defined
2447 // so create it
2448 sprintf( buf, "%s A", ft2232_device_desc );
2449 ft2232_device_desc_A = strdup( buf );
2450 }
2451 }
2452 else
2453 {
2454 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2455 }
2456
2457 return ERROR_OK;
2458 }
2459
2460
2461 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2462 {
2463 if (argc == 1)
2464 {
2465 ft2232_serial = strdup(args[0]);
2466 }
2467 else
2468 {
2469 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2470 }
2471
2472 return ERROR_OK;
2473 }
2474
2475
2476 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2477 {
2478 if (argc == 0)
2479 return ERROR_OK;
2480
2481 ft2232_layout = malloc(strlen(args[0]) + 1);
2482 strcpy(ft2232_layout, args[0]);
2483
2484 return ERROR_OK;
2485 }
2486
2487
2488 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2489 {
2490 int i;
2491
2492 if (argc > MAX_USB_IDS * 2)
2493 {
2494 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2495 "(maximum is %d pairs)", MAX_USB_IDS);
2496 argc = MAX_USB_IDS * 2;
2497 }
2498 if ( argc < 2 || (argc & 1) )
2499 {
2500 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2501 if (argc < 2)
2502 return ERROR_OK;
2503 }
2504
2505 for (i = 0; i + 1 < argc; i += 2)
2506 {
2507 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2508 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2509 }
2510
2511 /*
2512 * Explicitly terminate, in case there are multiples instances of
2513 * ft2232_vid_pid.
2514 */
2515 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2516
2517 return ERROR_OK;
2518 }
2519
2520
2521 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2522 {
2523 if (argc == 1)
2524 {
2525 ft2232_latency = atoi(args[0]);
2526 }
2527 else
2528 {
2529 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2530 }
2531
2532 return ERROR_OK;
2533 }
2534
2535
2536 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2537 {
2538 int retval = 0;
2539
2540 /* 7 bits of either ones or zeros. */
2541 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2542
2543 while (num_cycles > 0)
2544 {
2545 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2546 * at most 7 bits per invocation. Here we invoke it potentially
2547 * several times.
2548 */
2549 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2550
2551 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2552 {
2553 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2554 retval = ERROR_JTAG_QUEUE_FAILED;
2555
2556 first_unsent = cmd;
2557 }
2558
2559 /* command "Clock Data to TMS/CS Pin (no Read)" */
2560 BUFFER_ADD = 0x4b;
2561
2562 /* scan 7 bit */
2563 BUFFER_ADD = bitcount_per_command - 1;
2564
2565 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2566 BUFFER_ADD = tms;
2567
2568 require_send = 1;
2569
2570 num_cycles -= bitcount_per_command;
2571 }
2572
2573 return retval;
2574 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)