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 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
9 * Dick Hollenbeck <dick@softplc.com> *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
28 /* This code uses information contained in the MPSSE specification which was
30 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
31 * Hereafter this is called the "MPSSE Spec".
33 * The datasheet for the ftdichip.com's FT2232D part is here:
34 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
42 /* project specific includes */
44 #include "time_support.h"
52 #if (BUILD_FT2232_FTD2XX==1 && BUILD_FT2232_LIBFTDI==1)
53 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
54 #elif(BUILD_FT2232_FTD2XX!=1 && BUILD_FT2232_LIBFTDI!=1)
55 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
58 /* FT2232 access library includes */
59 #if BUILD_FT2232_FTD2XX == 1
61 #elif BUILD_FT2232_LIBFTDI == 1
65 /* max TCK for the high speed devices 30000 kHz */
66 #define FTDI_2232H_4232H_MAX_TCK 30000
68 static int ft2232_execute_queue(void);
70 static int ft2232_speed(int speed);
71 static int ft2232_speed_div(int speed, int* khz);
72 static int ft2232_khz(int khz, int* jtag_speed);
73 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
74 static int ft2232_init(void);
75 static int ft2232_quit(void);
77 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
78 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
79 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
80 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
81 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
85 * Function ft2232_stableclocks
86 * will send out \a num_cycles on the TCK line while the TAP(s)
87 * are in a stable state. Calling code must ensure that current state is
88 * stable, that verification is not done in here.
89 * @param num_cycles is the count of clocks cycles to send.
90 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
92 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
95 static char * ft2232_device_desc_A = NULL;
96 static char* ft2232_device_desc = NULL;
97 static char* ft2232_serial = NULL;
98 static char* ft2232_layout = NULL;
99 static u8 ft2232_latency = 2;
100 static unsigned ft2232_max_tck = 6000;
103 #define MAX_USB_IDS 8
104 /* vid = pid = 0 marks the end of the list */
105 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
106 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
108 typedef struct ft2232_layout_s
112 void (*reset)(int trst, int srst);
116 /* init procedures for supported layouts */
117 static int usbjtag_init(void);
118 static int jtagkey_init(void);
119 static int olimex_jtag_init(void);
120 static int flyswatter_init(void);
121 static int turtle_init(void);
122 static int comstick_init(void);
123 static int stm32stick_init(void);
124 static int axm0432_jtag_init(void);
125 static int sheevaplug_init(void);
126 static int icebear_jtag_init(void);
128 /* reset procedures for supported layouts */
129 static void usbjtag_reset(int trst, int srst);
130 static void jtagkey_reset(int trst, int srst);
131 static void olimex_jtag_reset(int trst, int srst);
132 static void flyswatter_reset(int trst, int srst);
133 static void turtle_reset(int trst, int srst);
134 static void comstick_reset(int trst, int srst);
135 static void stm32stick_reset(int trst, int srst);
136 static void axm0432_jtag_reset(int trst, int srst);
137 static void sheevaplug_reset(int trst, int srst);
138 static void icebear_jtag_reset(int trst, int srst);
140 /* blink procedures for layouts that support a blinking led */
141 static void olimex_jtag_blink(void);
142 static void flyswatter_jtag_blink(void);
143 static void turtle_jtag_blink(void);
145 ft2232_layout_t ft2232_layouts[] =
147 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
148 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
149 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
150 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
151 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
152 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
153 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
154 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
155 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
156 { "comstick", comstick_init, comstick_reset, NULL },
157 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
158 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
159 {"sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
160 { "icebear", icebear_jtag_init, icebear_jtag_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;
183 /* http://urjtag.wiki.sourceforge.net/Cable+FT2232 says:
185 "There is a significant difference between libftdi and libftd2xx. The latter
186 one allows to schedule up to 64*64 bytes of result data while libftdi fails
187 with more than 4*64. As a consequence, the FT2232 driver is forced to
188 perform around 16x more USB transactions for long command streams with TDO
189 capture when running with libftdi."
192 #define FT2232_BUFFER_SIZE 131072
193 a comment would have been nice.
196 #define FT2232_BUFFER_SIZE 131072
198 static u8* ft2232_buffer = NULL;
199 static int ft2232_buffer_size = 0;
200 static int ft2232_read_pointer = 0;
201 static int ft2232_expect_read = 0;
204 * Function buffer_write
205 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
206 * @param val is the byte to send.
208 static inline void buffer_write( u8 val )
210 assert( ft2232_buffer );
211 assert( (unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE );
212 ft2232_buffer[ft2232_buffer_size++] = val;
216 * Function buffer_read
217 * returns a byte from the byte buffer.
219 static inline u8 buffer_read(void)
221 assert( ft2232_buffer );
222 assert( ft2232_read_pointer < ft2232_buffer_size );
223 return ft2232_buffer[ft2232_read_pointer++];
229 * clocks out \a bit_count bits on the TMS line, starting with the least
230 * significant bit of tms_bits and progressing to more significant bits.
231 * Rigorous state transition logging is done here via tap_set_state().
233 * @param pmsse_cmd is one of the MPSSE TMS oriented commands such as 0x4b or 0x6b. See
234 * the MPSSE spec referenced above for their functionality. The MPSSE command
235 * "Clock Data to TMS/CS Pin (no Read)" is often used for this, 0x4b.
237 * @param tms_bits holds the sequence of bits to send.
238 * @param tms_count tells how many bits in the sequence.
239 * @param tdi_bit is a single bit which is passed on to TDI before the first TCK cycle
240 * and is held static for the duration of TMS clocking. See the MPSSE spec referenced above.
242 static void clock_tms( u8 mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit )
246 int tms_ndx; /* bit index into tms_byte */
248 assert( tms_count > 0 );
250 // LOG_DEBUG("mpsse cmd=%02x, tms_bits=0x%08x, bit_count=%d", mpsse_cmd, tms_bits, tms_count );
252 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
254 bool bit = tms_bits & 1;
257 tms_byte |= (1<<tms_ndx);
259 /* always do state transitions in public view */
260 tap_set_state( tap_state_transition(tap_get_state(), bit) );
262 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
267 if( tms_ndx==7 || i==tms_count-1 )
269 buffer_write( mpsse_cmd );
270 buffer_write( tms_ndx - 1 );
272 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
273 TMS/CS and is held static for the duration of TMS/CS clocking.
275 buffer_write( tms_byte | (tdi_bit << 7) );
282 * Function get_tms_buffer_requirements
283 * returns what clock_tms() will consume if called with
286 static inline int get_tms_buffer_requirements( int bit_count )
288 return ((bit_count + 6)/7) * 3;
293 * Function move_to_state
294 * moves the TAP controller from the current state to a
295 * \a goal_state through a path given by tap_get_tms_path(). State transition
296 * logging is performed by delegation to clock_tms().
298 * @param goal_state is the destination state for the move.
300 static void move_to_state( tap_state_t goal_state )
302 tap_state_t start_state = tap_get_state();
304 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
305 lookup of the required TMS pattern to move to this state from the
309 /* do the 2 lookups */
310 int tms_bits = tap_get_tms_path(start_state, goal_state);
311 int tms_count = tap_get_tms_path_len(start_state, goal_state);
313 DEBUG_JTAG_IO( "start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state) );
315 clock_tms( 0x4b, tms_bits, tms_count, 0 );
319 jtag_interface_t ft2232_interface =
322 .execute_queue = ft2232_execute_queue,
323 .speed = ft2232_speed,
324 .speed_div = ft2232_speed_div,
326 .register_commands = ft2232_register_commands,
331 static int ft2232_write(u8* buf, int size, u32* bytes_written)
333 #if BUILD_FT2232_FTD2XX == 1
335 DWORD dw_bytes_written;
336 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
338 *bytes_written = dw_bytes_written;
339 LOG_ERROR("FT_Write returned: %lu", status);
340 return ERROR_JTAG_DEVICE_ERROR;
344 *bytes_written = dw_bytes_written;
347 #elif BUILD_FT2232_LIBFTDI == 1
349 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
352 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
353 return ERROR_JTAG_DEVICE_ERROR;
357 *bytes_written = retval;
364 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
366 #if BUILD_FT2232_FTD2XX == 1
372 while ( (*bytes_read < size) && timeout-- )
374 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
375 *bytes_read, &dw_bytes_read) ) != FT_OK )
378 LOG_ERROR("FT_Read returned: %lu", status);
379 return ERROR_JTAG_DEVICE_ERROR;
381 *bytes_read += dw_bytes_read;
384 #elif BUILD_FT2232_LIBFTDI == 1
389 while ( (*bytes_read < size) && timeout-- )
391 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
394 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
395 return ERROR_JTAG_DEVICE_ERROR;
397 *bytes_read += retval;
402 if (*bytes_read < size)
404 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
405 return ERROR_JTAG_DEVICE_ERROR;
412 static int ft2232_speed(int speed)
418 buf[0] = 0x86; /* command "set divisor" */
419 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
420 buf[2] = (speed >> 8) & 0xff; /* valueH */
422 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
423 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
425 LOG_ERROR("couldn't set FT2232 TCK speed");
433 static int ft2232_speed_div(int speed, int* khz)
435 /* Take a look in the FT2232 manual,
436 * AN2232C-01 Command Processor for
437 * MPSSE and MCU Host Bus. Chapter 3.8 */
439 *khz = ft2232_max_tck / (1 + speed);
445 static int ft2232_khz(int khz, int* jtag_speed)
449 LOG_ERROR("RCLK not supported");
453 /* Take a look in the FT2232 manual,
454 * AN2232C-01 Command Processor for
455 * MPSSE and MCU Host Bus. Chapter 3.8
457 * We will calc here with a multiplier
458 * of 10 for better rounding later. */
460 /* Calc speed, (ft2232_max_tck / khz) - 1 */
461 /* Use 65000 for better rounding */
462 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
464 /* Add 0.9 for rounding */
467 /* Calc real speed */
468 *jtag_speed = *jtag_speed / 10;
470 /* Check if speed is greater than 0 */
476 /* Check max value */
477 if (*jtag_speed > 0xFFFF)
479 *jtag_speed = 0xFFFF;
486 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
488 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
489 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
490 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
491 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
492 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
493 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
494 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
495 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
496 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
497 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
502 void ft2232_end_state(tap_state_t state)
504 if (tap_is_state_stable(state))
505 tap_set_end_state(state);
508 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
514 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
516 int num_bytes = (scan_size + 7) / 8;
517 int bits_left = scan_size;
520 while (num_bytes-- > 1)
522 buffer[cur_byte++] = buffer_read();
526 buffer[cur_byte] = 0x0;
528 /* There is one more partial byte left from the clock data in/out instructions */
531 buffer[cur_byte] = buffer_read() >> 1;
533 /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
534 buffer[cur_byte] = ( buffer[cur_byte] | ( ( (buffer_read()) << 1 ) & 0x80 )) >> (8 - bits_left);
538 static void ft2232_debug_dump_buffer(void)
544 for (i = 0; i < ft2232_buffer_size; i++)
546 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
549 LOG_DEBUG("%s", line);
555 LOG_DEBUG("%s", line);
559 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
569 #ifdef _DEBUG_USB_IO_
570 struct timeval start, inter, inter2, end;
571 struct timeval d_inter, d_inter2, d_end;
574 #ifdef _DEBUG_USB_COMMS_
575 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
576 ft2232_debug_dump_buffer();
579 #ifdef _DEBUG_USB_IO_
580 gettimeofday(&start, NULL);
583 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
585 LOG_ERROR("couldn't write MPSSE commands to FT2232");
589 #ifdef _DEBUG_USB_IO_
590 gettimeofday(&inter, NULL);
593 if (ft2232_expect_read)
596 ft2232_buffer_size = 0;
598 #ifdef _DEBUG_USB_IO_
599 gettimeofday(&inter2, NULL);
602 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
604 LOG_ERROR("couldn't read from FT2232");
608 #ifdef _DEBUG_USB_IO_
609 gettimeofday(&end, NULL);
611 timeval_subtract(&d_inter, &inter, &start);
612 timeval_subtract(&d_inter2, &inter2, &start);
613 timeval_subtract(&d_end, &end, &start);
615 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
616 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
617 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
618 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
621 ft2232_buffer_size = bytes_read;
623 if (ft2232_expect_read != ft2232_buffer_size)
625 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
628 ft2232_debug_dump_buffer();
633 #ifdef _DEBUG_USB_COMMS_
634 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
635 ft2232_debug_dump_buffer();
639 ft2232_expect_read = 0;
640 ft2232_read_pointer = 0;
642 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
643 * that wasn't handled by a caller-provided error handler
653 type = jtag_scan_type(cmd->cmd.scan);
654 if (type != SCAN_OUT)
656 scan_size = jtag_scan_size(cmd->cmd.scan);
657 buffer = calloc(CEIL(scan_size, 8), 1);
658 ft2232_read_scan(type, buffer, scan_size);
659 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
660 retval = ERROR_JTAG_QUEUE_FAILED;
672 ft2232_buffer_size = 0;
679 * Function ft2232_add_pathmove
680 * moves the TAP controller from the current state to a new state through the
681 * given path, where path is an array of tap_state_t's.
683 * @param path is an array of tap_stat_t which gives the states to traverse through
684 * ending with the last state at path[num_states-1]
685 * @param num_states is the count of state steps to move through
687 static void ft2232_add_pathmove( tap_state_t* path, int num_states )
691 tap_state_t walker = tap_get_state();
693 assert( (unsigned) num_states <= 32u ); /* tms_bits only holds 32 bits */
695 /* this loop verifies that the path is legal and logs each state in the path */
696 for( state_ndx = 0; state_ndx < num_states; ++state_ndx )
698 tap_state_t desired_next_state = path[state_ndx];
700 if (tap_state_transition(walker, false) == desired_next_state )
701 ; /* bit within tms_bits at index state_ndx is already zero */
702 else if (tap_state_transition(walker, true) == desired_next_state )
703 tms_bits |= (1<<state_ndx);
706 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition",
707 tap_state_name(walker), tap_state_name(desired_next_state) );
711 walker = desired_next_state;
714 clock_tms( 0x4b, tms_bits, num_states, 0 );
716 tap_set_end_state(tap_get_state());
720 void ft2232_add_scan(bool ir_scan, enum scan_type type, u8* buffer, int scan_size)
722 int num_bytes = (scan_size + 7) / 8;
723 int bits_left = scan_size;
729 if (tap_get_state() != TAP_DRSHIFT)
731 move_to_state( TAP_DRSHIFT );
736 if (tap_get_state() != TAP_IRSHIFT)
738 move_to_state( TAP_IRSHIFT );
742 /* add command for complete bytes */
743 while (num_bytes > 1)
748 /* Clock Data Bytes In and Out LSB First */
749 buffer_write( 0x39 );
750 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
752 else if (type == SCAN_OUT)
754 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
755 buffer_write( 0x19 );
756 /* LOG_DEBUG("added TDI bytes (o)"); */
758 else if (type == SCAN_IN)
760 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
761 buffer_write( 0x28 );
762 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
765 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
766 num_bytes -= thisrun_bytes;
768 buffer_write( (u8) (thisrun_bytes - 1) );
769 buffer_write( (u8) ((thisrun_bytes - 1) >> 8) );
773 /* add complete bytes */
774 while (thisrun_bytes-- > 0)
776 buffer_write( buffer[cur_byte++] );
780 else /* (type == SCAN_IN) */
782 bits_left -= 8 * (thisrun_bytes);
786 /* the most signifcant bit is scanned during TAP movement */
788 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
792 /* process remaining bits but the last one */
797 /* Clock Data Bits In and Out LSB First */
798 buffer_write( 0x3b );
799 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
801 else if (type == SCAN_OUT)
803 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
804 buffer_write( 0x1b );
805 /* LOG_DEBUG("added TDI bits (o)"); */
807 else if (type == SCAN_IN)
809 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
810 buffer_write( 0x2a );
811 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
814 buffer_write( bits_left - 2 );
816 buffer_write( buffer[cur_byte] );
819 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
820 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
824 /* Clock Data Bits In and Out LSB First */
825 buffer_write( 0x3b );
826 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
828 else if (type == SCAN_OUT)
830 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
831 buffer_write( 0x1b );
832 /* LOG_DEBUG("added TDI bits (o)"); */
834 else if (type == SCAN_IN)
836 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
837 buffer_write( 0x2a );
838 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
841 buffer_write( last_bit );
849 /* move from Shift-IR/DR to end state */
850 if (type != SCAN_OUT)
852 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
853 /* This must be coordinated with the bit shifts in ft2232_read_scan */
856 /* Clock Data to TMS/CS Pin with Read */
858 /* LOG_DEBUG("added TMS scan (read)"); */
862 tms_bits = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
863 tms_count = tap_get_tms_path_len( tap_get_state(), tap_get_end_state() );
864 /* Clock Data to TMS/CS Pin (no Read) */
866 /* LOG_DEBUG("added TMS scan (no read)"); */
869 clock_tms( mpsse_cmd, tms_bits, tms_count, last_bit );
872 if (tap_get_state() != tap_get_end_state())
874 move_to_state( tap_get_end_state() );
879 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
881 int num_bytes = (scan_size + 7) / 8;
882 int bits_left = scan_size;
885 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
886 u8* receive_pointer = receive_buffer;
890 int thisrun_read = 0;
894 LOG_ERROR("BUG: large IR scans are not supported");
898 if (tap_get_state() != TAP_DRSHIFT)
900 move_to_state( TAP_DRSHIFT );
903 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
905 LOG_ERROR("couldn't write MPSSE commands to FT2232");
908 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
909 ft2232_buffer_size = 0;
911 /* add command for complete bytes */
912 while (num_bytes > 1)
918 /* Clock Data Bytes In and Out LSB First */
919 buffer_write( 0x39 );
920 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
922 else if (type == SCAN_OUT)
924 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
925 buffer_write( 0x19 );
926 /* LOG_DEBUG("added TDI bytes (o)"); */
928 else if (type == SCAN_IN)
930 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
931 buffer_write( 0x28 );
932 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
935 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
936 thisrun_read = thisrun_bytes;
937 num_bytes -= thisrun_bytes;
938 buffer_write( (u8) (thisrun_bytes - 1) );
939 buffer_write( (u8) ( (thisrun_bytes - 1) >> 8 ));
943 /* add complete bytes */
944 while (thisrun_bytes-- > 0)
946 buffer_write( buffer[cur_byte] );
951 else /* (type == SCAN_IN) */
953 bits_left -= 8 * (thisrun_bytes);
956 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
958 LOG_ERROR("couldn't write MPSSE commands to FT2232");
961 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
962 ft2232_buffer_size = 0;
964 if (type != SCAN_OUT)
966 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
968 LOG_ERROR("couldn't read from FT2232");
971 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
972 receive_pointer += bytes_read;
978 /* the most signifcant bit is scanned during TAP movement */
980 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
984 /* process remaining bits but the last one */
989 /* Clock Data Bits In and Out LSB First */
990 buffer_write( 0x3b );
991 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
993 else if (type == SCAN_OUT)
995 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
996 buffer_write( 0x1b );
997 /* LOG_DEBUG("added TDI bits (o)"); */
999 else if (type == SCAN_IN)
1001 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1002 buffer_write( 0x2a );
1003 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1005 buffer_write( bits_left - 2 );
1006 if (type != SCAN_IN)
1007 buffer_write( buffer[cur_byte] );
1009 if (type != SCAN_OUT)
1013 if (tap_get_end_state() == TAP_DRSHIFT)
1015 if (type == SCAN_IO)
1017 /* Clock Data Bits In and Out LSB First */
1018 buffer_write( 0x3b );
1019 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1021 else if (type == SCAN_OUT)
1023 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1024 buffer_write( 0x1b );
1025 /* LOG_DEBUG("added TDI bits (o)"); */
1027 else if (type == SCAN_IN)
1029 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1030 buffer_write( 0x2a );
1031 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1033 buffer_write( 0x0 );
1034 buffer_write( last_bit );
1038 int tms_bits = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1039 int tms_count = tap_get_tms_path_len( tap_get_state(), tap_get_end_state() );
1042 /* move from Shift-IR/DR to end state */
1043 if (type != SCAN_OUT)
1045 /* Clock Data to TMS/CS Pin with Read */
1047 /* LOG_DEBUG("added TMS scan (read)"); */
1051 /* Clock Data to TMS/CS Pin (no Read) */
1053 /* LOG_DEBUG("added TMS scan (no read)"); */
1056 clock_tms( mpsse_cmd, tms_bits, tms_count, last_bit );
1059 if (type != SCAN_OUT)
1062 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
1064 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1067 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
1068 ft2232_buffer_size = 0;
1070 if (type != SCAN_OUT)
1072 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
1074 LOG_ERROR("couldn't read from FT2232");
1077 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
1078 receive_pointer += bytes_read;
1085 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1087 int predicted_size = 3;
1088 int num_bytes = (scan_size - 1) / 8;
1090 if (tap_get_state() != TAP_DRSHIFT)
1091 predicted_size += get_tms_buffer_requirements( tap_get_tms_path_len( tap_get_state(), TAP_DRSHIFT) );
1093 if (type == SCAN_IN) /* only from device to host */
1095 /* complete bytes */
1096 predicted_size += CEIL(num_bytes, 65536) * 3;
1098 /* remaining bits - 1 (up to 7) */
1099 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
1101 else /* host to device, or bidirectional */
1103 /* complete bytes */
1104 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
1106 /* remaining bits -1 (up to 7) */
1107 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
1110 return predicted_size;
1114 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1116 int predicted_size = 0;
1118 if (type != SCAN_OUT)
1120 /* complete bytes */
1121 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
1123 /* remaining bits - 1 */
1124 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
1126 /* last bit (from TMS scan) */
1127 predicted_size += 1;
1130 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1132 return predicted_size;
1136 static void usbjtag_reset(int trst, int srst)
1140 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1141 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1143 low_output &= ~nTRST; /* switch output low */
1147 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1148 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1150 low_output |= nTRST; /* switch output high */
1155 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1156 low_output &= ~nSRST; /* switch output low */
1158 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1162 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1163 low_output |= nSRST; /* switch output high */
1165 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1168 /* command "set data bits low byte" */
1169 buffer_write( 0x80 );
1170 buffer_write( low_output );
1171 buffer_write( low_direction );
1175 static void jtagkey_reset(int trst, int srst)
1179 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1180 high_output &= ~nTRSTnOE;
1182 high_output &= ~nTRST;
1186 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1187 high_output |= nTRSTnOE;
1189 high_output |= nTRST;
1194 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1195 high_output &= ~nSRST;
1197 high_output &= ~nSRSTnOE;
1201 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1202 high_output |= nSRST;
1204 high_output |= nSRSTnOE;
1207 /* command "set data bits high byte" */
1208 buffer_write( 0x82 );
1209 buffer_write( high_output );
1210 buffer_write( high_direction );
1211 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1216 static void olimex_jtag_reset(int trst, int srst)
1220 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1221 high_output &= ~nTRSTnOE;
1223 high_output &= ~nTRST;
1227 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1228 high_output |= nTRSTnOE;
1230 high_output |= nTRST;
1235 high_output |= nSRST;
1239 high_output &= ~nSRST;
1242 /* command "set data bits high byte" */
1243 buffer_write( 0x82 );
1244 buffer_write( high_output );
1245 buffer_write( high_direction );
1246 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1251 static void axm0432_jtag_reset(int trst, int srst)
1255 tap_set_state(TAP_RESET);
1256 high_output &= ~nTRST;
1260 high_output |= nTRST;
1265 high_output &= ~nSRST;
1269 high_output |= nSRST;
1272 /* command "set data bits low byte" */
1273 buffer_write( 0x82 );
1274 buffer_write( high_output );
1275 buffer_write( high_direction );
1276 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1281 static void flyswatter_reset(int trst, int srst)
1285 low_output &= ~nTRST;
1289 low_output |= nTRST;
1294 low_output |= nSRST;
1298 low_output &= ~nSRST;
1301 /* command "set data bits low byte" */
1302 buffer_write( 0x80 );
1303 buffer_write( low_output );
1304 buffer_write( low_direction );
1305 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1309 static void turtle_reset(int trst, int srst)
1315 low_output |= nSRST;
1319 low_output &= ~nSRST;
1322 /* command "set data bits low byte" */
1323 buffer_write( 0x80 );
1324 buffer_write( low_output );
1325 buffer_write( low_direction );
1326 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1330 static void comstick_reset(int trst, int srst)
1334 high_output &= ~nTRST;
1338 high_output |= nTRST;
1343 high_output &= ~nSRST;
1347 high_output |= nSRST;
1350 /* command "set data bits high byte" */
1351 buffer_write( 0x82 );
1352 buffer_write( high_output );
1353 buffer_write( high_direction );
1354 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1359 static void stm32stick_reset(int trst, int srst)
1363 high_output &= ~nTRST;
1367 high_output |= nTRST;
1372 low_output &= ~nSRST;
1376 low_output |= nSRST;
1379 /* command "set data bits low byte" */
1380 buffer_write( 0x80 );
1381 buffer_write( low_output );
1382 buffer_write( low_direction );
1384 /* command "set data bits high byte" */
1385 buffer_write( 0x82 );
1386 buffer_write( high_output );
1387 buffer_write( high_direction );
1388 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1394 static void sheevaplug_reset(int trst, int srst)
1397 high_output &= ~nTRST;
1399 high_output |= nTRST;
1402 high_output &= ~nSRSTnOE;
1404 high_output |= nSRSTnOE;
1406 /* command "set data bits high byte" */
1407 buffer_write( 0x82 );
1408 buffer_write( high_output );
1409 buffer_write( high_direction );
1410 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1413 static int ft2232_execute_end_state(jtag_command_t *cmd)
1418 DEBUG_JTAG_IO("execute_end_state: %s", tap_state_name(cmd->cmd.end_state->end_state) );
1420 if (cmd->cmd.end_state->end_state != TAP_INVALID)
1421 ft2232_end_state(cmd->cmd.end_state->end_state);
1427 static int ft2232_execute_runtest(jtag_command_t *cmd)
1431 int predicted_size = 0;
1434 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1435 cmd->cmd.runtest->num_cycles,
1436 tap_state_name(cmd->cmd.runtest->end_state));
1438 /* only send the maximum buffer size that FT2232C can handle */
1440 if (tap_get_state() != TAP_IDLE)
1441 predicted_size += 3;
1442 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1443 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1444 predicted_size += 3;
1445 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1446 predicted_size += 3;
1447 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1449 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1450 retval = ERROR_JTAG_QUEUE_FAILED;
1454 if (tap_get_state() != TAP_IDLE)
1456 move_to_state( TAP_IDLE );
1459 i = cmd->cmd.runtest->num_cycles;
1462 /* there are no state transitions in this code, so omit state tracking */
1464 /* command "Clock Data to TMS/CS Pin (no Read)" */
1465 buffer_write( 0x4b );
1468 buffer_write( (i > 7) ? 6 : (i - 1) );
1471 buffer_write( 0x0 );
1472 tap_set_state(TAP_IDLE);
1474 i -= (i > 7) ? 7 : i;
1475 /* LOG_DEBUG("added TMS scan (no read)"); */
1478 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1479 ft2232_end_state(cmd->cmd.runtest->end_state);
1481 if ( tap_get_state() != tap_get_end_state() )
1483 move_to_state( tap_get_end_state() );
1487 #ifdef _DEBUG_JTAG_IO_
1488 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1495 static int ft2232_execute_statemove(jtag_command_t *cmd)
1497 int predicted_size = 0;
1498 int retval = ERROR_OK;
1500 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1502 /* only send the maximum buffer size that FT2232C can handle */
1504 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1506 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1507 retval = ERROR_JTAG_QUEUE_FAILED;
1511 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1512 ft2232_end_state(cmd->cmd.statemove->end_state);
1514 /* move to end state */
1515 if ( tap_get_state() != tap_get_end_state() )
1517 move_to_state( tap_get_end_state() );
1524 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1526 int predicted_size = 0;
1527 int retval = ERROR_OK;
1529 tap_state_t* path = cmd->cmd.pathmove->path;
1530 int num_states = cmd->cmd.pathmove->num_states;
1532 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1533 tap_state_name( tap_get_state() ),
1534 tap_state_name( path[num_states-1] )
1537 /* only send the maximum buffer size that FT2232C can handle */
1538 predicted_size = 3 * CEIL(num_states, 7);
1539 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1541 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1542 retval = ERROR_JTAG_QUEUE_FAILED;
1548 ft2232_add_pathmove( path, num_states );
1555 static int ft2232_execute_scan(jtag_command_t *cmd)
1558 int scan_size; /* size of IR or DR scan */
1559 int predicted_size = 0;
1560 int retval = ERROR_OK;
1562 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1564 DEBUG_JTAG_IO( "%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type );
1566 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1568 predicted_size = ft2232_predict_scan_out(scan_size, type);
1569 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1571 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1572 /* unsent commands before this */
1573 if (first_unsent != cmd)
1574 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1575 retval = ERROR_JTAG_QUEUE_FAILED;
1577 /* current command */
1578 if (cmd->cmd.scan->end_state != TAP_INVALID)
1579 ft2232_end_state(cmd->cmd.scan->end_state);
1580 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1582 first_unsent = cmd->next;
1587 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1589 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1592 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1593 retval = ERROR_JTAG_QUEUE_FAILED;
1597 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1598 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1599 if (cmd->cmd.scan->end_state != TAP_INVALID)
1600 ft2232_end_state(cmd->cmd.scan->end_state);
1601 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1605 #ifdef _DEBUG_JTAG_IO_
1606 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1607 tap_state_name( tap_get_end_state() ) );
1613 static int ft2232_execute_reset(jtag_command_t *cmd)
1616 int predicted_size = 0;
1619 DEBUG_JTAG_IO("reset trst: %i srst %i",
1620 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1622 /* only send the maximum buffer size that FT2232C can handle */
1624 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1626 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1627 retval = ERROR_JTAG_QUEUE_FAILED;
1632 if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
1634 tap_set_state(TAP_RESET);
1636 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1639 #ifdef _DEBUG_JTAG_IO_
1640 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1645 static int ft2232_execute_sleep(jtag_command_t *cmd)
1650 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1652 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1653 retval = ERROR_JTAG_QUEUE_FAILED;
1654 first_unsent = cmd->next;
1655 jtag_sleep(cmd->cmd.sleep->us);
1656 #ifdef _DEBUG_JTAG_IO_
1657 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1663 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1668 /* this is only allowed while in a stable state. A check for a stable
1669 * state was done in jtag_add_clocks()
1671 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1672 retval = ERROR_JTAG_QUEUE_FAILED;
1673 #ifdef _DEBUG_JTAG_IO_
1674 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1680 static int ft2232_execute_command(jtag_command_t *cmd)
1687 case JTAG_END_STATE: retval = ft2232_execute_end_state(cmd); break;
1688 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1689 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1690 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1691 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1692 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1693 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1694 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1696 LOG_ERROR("BUG: unknown JTAG command type encountered");
1702 static int ft2232_execute_queue()
1704 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1707 first_unsent = cmd; /* next command that has to be sent */
1710 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1711 * that wasn't handled by a caller-provided error handler
1715 ft2232_buffer_size = 0;
1716 ft2232_expect_read = 0;
1718 /* blink, if the current layout has that feature */
1724 if (ft2232_execute_command(cmd) != ERROR_OK)
1725 retval = ERROR_JTAG_QUEUE_FAILED;
1726 /* Start reading input before FT2232 TX buffer fills up */
1728 if (ft2232_expect_read > 256)
1730 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1731 retval = ERROR_JTAG_QUEUE_FAILED;
1736 if (require_send > 0)
1737 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1738 retval = ERROR_JTAG_QUEUE_FAILED;
1744 #if BUILD_FT2232_FTD2XX == 1
1745 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1748 DWORD openex_flags = 0;
1749 char* openex_string = NULL;
1752 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1755 /* Add non-standard Vid/Pid to the linux driver */
1756 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1758 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1762 if (ft2232_device_desc && ft2232_serial)
1764 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1765 ft2232_device_desc = NULL;
1768 if (ft2232_device_desc)
1770 openex_string = ft2232_device_desc;
1771 openex_flags = FT_OPEN_BY_DESCRIPTION;
1773 else if (ft2232_serial)
1775 openex_string = ft2232_serial;
1776 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1780 LOG_ERROR("neither device description nor serial number specified");
1781 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1783 return ERROR_JTAG_INIT_FAILED;
1786 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1787 if( status != FT_OK ){
1788 // under Win32, the FTD2XX driver appends an "A" to the end
1789 // of the description, if we tried by the desc, then
1790 // try by the alternate "A" description.
1791 if( openex_string == ft2232_device_desc ){
1792 // Try the alternate method.
1793 openex_string = ft2232_device_desc_A;
1794 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1795 if( status == FT_OK ){
1796 // yea, the "alternate" method worked!
1798 // drat, give the user a meaningfull message.
1799 // telling the use we tried *BOTH* methods.
1800 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1802 ft2232_device_desc_A );
1807 if ( status != FT_OK )
1813 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1815 return ERROR_JTAG_INIT_FAILED;
1817 LOG_ERROR("unable to open ftdi device: %lu", status);
1818 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1819 if (status == FT_OK)
1821 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1824 for (i = 0; i < num_devices; i++)
1825 desc_array[i] = malloc(64);
1827 desc_array[num_devices] = NULL;
1829 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1831 if (status == FT_OK)
1833 LOG_ERROR("ListDevices: %lu\n", num_devices);
1834 for (i = 0; i < num_devices; i++)
1835 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1838 for (i = 0; i < num_devices; i++)
1839 free(desc_array[i]);
1845 LOG_ERROR("ListDevices: NONE\n");
1847 return ERROR_JTAG_INIT_FAILED;
1850 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1852 LOG_ERROR("unable to set latency timer: %lu", status);
1853 return ERROR_JTAG_INIT_FAILED;
1856 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1858 LOG_ERROR("unable to get latency timer: %lu", status);
1859 return ERROR_JTAG_INIT_FAILED;
1863 LOG_DEBUG("current latency timer: %i", latency_timer);
1866 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1868 LOG_ERROR("unable to set timeouts: %lu", status);
1869 return ERROR_JTAG_INIT_FAILED;
1872 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1874 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1875 return ERROR_JTAG_INIT_FAILED;
1882 static int ft2232_purge_ftd2xx(void)
1886 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1888 LOG_ERROR("error purging ftd2xx device: %lu", status);
1889 return ERROR_JTAG_INIT_FAILED;
1896 #endif /* BUILD_FT2232_FTD2XX == 1 */
1898 #if BUILD_FT2232_LIBFTDI == 1
1899 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1903 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1904 ft2232_layout, vid, pid);
1906 if (ftdi_init(&ftdic) < 0)
1907 return ERROR_JTAG_INIT_FAILED;
1909 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1911 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1912 return ERROR_JTAG_INIT_FAILED;
1915 /* context, vendor id, product id */
1916 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1920 LOG_WARNING("unable to open ftdi device (trying more): %s",
1923 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1925 return ERROR_JTAG_INIT_FAILED;
1928 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
1929 if (ftdi_usb_reset(&ftdic) < 0)
1931 LOG_ERROR("unable to reset ftdi device");
1932 return ERROR_JTAG_INIT_FAILED;
1935 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1937 LOG_ERROR("unable to set latency timer");
1938 return ERROR_JTAG_INIT_FAILED;
1941 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1943 LOG_ERROR("unable to get latency timer");
1944 return ERROR_JTAG_INIT_FAILED;
1948 LOG_DEBUG("current latency timer: %i", latency_timer);
1951 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1957 static int ft2232_purge_libftdi(void)
1959 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1961 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1962 return ERROR_JTAG_INIT_FAILED;
1969 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1971 static int ft2232_init(void)
1976 ft2232_layout_t* cur_layout = ft2232_layouts;
1979 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE)==7)
1981 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
1985 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
1988 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1990 ft2232_layout = "usbjtag";
1991 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1994 while (cur_layout->name)
1996 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1998 layout = cur_layout;
2006 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2007 return ERROR_JTAG_INIT_FAILED;
2013 * "more indicates that there are more IDs to try, so we should
2014 * not print an error for an ID mismatch (but for anything
2017 * try_more indicates that the error code returned indicates an
2018 * ID mismatch (and nothing else) and that we should proceeed
2019 * with the next ID pair.
2021 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2024 #if BUILD_FT2232_FTD2XX == 1
2025 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2027 #elif BUILD_FT2232_LIBFTDI == 1
2028 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2033 if (!more || !try_more)
2037 ft2232_buffer_size = 0;
2038 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2040 if (layout->init() != ERROR_OK)
2041 return ERROR_JTAG_INIT_FAILED;
2043 ft2232_speed(jtag_speed);
2045 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2046 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
2048 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2049 return ERROR_JTAG_INIT_FAILED;
2052 #if BUILD_FT2232_FTD2XX == 1
2053 return ft2232_purge_ftd2xx();
2054 #elif BUILD_FT2232_LIBFTDI == 1
2055 return ft2232_purge_libftdi();
2062 static int usbjtag_init(void)
2068 low_direction = 0x0b;
2070 if (strcmp(ft2232_layout, "usbjtag") == 0)
2077 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2084 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2091 low_direction = 0x8b;
2095 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2096 return ERROR_JTAG_INIT_FAILED;
2099 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2101 low_direction &= ~nTRSTnOE; /* nTRST input */
2102 low_output &= ~nTRST; /* nTRST = 0 */
2106 low_direction |= nTRSTnOE; /* nTRST output */
2107 low_output |= nTRST; /* nTRST = 1 */
2110 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2112 low_direction |= nSRSTnOE; /* nSRST output */
2113 low_output |= nSRST; /* nSRST = 1 */
2117 low_direction &= ~nSRSTnOE; /* nSRST input */
2118 low_output &= ~nSRST; /* nSRST = 0 */
2121 /* initialize low byte for jtag */
2122 buf[0] = 0x80; /* command "set data bits low byte" */
2123 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
2124 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2125 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2127 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2129 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2130 return ERROR_JTAG_INIT_FAILED;
2137 static int axm0432_jtag_init(void)
2143 low_direction = 0x2b;
2145 /* initialize low byte for jtag */
2146 buf[0] = 0x80; /* command "set data bits low byte" */
2147 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2148 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2149 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2151 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2153 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2154 return ERROR_JTAG_INIT_FAILED;
2157 if (strcmp(layout->name, "axm0432_jtag") == 0)
2160 nTRSTnOE = 0x0; /* No output enable for TRST*/
2162 nSRSTnOE = 0x0; /* No output enable for SRST*/
2166 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2171 high_direction = 0x0c;
2173 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2175 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2179 high_output |= nTRST;
2182 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2184 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2188 high_output |= nSRST;
2191 /* initialize high port */
2192 buf[0] = 0x82; /* command "set data bits high byte" */
2193 buf[1] = high_output; /* value */
2194 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2195 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2197 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2199 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2200 return ERROR_JTAG_INIT_FAILED;
2207 static int jtagkey_init(void)
2213 low_direction = 0x1b;
2215 /* initialize low byte for jtag */
2216 buf[0] = 0x80; /* command "set data bits low byte" */
2217 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2218 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2219 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2221 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2223 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2224 return ERROR_JTAG_INIT_FAILED;
2227 if (strcmp(layout->name, "jtagkey") == 0)
2234 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2235 || (strcmp(layout->name, "oocdlink") == 0) )
2244 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2249 high_direction = 0x0f;
2251 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2253 high_output |= nTRSTnOE;
2254 high_output &= ~nTRST;
2258 high_output &= ~nTRSTnOE;
2259 high_output |= nTRST;
2262 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2264 high_output &= ~nSRSTnOE;
2265 high_output |= nSRST;
2269 high_output |= nSRSTnOE;
2270 high_output &= ~nSRST;
2273 /* initialize high port */
2274 buf[0] = 0x82; /* command "set data bits high byte" */
2275 buf[1] = high_output; /* value */
2276 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2277 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2279 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2281 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2282 return ERROR_JTAG_INIT_FAILED;
2289 static int olimex_jtag_init(void)
2295 low_direction = 0x1b;
2297 /* initialize low byte for jtag */
2298 buf[0] = 0x80; /* command "set data bits low byte" */
2299 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2300 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2301 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2303 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2305 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2306 return ERROR_JTAG_INIT_FAILED;
2312 nSRSTnOE = 0x00; /* no output enable for nSRST */
2315 high_direction = 0x0f;
2317 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2319 high_output |= nTRSTnOE;
2320 high_output &= ~nTRST;
2324 high_output &= ~nTRSTnOE;
2325 high_output |= nTRST;
2328 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2330 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2334 high_output &= ~nSRST;
2337 /* turn red LED on */
2338 high_output |= 0x08;
2340 /* initialize high port */
2341 buf[0] = 0x82; /* command "set data bits high byte" */
2342 buf[1] = high_output; /* value */
2343 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2344 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2346 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2348 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2349 return ERROR_JTAG_INIT_FAILED;
2356 static int flyswatter_init(void)
2362 low_direction = 0xfb;
2364 /* initialize low byte for jtag */
2365 buf[0] = 0x80; /* command "set data bits low byte" */
2366 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2367 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2368 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2370 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2372 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2373 return ERROR_JTAG_INIT_FAILED;
2377 nTRSTnOE = 0x0; /* not output enable for nTRST */
2379 nSRSTnOE = 0x00; /* no output enable for nSRST */
2382 high_direction = 0x0c;
2384 /* turn red LED3 on, LED2 off */
2385 high_output |= 0x08;
2387 /* initialize high port */
2388 buf[0] = 0x82; /* command "set data bits high byte" */
2389 buf[1] = high_output; /* value */
2390 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2391 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2393 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2395 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2396 return ERROR_JTAG_INIT_FAILED;
2403 static int turtle_init(void)
2409 low_direction = 0x5b;
2411 /* initialize low byte for jtag */
2412 buf[0] = 0x80; /* command "set data bits low byte" */
2413 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2414 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2415 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2417 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2419 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2420 return ERROR_JTAG_INIT_FAILED;
2426 high_direction = 0x0C;
2428 /* initialize high port */
2429 buf[0] = 0x82; /* command "set data bits high byte" */
2430 buf[1] = high_output;
2431 buf[2] = high_direction;
2432 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2434 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2436 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2437 return ERROR_JTAG_INIT_FAILED;
2444 static int comstick_init(void)
2450 low_direction = 0x0b;
2452 /* initialize low byte for jtag */
2453 buf[0] = 0x80; /* command "set data bits low byte" */
2454 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2455 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2456 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2458 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2460 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2461 return ERROR_JTAG_INIT_FAILED;
2465 nTRSTnOE = 0x00; /* no output enable for nTRST */
2467 nSRSTnOE = 0x00; /* no output enable for nSRST */
2470 high_direction = 0x03;
2472 /* initialize high port */
2473 buf[0] = 0x82; /* command "set data bits high byte" */
2474 buf[1] = high_output;
2475 buf[2] = high_direction;
2476 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2478 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2480 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2481 return ERROR_JTAG_INIT_FAILED;
2488 static int stm32stick_init(void)
2494 low_direction = 0x8b;
2496 /* initialize low byte for jtag */
2497 buf[0] = 0x80; /* command "set data bits low byte" */
2498 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2499 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2500 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2502 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2504 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2505 return ERROR_JTAG_INIT_FAILED;
2509 nTRSTnOE = 0x00; /* no output enable for nTRST */
2511 nSRSTnOE = 0x00; /* no output enable for nSRST */
2514 high_direction = 0x03;
2516 /* initialize high port */
2517 buf[0] = 0x82; /* command "set data bits high byte" */
2518 buf[1] = high_output;
2519 buf[2] = high_direction;
2520 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2522 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2524 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2525 return ERROR_JTAG_INIT_FAILED;
2532 static int sheevaplug_init(void)
2538 low_direction = 0x1b;
2540 /* initialize low byte for jtag */
2541 buf[0] = 0x80; /* command "set data bits low byte" */
2542 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2543 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2544 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2546 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2548 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2549 return ERROR_JTAG_INIT_FAILED;
2558 high_direction = 0x0f;
2560 /* nTRST is always push-pull */
2561 high_output &= ~nTRSTnOE;
2562 high_output |= nTRST;
2564 /* nSRST is always open-drain */
2565 high_output |= nSRSTnOE;
2566 high_output &= ~nSRST;
2568 /* initialize high port */
2569 buf[0] = 0x82; /* command "set data bits high byte" */
2570 buf[1] = high_output; /* value */
2571 buf[2] = high_direction; /* all outputs - xRST */
2572 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2574 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2576 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2577 return ERROR_JTAG_INIT_FAILED;
2583 static void olimex_jtag_blink(void)
2585 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2586 * ACBUS3 is bit 3 of the GPIOH port
2588 if (high_output & 0x08)
2590 /* set port pin high */
2591 high_output &= 0x07;
2595 /* set port pin low */
2596 high_output |= 0x08;
2599 buffer_write( 0x82 );
2600 buffer_write( high_output );
2601 buffer_write( high_direction );
2605 static void flyswatter_jtag_blink(void)
2608 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2610 high_output ^= 0x0c;
2612 buffer_write( 0x82 );
2613 buffer_write( high_output );
2614 buffer_write( high_direction );
2618 static void turtle_jtag_blink(void)
2621 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2623 if (high_output & 0x08)
2632 buffer_write( 0x82 );
2633 buffer_write( high_output );
2634 buffer_write( high_direction );
2638 static int ft2232_quit(void)
2640 #if BUILD_FT2232_FTD2XX == 1
2643 status = FT_Close(ftdih);
2644 #elif BUILD_FT2232_LIBFTDI == 1
2645 ftdi_disable_bitbang(&ftdic);
2647 ftdi_usb_close(&ftdic);
2649 ftdi_deinit(&ftdic);
2652 free(ft2232_buffer);
2653 ft2232_buffer = NULL;
2659 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2665 ft2232_device_desc = strdup(args[0]);
2666 cp = strchr( ft2232_device_desc, 0 );
2667 // under Win32, the FTD2XX driver appends an "A" to the end
2668 // of the description, this examines the given desc
2669 // and creates the 'missing' _A or non_A variable.
2670 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2671 // it was, so make this the "A" version.
2672 ft2232_device_desc_A = ft2232_device_desc;
2673 // and *CREATE* the non-A version.
2674 strcpy( buf, ft2232_device_desc );
2675 cp = strchr( buf, 0 );
2677 ft2232_device_desc = strdup( buf );
2679 // <space>A not defined
2681 sprintf( buf, "%s A", ft2232_device_desc );
2682 ft2232_device_desc_A = strdup( buf );
2687 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2694 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2698 ft2232_serial = strdup(args[0]);
2702 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2709 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2714 ft2232_layout = malloc(strlen(args[0]) + 1);
2715 strcpy(ft2232_layout, args[0]);
2721 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2725 if (argc > MAX_USB_IDS * 2)
2727 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2728 "(maximum is %d pairs)", MAX_USB_IDS);
2729 argc = MAX_USB_IDS * 2;
2731 if ( argc < 2 || (argc & 1) )
2733 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2738 for (i = 0; i + 1 < argc; i += 2)
2740 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2741 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2745 * Explicitly terminate, in case there are multiples instances of
2748 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2754 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2758 ft2232_latency = atoi(args[0]);
2762 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2769 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2773 /* 7 bits of either ones or zeros. */
2774 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2776 while (num_cycles > 0)
2778 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2779 * at most 7 bits per invocation. Here we invoke it potentially
2782 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2784 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2786 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2787 retval = ERROR_JTAG_QUEUE_FAILED;
2792 /* there are no state transitions in this code, so omit state tracking */
2794 /* command "Clock Data to TMS/CS Pin (no Read)" */
2795 buffer_write( 0x4b );
2798 buffer_write( bitcount_per_command - 1 );
2800 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2801 buffer_write( tms );
2805 num_cycles -= bitcount_per_command;
2812 /* ---------------------------------------------------------------------
2813 * Support for IceBear JTAG adapter from Section5:
2814 * http://section5.ch/icebear
2816 * Author: Sten, debian@sansys-electronic.com
2819 /* Icebear pin layout
2821 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2822 * GND GND | 4 3| n.c.
2823 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2824 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2825 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2826 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2827 * ADBUS2 TDO |14 13| GND GND
2829 * ADBUS0 O L TCK ACBUS0 GND
2830 * ADBUS1 O L TDI ACBUS1 GND
2831 * ADBUS2 I TDO ACBUS2 n.c.
2832 * ADBUS3 O H TMS ACBUS3 n.c.
2838 static int icebear_jtag_init(void) {
2842 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2843 low_output = 0x08; /* high: TMS; low: TCK TDI */
2847 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2848 low_direction &= ~nTRST; /* nTRST high impedance */
2851 low_direction |= nTRST;
2852 low_output |= nTRST;
2855 low_direction |= nSRST;
2856 low_output |= nSRST;
2858 /* initialize low byte for jtag */
2859 buf[0] = 0x80; /* command "set data bits low byte" */
2860 buf[1] = low_output;
2861 buf[2] = low_direction;
2862 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2864 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2865 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2866 return ERROR_JTAG_INIT_FAILED;
2870 high_direction = 0x00;
2873 /* initialize high port */
2874 buf[0] = 0x82; /* command "set data bits high byte" */
2875 buf[1] = high_output; /* value */
2876 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2877 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2879 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2880 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2881 return ERROR_JTAG_INIT_FAILED;
2887 static void icebear_jtag_reset(int trst, int srst) {
2890 low_direction |= nTRST;
2891 low_output &= ~nTRST;
2893 else if (trst == 0) {
2894 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
2895 low_direction &= ~nTRST;
2897 low_output |= nTRST;
2901 low_output &= ~nSRST;
2903 else if (srst == 0) {
2904 low_output |= nSRST;
2907 /* command "set data bits low byte" */
2908 buffer_write( 0x80 );
2909 buffer_write( low_output );
2910 buffer_write( low_direction );
2912 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);