1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
32 * JTAG adapters based on the FT2232 full and high speed USB parts are
33 * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
34 * are discrete, but development boards may integrate them as alternatives
35 * to more capable (and expensive) third party JTAG pods. Since JTAG uses
36 * only one of the two parts on these devices, on integrated boards the
37 * second port often serves as a USB-to-serial adapter for the target's
38 * console UART even when the JTAG port is not in use. (Systems which
39 * support ARM's SWD in addition to JTAG, or instead of it, may use that
40 * second port for reading SWV trace data.)
42 * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
43 * request/response interactions involve round trips over the USB link.
44 * A "smart" JTAG adapter has intelligence close to the scan chain, so it
45 * can for example poll quickly for a status change (usually taking on the
46 * order of microseconds not milliseconds) before beginning a queued
47 * transaction which require the previous one to have completed.
49 * There are dozens of adapters of this type, differing in details which
50 * this driver needs to understand. Those "layout" details are required
51 * as part of FT2232 driver configuration.
53 * This code uses information contained in the MPSSE specification which was
55 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
56 * Hereafter this is called the "MPSSE Spec".
58 * The datasheet for the ftdichip.com's FT2232D part is here:
59 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
61 * Also note the issue with code 0x4b (clock data to TMS) noted in
62 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
63 * which can affect longer JTAG state paths.
70 /* project specific includes */
71 #include <jtag/interface.h>
72 #include <helper/time_support.h>
80 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
81 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
82 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
83 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
86 /* FT2232 access library includes */
87 #if BUILD_FT2232_FTD2XX == 1
89 #elif BUILD_FT2232_LIBFTDI == 1
93 /* max TCK for the high speed devices 30000 kHz */
94 #define FTDI_2232H_4232H_MAX_TCK 30000
95 /* max TCK for the full speed devices 6000 kHz */
96 #define FTDI_2232C_MAX_TCK 6000
97 /* this speed value tells that RTCK is requested */
101 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
102 * errors with a retry count of 100. Increasing it solves the problem for me.
105 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
106 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
109 #define LIBFTDI_READ_RETRY_COUNT 2000
111 #ifndef BUILD_FT2232_HIGHSPEED
112 #if BUILD_FT2232_FTD2XX == 1
113 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
114 #elif BUILD_FT2232_LIBFTDI == 1
115 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
120 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
121 * stable state. Calling code must ensure that current state is stable,
122 * that verification is not done in here.
124 * @param num_cycles The number of clocks cycles to send.
125 * @param cmd The command to send.
127 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
129 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
131 static char * ft2232_device_desc_A = NULL;
132 static char* ft2232_device_desc = NULL;
133 static char* ft2232_serial = NULL;
134 static char* ft2232_layout = NULL;
135 static uint8_t ft2232_latency = 2;
136 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
138 #define MAX_USB_IDS 8
139 /* vid = pid = 0 marks the end of the list */
140 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
141 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
143 struct ft2232_layout {
146 void (*reset)(int trst, int srst);
150 /* init procedures for supported layouts */
151 static int usbjtag_init(void);
152 static int jtagkey_init(void);
153 static int olimex_jtag_init(void);
154 static int flyswatter_init(void);
155 static int turtle_init(void);
156 static int comstick_init(void);
157 static int stm32stick_init(void);
158 static int axm0432_jtag_init(void);
159 static int sheevaplug_init(void);
160 static int icebear_jtag_init(void);
161 static int cortino_jtag_init(void);
162 static int signalyzer_h_init(void);
163 static int ktlink_init(void);
165 /* reset procedures for supported layouts */
166 static void usbjtag_reset(int trst, int srst);
167 static void jtagkey_reset(int trst, int srst);
168 static void olimex_jtag_reset(int trst, int srst);
169 static void flyswatter_reset(int trst, int srst);
170 static void turtle_reset(int trst, int srst);
171 static void comstick_reset(int trst, int srst);
172 static void stm32stick_reset(int trst, int srst);
173 static void axm0432_jtag_reset(int trst, int srst);
174 static void sheevaplug_reset(int trst, int srst);
175 static void icebear_jtag_reset(int trst, int srst);
176 static void signalyzer_h_reset(int trst, int srst);
177 static void ktlink_reset(int trst, int srst);
179 /* blink procedures for layouts that support a blinking led */
180 static void olimex_jtag_blink(void);
181 static void flyswatter_jtag_blink(void);
182 static void turtle_jtag_blink(void);
183 static void signalyzer_h_blink(void);
184 static void ktlink_blink(void);
186 static const struct ft2232_layout ft2232_layouts[] =
188 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
189 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
190 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
191 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
192 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
193 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
194 { "luminary_icdi", usbjtag_init, usbjtag_reset, NULL },
195 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
196 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
197 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
198 { "comstick", comstick_init, comstick_reset, NULL },
199 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
200 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
201 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
202 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
203 { "cortino", cortino_jtag_init, comstick_reset, NULL },
204 { "signalyzer-h", signalyzer_h_init, signalyzer_h_reset, signalyzer_h_blink },
205 { "ktlink", ktlink_init, ktlink_reset, ktlink_blink },
206 { NULL, NULL, NULL, NULL },
209 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
211 static const struct ft2232_layout *layout;
212 static uint8_t low_output = 0x0;
213 static uint8_t low_direction = 0x0;
214 static uint8_t high_output = 0x0;
215 static uint8_t high_direction = 0x0;
217 #if BUILD_FT2232_FTD2XX == 1
218 static FT_HANDLE ftdih = NULL;
219 static FT_DEVICE ftdi_device = 0;
220 #elif BUILD_FT2232_LIBFTDI == 1
221 static struct ftdi_context ftdic;
222 static enum ftdi_chip_type ftdi_device;
225 static struct jtag_command* first_unsent; /* next command that has to be sent */
226 static int require_send;
228 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
230 "There is a significant difference between libftdi and libftd2xx. The latter
231 one allows to schedule up to 64*64 bytes of result data while libftdi fails
232 with more than 4*64. As a consequence, the FT2232 driver is forced to
233 perform around 16x more USB transactions for long command streams with TDO
234 capture when running with libftdi."
237 #define FT2232_BUFFER_SIZE 131072
238 a comment would have been nice.
241 #define FT2232_BUFFER_SIZE 131072
243 static uint8_t* ft2232_buffer = NULL;
244 static int ft2232_buffer_size = 0;
245 static int ft2232_read_pointer = 0;
246 static int ft2232_expect_read = 0;
249 * Function buffer_write
250 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
251 * @param val is the byte to send.
253 static inline void buffer_write(uint8_t val)
255 assert(ft2232_buffer);
256 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
257 ft2232_buffer[ft2232_buffer_size++] = val;
261 * Function buffer_read
262 * returns a byte from the byte buffer.
264 static inline uint8_t buffer_read(void)
266 assert(ft2232_buffer);
267 assert(ft2232_read_pointer < ft2232_buffer_size);
268 return ft2232_buffer[ft2232_read_pointer++];
272 * Clocks out \a bit_count bits on the TMS line, starting with the least
273 * significant bit of tms_bits and progressing to more significant bits.
274 * Rigorous state transition logging is done here via tap_set_state().
276 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
277 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
278 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
279 * is often used for this, 0x4b.
281 * @param tms_bits Holds the sequence of bits to send.
282 * @param tms_count Tells how many bits in the sequence.
283 * @param tdi_bit A single bit to pass on to TDI before the first TCK
284 * cycle and held static for the duration of TMS clocking.
286 * See the MPSSE spec referenced above.
288 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
292 int tms_ndx; /* bit index into tms_byte */
294 assert(tms_count > 0);
296 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
297 mpsse_cmd, tms_bits, tms_count);
299 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
301 bool bit = tms_bits & 1;
304 tms_byte |= (1 << tms_ndx);
306 /* always do state transitions in public view */
307 tap_set_state(tap_state_transition(tap_get_state(), bit));
309 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
314 if (tms_ndx == 7 || i == tms_count-1)
316 buffer_write(mpsse_cmd);
317 buffer_write(tms_ndx - 1);
319 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
320 TMS/CS and is held static for the duration of TMS/CS clocking.
322 buffer_write(tms_byte | (tdi_bit << 7));
328 * Function get_tms_buffer_requirements
329 * returns what clock_tms() will consume if called with
332 static inline int get_tms_buffer_requirements(int bit_count)
334 return ((bit_count + 6)/7) * 3;
338 * Function move_to_state
339 * moves the TAP controller from the current state to a
340 * \a goal_state through a path given by tap_get_tms_path(). State transition
341 * logging is performed by delegation to clock_tms().
343 * @param goal_state is the destination state for the move.
345 static void move_to_state(tap_state_t goal_state)
347 tap_state_t start_state = tap_get_state();
349 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
350 lookup of the required TMS pattern to move to this state from the
354 /* do the 2 lookups */
355 int tms_bits = tap_get_tms_path(start_state, goal_state);
356 int tms_count = tap_get_tms_path_len(start_state, goal_state);
358 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
360 clock_tms(0x4b, tms_bits, tms_count, 0);
363 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
365 #if BUILD_FT2232_FTD2XX == 1
367 DWORD dw_bytes_written;
368 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
370 *bytes_written = dw_bytes_written;
371 LOG_ERROR("FT_Write returned: %lu", status);
372 return ERROR_JTAG_DEVICE_ERROR;
376 *bytes_written = dw_bytes_written;
379 #elif BUILD_FT2232_LIBFTDI == 1
381 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
384 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
385 return ERROR_JTAG_DEVICE_ERROR;
389 *bytes_written = retval;
395 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
397 #if BUILD_FT2232_FTD2XX == 1
403 while ((*bytes_read < size) && timeout--)
405 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
406 *bytes_read, &dw_bytes_read)) != FT_OK)
409 LOG_ERROR("FT_Read returned: %lu", status);
410 return ERROR_JTAG_DEVICE_ERROR;
412 *bytes_read += dw_bytes_read;
415 #elif BUILD_FT2232_LIBFTDI == 1
417 int timeout = LIBFTDI_READ_RETRY_COUNT;
420 while ((*bytes_read < size) && timeout--)
422 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
425 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
426 return ERROR_JTAG_DEVICE_ERROR;
428 *bytes_read += retval;
433 if (*bytes_read < size)
435 LOG_ERROR("couldn't read enough bytes from "
436 "FT2232 device (%i < %i)",
437 (unsigned)*bytes_read,
439 return ERROR_JTAG_DEVICE_ERROR;
445 static bool ft2232_device_is_highspeed(void)
447 #if BUILD_FT2232_FTD2XX == 1
448 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
449 #elif BUILD_FT2232_LIBFTDI == 1
450 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
455 * Commands that only apply to the FT2232H and FT4232H devices.
456 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
457 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
460 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
462 uint8_t buf = enable ? 0x96 : 0x97;
463 LOG_DEBUG("%2.2x", buf);
465 uint32_t bytes_written;
466 int retval = ft2232_write(&buf, 1, &bytes_written);
467 if ((ERROR_OK != retval) || (bytes_written != 1))
469 LOG_ERROR("couldn't write command to %s adaptive clocking"
470 , enable ? "enable" : "disable");
478 * Enable/disable the clk divide by 5 of the 60MHz master clock.
479 * This result in a JTAG clock speed range of 91.553Hz-6MHz
480 * respective 457.763Hz-30MHz.
482 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
484 uint32_t bytes_written;
485 uint8_t buf = enable ? 0x8b : 0x8a;
486 int retval = ft2232_write(&buf, 1, &bytes_written);
487 if ((ERROR_OK != retval) || (bytes_written != 1))
489 LOG_ERROR("couldn't write command to %s clk divide by 5"
490 , enable ? "enable" : "disable");
491 return ERROR_JTAG_INIT_FAILED;
493 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
494 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
499 static int ft2232_speed(int speed)
503 uint32_t bytes_written;
506 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
507 if (ft2232_device_is_highspeed())
508 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
509 else if (enable_adaptive_clocking)
511 LOG_ERROR("ft2232 device %lu does not support RTCK"
512 , (long unsigned int)ftdi_device);
516 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
519 buf[0] = 0x86; /* command "set divisor" */
520 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
521 buf[2] = (speed >> 8) & 0xff; /* valueH */
523 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
524 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
526 LOG_ERROR("couldn't set FT2232 TCK speed");
533 static int ft2232_speed_div(int speed, int* khz)
535 /* Take a look in the FT2232 manual,
536 * AN2232C-01 Command Processor for
537 * MPSSE and MCU Host Bus. Chapter 3.8 */
539 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
544 static int ft2232_khz(int khz, int* jtag_speed)
548 if (ft2232_device_is_highspeed())
550 *jtag_speed = RTCK_SPEED;
555 LOG_DEBUG("RCLK not supported");
560 /* Take a look in the FT2232 manual,
561 * AN2232C-01 Command Processor for
562 * MPSSE and MCU Host Bus. Chapter 3.8
564 * We will calc here with a multiplier
565 * of 10 for better rounding later. */
567 /* Calc speed, (ft2232_max_tck / khz) - 1 */
568 /* Use 65000 for better rounding */
569 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
571 /* Add 0.9 for rounding */
574 /* Calc real speed */
575 *jtag_speed = *jtag_speed / 10;
577 /* Check if speed is greater than 0 */
583 /* Check max value */
584 if (*jtag_speed > 0xFFFF)
586 *jtag_speed = 0xFFFF;
592 static void ft2232_end_state(tap_state_t state)
594 if (tap_is_state_stable(state))
595 tap_set_end_state(state);
598 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
603 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
605 int num_bytes = (scan_size + 7) / 8;
606 int bits_left = scan_size;
609 while (num_bytes-- > 1)
611 buffer[cur_byte++] = buffer_read();
615 buffer[cur_byte] = 0x0;
617 /* There is one more partial byte left from the clock data in/out instructions */
620 buffer[cur_byte] = buffer_read() >> 1;
622 /* 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 */
623 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
626 static void ft2232_debug_dump_buffer(void)
632 for (i = 0; i < ft2232_buffer_size; i++)
634 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
637 LOG_DEBUG("%s", line);
643 LOG_DEBUG("%s", line);
646 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
648 struct jtag_command* cmd;
653 uint32_t bytes_written = 0;
654 uint32_t bytes_read = 0;
656 #ifdef _DEBUG_USB_IO_
657 struct timeval start, inter, inter2, end;
658 struct timeval d_inter, d_inter2, d_end;
661 #ifdef _DEBUG_USB_COMMS_
662 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
663 ft2232_debug_dump_buffer();
666 #ifdef _DEBUG_USB_IO_
667 gettimeofday(&start, NULL);
670 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
672 LOG_ERROR("couldn't write MPSSE commands to FT2232");
676 #ifdef _DEBUG_USB_IO_
677 gettimeofday(&inter, NULL);
680 if (ft2232_expect_read)
682 /* FIXME this "timeout" is never changed ... */
683 int timeout = LIBFTDI_READ_RETRY_COUNT;
684 ft2232_buffer_size = 0;
686 #ifdef _DEBUG_USB_IO_
687 gettimeofday(&inter2, NULL);
690 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
692 LOG_ERROR("couldn't read from FT2232");
696 #ifdef _DEBUG_USB_IO_
697 gettimeofday(&end, NULL);
699 timeval_subtract(&d_inter, &inter, &start);
700 timeval_subtract(&d_inter2, &inter2, &start);
701 timeval_subtract(&d_end, &end, &start);
703 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
704 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
705 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
706 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
709 ft2232_buffer_size = bytes_read;
711 if (ft2232_expect_read != ft2232_buffer_size)
713 LOG_ERROR("ft2232_expect_read (%i) != "
714 "ft2232_buffer_size (%i) "
718 LIBFTDI_READ_RETRY_COUNT - timeout);
719 ft2232_debug_dump_buffer();
724 #ifdef _DEBUG_USB_COMMS_
725 LOG_DEBUG("read buffer (%i retries): %i bytes",
726 LIBFTDI_READ_RETRY_COUNT - timeout,
728 ft2232_debug_dump_buffer();
732 ft2232_expect_read = 0;
733 ft2232_read_pointer = 0;
735 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
736 * that wasn't handled by a caller-provided error handler
746 type = jtag_scan_type(cmd->cmd.scan);
747 if (type != SCAN_OUT)
749 scan_size = jtag_scan_size(cmd->cmd.scan);
750 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
751 ft2232_read_scan(type, buffer, scan_size);
752 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
753 retval = ERROR_JTAG_QUEUE_FAILED;
765 ft2232_buffer_size = 0;
771 * Function ft2232_add_pathmove
772 * moves the TAP controller from the current state to a new state through the
773 * given path, where path is an array of tap_state_t's.
775 * @param path is an array of tap_stat_t which gives the states to traverse through
776 * ending with the last state at path[num_states-1]
777 * @param num_states is the count of state steps to move through
779 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
783 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
787 /* this loop verifies that the path is legal and logs each state in the path */
790 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
792 int num_states_batch = num_states > 7 ? 7 : num_states;
794 /* command "Clock Data to TMS/CS Pin (no Read)" */
797 /* number of states remaining */
798 buffer_write(num_states_batch - 1);
800 while (num_states_batch--) {
801 /* either TMS=0 or TMS=1 must work ... */
802 if (tap_state_transition(tap_get_state(), false)
803 == path[state_count])
804 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
805 else if (tap_state_transition(tap_get_state(), true)
806 == path[state_count])
807 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
809 /* ... or else the caller goofed BADLY */
811 LOG_ERROR("BUG: %s -> %s isn't a valid "
812 "TAP state transition",
813 tap_state_name(tap_get_state()),
814 tap_state_name(path[state_count]));
818 tap_set_state(path[state_count]);
823 buffer_write(tms_byte);
825 tap_set_end_state(tap_get_state());
828 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
830 int num_bytes = (scan_size + 7) / 8;
831 int bits_left = scan_size;
837 if (tap_get_state() != TAP_DRSHIFT)
839 move_to_state(TAP_DRSHIFT);
844 if (tap_get_state() != TAP_IRSHIFT)
846 move_to_state(TAP_IRSHIFT);
850 /* add command for complete bytes */
851 while (num_bytes > 1)
856 /* Clock Data Bytes In and Out LSB First */
858 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
860 else if (type == SCAN_OUT)
862 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
864 /* LOG_DEBUG("added TDI bytes (o)"); */
866 else if (type == SCAN_IN)
868 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
870 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
873 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
874 num_bytes -= thisrun_bytes;
876 buffer_write((uint8_t) (thisrun_bytes - 1));
877 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
881 /* add complete bytes */
882 while (thisrun_bytes-- > 0)
884 buffer_write(buffer[cur_byte++]);
888 else /* (type == SCAN_IN) */
890 bits_left -= 8 * (thisrun_bytes);
894 /* the most signifcant bit is scanned during TAP movement */
896 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
900 /* process remaining bits but the last one */
905 /* Clock Data Bits In and Out LSB First */
907 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
909 else if (type == SCAN_OUT)
911 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
913 /* LOG_DEBUG("added TDI bits (o)"); */
915 else if (type == SCAN_IN)
917 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
919 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
922 buffer_write(bits_left - 2);
924 buffer_write(buffer[cur_byte]);
927 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
928 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
932 /* Clock Data Bits In and Out LSB First */
934 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
936 else if (type == SCAN_OUT)
938 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
940 /* LOG_DEBUG("added TDI bits (o)"); */
942 else if (type == SCAN_IN)
944 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
946 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
949 buffer_write(last_bit);
957 /* move from Shift-IR/DR to end state */
958 if (type != SCAN_OUT)
960 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
961 /* This must be coordinated with the bit shifts in ft2232_read_scan */
964 /* Clock Data to TMS/CS Pin with Read */
969 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
970 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
971 /* Clock Data to TMS/CS Pin (no Read) */
975 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
976 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
979 if (tap_get_state() != tap_get_end_state())
981 move_to_state(tap_get_end_state());
985 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
987 int num_bytes = (scan_size + 7) / 8;
988 int bits_left = scan_size;
991 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
992 uint8_t* receive_pointer = receive_buffer;
993 uint32_t bytes_written;
996 int thisrun_read = 0;
1000 LOG_ERROR("BUG: large IR scans are not supported");
1004 if (tap_get_state() != TAP_DRSHIFT)
1006 move_to_state(TAP_DRSHIFT);
1009 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1011 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1014 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1015 ft2232_buffer_size, (int)bytes_written);
1016 ft2232_buffer_size = 0;
1018 /* add command for complete bytes */
1019 while (num_bytes > 1)
1023 if (type == SCAN_IO)
1025 /* Clock Data Bytes In and Out LSB First */
1027 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1029 else if (type == SCAN_OUT)
1031 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1033 /* LOG_DEBUG("added TDI bytes (o)"); */
1035 else if (type == SCAN_IN)
1037 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1039 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1042 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1043 thisrun_read = thisrun_bytes;
1044 num_bytes -= thisrun_bytes;
1045 buffer_write((uint8_t) (thisrun_bytes - 1));
1046 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1048 if (type != SCAN_IN)
1050 /* add complete bytes */
1051 while (thisrun_bytes-- > 0)
1053 buffer_write(buffer[cur_byte]);
1058 else /* (type == SCAN_IN) */
1060 bits_left -= 8 * (thisrun_bytes);
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",
1070 (int)bytes_written);
1071 ft2232_buffer_size = 0;
1073 if (type != SCAN_OUT)
1075 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1077 LOG_ERROR("couldn't read from FT2232");
1080 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1083 receive_pointer += bytes_read;
1089 /* the most signifcant bit is scanned during TAP movement */
1090 if (type != SCAN_IN)
1091 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1095 /* process remaining bits but the last one */
1098 if (type == SCAN_IO)
1100 /* Clock Data Bits In and Out LSB First */
1102 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1104 else if (type == SCAN_OUT)
1106 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1108 /* LOG_DEBUG("added TDI bits (o)"); */
1110 else if (type == SCAN_IN)
1112 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1114 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1116 buffer_write(bits_left - 2);
1117 if (type != SCAN_IN)
1118 buffer_write(buffer[cur_byte]);
1120 if (type != SCAN_OUT)
1124 if (tap_get_end_state() == TAP_DRSHIFT)
1126 if (type == SCAN_IO)
1128 /* Clock Data Bits In and Out LSB First */
1130 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1132 else if (type == SCAN_OUT)
1134 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1136 /* LOG_DEBUG("added TDI bits (o)"); */
1138 else if (type == SCAN_IN)
1140 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1142 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1145 buffer_write(last_bit);
1149 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1150 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1153 /* move from Shift-IR/DR to end state */
1154 if (type != SCAN_OUT)
1156 /* Clock Data to TMS/CS Pin with Read */
1158 /* LOG_DEBUG("added TMS scan (read)"); */
1162 /* Clock Data to TMS/CS Pin (no Read) */
1164 /* LOG_DEBUG("added TMS scan (no read)"); */
1167 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1168 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1171 if (type != SCAN_OUT)
1174 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1176 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1179 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1181 (int)bytes_written);
1182 ft2232_buffer_size = 0;
1184 if (type != SCAN_OUT)
1186 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1188 LOG_ERROR("couldn't read from FT2232");
1191 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1194 receive_pointer += bytes_read;
1200 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1202 int predicted_size = 3;
1203 int num_bytes = (scan_size - 1) / 8;
1205 if (tap_get_state() != TAP_DRSHIFT)
1206 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1208 if (type == SCAN_IN) /* only from device to host */
1210 /* complete bytes */
1211 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1213 /* remaining bits - 1 (up to 7) */
1214 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1216 else /* host to device, or bidirectional */
1218 /* complete bytes */
1219 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1221 /* remaining bits -1 (up to 7) */
1222 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1225 return predicted_size;
1228 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1230 int predicted_size = 0;
1232 if (type != SCAN_OUT)
1234 /* complete bytes */
1235 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1237 /* remaining bits - 1 */
1238 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1240 /* last bit (from TMS scan) */
1241 predicted_size += 1;
1244 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1246 return predicted_size;
1249 static void usbjtag_reset(int trst, int srst)
1251 enum reset_types jtag_reset_config = jtag_get_reset_config();
1254 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1255 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1257 low_output &= ~nTRST; /* switch output low */
1261 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1262 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1264 low_output |= nTRST; /* switch output high */
1269 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1270 low_output &= ~nSRST; /* switch output low */
1272 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1276 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1277 low_output |= nSRST; /* switch output high */
1279 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1282 /* command "set data bits low byte" */
1284 buffer_write(low_output);
1285 buffer_write(low_direction);
1288 static void jtagkey_reset(int trst, int srst)
1290 enum reset_types jtag_reset_config = jtag_get_reset_config();
1293 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1294 high_output &= ~nTRSTnOE;
1296 high_output &= ~nTRST;
1300 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1301 high_output |= nTRSTnOE;
1303 high_output |= nTRST;
1308 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1309 high_output &= ~nSRST;
1311 high_output &= ~nSRSTnOE;
1315 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1316 high_output |= nSRST;
1318 high_output |= nSRSTnOE;
1321 /* command "set data bits high byte" */
1323 buffer_write(high_output);
1324 buffer_write(high_direction);
1325 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1329 static void olimex_jtag_reset(int trst, int srst)
1331 enum reset_types jtag_reset_config = jtag_get_reset_config();
1334 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1335 high_output &= ~nTRSTnOE;
1337 high_output &= ~nTRST;
1341 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1342 high_output |= nTRSTnOE;
1344 high_output |= nTRST;
1349 high_output |= nSRST;
1353 high_output &= ~nSRST;
1356 /* command "set data bits high byte" */
1358 buffer_write(high_output);
1359 buffer_write(high_direction);
1360 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1364 static void axm0432_jtag_reset(int trst, int srst)
1368 tap_set_state(TAP_RESET);
1369 high_output &= ~nTRST;
1373 high_output |= nTRST;
1378 high_output &= ~nSRST;
1382 high_output |= nSRST;
1385 /* command "set data bits low byte" */
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,
1393 static void flyswatter_reset(int trst, int srst)
1397 low_output &= ~nTRST;
1401 low_output |= nTRST;
1406 low_output |= nSRST;
1410 low_output &= ~nSRST;
1413 /* command "set data bits low byte" */
1415 buffer_write(low_output);
1416 buffer_write(low_direction);
1417 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1420 static void turtle_reset(int trst, int srst)
1426 low_output |= nSRST;
1430 low_output &= ~nSRST;
1433 /* command "set data bits low byte" */
1435 buffer_write(low_output);
1436 buffer_write(low_direction);
1437 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1440 static void comstick_reset(int trst, int srst)
1444 high_output &= ~nTRST;
1448 high_output |= nTRST;
1453 high_output &= ~nSRST;
1457 high_output |= nSRST;
1460 /* command "set data bits high byte" */
1462 buffer_write(high_output);
1463 buffer_write(high_direction);
1464 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1468 static void stm32stick_reset(int trst, int srst)
1472 high_output &= ~nTRST;
1476 high_output |= nTRST;
1481 low_output &= ~nSRST;
1485 low_output |= nSRST;
1488 /* command "set data bits low byte" */
1490 buffer_write(low_output);
1491 buffer_write(low_direction);
1493 /* command "set data bits high byte" */
1495 buffer_write(high_output);
1496 buffer_write(high_direction);
1497 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1501 static void sheevaplug_reset(int trst, int srst)
1504 high_output &= ~nTRST;
1506 high_output |= nTRST;
1509 high_output &= ~nSRSTnOE;
1511 high_output |= nSRSTnOE;
1513 /* command "set data bits high byte" */
1515 buffer_write(high_output);
1516 buffer_write(high_direction);
1517 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1520 static int ft2232_execute_runtest(struct jtag_command *cmd)
1524 int predicted_size = 0;
1527 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1528 cmd->cmd.runtest->num_cycles,
1529 tap_state_name(cmd->cmd.runtest->end_state));
1531 /* only send the maximum buffer size that FT2232C can handle */
1533 if (tap_get_state() != TAP_IDLE)
1534 predicted_size += 3;
1535 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1536 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1537 predicted_size += 3;
1538 if (tap_get_end_state() != TAP_IDLE)
1539 predicted_size += 3;
1540 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1542 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1543 retval = ERROR_JTAG_QUEUE_FAILED;
1547 if (tap_get_state() != TAP_IDLE)
1549 move_to_state(TAP_IDLE);
1552 i = cmd->cmd.runtest->num_cycles;
1555 /* there are no state transitions in this code, so omit state tracking */
1557 /* command "Clock Data to TMS/CS Pin (no Read)" */
1561 buffer_write((i > 7) ? 6 : (i - 1));
1566 i -= (i > 7) ? 7 : i;
1567 /* LOG_DEBUG("added TMS scan (no read)"); */
1570 ft2232_end_state(cmd->cmd.runtest->end_state);
1572 if (tap_get_state() != tap_get_end_state())
1574 move_to_state(tap_get_end_state());
1578 DEBUG_JTAG_IO("runtest: %i, end in %s",
1579 cmd->cmd.runtest->num_cycles,
1580 tap_state_name(tap_get_end_state()));
1584 static int ft2232_execute_statemove(struct jtag_command *cmd)
1586 int predicted_size = 0;
1587 int retval = ERROR_OK;
1589 DEBUG_JTAG_IO("statemove end in %s",
1590 tap_state_name(cmd->cmd.statemove->end_state));
1592 /* only send the maximum buffer size that FT2232C can handle */
1594 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1596 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1597 retval = ERROR_JTAG_QUEUE_FAILED;
1601 ft2232_end_state(cmd->cmd.statemove->end_state);
1603 /* For TAP_RESET, ignore the current recorded state. It's often
1604 * wrong at server startup, and this transation is critical whenever
1607 if (tap_get_end_state() == TAP_RESET) {
1608 clock_tms(0x4b, 0xff, 5, 0);
1611 /* shortest-path move to desired end state */
1612 } else if (tap_get_state() != tap_get_end_state())
1614 move_to_state(tap_get_end_state());
1621 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1623 int predicted_size = 0;
1624 int retval = ERROR_OK;
1626 tap_state_t* path = cmd->cmd.pathmove->path;
1627 int num_states = cmd->cmd.pathmove->num_states;
1629 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1630 tap_state_name(tap_get_state()),
1631 tap_state_name(path[num_states-1]));
1633 /* only send the maximum buffer size that FT2232C can handle */
1634 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1635 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1637 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1638 retval = ERROR_JTAG_QUEUE_FAILED;
1644 ft2232_add_pathmove(path, num_states);
1650 static int ft2232_execute_scan(struct jtag_command *cmd)
1653 int scan_size; /* size of IR or DR scan */
1654 int predicted_size = 0;
1655 int retval = ERROR_OK;
1657 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1659 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1661 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1663 predicted_size = ft2232_predict_scan_out(scan_size, type);
1664 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1666 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1667 /* unsent commands before this */
1668 if (first_unsent != cmd)
1669 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1670 retval = ERROR_JTAG_QUEUE_FAILED;
1672 /* current command */
1673 ft2232_end_state(cmd->cmd.scan->end_state);
1674 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1676 first_unsent = cmd->next;
1681 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1683 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1686 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1687 retval = ERROR_JTAG_QUEUE_FAILED;
1691 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1692 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1693 ft2232_end_state(cmd->cmd.scan->end_state);
1694 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1698 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1699 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1700 tap_state_name(tap_get_end_state()));
1705 static int ft2232_execute_reset(struct jtag_command *cmd)
1708 int predicted_size = 0;
1711 DEBUG_JTAG_IO("reset trst: %i srst %i",
1712 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1714 /* only send the maximum buffer size that FT2232C can handle */
1716 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1718 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1719 retval = ERROR_JTAG_QUEUE_FAILED;
1724 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1726 tap_set_state(TAP_RESET);
1729 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1732 DEBUG_JTAG_IO("trst: %i, srst: %i",
1733 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1737 static int ft2232_execute_sleep(struct jtag_command *cmd)
1742 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1744 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1745 retval = ERROR_JTAG_QUEUE_FAILED;
1746 first_unsent = cmd->next;
1747 jtag_sleep(cmd->cmd.sleep->us);
1748 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1750 tap_state_name(tap_get_state()));
1754 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1759 /* this is only allowed while in a stable state. A check for a stable
1760 * state was done in jtag_add_clocks()
1762 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1763 retval = ERROR_JTAG_QUEUE_FAILED;
1764 DEBUG_JTAG_IO("clocks %i while in %s",
1765 cmd->cmd.stableclocks->num_cycles,
1766 tap_state_name(tap_get_state()));
1770 static int ft2232_execute_command(struct jtag_command *cmd)
1777 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1778 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1779 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1780 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1781 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1782 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1783 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1785 LOG_ERROR("BUG: unknown JTAG command type encountered");
1791 static int ft2232_execute_queue(void)
1793 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
1796 first_unsent = cmd; /* next command that has to be sent */
1799 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1800 * that wasn't handled by a caller-provided error handler
1804 ft2232_buffer_size = 0;
1805 ft2232_expect_read = 0;
1807 /* blink, if the current layout has that feature */
1813 if (ft2232_execute_command(cmd) != ERROR_OK)
1814 retval = ERROR_JTAG_QUEUE_FAILED;
1815 /* Start reading input before FT2232 TX buffer fills up */
1817 if (ft2232_expect_read > 256)
1819 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1820 retval = ERROR_JTAG_QUEUE_FAILED;
1825 if (require_send > 0)
1826 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1827 retval = ERROR_JTAG_QUEUE_FAILED;
1832 #if BUILD_FT2232_FTD2XX == 1
1833 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1837 char SerialNumber[16];
1838 char Description[64];
1839 DWORD openex_flags = 0;
1840 char* openex_string = NULL;
1841 uint8_t latency_timer;
1843 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1846 /* Add non-standard Vid/Pid to the linux driver */
1847 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1849 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1853 if (ft2232_device_desc && ft2232_serial)
1855 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1856 ft2232_device_desc = NULL;
1859 if (ft2232_device_desc)
1861 openex_string = ft2232_device_desc;
1862 openex_flags = FT_OPEN_BY_DESCRIPTION;
1864 else if (ft2232_serial)
1866 openex_string = ft2232_serial;
1867 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1871 LOG_ERROR("neither device description nor serial number specified");
1872 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1874 return ERROR_JTAG_INIT_FAILED;
1877 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1878 if (status != FT_OK) {
1879 /* under Win32, the FTD2XX driver appends an "A" to the end
1880 * of the description, if we tried by the desc, then
1881 * try by the alternate "A" description. */
1882 if (openex_string == ft2232_device_desc) {
1883 /* Try the alternate method. */
1884 openex_string = ft2232_device_desc_A;
1885 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1886 if (status == FT_OK) {
1887 /* yea, the "alternate" method worked! */
1889 /* drat, give the user a meaningfull message.
1890 * telling the use we tried *BOTH* methods. */
1891 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1893 ft2232_device_desc_A);
1898 if (status != FT_OK)
1904 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1906 return ERROR_JTAG_INIT_FAILED;
1908 LOG_ERROR("unable to open ftdi device: %lu", status);
1909 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1910 if (status == FT_OK)
1912 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1915 for (i = 0; i < num_devices; i++)
1916 desc_array[i] = malloc(64);
1918 desc_array[num_devices] = NULL;
1920 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1922 if (status == FT_OK)
1924 LOG_ERROR("ListDevices: %lu\n", num_devices);
1925 for (i = 0; i < num_devices; i++)
1926 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1929 for (i = 0; i < num_devices; i++)
1930 free(desc_array[i]);
1936 LOG_ERROR("ListDevices: NONE\n");
1938 return ERROR_JTAG_INIT_FAILED;
1941 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1943 LOG_ERROR("unable to set latency timer: %lu", status);
1944 return ERROR_JTAG_INIT_FAILED;
1947 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1949 LOG_ERROR("unable to get latency timer: %lu", status);
1950 return ERROR_JTAG_INIT_FAILED;
1954 LOG_DEBUG("current latency timer: %i", latency_timer);
1957 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1959 LOG_ERROR("unable to set timeouts: %lu", status);
1960 return ERROR_JTAG_INIT_FAILED;
1963 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1965 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1966 return ERROR_JTAG_INIT_FAILED;
1969 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1971 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1972 return ERROR_JTAG_INIT_FAILED;
1976 static const char* type_str[] =
1977 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1978 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
1979 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
1980 ? ftdi_device : FT_DEVICE_UNKNOWN;
1981 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
1982 LOG_INFO("deviceID: %lu", deviceID);
1983 LOG_INFO("SerialNumber: %s", SerialNumber);
1984 LOG_INFO("Description: %s", Description);
1990 static int ft2232_purge_ftd2xx(void)
1994 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1996 LOG_ERROR("error purging ftd2xx device: %lu", status);
1997 return ERROR_JTAG_INIT_FAILED;
2003 #endif /* BUILD_FT2232_FTD2XX == 1 */
2005 #if BUILD_FT2232_LIBFTDI == 1
2006 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
2008 uint8_t latency_timer;
2010 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2011 ft2232_layout, vid, pid);
2013 if (ftdi_init(&ftdic) < 0)
2014 return ERROR_JTAG_INIT_FAILED;
2016 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
2018 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2019 return ERROR_JTAG_INIT_FAILED;
2022 /* context, vendor id, product id */
2023 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2027 LOG_WARNING("unable to open ftdi device (trying more): %s",
2030 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2032 return ERROR_JTAG_INIT_FAILED;
2035 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2036 if (ftdi_usb_reset(&ftdic) < 0)
2038 LOG_ERROR("unable to reset ftdi device");
2039 return ERROR_JTAG_INIT_FAILED;
2042 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2044 LOG_ERROR("unable to set latency timer");
2045 return ERROR_JTAG_INIT_FAILED;
2048 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2050 LOG_ERROR("unable to get latency timer");
2051 return ERROR_JTAG_INIT_FAILED;
2055 LOG_DEBUG("current latency timer: %i", latency_timer);
2058 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2060 ftdi_device = ftdic.type;
2061 static const char* type_str[] =
2062 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2063 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2064 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2065 ? ftdi_device : no_of_known_types;
2066 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2070 static int ft2232_purge_libftdi(void)
2072 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2074 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2075 return ERROR_JTAG_INIT_FAILED;
2081 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2083 static int ft2232_init(void)
2087 uint32_t bytes_written;
2088 const struct ft2232_layout* cur_layout = ft2232_layouts;
2091 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2093 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2097 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2100 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2102 ft2232_layout = "usbjtag";
2103 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2106 while (cur_layout->name)
2108 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2110 layout = cur_layout;
2118 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2119 return ERROR_JTAG_INIT_FAILED;
2125 * "more indicates that there are more IDs to try, so we should
2126 * not print an error for an ID mismatch (but for anything
2129 * try_more indicates that the error code returned indicates an
2130 * ID mismatch (and nothing else) and that we should proceeed
2131 * with the next ID pair.
2133 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2136 #if BUILD_FT2232_FTD2XX == 1
2137 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2139 #elif BUILD_FT2232_LIBFTDI == 1
2140 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2145 if (!more || !try_more)
2149 ft2232_buffer_size = 0;
2150 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2152 if (layout->init() != ERROR_OK)
2153 return ERROR_JTAG_INIT_FAILED;
2155 if (ft2232_device_is_highspeed())
2157 #ifndef BUILD_FT2232_HIGHSPEED
2158 #if BUILD_FT2232_FTD2XX == 1
2159 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2160 #elif BUILD_FT2232_LIBFTDI == 1
2161 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2164 /* make sure the legacy mode is disabled */
2165 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2166 return ERROR_JTAG_INIT_FAILED;
2169 ft2232_speed(jtag_get_speed());
2171 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2172 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2174 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2175 return ERROR_JTAG_INIT_FAILED;
2178 #if BUILD_FT2232_FTD2XX == 1
2179 return ft2232_purge_ftd2xx();
2180 #elif BUILD_FT2232_LIBFTDI == 1
2181 return ft2232_purge_libftdi();
2187 static int usbjtag_init(void)
2190 uint32_t bytes_written;
2193 low_direction = 0x0b;
2195 if (strcmp(ft2232_layout, "usbjtag") == 0)
2202 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2209 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2211 /* There are multiple revisions of LM3S811 eval boards:
2212 * - Rev B (and older?) boards have no SWO trace support.
2213 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2214 * they should use the "luminary_icdi" layout instead.
2221 low_direction = 0x8b;
2223 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2225 /* Most Luminary eval boards support SWO trace output,
2226 * and should use this "luminary_icdi" layout.
2233 low_direction = 0xcb;
2237 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2238 return ERROR_JTAG_INIT_FAILED;
2241 enum reset_types jtag_reset_config = jtag_get_reset_config();
2242 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2244 low_direction &= ~nTRSTnOE; /* nTRST input */
2245 low_output &= ~nTRST; /* nTRST = 0 */
2249 low_direction |= nTRSTnOE; /* nTRST output */
2250 low_output |= nTRST; /* nTRST = 1 */
2253 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2255 low_direction |= nSRSTnOE; /* nSRST output */
2256 low_output |= nSRST; /* nSRST = 1 */
2260 low_direction &= ~nSRSTnOE; /* nSRST input */
2261 low_output &= ~nSRST; /* nSRST = 0 */
2264 /* initialize low byte for jtag */
2265 buf[0] = 0x80; /* command "set data bits low byte" */
2266 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2267 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2268 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2270 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2272 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2273 return ERROR_JTAG_INIT_FAILED;
2279 static int axm0432_jtag_init(void)
2282 uint32_t bytes_written;
2285 low_direction = 0x2b;
2287 /* initialize low byte for jtag */
2288 buf[0] = 0x80; /* command "set data bits low byte" */
2289 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2290 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2291 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2293 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2295 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2296 return ERROR_JTAG_INIT_FAILED;
2299 if (strcmp(layout->name, "axm0432_jtag") == 0)
2302 nTRSTnOE = 0x0; /* No output enable for TRST*/
2304 nSRSTnOE = 0x0; /* No output enable for SRST*/
2308 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2313 high_direction = 0x0c;
2315 enum reset_types jtag_reset_config = jtag_get_reset_config();
2316 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2318 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2322 high_output |= nTRST;
2325 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2327 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2331 high_output |= nSRST;
2334 /* initialize high port */
2335 buf[0] = 0x82; /* command "set data bits high byte" */
2336 buf[1] = high_output; /* value */
2337 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2338 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2340 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2342 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2343 return ERROR_JTAG_INIT_FAILED;
2349 static int jtagkey_init(void)
2352 uint32_t bytes_written;
2355 low_direction = 0x1b;
2357 /* initialize low byte for jtag */
2358 buf[0] = 0x80; /* command "set data bits low byte" */
2359 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2360 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2361 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2363 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2365 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2366 return ERROR_JTAG_INIT_FAILED;
2369 if (strcmp(layout->name, "jtagkey") == 0)
2376 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2377 || (strcmp(layout->name, "oocdlink") == 0))
2386 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2391 high_direction = 0x0f;
2393 enum reset_types jtag_reset_config = jtag_get_reset_config();
2394 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2396 high_output |= nTRSTnOE;
2397 high_output &= ~nTRST;
2401 high_output &= ~nTRSTnOE;
2402 high_output |= nTRST;
2405 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2407 high_output &= ~nSRSTnOE;
2408 high_output |= nSRST;
2412 high_output |= nSRSTnOE;
2413 high_output &= ~nSRST;
2416 /* initialize high port */
2417 buf[0] = 0x82; /* command "set data bits high byte" */
2418 buf[1] = high_output; /* value */
2419 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2420 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2422 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2424 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2425 return ERROR_JTAG_INIT_FAILED;
2431 static int olimex_jtag_init(void)
2434 uint32_t bytes_written;
2437 low_direction = 0x1b;
2439 /* initialize low byte for jtag */
2440 buf[0] = 0x80; /* command "set data bits low byte" */
2441 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2442 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2443 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2445 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2447 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2448 return ERROR_JTAG_INIT_FAILED;
2454 nSRSTnOE = 0x00; /* no output enable for nSRST */
2457 high_direction = 0x0f;
2459 enum reset_types jtag_reset_config = jtag_get_reset_config();
2460 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2462 high_output |= nTRSTnOE;
2463 high_output &= ~nTRST;
2467 high_output &= ~nTRSTnOE;
2468 high_output |= nTRST;
2471 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2473 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2477 high_output &= ~nSRST;
2480 /* turn red LED on */
2481 high_output |= 0x08;
2483 /* initialize high port */
2484 buf[0] = 0x82; /* command "set data bits high byte" */
2485 buf[1] = high_output; /* value */
2486 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2487 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2489 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2491 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2492 return ERROR_JTAG_INIT_FAILED;
2498 static int flyswatter_init(void)
2501 uint32_t bytes_written;
2504 low_direction = 0xfb;
2506 /* initialize low byte for jtag */
2507 buf[0] = 0x80; /* command "set data bits low byte" */
2508 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2509 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2510 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2512 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2514 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2515 return ERROR_JTAG_INIT_FAILED;
2519 nTRSTnOE = 0x0; /* not output enable for nTRST */
2521 nSRSTnOE = 0x00; /* no output enable for nSRST */
2524 high_direction = 0x0c;
2526 /* turn red LED3 on, LED2 off */
2527 high_output |= 0x08;
2529 /* initialize high port */
2530 buf[0] = 0x82; /* command "set data bits high byte" */
2531 buf[1] = high_output; /* value */
2532 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2533 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2535 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2537 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2538 return ERROR_JTAG_INIT_FAILED;
2544 static int turtle_init(void)
2547 uint32_t bytes_written;
2550 low_direction = 0x5b;
2552 /* initialize low byte for jtag */
2553 buf[0] = 0x80; /* command "set data bits low byte" */
2554 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2555 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2556 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2558 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2560 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2561 return ERROR_JTAG_INIT_FAILED;
2567 high_direction = 0x0C;
2569 /* initialize high port */
2570 buf[0] = 0x82; /* command "set data bits high byte" */
2571 buf[1] = high_output;
2572 buf[2] = high_direction;
2573 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2575 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2577 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2578 return ERROR_JTAG_INIT_FAILED;
2584 static int comstick_init(void)
2587 uint32_t bytes_written;
2590 low_direction = 0x0b;
2592 /* initialize low byte for jtag */
2593 buf[0] = 0x80; /* command "set data bits low byte" */
2594 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2595 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2596 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2598 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2600 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2601 return ERROR_JTAG_INIT_FAILED;
2605 nTRSTnOE = 0x00; /* no output enable for nTRST */
2607 nSRSTnOE = 0x00; /* no output enable for nSRST */
2610 high_direction = 0x03;
2612 /* initialize high port */
2613 buf[0] = 0x82; /* command "set data bits high byte" */
2614 buf[1] = high_output;
2615 buf[2] = high_direction;
2616 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2618 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2620 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2621 return ERROR_JTAG_INIT_FAILED;
2627 static int stm32stick_init(void)
2630 uint32_t bytes_written;
2633 low_direction = 0x8b;
2635 /* initialize low byte for jtag */
2636 buf[0] = 0x80; /* command "set data bits low byte" */
2637 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2638 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2639 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2641 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2643 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2644 return ERROR_JTAG_INIT_FAILED;
2648 nTRSTnOE = 0x00; /* no output enable for nTRST */
2650 nSRSTnOE = 0x00; /* no output enable for nSRST */
2653 high_direction = 0x03;
2655 /* initialize high port */
2656 buf[0] = 0x82; /* command "set data bits high byte" */
2657 buf[1] = high_output;
2658 buf[2] = high_direction;
2659 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2661 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2663 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2664 return ERROR_JTAG_INIT_FAILED;
2670 static int sheevaplug_init(void)
2673 uint32_t bytes_written;
2676 low_direction = 0x1b;
2678 /* initialize low byte for jtag */
2679 buf[0] = 0x80; /* command "set data bits low byte" */
2680 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2681 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2682 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2684 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2686 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2687 return ERROR_JTAG_INIT_FAILED;
2696 high_direction = 0x0f;
2698 /* nTRST is always push-pull */
2699 high_output &= ~nTRSTnOE;
2700 high_output |= nTRST;
2702 /* nSRST is always open-drain */
2703 high_output |= nSRSTnOE;
2704 high_output &= ~nSRST;
2706 /* initialize high port */
2707 buf[0] = 0x82; /* command "set data bits high byte" */
2708 buf[1] = high_output; /* value */
2709 buf[2] = high_direction; /* all outputs - xRST */
2710 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2712 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2714 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2715 return ERROR_JTAG_INIT_FAILED;
2721 static int cortino_jtag_init(void)
2724 uint32_t bytes_written;
2727 low_direction = 0x1b;
2729 /* initialize low byte for jtag */
2730 buf[0] = 0x80; /* command "set data bits low byte" */
2731 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2732 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2733 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2735 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2737 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2738 return ERROR_JTAG_INIT_FAILED;
2742 nTRSTnOE = 0x00; /* no output enable for nTRST */
2744 nSRSTnOE = 0x00; /* no output enable for nSRST */
2747 high_direction = 0x03;
2749 /* initialize high port */
2750 buf[0] = 0x82; /* command "set data bits high byte" */
2751 buf[1] = high_output;
2752 buf[2] = high_direction;
2753 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2755 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2757 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2758 return ERROR_JTAG_INIT_FAILED;
2764 static void olimex_jtag_blink(void)
2766 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2767 * ACBUS3 is bit 3 of the GPIOH port
2769 if (high_output & 0x08)
2771 /* set port pin high */
2772 high_output &= 0x07;
2776 /* set port pin low */
2777 high_output |= 0x08;
2781 buffer_write(high_output);
2782 buffer_write(high_direction);
2785 static void flyswatter_jtag_blink(void)
2788 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2790 high_output ^= 0x0c;
2793 buffer_write(high_output);
2794 buffer_write(high_direction);
2797 static void turtle_jtag_blink(void)
2800 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2802 if (high_output & 0x08)
2812 buffer_write(high_output);
2813 buffer_write(high_direction);
2816 static int ft2232_quit(void)
2818 #if BUILD_FT2232_FTD2XX == 1
2821 status = FT_Close(ftdih);
2822 #elif BUILD_FT2232_LIBFTDI == 1
2823 ftdi_usb_close(&ftdic);
2825 ftdi_deinit(&ftdic);
2828 free(ft2232_buffer);
2829 ft2232_buffer = NULL;
2834 COMMAND_HANDLER(ft2232_handle_device_desc_command)
2840 ft2232_device_desc = strdup(CMD_ARGV[0]);
2841 cp = strchr(ft2232_device_desc, 0);
2842 /* under Win32, the FTD2XX driver appends an "A" to the end
2843 * of the description, this examines the given desc
2844 * and creates the 'missing' _A or non_A variable. */
2845 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2846 /* it was, so make this the "A" version. */
2847 ft2232_device_desc_A = ft2232_device_desc;
2848 /* and *CREATE* the non-A version. */
2849 strcpy(buf, ft2232_device_desc);
2850 cp = strchr(buf, 0);
2852 ft2232_device_desc = strdup(buf);
2854 /* <space > A not defined
2856 sprintf(buf, "%s A", ft2232_device_desc);
2857 ft2232_device_desc_A = strdup(buf);
2862 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2868 COMMAND_HANDLER(ft2232_handle_serial_command)
2872 ft2232_serial = strdup(CMD_ARGV[0]);
2876 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2882 COMMAND_HANDLER(ft2232_handle_layout_command)
2887 ft2232_layout = malloc(strlen(CMD_ARGV[0]) + 1);
2888 strcpy(ft2232_layout, CMD_ARGV[0]);
2893 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
2895 if (CMD_ARGC > MAX_USB_IDS * 2)
2897 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2898 "(maximum is %d pairs)", MAX_USB_IDS);
2899 CMD_ARGC = MAX_USB_IDS * 2;
2901 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
2903 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2905 return ERROR_COMMAND_SYNTAX_ERROR;
2906 /* remove the incomplete trailing id */
2911 for (i = 0; i < CMD_ARGC; i += 2)
2913 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
2914 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
2918 * Explicitly terminate, in case there are multiples instances of
2921 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2926 COMMAND_HANDLER(ft2232_handle_latency_command)
2930 ft2232_latency = atoi(CMD_ARGV[0]);
2934 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2940 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
2944 /* 7 bits of either ones or zeros. */
2945 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2947 while (num_cycles > 0)
2949 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2950 * at most 7 bits per invocation. Here we invoke it potentially
2953 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2955 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2957 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2958 retval = ERROR_JTAG_QUEUE_FAILED;
2963 /* there are no state transitions in this code, so omit state tracking */
2965 /* command "Clock Data to TMS/CS Pin (no Read)" */
2969 buffer_write(bitcount_per_command - 1);
2971 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2976 num_cycles -= bitcount_per_command;
2982 /* ---------------------------------------------------------------------
2983 * Support for IceBear JTAG adapter from Section5:
2984 * http://section5.ch/icebear
2986 * Author: Sten, debian@sansys-electronic.com
2989 /* Icebear pin layout
2991 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2992 * GND GND | 4 3| n.c.
2993 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2994 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2995 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2996 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2997 * ADBUS2 TDO |14 13| GND GND
2999 * ADBUS0 O L TCK ACBUS0 GND
3000 * ADBUS1 O L TDI ACBUS1 GND
3001 * ADBUS2 I TDO ACBUS2 n.c.
3002 * ADBUS3 O H TMS ACBUS3 n.c.
3008 static int icebear_jtag_init(void) {
3010 uint32_t bytes_written;
3012 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3013 low_output = 0x08; /* high: TMS; low: TCK TDI */
3017 enum reset_types jtag_reset_config = jtag_get_reset_config();
3018 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3019 low_direction &= ~nTRST; /* nTRST high impedance */
3022 low_direction |= nTRST;
3023 low_output |= nTRST;
3026 low_direction |= nSRST;
3027 low_output |= nSRST;
3029 /* initialize low byte for jtag */
3030 buf[0] = 0x80; /* command "set data bits low byte" */
3031 buf[1] = low_output;
3032 buf[2] = low_direction;
3033 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3035 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3036 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3037 return ERROR_JTAG_INIT_FAILED;
3041 high_direction = 0x00;
3044 /* initialize high port */
3045 buf[0] = 0x82; /* command "set data bits high byte" */
3046 buf[1] = high_output; /* value */
3047 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3048 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3050 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3051 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3052 return ERROR_JTAG_INIT_FAILED;
3058 static void icebear_jtag_reset(int trst, int srst) {
3061 low_direction |= nTRST;
3062 low_output &= ~nTRST;
3064 else if (trst == 0) {
3065 enum reset_types jtag_reset_config = jtag_get_reset_config();
3066 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3067 low_direction &= ~nTRST;
3069 low_output |= nTRST;
3073 low_output &= ~nSRST;
3075 else if (srst == 0) {
3076 low_output |= nSRST;
3079 /* command "set data bits low byte" */
3081 buffer_write(low_output);
3082 buffer_write(low_direction);
3084 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3087 /* ---------------------------------------------------------------------
3088 * Support for Signalyzer H2 and Signalyzer H4
3089 * JTAG adapter from Xverve Technologies Inc.
3090 * http://www.signalyzer.com or http://www.xverve.com
3092 * Author: Oleg Seiljus, oleg@signalyzer.com
3094 static unsigned char signalyzer_h_side;
3095 static unsigned int signalyzer_h_adapter_type;
3097 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3099 #if BUILD_FT2232_FTD2XX == 1
3100 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3103 #define SIGNALYZER_COMMAND_ADDR 128
3104 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3106 #define SIGNALYZER_COMMAND_VERSION 0x41
3107 #define SIGNALYZER_COMMAND_RESET 0x42
3108 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3109 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3110 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3111 #define SIGNALYZER_COMMAND_LED_SET 0x53
3112 #define SIGNALYZER_COMMAND_ADC 0x54
3113 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3114 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3115 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3116 #define SIGNALYZER_COMMAND_I2C 0x58
3118 #define SIGNALYZER_CHAN_A 1
3119 #define SIGNALYZER_CHAN_B 2
3120 /* LEDS use channel C */
3121 #define SIGNALYZER_CHAN_C 4
3123 #define SIGNALYZER_LED_GREEN 1
3124 #define SIGNALYZER_LED_RED 2
3126 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3127 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3128 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3129 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3130 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3133 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3135 #if BUILD_FT2232_FTD2XX == 1
3136 return FT_WriteEE(ftdih, address, value);
3137 #elif BUILD_FT2232_LIBFTDI == 1
3142 #if BUILD_FT2232_FTD2XX == 1
3143 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3145 return FT_ReadEE(ftdih, address, value);
3149 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3150 int on_time_ms, int off_time_ms, unsigned char cycles)
3152 unsigned char on_time;
3153 unsigned char off_time;
3155 if (on_time_ms < 0xFFFF)
3156 on_time = (unsigned char)(on_time_ms / 62);
3160 off_time = (unsigned char)(off_time_ms / 62);
3162 #if BUILD_FT2232_FTD2XX == 1
3165 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3166 ((uint32_t)(channel << 8) | led))) != FT_OK)
3168 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3169 return ERROR_JTAG_DEVICE_ERROR;
3172 if ((status = signalyzer_h_ctrl_write(
3173 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3174 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3176 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3177 return ERROR_JTAG_DEVICE_ERROR;
3180 if ((status = signalyzer_h_ctrl_write(
3181 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3182 ((uint32_t)cycles))) != FT_OK)
3184 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3185 return ERROR_JTAG_DEVICE_ERROR;
3188 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3189 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3191 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3192 return ERROR_JTAG_DEVICE_ERROR;
3196 #elif BUILD_FT2232_LIBFTDI == 1
3199 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3200 ((uint32_t)(channel << 8) | led))) < 0)
3202 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3203 ftdi_get_error_string(&ftdic));
3204 return ERROR_JTAG_DEVICE_ERROR;
3207 if ((retval = signalyzer_h_ctrl_write(
3208 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3209 ((uint32_t)(on_time << 8) | off_time))) < 0)
3211 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3212 ftdi_get_error_string(&ftdic));
3213 return ERROR_JTAG_DEVICE_ERROR;
3216 if ((retval = signalyzer_h_ctrl_write(
3217 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3218 (uint32_t)cycles)) < 0)
3220 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3221 ftdi_get_error_string(&ftdic));
3222 return ERROR_JTAG_DEVICE_ERROR;
3225 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3226 SIGNALYZER_COMMAND_LED_SET)) < 0)
3228 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3229 ftdi_get_error_string(&ftdic));
3230 return ERROR_JTAG_DEVICE_ERROR;
3237 static int signalyzer_h_init(void)
3239 #if BUILD_FT2232_FTD2XX == 1
3246 uint16_t read_buf[12] = { 0 };
3248 uint32_t bytes_written;
3250 /* turn on center green led */
3251 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3252 0xFFFF, 0x00, 0x00);
3254 /* determine what channel config wants to open
3255 * TODO: change me... current implementation is made to work
3256 * with openocd description parsing.
3258 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3262 signalyzer_h_side = *(end_of_desc - 1);
3263 if (signalyzer_h_side == 'B')
3264 signalyzer_h_side = SIGNALYZER_CHAN_B;
3266 signalyzer_h_side = SIGNALYZER_CHAN_A;
3270 LOG_ERROR("No Channel was specified");
3274 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3277 #if BUILD_FT2232_FTD2XX == 1
3278 /* read signalyzer versionining information */
3279 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3280 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3282 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3283 return ERROR_JTAG_DEVICE_ERROR;
3286 for (i = 0; i < 10; i++)
3288 if ((status = signalyzer_h_ctrl_read(
3289 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3290 &read_buf[i])) != FT_OK)
3292 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3294 return ERROR_JTAG_DEVICE_ERROR;
3298 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3299 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3300 read_buf[4], read_buf[5], read_buf[6]);
3302 /* set gpio register */
3303 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3304 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3306 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3307 return ERROR_JTAG_DEVICE_ERROR;
3310 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3313 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3314 return ERROR_JTAG_DEVICE_ERROR;
3317 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3318 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3320 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3321 return ERROR_JTAG_DEVICE_ERROR;
3324 /* read adapter type information */
3325 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3326 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3328 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3329 return ERROR_JTAG_DEVICE_ERROR;
3332 if ((status = signalyzer_h_ctrl_write(
3333 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3335 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3336 return ERROR_JTAG_DEVICE_ERROR;
3339 if ((status = signalyzer_h_ctrl_write(
3340 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3342 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3343 return ERROR_JTAG_DEVICE_ERROR;
3346 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3347 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3349 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3350 return ERROR_JTAG_DEVICE_ERROR;
3355 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3356 &read_buf[0])) != FT_OK)
3358 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3359 return ERROR_JTAG_DEVICE_ERROR;
3362 if (read_buf[0] != 0x0498)
3363 signalyzer_h_adapter_type = 0x0000;
3366 for (i = 0; i < 4; i++)
3368 if ((status = signalyzer_h_ctrl_read(
3369 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3370 &read_buf[i])) != FT_OK)
3372 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3374 return ERROR_JTAG_DEVICE_ERROR;
3378 signalyzer_h_adapter_type = read_buf[0];
3381 #elif BUILD_FT2232_LIBFTDI == 1
3382 /* currently libftdi does not allow reading individual eeprom
3383 * locations, therefore adapter type cannot be detected.
3384 * override with most common type
3386 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3389 enum reset_types jtag_reset_config = jtag_get_reset_config();
3391 /* ADAPTOR: EM_LT16_A */
3392 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3394 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3395 "detected. (HW: %2x).", (read_buf[1] >> 8));
3403 low_direction = 0x1b;
3406 high_direction = 0x0;
3408 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3410 low_direction &= ~nTRSTnOE; /* nTRST input */
3411 low_output &= ~nTRST; /* nTRST = 0 */
3415 low_direction |= nTRSTnOE; /* nTRST output */
3416 low_output |= nTRST; /* nTRST = 1 */
3419 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3421 low_direction |= nSRSTnOE; /* nSRST output */
3422 low_output |= nSRST; /* nSRST = 1 */
3426 low_direction &= ~nSRSTnOE; /* nSRST input */
3427 low_output &= ~nSRST; /* nSRST = 0 */
3430 #if BUILD_FT2232_FTD2XX == 1
3431 /* enable power to the module */
3432 if ((status = signalyzer_h_ctrl_write(
3433 SIGNALYZER_DATA_BUFFER_ADDR,
3434 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3437 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3439 return ERROR_JTAG_DEVICE_ERROR;
3442 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3443 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3445 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3447 return ERROR_JTAG_DEVICE_ERROR;
3450 /* set gpio mode register */
3451 if ((status = signalyzer_h_ctrl_write(
3452 SIGNALYZER_DATA_BUFFER_ADDR,
3453 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3455 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3457 return ERROR_JTAG_DEVICE_ERROR;
3460 if ((status = signalyzer_h_ctrl_write(
3461 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3464 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3466 return ERROR_JTAG_DEVICE_ERROR;
3469 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3470 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3472 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3474 return ERROR_JTAG_DEVICE_ERROR;
3477 /* set gpio register */
3478 if ((status = signalyzer_h_ctrl_write(
3479 SIGNALYZER_DATA_BUFFER_ADDR,
3480 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3482 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3484 return ERROR_JTAG_DEVICE_ERROR;
3487 if ((status = signalyzer_h_ctrl_write(
3488 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3491 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3493 return ERROR_JTAG_DEVICE_ERROR;
3496 if ((status = signalyzer_h_ctrl_write(
3497 SIGNALYZER_COMMAND_ADDR,
3498 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3500 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3502 return ERROR_JTAG_DEVICE_ERROR;
3507 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3508 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3509 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3510 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3511 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3513 if (signalyzer_h_adapter_type
3514 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3515 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3516 "detected. (HW: %2x).", (read_buf[1] >> 8));
3517 else if (signalyzer_h_adapter_type
3518 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3519 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3520 "(ARM JTAG with PSU) detected. (HW: %2x).",
3521 (read_buf[1] >> 8));
3522 else if (signalyzer_h_adapter_type
3523 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3524 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3525 "detected. (HW: %2x).", (read_buf[1] >> 8));
3526 else if (signalyzer_h_adapter_type
3527 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3528 LOG_INFO("Signalyzer: EM-JTAG-P "
3529 "(Generic JTAG with PSU) detected. (HW: %2x).",
3530 (read_buf[1] >> 8));
3538 low_direction = 0x1b;
3541 high_direction = 0x1f;
3543 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3545 high_output |= nTRSTnOE;
3546 high_output &= ~nTRST;
3550 high_output &= ~nTRSTnOE;
3551 high_output |= nTRST;
3554 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3556 high_output &= ~nSRSTnOE;
3557 high_output |= nSRST;
3561 high_output |= nSRSTnOE;
3562 high_output &= ~nSRST;
3565 #if BUILD_FT2232_FTD2XX == 1
3566 /* enable power to the module */
3567 if ((status = signalyzer_h_ctrl_write(
3568 SIGNALYZER_DATA_BUFFER_ADDR,
3569 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3572 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3574 return ERROR_JTAG_DEVICE_ERROR;
3577 if ((status = signalyzer_h_ctrl_write(
3578 SIGNALYZER_COMMAND_ADDR,
3579 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3581 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3583 return ERROR_JTAG_DEVICE_ERROR;
3586 /* set gpio mode register (IO_16 and IO_17 set as analog
3587 * inputs, other is gpio)
3589 if ((status = signalyzer_h_ctrl_write(
3590 SIGNALYZER_DATA_BUFFER_ADDR,
3591 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3593 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3595 return ERROR_JTAG_DEVICE_ERROR;
3598 if ((status = signalyzer_h_ctrl_write(
3599 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3602 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3604 return ERROR_JTAG_DEVICE_ERROR;
3607 if ((status = signalyzer_h_ctrl_write(
3608 SIGNALYZER_COMMAND_ADDR,
3609 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3611 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3613 return ERROR_JTAG_DEVICE_ERROR;
3616 /* set gpio register (all inputs, for -P modules,
3617 * PSU will be turned off)
3619 if ((status = signalyzer_h_ctrl_write(
3620 SIGNALYZER_DATA_BUFFER_ADDR,
3621 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3623 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3625 return ERROR_JTAG_DEVICE_ERROR;
3628 if ((status = signalyzer_h_ctrl_write(
3629 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3632 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3634 return ERROR_JTAG_DEVICE_ERROR;
3637 if ((status = signalyzer_h_ctrl_write(
3638 SIGNALYZER_COMMAND_ADDR,
3639 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3641 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3643 return ERROR_JTAG_DEVICE_ERROR;
3648 else if (signalyzer_h_adapter_type == 0x0000)
3650 LOG_INFO("Signalyzer: No external modules were detected.");
3658 low_direction = 0x1b;
3661 high_direction = 0x0;
3663 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3665 low_direction &= ~nTRSTnOE; /* nTRST input */
3666 low_output &= ~nTRST; /* nTRST = 0 */
3670 low_direction |= nTRSTnOE; /* nTRST output */
3671 low_output |= nTRST; /* nTRST = 1 */
3674 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3676 low_direction |= nSRSTnOE; /* nSRST output */
3677 low_output |= nSRST; /* nSRST = 1 */
3681 low_direction &= ~nSRSTnOE; /* nSRST input */
3682 low_output &= ~nSRST; /* nSRST = 0 */
3687 LOG_ERROR("Unknown module type is detected: %.4x",
3688 signalyzer_h_adapter_type);
3689 return ERROR_JTAG_DEVICE_ERROR;
3692 /* initialize low byte of controller for jtag operation */
3694 buf[1] = low_output;
3695 buf[2] = low_direction;
3697 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
3698 || (bytes_written != 3))
3700 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3701 return ERROR_JTAG_INIT_FAILED;
3704 #if BUILD_FT2232_FTD2XX == 1
3705 if (ftdi_device == FT_DEVICE_2232H)
3707 /* initialize high byte of controller for jtag operation */
3709 buf[1] = high_output;
3710 buf[2] = high_direction;
3712 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3713 || (bytes_written != 3))
3715 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3716 return ERROR_JTAG_INIT_FAILED;
3719 #elif BUILD_FT2232_LIBFTDI == 1
3720 if (ftdi_device == TYPE_2232H)
3722 /* initialize high byte of controller for jtag operation */
3724 buf[1] = high_output;
3725 buf[2] = high_direction;
3727 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3728 || (bytes_written != 3))
3730 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3731 return ERROR_JTAG_INIT_FAILED;
3738 static void signalyzer_h_reset(int trst, int srst)
3740 enum reset_types jtag_reset_config = jtag_get_reset_config();
3742 /* ADAPTOR: EM_LT16_A */
3743 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3747 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3748 /* switch to output pin (output is low) */
3749 low_direction |= nTRSTnOE;
3751 /* switch output low */
3752 low_output &= ~nTRST;
3756 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3757 /* switch to input pin (high-Z + internal
3758 * and external pullup) */
3759 low_direction &= ~nTRSTnOE;
3761 /* switch output high */
3762 low_output |= nTRST;
3767 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3768 /* switch output low */
3769 low_output &= ~nSRST;
3771 /* switch to output pin (output is low) */
3772 low_direction |= nSRSTnOE;
3776 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3777 /* switch output high */
3778 low_output |= nSRST;
3780 /* switch to input pin (high-Z) */
3781 low_direction &= ~nSRSTnOE;
3784 /* command "set data bits low byte" */
3786 buffer_write(low_output);
3787 buffer_write(low_direction);
3788 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3789 "low_direction: 0x%2.2x",
3790 trst, srst, low_output, low_direction);
3792 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3793 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3794 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3795 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3796 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3800 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3801 high_output &= ~nTRSTnOE;
3803 high_output &= ~nTRST;
3807 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3808 high_output |= nTRSTnOE;
3810 high_output |= nTRST;
3815 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3816 high_output &= ~nSRST;
3818 high_output &= ~nSRSTnOE;
3822 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3823 high_output |= nSRST;
3825 high_output |= nSRSTnOE;
3828 /* command "set data bits high byte" */
3830 buffer_write(high_output);
3831 buffer_write(high_direction);
3832 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
3833 "high_direction: 0x%2.2x",
3834 trst, srst, high_output, high_direction);
3836 else if (signalyzer_h_adapter_type == 0x0000)
3840 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3841 /* switch to output pin (output is low) */
3842 low_direction |= nTRSTnOE;
3844 /* switch output low */
3845 low_output &= ~nTRST;
3849 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3850 /* switch to input pin (high-Z + internal
3851 * and external pullup) */
3852 low_direction &= ~nTRSTnOE;
3854 /* switch output high */
3855 low_output |= nTRST;
3860 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3861 /* switch output low */
3862 low_output &= ~nSRST;
3864 /* switch to output pin (output is low) */
3865 low_direction |= nSRSTnOE;
3869 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3870 /* switch output high */
3871 low_output |= nSRST;
3873 /* switch to input pin (high-Z) */
3874 low_direction &= ~nSRSTnOE;
3877 /* command "set data bits low byte" */
3879 buffer_write(low_output);
3880 buffer_write(low_direction);
3881 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3882 "low_direction: 0x%2.2x",
3883 trst, srst, low_output, low_direction);
3887 static void signalyzer_h_blink(void)
3889 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
3892 /********************************************************************
3893 * Support for KT-LINK
3894 * JTAG adapter from KRISTECH
3895 * http://www.kristech.eu
3896 *******************************************************************/
3897 static int ktlink_init(void)
3900 uint32_t bytes_written;
3901 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
3903 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
3904 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
3906 // initialize low port
3907 buf[0] = 0x80; // command "set data bits low byte"
3908 buf[1] = low_output;
3909 buf[2] = low_direction;
3910 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3912 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3914 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3915 return ERROR_JTAG_INIT_FAILED;
3923 high_output = 0x80; // turn LED on
3924 high_direction = 0xFF; // all outputs
3926 enum reset_types jtag_reset_config = jtag_get_reset_config();
3928 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3929 high_output |= nTRSTnOE;
3930 high_output &= ~nTRST;
3932 high_output &= ~nTRSTnOE;
3933 high_output |= nTRST;
3936 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3937 high_output &= ~nSRSTnOE;
3938 high_output |= nSRST;
3940 high_output |= nSRSTnOE;
3941 high_output &= ~nSRST;
3944 // initialize high port
3945 buf[0] = 0x82; // command "set data bits high byte"
3946 buf[1] = high_output; // value
3947 buf[2] = high_direction;
3948 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3950 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3952 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3953 return ERROR_JTAG_INIT_FAILED;
3959 static void ktlink_reset(int trst, int srst)
3961 enum reset_types jtag_reset_config = jtag_get_reset_config();
3964 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3965 high_output &= ~nTRSTnOE;
3967 high_output &= ~nTRST;
3968 } else if (trst == 0) {
3969 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3970 high_output |= nTRSTnOE;
3972 high_output |= nTRST;
3976 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3977 high_output &= ~nSRST;
3979 high_output &= ~nSRSTnOE;
3980 } else if (srst == 0) {
3981 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3982 high_output |= nSRST;
3984 high_output |= nSRSTnOE;
3987 buffer_write(0x82); // command "set data bits high byte"
3988 buffer_write(high_output);
3989 buffer_write(high_direction);
3990 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
3993 static void ktlink_blink(void)
3995 /* LED connected to ACBUS7 */
3996 if (high_output & 0x80)
3997 high_output &= 0x7F;
3999 high_output |= 0x80;
4001 buffer_write(0x82); // command "set data bits high byte"
4002 buffer_write(high_output);
4003 buffer_write(high_direction);
4006 static const struct command_registration ft2232_command_handlers[] = {
4008 .name = "ft2232_device_desc",
4009 .handler = &ft2232_handle_device_desc_command,
4010 .mode = COMMAND_CONFIG,
4011 .help = "set the USB device description of the FTDI FT2232 device",
4012 .usage = "description_string",
4015 .name = "ft2232_serial",
4016 .handler = &ft2232_handle_serial_command,
4017 .mode = COMMAND_CONFIG,
4018 .help = "set the serial number of the FTDI FT2232 device",
4019 .usage = "serial_string",
4022 .name = "ft2232_layout",
4023 .handler = &ft2232_handle_layout_command,
4024 .mode = COMMAND_CONFIG,
4025 .help = "set the layout of the FT2232 GPIO signals used "
4026 "to control output-enables and reset signals",
4027 .usage = "layout_name",
4030 .name = "ft2232_vid_pid",
4031 .handler = &ft2232_handle_vid_pid_command,
4032 .mode = COMMAND_CONFIG,
4033 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4034 .usage = "(vid pid)* ",
4037 .name = "ft2232_latency",
4038 .handler = &ft2232_handle_latency_command,
4039 .mode = COMMAND_CONFIG,
4040 .help = "set the FT2232 latency timer to a new value",
4043 COMMAND_REGISTRATION_DONE
4046 struct jtag_interface ft2232_interface = {
4048 .commands = ft2232_command_handlers,
4050 .init = ft2232_init,
4051 .quit = ft2232_quit,
4052 .speed = ft2232_speed,
4053 .speed_div = ft2232_speed_div,
4055 .execute_queue = ft2232_execute_queue,