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 */
43 #define INCLUDE_JTAG_INTERFACE_H
45 #include "time_support.h"
53 #if (BUILD_FT2232_FTD2XX==1 && BUILD_FT2232_LIBFTDI==1)
54 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
55 #elif(BUILD_FT2232_FTD2XX!=1 && BUILD_FT2232_LIBFTDI!=1)
56 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
59 /* FT2232 access library includes */
60 #if BUILD_FT2232_FTD2XX == 1
62 #elif BUILD_FT2232_LIBFTDI == 1
66 /* max TCK for the high speed devices 30000 kHz */
67 #define FTDI_2232H_4232H_MAX_TCK 30000
69 static int ft2232_execute_queue(void);
71 static int ft2232_speed(int speed);
72 static int ft2232_speed_div(int speed, int* khz);
73 static int ft2232_khz(int khz, int* jtag_speed);
74 static int ft2232_register_commands(struct command_context_s* cmd_ctx);
75 static int ft2232_init(void);
76 static int ft2232_quit(void);
78 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
79 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
80 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
81 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
82 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
86 * Function ft2232_stableclocks
87 * will send out \a num_cycles on the TCK line while the TAP(s)
88 * are in a stable state. Calling code must ensure that current state is
89 * stable, that verification is not done in here.
90 * @param num_cycles is the count of clocks cycles to send.
91 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
93 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
96 static char * ft2232_device_desc_A = NULL;
97 static char* ft2232_device_desc = NULL;
98 static char* ft2232_serial = NULL;
99 static char* ft2232_layout = NULL;
100 static u8 ft2232_latency = 2;
101 static unsigned ft2232_max_tck = 6000;
104 #define MAX_USB_IDS 8
105 /* vid = pid = 0 marks the end of the list */
106 static u16 ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
107 static u16 ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
109 typedef struct ft2232_layout_s
113 void (*reset)(int trst, int srst);
117 /* init procedures for supported layouts */
118 static int usbjtag_init(void);
119 static int jtagkey_init(void);
120 static int olimex_jtag_init(void);
121 static int flyswatter_init(void);
122 static int turtle_init(void);
123 static int comstick_init(void);
124 static int stm32stick_init(void);
125 static int axm0432_jtag_init(void);
126 static int sheevaplug_init(void);
127 static int icebear_jtag_init(void);
128 static int cortino_jtag_init(void);
130 /* reset procedures for supported layouts */
131 static void usbjtag_reset(int trst, int srst);
132 static void jtagkey_reset(int trst, int srst);
133 static void olimex_jtag_reset(int trst, int srst);
134 static void flyswatter_reset(int trst, int srst);
135 static void turtle_reset(int trst, int srst);
136 static void comstick_reset(int trst, int srst);
137 static void stm32stick_reset(int trst, int srst);
138 static void axm0432_jtag_reset(int trst, int srst);
139 static void sheevaplug_reset(int trst, int srst);
140 static void icebear_jtag_reset(int trst, int srst);
142 /* blink procedures for layouts that support a blinking led */
143 static void olimex_jtag_blink(void);
144 static void flyswatter_jtag_blink(void);
145 static void turtle_jtag_blink(void);
147 ft2232_layout_t ft2232_layouts[] =
149 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
150 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
151 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
152 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
153 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
154 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
155 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
156 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
157 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
158 { "comstick", comstick_init, comstick_reset, NULL },
159 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
160 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
161 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
162 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
163 { "cortino", cortino_jtag_init, comstick_reset, NULL },
164 { NULL, NULL, NULL, NULL },
167 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
169 static ft2232_layout_t* layout;
170 static u8 low_output = 0x0;
171 static u8 low_direction = 0x0;
172 static u8 high_output = 0x0;
173 static u8 high_direction = 0x0;
175 #if BUILD_FT2232_FTD2XX == 1
176 static FT_HANDLE ftdih = NULL;
177 #elif BUILD_FT2232_LIBFTDI == 1
178 static struct ftdi_context ftdic;
182 static jtag_command_t* first_unsent; /* next command that has to be sent */
183 static int require_send;
186 /* http://urjtag.wiki.sourceforge.net/Cable+FT2232 says:
188 "There is a significant difference between libftdi and libftd2xx. The latter
189 one allows to schedule up to 64*64 bytes of result data while libftdi fails
190 with more than 4*64. As a consequence, the FT2232 driver is forced to
191 perform around 16x more USB transactions for long command streams with TDO
192 capture when running with libftdi."
195 #define FT2232_BUFFER_SIZE 131072
196 a comment would have been nice.
199 #define FT2232_BUFFER_SIZE 131072
201 static u8* ft2232_buffer = NULL;
202 static int ft2232_buffer_size = 0;
203 static int ft2232_read_pointer = 0;
204 static int ft2232_expect_read = 0;
207 * Function buffer_write
208 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
209 * @param val is the byte to send.
211 static inline void buffer_write( u8 val )
213 assert( ft2232_buffer );
214 assert( (unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE );
215 ft2232_buffer[ft2232_buffer_size++] = val;
219 * Function buffer_read
220 * returns a byte from the byte buffer.
222 static inline u8 buffer_read(void)
224 assert( ft2232_buffer );
225 assert( ft2232_read_pointer < ft2232_buffer_size );
226 return ft2232_buffer[ft2232_read_pointer++];
232 * clocks out \a bit_count bits on the TMS line, starting with the least
233 * significant bit of tms_bits and progressing to more significant bits.
234 * Rigorous state transition logging is done here via tap_set_state().
236 * @param pmsse_cmd is one of the MPSSE TMS oriented commands such as 0x4b or 0x6b. See
237 * the MPSSE spec referenced above for their functionality. The MPSSE command
238 * "Clock Data to TMS/CS Pin (no Read)" is often used for this, 0x4b.
240 * @param tms_bits holds the sequence of bits to send.
241 * @param tms_count tells how many bits in the sequence.
242 * @param tdi_bit is a single bit which is passed on to TDI before the first TCK cycle
243 * and is held static for the duration of TMS clocking. See the MPSSE spec referenced above.
245 static void clock_tms( u8 mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit )
249 int tms_ndx; /* bit index into tms_byte */
251 assert( tms_count > 0 );
253 // LOG_DEBUG("mpsse cmd=%02x, tms_bits=0x%08x, bit_count=%d", mpsse_cmd, tms_bits, tms_count );
255 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
257 bool bit = tms_bits & 1;
260 tms_byte |= (1<<tms_ndx);
262 /* always do state transitions in public view */
263 tap_set_state( tap_state_transition(tap_get_state(), bit) );
265 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
270 if( tms_ndx==7 || i==tms_count-1 )
272 buffer_write( mpsse_cmd );
273 buffer_write( tms_ndx - 1 );
275 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
276 TMS/CS and is held static for the duration of TMS/CS clocking.
278 buffer_write( tms_byte | (tdi_bit << 7) );
285 * Function get_tms_buffer_requirements
286 * returns what clock_tms() will consume if called with
289 static inline int get_tms_buffer_requirements( int bit_count )
291 return ((bit_count + 6)/7) * 3;
296 * Function move_to_state
297 * moves the TAP controller from the current state to a
298 * \a goal_state through a path given by tap_get_tms_path(). State transition
299 * logging is performed by delegation to clock_tms().
301 * @param goal_state is the destination state for the move.
303 static void move_to_state( tap_state_t goal_state )
305 tap_state_t start_state = tap_get_state();
307 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
308 lookup of the required TMS pattern to move to this state from the
312 /* do the 2 lookups */
313 int tms_bits = tap_get_tms_path(start_state, goal_state);
314 int tms_count = tap_get_tms_path_len(start_state, goal_state);
316 DEBUG_JTAG_IO( "start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state) );
318 clock_tms( 0x4b, tms_bits, tms_count, 0 );
322 jtag_interface_t ft2232_interface =
325 .execute_queue = ft2232_execute_queue,
326 .speed = ft2232_speed,
327 .speed_div = ft2232_speed_div,
329 .register_commands = ft2232_register_commands,
334 static int ft2232_write(u8* buf, int size, u32* bytes_written)
336 #if BUILD_FT2232_FTD2XX == 1
338 DWORD dw_bytes_written;
339 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
341 *bytes_written = dw_bytes_written;
342 LOG_ERROR("FT_Write returned: %lu", status);
343 return ERROR_JTAG_DEVICE_ERROR;
347 *bytes_written = dw_bytes_written;
350 #elif BUILD_FT2232_LIBFTDI == 1
352 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
355 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
356 return ERROR_JTAG_DEVICE_ERROR;
360 *bytes_written = retval;
367 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
369 #if BUILD_FT2232_FTD2XX == 1
375 while ( (*bytes_read < size) && timeout-- )
377 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
378 *bytes_read, &dw_bytes_read) ) != FT_OK )
381 LOG_ERROR("FT_Read returned: %lu", status);
382 return ERROR_JTAG_DEVICE_ERROR;
384 *bytes_read += dw_bytes_read;
387 #elif BUILD_FT2232_LIBFTDI == 1
392 while ( (*bytes_read < size) && timeout-- )
394 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
397 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
398 return ERROR_JTAG_DEVICE_ERROR;
400 *bytes_read += retval;
405 if (*bytes_read < size)
407 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
408 return ERROR_JTAG_DEVICE_ERROR;
415 static int ft2232_speed(int speed)
421 buf[0] = 0x86; /* command "set divisor" */
422 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
423 buf[2] = (speed >> 8) & 0xff; /* valueH */
425 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
426 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
428 LOG_ERROR("couldn't set FT2232 TCK speed");
436 static int ft2232_speed_div(int speed, int* khz)
438 /* Take a look in the FT2232 manual,
439 * AN2232C-01 Command Processor for
440 * MPSSE and MCU Host Bus. Chapter 3.8 */
442 *khz = ft2232_max_tck / (1 + speed);
448 static int ft2232_khz(int khz, int* jtag_speed)
452 LOG_DEBUG("RTCK not supported");
456 /* Take a look in the FT2232 manual,
457 * AN2232C-01 Command Processor for
458 * MPSSE and MCU Host Bus. Chapter 3.8
460 * We will calc here with a multiplier
461 * of 10 for better rounding later. */
463 /* Calc speed, (ft2232_max_tck / khz) - 1 */
464 /* Use 65000 for better rounding */
465 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
467 /* Add 0.9 for rounding */
470 /* Calc real speed */
471 *jtag_speed = *jtag_speed / 10;
473 /* Check if speed is greater than 0 */
479 /* Check max value */
480 if (*jtag_speed > 0xFFFF)
482 *jtag_speed = 0xFFFF;
489 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
491 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
492 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
493 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
494 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
495 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
496 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
497 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
498 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
499 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
500 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
505 void ft2232_end_state(tap_state_t state)
507 if (tap_is_state_stable(state))
508 tap_set_end_state(state);
511 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
516 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
518 int num_bytes = (scan_size + 7) / 8;
519 int bits_left = scan_size;
522 while (num_bytes-- > 1)
524 buffer[cur_byte++] = buffer_read();
528 buffer[cur_byte] = 0x0;
530 /* There is one more partial byte left from the clock data in/out instructions */
533 buffer[cur_byte] = buffer_read() >> 1;
535 /* 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 */
536 buffer[cur_byte] = ( buffer[cur_byte] | ( ( (buffer_read()) << 1 ) & 0x80 )) >> (8 - bits_left);
540 static void ft2232_debug_dump_buffer(void)
546 for (i = 0; i < ft2232_buffer_size; i++)
548 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
551 LOG_DEBUG("%s", line);
557 LOG_DEBUG("%s", line);
561 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
571 #ifdef _DEBUG_USB_IO_
572 struct timeval start, inter, inter2, end;
573 struct timeval d_inter, d_inter2, d_end;
576 #ifdef _DEBUG_USB_COMMS_
577 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
578 ft2232_debug_dump_buffer();
581 #ifdef _DEBUG_USB_IO_
582 gettimeofday(&start, NULL);
585 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
587 LOG_ERROR("couldn't write MPSSE commands to FT2232");
591 #ifdef _DEBUG_USB_IO_
592 gettimeofday(&inter, NULL);
595 if (ft2232_expect_read)
598 ft2232_buffer_size = 0;
600 #ifdef _DEBUG_USB_IO_
601 gettimeofday(&inter2, NULL);
604 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
606 LOG_ERROR("couldn't read from FT2232");
610 #ifdef _DEBUG_USB_IO_
611 gettimeofday(&end, NULL);
613 timeval_subtract(&d_inter, &inter, &start);
614 timeval_subtract(&d_inter2, &inter2, &start);
615 timeval_subtract(&d_end, &end, &start);
617 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
618 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
619 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
620 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
623 ft2232_buffer_size = bytes_read;
625 if (ft2232_expect_read != ft2232_buffer_size)
627 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
630 ft2232_debug_dump_buffer();
635 #ifdef _DEBUG_USB_COMMS_
636 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
637 ft2232_debug_dump_buffer();
641 ft2232_expect_read = 0;
642 ft2232_read_pointer = 0;
644 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
645 * that wasn't handled by a caller-provided error handler
655 type = jtag_scan_type(cmd->cmd.scan);
656 if (type != SCAN_OUT)
658 scan_size = jtag_scan_size(cmd->cmd.scan);
659 buffer = calloc(CEIL(scan_size, 8), 1);
660 ft2232_read_scan(type, buffer, scan_size);
661 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
662 retval = ERROR_JTAG_QUEUE_FAILED;
674 ft2232_buffer_size = 0;
681 * Function ft2232_add_pathmove
682 * moves the TAP controller from the current state to a new state through the
683 * given path, where path is an array of tap_state_t's.
685 * @param path is an array of tap_stat_t which gives the states to traverse through
686 * ending with the last state at path[num_states-1]
687 * @param num_states is the count of state steps to move through
689 static void ft2232_add_pathmove( tap_state_t* path, int num_states )
693 tap_state_t walker = tap_get_state();
695 assert( (unsigned) num_states <= 32u ); /* tms_bits only holds 32 bits */
697 /* this loop verifies that the path is legal and logs each state in the path */
698 for( state_ndx = 0; state_ndx < num_states; ++state_ndx )
700 tap_state_t desired_next_state = path[state_ndx];
702 if (tap_state_transition(walker, false) == desired_next_state )
703 ; /* bit within tms_bits at index state_ndx is already zero */
704 else if (tap_state_transition(walker, true) == desired_next_state )
705 tms_bits |= (1<<state_ndx);
708 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition",
709 tap_state_name(walker), tap_state_name(desired_next_state) );
713 walker = desired_next_state;
716 clock_tms( 0x4b, tms_bits, num_states, 0 );
718 tap_set_end_state(tap_get_state());
722 void ft2232_add_scan(bool ir_scan, enum scan_type type, u8* buffer, int scan_size)
724 int num_bytes = (scan_size + 7) / 8;
725 int bits_left = scan_size;
731 if (tap_get_state() != TAP_DRSHIFT)
733 move_to_state( TAP_DRSHIFT );
738 if (tap_get_state() != TAP_IRSHIFT)
740 move_to_state( TAP_IRSHIFT );
744 /* add command for complete bytes */
745 while (num_bytes > 1)
750 /* Clock Data Bytes In and Out LSB First */
751 buffer_write( 0x39 );
752 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
754 else if (type == SCAN_OUT)
756 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
757 buffer_write( 0x19 );
758 /* LOG_DEBUG("added TDI bytes (o)"); */
760 else if (type == SCAN_IN)
762 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
763 buffer_write( 0x28 );
764 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
767 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
768 num_bytes -= thisrun_bytes;
770 buffer_write( (u8) (thisrun_bytes - 1) );
771 buffer_write( (u8) ((thisrun_bytes - 1) >> 8) );
775 /* add complete bytes */
776 while (thisrun_bytes-- > 0)
778 buffer_write( buffer[cur_byte++] );
782 else /* (type == SCAN_IN) */
784 bits_left -= 8 * (thisrun_bytes);
788 /* the most signifcant bit is scanned during TAP movement */
790 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
794 /* process remaining bits but the last one */
799 /* Clock Data Bits In and Out LSB First */
800 buffer_write( 0x3b );
801 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
803 else if (type == SCAN_OUT)
805 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
806 buffer_write( 0x1b );
807 /* LOG_DEBUG("added TDI bits (o)"); */
809 else if (type == SCAN_IN)
811 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
812 buffer_write( 0x2a );
813 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
816 buffer_write( bits_left - 2 );
818 buffer_write( buffer[cur_byte] );
821 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
822 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
826 /* Clock Data Bits In and Out LSB First */
827 buffer_write( 0x3b );
828 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
830 else if (type == SCAN_OUT)
832 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
833 buffer_write( 0x1b );
834 /* LOG_DEBUG("added TDI bits (o)"); */
836 else if (type == SCAN_IN)
838 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
839 buffer_write( 0x2a );
840 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
843 buffer_write( last_bit );
851 /* move from Shift-IR/DR to end state */
852 if (type != SCAN_OUT)
854 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
855 /* This must be coordinated with the bit shifts in ft2232_read_scan */
858 /* Clock Data to TMS/CS Pin with Read */
860 /* LOG_DEBUG("added TMS scan (read)"); */
864 tms_bits = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
865 tms_count = tap_get_tms_path_len( tap_get_state(), tap_get_end_state() );
866 /* Clock Data to TMS/CS Pin (no Read) */
868 /* LOG_DEBUG("added TMS scan (no read)"); */
871 clock_tms( mpsse_cmd, tms_bits, tms_count, last_bit );
874 if (tap_get_state() != tap_get_end_state())
876 move_to_state( tap_get_end_state() );
881 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
883 int num_bytes = (scan_size + 7) / 8;
884 int bits_left = scan_size;
887 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
888 u8* receive_pointer = receive_buffer;
892 int thisrun_read = 0;
896 LOG_ERROR("BUG: large IR scans are not supported");
900 if (tap_get_state() != TAP_DRSHIFT)
902 move_to_state( TAP_DRSHIFT );
905 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
907 LOG_ERROR("couldn't write MPSSE commands to FT2232");
910 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
911 ft2232_buffer_size = 0;
913 /* add command for complete bytes */
914 while (num_bytes > 1)
920 /* Clock Data Bytes In and Out LSB First */
921 buffer_write( 0x39 );
922 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
924 else if (type == SCAN_OUT)
926 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
927 buffer_write( 0x19 );
928 /* LOG_DEBUG("added TDI bytes (o)"); */
930 else if (type == SCAN_IN)
932 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
933 buffer_write( 0x28 );
934 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
937 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
938 thisrun_read = thisrun_bytes;
939 num_bytes -= thisrun_bytes;
940 buffer_write( (u8) (thisrun_bytes - 1) );
941 buffer_write( (u8) ( (thisrun_bytes - 1) >> 8 ));
945 /* add complete bytes */
946 while (thisrun_bytes-- > 0)
948 buffer_write( buffer[cur_byte] );
953 else /* (type == SCAN_IN) */
955 bits_left -= 8 * (thisrun_bytes);
958 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
960 LOG_ERROR("couldn't write MPSSE commands to FT2232");
963 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
964 ft2232_buffer_size = 0;
966 if (type != SCAN_OUT)
968 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
970 LOG_ERROR("couldn't read from FT2232");
973 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
974 receive_pointer += bytes_read;
980 /* the most signifcant bit is scanned during TAP movement */
982 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
986 /* process remaining bits but the last one */
991 /* Clock Data Bits In and Out LSB First */
992 buffer_write( 0x3b );
993 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
995 else if (type == SCAN_OUT)
997 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
998 buffer_write( 0x1b );
999 /* LOG_DEBUG("added TDI bits (o)"); */
1001 else if (type == SCAN_IN)
1003 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1004 buffer_write( 0x2a );
1005 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1007 buffer_write( bits_left - 2 );
1008 if (type != SCAN_IN)
1009 buffer_write( buffer[cur_byte] );
1011 if (type != SCAN_OUT)
1015 if (tap_get_end_state() == TAP_DRSHIFT)
1017 if (type == SCAN_IO)
1019 /* Clock Data Bits In and Out LSB First */
1020 buffer_write( 0x3b );
1021 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1023 else if (type == SCAN_OUT)
1025 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1026 buffer_write( 0x1b );
1027 /* LOG_DEBUG("added TDI bits (o)"); */
1029 else if (type == SCAN_IN)
1031 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1032 buffer_write( 0x2a );
1033 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1035 buffer_write( 0x0 );
1036 buffer_write( last_bit );
1040 int tms_bits = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1041 int tms_count = tap_get_tms_path_len( tap_get_state(), tap_get_end_state() );
1044 /* move from Shift-IR/DR to end state */
1045 if (type != SCAN_OUT)
1047 /* Clock Data to TMS/CS Pin with Read */
1049 /* LOG_DEBUG("added TMS scan (read)"); */
1053 /* Clock Data to TMS/CS Pin (no Read) */
1055 /* LOG_DEBUG("added TMS scan (no read)"); */
1058 clock_tms( mpsse_cmd, tms_bits, tms_count, last_bit );
1061 if (type != SCAN_OUT)
1064 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
1066 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1069 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
1070 ft2232_buffer_size = 0;
1072 if (type != SCAN_OUT)
1074 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
1076 LOG_ERROR("couldn't read from FT2232");
1079 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
1080 receive_pointer += bytes_read;
1087 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1089 int predicted_size = 3;
1090 int num_bytes = (scan_size - 1) / 8;
1092 if (tap_get_state() != TAP_DRSHIFT)
1093 predicted_size += get_tms_buffer_requirements( tap_get_tms_path_len( tap_get_state(), TAP_DRSHIFT) );
1095 if (type == SCAN_IN) /* only from device to host */
1097 /* complete bytes */
1098 predicted_size += CEIL(num_bytes, 65536) * 3;
1100 /* remaining bits - 1 (up to 7) */
1101 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
1103 else /* host to device, or bidirectional */
1105 /* complete bytes */
1106 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
1108 /* remaining bits -1 (up to 7) */
1109 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
1112 return predicted_size;
1116 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1118 int predicted_size = 0;
1120 if (type != SCAN_OUT)
1122 /* complete bytes */
1123 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
1125 /* remaining bits - 1 */
1126 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
1128 /* last bit (from TMS scan) */
1129 predicted_size += 1;
1132 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1134 return predicted_size;
1138 static void usbjtag_reset(int trst, int srst)
1142 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1143 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1145 low_output &= ~nTRST; /* switch output low */
1149 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1150 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1152 low_output |= nTRST; /* switch output high */
1157 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1158 low_output &= ~nSRST; /* switch output low */
1160 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1164 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1165 low_output |= nSRST; /* switch output high */
1167 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1170 /* command "set data bits low byte" */
1171 buffer_write( 0x80 );
1172 buffer_write( low_output );
1173 buffer_write( low_direction );
1177 static void jtagkey_reset(int trst, int srst)
1181 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1182 high_output &= ~nTRSTnOE;
1184 high_output &= ~nTRST;
1188 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1189 high_output |= nTRSTnOE;
1191 high_output |= nTRST;
1196 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1197 high_output &= ~nSRST;
1199 high_output &= ~nSRSTnOE;
1203 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1204 high_output |= nSRST;
1206 high_output |= nSRSTnOE;
1209 /* command "set data bits high byte" */
1210 buffer_write( 0x82 );
1211 buffer_write( high_output );
1212 buffer_write( high_direction );
1213 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1218 static void olimex_jtag_reset(int trst, int srst)
1222 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1223 high_output &= ~nTRSTnOE;
1225 high_output &= ~nTRST;
1229 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1230 high_output |= nTRSTnOE;
1232 high_output |= nTRST;
1237 high_output |= nSRST;
1241 high_output &= ~nSRST;
1244 /* command "set data bits high byte" */
1245 buffer_write( 0x82 );
1246 buffer_write( high_output );
1247 buffer_write( high_direction );
1248 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1253 static void axm0432_jtag_reset(int trst, int srst)
1257 tap_set_state(TAP_RESET);
1258 high_output &= ~nTRST;
1262 high_output |= nTRST;
1267 high_output &= ~nSRST;
1271 high_output |= nSRST;
1274 /* command "set data bits low byte" */
1275 buffer_write( 0x82 );
1276 buffer_write( high_output );
1277 buffer_write( high_direction );
1278 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1283 static void flyswatter_reset(int trst, int srst)
1287 low_output &= ~nTRST;
1291 low_output |= nTRST;
1296 low_output |= nSRST;
1300 low_output &= ~nSRST;
1303 /* command "set data bits low byte" */
1304 buffer_write( 0x80 );
1305 buffer_write( low_output );
1306 buffer_write( low_direction );
1307 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1311 static void turtle_reset(int trst, int srst)
1317 low_output |= nSRST;
1321 low_output &= ~nSRST;
1324 /* command "set data bits low byte" */
1325 buffer_write( 0x80 );
1326 buffer_write( low_output );
1327 buffer_write( low_direction );
1328 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1332 static void comstick_reset(int trst, int srst)
1336 high_output &= ~nTRST;
1340 high_output |= nTRST;
1345 high_output &= ~nSRST;
1349 high_output |= nSRST;
1352 /* command "set data bits high byte" */
1353 buffer_write( 0x82 );
1354 buffer_write( high_output );
1355 buffer_write( high_direction );
1356 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1361 static void stm32stick_reset(int trst, int srst)
1365 high_output &= ~nTRST;
1369 high_output |= nTRST;
1374 low_output &= ~nSRST;
1378 low_output |= nSRST;
1381 /* command "set data bits low byte" */
1382 buffer_write( 0x80 );
1383 buffer_write( low_output );
1384 buffer_write( low_direction );
1386 /* command "set data bits high byte" */
1387 buffer_write( 0x82 );
1388 buffer_write( high_output );
1389 buffer_write( high_direction );
1390 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1396 static void sheevaplug_reset(int trst, int srst)
1399 high_output &= ~nTRST;
1401 high_output |= nTRST;
1404 high_output &= ~nSRSTnOE;
1406 high_output |= nSRSTnOE;
1408 /* command "set data bits high byte" */
1409 buffer_write( 0x82 );
1410 buffer_write( high_output );
1411 buffer_write( high_direction );
1412 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1415 static int ft2232_execute_runtest(jtag_command_t *cmd)
1419 int predicted_size = 0;
1422 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1423 cmd->cmd.runtest->num_cycles,
1424 tap_state_name(cmd->cmd.runtest->end_state));
1426 /* only send the maximum buffer size that FT2232C can handle */
1428 if (tap_get_state() != TAP_IDLE)
1429 predicted_size += 3;
1430 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1431 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1432 predicted_size += 3;
1433 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1434 predicted_size += 3;
1435 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1437 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1438 retval = ERROR_JTAG_QUEUE_FAILED;
1442 if (tap_get_state() != TAP_IDLE)
1444 move_to_state( TAP_IDLE );
1447 i = cmd->cmd.runtest->num_cycles;
1450 /* there are no state transitions in this code, so omit state tracking */
1452 /* command "Clock Data to TMS/CS Pin (no Read)" */
1453 buffer_write( 0x4b );
1456 buffer_write( (i > 7) ? 6 : (i - 1) );
1459 buffer_write( 0x0 );
1460 tap_set_state(TAP_IDLE);
1462 i -= (i > 7) ? 7 : i;
1463 /* LOG_DEBUG("added TMS scan (no read)"); */
1466 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1467 ft2232_end_state(cmd->cmd.runtest->end_state);
1469 if ( tap_get_state() != tap_get_end_state() )
1471 move_to_state( tap_get_end_state() );
1475 #ifdef _DEBUG_JTAG_IO_
1476 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1483 static int ft2232_execute_statemove(jtag_command_t *cmd)
1485 int predicted_size = 0;
1486 int retval = ERROR_OK;
1488 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1490 /* only send the maximum buffer size that FT2232C can handle */
1492 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1494 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1495 retval = ERROR_JTAG_QUEUE_FAILED;
1499 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1500 ft2232_end_state(cmd->cmd.statemove->end_state);
1502 /* move to end state */
1503 if ( tap_get_state() != tap_get_end_state() )
1505 move_to_state( tap_get_end_state() );
1512 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1514 int predicted_size = 0;
1515 int retval = ERROR_OK;
1517 tap_state_t* path = cmd->cmd.pathmove->path;
1518 int num_states = cmd->cmd.pathmove->num_states;
1520 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1521 tap_state_name( tap_get_state() ),
1522 tap_state_name( path[num_states-1] )
1525 /* only send the maximum buffer size that FT2232C can handle */
1526 predicted_size = 3 * CEIL(num_states, 7);
1527 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1529 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1530 retval = ERROR_JTAG_QUEUE_FAILED;
1536 ft2232_add_pathmove( path, num_states );
1543 static int ft2232_execute_scan(jtag_command_t *cmd)
1546 int scan_size; /* size of IR or DR scan */
1547 int predicted_size = 0;
1548 int retval = ERROR_OK;
1550 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1552 DEBUG_JTAG_IO( "%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type );
1554 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1556 predicted_size = ft2232_predict_scan_out(scan_size, type);
1557 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1559 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1560 /* unsent commands before this */
1561 if (first_unsent != cmd)
1562 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1563 retval = ERROR_JTAG_QUEUE_FAILED;
1565 /* current command */
1566 if (cmd->cmd.scan->end_state != TAP_INVALID)
1567 ft2232_end_state(cmd->cmd.scan->end_state);
1568 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1570 first_unsent = cmd->next;
1575 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1577 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1580 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1581 retval = ERROR_JTAG_QUEUE_FAILED;
1585 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1586 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1587 if (cmd->cmd.scan->end_state != TAP_INVALID)
1588 ft2232_end_state(cmd->cmd.scan->end_state);
1589 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1593 #ifdef _DEBUG_JTAG_IO_
1594 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1595 tap_state_name( tap_get_end_state() ) );
1601 static int ft2232_execute_reset(jtag_command_t *cmd)
1604 int predicted_size = 0;
1607 DEBUG_JTAG_IO("reset trst: %i srst %i",
1608 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1610 /* only send the maximum buffer size that FT2232C can handle */
1612 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1614 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1615 retval = ERROR_JTAG_QUEUE_FAILED;
1620 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1623 #ifdef _DEBUG_JTAG_IO_
1624 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1629 static int ft2232_execute_sleep(jtag_command_t *cmd)
1634 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1636 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1637 retval = ERROR_JTAG_QUEUE_FAILED;
1638 first_unsent = cmd->next;
1639 jtag_sleep(cmd->cmd.sleep->us);
1640 #ifdef _DEBUG_JTAG_IO_
1641 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1647 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1652 /* this is only allowed while in a stable state. A check for a stable
1653 * state was done in jtag_add_clocks()
1655 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1656 retval = ERROR_JTAG_QUEUE_FAILED;
1657 #ifdef _DEBUG_JTAG_IO_
1658 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1664 static int ft2232_execute_command(jtag_command_t *cmd)
1671 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1672 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1673 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1674 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1675 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1676 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1677 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1679 LOG_ERROR("BUG: unknown JTAG command type encountered");
1685 static int ft2232_execute_queue()
1687 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1690 first_unsent = cmd; /* next command that has to be sent */
1693 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1694 * that wasn't handled by a caller-provided error handler
1698 ft2232_buffer_size = 0;
1699 ft2232_expect_read = 0;
1701 /* blink, if the current layout has that feature */
1707 if (ft2232_execute_command(cmd) != ERROR_OK)
1708 retval = ERROR_JTAG_QUEUE_FAILED;
1709 /* Start reading input before FT2232 TX buffer fills up */
1711 if (ft2232_expect_read > 256)
1713 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1714 retval = ERROR_JTAG_QUEUE_FAILED;
1719 if (require_send > 0)
1720 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1721 retval = ERROR_JTAG_QUEUE_FAILED;
1727 #if BUILD_FT2232_FTD2XX == 1
1728 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1731 DWORD openex_flags = 0;
1732 char* openex_string = NULL;
1735 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1738 /* Add non-standard Vid/Pid to the linux driver */
1739 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1741 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1745 if (ft2232_device_desc && ft2232_serial)
1747 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1748 ft2232_device_desc = NULL;
1751 if (ft2232_device_desc)
1753 openex_string = ft2232_device_desc;
1754 openex_flags = FT_OPEN_BY_DESCRIPTION;
1756 else if (ft2232_serial)
1758 openex_string = ft2232_serial;
1759 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1763 LOG_ERROR("neither device description nor serial number specified");
1764 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1766 return ERROR_JTAG_INIT_FAILED;
1769 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1770 if( status != FT_OK ){
1771 // under Win32, the FTD2XX driver appends an "A" to the end
1772 // of the description, if we tried by the desc, then
1773 // try by the alternate "A" description.
1774 if( openex_string == ft2232_device_desc ){
1775 // Try the alternate method.
1776 openex_string = ft2232_device_desc_A;
1777 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1778 if( status == FT_OK ){
1779 // yea, the "alternate" method worked!
1781 // drat, give the user a meaningfull message.
1782 // telling the use we tried *BOTH* methods.
1783 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1785 ft2232_device_desc_A );
1790 if ( status != FT_OK )
1796 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1798 return ERROR_JTAG_INIT_FAILED;
1800 LOG_ERROR("unable to open ftdi device: %lu", status);
1801 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1802 if (status == FT_OK)
1804 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1807 for (i = 0; i < num_devices; i++)
1808 desc_array[i] = malloc(64);
1810 desc_array[num_devices] = NULL;
1812 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1814 if (status == FT_OK)
1816 LOG_ERROR("ListDevices: %lu\n", num_devices);
1817 for (i = 0; i < num_devices; i++)
1818 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1821 for (i = 0; i < num_devices; i++)
1822 free(desc_array[i]);
1828 LOG_ERROR("ListDevices: NONE\n");
1830 return ERROR_JTAG_INIT_FAILED;
1833 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1835 LOG_ERROR("unable to set latency timer: %lu", status);
1836 return ERROR_JTAG_INIT_FAILED;
1839 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1841 LOG_ERROR("unable to get latency timer: %lu", status);
1842 return ERROR_JTAG_INIT_FAILED;
1846 LOG_DEBUG("current latency timer: %i", latency_timer);
1849 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1851 LOG_ERROR("unable to set timeouts: %lu", status);
1852 return ERROR_JTAG_INIT_FAILED;
1855 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1857 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1858 return ERROR_JTAG_INIT_FAILED;
1865 static int ft2232_purge_ftd2xx(void)
1869 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1871 LOG_ERROR("error purging ftd2xx device: %lu", status);
1872 return ERROR_JTAG_INIT_FAILED;
1879 #endif /* BUILD_FT2232_FTD2XX == 1 */
1881 #if BUILD_FT2232_LIBFTDI == 1
1882 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1886 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1887 ft2232_layout, vid, pid);
1889 if (ftdi_init(&ftdic) < 0)
1890 return ERROR_JTAG_INIT_FAILED;
1892 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1894 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1895 return ERROR_JTAG_INIT_FAILED;
1898 /* context, vendor id, product id */
1899 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1903 LOG_WARNING("unable to open ftdi device (trying more): %s",
1906 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1908 return ERROR_JTAG_INIT_FAILED;
1911 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
1912 if (ftdi_usb_reset(&ftdic) < 0)
1914 LOG_ERROR("unable to reset ftdi device");
1915 return ERROR_JTAG_INIT_FAILED;
1918 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1920 LOG_ERROR("unable to set latency timer");
1921 return ERROR_JTAG_INIT_FAILED;
1924 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1926 LOG_ERROR("unable to get latency timer");
1927 return ERROR_JTAG_INIT_FAILED;
1931 LOG_DEBUG("current latency timer: %i", latency_timer);
1934 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1940 static int ft2232_purge_libftdi(void)
1942 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1944 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1945 return ERROR_JTAG_INIT_FAILED;
1952 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1954 static int ft2232_init(void)
1959 ft2232_layout_t* cur_layout = ft2232_layouts;
1962 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE)==7)
1964 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
1968 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
1971 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1973 ft2232_layout = "usbjtag";
1974 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1977 while (cur_layout->name)
1979 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1981 layout = cur_layout;
1989 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1990 return ERROR_JTAG_INIT_FAILED;
1996 * "more indicates that there are more IDs to try, so we should
1997 * not print an error for an ID mismatch (but for anything
2000 * try_more indicates that the error code returned indicates an
2001 * ID mismatch (and nothing else) and that we should proceeed
2002 * with the next ID pair.
2004 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2007 #if BUILD_FT2232_FTD2XX == 1
2008 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2010 #elif BUILD_FT2232_LIBFTDI == 1
2011 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2016 if (!more || !try_more)
2020 ft2232_buffer_size = 0;
2021 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2023 if (layout->init() != ERROR_OK)
2024 return ERROR_JTAG_INIT_FAILED;
2026 ft2232_speed(jtag_speed);
2028 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2029 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
2031 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2032 return ERROR_JTAG_INIT_FAILED;
2035 #if BUILD_FT2232_FTD2XX == 1
2036 return ft2232_purge_ftd2xx();
2037 #elif BUILD_FT2232_LIBFTDI == 1
2038 return ft2232_purge_libftdi();
2045 static int usbjtag_init(void)
2051 low_direction = 0x0b;
2053 if (strcmp(ft2232_layout, "usbjtag") == 0)
2060 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2067 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2074 low_direction = 0x8b;
2078 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2079 return ERROR_JTAG_INIT_FAILED;
2082 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2084 low_direction &= ~nTRSTnOE; /* nTRST input */
2085 low_output &= ~nTRST; /* nTRST = 0 */
2089 low_direction |= nTRSTnOE; /* nTRST output */
2090 low_output |= nTRST; /* nTRST = 1 */
2093 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2095 low_direction |= nSRSTnOE; /* nSRST output */
2096 low_output |= nSRST; /* nSRST = 1 */
2100 low_direction &= ~nSRSTnOE; /* nSRST input */
2101 low_output &= ~nSRST; /* nSRST = 0 */
2104 /* initialize low byte for jtag */
2105 buf[0] = 0x80; /* command "set data bits low byte" */
2106 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
2107 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2108 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2110 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2112 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2113 return ERROR_JTAG_INIT_FAILED;
2120 static int axm0432_jtag_init(void)
2126 low_direction = 0x2b;
2128 /* initialize low byte for jtag */
2129 buf[0] = 0x80; /* command "set data bits low byte" */
2130 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2131 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2132 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2134 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2136 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2137 return ERROR_JTAG_INIT_FAILED;
2140 if (strcmp(layout->name, "axm0432_jtag") == 0)
2143 nTRSTnOE = 0x0; /* No output enable for TRST*/
2145 nSRSTnOE = 0x0; /* No output enable for SRST*/
2149 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2154 high_direction = 0x0c;
2156 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2158 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2162 high_output |= nTRST;
2165 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2167 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2171 high_output |= nSRST;
2174 /* initialize high port */
2175 buf[0] = 0x82; /* command "set data bits high byte" */
2176 buf[1] = high_output; /* value */
2177 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2178 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2180 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2182 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2183 return ERROR_JTAG_INIT_FAILED;
2190 static int jtagkey_init(void)
2196 low_direction = 0x1b;
2198 /* initialize low byte for jtag */
2199 buf[0] = 0x80; /* command "set data bits low byte" */
2200 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2201 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2202 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2204 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2206 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2207 return ERROR_JTAG_INIT_FAILED;
2210 if (strcmp(layout->name, "jtagkey") == 0)
2217 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2218 || (strcmp(layout->name, "oocdlink") == 0) )
2227 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2232 high_direction = 0x0f;
2234 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2236 high_output |= nTRSTnOE;
2237 high_output &= ~nTRST;
2241 high_output &= ~nTRSTnOE;
2242 high_output |= nTRST;
2245 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2247 high_output &= ~nSRSTnOE;
2248 high_output |= nSRST;
2252 high_output |= nSRSTnOE;
2253 high_output &= ~nSRST;
2256 /* initialize high port */
2257 buf[0] = 0x82; /* command "set data bits high byte" */
2258 buf[1] = high_output; /* value */
2259 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2260 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2262 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2264 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2265 return ERROR_JTAG_INIT_FAILED;
2272 static int olimex_jtag_init(void)
2278 low_direction = 0x1b;
2280 /* initialize low byte for jtag */
2281 buf[0] = 0x80; /* command "set data bits low byte" */
2282 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2283 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2284 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2286 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2288 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2289 return ERROR_JTAG_INIT_FAILED;
2295 nSRSTnOE = 0x00; /* no output enable for nSRST */
2298 high_direction = 0x0f;
2300 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2302 high_output |= nTRSTnOE;
2303 high_output &= ~nTRST;
2307 high_output &= ~nTRSTnOE;
2308 high_output |= nTRST;
2311 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2313 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2317 high_output &= ~nSRST;
2320 /* turn red LED on */
2321 high_output |= 0x08;
2323 /* initialize high port */
2324 buf[0] = 0x82; /* command "set data bits high byte" */
2325 buf[1] = high_output; /* value */
2326 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2327 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2329 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2331 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2332 return ERROR_JTAG_INIT_FAILED;
2339 static int flyswatter_init(void)
2345 low_direction = 0xfb;
2347 /* initialize low byte for jtag */
2348 buf[0] = 0x80; /* command "set data bits low byte" */
2349 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2350 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2351 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2353 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2355 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2356 return ERROR_JTAG_INIT_FAILED;
2360 nTRSTnOE = 0x0; /* not output enable for nTRST */
2362 nSRSTnOE = 0x00; /* no output enable for nSRST */
2365 high_direction = 0x0c;
2367 /* turn red LED3 on, LED2 off */
2368 high_output |= 0x08;
2370 /* initialize high port */
2371 buf[0] = 0x82; /* command "set data bits high byte" */
2372 buf[1] = high_output; /* value */
2373 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2374 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2376 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2378 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2379 return ERROR_JTAG_INIT_FAILED;
2386 static int turtle_init(void)
2392 low_direction = 0x5b;
2394 /* initialize low byte for jtag */
2395 buf[0] = 0x80; /* command "set data bits low byte" */
2396 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2397 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2398 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2400 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2402 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2403 return ERROR_JTAG_INIT_FAILED;
2409 high_direction = 0x0C;
2411 /* initialize high port */
2412 buf[0] = 0x82; /* command "set data bits high byte" */
2413 buf[1] = high_output;
2414 buf[2] = high_direction;
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;
2427 static int comstick_init(void)
2433 low_direction = 0x0b;
2435 /* initialize low byte for jtag */
2436 buf[0] = 0x80; /* command "set data bits low byte" */
2437 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2438 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2439 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2441 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2443 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2444 return ERROR_JTAG_INIT_FAILED;
2448 nTRSTnOE = 0x00; /* no output enable for nTRST */
2450 nSRSTnOE = 0x00; /* no output enable for nSRST */
2453 high_direction = 0x03;
2455 /* initialize high port */
2456 buf[0] = 0x82; /* command "set data bits high byte" */
2457 buf[1] = high_output;
2458 buf[2] = high_direction;
2459 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2461 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2463 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2464 return ERROR_JTAG_INIT_FAILED;
2471 static int stm32stick_init(void)
2477 low_direction = 0x8b;
2479 /* initialize low byte for jtag */
2480 buf[0] = 0x80; /* command "set data bits low byte" */
2481 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2482 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2483 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2485 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2487 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2488 return ERROR_JTAG_INIT_FAILED;
2492 nTRSTnOE = 0x00; /* no output enable for nTRST */
2494 nSRSTnOE = 0x00; /* no output enable for nSRST */
2497 high_direction = 0x03;
2499 /* initialize high port */
2500 buf[0] = 0x82; /* command "set data bits high byte" */
2501 buf[1] = high_output;
2502 buf[2] = high_direction;
2503 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2505 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2507 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2508 return ERROR_JTAG_INIT_FAILED;
2515 static int sheevaplug_init(void)
2521 low_direction = 0x1b;
2523 /* initialize low byte for jtag */
2524 buf[0] = 0x80; /* command "set data bits low byte" */
2525 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2526 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2527 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2529 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2531 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2532 return ERROR_JTAG_INIT_FAILED;
2541 high_direction = 0x0f;
2543 /* nTRST is always push-pull */
2544 high_output &= ~nTRSTnOE;
2545 high_output |= nTRST;
2547 /* nSRST is always open-drain */
2548 high_output |= nSRSTnOE;
2549 high_output &= ~nSRST;
2551 /* initialize high port */
2552 buf[0] = 0x82; /* command "set data bits high byte" */
2553 buf[1] = high_output; /* value */
2554 buf[2] = high_direction; /* all outputs - xRST */
2555 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2557 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2559 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2560 return ERROR_JTAG_INIT_FAILED;
2566 static int cortino_jtag_init(void)
2572 low_direction = 0x1b;
2574 /* initialize low byte for jtag */
2575 buf[0] = 0x80; /* command "set data bits low byte" */
2576 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2577 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2578 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2580 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2582 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2583 return ERROR_JTAG_INIT_FAILED;
2587 nTRSTnOE = 0x00; /* no output enable for nTRST */
2589 nSRSTnOE = 0x00; /* no output enable for nSRST */
2592 high_direction = 0x03;
2594 /* initialize high port */
2595 buf[0] = 0x82; /* command "set data bits high byte" */
2596 buf[1] = high_output;
2597 buf[2] = high_direction;
2598 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2600 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2602 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2603 return ERROR_JTAG_INIT_FAILED;
2609 static void olimex_jtag_blink(void)
2611 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2612 * ACBUS3 is bit 3 of the GPIOH port
2614 if (high_output & 0x08)
2616 /* set port pin high */
2617 high_output &= 0x07;
2621 /* set port pin low */
2622 high_output |= 0x08;
2625 buffer_write( 0x82 );
2626 buffer_write( high_output );
2627 buffer_write( high_direction );
2631 static void flyswatter_jtag_blink(void)
2634 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2636 high_output ^= 0x0c;
2638 buffer_write( 0x82 );
2639 buffer_write( high_output );
2640 buffer_write( high_direction );
2644 static void turtle_jtag_blink(void)
2647 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2649 if (high_output & 0x08)
2658 buffer_write( 0x82 );
2659 buffer_write( high_output );
2660 buffer_write( high_direction );
2664 static int ft2232_quit(void)
2666 #if BUILD_FT2232_FTD2XX == 1
2669 status = FT_Close(ftdih);
2670 #elif BUILD_FT2232_LIBFTDI == 1
2671 ftdi_usb_close(&ftdic);
2673 ftdi_deinit(&ftdic);
2676 free(ft2232_buffer);
2677 ft2232_buffer = NULL;
2683 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2689 ft2232_device_desc = strdup(args[0]);
2690 cp = strchr( ft2232_device_desc, 0 );
2691 // under Win32, the FTD2XX driver appends an "A" to the end
2692 // of the description, this examines the given desc
2693 // and creates the 'missing' _A or non_A variable.
2694 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2695 // it was, so make this the "A" version.
2696 ft2232_device_desc_A = ft2232_device_desc;
2697 // and *CREATE* the non-A version.
2698 strcpy( buf, ft2232_device_desc );
2699 cp = strchr( buf, 0 );
2701 ft2232_device_desc = strdup( buf );
2703 // <space>A not defined
2705 sprintf( buf, "%s A", ft2232_device_desc );
2706 ft2232_device_desc_A = strdup( buf );
2711 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2718 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2722 ft2232_serial = strdup(args[0]);
2726 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2733 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2738 ft2232_layout = malloc(strlen(args[0]) + 1);
2739 strcpy(ft2232_layout, args[0]);
2745 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2749 if (argc > MAX_USB_IDS * 2)
2751 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2752 "(maximum is %d pairs)", MAX_USB_IDS);
2753 argc = MAX_USB_IDS * 2;
2755 if ( argc < 2 || (argc & 1) )
2757 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2762 for (i = 0; i + 1 < argc; i += 2)
2764 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2765 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2769 * Explicitly terminate, in case there are multiples instances of
2772 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2778 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2782 ft2232_latency = atoi(args[0]);
2786 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2793 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2797 /* 7 bits of either ones or zeros. */
2798 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2800 while (num_cycles > 0)
2802 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2803 * at most 7 bits per invocation. Here we invoke it potentially
2806 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2808 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2810 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2811 retval = ERROR_JTAG_QUEUE_FAILED;
2816 /* there are no state transitions in this code, so omit state tracking */
2818 /* command "Clock Data to TMS/CS Pin (no Read)" */
2819 buffer_write( 0x4b );
2822 buffer_write( bitcount_per_command - 1 );
2824 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2825 buffer_write( tms );
2829 num_cycles -= bitcount_per_command;
2836 /* ---------------------------------------------------------------------
2837 * Support for IceBear JTAG adapter from Section5:
2838 * http://section5.ch/icebear
2840 * Author: Sten, debian@sansys-electronic.com
2843 /* Icebear pin layout
2845 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2846 * GND GND | 4 3| n.c.
2847 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2848 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2849 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2850 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2851 * ADBUS2 TDO |14 13| GND GND
2853 * ADBUS0 O L TCK ACBUS0 GND
2854 * ADBUS1 O L TDI ACBUS1 GND
2855 * ADBUS2 I TDO ACBUS2 n.c.
2856 * ADBUS3 O H TMS ACBUS3 n.c.
2862 static int icebear_jtag_init(void) {
2866 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2867 low_output = 0x08; /* high: TMS; low: TCK TDI */
2871 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2872 low_direction &= ~nTRST; /* nTRST high impedance */
2875 low_direction |= nTRST;
2876 low_output |= nTRST;
2879 low_direction |= nSRST;
2880 low_output |= nSRST;
2882 /* initialize low byte for jtag */
2883 buf[0] = 0x80; /* command "set data bits low byte" */
2884 buf[1] = low_output;
2885 buf[2] = low_direction;
2886 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2888 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2889 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2890 return ERROR_JTAG_INIT_FAILED;
2894 high_direction = 0x00;
2897 /* initialize high port */
2898 buf[0] = 0x82; /* command "set data bits high byte" */
2899 buf[1] = high_output; /* value */
2900 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2901 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2903 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2904 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2905 return ERROR_JTAG_INIT_FAILED;
2911 static void icebear_jtag_reset(int trst, int srst) {
2914 low_direction |= nTRST;
2915 low_output &= ~nTRST;
2917 else if (trst == 0) {
2918 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
2919 low_direction &= ~nTRST;
2921 low_output |= nTRST;
2925 low_output &= ~nSRST;
2927 else if (srst == 0) {
2928 low_output |= nSRST;
2931 /* command "set data bits low byte" */
2932 buffer_write( 0x80 );
2933 buffer_write( low_output );
2934 buffer_write( low_direction );
2936 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);