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);
127 static int cortino_jtag_init(void);
129 /* reset procedures for supported layouts */
130 static void usbjtag_reset(int trst, int srst);
131 static void jtagkey_reset(int trst, int srst);
132 static void olimex_jtag_reset(int trst, int srst);
133 static void flyswatter_reset(int trst, int srst);
134 static void turtle_reset(int trst, int srst);
135 static void comstick_reset(int trst, int srst);
136 static void stm32stick_reset(int trst, int srst);
137 static void axm0432_jtag_reset(int trst, int srst);
138 static void sheevaplug_reset(int trst, int srst);
139 static void icebear_jtag_reset(int trst, int srst);
141 /* blink procedures for layouts that support a blinking led */
142 static void olimex_jtag_blink(void);
143 static void flyswatter_jtag_blink(void);
144 static void turtle_jtag_blink(void);
146 ft2232_layout_t ft2232_layouts[] =
148 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
149 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
150 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
151 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
152 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
153 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
154 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
155 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
156 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
157 { "comstick", comstick_init, comstick_reset, NULL },
158 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
159 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
160 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
161 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
162 { "cortino", cortino_jtag_init, comstick_reset, NULL },
163 { NULL, NULL, NULL, NULL },
166 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
168 static ft2232_layout_t* layout;
169 static u8 low_output = 0x0;
170 static u8 low_direction = 0x0;
171 static u8 high_output = 0x0;
172 static u8 high_direction = 0x0;
174 #if BUILD_FT2232_FTD2XX == 1
175 static FT_HANDLE ftdih = NULL;
176 #elif BUILD_FT2232_LIBFTDI == 1
177 static struct ftdi_context ftdic;
181 static jtag_command_t* first_unsent; /* next command that has to be sent */
182 static int require_send;
185 /* http://urjtag.wiki.sourceforge.net/Cable+FT2232 says:
187 "There is a significant difference between libftdi and libftd2xx. The latter
188 one allows to schedule up to 64*64 bytes of result data while libftdi fails
189 with more than 4*64. As a consequence, the FT2232 driver is forced to
190 perform around 16x more USB transactions for long command streams with TDO
191 capture when running with libftdi."
194 #define FT2232_BUFFER_SIZE 131072
195 a comment would have been nice.
198 #define FT2232_BUFFER_SIZE 131072
200 static u8* ft2232_buffer = NULL;
201 static int ft2232_buffer_size = 0;
202 static int ft2232_read_pointer = 0;
203 static int ft2232_expect_read = 0;
206 * Function buffer_write
207 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
208 * @param val is the byte to send.
210 static inline void buffer_write( u8 val )
212 assert( ft2232_buffer );
213 assert( (unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE );
214 ft2232_buffer[ft2232_buffer_size++] = val;
218 * Function buffer_read
219 * returns a byte from the byte buffer.
221 static inline u8 buffer_read(void)
223 assert( ft2232_buffer );
224 assert( ft2232_read_pointer < ft2232_buffer_size );
225 return ft2232_buffer[ft2232_read_pointer++];
231 * clocks out \a bit_count bits on the TMS line, starting with the least
232 * significant bit of tms_bits and progressing to more significant bits.
233 * Rigorous state transition logging is done here via tap_set_state().
235 * @param pmsse_cmd is one of the MPSSE TMS oriented commands such as 0x4b or 0x6b. See
236 * the MPSSE spec referenced above for their functionality. The MPSSE command
237 * "Clock Data to TMS/CS Pin (no Read)" is often used for this, 0x4b.
239 * @param tms_bits holds the sequence of bits to send.
240 * @param tms_count tells how many bits in the sequence.
241 * @param tdi_bit is a single bit which is passed on to TDI before the first TCK cycle
242 * and is held static for the duration of TMS clocking. See the MPSSE spec referenced above.
244 static void clock_tms( u8 mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit )
248 int tms_ndx; /* bit index into tms_byte */
250 assert( tms_count > 0 );
252 // LOG_DEBUG("mpsse cmd=%02x, tms_bits=0x%08x, bit_count=%d", mpsse_cmd, tms_bits, tms_count );
254 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
256 bool bit = tms_bits & 1;
259 tms_byte |= (1<<tms_ndx);
261 /* always do state transitions in public view */
262 tap_set_state( tap_state_transition(tap_get_state(), bit) );
264 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
269 if( tms_ndx==7 || i==tms_count-1 )
271 buffer_write( mpsse_cmd );
272 buffer_write( tms_ndx - 1 );
274 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
275 TMS/CS and is held static for the duration of TMS/CS clocking.
277 buffer_write( tms_byte | (tdi_bit << 7) );
284 * Function get_tms_buffer_requirements
285 * returns what clock_tms() will consume if called with
288 static inline int get_tms_buffer_requirements( int bit_count )
290 return ((bit_count + 6)/7) * 3;
295 * Function move_to_state
296 * moves the TAP controller from the current state to a
297 * \a goal_state through a path given by tap_get_tms_path(). State transition
298 * logging is performed by delegation to clock_tms().
300 * @param goal_state is the destination state for the move.
302 static void move_to_state( tap_state_t goal_state )
304 tap_state_t start_state = tap_get_state();
306 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
307 lookup of the required TMS pattern to move to this state from the
311 /* do the 2 lookups */
312 int tms_bits = tap_get_tms_path(start_state, goal_state);
313 int tms_count = tap_get_tms_path_len(start_state, goal_state);
315 DEBUG_JTAG_IO( "start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state) );
317 clock_tms( 0x4b, tms_bits, tms_count, 0 );
321 jtag_interface_t ft2232_interface =
324 .execute_queue = ft2232_execute_queue,
325 .speed = ft2232_speed,
326 .speed_div = ft2232_speed_div,
328 .register_commands = ft2232_register_commands,
333 static int ft2232_write(u8* buf, int size, u32* bytes_written)
335 #if BUILD_FT2232_FTD2XX == 1
337 DWORD dw_bytes_written;
338 if ( ( status = FT_Write(ftdih, buf, size, &dw_bytes_written) ) != FT_OK )
340 *bytes_written = dw_bytes_written;
341 LOG_ERROR("FT_Write returned: %lu", status);
342 return ERROR_JTAG_DEVICE_ERROR;
346 *bytes_written = dw_bytes_written;
349 #elif BUILD_FT2232_LIBFTDI == 1
351 if ( ( retval = ftdi_write_data(&ftdic, buf, size) ) < 0 )
354 LOG_ERROR( "ftdi_write_data: %s", ftdi_get_error_string(&ftdic) );
355 return ERROR_JTAG_DEVICE_ERROR;
359 *bytes_written = retval;
366 static int ft2232_read(u8* buf, u32 size, u32* bytes_read)
368 #if BUILD_FT2232_FTD2XX == 1
374 while ( (*bytes_read < size) && timeout-- )
376 if ( ( status = FT_Read(ftdih, buf + *bytes_read, size -
377 *bytes_read, &dw_bytes_read) ) != FT_OK )
380 LOG_ERROR("FT_Read returned: %lu", status);
381 return ERROR_JTAG_DEVICE_ERROR;
383 *bytes_read += dw_bytes_read;
386 #elif BUILD_FT2232_LIBFTDI == 1
391 while ( (*bytes_read < size) && timeout-- )
393 if ( ( retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read) ) < 0 )
396 LOG_ERROR( "ftdi_read_data: %s", ftdi_get_error_string(&ftdic) );
397 return ERROR_JTAG_DEVICE_ERROR;
399 *bytes_read += retval;
404 if (*bytes_read < size)
406 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
407 return ERROR_JTAG_DEVICE_ERROR;
414 static int ft2232_speed(int speed)
420 buf[0] = 0x86; /* command "set divisor" */
421 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
422 buf[2] = (speed >> 8) & 0xff; /* valueH */
424 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
425 if ( ( ( retval = ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
427 LOG_ERROR("couldn't set FT2232 TCK speed");
435 static int ft2232_speed_div(int speed, int* khz)
437 /* Take a look in the FT2232 manual,
438 * AN2232C-01 Command Processor for
439 * MPSSE and MCU Host Bus. Chapter 3.8 */
441 *khz = ft2232_max_tck / (1 + speed);
447 static int ft2232_khz(int khz, int* jtag_speed)
451 LOG_DEBUG("RTCK not supported");
455 /* Take a look in the FT2232 manual,
456 * AN2232C-01 Command Processor for
457 * MPSSE and MCU Host Bus. Chapter 3.8
459 * We will calc here with a multiplier
460 * of 10 for better rounding later. */
462 /* Calc speed, (ft2232_max_tck / khz) - 1 */
463 /* Use 65000 for better rounding */
464 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
466 /* Add 0.9 for rounding */
469 /* Calc real speed */
470 *jtag_speed = *jtag_speed / 10;
472 /* Check if speed is greater than 0 */
478 /* Check max value */
479 if (*jtag_speed > 0xFFFF)
481 *jtag_speed = 0xFFFF;
488 static int ft2232_register_commands(struct command_context_s* cmd_ctx)
490 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
491 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
492 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
493 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
494 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
495 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
496 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
497 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
498 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
499 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
504 void ft2232_end_state(tap_state_t state)
506 if (tap_is_state_stable(state))
507 tap_set_end_state(state);
510 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
515 static void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
517 int num_bytes = (scan_size + 7) / 8;
518 int bits_left = scan_size;
521 while (num_bytes-- > 1)
523 buffer[cur_byte++] = buffer_read();
527 buffer[cur_byte] = 0x0;
529 /* There is one more partial byte left from the clock data in/out instructions */
532 buffer[cur_byte] = buffer_read() >> 1;
534 /* 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 */
535 buffer[cur_byte] = ( buffer[cur_byte] | ( ( (buffer_read()) << 1 ) & 0x80 )) >> (8 - bits_left);
539 static void ft2232_debug_dump_buffer(void)
545 for (i = 0; i < ft2232_buffer_size; i++)
547 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
550 LOG_DEBUG("%s", line);
556 LOG_DEBUG("%s", line);
560 static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
570 #ifdef _DEBUG_USB_IO_
571 struct timeval start, inter, inter2, end;
572 struct timeval d_inter, d_inter2, d_end;
575 #ifdef _DEBUG_USB_COMMS_
576 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
577 ft2232_debug_dump_buffer();
580 #ifdef _DEBUG_USB_IO_
581 gettimeofday(&start, NULL);
584 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
586 LOG_ERROR("couldn't write MPSSE commands to FT2232");
590 #ifdef _DEBUG_USB_IO_
591 gettimeofday(&inter, NULL);
594 if (ft2232_expect_read)
597 ft2232_buffer_size = 0;
599 #ifdef _DEBUG_USB_IO_
600 gettimeofday(&inter2, NULL);
603 if ( ( retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read) ) != ERROR_OK )
605 LOG_ERROR("couldn't read from FT2232");
609 #ifdef _DEBUG_USB_IO_
610 gettimeofday(&end, NULL);
612 timeval_subtract(&d_inter, &inter, &start);
613 timeval_subtract(&d_inter2, &inter2, &start);
614 timeval_subtract(&d_end, &end, &start);
616 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
617 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
618 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
619 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
622 ft2232_buffer_size = bytes_read;
624 if (ft2232_expect_read != ft2232_buffer_size)
626 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
629 ft2232_debug_dump_buffer();
634 #ifdef _DEBUG_USB_COMMS_
635 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
636 ft2232_debug_dump_buffer();
640 ft2232_expect_read = 0;
641 ft2232_read_pointer = 0;
643 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
644 * that wasn't handled by a caller-provided error handler
654 type = jtag_scan_type(cmd->cmd.scan);
655 if (type != SCAN_OUT)
657 scan_size = jtag_scan_size(cmd->cmd.scan);
658 buffer = calloc(CEIL(scan_size, 8), 1);
659 ft2232_read_scan(type, buffer, scan_size);
660 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
661 retval = ERROR_JTAG_QUEUE_FAILED;
673 ft2232_buffer_size = 0;
680 * Function ft2232_add_pathmove
681 * moves the TAP controller from the current state to a new state through the
682 * given path, where path is an array of tap_state_t's.
684 * @param path is an array of tap_stat_t which gives the states to traverse through
685 * ending with the last state at path[num_states-1]
686 * @param num_states is the count of state steps to move through
688 static void ft2232_add_pathmove( tap_state_t* path, int num_states )
692 tap_state_t walker = tap_get_state();
694 assert( (unsigned) num_states <= 32u ); /* tms_bits only holds 32 bits */
696 /* this loop verifies that the path is legal and logs each state in the path */
697 for( state_ndx = 0; state_ndx < num_states; ++state_ndx )
699 tap_state_t desired_next_state = path[state_ndx];
701 if (tap_state_transition(walker, false) == desired_next_state )
702 ; /* bit within tms_bits at index state_ndx is already zero */
703 else if (tap_state_transition(walker, true) == desired_next_state )
704 tms_bits |= (1<<state_ndx);
707 LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition",
708 tap_state_name(walker), tap_state_name(desired_next_state) );
712 walker = desired_next_state;
715 clock_tms( 0x4b, tms_bits, num_states, 0 );
717 tap_set_end_state(tap_get_state());
721 void ft2232_add_scan(bool ir_scan, enum scan_type type, u8* buffer, int scan_size)
723 int num_bytes = (scan_size + 7) / 8;
724 int bits_left = scan_size;
730 if (tap_get_state() != TAP_DRSHIFT)
732 move_to_state( TAP_DRSHIFT );
737 if (tap_get_state() != TAP_IRSHIFT)
739 move_to_state( TAP_IRSHIFT );
743 /* add command for complete bytes */
744 while (num_bytes > 1)
749 /* Clock Data Bytes In and Out LSB First */
750 buffer_write( 0x39 );
751 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
753 else if (type == SCAN_OUT)
755 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
756 buffer_write( 0x19 );
757 /* LOG_DEBUG("added TDI bytes (o)"); */
759 else if (type == SCAN_IN)
761 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
762 buffer_write( 0x28 );
763 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
766 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
767 num_bytes -= thisrun_bytes;
769 buffer_write( (u8) (thisrun_bytes - 1) );
770 buffer_write( (u8) ((thisrun_bytes - 1) >> 8) );
774 /* add complete bytes */
775 while (thisrun_bytes-- > 0)
777 buffer_write( buffer[cur_byte++] );
781 else /* (type == SCAN_IN) */
783 bits_left -= 8 * (thisrun_bytes);
787 /* the most signifcant bit is scanned during TAP movement */
789 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
793 /* process remaining bits but the last one */
798 /* Clock Data Bits In and Out LSB First */
799 buffer_write( 0x3b );
800 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
802 else if (type == SCAN_OUT)
804 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
805 buffer_write( 0x1b );
806 /* LOG_DEBUG("added TDI bits (o)"); */
808 else if (type == SCAN_IN)
810 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
811 buffer_write( 0x2a );
812 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
815 buffer_write( bits_left - 2 );
817 buffer_write( buffer[cur_byte] );
820 if ( ( ir_scan && (tap_get_end_state() == TAP_IRSHIFT) )
821 || ( !ir_scan && (tap_get_end_state() == TAP_DRSHIFT) ) )
825 /* Clock Data Bits In and Out LSB First */
826 buffer_write( 0x3b );
827 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
829 else if (type == SCAN_OUT)
831 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
832 buffer_write( 0x1b );
833 /* LOG_DEBUG("added TDI bits (o)"); */
835 else if (type == SCAN_IN)
837 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
838 buffer_write( 0x2a );
839 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
842 buffer_write( last_bit );
850 /* move from Shift-IR/DR to end state */
851 if (type != SCAN_OUT)
853 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
854 /* This must be coordinated with the bit shifts in ft2232_read_scan */
857 /* Clock Data to TMS/CS Pin with Read */
859 /* LOG_DEBUG("added TMS scan (read)"); */
863 tms_bits = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
864 tms_count = tap_get_tms_path_len( tap_get_state(), tap_get_end_state() );
865 /* Clock Data to TMS/CS Pin (no Read) */
867 /* LOG_DEBUG("added TMS scan (no read)"); */
870 clock_tms( mpsse_cmd, tms_bits, tms_count, last_bit );
873 if (tap_get_state() != tap_get_end_state())
875 move_to_state( tap_get_end_state() );
880 static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, u8* buffer, int scan_size)
882 int num_bytes = (scan_size + 7) / 8;
883 int bits_left = scan_size;
886 u8* receive_buffer = malloc( CEIL(scan_size, 8) );
887 u8* receive_pointer = receive_buffer;
891 int thisrun_read = 0;
895 LOG_ERROR("BUG: large IR scans are not supported");
899 if (tap_get_state() != TAP_DRSHIFT)
901 move_to_state( TAP_DRSHIFT );
904 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
906 LOG_ERROR("couldn't write MPSSE commands to FT2232");
909 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
910 ft2232_buffer_size = 0;
912 /* add command for complete bytes */
913 while (num_bytes > 1)
919 /* Clock Data Bytes In and Out LSB First */
920 buffer_write( 0x39 );
921 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
923 else if (type == SCAN_OUT)
925 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
926 buffer_write( 0x19 );
927 /* LOG_DEBUG("added TDI bytes (o)"); */
929 else if (type == SCAN_IN)
931 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
932 buffer_write( 0x28 );
933 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
936 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
937 thisrun_read = thisrun_bytes;
938 num_bytes -= thisrun_bytes;
939 buffer_write( (u8) (thisrun_bytes - 1) );
940 buffer_write( (u8) ( (thisrun_bytes - 1) >> 8 ));
944 /* add complete bytes */
945 while (thisrun_bytes-- > 0)
947 buffer_write( buffer[cur_byte] );
952 else /* (type == SCAN_IN) */
954 bits_left -= 8 * (thisrun_bytes);
957 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
959 LOG_ERROR("couldn't write MPSSE commands to FT2232");
962 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
963 ft2232_buffer_size = 0;
965 if (type != SCAN_OUT)
967 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
969 LOG_ERROR("couldn't read from FT2232");
972 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
973 receive_pointer += bytes_read;
979 /* the most signifcant bit is scanned during TAP movement */
981 last_bit = ( buffer[cur_byte] >> (bits_left - 1) ) & 0x1;
985 /* process remaining bits but the last one */
990 /* Clock Data Bits In and Out LSB First */
991 buffer_write( 0x3b );
992 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
994 else if (type == SCAN_OUT)
996 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
997 buffer_write( 0x1b );
998 /* LOG_DEBUG("added TDI bits (o)"); */
1000 else if (type == SCAN_IN)
1002 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1003 buffer_write( 0x2a );
1004 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1006 buffer_write( bits_left - 2 );
1007 if (type != SCAN_IN)
1008 buffer_write( buffer[cur_byte] );
1010 if (type != SCAN_OUT)
1014 if (tap_get_end_state() == TAP_DRSHIFT)
1016 if (type == SCAN_IO)
1018 /* Clock Data Bits In and Out LSB First */
1019 buffer_write( 0x3b );
1020 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1022 else if (type == SCAN_OUT)
1024 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1025 buffer_write( 0x1b );
1026 /* LOG_DEBUG("added TDI bits (o)"); */
1028 else if (type == SCAN_IN)
1030 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1031 buffer_write( 0x2a );
1032 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1034 buffer_write( 0x0 );
1035 buffer_write( last_bit );
1039 int tms_bits = tap_get_tms_path( tap_get_state(), tap_get_end_state() );
1040 int tms_count = tap_get_tms_path_len( tap_get_state(), tap_get_end_state() );
1043 /* move from Shift-IR/DR to end state */
1044 if (type != SCAN_OUT)
1046 /* Clock Data to TMS/CS Pin with Read */
1048 /* LOG_DEBUG("added TMS scan (read)"); */
1052 /* Clock Data to TMS/CS Pin (no Read) */
1054 /* LOG_DEBUG("added TMS scan (no read)"); */
1057 clock_tms( mpsse_cmd, tms_bits, tms_count, last_bit );
1060 if (type != SCAN_OUT)
1063 if ( ( retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written) ) != ERROR_OK )
1065 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1068 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
1069 ft2232_buffer_size = 0;
1071 if (type != SCAN_OUT)
1073 if ( ( retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read) ) != ERROR_OK )
1075 LOG_ERROR("couldn't read from FT2232");
1078 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
1079 receive_pointer += bytes_read;
1086 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1088 int predicted_size = 3;
1089 int num_bytes = (scan_size - 1) / 8;
1091 if (tap_get_state() != TAP_DRSHIFT)
1092 predicted_size += get_tms_buffer_requirements( tap_get_tms_path_len( tap_get_state(), TAP_DRSHIFT) );
1094 if (type == SCAN_IN) /* only from device to host */
1096 /* complete bytes */
1097 predicted_size += CEIL(num_bytes, 65536) * 3;
1099 /* remaining bits - 1 (up to 7) */
1100 predicted_size += ( (scan_size - 1) % 8 ) ? 2 : 0;
1102 else /* host to device, or bidirectional */
1104 /* complete bytes */
1105 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
1107 /* remaining bits -1 (up to 7) */
1108 predicted_size += ( (scan_size - 1) % 8 ) ? 3 : 0;
1111 return predicted_size;
1115 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1117 int predicted_size = 0;
1119 if (type != SCAN_OUT)
1121 /* complete bytes */
1122 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
1124 /* remaining bits - 1 */
1125 predicted_size += ( (scan_size - 1) % 8 ) ? 1 : 0;
1127 /* last bit (from TMS scan) */
1128 predicted_size += 1;
1131 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1133 return predicted_size;
1137 static void usbjtag_reset(int trst, int srst)
1141 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1142 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1144 low_output &= ~nTRST; /* switch output low */
1148 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1149 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1151 low_output |= nTRST; /* switch output high */
1156 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1157 low_output &= ~nSRST; /* switch output low */
1159 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1163 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1164 low_output |= nSRST; /* switch output high */
1166 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1169 /* command "set data bits low byte" */
1170 buffer_write( 0x80 );
1171 buffer_write( low_output );
1172 buffer_write( low_direction );
1176 static void jtagkey_reset(int trst, int srst)
1180 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1181 high_output &= ~nTRSTnOE;
1183 high_output &= ~nTRST;
1187 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1188 high_output |= nTRSTnOE;
1190 high_output |= nTRST;
1195 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1196 high_output &= ~nSRST;
1198 high_output &= ~nSRSTnOE;
1202 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1203 high_output |= nSRST;
1205 high_output |= nSRSTnOE;
1208 /* command "set data bits high byte" */
1209 buffer_write( 0x82 );
1210 buffer_write( high_output );
1211 buffer_write( high_direction );
1212 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1217 static void olimex_jtag_reset(int trst, int srst)
1221 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1222 high_output &= ~nTRSTnOE;
1224 high_output &= ~nTRST;
1228 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1229 high_output |= nTRSTnOE;
1231 high_output |= nTRST;
1236 high_output |= nSRST;
1240 high_output &= ~nSRST;
1243 /* command "set data bits high byte" */
1244 buffer_write( 0x82 );
1245 buffer_write( high_output );
1246 buffer_write( high_direction );
1247 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1252 static void axm0432_jtag_reset(int trst, int srst)
1256 tap_set_state(TAP_RESET);
1257 high_output &= ~nTRST;
1261 high_output |= nTRST;
1266 high_output &= ~nSRST;
1270 high_output |= nSRST;
1273 /* command "set data bits low byte" */
1274 buffer_write( 0x82 );
1275 buffer_write( high_output );
1276 buffer_write( high_direction );
1277 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1282 static void flyswatter_reset(int trst, int srst)
1286 low_output &= ~nTRST;
1290 low_output |= nTRST;
1295 low_output |= nSRST;
1299 low_output &= ~nSRST;
1302 /* command "set data bits low byte" */
1303 buffer_write( 0x80 );
1304 buffer_write( low_output );
1305 buffer_write( low_direction );
1306 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1310 static void turtle_reset(int trst, int srst)
1316 low_output |= nSRST;
1320 low_output &= ~nSRST;
1323 /* command "set data bits low byte" */
1324 buffer_write( 0x80 );
1325 buffer_write( low_output );
1326 buffer_write( low_direction );
1327 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1331 static void comstick_reset(int trst, int srst)
1335 high_output &= ~nTRST;
1339 high_output |= nTRST;
1344 high_output &= ~nSRST;
1348 high_output |= nSRST;
1351 /* command "set data bits high byte" */
1352 buffer_write( 0x82 );
1353 buffer_write( high_output );
1354 buffer_write( high_direction );
1355 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1360 static void stm32stick_reset(int trst, int srst)
1364 high_output &= ~nTRST;
1368 high_output |= nTRST;
1373 low_output &= ~nSRST;
1377 low_output |= nSRST;
1380 /* command "set data bits low byte" */
1381 buffer_write( 0x80 );
1382 buffer_write( low_output );
1383 buffer_write( low_direction );
1385 /* command "set data bits high byte" */
1386 buffer_write( 0x82 );
1387 buffer_write( high_output );
1388 buffer_write( high_direction );
1389 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1395 static void sheevaplug_reset(int trst, int srst)
1398 high_output &= ~nTRST;
1400 high_output |= nTRST;
1403 high_output &= ~nSRSTnOE;
1405 high_output |= nSRSTnOE;
1407 /* command "set data bits high byte" */
1408 buffer_write( 0x82 );
1409 buffer_write( high_output );
1410 buffer_write( high_direction );
1411 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1414 static int ft2232_execute_runtest(jtag_command_t *cmd)
1418 int predicted_size = 0;
1421 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1422 cmd->cmd.runtest->num_cycles,
1423 tap_state_name(cmd->cmd.runtest->end_state));
1425 /* only send the maximum buffer size that FT2232C can handle */
1427 if (tap_get_state() != TAP_IDLE)
1428 predicted_size += 3;
1429 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1430 if ( (cmd->cmd.runtest->end_state != TAP_INVALID) && (cmd->cmd.runtest->end_state != TAP_IDLE) )
1431 predicted_size += 3;
1432 if ( (cmd->cmd.runtest->end_state == TAP_INVALID) && (tap_get_end_state() != TAP_IDLE) )
1433 predicted_size += 3;
1434 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1436 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1437 retval = ERROR_JTAG_QUEUE_FAILED;
1441 if (tap_get_state() != TAP_IDLE)
1443 move_to_state( TAP_IDLE );
1446 i = cmd->cmd.runtest->num_cycles;
1449 /* there are no state transitions in this code, so omit state tracking */
1451 /* command "Clock Data to TMS/CS Pin (no Read)" */
1452 buffer_write( 0x4b );
1455 buffer_write( (i > 7) ? 6 : (i - 1) );
1458 buffer_write( 0x0 );
1459 tap_set_state(TAP_IDLE);
1461 i -= (i > 7) ? 7 : i;
1462 /* LOG_DEBUG("added TMS scan (no read)"); */
1465 if (cmd->cmd.runtest->end_state != TAP_INVALID)
1466 ft2232_end_state(cmd->cmd.runtest->end_state);
1468 if ( tap_get_state() != tap_get_end_state() )
1470 move_to_state( tap_get_end_state() );
1474 #ifdef _DEBUG_JTAG_IO_
1475 LOG_DEBUG( "runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name( tap_get_end_state() ) );
1482 static int ft2232_execute_statemove(jtag_command_t *cmd)
1484 int predicted_size = 0;
1485 int retval = ERROR_OK;
1487 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1489 /* only send the maximum buffer size that FT2232C can handle */
1491 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1493 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1494 retval = ERROR_JTAG_QUEUE_FAILED;
1498 if (cmd->cmd.statemove->end_state != TAP_INVALID)
1499 ft2232_end_state(cmd->cmd.statemove->end_state);
1501 /* move to end state */
1502 if ( tap_get_state() != tap_get_end_state() )
1504 move_to_state( tap_get_end_state() );
1511 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1513 int predicted_size = 0;
1514 int retval = ERROR_OK;
1516 tap_state_t* path = cmd->cmd.pathmove->path;
1517 int num_states = cmd->cmd.pathmove->num_states;
1519 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1520 tap_state_name( tap_get_state() ),
1521 tap_state_name( path[num_states-1] )
1524 /* only send the maximum buffer size that FT2232C can handle */
1525 predicted_size = 3 * CEIL(num_states, 7);
1526 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1528 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1529 retval = ERROR_JTAG_QUEUE_FAILED;
1535 ft2232_add_pathmove( path, num_states );
1542 static int ft2232_execute_scan(jtag_command_t *cmd)
1545 int scan_size; /* size of IR or DR scan */
1546 int predicted_size = 0;
1547 int retval = ERROR_OK;
1549 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1551 DEBUG_JTAG_IO( "%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type );
1553 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1555 predicted_size = ft2232_predict_scan_out(scan_size, type);
1556 if ( (predicted_size + 1) > FT2232_BUFFER_SIZE )
1558 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1559 /* unsent commands before this */
1560 if (first_unsent != cmd)
1561 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1562 retval = ERROR_JTAG_QUEUE_FAILED;
1564 /* current command */
1565 if (cmd->cmd.scan->end_state != TAP_INVALID)
1566 ft2232_end_state(cmd->cmd.scan->end_state);
1567 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1569 first_unsent = cmd->next;
1574 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1576 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1579 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1580 retval = ERROR_JTAG_QUEUE_FAILED;
1584 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1585 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1586 if (cmd->cmd.scan->end_state != TAP_INVALID)
1587 ft2232_end_state(cmd->cmd.scan->end_state);
1588 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1592 #ifdef _DEBUG_JTAG_IO_
1593 LOG_DEBUG( "%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1594 tap_state_name( tap_get_end_state() ) );
1600 static int ft2232_execute_reset(jtag_command_t *cmd)
1603 int predicted_size = 0;
1606 DEBUG_JTAG_IO("reset trst: %i srst %i",
1607 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1609 /* only send the maximum buffer size that FT2232C can handle */
1611 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1613 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1614 retval = ERROR_JTAG_QUEUE_FAILED;
1619 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1622 #ifdef _DEBUG_JTAG_IO_
1623 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1628 static int ft2232_execute_sleep(jtag_command_t *cmd)
1633 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1635 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1636 retval = ERROR_JTAG_QUEUE_FAILED;
1637 first_unsent = cmd->next;
1638 jtag_sleep(cmd->cmd.sleep->us);
1639 #ifdef _DEBUG_JTAG_IO_
1640 LOG_DEBUG( "sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name( tap_get_state() ) );
1646 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1651 /* this is only allowed while in a stable state. A check for a stable
1652 * state was done in jtag_add_clocks()
1654 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1655 retval = ERROR_JTAG_QUEUE_FAILED;
1656 #ifdef _DEBUG_JTAG_IO_
1657 LOG_DEBUG( "clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name( tap_get_state() ) );
1663 static int ft2232_execute_command(jtag_command_t *cmd)
1670 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1671 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1672 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1673 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1674 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1675 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1676 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1678 LOG_ERROR("BUG: unknown JTAG command type encountered");
1684 static int ft2232_execute_queue()
1686 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1689 first_unsent = cmd; /* next command that has to be sent */
1692 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1693 * that wasn't handled by a caller-provided error handler
1697 ft2232_buffer_size = 0;
1698 ft2232_expect_read = 0;
1700 /* blink, if the current layout has that feature */
1706 if (ft2232_execute_command(cmd) != ERROR_OK)
1707 retval = ERROR_JTAG_QUEUE_FAILED;
1708 /* Start reading input before FT2232 TX buffer fills up */
1710 if (ft2232_expect_read > 256)
1712 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1713 retval = ERROR_JTAG_QUEUE_FAILED;
1718 if (require_send > 0)
1719 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1720 retval = ERROR_JTAG_QUEUE_FAILED;
1726 #if BUILD_FT2232_FTD2XX == 1
1727 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int* try_more)
1730 DWORD openex_flags = 0;
1731 char* openex_string = NULL;
1734 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1737 /* Add non-standard Vid/Pid to the linux driver */
1738 if ( ( status = FT_SetVIDPID(vid, pid) ) != FT_OK )
1740 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1744 if (ft2232_device_desc && ft2232_serial)
1746 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1747 ft2232_device_desc = NULL;
1750 if (ft2232_device_desc)
1752 openex_string = ft2232_device_desc;
1753 openex_flags = FT_OPEN_BY_DESCRIPTION;
1755 else if (ft2232_serial)
1757 openex_string = ft2232_serial;
1758 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1762 LOG_ERROR("neither device description nor serial number specified");
1763 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1765 return ERROR_JTAG_INIT_FAILED;
1768 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1769 if( status != FT_OK ){
1770 // under Win32, the FTD2XX driver appends an "A" to the end
1771 // of the description, if we tried by the desc, then
1772 // try by the alternate "A" description.
1773 if( openex_string == ft2232_device_desc ){
1774 // Try the alternate method.
1775 openex_string = ft2232_device_desc_A;
1776 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1777 if( status == FT_OK ){
1778 // yea, the "alternate" method worked!
1780 // drat, give the user a meaningfull message.
1781 // telling the use we tried *BOTH* methods.
1782 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1784 ft2232_device_desc_A );
1789 if ( status != FT_OK )
1795 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1797 return ERROR_JTAG_INIT_FAILED;
1799 LOG_ERROR("unable to open ftdi device: %lu", status);
1800 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1801 if (status == FT_OK)
1803 char** desc_array = malloc( sizeof(char*) * (num_devices + 1) );
1806 for (i = 0; i < num_devices; i++)
1807 desc_array[i] = malloc(64);
1809 desc_array[num_devices] = NULL;
1811 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1813 if (status == FT_OK)
1815 LOG_ERROR("ListDevices: %lu\n", num_devices);
1816 for (i = 0; i < num_devices; i++)
1817 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1820 for (i = 0; i < num_devices; i++)
1821 free(desc_array[i]);
1827 LOG_ERROR("ListDevices: NONE\n");
1829 return ERROR_JTAG_INIT_FAILED;
1832 if ( ( status = FT_SetLatencyTimer(ftdih, ft2232_latency) ) != FT_OK )
1834 LOG_ERROR("unable to set latency timer: %lu", status);
1835 return ERROR_JTAG_INIT_FAILED;
1838 if ( ( status = FT_GetLatencyTimer(ftdih, &latency_timer) ) != FT_OK )
1840 LOG_ERROR("unable to get latency timer: %lu", status);
1841 return ERROR_JTAG_INIT_FAILED;
1845 LOG_DEBUG("current latency timer: %i", latency_timer);
1848 if ( ( status = FT_SetTimeouts(ftdih, 5000, 5000) ) != FT_OK )
1850 LOG_ERROR("unable to set timeouts: %lu", status);
1851 return ERROR_JTAG_INIT_FAILED;
1854 if ( ( status = FT_SetBitMode(ftdih, 0x0b, 2) ) != FT_OK )
1856 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1857 return ERROR_JTAG_INIT_FAILED;
1864 static int ft2232_purge_ftd2xx(void)
1868 if ( ( status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX) ) != FT_OK )
1870 LOG_ERROR("error purging ftd2xx device: %lu", status);
1871 return ERROR_JTAG_INIT_FAILED;
1878 #endif /* BUILD_FT2232_FTD2XX == 1 */
1880 #if BUILD_FT2232_LIBFTDI == 1
1881 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int* try_more)
1885 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1886 ft2232_layout, vid, pid);
1888 if (ftdi_init(&ftdic) < 0)
1889 return ERROR_JTAG_INIT_FAILED;
1891 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1893 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1894 return ERROR_JTAG_INIT_FAILED;
1897 /* context, vendor id, product id */
1898 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1902 LOG_WARNING("unable to open ftdi device (trying more): %s",
1905 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1907 return ERROR_JTAG_INIT_FAILED;
1910 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
1911 if (ftdi_usb_reset(&ftdic) < 0)
1913 LOG_ERROR("unable to reset ftdi device");
1914 return ERROR_JTAG_INIT_FAILED;
1917 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1919 LOG_ERROR("unable to set latency timer");
1920 return ERROR_JTAG_INIT_FAILED;
1923 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1925 LOG_ERROR("unable to get latency timer");
1926 return ERROR_JTAG_INIT_FAILED;
1930 LOG_DEBUG("current latency timer: %i", latency_timer);
1933 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1939 static int ft2232_purge_libftdi(void)
1941 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1943 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1944 return ERROR_JTAG_INIT_FAILED;
1951 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1953 static int ft2232_init(void)
1958 ft2232_layout_t* cur_layout = ft2232_layouts;
1961 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE)==7)
1963 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
1967 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
1970 if ( (ft2232_layout == NULL) || (ft2232_layout[0] == 0) )
1972 ft2232_layout = "usbjtag";
1973 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1976 while (cur_layout->name)
1978 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1980 layout = cur_layout;
1988 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1989 return ERROR_JTAG_INIT_FAILED;
1995 * "more indicates that there are more IDs to try, so we should
1996 * not print an error for an ID mismatch (but for anything
1999 * try_more indicates that the error code returned indicates an
2000 * ID mismatch (and nothing else) and that we should proceeed
2001 * with the next ID pair.
2003 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2006 #if BUILD_FT2232_FTD2XX == 1
2007 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2009 #elif BUILD_FT2232_LIBFTDI == 1
2010 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2015 if (!more || !try_more)
2019 ft2232_buffer_size = 0;
2020 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2022 if (layout->init() != ERROR_OK)
2023 return ERROR_JTAG_INIT_FAILED;
2025 ft2232_speed(jtag_speed);
2027 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2028 if ( ( ( retval = ft2232_write(buf, 1, &bytes_written) ) != ERROR_OK ) || (bytes_written != 1) )
2030 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2031 return ERROR_JTAG_INIT_FAILED;
2034 #if BUILD_FT2232_FTD2XX == 1
2035 return ft2232_purge_ftd2xx();
2036 #elif BUILD_FT2232_LIBFTDI == 1
2037 return ft2232_purge_libftdi();
2044 static int usbjtag_init(void)
2050 low_direction = 0x0b;
2052 if (strcmp(ft2232_layout, "usbjtag") == 0)
2059 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2066 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2073 low_direction = 0x8b;
2077 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2078 return ERROR_JTAG_INIT_FAILED;
2081 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2083 low_direction &= ~nTRSTnOE; /* nTRST input */
2084 low_output &= ~nTRST; /* nTRST = 0 */
2088 low_direction |= nTRSTnOE; /* nTRST output */
2089 low_output |= nTRST; /* nTRST = 1 */
2092 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2094 low_direction |= nSRSTnOE; /* nSRST output */
2095 low_output |= nSRST; /* nSRST = 1 */
2099 low_direction &= ~nSRSTnOE; /* nSRST input */
2100 low_output &= ~nSRST; /* nSRST = 0 */
2103 /* initialize low byte for jtag */
2104 buf[0] = 0x80; /* command "set data bits low byte" */
2105 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
2106 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2107 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2109 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2111 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2112 return ERROR_JTAG_INIT_FAILED;
2119 static int axm0432_jtag_init(void)
2125 low_direction = 0x2b;
2127 /* initialize low byte for jtag */
2128 buf[0] = 0x80; /* command "set data bits low byte" */
2129 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2130 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2131 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2133 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2135 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2136 return ERROR_JTAG_INIT_FAILED;
2139 if (strcmp(layout->name, "axm0432_jtag") == 0)
2142 nTRSTnOE = 0x0; /* No output enable for TRST*/
2144 nSRSTnOE = 0x0; /* No output enable for SRST*/
2148 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2153 high_direction = 0x0c;
2155 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2157 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2161 high_output |= nTRST;
2164 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2166 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2170 high_output |= nSRST;
2173 /* initialize high port */
2174 buf[0] = 0x82; /* command "set data bits high byte" */
2175 buf[1] = high_output; /* value */
2176 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2177 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2179 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2181 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2182 return ERROR_JTAG_INIT_FAILED;
2189 static int jtagkey_init(void)
2195 low_direction = 0x1b;
2197 /* initialize low byte for jtag */
2198 buf[0] = 0x80; /* command "set data bits low byte" */
2199 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2200 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2201 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2203 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2205 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2206 return ERROR_JTAG_INIT_FAILED;
2209 if (strcmp(layout->name, "jtagkey") == 0)
2216 else if ( (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2217 || (strcmp(layout->name, "oocdlink") == 0) )
2226 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2231 high_direction = 0x0f;
2233 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2235 high_output |= nTRSTnOE;
2236 high_output &= ~nTRST;
2240 high_output &= ~nTRSTnOE;
2241 high_output |= nTRST;
2244 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2246 high_output &= ~nSRSTnOE;
2247 high_output |= nSRST;
2251 high_output |= nSRSTnOE;
2252 high_output &= ~nSRST;
2255 /* initialize high port */
2256 buf[0] = 0x82; /* command "set data bits high byte" */
2257 buf[1] = high_output; /* value */
2258 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2261 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2263 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2264 return ERROR_JTAG_INIT_FAILED;
2271 static int olimex_jtag_init(void)
2277 low_direction = 0x1b;
2279 /* initialize low byte for jtag */
2280 buf[0] = 0x80; /* command "set data bits low byte" */
2281 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2282 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2283 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2285 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2287 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2288 return ERROR_JTAG_INIT_FAILED;
2294 nSRSTnOE = 0x00; /* no output enable for nSRST */
2297 high_direction = 0x0f;
2299 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2301 high_output |= nTRSTnOE;
2302 high_output &= ~nTRST;
2306 high_output &= ~nTRSTnOE;
2307 high_output |= nTRST;
2310 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2312 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2316 high_output &= ~nSRST;
2319 /* turn red LED on */
2320 high_output |= 0x08;
2322 /* initialize high port */
2323 buf[0] = 0x82; /* command "set data bits high byte" */
2324 buf[1] = high_output; /* value */
2325 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2326 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2328 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2330 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2331 return ERROR_JTAG_INIT_FAILED;
2338 static int flyswatter_init(void)
2344 low_direction = 0xfb;
2346 /* initialize low byte for jtag */
2347 buf[0] = 0x80; /* command "set data bits low byte" */
2348 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2349 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2350 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2352 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2354 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2355 return ERROR_JTAG_INIT_FAILED;
2359 nTRSTnOE = 0x0; /* not output enable for nTRST */
2361 nSRSTnOE = 0x00; /* no output enable for nSRST */
2364 high_direction = 0x0c;
2366 /* turn red LED3 on, LED2 off */
2367 high_output |= 0x08;
2369 /* initialize high port */
2370 buf[0] = 0x82; /* command "set data bits high byte" */
2371 buf[1] = high_output; /* value */
2372 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2373 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2375 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2377 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2378 return ERROR_JTAG_INIT_FAILED;
2385 static int turtle_init(void)
2391 low_direction = 0x5b;
2393 /* initialize low byte for jtag */
2394 buf[0] = 0x80; /* command "set data bits low byte" */
2395 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2396 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2397 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2399 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2401 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2402 return ERROR_JTAG_INIT_FAILED;
2408 high_direction = 0x0C;
2410 /* initialize high port */
2411 buf[0] = 0x82; /* command "set data bits high byte" */
2412 buf[1] = high_output;
2413 buf[2] = high_direction;
2414 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2416 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2418 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2419 return ERROR_JTAG_INIT_FAILED;
2426 static int comstick_init(void)
2432 low_direction = 0x0b;
2434 /* initialize low byte for jtag */
2435 buf[0] = 0x80; /* command "set data bits low byte" */
2436 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2437 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2438 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2440 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2442 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2443 return ERROR_JTAG_INIT_FAILED;
2447 nTRSTnOE = 0x00; /* no output enable for nTRST */
2449 nSRSTnOE = 0x00; /* no output enable for nSRST */
2452 high_direction = 0x03;
2454 /* initialize high port */
2455 buf[0] = 0x82; /* command "set data bits high byte" */
2456 buf[1] = high_output;
2457 buf[2] = high_direction;
2458 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2460 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2462 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2463 return ERROR_JTAG_INIT_FAILED;
2470 static int stm32stick_init(void)
2476 low_direction = 0x8b;
2478 /* initialize low byte for jtag */
2479 buf[0] = 0x80; /* command "set data bits low byte" */
2480 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2481 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2482 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2484 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2486 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2487 return ERROR_JTAG_INIT_FAILED;
2491 nTRSTnOE = 0x00; /* no output enable for nTRST */
2493 nSRSTnOE = 0x00; /* no output enable for nSRST */
2496 high_direction = 0x03;
2498 /* initialize high port */
2499 buf[0] = 0x82; /* command "set data bits high byte" */
2500 buf[1] = high_output;
2501 buf[2] = high_direction;
2502 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2504 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2506 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2507 return ERROR_JTAG_INIT_FAILED;
2514 static int sheevaplug_init(void)
2520 low_direction = 0x1b;
2522 /* initialize low byte for jtag */
2523 buf[0] = 0x80; /* command "set data bits low byte" */
2524 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2525 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2526 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2528 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2530 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2531 return ERROR_JTAG_INIT_FAILED;
2540 high_direction = 0x0f;
2542 /* nTRST is always push-pull */
2543 high_output &= ~nTRSTnOE;
2544 high_output |= nTRST;
2546 /* nSRST is always open-drain */
2547 high_output |= nSRSTnOE;
2548 high_output &= ~nSRST;
2550 /* initialize high port */
2551 buf[0] = 0x82; /* command "set data bits high byte" */
2552 buf[1] = high_output; /* value */
2553 buf[2] = high_direction; /* all outputs - xRST */
2554 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2556 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2558 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2559 return ERROR_JTAG_INIT_FAILED;
2565 static int cortino_jtag_init(void)
2571 low_direction = 0x1b;
2573 /* initialize low byte for jtag */
2574 buf[0] = 0x80; /* command "set data bits low byte" */
2575 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2576 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2577 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2579 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2581 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2582 return ERROR_JTAG_INIT_FAILED;
2586 nTRSTnOE = 0x00; /* no output enable for nTRST */
2588 nSRSTnOE = 0x00; /* no output enable for nSRST */
2591 high_direction = 0x03;
2593 /* initialize high port */
2594 buf[0] = 0x82; /* command "set data bits high byte" */
2595 buf[1] = high_output;
2596 buf[2] = high_direction;
2597 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2599 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) )
2601 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2602 return ERROR_JTAG_INIT_FAILED;
2608 static void olimex_jtag_blink(void)
2610 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2611 * ACBUS3 is bit 3 of the GPIOH port
2613 if (high_output & 0x08)
2615 /* set port pin high */
2616 high_output &= 0x07;
2620 /* set port pin low */
2621 high_output |= 0x08;
2624 buffer_write( 0x82 );
2625 buffer_write( high_output );
2626 buffer_write( high_direction );
2630 static void flyswatter_jtag_blink(void)
2633 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2635 high_output ^= 0x0c;
2637 buffer_write( 0x82 );
2638 buffer_write( high_output );
2639 buffer_write( high_direction );
2643 static void turtle_jtag_blink(void)
2646 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2648 if (high_output & 0x08)
2657 buffer_write( 0x82 );
2658 buffer_write( high_output );
2659 buffer_write( high_direction );
2663 static int ft2232_quit(void)
2665 #if BUILD_FT2232_FTD2XX == 1
2668 status = FT_Close(ftdih);
2669 #elif BUILD_FT2232_LIBFTDI == 1
2670 ftdi_usb_close(&ftdic);
2672 ftdi_deinit(&ftdic);
2675 free(ft2232_buffer);
2676 ft2232_buffer = NULL;
2682 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2688 ft2232_device_desc = strdup(args[0]);
2689 cp = strchr( ft2232_device_desc, 0 );
2690 // under Win32, the FTD2XX driver appends an "A" to the end
2691 // of the description, this examines the given desc
2692 // and creates the 'missing' _A or non_A variable.
2693 if( (cp[-1] == 'A') && (cp[-2]==' ') ){
2694 // it was, so make this the "A" version.
2695 ft2232_device_desc_A = ft2232_device_desc;
2696 // and *CREATE* the non-A version.
2697 strcpy( buf, ft2232_device_desc );
2698 cp = strchr( buf, 0 );
2700 ft2232_device_desc = strdup( buf );
2702 // <space>A not defined
2704 sprintf( buf, "%s A", ft2232_device_desc );
2705 ft2232_device_desc_A = strdup( buf );
2710 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2717 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2721 ft2232_serial = strdup(args[0]);
2725 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2732 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2737 ft2232_layout = malloc(strlen(args[0]) + 1);
2738 strcpy(ft2232_layout, args[0]);
2744 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2748 if (argc > MAX_USB_IDS * 2)
2750 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2751 "(maximum is %d pairs)", MAX_USB_IDS);
2752 argc = MAX_USB_IDS * 2;
2754 if ( argc < 2 || (argc & 1) )
2756 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2761 for (i = 0; i + 1 < argc; i += 2)
2763 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2764 ft2232_pid[i >> 1] = strtol(args[i + 1], NULL, 0);
2768 * Explicitly terminate, in case there are multiples instances of
2771 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2777 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2781 ft2232_latency = atoi(args[0]);
2785 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2792 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2796 /* 7 bits of either ones or zeros. */
2797 u8 tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2799 while (num_cycles > 0)
2801 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2802 * at most 7 bits per invocation. Here we invoke it potentially
2805 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2807 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2809 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2810 retval = ERROR_JTAG_QUEUE_FAILED;
2815 /* there are no state transitions in this code, so omit state tracking */
2817 /* command "Clock Data to TMS/CS Pin (no Read)" */
2818 buffer_write( 0x4b );
2821 buffer_write( bitcount_per_command - 1 );
2823 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2824 buffer_write( tms );
2828 num_cycles -= bitcount_per_command;
2835 /* ---------------------------------------------------------------------
2836 * Support for IceBear JTAG adapter from Section5:
2837 * http://section5.ch/icebear
2839 * Author: Sten, debian@sansys-electronic.com
2842 /* Icebear pin layout
2844 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2845 * GND GND | 4 3| n.c.
2846 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2847 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2848 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2849 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2850 * ADBUS2 TDO |14 13| GND GND
2852 * ADBUS0 O L TCK ACBUS0 GND
2853 * ADBUS1 O L TDI ACBUS1 GND
2854 * ADBUS2 I TDO ACBUS2 n.c.
2855 * ADBUS3 O H TMS ACBUS3 n.c.
2861 static int icebear_jtag_init(void) {
2865 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2866 low_output = 0x08; /* high: TMS; low: TCK TDI */
2870 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2871 low_direction &= ~nTRST; /* nTRST high impedance */
2874 low_direction |= nTRST;
2875 low_output |= nTRST;
2878 low_direction |= nSRST;
2879 low_output |= nSRST;
2881 /* initialize low byte for jtag */
2882 buf[0] = 0x80; /* command "set data bits low byte" */
2883 buf[1] = low_output;
2884 buf[2] = low_direction;
2885 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2887 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2888 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2889 return ERROR_JTAG_INIT_FAILED;
2893 high_direction = 0x00;
2896 /* initialize high port */
2897 buf[0] = 0x82; /* command "set data bits high byte" */
2898 buf[1] = high_output; /* value */
2899 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2900 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2902 if ( ( ( ft2232_write(buf, 3, &bytes_written) ) != ERROR_OK ) || (bytes_written != 3) ) {
2903 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2904 return ERROR_JTAG_INIT_FAILED;
2910 static void icebear_jtag_reset(int trst, int srst) {
2913 low_direction |= nTRST;
2914 low_output &= ~nTRST;
2916 else if (trst == 0) {
2917 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
2918 low_direction &= ~nTRST;
2920 low_output |= nTRST;
2924 low_output &= ~nSRST;
2926 else if (srst == 0) {
2927 low_output |= nSRST;
2930 /* command "set data bits low byte" */
2931 buffer_write( 0x80 );
2932 buffer_write( low_output );
2933 buffer_write( low_direction );
2935 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);