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 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1635 #ifdef _DEBUG_JTAG_IO_
1636 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1641 static int ft2232_execute_sleep(jtag_command_t *cmd)
1646 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1648 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1649 retval = ERROR_JTAG_QUEUE_FAILED;
1650 first_unsent = cmd->next;
1651 jtag_sleep(cmd->cmd.sleep->us);
1652 #ifdef _DEBUG_JTAG_IO_
1653 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1659 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1664 /* this is only allowed while in a stable state. A check for a stable
1665 * state was done in jtag_add_clocks()
1667 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1668 retval = ERROR_JTAG_QUEUE_FAILED;
1669 #ifdef _DEBUG_JTAG_IO_
1670 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1676 static int ft2232_execute_command(jtag_command_t *cmd)
1683 case JTAG_END_STATE: retval = ft2232_execute_end_state(cmd); break;
1684 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1685 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1686 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1687 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1688 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1689 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1690 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1692 LOG_ERROR("BUG: unknown JTAG command type encountered");
1698 static int ft2232_execute_queue()
1700 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1703 first_unsent = cmd; /* next command that has to be sent */
1706 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1707 * that wasn't handled by a caller-provided error handler
1711 ft2232_buffer_size = 0;
1712 ft2232_expect_read = 0;
1714 /* blink, if the current layout has that feature */
1720 if (ft2232_execute_command(cmd) != ERROR_OK)
1721 retval = ERROR_JTAG_QUEUE_FAILED;
1722 /* Start reading input before FT2232 TX buffer fills up */
1724 if (ft2232_expect_read > 256)
1726 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1727 retval = ERROR_JTAG_QUEUE_FAILED;
1732 if (require_send > 0)
1733 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1734 retval = ERROR_JTAG_QUEUE_FAILED;
1740 #if BUILD_FT2232_FTD2XX == 1
1741 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1744 DWORD openex_flags = 0;
1745 char* openex_string = NULL;
1748 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1751 /* Add non-standard Vid/Pid to the linux driver */
1752 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1754 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1758 if (ft2232_device_desc && ft2232_serial)
1760 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1761 ft2232_device_desc = NULL;
1764 if (ft2232_device_desc)
1766 openex_string = ft2232_device_desc;
1767 openex_flags = FT_OPEN_BY_DESCRIPTION;
1769 else if (ft2232_serial)
1771 openex_string = ft2232_serial;
1772 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1776 LOG_ERROR("neither device description nor serial number specified");
1777 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1779 return ERROR_JTAG_INIT_FAILED;
1782 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1783 if( status != FT_OK ){
1784 // under Win32, the FTD2XX driver appends an "A" to the end
1785 // of the description, if we tried by the desc, then
1786 // try by the alternate "A" description.
1787 if( openex_string == ft2232_device_desc ){
1788 // Try the alternate method.
1789 openex_string = ft2232_device_desc_A;
1790 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1791 if( status == FT_OK ){
1792 // yea, the "alternate" method worked!
1794 // drat, give the user a meaningfull message.
1795 // telling the use we tried *BOTH* methods.
1796 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1798 ft2232_device_desc_A );
1803 if ( status != FT_OK )
1809 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1811 return ERROR_JTAG_INIT_FAILED;
1813 LOG_ERROR("unable to open ftdi device: %lu", status);
1814 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1815 if (status == FT_OK)
1817 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1820 for (i = 0; i < num_devices; i++)
1821 desc_array[i] = malloc(64);
1823 desc_array[num_devices] = NULL;
1825 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1827 if (status == FT_OK)
1829 LOG_ERROR("ListDevices: %lu\n", num_devices);
1830 for (i = 0; i < num_devices; i++)
1831 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1834 for (i = 0; i < num_devices; i++)
1835 free(desc_array[i]);
1841 LOG_ERROR("ListDevices: NONE\n");
1843 return ERROR_JTAG_INIT_FAILED;
1846 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1848 LOG_ERROR("unable to set latency timer: %lu", status);
1849 return ERROR_JTAG_INIT_FAILED;
1852 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1854 LOG_ERROR("unable to get latency timer: %lu", status);
1855 return ERROR_JTAG_INIT_FAILED;
1859 LOG_DEBUG("current latency timer: %i", latency_timer);
1862 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1864 LOG_ERROR("unable to set timeouts: %lu", status);
1865 return ERROR_JTAG_INIT_FAILED;
1868 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1870 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1871 return ERROR_JTAG_INIT_FAILED;
1878 static int ft2232_purge_ftd2xx(void)
1882 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1884 LOG_ERROR("error purging ftd2xx device: %lu", status);
1885 return ERROR_JTAG_INIT_FAILED;
1892 #endif /* BUILD_FT2232_FTD2XX == 1 */
1894 #if BUILD_FT2232_LIBFTDI == 1
1895 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1899 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1900 ft2232_layout, vid, pid);
1902 if (ftdi_init(&ftdic) < 0)
1903 return ERROR_JTAG_INIT_FAILED;
1905 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1907 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1908 return ERROR_JTAG_INIT_FAILED;
1911 /* context, vendor id, product id */
1912 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1916 LOG_WARNING("unable to open ftdi device (trying more): %s",
1919 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1921 return ERROR_JTAG_INIT_FAILED;
1924 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
1925 if (ftdi_usb_reset(&ftdic) < 0)
1927 LOG_ERROR("unable to reset ftdi device");
1928 return ERROR_JTAG_INIT_FAILED;
1931 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1933 LOG_ERROR("unable to set latency timer");
1934 return ERROR_JTAG_INIT_FAILED;
1937 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1939 LOG_ERROR("unable to get latency timer");
1940 return ERROR_JTAG_INIT_FAILED;
1944 LOG_DEBUG("current latency timer: %i", latency_timer);
1947 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1953 static int ft2232_purge_libftdi(void)
1955 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1957 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1958 return ERROR_JTAG_INIT_FAILED;
1965 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1967 static int ft2232_init(void)
1972 ft2232_layout_t* cur_layout = ft2232_layouts;
1975 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE)==7)
1977 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
1981 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
1984 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1986 ft2232_layout = "usbjtag";
1987 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1990 while (cur_layout->name)
1992 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1994 layout = cur_layout;
2002 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2003 return ERROR_JTAG_INIT_FAILED;
2009 * "more indicates that there are more IDs to try, so we should
2010 * not print an error for an ID mismatch (but for anything
2013 * try_more indicates that the error code returned indicates an
2014 * ID mismatch (and nothing else) and that we should proceeed
2015 * with the next ID pair.
2017 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2020 #if BUILD_FT2232_FTD2XX == 1
2021 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2023 #elif BUILD_FT2232_LIBFTDI == 1
2024 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2029 if (!more || !try_more)
2033 ft2232_buffer_size = 0;
2034 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2036 if (layout->init() != ERROR_OK)
2037 return ERROR_JTAG_INIT_FAILED;
2039 ft2232_speed(jtag_speed);
2041 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2042 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
2044 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2045 return ERROR_JTAG_INIT_FAILED;
2048 #if BUILD_FT2232_FTD2XX == 1
2049 return ft2232_purge_ftd2xx();
2050 #elif BUILD_FT2232_LIBFTDI == 1
2051 return ft2232_purge_libftdi();
2058 static int usbjtag_init(void)
2064 low_direction = 0x0b;
2066 if (strcmp(ft2232_layout, "usbjtag") == 0)
2073 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2080 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2087 low_direction = 0x8b;
2091 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2092 return ERROR_JTAG_INIT_FAILED;
2095 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2097 low_direction &= ~nTRSTnOE; /* nTRST input */
2098 low_output &= ~nTRST; /* nTRST = 0 */
2102 low_direction |= nTRSTnOE; /* nTRST output */
2103 low_output |= nTRST; /* nTRST = 1 */
2106 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2108 low_direction |= nSRSTnOE; /* nSRST output */
2109 low_output |= nSRST; /* nSRST = 1 */
2113 low_direction &= ~nSRSTnOE; /* nSRST input */
2114 low_output &= ~nSRST; /* nSRST = 0 */
2117 /* initialize low byte for jtag */
2118 buf[0] = 0x80; /* command "set data bits low byte" */
2119 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
2120 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2121 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2123 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2125 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2126 return ERROR_JTAG_INIT_FAILED;
2133 static int axm0432_jtag_init(void)
2139 low_direction = 0x2b;
2141 /* initialize low byte for jtag */
2142 buf[0] = 0x80; /* command "set data bits low byte" */
2143 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2144 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2145 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2147 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2149 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2150 return ERROR_JTAG_INIT_FAILED;
2153 if (strcmp(layout->name, "axm0432_jtag") == 0)
2156 nTRSTnOE = 0x0; /* No output enable for TRST*/
2158 nSRSTnOE = 0x0; /* No output enable for SRST*/
2162 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2167 high_direction = 0x0c;
2169 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2171 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2175 high_output |= nTRST;
2178 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2180 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2184 high_output |= nSRST;
2187 /* initialize high port */
2188 buf[0] = 0x82; /* command "set data bits high byte" */
2189 buf[1] = high_output; /* value */
2190 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2191 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2193 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2195 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2196 return ERROR_JTAG_INIT_FAILED;
2203 static int jtagkey_init(void)
2209 low_direction = 0x1b;
2211 /* initialize low byte for jtag */
2212 buf[0] = 0x80; /* command "set data bits low byte" */
2213 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2214 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2215 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2217 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2219 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2220 return ERROR_JTAG_INIT_FAILED;
2223 if (strcmp(layout->name, "jtagkey") == 0)
2230 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2231 || (strcmp(layout->name, "oocdlink") == 0) )
2240 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2245 high_direction = 0x0f;
2247 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2249 high_output |= nTRSTnOE;
2250 high_output &= ~nTRST;
2254 high_output &= ~nTRSTnOE;
2255 high_output |= nTRST;
2258 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2260 high_output &= ~nSRSTnOE;
2261 high_output |= nSRST;
2265 high_output |= nSRSTnOE;
2266 high_output &= ~nSRST;
2269 /* initialize high port */
2270 buf[0] = 0x82; /* command "set data bits high byte" */
2271 buf[1] = high_output; /* value */
2272 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2273 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2275 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2277 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2278 return ERROR_JTAG_INIT_FAILED;
2285 static int olimex_jtag_init(void)
2291 low_direction = 0x1b;
2293 /* initialize low byte for jtag */
2294 buf[0] = 0x80; /* command "set data bits low byte" */
2295 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2296 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2297 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2299 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2301 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2302 return ERROR_JTAG_INIT_FAILED;
2308 nSRSTnOE = 0x00; /* no output enable for nSRST */
2311 high_direction = 0x0f;
2313 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2315 high_output |= nTRSTnOE;
2316 high_output &= ~nTRST;
2320 high_output &= ~nTRSTnOE;
2321 high_output |= nTRST;
2324 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2326 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2330 high_output &= ~nSRST;
2333 /* turn red LED on */
2334 high_output |= 0x08;
2336 /* initialize high port */
2337 buf[0] = 0x82; /* command "set data bits high byte" */
2338 buf[1] = high_output; /* value */
2339 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2340 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2342 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2344 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2345 return ERROR_JTAG_INIT_FAILED;
2352 static int flyswatter_init(void)
2358 low_direction = 0xfb;
2360 /* initialize low byte for jtag */
2361 buf[0] = 0x80; /* command "set data bits low byte" */
2362 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2363 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2364 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2366 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2368 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2369 return ERROR_JTAG_INIT_FAILED;
2373 nTRSTnOE = 0x0; /* not output enable for nTRST */
2375 nSRSTnOE = 0x00; /* no output enable for nSRST */
2378 high_direction = 0x0c;
2380 /* turn red LED3 on, LED2 off */
2381 high_output |= 0x08;
2383 /* initialize high port */
2384 buf[0] = 0x82; /* command "set data bits high byte" */
2385 buf[1] = high_output; /* value */
2386 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2387 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2389 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2391 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2392 return ERROR_JTAG_INIT_FAILED;
2399 static int turtle_init(void)
2405 low_direction = 0x5b;
2407 /* initialize low byte for jtag */
2408 buf[0] = 0x80; /* command "set data bits low byte" */
2409 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2410 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2411 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2413 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2415 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2416 return ERROR_JTAG_INIT_FAILED;
2422 high_direction = 0x0C;
2424 /* initialize high port */
2425 buf[0] = 0x82; /* command "set data bits high byte" */
2426 buf[1] = high_output;
2427 buf[2] = high_direction;
2428 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2430 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2432 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2433 return ERROR_JTAG_INIT_FAILED;
2440 static int comstick_init(void)
2446 low_direction = 0x0b;
2448 /* initialize low byte for jtag */
2449 buf[0] = 0x80; /* command "set data bits low byte" */
2450 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2451 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2452 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2454 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2456 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2457 return ERROR_JTAG_INIT_FAILED;
2461 nTRSTnOE = 0x00; /* no output enable for nTRST */
2463 nSRSTnOE = 0x00; /* no output enable for nSRST */
2466 high_direction = 0x03;
2468 /* initialize high port */
2469 buf[0] = 0x82; /* command "set data bits high byte" */
2470 buf[1] = high_output;
2471 buf[2] = high_direction;
2472 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2474 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2476 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2477 return ERROR_JTAG_INIT_FAILED;
2484 static int stm32stick_init(void)
2490 low_direction = 0x8b;
2492 /* initialize low byte for jtag */
2493 buf[0] = 0x80; /* command "set data bits low byte" */
2494 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2495 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2496 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2498 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2500 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2501 return ERROR_JTAG_INIT_FAILED;
2505 nTRSTnOE = 0x00; /* no output enable for nTRST */
2507 nSRSTnOE = 0x00; /* no output enable for nSRST */
2510 high_direction = 0x03;
2512 /* initialize high port */
2513 buf[0] = 0x82; /* command "set data bits high byte" */
2514 buf[1] = high_output;
2515 buf[2] = high_direction;
2516 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2518 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2520 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2521 return ERROR_JTAG_INIT_FAILED;
2528 static int sheevaplug_init(void)
2534 low_direction = 0x1b;
2536 /* initialize low byte for jtag */
2537 buf[0] = 0x80; /* command "set data bits low byte" */
2538 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2539 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2540 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2542 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2544 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2545 return ERROR_JTAG_INIT_FAILED;
2554 high_direction = 0x0f;
2556 /* nTRST is always push-pull */
2557 high_output &= ~nTRSTnOE;
2558 high_output |= nTRST;
2560 /* nSRST is always open-drain */
2561 high_output |= nSRSTnOE;
2562 high_output &= ~nSRST;
2564 /* initialize high port */
2565 buf[0] = 0x82; /* command "set data bits high byte" */
2566 buf[1] = high_output; /* value */
2567 buf[2] = high_direction; /* all outputs - xRST */
2568 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2570 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2572 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2573 return ERROR_JTAG_INIT_FAILED;
2579 static void olimex_jtag_blink(void)
2581 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2582 * ACBUS3 is bit 3 of the GPIOH port
2584 if (high_output & 0x08)
2586 /* set port pin high */
2587 high_output &= 0x07;
2591 /* set port pin low */
2592 high_output |= 0x08;
2595 buffer_write( 0x82 );
2596 buffer_write( high_output );
2597 buffer_write( high_direction );
2601 static void flyswatter_jtag_blink(void)
2604 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2606 high_output ^= 0x0c;
2608 buffer_write( 0x82 );
2609 buffer_write( high_output );
2610 buffer_write( high_direction );
2614 static void turtle_jtag_blink(void)
2617 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2619 if (high_output & 0x08)
2628 buffer_write( 0x82 );
2629 buffer_write( high_output );
2630 buffer_write( high_direction );
2634 static int ft2232_quit(void)
2636 #if BUILD_FT2232_FTD2XX == 1
2639 status = FT_Close(ftdih);
2640 #elif BUILD_FT2232_LIBFTDI == 1
2641 ftdi_usb_close(&ftdic);
2643 ftdi_deinit(&ftdic);
2646 free(ft2232_buffer);
2647 ft2232_buffer = NULL;
2653 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2659 ft2232_device_desc = strdup(args[0]);
2660 cp = strchr( ft2232_device_desc, 0 );
2661 // under Win32, the FTD2XX driver appends an "A" to the end
2662 // of the description, this examines the given desc
2663 // and creates the 'missing' _A or non_A variable.
2664 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2665 // it was, so make this the "A" version.
2666 ft2232_device_desc_A = ft2232_device_desc;
2667 // and *CREATE* the non-A version.
2668 strcpy( buf, ft2232_device_desc );
2669 cp = strchr( buf, 0 );
2671 ft2232_device_desc = strdup( buf );
2673 // <space>A not defined
2675 sprintf( buf, "%s A", ft2232_device_desc );
2676 ft2232_device_desc_A = strdup( buf );
2681 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2688 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2692 ft2232_serial = strdup(args[0]);
2696 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2703 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2708 ft2232_layout = malloc(strlen(args[0]) + 1);
2709 strcpy(ft2232_layout, args[0]);
2715 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2719 if (argc > MAX_USB_IDS * 2)
2721 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2722 "(maximum is %d pairs)", MAX_USB_IDS);
2723 argc = MAX_USB_IDS * 2;
2725 if ( argc < 2 || (argc & 1) )
2727 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2732 for (i = 0; i + 1 < argc; i += 2)
2734 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2735 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2739 * Explicitly terminate, in case there are multiples instances of
2742 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2748 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2752 ft2232_latency = atoi(args[0]);
2756 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2763 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2767 /* 7 bits of either ones or zeros. */
2768 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2770 while (num_cycles > 0)
2772 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2773 * at most 7 bits per invocation. Here we invoke it potentially
2776 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2778 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2780 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2781 retval = ERROR_JTAG_QUEUE_FAILED;
2786 /* there are no state transitions in this code, so omit state tracking */
2788 /* command "Clock Data to TMS/CS Pin (no Read)" */
2789 buffer_write( 0x4b );
2792 buffer_write( bitcount_per_command - 1 );
2794 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2795 buffer_write( tms );
2799 num_cycles -= bitcount_per_command;
2806 /* ---------------------------------------------------------------------
2807 * Support for IceBear JTAG adapter from Section5:
2808 * http://section5.ch/icebear
2810 * Author: Sten, debian@sansys-electronic.com
2813 /* Icebear pin layout
2815 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2816 * GND GND | 4 3| n.c.
2817 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2818 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2819 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2820 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2821 * ADBUS2 TDO |14 13| GND GND
2823 * ADBUS0 O L TCK ACBUS0 GND
2824 * ADBUS1 O L TDI ACBUS1 GND
2825 * ADBUS2 I TDO ACBUS2 n.c.
2826 * ADBUS3 O H TMS ACBUS3 n.c.
2832 static int icebear_jtag_init(void) {
2836 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2837 low_output = 0x08; /* high: TMS; low: TCK TDI */
2841 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2842 low_direction &= ~nTRST; /* nTRST high impedance */
2845 low_direction |= nTRST;
2846 low_output |= nTRST;
2849 low_direction |= nSRST;
2850 low_output |= nSRST;
2852 /* initialize low byte for jtag */
2853 buf[0] = 0x80; /* command "set data bits low byte" */
2854 buf[1] = low_output;
2855 buf[2] = low_direction;
2856 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2858 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2859 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2860 return ERROR_JTAG_INIT_FAILED;
2864 high_direction = 0x00;
2867 /* initialize high port */
2868 buf[0] = 0x82; /* command "set data bits high byte" */
2869 buf[1] = high_output; /* value */
2870 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2871 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2873 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2874 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2875 return ERROR_JTAG_INIT_FAILED;
2881 static void icebear_jtag_reset(int trst, int srst) {
2884 low_direction |= nTRST;
2885 low_output &= ~nTRST;
2887 else if (trst == 0) {
2888 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
2889 low_direction &= ~nTRST;
2891 low_output |= nTRST;
2895 low_output &= ~nSRST;
2897 else if (srst == 0) {
2898 low_output |= nSRST;
2901 /* command "set data bits low byte" */
2902 buffer_write( 0x80 );
2903 buffer_write( low_output );
2904 buffer_write( low_direction );
2906 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);