1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
25 /* This code uses information contained in the MPSSE specification which was
27 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
28 * Hereafter this is called the "MPSSE Spec".
40 /* project specific includes */
42 #include "time_support.h"
45 /* FT2232 access library includes */
46 #if BUILD_FT2232_FTD2XX == 1
48 #elif BUILD_FT2232_LIBFTDI == 1
52 static int ft2232_execute_queue(void);
54 static int ft2232_speed(int speed);
55 static int ft2232_speed_div(int speed, int* khz);
56 static int ft2232_khz(int khz, int* jtag_speed);
57 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
58 static int ft2232_init(void);
59 static int ft2232_quit(void);
61 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
62 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
63 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
64 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
65 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
69 * Function ft2232_stableclocks
70 * will send out \a num_cycles on the TCK line while the TAP(s)
71 * are in a stable state. Calling code must ensure that current state is
72 * stable, that verification is not done in here.
73 * @param num_cycles is the count of clocks cycles to send.
74 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
76 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
79 static char * ft2232_device_desc_A = NULL;
80 static char* ft2232_device_desc = NULL;
81 static char* ft2232_serial = NULL;
82 static char* ft2232_layout = NULL;
83 static unsigned char ft2232_latency = 2;
86 /* vid = pid = 0 marks the end of the list */
87 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
88 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
90 typedef struct ft2232_layout_s
94 void (*reset)(int trst, int srst);
98 /* init procedures for supported layouts */
99 static int usbjtag_init(void);
100 static int jtagkey_init(void);
101 static int olimex_jtag_init(void);
102 static int flyswatter_init(void);
103 static int turtle_init(void);
104 static int comstick_init(void);
105 static int stm32stick_init(void);
106 static int axm0432_jtag_init(void);
107 static int sheevaplug_init(void);
108 static int icebear_jtag_init(void);
110 /* reset procedures for supported layouts */
111 static void usbjtag_reset(int trst, int srst);
112 static void jtagkey_reset(int trst, int srst);
113 static void olimex_jtag_reset(int trst, int srst);
114 static void flyswatter_reset(int trst, int srst);
115 static void turtle_reset(int trst, int srst);
116 static void comstick_reset(int trst, int srst);
117 static void stm32stick_reset(int trst, int srst);
118 static void axm0432_jtag_reset(int trst, int srst);
119 static void sheevaplug_reset(int trst, int srst);
120 static void icebear_jtag_reset(int trst, int srst);
122 /* blink procedures for layouts that support a blinking led */
123 static void olimex_jtag_blink(void);
124 static void flyswatter_jtag_blink(void);
125 static void turtle_jtag_blink(void);
127 ft2232_layout_t ft2232_layouts[] =
129 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
130 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
131 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
132 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
133 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
134 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
135 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
136 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
137 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
138 { "comstick", comstick_init, comstick_reset, NULL },
139 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
140 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
141 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
142 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
143 { NULL, NULL, NULL, NULL },
146 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
148 static ft2232_layout_t* layout;
149 static u8 low_output = 0x0;
150 static u8 low_direction = 0x0;
151 static u8 high_output = 0x0;
152 static u8 high_direction = 0x0;
154 #if BUILD_FT2232_FTD2XX == 1
155 static FT_HANDLE ftdih = NULL;
156 #elif BUILD_FT2232_LIBFTDI == 1
157 static struct ftdi_context ftdic;
161 static jtag_command_t* first_unsent; /* next command that has to be sent */
162 static int require_send;
164 static u8* ft2232_buffer = NULL;
165 static int ft2232_buffer_size = 0;
166 static int ft2232_read_pointer = 0;
167 static int ft2232_expect_read = 0;
169 #define FT2232_BUFFER_SIZE 131072
170 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
171 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
173 jtag_interface_t ft2232_interface =
176 .execute_queue = ft2232_execute_queue,
177 .speed = ft2232_speed,
178 .speed_div = ft2232_speed_div,
180 .register_commands = ft2232_register_commands,
185 static int ft2232_write(u8* buf, int size, u32* bytes_written)
187 #if BUILD_FT2232_FTD2XX == 1
189 DWORD dw_bytes_written;
190 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
192 *bytes_written = dw_bytes_written;
193 LOG_ERROR("FT_Write returned: %lu", status);
194 return ERROR_JTAG_DEVICE_ERROR;
198 *bytes_written = dw_bytes_written;
201 #elif BUILD_FT2232_LIBFTDI == 1
203 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
206 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
207 return ERROR_JTAG_DEVICE_ERROR;
211 *bytes_written = retval;
218 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
220 #if BUILD_FT2232_FTD2XX == 1
226 while ( (*bytes_read < size) && timeout-- )
228 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
229 *bytes_read, &dw_bytes_read) ) != FT_OK )
232 LOG_ERROR("FT_Read returned: %lu", status);
233 return ERROR_JTAG_DEVICE_ERROR;
235 *bytes_read += dw_bytes_read;
238 #elif BUILD_FT2232_LIBFTDI == 1
243 while ( (*bytes_read < size) && timeout-- )
245 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
248 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
249 return ERROR_JTAG_DEVICE_ERROR;
251 *bytes_read += retval;
256 if (*bytes_read < size)
258 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
259 return ERROR_JTAG_DEVICE_ERROR;
266 static int ft2232_speed(int speed)
272 buf[0] = 0x86; /* command "set divisor" */
273 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
274 buf[2] = (speed >> 8) & 0xff; /* valueH */
276 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
277 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
279 LOG_ERROR("couldn't set FT2232 TCK speed");
287 static int ft2232_speed_div(int speed, int* khz)
289 /* Take a look in the FT2232 manual,
290 * AN2232C-01 Command Processor for
291 * MPSSE and MCU Host Bus. Chapter 3.8 */
293 *khz = 6000 / (1 + speed);
299 static int ft2232_khz(int khz, int* jtag_speed)
303 LOG_ERROR("RCLK not supported");
307 /* Take a look in the FT2232 manual,
308 * AN2232C-01 Command Processor for
309 * MPSSE and MCU Host Bus. Chapter 3.8
311 * We will calc here with a multiplier
312 * of 10 for better rounding later. */
314 /* Calc speed, (6000 / khz) - 1 */
315 /* Use 65000 for better rounding */
316 *jtag_speed = (60000 / khz) - 10;
318 /* Add 0.9 for rounding */
321 /* Calc real speed */
322 *jtag_speed = *jtag_speed / 10;
324 /* Check if speed is greater than 0 */
330 /* Check max value */
331 if (*jtag_speed > 0xFFFF)
333 *jtag_speed = 0xFFFF;
340 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
342 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
343 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
344 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
345 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
346 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
347 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
348 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
349 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
350 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
351 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
356 void ft2232_end_state(tap_state_t state)
358 if (tap_is_state_stable(state))
359 tap_set_end_state(state);
362 LOG_ERROR("BUG: %i is not a valid end state", state);
368 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
370 int num_bytes = (scan_size + 7) / 8;
371 int bits_left = scan_size;
374 while (num_bytes-- > 1)
376 buffer[cur_byte] = BUFFER_READ;
381 buffer[cur_byte] = 0x0;
385 buffer[cur_byte] = BUFFER_READ >> 1;
388 buffer[cur_byte] = ( buffer[cur_byte] | ( (BUFFER_READ & 0x02) << 6 ) ) >> (8 - bits_left);
392 static void ft2232_debug_dump_buffer(void)
398 for (i = 0; i < ft2232_buffer_size; i++)
400 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
403 LOG_DEBUG("%s", line);
409 LOG_DEBUG("%s", line);
413 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
423 #ifdef _DEBUG_USB_IO_
424 struct timeval start, inter, inter2, end;
425 struct timeval d_inter, d_inter2, d_end;
428 #ifdef _DEBUG_USB_COMMS_
429 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
430 ft2232_debug_dump_buffer();
433 #ifdef _DEBUG_USB_IO_
434 gettimeofday(&start, NULL);
437 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
439 LOG_ERROR("couldn't write MPSSE commands to FT2232");
443 #ifdef _DEBUG_USB_IO_
444 gettimeofday(&inter, NULL);
447 if (ft2232_expect_read)
450 ft2232_buffer_size = 0;
452 #ifdef _DEBUG_USB_IO_
453 gettimeofday(&inter2, NULL);
456 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
458 LOG_ERROR("couldn't read from FT2232");
462 #ifdef _DEBUG_USB_IO_
463 gettimeofday(&end, NULL);
465 timeval_subtract(&d_inter, &inter, &start);
466 timeval_subtract(&d_inter2, &inter2, &start);
467 timeval_subtract(&d_end, &end, &start);
469 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
470 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
471 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
472 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
475 ft2232_buffer_size = bytes_read;
477 if (ft2232_expect_read != ft2232_buffer_size)
479 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
482 ft2232_debug_dump_buffer();
487 #ifdef _DEBUG_USB_COMMS_
488 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
489 ft2232_debug_dump_buffer();
493 ft2232_expect_read = 0;
494 ft2232_read_pointer = 0;
496 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
497 * that wasn't handled by a caller-provided error handler
507 type = jtag_scan_type(cmd->cmd.scan);
508 if (type != SCAN_OUT)
510 scan_size = jtag_scan_size(cmd->cmd.scan);
511 buffer = calloc(CEIL(scan_size, 8), 1);
512 ft2232_read_scan(type, buffer, scan_size);
513 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
514 retval = ERROR_JTAG_QUEUE_FAILED;
526 ft2232_buffer_size = 0;
532 static void ft2232_add_pathmove(pathmove_command_t* cmd)
534 int num_states = cmd->num_states;
539 u8 tms_byte = 0; /* zero this on each MPSSE batch */
543 int num_states_batch = num_states > 7 ? 7 : num_states;
545 /* command "Clock Data to TMS/CS Pin (no Read)" */
548 /* number of states remaining */
549 BUFFER_ADD = num_states_batch - 1;
551 while (num_states_batch--)
553 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
554 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
555 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
556 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
559 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
560 tap_get_state() ), tap_state_name(cmd->path[state_count]) );
564 tap_set_state(cmd->path[state_count]);
569 BUFFER_ADD = tms_byte;
572 tap_set_end_state(tap_get_state());
576 void ft2232_add_scan(int ir_scan, enum scan_type type, u8* buffer, int scan_size)
578 int num_bytes = (scan_size + 7) / 8;
579 int bits_left = scan_size;
583 if ( !( ( !ir_scan && (tap_get_state() == TAP_DRSHIFT) )
584 || ( ir_scan && (tap_get_state() == TAP_IRSHIFT) ) ) )
586 /* command "Clock Data to TMS/CS Pin (no Read)" */
589 BUFFER_ADD = 0x6; /* scan 7 bits */
594 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IRSHIFT);
595 tap_set_state(TAP_IRSHIFT);
599 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
600 tap_set_state(TAP_DRSHIFT);
602 /* LOG_DEBUG("added TMS scan (no read)"); */
605 /* add command for complete bytes */
606 while (num_bytes > 1)
611 /* Clock Data Bytes In and Out LSB First */
613 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
615 else if (type == SCAN_OUT)
617 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
619 /* LOG_DEBUG("added TDI bytes (o)"); */
621 else if (type == SCAN_IN)
623 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
625 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
628 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
629 num_bytes -= thisrun_bytes;
630 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
631 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
635 /* add complete bytes */
636 while (thisrun_bytes-- > 0)
638 BUFFER_ADD = buffer[cur_byte];
643 else /* (type == SCAN_IN) */
645 bits_left -= 8 * (thisrun_bytes);
649 /* the most signifcant bit is scanned during TAP movement */
651 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
655 /* process remaining bits but the last one */
660 /* Clock Data Bits In and Out LSB First */
662 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
664 else if (type == SCAN_OUT)
666 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
668 /* LOG_DEBUG("added TDI bits (o)"); */
670 else if (type == SCAN_IN)
672 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
674 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
676 BUFFER_ADD = bits_left - 2;
678 BUFFER_ADD = buffer[cur_byte];
681 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
682 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
686 /* Clock Data Bits In and Out LSB First */
688 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
690 else if (type == SCAN_OUT)
692 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
694 /* LOG_DEBUG("added TDI bits (o)"); */
696 else if (type == SCAN_IN)
698 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
700 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
703 BUFFER_ADD = last_bit;
707 /* move from Shift-IR/DR to end state */
708 if (type != SCAN_OUT)
710 /* Clock Data to TMS/CS Pin with Read */
712 /* LOG_DEBUG("added TMS scan (read)"); */
716 /* Clock Data to TMS/CS Pin (no Read) */
718 /* LOG_DEBUG("added TMS scan (no read)"); */
720 BUFFER_ADD = 0x6; /* scan 7 bits */
722 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
723 tap_set_state( tap_get_end_state() );
728 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
730 int num_bytes = (scan_size + 7) / 8;
731 int bits_left = scan_size;
734 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
735 u8* receive_pointer = receive_buffer;
739 int thisrun_read = 0;
743 LOG_ERROR("BUG: large IR scans are not supported");
747 if (tap_get_state() != TAP_DRSHIFT)
749 /* command "Clock Data to TMS/CS Pin (no Read)" */
752 BUFFER_ADD = 0x6; /* scan 7 bits */
755 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
756 tap_set_state(TAP_DRSHIFT);
759 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
761 LOG_ERROR("couldn't write MPSSE commands to FT2232");
764 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
765 ft2232_buffer_size = 0;
767 /* add command for complete bytes */
768 while (num_bytes > 1)
774 /* Clock Data Bytes In and Out LSB First */
776 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
778 else if (type == SCAN_OUT)
780 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
782 /* LOG_DEBUG("added TDI bytes (o)"); */
784 else if (type == SCAN_IN)
786 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
788 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
791 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
792 thisrun_read = thisrun_bytes;
793 num_bytes -= thisrun_bytes;
794 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
795 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
799 /* add complete bytes */
800 while (thisrun_bytes-- > 0)
802 BUFFER_ADD = buffer[cur_byte];
807 else /* (type == SCAN_IN) */
809 bits_left -= 8 * (thisrun_bytes);
812 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
814 LOG_ERROR("couldn't write MPSSE commands to FT2232");
817 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
818 ft2232_buffer_size = 0;
820 if (type != SCAN_OUT)
822 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
824 LOG_ERROR("couldn't read from FT2232");
827 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
828 receive_pointer += bytes_read;
834 /* the most signifcant bit is scanned during TAP movement */
836 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
840 /* process remaining bits but the last one */
845 /* Clock Data Bits In and Out LSB First */
847 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
849 else if (type == SCAN_OUT)
851 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
853 /* LOG_DEBUG("added TDI bits (o)"); */
855 else if (type == SCAN_IN)
857 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
859 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
861 BUFFER_ADD = bits_left - 2;
863 BUFFER_ADD = buffer[cur_byte];
865 if (type != SCAN_OUT)
869 if (tap_get_end_state() == TAP_DRSHIFT)
873 /* Clock Data Bits In and Out LSB First */
875 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
877 else if (type == SCAN_OUT)
879 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
881 /* LOG_DEBUG("added TDI bits (o)"); */
883 else if (type == SCAN_IN)
885 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
887 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
890 BUFFER_ADD = last_bit;
894 /* move from Shift-IR/DR to end state */
895 if (type != SCAN_OUT)
897 /* Clock Data to TMS/CS Pin with Read */
899 /* LOG_DEBUG("added TMS scan (read)"); */
903 /* Clock Data to TMS/CS Pin (no Read) */
905 /* LOG_DEBUG("added TMS scan (no read)"); */
908 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
909 tap_set_state( tap_get_end_state() );
912 if (type != SCAN_OUT)
915 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
917 LOG_ERROR("couldn't write MPSSE commands to FT2232");
920 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
921 ft2232_buffer_size = 0;
923 if (type != SCAN_OUT)
925 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
927 LOG_ERROR("couldn't read from FT2232");
930 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
931 receive_pointer += bytes_read;
938 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
940 int predicted_size = 3;
941 int num_bytes = (scan_size - 1) / 8;
943 if (tap_get_state() != TAP_DRSHIFT)
946 if (type == SCAN_IN) /* only from device to host */
949 predicted_size += CEIL(num_bytes, 65536) * 3;
950 /* remaining bits - 1 (up to 7) */
951 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
953 else /* host to device, or bidirectional */
956 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
957 /* remaining bits -1 (up to 7) */
958 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
961 return predicted_size;
965 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
967 int predicted_size = 0;
969 if (type != SCAN_OUT)
972 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
974 /* remaining bits - 1 */
975 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
977 /* last bit (from TMS scan) */
981 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
983 return predicted_size;
987 static void usbjtag_reset(int trst, int srst)
991 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
992 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
994 low_output &= ~nTRST; /* switch output low */
998 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
999 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1001 low_output |= nTRST; /* switch output high */
1006 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1007 low_output &= ~nSRST; /* switch output low */
1009 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1013 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1014 low_output |= nSRST; /* switch output high */
1016 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1019 /* command "set data bits low byte" */
1021 BUFFER_ADD = low_output;
1022 BUFFER_ADD = low_direction;
1026 static void jtagkey_reset(int trst, int srst)
1030 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1031 high_output &= ~nTRSTnOE;
1033 high_output &= ~nTRST;
1037 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1038 high_output |= nTRSTnOE;
1040 high_output |= nTRST;
1045 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1046 high_output &= ~nSRST;
1048 high_output &= ~nSRSTnOE;
1052 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1053 high_output |= nSRST;
1055 high_output |= nSRSTnOE;
1058 /* command "set data bits high byte" */
1060 BUFFER_ADD = high_output;
1061 BUFFER_ADD = high_direction;
1062 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1067 static void olimex_jtag_reset(int trst, int srst)
1071 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1072 high_output &= ~nTRSTnOE;
1074 high_output &= ~nTRST;
1078 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1079 high_output |= nTRSTnOE;
1081 high_output |= nTRST;
1086 high_output |= nSRST;
1090 high_output &= ~nSRST;
1093 /* command "set data bits high byte" */
1095 BUFFER_ADD = high_output;
1096 BUFFER_ADD = high_direction;
1097 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1102 static void axm0432_jtag_reset(int trst, int srst)
1106 tap_set_state(TAP_RESET);
1107 high_output &= ~nTRST;
1111 high_output |= nTRST;
1116 high_output &= ~nSRST;
1120 high_output |= nSRST;
1123 /* command "set data bits low byte" */
1125 BUFFER_ADD = high_output;
1126 BUFFER_ADD = high_direction;
1127 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1132 static void flyswatter_reset(int trst, int srst)
1136 low_output &= ~nTRST;
1140 low_output |= nTRST;
1145 low_output |= nSRST;
1149 low_output &= ~nSRST;
1152 /* command "set data bits low byte" */
1154 BUFFER_ADD = low_output;
1155 BUFFER_ADD = low_direction;
1156 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1160 static void turtle_reset(int trst, int srst)
1166 low_output |= nSRST;
1170 low_output &= ~nSRST;
1173 /* command "set data bits low byte" */
1175 BUFFER_ADD = low_output;
1176 BUFFER_ADD = low_direction;
1177 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1181 static void comstick_reset(int trst, int srst)
1185 high_output &= ~nTRST;
1189 high_output |= nTRST;
1194 high_output &= ~nSRST;
1198 high_output |= nSRST;
1201 /* command "set data bits high byte" */
1203 BUFFER_ADD = high_output;
1204 BUFFER_ADD = high_direction;
1205 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1210 static void stm32stick_reset(int trst, int srst)
1214 high_output &= ~nTRST;
1218 high_output |= nTRST;
1223 low_output &= ~nSRST;
1227 low_output |= nSRST;
1230 /* command "set data bits low byte" */
1232 BUFFER_ADD = low_output;
1233 BUFFER_ADD = low_direction;
1235 /* command "set data bits high byte" */
1237 BUFFER_ADD = high_output;
1238 BUFFER_ADD = high_direction;
1239 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1245 static void sheevaplug_reset(int trst, int srst)
1248 high_output &= ~nTRST;
1250 high_output |= nTRST;
1253 high_output &= ~nSRSTnOE;
1255 high_output |= nSRSTnOE;
1257 /* command "set data bits high byte" */
1259 BUFFER_ADD = high_output;
1260 BUFFER_ADD = high_direction;
1261 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1264 static int ft2232_execute_end_state(jtag_command_t *cmd)
1269 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
1271 if (cmd->cmd.end_state->end_state != TAP_INVALID)
1272 ft2232_end_state(cmd->cmd.end_state->end_state);
1278 static int ft2232_execute_runtest(jtag_command_t *cmd)
1282 int predicted_size = 0;
1285 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
1286 cmd->cmd.runtest->num_cycles,
1287 cmd->cmd.runtest->end_state);
1288 /* only send the maximum buffer size that FT2232C can handle */
1290 if (tap_get_state() != TAP_IDLE)
1291 predicted_size += 3;
1292 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1293 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1294 predicted_size += 3;
1295 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1296 predicted_size += 3;
1297 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1299 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1300 retval = ERROR_JTAG_QUEUE_FAILED;
1304 if (tap_get_state() != TAP_IDLE)
1306 /* command "Clock Data to TMS/CS Pin (no Read)" */
1308 BUFFER_ADD = 0x6; /* scan 7 bits */
1311 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IDLE);
1312 tap_set_state(TAP_IDLE);
1315 i = cmd->cmd.runtest->num_cycles;
1318 /* command "Clock Data to TMS/CS Pin (no Read)" */
1322 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1326 tap_set_state(TAP_IDLE);
1327 i -= (i > 7) ? 7 : i;
1328 /* LOG_DEBUG("added TMS scan (no read)"); */
1331 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1332 ft2232_end_state(cmd->cmd.runtest->end_state);
1334 if ( tap_get_state() != tap_get_end_state() )
1336 /* command "Clock Data to TMS/CS Pin (no Read)" */
1341 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1342 tap_set_state( tap_get_end_state() );
1343 /* LOG_DEBUG("added TMS scan (no read)"); */
1346 #ifdef _DEBUG_JTAG_IO_
1347 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1353 static int ft2232_execute_statemove(jtag_command_t *cmd)
1356 int predicted_size = 0;
1359 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1361 /* only send the maximum buffer size that FT2232C can handle */
1363 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1365 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1366 retval = ERROR_JTAG_QUEUE_FAILED;
1370 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1371 ft2232_end_state(cmd->cmd.statemove->end_state);
1373 /* command "Clock Data to TMS/CS Pin (no Read)" */
1376 BUFFER_ADD = 0x6; /* scan 7 bits */
1379 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1380 /* LOG_DEBUG("added TMS scan (no read)"); */
1381 tap_set_state( tap_get_end_state() );
1383 #ifdef _DEBUG_JTAG_IO_
1384 LOG_DEBUG( "statemove: %s", tap_state_name( tap_get_end_state() ) );
1390 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1393 int predicted_size = 0;
1396 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
1397 cmd->cmd.pathmove->num_states,
1398 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1399 /* only send the maximum buffer size that FT2232C can handle */
1400 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1401 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1403 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1404 retval = ERROR_JTAG_QUEUE_FAILED;
1408 ft2232_add_pathmove(cmd->cmd.pathmove);
1410 #ifdef _DEBUG_JTAG_IO_
1411 LOG_DEBUG( "pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1412 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]) );
1417 static int ft2232_execute_scan(jtag_command_t *cmd)
1421 int scan_size; /* size of IR or DR scan */
1422 enum scan_type type;
1423 int predicted_size = 0;
1426 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1427 type = jtag_scan_type(cmd->cmd.scan);
1428 predicted_size = ft2232_predict_scan_out(scan_size, type);
1429 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1431 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1432 /* unsent commands before this */
1433 if (first_unsent != cmd)
1434 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1435 retval = ERROR_JTAG_QUEUE_FAILED;
1437 /* current command */
1438 if (cmd->cmd.scan->end_state != TAP_INVALID)
1439 ft2232_end_state(cmd->cmd.scan->end_state);
1440 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1442 first_unsent = cmd->next;
1447 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1449 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1452 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1453 retval = ERROR_JTAG_QUEUE_FAILED;
1457 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1458 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1459 if (cmd->cmd.scan->end_state != TAP_INVALID)
1460 ft2232_end_state(cmd->cmd.scan->end_state);
1461 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1465 #ifdef _DEBUG_JTAG_IO_
1466 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1467 tap_state_name( tap_get_end_state() ) );
1473 static int ft2232_execute_reset(jtag_command_t *cmd)
1476 int predicted_size = 0;
1479 DEBUG_JTAG_IO("reset trst: %i srst %i",
1480 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1482 /* only send the maximum buffer size that FT2232C can handle */
1484 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1486 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1487 retval = ERROR_JTAG_QUEUE_FAILED;
1492 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1494 tap_set_state(TAP_RESET);
1496 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1499 #ifdef _DEBUG_JTAG_IO_
1500 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1505 static int ft2232_execute_sleep(jtag_command_t *cmd)
1510 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1512 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1513 retval = ERROR_JTAG_QUEUE_FAILED;
1514 first_unsent = cmd->next;
1515 jtag_sleep(cmd->cmd.sleep->us);
1516 #ifdef _DEBUG_JTAG_IO_
1517 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1523 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1528 /* this is only allowed while in a stable state. A check for a stable
1529 * state was done in jtag_add_clocks()
1531 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1532 retval = ERROR_JTAG_QUEUE_FAILED;
1533 #ifdef _DEBUG_JTAG_IO_
1534 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1540 static int ft2232_execute_command(jtag_command_t *cmd)
1547 case JTAG_END_STATE: retval = ft2232_execute_end_state(cmd); break;
1548 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1549 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1550 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1551 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1552 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1553 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1554 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1556 LOG_ERROR("BUG: unknown JTAG command type encountered");
1562 static int ft2232_execute_queue()
1564 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1567 first_unsent = cmd; /* next command that has to be sent */
1570 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1571 * that wasn't handled by a caller-provided error handler
1575 ft2232_buffer_size = 0;
1576 ft2232_expect_read = 0;
1578 /* blink, if the current layout has that feature */
1584 if (ft2232_execute_command(cmd) != ERROR_OK)
1585 retval = ERROR_JTAG_QUEUE_FAILED;
1586 /* Start reading input before FT2232 TX buffer fills up */
1588 if (ft2232_expect_read > 256)
1590 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1591 retval = ERROR_JTAG_QUEUE_FAILED;
1596 if (require_send > 0)
1597 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1598 retval = ERROR_JTAG_QUEUE_FAILED;
1604 #if BUILD_FT2232_FTD2XX == 1
1605 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1608 DWORD openex_flags = 0;
1609 char* openex_string = NULL;
1612 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1615 /* Add non-standard Vid/Pid to the linux driver */
1616 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1618 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1622 if (ft2232_device_desc && ft2232_serial)
1624 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1625 ft2232_device_desc = NULL;
1628 if (ft2232_device_desc)
1630 openex_string = ft2232_device_desc;
1631 openex_flags = FT_OPEN_BY_DESCRIPTION;
1633 else if (ft2232_serial)
1635 openex_string = ft2232_serial;
1636 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1640 LOG_ERROR("neither device description nor serial number specified");
1641 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1643 return ERROR_JTAG_INIT_FAILED;
1646 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1647 if( status != FT_OK ){
1648 // under Win32, the FTD2XX driver appends an "A" to the end
1649 // of the description, if we tried by the desc, then
1650 // try by the alternate "A" description.
1651 if( openex_string == ft2232_device_desc ){
1652 // Try the alternate method.
1653 openex_string = ft2232_device_desc_A;
1654 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1655 if( status == FT_OK ){
1656 // yea, the "alternate" method worked!
1658 // drat, give the user a meaningfull message.
1659 // telling the use we tried *BOTH* methods.
1660 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1662 ft2232_device_desc_A );
1667 if ( status != FT_OK )
1673 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1675 return ERROR_JTAG_INIT_FAILED;
1677 LOG_ERROR("unable to open ftdi device: %lu", status);
1678 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1679 if (status == FT_OK)
1681 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1684 for (i = 0; i < num_devices; i++)
1685 desc_array[i] = malloc(64);
1687 desc_array[num_devices] = NULL;
1689 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1691 if (status == FT_OK)
1693 LOG_ERROR("ListDevices: %lu\n", num_devices);
1694 for (i = 0; i < num_devices; i++)
1695 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1698 for (i = 0; i < num_devices; i++)
1699 free(desc_array[i]);
1705 LOG_ERROR("ListDevices: NONE\n");
1707 return ERROR_JTAG_INIT_FAILED;
1710 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1712 LOG_ERROR("unable to set latency timer: %lu", status);
1713 return ERROR_JTAG_INIT_FAILED;
1716 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1718 LOG_ERROR("unable to get latency timer: %lu", status);
1719 return ERROR_JTAG_INIT_FAILED;
1723 LOG_DEBUG("current latency timer: %i", latency_timer);
1726 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1728 LOG_ERROR("unable to set timeouts: %lu", status);
1729 return ERROR_JTAG_INIT_FAILED;
1732 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1734 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1735 return ERROR_JTAG_INIT_FAILED;
1742 static int ft2232_purge_ftd2xx(void)
1746 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1748 LOG_ERROR("error purging ftd2xx device: %lu", status);
1749 return ERROR_JTAG_INIT_FAILED;
1756 #endif /* BUILD_FT2232_FTD2XX == 1 */
1758 #if BUILD_FT2232_LIBFTDI == 1
1759 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1763 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1764 ft2232_layout, vid, pid);
1766 if (ftdi_init(&ftdic) < 0)
1767 return ERROR_JTAG_INIT_FAILED;
1769 /* context, vendor id, product id */
1770 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1774 LOG_WARNING("unable to open ftdi device (trying more): %s",
1777 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1779 return ERROR_JTAG_INIT_FAILED;
1782 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1784 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1785 return ERROR_JTAG_INIT_FAILED;
1788 if (ftdi_usb_reset(&ftdic) < 0)
1790 LOG_ERROR("unable to reset ftdi device");
1791 return ERROR_JTAG_INIT_FAILED;
1794 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1796 LOG_ERROR("unable to set latency timer");
1797 return ERROR_JTAG_INIT_FAILED;
1800 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1802 LOG_ERROR("unable to get latency timer");
1803 return ERROR_JTAG_INIT_FAILED;
1807 LOG_DEBUG("current latency timer: %i", latency_timer);
1810 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1816 static int ft2232_purge_libftdi(void)
1818 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1820 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1821 return ERROR_JTAG_INIT_FAILED;
1828 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1830 static int ft2232_init(void)
1835 ft2232_layout_t* cur_layout = ft2232_layouts;
1838 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1840 ft2232_layout = "usbjtag";
1841 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1844 while (cur_layout->name)
1846 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1848 layout = cur_layout;
1856 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1857 return ERROR_JTAG_INIT_FAILED;
1863 * "more indicates that there are more IDs to try, so we should
1864 * not print an error for an ID mismatch (but for anything
1867 * try_more indicates that the error code returned indicates an
1868 * ID mismatch (and nothing else) and that we should proceeed
1869 * with the next ID pair.
1871 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
1874 #if BUILD_FT2232_FTD2XX == 1
1875 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1877 #elif BUILD_FT2232_LIBFTDI == 1
1878 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1883 if (!more || !try_more)
1887 ft2232_buffer_size = 0;
1888 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1890 if (layout->init() != ERROR_OK)
1891 return ERROR_JTAG_INIT_FAILED;
1893 ft2232_speed(jtag_speed);
1895 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1896 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
1898 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1899 return ERROR_JTAG_INIT_FAILED;
1902 #if BUILD_FT2232_FTD2XX == 1
1903 return ft2232_purge_ftd2xx();
1904 #elif BUILD_FT2232_LIBFTDI == 1
1905 return ft2232_purge_libftdi();
1912 static int usbjtag_init(void)
1918 low_direction = 0x0b;
1920 if (strcmp(ft2232_layout, "usbjtag") == 0)
1927 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1934 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1941 low_direction = 0x8b;
1945 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1946 return ERROR_JTAG_INIT_FAILED;
1949 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1951 low_direction &= ~nTRSTnOE; /* nTRST input */
1952 low_output &= ~nTRST; /* nTRST = 0 */
1956 low_direction |= nTRSTnOE; /* nTRST output */
1957 low_output |= nTRST; /* nTRST = 1 */
1960 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1962 low_direction |= nSRSTnOE; /* nSRST output */
1963 low_output |= nSRST; /* nSRST = 1 */
1967 low_direction &= ~nSRSTnOE; /* nSRST input */
1968 low_output &= ~nSRST; /* nSRST = 0 */
1971 /* initialize low byte for jtag */
1972 buf[0] = 0x80; /* command "set data bits low byte" */
1973 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1974 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1975 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1977 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1979 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1980 return ERROR_JTAG_INIT_FAILED;
1987 static int axm0432_jtag_init(void)
1993 low_direction = 0x2b;
1995 /* initialize low byte for jtag */
1996 buf[0] = 0x80; /* command "set data bits low byte" */
1997 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1998 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1999 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2001 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2003 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2004 return ERROR_JTAG_INIT_FAILED;
2007 if (strcmp(layout->name, "axm0432_jtag") == 0)
2010 nTRSTnOE = 0x0; /* No output enable for TRST*/
2012 nSRSTnOE = 0x0; /* No output enable for SRST*/
2016 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2021 high_direction = 0x0c;
2023 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2025 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2029 high_output |= nTRST;
2032 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2034 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2038 high_output |= nSRST;
2041 /* initialize high port */
2042 buf[0] = 0x82; /* command "set data bits high byte" */
2043 buf[1] = high_output; /* value */
2044 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2045 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2047 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2049 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2050 return ERROR_JTAG_INIT_FAILED;
2057 static int jtagkey_init(void)
2063 low_direction = 0x1b;
2065 /* initialize low byte for jtag */
2066 buf[0] = 0x80; /* command "set data bits low byte" */
2067 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2068 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2069 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2071 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2073 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2074 return ERROR_JTAG_INIT_FAILED;
2077 if (strcmp(layout->name, "jtagkey") == 0)
2084 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2085 || (strcmp(layout->name, "oocdlink") == 0) )
2094 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2099 high_direction = 0x0f;
2101 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2103 high_output |= nTRSTnOE;
2104 high_output &= ~nTRST;
2108 high_output &= ~nTRSTnOE;
2109 high_output |= nTRST;
2112 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2114 high_output &= ~nSRSTnOE;
2115 high_output |= nSRST;
2119 high_output |= nSRSTnOE;
2120 high_output &= ~nSRST;
2123 /* initialize high port */
2124 buf[0] = 0x82; /* command "set data bits high byte" */
2125 buf[1] = high_output; /* value */
2126 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2127 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2129 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2131 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2132 return ERROR_JTAG_INIT_FAILED;
2139 static int olimex_jtag_init(void)
2145 low_direction = 0x1b;
2147 /* initialize low byte for jtag */
2148 buf[0] = 0x80; /* command "set data bits low byte" */
2149 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2150 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2151 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2153 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2155 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2156 return ERROR_JTAG_INIT_FAILED;
2162 nSRSTnOE = 0x00; /* no output enable for nSRST */
2165 high_direction = 0x0f;
2167 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2169 high_output |= nTRSTnOE;
2170 high_output &= ~nTRST;
2174 high_output &= ~nTRSTnOE;
2175 high_output |= nTRST;
2178 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2180 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2184 high_output &= ~nSRST;
2187 /* turn red LED on */
2188 high_output |= 0x08;
2190 /* initialize high port */
2191 buf[0] = 0x82; /* command "set data bits high byte" */
2192 buf[1] = high_output; /* value */
2193 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2194 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2196 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2198 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2199 return ERROR_JTAG_INIT_FAILED;
2206 static int flyswatter_init(void)
2212 low_direction = 0xfb;
2214 /* initialize low byte for jtag */
2215 buf[0] = 0x80; /* command "set data bits low byte" */
2216 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2217 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2218 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2220 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2222 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2223 return ERROR_JTAG_INIT_FAILED;
2227 nTRSTnOE = 0x0; /* not output enable for nTRST */
2229 nSRSTnOE = 0x00; /* no output enable for nSRST */
2232 high_direction = 0x0c;
2234 /* turn red LED3 on, LED2 off */
2235 high_output |= 0x08;
2237 /* initialize high port */
2238 buf[0] = 0x82; /* command "set data bits high byte" */
2239 buf[1] = high_output; /* value */
2240 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2241 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2243 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2245 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2246 return ERROR_JTAG_INIT_FAILED;
2253 static int turtle_init(void)
2259 low_direction = 0x5b;
2261 /* initialize low byte for jtag */
2262 buf[0] = 0x80; /* command "set data bits low byte" */
2263 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2264 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2265 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2267 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2269 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2270 return ERROR_JTAG_INIT_FAILED;
2276 high_direction = 0x0C;
2278 /* initialize high port */
2279 buf[0] = 0x82; /* command "set data bits high byte" */
2280 buf[1] = high_output;
2281 buf[2] = high_direction;
2282 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2284 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2286 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2287 return ERROR_JTAG_INIT_FAILED;
2294 static int comstick_init(void)
2300 low_direction = 0x0b;
2302 /* initialize low byte for jtag */
2303 buf[0] = 0x80; /* command "set data bits low byte" */
2304 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2305 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2306 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2308 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2310 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2311 return ERROR_JTAG_INIT_FAILED;
2315 nTRSTnOE = 0x00; /* no output enable for nTRST */
2317 nSRSTnOE = 0x00; /* no output enable for nSRST */
2320 high_direction = 0x03;
2322 /* initialize high port */
2323 buf[0] = 0x82; /* command "set data bits high byte" */
2324 buf[1] = high_output;
2325 buf[2] = high_direction;
2326 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2328 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2330 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2331 return ERROR_JTAG_INIT_FAILED;
2338 static int stm32stick_init(void)
2344 low_direction = 0x8b;
2346 /* initialize low byte for jtag */
2347 buf[0] = 0x80; /* command "set data bits low byte" */
2348 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2349 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2350 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2352 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2354 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2355 return ERROR_JTAG_INIT_FAILED;
2359 nTRSTnOE = 0x00; /* no output enable for nTRST */
2361 nSRSTnOE = 0x00; /* no output enable for nSRST */
2364 high_direction = 0x03;
2366 /* initialize high port */
2367 buf[0] = 0x82; /* command "set data bits high byte" */
2368 buf[1] = high_output;
2369 buf[2] = high_direction;
2370 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2372 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2374 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2375 return ERROR_JTAG_INIT_FAILED;
2382 static int sheevaplug_init(void)
2388 low_direction = 0x1b;
2390 /* initialize low byte for jtag */
2391 buf[0] = 0x80; /* command "set data bits low byte" */
2392 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2393 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2394 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2396 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2398 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2399 return ERROR_JTAG_INIT_FAILED;
2408 high_direction = 0x0f;
2410 /* nTRST is always push-pull */
2411 high_output &= ~nTRSTnOE;
2412 high_output |= nTRST;
2414 /* nSRST is always open-drain */
2415 high_output |= nSRSTnOE;
2416 high_output &= ~nSRST;
2418 /* initialize high port */
2419 buf[0] = 0x82; /* command "set data bits high byte" */
2420 buf[1] = high_output; /* value */
2421 buf[2] = high_direction; /* all outputs - xRST */
2422 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2424 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2426 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2427 return ERROR_JTAG_INIT_FAILED;
2433 static void olimex_jtag_blink(void)
2435 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2436 * ACBUS3 is bit 3 of the GPIOH port
2438 if (high_output & 0x08)
2440 /* set port pin high */
2441 high_output &= 0x07;
2445 /* set port pin low */
2446 high_output |= 0x08;
2450 BUFFER_ADD = high_output;
2451 BUFFER_ADD = high_direction;
2455 static void flyswatter_jtag_blink(void)
2458 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2460 high_output ^= 0x0c;
2463 BUFFER_ADD = high_output;
2464 BUFFER_ADD = high_direction;
2468 static void turtle_jtag_blink(void)
2471 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2473 if (high_output & 0x08)
2483 BUFFER_ADD = high_output;
2484 BUFFER_ADD = high_direction;
2488 static int ft2232_quit(void)
2490 #if BUILD_FT2232_FTD2XX == 1
2493 status = FT_Close(ftdih);
2494 #elif BUILD_FT2232_LIBFTDI == 1
2495 ftdi_disable_bitbang(&ftdic);
2497 ftdi_usb_close(&ftdic);
2499 ftdi_deinit(&ftdic);
2502 free(ft2232_buffer);
2503 ft2232_buffer = NULL;
2509 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2515 ft2232_device_desc = strdup(args[0]);
2516 cp = strchr( ft2232_device_desc, 0 );
2517 // under Win32, the FTD2XX driver appends an "A" to the end
2518 // of the description, this examines the given desc
2519 // and creates the 'missing' _A or non_A variable.
2520 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2521 // it was, so make this the "A" version.
2522 ft2232_device_desc_A = ft2232_device_desc;
2523 // and *CREATE* the non-A version.
2524 strcpy( buf, ft2232_device_desc );
2525 cp = strchr( buf, 0 );
2527 ft2232_device_desc = strdup( buf );
2529 // <space>A not defined
2531 sprintf( buf, "%s A", ft2232_device_desc );
2532 ft2232_device_desc_A = strdup( buf );
2537 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2544 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2548 ft2232_serial = strdup(args[0]);
2552 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2559 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2564 ft2232_layout = malloc(strlen(args[0]) + 1);
2565 strcpy(ft2232_layout, args[0]);
2571 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2575 if (argc > MAX_USB_IDS * 2)
2577 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2578 "(maximum is %d pairs)", MAX_USB_IDS);
2579 argc = MAX_USB_IDS * 2;
2581 if ( argc < 2 || (argc & 1) )
2583 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2588 for (i = 0; i + 1 < argc; i += 2)
2590 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2591 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2595 * Explicitly terminate, in case there are multiples instances of
2598 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2604 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2608 ft2232_latency = atoi(args[0]);
2612 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2619 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2623 /* 7 bits of either ones or zeros. */
2624 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2626 while (num_cycles > 0)
2628 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2629 * at most 7 bits per invocation. Here we invoke it potentially
2632 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2634 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2636 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2637 retval = ERROR_JTAG_QUEUE_FAILED;
2642 /* command "Clock Data to TMS/CS Pin (no Read)" */
2646 BUFFER_ADD = bitcount_per_command - 1;
2648 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2653 num_cycles -= bitcount_per_command;
2659 /* ---------------------------------------------------------------------
2660 * Support for IceBear JTAG adapter from Section5:
2661 * http://section5.ch/icebear
2663 * Author: Sten, debian@sansys-electronic.com
2666 /* Icebear pin layout
2668 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2669 * GND GND | 4 3| n.c.
2670 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2671 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2672 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2673 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2674 * ADBUS2 TDO |14 13| GND GND
2676 * ADBUS0 O L TCK ACBUS0 GND
2677 * ADBUS1 O L TDI ACBUS1 GND
2678 * ADBUS2 I TDO ACBUS2 n.c.
2679 * ADBUS3 O H TMS ACBUS3 n.c.
2685 static int icebear_jtag_init(void) {
2689 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2690 low_output = 0x08; /* high: TMS; low: TCK TDI */
2694 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2695 low_direction &= ~nTRST; /* nTRST high impedance */
2698 low_direction |= nTRST;
2699 low_output |= nTRST;
2702 low_direction |= nSRST;
2703 low_output |= nSRST;
2705 /* initialize low byte for jtag */
2706 buf[0] = 0x80; /* command "set data bits low byte" */
2707 buf[1] = low_output;
2708 buf[2] = low_direction;
2709 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2711 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2712 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2713 return ERROR_JTAG_INIT_FAILED;
2717 high_direction = 0x00;
2720 /* initialize high port */
2721 buf[0] = 0x82; /* command "set data bits high byte" */
2722 buf[1] = high_output; /* value */
2723 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2724 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2726 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2727 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2728 return ERROR_JTAG_INIT_FAILED;
2734 static void icebear_jtag_reset(int trst, int srst) {
2737 low_direction |= nTRST;
2738 low_output &= ~nTRST;
2740 else if (trst == 0) {
2741 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
2742 low_direction &= ~nTRST;
2744 low_output |= nTRST;
2748 low_output &= ~nSRST;
2750 else if (srst == 0) {
2751 low_output |= nSRST;
2754 /* command "set data bits low byte" */
2756 BUFFER_ADD = low_output;
2757 BUFFER_ADD = low_direction;
2758 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);