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 #include "replacements.h"
42 /* project specific includes */
46 #include "configuration.h"
47 #include "time_support.h"
54 /* FT2232 access library includes */
55 #if BUILD_FT2232_FTD2XX == 1
57 #elif BUILD_FT2232_LIBFTDI == 1
61 /* enable this to debug io latency
64 #define _DEBUG_USB_IO_
67 /* enable this to debug communication
70 #define _DEBUG_USB_COMMS_
73 int ft2232_execute_queue(void);
75 int ft2232_speed(int speed);
76 int ft2232_speed_div(int speed, int* khz);
77 int ft2232_khz(int khz, int* jtag_speed);
78 int ft2232_register_commands(struct command_context_s* cmd_ctx);
79 int ft2232_init(void);
80 int ft2232_quit(void);
82 int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
83 int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
84 int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
85 int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
86 int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
90 * Function ft2232_stableclocks
91 * will send out \a num_cycles on the TCK line while the TAP(s)
92 * are in a stable state. Calling code must ensure that current state is
93 * stable, that verification is not done in here.
94 * @param num_cycles is the count of clocks cycles to send.
95 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
97 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
100 char * ft2232_device_desc_A = NULL;
101 char* ft2232_device_desc = NULL;
102 char* ft2232_serial = NULL;
103 char* ft2232_layout = NULL;
104 unsigned char ft2232_latency = 2;
106 #define MAX_USB_IDS 8
107 /* vid = pid = 0 marks the end of the list */
108 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
109 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
111 typedef struct ft2232_layout_s
115 void (*reset)(int trst, int srst);
119 /* init procedures for supported layouts */
120 int usbjtag_init(void);
121 int jtagkey_init(void);
122 int olimex_jtag_init(void);
123 int flyswatter_init(void);
124 int turtle_init(void);
125 int comstick_init(void);
126 int stm32stick_init(void);
127 int axm0432_jtag_init(void);
128 int sheevaplug_init(void);
130 /* reset procedures for supported layouts */
131 void usbjtag_reset(int trst, int srst);
132 void jtagkey_reset(int trst, int srst);
133 void olimex_jtag_reset(int trst, int srst);
134 void flyswatter_reset(int trst, int srst);
135 void turtle_reset(int trst, int srst);
136 void comstick_reset(int trst, int srst);
137 void stm32stick_reset(int trst, int srst);
138 void axm0432_jtag_reset(int trst, int srst);
139 void sheevaplug_reset(int trst, int srst);
141 /* blink procedures for layouts that support a blinking led */
142 void olimex_jtag_blink(void);
143 void flyswatter_jtag_blink(void);
144 void turtle_jtag_blink(void);
146 ft2232_layout_t ft2232_layouts[] =
148 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
149 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
150 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
151 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
152 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
153 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
154 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
155 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
156 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
157 { "comstick", comstick_init, comstick_reset, NULL },
158 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
159 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
160 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
161 { NULL, NULL, NULL, NULL },
164 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
166 static ft2232_layout_t* layout;
167 static u8 low_output = 0x0;
168 static u8 low_direction = 0x0;
169 static u8 high_output = 0x0;
170 static u8 high_direction = 0x0;
172 #if BUILD_FT2232_FTD2XX == 1
173 static FT_HANDLE ftdih = NULL;
174 #elif BUILD_FT2232_LIBFTDI == 1
175 static struct ftdi_context ftdic;
179 static jtag_command_t* first_unsent; /* next command that has to be sent */
180 static int require_send;
182 static u8* ft2232_buffer = NULL;
183 static int ft2232_buffer_size = 0;
184 static int ft2232_read_pointer = 0;
185 static int ft2232_expect_read = 0;
187 #define FT2232_BUFFER_SIZE 131072
188 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
189 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
191 jtag_interface_t ft2232_interface =
194 .execute_queue = ft2232_execute_queue,
195 .speed = ft2232_speed,
196 .speed_div = ft2232_speed_div,
198 .register_commands = ft2232_register_commands,
203 int ft2232_write(u8* buf, int size, u32* bytes_written)
205 #if BUILD_FT2232_FTD2XX == 1
207 DWORD dw_bytes_written;
208 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
210 *bytes_written = dw_bytes_written;
211 LOG_ERROR("FT_Write returned: %lu", status);
212 return ERROR_JTAG_DEVICE_ERROR;
216 *bytes_written = dw_bytes_written;
219 #elif BUILD_FT2232_LIBFTDI == 1
221 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
224 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
225 return ERROR_JTAG_DEVICE_ERROR;
229 *bytes_written = retval;
236 int ft2232_read(u8* buf, u32 size, u32* bytes_read)
238 #if BUILD_FT2232_FTD2XX == 1
244 while ( (*bytes_read < size) && timeout-- )
246 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
247 *bytes_read, &dw_bytes_read) ) != FT_OK )
250 LOG_ERROR("FT_Read returned: %lu", status);
251 return ERROR_JTAG_DEVICE_ERROR;
253 *bytes_read += dw_bytes_read;
256 #elif BUILD_FT2232_LIBFTDI == 1
261 while ( (*bytes_read < size) && timeout-- )
263 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
266 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
267 return ERROR_JTAG_DEVICE_ERROR;
269 *bytes_read += retval;
274 if (*bytes_read < size)
276 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
277 return ERROR_JTAG_DEVICE_ERROR;
284 int ft2232_speed(int speed)
290 buf[0] = 0x86; /* command "set divisor" */
291 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
292 buf[2] = (speed >> 8) & 0xff; /* valueH */
294 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
295 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
297 LOG_ERROR("couldn't set FT2232 TCK speed");
305 int ft2232_speed_div(int speed, int* khz)
307 /* Take a look in the FT2232 manual,
308 * AN2232C-01 Command Processor for
309 * MPSSE and MCU Host Bus. Chapter 3.8 */
311 *khz = 6000 / (1 + speed);
317 int ft2232_khz(int khz, int* jtag_speed)
321 LOG_ERROR("RCLK not supported");
325 /* Take a look in the FT2232 manual,
326 * AN2232C-01 Command Processor for
327 * MPSSE and MCU Host Bus. Chapter 3.8
329 * We will calc here with a multiplier
330 * of 10 for better rounding later. */
332 /* Calc speed, (6000 / khz) - 1 */
333 /* Use 65000 for better rounding */
334 *jtag_speed = (60000 / khz) - 10;
336 /* Add 0.9 for rounding */
339 /* Calc real speed */
340 *jtag_speed = *jtag_speed / 10;
342 /* Check if speed is greater than 0 */
348 /* Check max value */
349 if (*jtag_speed > 0xFFFF)
351 *jtag_speed = 0xFFFF;
358 int ft2232_register_commands(struct command_context_s* cmd_ctx)
360 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
361 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
362 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
363 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
364 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
365 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
366 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
367 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
368 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
369 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
374 void ft2232_end_state(tap_state_t state)
376 if (tap_is_state_stable(state))
377 tap_set_end_state(state);
380 LOG_ERROR("BUG: %i is not a valid end state", state);
386 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
388 int num_bytes = (scan_size + 7) / 8;
389 int bits_left = scan_size;
392 while (num_bytes-- > 1)
394 buffer[cur_byte] = BUFFER_READ;
399 buffer[cur_byte] = 0x0;
403 buffer[cur_byte] = BUFFER_READ >> 1;
406 buffer[cur_byte] = ( buffer[cur_byte] | ( (BUFFER_READ & 0x02) << 6 ) ) >> (8 - bits_left);
410 void ft2232_debug_dump_buffer(void)
416 for (i = 0; i < ft2232_buffer_size; i++)
418 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
421 LOG_DEBUG("%s", line);
427 LOG_DEBUG("%s", line);
431 int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
441 #ifdef _DEBUG_USB_IO_
442 struct timeval start, inter, inter2, end;
443 struct timeval d_inter, d_inter2, d_end;
446 #ifdef _DEBUG_USB_COMMS_
447 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
448 ft2232_debug_dump_buffer();
451 #ifdef _DEBUG_USB_IO_
452 gettimeofday(&start, NULL);
455 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
457 LOG_ERROR("couldn't write MPSSE commands to FT2232");
461 #ifdef _DEBUG_USB_IO_
462 gettimeofday(&inter, NULL);
465 if (ft2232_expect_read)
468 ft2232_buffer_size = 0;
470 #ifdef _DEBUG_USB_IO_
471 gettimeofday(&inter2, NULL);
474 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
476 LOG_ERROR("couldn't read from FT2232");
480 #ifdef _DEBUG_USB_IO_
481 gettimeofday(&end, NULL);
483 timeval_subtract(&d_inter, &inter, &start);
484 timeval_subtract(&d_inter2, &inter2, &start);
485 timeval_subtract(&d_end, &end, &start);
487 LOG_INFO("inter: %i.%06i, inter2: %i.%06i end: %i.%06i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec,
488 d_inter2.tv_usec, d_end.tv_sec,
492 ft2232_buffer_size = bytes_read;
494 if (ft2232_expect_read != ft2232_buffer_size)
496 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
499 ft2232_debug_dump_buffer();
504 #ifdef _DEBUG_USB_COMMS_
505 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
506 ft2232_debug_dump_buffer();
510 ft2232_expect_read = 0;
511 ft2232_read_pointer = 0;
513 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
514 * that wasn't handled by a caller-provided error handler
524 type = jtag_scan_type(cmd->cmd.scan);
525 if (type != SCAN_OUT)
527 scan_size = jtag_scan_size(cmd->cmd.scan);
528 buffer = calloc(CEIL(scan_size, 8), 1);
529 ft2232_read_scan(type, buffer, scan_size);
530 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
531 retval = ERROR_JTAG_QUEUE_FAILED;
543 ft2232_buffer_size = 0;
549 void ft2232_add_pathmove(pathmove_command_t* cmd)
551 int num_states = cmd->num_states;
556 u8 tms_byte = 0; /* zero this on each MPSSE batch */
560 int num_states_batch = num_states > 7 ? 7 : num_states;
562 /* command "Clock Data to TMS/CS Pin (no Read)" */
565 /* number of states remaining */
566 BUFFER_ADD = num_states_batch - 1;
568 while (num_states_batch--)
570 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
571 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
572 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
573 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
576 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
577 tap_get_state() ), tap_state_name(cmd->path[state_count]) );
581 tap_set_state(cmd->path[state_count]);
586 BUFFER_ADD = tms_byte;
589 tap_set_end_state(tap_get_state());
593 void ft2232_add_scan(int ir_scan, enum scan_type type, u8* buffer, int scan_size)
595 int num_bytes = (scan_size + 7) / 8;
596 int bits_left = scan_size;
600 if ( !( ( !ir_scan && (tap_get_state() == TAP_DRSHIFT) )
601 || ( ir_scan && (tap_get_state() == TAP_IRSHIFT) ) ) )
603 /* command "Clock Data to TMS/CS Pin (no Read)" */
606 BUFFER_ADD = 0x6; /* scan 7 bits */
611 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IRSHIFT);
612 tap_set_state(TAP_IRSHIFT);
616 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
617 tap_set_state(TAP_DRSHIFT);
619 /* LOG_DEBUG("added TMS scan (no read)"); */
622 /* add command for complete bytes */
623 while (num_bytes > 1)
628 /* Clock Data Bytes In and Out LSB First */
630 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
632 else if (type == SCAN_OUT)
634 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
636 /* LOG_DEBUG("added TDI bytes (o)"); */
638 else if (type == SCAN_IN)
640 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
642 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
645 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
646 num_bytes -= thisrun_bytes;
647 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
648 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
652 /* add complete bytes */
653 while (thisrun_bytes-- > 0)
655 BUFFER_ADD = buffer[cur_byte];
660 else /* (type == SCAN_IN) */
662 bits_left -= 8 * (thisrun_bytes);
666 /* the most signifcant bit is scanned during TAP movement */
668 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
672 /* process remaining bits but the last one */
677 /* Clock Data Bits In and Out LSB First */
679 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
681 else if (type == SCAN_OUT)
683 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
685 /* LOG_DEBUG("added TDI bits (o)"); */
687 else if (type == SCAN_IN)
689 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
691 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
693 BUFFER_ADD = bits_left - 2;
695 BUFFER_ADD = buffer[cur_byte];
698 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
699 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
703 /* Clock Data Bits In and Out LSB First */
705 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
707 else if (type == SCAN_OUT)
709 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
711 /* LOG_DEBUG("added TDI bits (o)"); */
713 else if (type == SCAN_IN)
715 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
717 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
720 BUFFER_ADD = last_bit;
724 /* move from Shift-IR/DR to end state */
725 if (type != SCAN_OUT)
727 /* Clock Data to TMS/CS Pin with Read */
729 /* LOG_DEBUG("added TMS scan (read)"); */
733 /* Clock Data to TMS/CS Pin (no Read) */
735 /* LOG_DEBUG("added TMS scan (no read)"); */
737 BUFFER_ADD = 0x6; /* scan 7 bits */
739 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
740 tap_set_state( tap_get_end_state() );
745 int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
747 int num_bytes = (scan_size + 7) / 8;
748 int bits_left = scan_size;
751 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
752 u8* receive_pointer = receive_buffer;
756 int thisrun_read = 0;
760 LOG_ERROR("BUG: large IR scans are not supported");
764 if (tap_get_state() != TAP_DRSHIFT)
766 /* command "Clock Data to TMS/CS Pin (no Read)" */
769 BUFFER_ADD = 0x6; /* scan 7 bits */
772 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_DRSHIFT);
773 tap_set_state(TAP_DRSHIFT);
776 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
778 LOG_ERROR("couldn't write MPSSE commands to FT2232");
781 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
782 ft2232_buffer_size = 0;
784 /* add command for complete bytes */
785 while (num_bytes > 1)
791 /* Clock Data Bytes In and Out LSB First */
793 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
795 else if (type == SCAN_OUT)
797 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
799 /* LOG_DEBUG("added TDI bytes (o)"); */
801 else if (type == SCAN_IN)
803 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
805 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
808 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
809 thisrun_read = thisrun_bytes;
810 num_bytes -= thisrun_bytes;
811 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
812 BUFFER_ADD = ( (thisrun_bytes - 1) >> 8 ) & 0xff;
816 /* add complete bytes */
817 while (thisrun_bytes-- > 0)
819 BUFFER_ADD = buffer[cur_byte];
824 else /* (type == SCAN_IN) */
826 bits_left -= 8 * (thisrun_bytes);
829 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
831 LOG_ERROR("couldn't write MPSSE commands to FT2232");
834 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
835 ft2232_buffer_size = 0;
837 if (type != SCAN_OUT)
839 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
841 LOG_ERROR("couldn't read from FT2232");
844 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
845 receive_pointer += bytes_read;
851 /* the most signifcant bit is scanned during TAP movement */
853 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
857 /* process remaining bits but the last one */
862 /* Clock Data Bits In and Out LSB First */
864 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
866 else if (type == SCAN_OUT)
868 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
870 /* LOG_DEBUG("added TDI bits (o)"); */
872 else if (type == SCAN_IN)
874 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
876 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
878 BUFFER_ADD = bits_left - 2;
880 BUFFER_ADD = buffer[cur_byte];
882 if (type != SCAN_OUT)
886 if (tap_get_end_state() == TAP_DRSHIFT)
890 /* Clock Data Bits In and Out LSB First */
892 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
894 else if (type == SCAN_OUT)
896 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
898 /* LOG_DEBUG("added TDI bits (o)"); */
900 else if (type == SCAN_IN)
902 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
904 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
907 BUFFER_ADD = last_bit;
911 /* move from Shift-IR/DR to end state */
912 if (type != SCAN_OUT)
914 /* Clock Data to TMS/CS Pin with Read */
916 /* LOG_DEBUG("added TMS scan (read)"); */
920 /* Clock Data to TMS/CS Pin (no Read) */
922 /* LOG_DEBUG("added TMS scan (no read)"); */
925 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() ) | (last_bit << 7);
926 tap_set_state( tap_get_end_state() );
929 if (type != SCAN_OUT)
932 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
934 LOG_ERROR("couldn't write MPSSE commands to FT2232");
937 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
938 ft2232_buffer_size = 0;
940 if (type != SCAN_OUT)
942 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
944 LOG_ERROR("couldn't read from FT2232");
947 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
948 receive_pointer += bytes_read;
955 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
957 int predicted_size = 3;
958 int num_bytes = (scan_size - 1) / 8;
960 if (tap_get_state() != TAP_DRSHIFT)
963 if (type == SCAN_IN) /* only from device to host */
966 predicted_size += CEIL(num_bytes, 65536) * 3;
967 /* remaining bits - 1 (up to 7) */
968 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
970 else /* host to device, or bidirectional */
973 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
974 /* remaining bits -1 (up to 7) */
975 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
978 return predicted_size;
982 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
984 int predicted_size = 0;
986 if (type != SCAN_OUT)
989 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
991 /* remaining bits - 1 */
992 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
994 /* last bit (from TMS scan) */
998 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1000 return predicted_size;
1004 void usbjtag_reset(int trst, int srst)
1008 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1009 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1011 low_output &= ~nTRST; /* switch output low */
1015 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1016 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1018 low_output |= nTRST; /* switch output high */
1023 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1024 low_output &= ~nSRST; /* switch output low */
1026 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1030 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1031 low_output |= nSRST; /* switch output high */
1033 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1036 /* command "set data bits low byte" */
1038 BUFFER_ADD = low_output;
1039 BUFFER_ADD = low_direction;
1043 void jtagkey_reset(int trst, int srst)
1047 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1048 high_output &= ~nTRSTnOE;
1050 high_output &= ~nTRST;
1054 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1055 high_output |= nTRSTnOE;
1057 high_output |= nTRST;
1062 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1063 high_output &= ~nSRST;
1065 high_output &= ~nSRSTnOE;
1069 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1070 high_output |= nSRST;
1072 high_output |= nSRSTnOE;
1075 /* command "set data bits high byte" */
1077 BUFFER_ADD = high_output;
1078 BUFFER_ADD = high_direction;
1079 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1084 void olimex_jtag_reset(int trst, int srst)
1088 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1089 high_output &= ~nTRSTnOE;
1091 high_output &= ~nTRST;
1095 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1096 high_output |= nTRSTnOE;
1098 high_output |= nTRST;
1103 high_output |= nSRST;
1107 high_output &= ~nSRST;
1110 /* command "set data bits high byte" */
1112 BUFFER_ADD = high_output;
1113 BUFFER_ADD = high_direction;
1114 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1119 void axm0432_jtag_reset(int trst, int srst)
1123 tap_set_state(TAP_RESET);
1124 high_output &= ~nTRST;
1128 high_output |= nTRST;
1133 high_output &= ~nSRST;
1137 high_output |= nSRST;
1140 /* command "set data bits low byte" */
1142 BUFFER_ADD = high_output;
1143 BUFFER_ADD = high_direction;
1144 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1149 void flyswatter_reset(int trst, int srst)
1153 low_output &= ~nTRST;
1157 low_output |= nTRST;
1162 low_output |= nSRST;
1166 low_output &= ~nSRST;
1169 /* command "set data bits low byte" */
1171 BUFFER_ADD = low_output;
1172 BUFFER_ADD = low_direction;
1173 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1177 void turtle_reset(int trst, int srst)
1183 low_output |= nSRST;
1187 low_output &= ~nSRST;
1190 /* command "set data bits low byte" */
1192 BUFFER_ADD = low_output;
1193 BUFFER_ADD = low_direction;
1194 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1198 void comstick_reset(int trst, int srst)
1202 high_output &= ~nTRST;
1206 high_output |= nTRST;
1211 high_output &= ~nSRST;
1215 high_output |= nSRST;
1218 /* command "set data bits high byte" */
1220 BUFFER_ADD = high_output;
1221 BUFFER_ADD = high_direction;
1222 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1227 void stm32stick_reset(int trst, int srst)
1231 high_output &= ~nTRST;
1235 high_output |= nTRST;
1240 low_output &= ~nSRST;
1244 low_output |= nSRST;
1247 /* command "set data bits low byte" */
1249 BUFFER_ADD = low_output;
1250 BUFFER_ADD = low_direction;
1252 /* command "set data bits high byte" */
1254 BUFFER_ADD = high_output;
1255 BUFFER_ADD = high_direction;
1256 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1262 void sheevaplug_reset(int trst, int srst)
1265 high_output &= ~nTRST;
1267 high_output |= nTRST;
1270 high_output &= ~nSRSTnOE;
1272 high_output |= nSRSTnOE;
1274 /* command "set data bits high byte" */
1276 BUFFER_ADD = high_output;
1277 BUFFER_ADD = high_direction;
1278 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1281 int ft2232_execute_queue()
1283 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1285 int scan_size; /* size of IR or DR scan */
1286 enum scan_type type;
1288 int predicted_size = 0;
1291 first_unsent = cmd; /* next command that has to be sent */
1294 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1295 * that wasn't handled by a caller-provided error handler
1299 ft2232_buffer_size = 0;
1300 ft2232_expect_read = 0;
1302 /* blink, if the current layout has that feature */
1310 case JTAG_END_STATE:
1311 if (cmd->cmd.end_state->end_state != TAP_INVALID)
1312 ft2232_end_state(cmd->cmd.end_state->end_state);
1316 /* only send the maximum buffer size that FT2232C can handle */
1318 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1320 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1321 retval = ERROR_JTAG_QUEUE_FAILED;
1326 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1328 tap_set_state(TAP_RESET);
1330 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1333 #ifdef _DEBUG_JTAG_IO_
1334 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1339 /* only send the maximum buffer size that FT2232C can handle */
1341 if (tap_get_state() != TAP_IDLE)
1342 predicted_size += 3;
1343 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1344 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1345 predicted_size += 3;
1346 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1347 predicted_size += 3;
1348 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1350 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1351 retval = ERROR_JTAG_QUEUE_FAILED;
1355 if (tap_get_state() != TAP_IDLE)
1357 /* command "Clock Data to TMS/CS Pin (no Read)" */
1359 BUFFER_ADD = 0x6; /* scan 7 bits */
1362 BUFFER_ADD = tap_get_tms_path(tap_get_state(), TAP_IDLE);
1363 tap_set_state(TAP_IDLE);
1366 i = cmd->cmd.runtest->num_cycles;
1369 /* command "Clock Data to TMS/CS Pin (no Read)" */
1373 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1377 tap_set_state(TAP_IDLE);
1378 i -= (i > 7) ? 7 : i;
1379 /* LOG_DEBUG("added TMS scan (no read)"); */
1382 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1383 ft2232_end_state(cmd->cmd.runtest->end_state);
1385 if ( tap_get_state() != tap_get_end_state() )
1387 /* command "Clock Data to TMS/CS Pin (no Read)" */
1392 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1393 tap_set_state( tap_get_end_state() );
1394 /* LOG_DEBUG("added TMS scan (no read)"); */
1397 #ifdef _DEBUG_JTAG_IO_
1398 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1402 case JTAG_STATEMOVE:
1403 /* only send the maximum buffer size that FT2232C can handle */
1405 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1407 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1408 retval = ERROR_JTAG_QUEUE_FAILED;
1412 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1413 ft2232_end_state(cmd->cmd.statemove->end_state);
1415 /* command "Clock Data to TMS/CS Pin (no Read)" */
1418 BUFFER_ADD = 0x6; /* scan 7 bits */
1421 BUFFER_ADD = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1422 /* LOG_DEBUG("added TMS scan (no read)"); */
1423 tap_set_state( tap_get_end_state() );
1425 #ifdef _DEBUG_JTAG_IO_
1426 LOG_DEBUG( "statemove: %s", tap_state_name( tap_get_end_state() ) );
1431 /* only send the maximum buffer size that FT2232C can handle */
1432 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1433 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1435 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1436 retval = ERROR_JTAG_QUEUE_FAILED;
1440 ft2232_add_pathmove(cmd->cmd.pathmove);
1442 #ifdef _DEBUG_JTAG_IO_
1443 LOG_DEBUG( "pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1444 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]) );
1449 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1450 type = jtag_scan_type(cmd->cmd.scan);
1451 predicted_size = ft2232_predict_scan_out(scan_size, type);
1452 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1454 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1455 /* unsent commands before this */
1456 if (first_unsent != cmd)
1457 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1458 retval = ERROR_JTAG_QUEUE_FAILED;
1460 /* current command */
1461 if (cmd->cmd.scan->end_state != TAP_INVALID)
1462 ft2232_end_state(cmd->cmd.scan->end_state);
1463 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1465 first_unsent = cmd->next;
1470 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1472 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1475 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1476 retval = ERROR_JTAG_QUEUE_FAILED;
1480 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1481 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1482 if (cmd->cmd.scan->end_state != TAP_INVALID)
1483 ft2232_end_state(cmd->cmd.scan->end_state);
1484 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1488 #ifdef _DEBUG_JTAG_IO_
1489 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1490 tap_state_name( tap_get_end_state() ) );
1495 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1496 retval = ERROR_JTAG_QUEUE_FAILED;
1497 first_unsent = cmd->next;
1498 jtag_sleep(cmd->cmd.sleep->us);
1499 #ifdef _DEBUG_JTAG_IO_
1500 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1504 case JTAG_STABLECLOCKS:
1506 /* this is only allowed while in a stable state. A check for a stable
1507 * state was done in jtag_add_clocks()
1509 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1510 retval = ERROR_JTAG_QUEUE_FAILED;
1511 #ifdef _DEBUG_JTAG_IO_
1512 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1517 LOG_ERROR("BUG: unknown JTAG command type encountered");
1524 if (require_send > 0)
1525 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1526 retval = ERROR_JTAG_QUEUE_FAILED;
1532 #if BUILD_FT2232_FTD2XX == 1
1533 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1536 DWORD openex_flags = 0;
1537 char* openex_string = NULL;
1540 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1543 /* Add non-standard Vid/Pid to the linux driver */
1544 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1546 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1550 if (ft2232_device_desc && ft2232_serial)
1552 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1553 ft2232_device_desc = NULL;
1556 if (ft2232_device_desc)
1558 openex_string = ft2232_device_desc;
1559 openex_flags = FT_OPEN_BY_DESCRIPTION;
1561 else if (ft2232_serial)
1563 openex_string = ft2232_serial;
1564 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1568 LOG_ERROR("neither device description nor serial number specified");
1569 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1571 return ERROR_JTAG_INIT_FAILED;
1574 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1575 if( status != FT_OK ){
1576 // under Win32, the FTD2XX driver appends an "A" to the end
1577 // of the description, if we tried by the desc, then
1578 // try by the alternate "A" description.
1579 if( openex_string == ft2232_device_desc ){
1580 // Try the alternate method.
1581 openex_string = ft2232_device_desc_A;
1582 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1583 if( status == FT_OK ){
1584 // yea, the "alternate" method worked!
1586 // drat, give the user a meaningfull message.
1587 // telling the use we tried *BOTH* methods.
1588 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1590 ft2232_device_desc_A );
1595 if ( status != FT_OK )
1601 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1603 return ERROR_JTAG_INIT_FAILED;
1605 LOG_ERROR("unable to open ftdi device: %lu", status);
1606 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1607 if (status == FT_OK)
1609 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1612 for (i = 0; i < num_devices; i++)
1613 desc_array[i] = malloc(64);
1615 desc_array[num_devices] = NULL;
1617 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1619 if (status == FT_OK)
1621 LOG_ERROR("ListDevices: %lu\n", num_devices);
1622 for (i = 0; i < num_devices; i++)
1623 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1626 for (i = 0; i < num_devices; i++)
1627 free(desc_array[i]);
1633 LOG_ERROR("ListDevices: NONE\n");
1635 return ERROR_JTAG_INIT_FAILED;
1638 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1640 LOG_ERROR("unable to set latency timer: %lu", status);
1641 return ERROR_JTAG_INIT_FAILED;
1644 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1646 LOG_ERROR("unable to get latency timer: %lu", status);
1647 return ERROR_JTAG_INIT_FAILED;
1651 LOG_DEBUG("current latency timer: %i", latency_timer);
1654 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1656 LOG_ERROR("unable to set timeouts: %lu", status);
1657 return ERROR_JTAG_INIT_FAILED;
1660 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1662 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1663 return ERROR_JTAG_INIT_FAILED;
1670 static int ft2232_purge_ftd2xx(void)
1674 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1676 LOG_ERROR("error purging ftd2xx device: %lu", status);
1677 return ERROR_JTAG_INIT_FAILED;
1684 #endif /* BUILD_FT2232_FTD2XX == 1 */
1686 #if BUILD_FT2232_LIBFTDI == 1
1687 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1691 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1692 ft2232_layout, vid, pid);
1694 if (ftdi_init(&ftdic) < 0)
1695 return ERROR_JTAG_INIT_FAILED;
1697 /* context, vendor id, product id */
1698 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1702 LOG_WARNING("unable to open ftdi device (trying more): %s",
1705 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1707 return ERROR_JTAG_INIT_FAILED;
1710 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1712 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1713 return ERROR_JTAG_INIT_FAILED;
1716 if (ftdi_usb_reset(&ftdic) < 0)
1718 LOG_ERROR("unable to reset ftdi device");
1719 return ERROR_JTAG_INIT_FAILED;
1722 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1724 LOG_ERROR("unable to set latency timer");
1725 return ERROR_JTAG_INIT_FAILED;
1728 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1730 LOG_ERROR("unable to get latency timer");
1731 return ERROR_JTAG_INIT_FAILED;
1735 LOG_DEBUG("current latency timer: %i", latency_timer);
1738 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1744 static int ft2232_purge_libftdi(void)
1746 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1748 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1749 return ERROR_JTAG_INIT_FAILED;
1756 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1758 int ft2232_init(void)
1763 ft2232_layout_t* cur_layout = ft2232_layouts;
1766 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1768 ft2232_layout = "usbjtag";
1769 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1772 while (cur_layout->name)
1774 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1776 layout = cur_layout;
1784 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1785 return ERROR_JTAG_INIT_FAILED;
1791 * "more indicates that there are more IDs to try, so we should
1792 * not print an error for an ID mismatch (but for anything
1795 * try_more indicates that the error code returned indicates an
1796 * ID mismatch (and nothing else) and that we should proceeed
1797 * with the next ID pair.
1799 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
1802 #if BUILD_FT2232_FTD2XX == 1
1803 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1805 #elif BUILD_FT2232_LIBFTDI == 1
1806 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1811 if (!more || !try_more)
1815 ft2232_buffer_size = 0;
1816 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1818 if (layout->init() != ERROR_OK)
1819 return ERROR_JTAG_INIT_FAILED;
1821 ft2232_speed(jtag_speed);
1823 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1824 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
1826 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1827 return ERROR_JTAG_INIT_FAILED;
1830 #if BUILD_FT2232_FTD2XX == 1
1831 return ft2232_purge_ftd2xx();
1832 #elif BUILD_FT2232_LIBFTDI == 1
1833 return ft2232_purge_libftdi();
1840 int usbjtag_init(void)
1846 low_direction = 0x0b;
1848 if (strcmp(ft2232_layout, "usbjtag") == 0)
1855 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1862 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1869 low_direction = 0x8b;
1873 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1874 return ERROR_JTAG_INIT_FAILED;
1877 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1879 low_direction &= ~nTRSTnOE; /* nTRST input */
1880 low_output &= ~nTRST; /* nTRST = 0 */
1884 low_direction |= nTRSTnOE; /* nTRST output */
1885 low_output |= nTRST; /* nTRST = 1 */
1888 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1890 low_direction |= nSRSTnOE; /* nSRST output */
1891 low_output |= nSRST; /* nSRST = 1 */
1895 low_direction &= ~nSRSTnOE; /* nSRST input */
1896 low_output &= ~nSRST; /* nSRST = 0 */
1899 /* initialize low byte for jtag */
1900 buf[0] = 0x80; /* command "set data bits low byte" */
1901 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1902 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1903 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1905 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1907 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1908 return ERROR_JTAG_INIT_FAILED;
1915 int axm0432_jtag_init(void)
1921 low_direction = 0x2b;
1923 /* initialize low byte for jtag */
1924 buf[0] = 0x80; /* command "set data bits low byte" */
1925 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1926 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1927 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1929 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1931 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1932 return ERROR_JTAG_INIT_FAILED;
1935 if (strcmp(layout->name, "axm0432_jtag") == 0)
1938 nTRSTnOE = 0x0; /* No output enable for TRST*/
1940 nSRSTnOE = 0x0; /* No output enable for SRST*/
1944 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1949 high_direction = 0x0c;
1951 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1953 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1957 high_output |= nTRST;
1960 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1962 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1966 high_output |= nSRST;
1969 /* initialize high port */
1970 buf[0] = 0x82; /* command "set data bits high byte" */
1971 buf[1] = high_output; /* value */
1972 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1973 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1975 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
1977 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1978 return ERROR_JTAG_INIT_FAILED;
1985 int jtagkey_init(void)
1991 low_direction = 0x1b;
1993 /* initialize low byte for jtag */
1994 buf[0] = 0x80; /* command "set data bits low byte" */
1995 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1996 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1997 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1999 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2001 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2002 return ERROR_JTAG_INIT_FAILED;
2005 if (strcmp(layout->name, "jtagkey") == 0)
2012 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2013 || (strcmp(layout->name, "oocdlink") == 0) )
2022 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2027 high_direction = 0x0f;
2029 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2031 high_output |= nTRSTnOE;
2032 high_output &= ~nTRST;
2036 high_output &= ~nTRSTnOE;
2037 high_output |= nTRST;
2040 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2042 high_output &= ~nSRSTnOE;
2043 high_output |= nSRST;
2047 high_output |= nSRSTnOE;
2048 high_output &= ~nSRST;
2051 /* initialize high port */
2052 buf[0] = 0x82; /* command "set data bits high byte" */
2053 buf[1] = high_output; /* value */
2054 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2055 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2057 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2059 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2060 return ERROR_JTAG_INIT_FAILED;
2067 int olimex_jtag_init(void)
2073 low_direction = 0x1b;
2075 /* initialize low byte for jtag */
2076 buf[0] = 0x80; /* command "set data bits low byte" */
2077 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2078 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2079 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2081 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2083 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2084 return ERROR_JTAG_INIT_FAILED;
2090 nSRSTnOE = 0x00; /* no output enable for nSRST */
2093 high_direction = 0x0f;
2095 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2097 high_output |= nTRSTnOE;
2098 high_output &= ~nTRST;
2102 high_output &= ~nTRSTnOE;
2103 high_output |= nTRST;
2106 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2108 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2112 high_output &= ~nSRST;
2115 /* turn red LED on */
2116 high_output |= 0x08;
2118 /* initialize high port */
2119 buf[0] = 0x82; /* command "set data bits high byte" */
2120 buf[1] = high_output; /* value */
2121 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2122 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2124 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2126 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2127 return ERROR_JTAG_INIT_FAILED;
2134 int flyswatter_init(void)
2140 low_direction = 0xfb;
2142 /* initialize low byte for jtag */
2143 buf[0] = 0x80; /* command "set data bits low byte" */
2144 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2145 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2146 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2148 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2150 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2151 return ERROR_JTAG_INIT_FAILED;
2155 nTRSTnOE = 0x0; /* not output enable for nTRST */
2157 nSRSTnOE = 0x00; /* no output enable for nSRST */
2160 high_direction = 0x0c;
2162 /* turn red LED3 on, LED2 off */
2163 high_output |= 0x08;
2165 /* initialize high port */
2166 buf[0] = 0x82; /* command "set data bits high byte" */
2167 buf[1] = high_output; /* value */
2168 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2169 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2171 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2173 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2174 return ERROR_JTAG_INIT_FAILED;
2181 int turtle_init(void)
2187 low_direction = 0x5b;
2189 /* initialize low byte for jtag */
2190 buf[0] = 0x80; /* command "set data bits low byte" */
2191 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2192 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2193 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2195 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2197 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2198 return ERROR_JTAG_INIT_FAILED;
2204 high_direction = 0x0C;
2206 /* initialize high port */
2207 buf[0] = 0x82; /* command "set data bits high byte" */
2208 buf[1] = high_output;
2209 buf[2] = high_direction;
2210 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2212 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2214 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2215 return ERROR_JTAG_INIT_FAILED;
2222 int comstick_init(void)
2228 low_direction = 0x0b;
2230 /* initialize low byte for jtag */
2231 buf[0] = 0x80; /* command "set data bits low byte" */
2232 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2233 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2234 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2236 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2238 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2239 return ERROR_JTAG_INIT_FAILED;
2243 nTRSTnOE = 0x00; /* no output enable for nTRST */
2245 nSRSTnOE = 0x00; /* no output enable for nSRST */
2248 high_direction = 0x03;
2250 /* initialize high port */
2251 buf[0] = 0x82; /* command "set data bits high byte" */
2252 buf[1] = high_output;
2253 buf[2] = high_direction;
2254 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2256 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2258 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2259 return ERROR_JTAG_INIT_FAILED;
2266 int stm32stick_init(void)
2272 low_direction = 0x8b;
2274 /* initialize low byte for jtag */
2275 buf[0] = 0x80; /* command "set data bits low byte" */
2276 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2277 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2278 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2280 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2282 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2283 return ERROR_JTAG_INIT_FAILED;
2287 nTRSTnOE = 0x00; /* no output enable for nTRST */
2289 nSRSTnOE = 0x00; /* no output enable for nSRST */
2292 high_direction = 0x03;
2294 /* initialize high port */
2295 buf[0] = 0x82; /* command "set data bits high byte" */
2296 buf[1] = high_output;
2297 buf[2] = high_direction;
2298 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2300 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2302 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2303 return ERROR_JTAG_INIT_FAILED;
2310 int sheevaplug_init(void)
2316 low_direction = 0x1b;
2318 /* initialize low byte for jtag */
2319 buf[0] = 0x80; /* command "set data bits low byte" */
2320 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2321 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2322 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2324 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2326 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2327 return ERROR_JTAG_INIT_FAILED;
2336 high_direction = 0x0f;
2338 /* nTRST is always push-pull */
2339 high_output &= ~nTRSTnOE;
2340 high_output |= nTRST;
2342 /* nSRST is always open-drain */
2343 high_output |= nSRSTnOE;
2344 high_output &= ~nSRST;
2346 /* initialize high port */
2347 buf[0] = 0x82; /* command "set data bits high byte" */
2348 buf[1] = high_output; /* value */
2349 buf[2] = high_direction; /* all outputs - xRST */
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 'sheevaplug' layout");
2355 return ERROR_JTAG_INIT_FAILED;
2361 void olimex_jtag_blink(void)
2363 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2364 * ACBUS3 is bit 3 of the GPIOH port
2366 if (high_output & 0x08)
2368 /* set port pin high */
2369 high_output &= 0x07;
2373 /* set port pin low */
2374 high_output |= 0x08;
2378 BUFFER_ADD = high_output;
2379 BUFFER_ADD = high_direction;
2383 void flyswatter_jtag_blink(void)
2386 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2388 high_output ^= 0x0c;
2391 BUFFER_ADD = high_output;
2392 BUFFER_ADD = high_direction;
2396 void turtle_jtag_blink(void)
2399 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2401 if (high_output & 0x08)
2411 BUFFER_ADD = high_output;
2412 BUFFER_ADD = high_direction;
2416 int ft2232_quit(void)
2418 #if BUILD_FT2232_FTD2XX == 1
2421 status = FT_Close(ftdih);
2422 #elif BUILD_FT2232_LIBFTDI == 1
2423 ftdi_disable_bitbang(&ftdic);
2425 ftdi_usb_close(&ftdic);
2427 ftdi_deinit(&ftdic);
2430 free(ft2232_buffer);
2431 ft2232_buffer = NULL;
2437 int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2443 ft2232_device_desc = strdup(args[0]);
2444 cp = strchr( ft2232_device_desc, 0 );
2445 // under Win32, the FTD2XX driver appends an "A" to the end
2446 // of the description, this examines the given desc
2447 // and creates the 'missing' _A or non_A variable.
2448 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2449 // it was, so make this the "A" version.
2450 ft2232_device_desc_A = ft2232_device_desc;
2451 // and *CREATE* the non-A version.
2452 strcpy( buf, ft2232_device_desc );
2453 cp = strchr( buf, 0 );
2455 ft2232_device_desc = strdup( buf );
2457 // <space>A not defined
2459 sprintf( buf, "%s A", ft2232_device_desc );
2460 ft2232_device_desc_A = strdup( buf );
2465 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2472 int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2476 ft2232_serial = strdup(args[0]);
2480 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2487 int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2492 ft2232_layout = malloc(strlen(args[0]) + 1);
2493 strcpy(ft2232_layout, args[0]);
2499 int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2503 if (argc > MAX_USB_IDS * 2)
2505 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2506 "(maximum is %d pairs)", MAX_USB_IDS);
2507 argc = MAX_USB_IDS * 2;
2509 if ( argc < 2 || (argc & 1) )
2511 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2516 for (i = 0; i + 1 < argc; i += 2)
2518 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2519 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2523 * Explicitly terminate, in case there are multiples instances of
2526 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2532 int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2536 ft2232_latency = atoi(args[0]);
2540 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2547 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2551 /* 7 bits of either ones or zeros. */
2552 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2554 while (num_cycles > 0)
2556 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2557 * at most 7 bits per invocation. Here we invoke it potentially
2560 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2562 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2564 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2565 retval = ERROR_JTAG_QUEUE_FAILED;
2570 /* command "Clock Data to TMS/CS Pin (no Read)" */
2574 BUFFER_ADD = bitcount_per_command - 1;
2576 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2581 num_cycles -= bitcount_per_command;