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 ports 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
99 #elif BUILD_FT2232_LIBFTDI == 1
103 /* max TCK for the high speed devices 30000 kHz */
104 #define FTDI_2232H_4232H_MAX_TCK 30000
105 /* max TCK for the full speed devices 6000 kHz */
106 #define FTDI_2232C_MAX_TCK 6000
107 /* this speed value tells that RTCK is requested */
108 #define RTCK_SPEED -1
111 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
112 * errors with a retry count of 100. Increasing it solves the problem for me.
115 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
116 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
119 #define LIBFTDI_READ_RETRY_COUNT 2000
121 #ifndef BUILD_FT2232_HIGHSPEED
122 #if BUILD_FT2232_FTD2XX == 1
123 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
124 #elif BUILD_FT2232_LIBFTDI == 1
125 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
130 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
131 * stable state. Calling code must ensure that current state is stable,
132 * that verification is not done in here.
134 * @param num_cycles The number of clocks cycles to send.
135 * @param cmd The command to send.
137 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
139 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
141 static char * ft2232_device_desc_A = NULL;
142 static char* ft2232_device_desc = NULL;
143 static char* ft2232_serial = NULL;
144 static char* ft2232_layout = NULL;
145 static uint8_t ft2232_latency = 2;
146 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
148 #define MAX_USB_IDS 8
149 /* vid = pid = 0 marks the end of the list */
150 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
151 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
153 struct ft2232_layout {
156 void (*reset)(int trst, int srst);
161 /* init procedures for supported layouts */
162 static int usbjtag_init(void);
163 static int jtagkey_init(void);
164 static int olimex_jtag_init(void);
165 static int flyswatter_init(void);
166 static int turtle_init(void);
167 static int comstick_init(void);
168 static int stm32stick_init(void);
169 static int axm0432_jtag_init(void);
170 static int sheevaplug_init(void);
171 static int icebear_jtag_init(void);
172 static int cortino_jtag_init(void);
173 static int signalyzer_h_init(void);
174 static int ktlink_init(void);
175 static int redbee_init(void);
177 /* reset procedures for supported layouts */
178 static void usbjtag_reset(int trst, int srst);
179 static void jtagkey_reset(int trst, int srst);
180 static void olimex_jtag_reset(int trst, int srst);
181 static void flyswatter_reset(int trst, int srst);
182 static void turtle_reset(int trst, int srst);
183 static void comstick_reset(int trst, int srst);
184 static void stm32stick_reset(int trst, int srst);
185 static void axm0432_jtag_reset(int trst, int srst);
186 static void sheevaplug_reset(int trst, int srst);
187 static void icebear_jtag_reset(int trst, int srst);
188 static void signalyzer_h_reset(int trst, int srst);
189 static void ktlink_reset(int trst, int srst);
190 static void redbee_reset(int trst, int srst);
192 /* blink procedures for layouts that support a blinking led */
193 static void olimex_jtag_blink(void);
194 static void flyswatter_jtag_blink(void);
195 static void turtle_jtag_blink(void);
196 static void signalyzer_h_blink(void);
197 static void ktlink_blink(void);
199 static const struct ft2232_layout ft2232_layouts[] =
202 .init = usbjtag_init,
203 .reset = usbjtag_reset,
206 .init = jtagkey_init,
207 .reset = jtagkey_reset,
209 { .name = "jtagkey_prototype_v1",
210 .init = jtagkey_init,
211 .reset = jtagkey_reset,
213 { .name = "oocdlink",
214 .init = jtagkey_init,
215 .reset = jtagkey_reset,
217 { .name = "signalyzer",
218 .init = usbjtag_init,
219 .reset = usbjtag_reset,
221 { .name = "evb_lm3s811",
222 .init = usbjtag_init,
223 .reset = usbjtag_reset,
225 { .name = "luminary_icdi",
226 .init = usbjtag_init,
227 .reset = usbjtag_reset,
229 { .name = "olimex-jtag",
230 .init = olimex_jtag_init,
231 .reset = olimex_jtag_reset,
232 .blink = olimex_jtag_blink
234 { .name = "flyswatter",
235 .init = flyswatter_init,
236 .reset = flyswatter_reset,
237 .blink = flyswatter_jtag_blink
239 { .name = "turtelizer2",
241 .reset = turtle_reset,
242 .blink = turtle_jtag_blink
244 { .name = "comstick",
245 .init = comstick_init,
246 .reset = comstick_reset,
248 { .name = "stm32stick",
249 .init = stm32stick_init,
250 .reset = stm32stick_reset,
252 { .name = "axm0432_jtag",
253 .init = axm0432_jtag_init,
254 .reset = axm0432_jtag_reset,
256 { .name = "sheevaplug",
257 .init = sheevaplug_init,
258 .reset = sheevaplug_reset,
261 .init = icebear_jtag_init,
262 .reset = icebear_jtag_reset,
265 .init = cortino_jtag_init,
266 .reset = comstick_reset,
268 { .name = "signalyzer-h",
269 .init = signalyzer_h_init,
270 .reset = signalyzer_h_reset,
271 .blink = signalyzer_h_blink
275 .reset = ktlink_reset,
276 .blink = ktlink_blink
278 { .name = "redbee-econotag",
280 .reset = redbee_reset,
282 { .name = "redbee-usb",
284 .reset = redbee_reset,
285 .channel = INTERFACE_B,
287 { .name = NULL, /* END OF TABLE */ },
290 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
292 static const struct ft2232_layout *layout;
293 static uint8_t low_output = 0x0;
294 static uint8_t low_direction = 0x0;
295 static uint8_t high_output = 0x0;
296 static uint8_t high_direction = 0x0;
298 #if BUILD_FT2232_FTD2XX == 1
299 static FT_HANDLE ftdih = NULL;
300 static FT_DEVICE ftdi_device = 0;
301 #elif BUILD_FT2232_LIBFTDI == 1
302 static struct ftdi_context ftdic;
303 static enum ftdi_chip_type ftdi_device;
306 static struct jtag_command* first_unsent; /* next command that has to be sent */
307 static int require_send;
309 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
311 "There is a significant difference between libftdi and libftd2xx. The latter
312 one allows to schedule up to 64*64 bytes of result data while libftdi fails
313 with more than 4*64. As a consequence, the FT2232 driver is forced to
314 perform around 16x more USB transactions for long command streams with TDO
315 capture when running with libftdi."
318 #define FT2232_BUFFER_SIZE 131072
319 a comment would have been nice.
322 #define FT2232_BUFFER_SIZE 131072
324 static uint8_t* ft2232_buffer = NULL;
325 static int ft2232_buffer_size = 0;
326 static int ft2232_read_pointer = 0;
327 static int ft2232_expect_read = 0;
330 * Function buffer_write
331 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
332 * @param val is the byte to send.
334 static inline void buffer_write(uint8_t val)
336 assert(ft2232_buffer);
337 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
338 ft2232_buffer[ft2232_buffer_size++] = val;
342 * Function buffer_read
343 * returns a byte from the byte buffer.
345 static inline uint8_t buffer_read(void)
347 assert(ft2232_buffer);
348 assert(ft2232_read_pointer < ft2232_buffer_size);
349 return ft2232_buffer[ft2232_read_pointer++];
353 * Clocks out \a bit_count bits on the TMS line, starting with the least
354 * significant bit of tms_bits and progressing to more significant bits.
355 * Rigorous state transition logging is done here via tap_set_state().
357 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
358 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
359 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
360 * is often used for this, 0x4b.
362 * @param tms_bits Holds the sequence of bits to send.
363 * @param tms_count Tells how many bits in the sequence.
364 * @param tdi_bit A single bit to pass on to TDI before the first TCK
365 * cycle and held static for the duration of TMS clocking.
367 * See the MPSSE spec referenced above.
369 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
373 int tms_ndx; /* bit index into tms_byte */
375 assert(tms_count > 0);
377 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
378 mpsse_cmd, tms_bits, tms_count);
380 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
382 bool bit = tms_bits & 1;
385 tms_byte |= (1 << tms_ndx);
387 /* always do state transitions in public view */
388 tap_set_state(tap_state_transition(tap_get_state(), bit));
390 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
395 if (tms_ndx == 7 || i == tms_count-1)
397 buffer_write(mpsse_cmd);
398 buffer_write(tms_ndx - 1);
400 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
401 TMS/CS and is held static for the duration of TMS/CS clocking.
403 buffer_write(tms_byte | (tdi_bit << 7));
409 * Function get_tms_buffer_requirements
410 * returns what clock_tms() will consume if called with
413 static inline int get_tms_buffer_requirements(int bit_count)
415 return ((bit_count + 6)/7) * 3;
419 * Function move_to_state
420 * moves the TAP controller from the current state to a
421 * \a goal_state through a path given by tap_get_tms_path(). State transition
422 * logging is performed by delegation to clock_tms().
424 * @param goal_state is the destination state for the move.
426 static void move_to_state(tap_state_t goal_state)
428 tap_state_t start_state = tap_get_state();
430 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
431 lookup of the required TMS pattern to move to this state from the
435 /* do the 2 lookups */
436 int tms_bits = tap_get_tms_path(start_state, goal_state);
437 int tms_count = tap_get_tms_path_len(start_state, goal_state);
439 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
441 clock_tms(0x4b, tms_bits, tms_count, 0);
444 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
446 #if BUILD_FT2232_FTD2XX == 1
448 DWORD dw_bytes_written;
449 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
451 *bytes_written = dw_bytes_written;
452 LOG_ERROR("FT_Write returned: %lu", status);
453 return ERROR_JTAG_DEVICE_ERROR;
457 *bytes_written = dw_bytes_written;
460 #elif BUILD_FT2232_LIBFTDI == 1
462 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
465 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
466 return ERROR_JTAG_DEVICE_ERROR;
470 *bytes_written = retval;
476 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
478 #if BUILD_FT2232_FTD2XX == 1
484 while ((*bytes_read < size) && timeout--)
486 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
487 *bytes_read, &dw_bytes_read)) != FT_OK)
490 LOG_ERROR("FT_Read returned: %lu", status);
491 return ERROR_JTAG_DEVICE_ERROR;
493 *bytes_read += dw_bytes_read;
496 #elif BUILD_FT2232_LIBFTDI == 1
498 int timeout = LIBFTDI_READ_RETRY_COUNT;
501 while ((*bytes_read < size) && timeout--)
503 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
506 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
507 return ERROR_JTAG_DEVICE_ERROR;
509 *bytes_read += retval;
514 if (*bytes_read < size)
516 LOG_ERROR("couldn't read enough bytes from "
517 "FT2232 device (%i < %i)",
518 (unsigned)*bytes_read,
520 return ERROR_JTAG_DEVICE_ERROR;
526 static bool ft2232_device_is_highspeed(void)
528 #if BUILD_FT2232_FTD2XX == 1
529 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
530 #elif BUILD_FT2232_LIBFTDI == 1
531 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
536 * Commands that only apply to the FT2232H and FT4232H devices.
537 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
538 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
541 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
543 uint8_t buf = enable ? 0x96 : 0x97;
544 LOG_DEBUG("%2.2x", buf);
546 uint32_t bytes_written;
547 int retval = ft2232_write(&buf, 1, &bytes_written);
548 if ((ERROR_OK != retval) || (bytes_written != 1))
550 LOG_ERROR("couldn't write command to %s adaptive clocking"
551 , enable ? "enable" : "disable");
559 * Enable/disable the clk divide by 5 of the 60MHz master clock.
560 * This result in a JTAG clock speed range of 91.553Hz-6MHz
561 * respective 457.763Hz-30MHz.
563 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
565 uint32_t bytes_written;
566 uint8_t buf = enable ? 0x8b : 0x8a;
567 int retval = ft2232_write(&buf, 1, &bytes_written);
568 if ((ERROR_OK != retval) || (bytes_written != 1))
570 LOG_ERROR("couldn't write command to %s clk divide by 5"
571 , enable ? "enable" : "disable");
572 return ERROR_JTAG_INIT_FAILED;
574 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
575 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
580 static int ft2232_speed(int speed)
584 uint32_t bytes_written;
587 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
588 if (ft2232_device_is_highspeed())
589 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
590 else if (enable_adaptive_clocking)
592 LOG_ERROR("ft2232 device %lu does not support RTCK"
593 , (long unsigned int)ftdi_device);
597 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
600 buf[0] = 0x86; /* command "set divisor" */
601 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
602 buf[2] = (speed >> 8) & 0xff; /* valueH */
604 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
605 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
607 LOG_ERROR("couldn't set FT2232 TCK speed");
614 static int ft2232_speed_div(int speed, int* khz)
616 /* Take a look in the FT2232 manual,
617 * AN2232C-01 Command Processor for
618 * MPSSE and MCU Host Bus. Chapter 3.8 */
620 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
625 static int ft2232_khz(int khz, int* jtag_speed)
629 if (ft2232_device_is_highspeed())
631 *jtag_speed = RTCK_SPEED;
636 LOG_DEBUG("RCLK not supported");
641 /* Take a look in the FT2232 manual,
642 * AN2232C-01 Command Processor for
643 * MPSSE and MCU Host Bus. Chapter 3.8
645 * We will calc here with a multiplier
646 * of 10 for better rounding later. */
648 /* Calc speed, (ft2232_max_tck / khz) - 1 */
649 /* Use 65000 for better rounding */
650 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
652 /* Add 0.9 for rounding */
655 /* Calc real speed */
656 *jtag_speed = *jtag_speed / 10;
658 /* Check if speed is greater than 0 */
664 /* Check max value */
665 if (*jtag_speed > 0xFFFF)
667 *jtag_speed = 0xFFFF;
673 static void ft2232_end_state(tap_state_t state)
675 if (tap_is_state_stable(state))
676 tap_set_end_state(state);
679 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
684 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
686 int num_bytes = (scan_size + 7) / 8;
687 int bits_left = scan_size;
690 while (num_bytes-- > 1)
692 buffer[cur_byte++] = buffer_read();
696 buffer[cur_byte] = 0x0;
698 /* There is one more partial byte left from the clock data in/out instructions */
701 buffer[cur_byte] = buffer_read() >> 1;
703 /* 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 */
704 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
707 static void ft2232_debug_dump_buffer(void)
713 for (i = 0; i < ft2232_buffer_size; i++)
715 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
718 LOG_DEBUG("%s", line);
724 LOG_DEBUG("%s", line);
727 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
729 struct jtag_command* cmd;
734 uint32_t bytes_written = 0;
735 uint32_t bytes_read = 0;
737 #ifdef _DEBUG_USB_IO_
738 struct timeval start, inter, inter2, end;
739 struct timeval d_inter, d_inter2, d_end;
742 #ifdef _DEBUG_USB_COMMS_
743 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
744 ft2232_debug_dump_buffer();
747 #ifdef _DEBUG_USB_IO_
748 gettimeofday(&start, NULL);
751 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
753 LOG_ERROR("couldn't write MPSSE commands to FT2232");
757 #ifdef _DEBUG_USB_IO_
758 gettimeofday(&inter, NULL);
761 if (ft2232_expect_read)
763 /* FIXME this "timeout" is never changed ... */
764 int timeout = LIBFTDI_READ_RETRY_COUNT;
765 ft2232_buffer_size = 0;
767 #ifdef _DEBUG_USB_IO_
768 gettimeofday(&inter2, NULL);
771 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
773 LOG_ERROR("couldn't read from FT2232");
777 #ifdef _DEBUG_USB_IO_
778 gettimeofday(&end, NULL);
780 timeval_subtract(&d_inter, &inter, &start);
781 timeval_subtract(&d_inter2, &inter2, &start);
782 timeval_subtract(&d_end, &end, &start);
784 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
785 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
786 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
787 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
790 ft2232_buffer_size = bytes_read;
792 if (ft2232_expect_read != ft2232_buffer_size)
794 LOG_ERROR("ft2232_expect_read (%i) != "
795 "ft2232_buffer_size (%i) "
799 LIBFTDI_READ_RETRY_COUNT - timeout);
800 ft2232_debug_dump_buffer();
805 #ifdef _DEBUG_USB_COMMS_
806 LOG_DEBUG("read buffer (%i retries): %i bytes",
807 LIBFTDI_READ_RETRY_COUNT - timeout,
809 ft2232_debug_dump_buffer();
813 ft2232_expect_read = 0;
814 ft2232_read_pointer = 0;
816 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
817 * that wasn't handled by a caller-provided error handler
827 type = jtag_scan_type(cmd->cmd.scan);
828 if (type != SCAN_OUT)
830 scan_size = jtag_scan_size(cmd->cmd.scan);
831 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
832 ft2232_read_scan(type, buffer, scan_size);
833 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
834 retval = ERROR_JTAG_QUEUE_FAILED;
846 ft2232_buffer_size = 0;
852 * Function ft2232_add_pathmove
853 * moves the TAP controller from the current state to a new state through the
854 * given path, where path is an array of tap_state_t's.
856 * @param path is an array of tap_stat_t which gives the states to traverse through
857 * ending with the last state at path[num_states-1]
858 * @param num_states is the count of state steps to move through
860 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
864 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
868 /* this loop verifies that the path is legal and logs each state in the path */
871 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
873 int num_states_batch = num_states > 7 ? 7 : num_states;
875 /* command "Clock Data to TMS/CS Pin (no Read)" */
878 /* number of states remaining */
879 buffer_write(num_states_batch - 1);
881 while (num_states_batch--) {
882 /* either TMS=0 or TMS=1 must work ... */
883 if (tap_state_transition(tap_get_state(), false)
884 == path[state_count])
885 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
886 else if (tap_state_transition(tap_get_state(), true)
887 == path[state_count])
888 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
890 /* ... or else the caller goofed BADLY */
892 LOG_ERROR("BUG: %s -> %s isn't a valid "
893 "TAP state transition",
894 tap_state_name(tap_get_state()),
895 tap_state_name(path[state_count]));
899 tap_set_state(path[state_count]);
904 buffer_write(tms_byte);
906 tap_set_end_state(tap_get_state());
909 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
911 int num_bytes = (scan_size + 7) / 8;
912 int bits_left = scan_size;
918 if (tap_get_state() != TAP_DRSHIFT)
920 move_to_state(TAP_DRSHIFT);
925 if (tap_get_state() != TAP_IRSHIFT)
927 move_to_state(TAP_IRSHIFT);
931 /* add command for complete bytes */
932 while (num_bytes > 1)
937 /* Clock Data Bytes In and Out LSB First */
939 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
941 else if (type == SCAN_OUT)
943 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
945 /* LOG_DEBUG("added TDI bytes (o)"); */
947 else if (type == SCAN_IN)
949 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
951 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
954 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
955 num_bytes -= thisrun_bytes;
957 buffer_write((uint8_t) (thisrun_bytes - 1));
958 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
962 /* add complete bytes */
963 while (thisrun_bytes-- > 0)
965 buffer_write(buffer[cur_byte++]);
969 else /* (type == SCAN_IN) */
971 bits_left -= 8 * (thisrun_bytes);
975 /* the most signifcant bit is scanned during TAP movement */
977 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
981 /* process remaining bits but the last one */
986 /* Clock Data Bits In and Out LSB First */
988 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
990 else if (type == SCAN_OUT)
992 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
994 /* LOG_DEBUG("added TDI bits (o)"); */
996 else if (type == SCAN_IN)
998 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1000 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1003 buffer_write(bits_left - 2);
1004 if (type != SCAN_IN)
1005 buffer_write(buffer[cur_byte]);
1008 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1009 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1011 if (type == SCAN_IO)
1013 /* Clock Data Bits In and Out LSB First */
1015 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1017 else if (type == SCAN_OUT)
1019 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1021 /* LOG_DEBUG("added TDI bits (o)"); */
1023 else if (type == SCAN_IN)
1025 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1027 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1030 buffer_write(last_bit);
1038 /* move from Shift-IR/DR to end state */
1039 if (type != SCAN_OUT)
1041 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1042 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1045 /* Clock Data to TMS/CS Pin with Read */
1050 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1051 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1052 /* Clock Data to TMS/CS Pin (no Read) */
1056 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1057 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1060 if (tap_get_state() != tap_get_end_state())
1062 move_to_state(tap_get_end_state());
1066 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1068 int num_bytes = (scan_size + 7) / 8;
1069 int bits_left = scan_size;
1072 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1073 uint8_t* receive_pointer = receive_buffer;
1074 uint32_t bytes_written;
1075 uint32_t bytes_read;
1077 int thisrun_read = 0;
1081 LOG_ERROR("BUG: large IR scans are not supported");
1085 if (tap_get_state() != TAP_DRSHIFT)
1087 move_to_state(TAP_DRSHIFT);
1090 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1092 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1095 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1096 ft2232_buffer_size, (int)bytes_written);
1097 ft2232_buffer_size = 0;
1099 /* add command for complete bytes */
1100 while (num_bytes > 1)
1104 if (type == SCAN_IO)
1106 /* Clock Data Bytes In and Out LSB First */
1108 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1110 else if (type == SCAN_OUT)
1112 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1114 /* LOG_DEBUG("added TDI bytes (o)"); */
1116 else if (type == SCAN_IN)
1118 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1120 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1123 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1124 thisrun_read = thisrun_bytes;
1125 num_bytes -= thisrun_bytes;
1126 buffer_write((uint8_t) (thisrun_bytes - 1));
1127 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1129 if (type != SCAN_IN)
1131 /* add complete bytes */
1132 while (thisrun_bytes-- > 0)
1134 buffer_write(buffer[cur_byte]);
1139 else /* (type == SCAN_IN) */
1141 bits_left -= 8 * (thisrun_bytes);
1144 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1146 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1149 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1151 (int)bytes_written);
1152 ft2232_buffer_size = 0;
1154 if (type != SCAN_OUT)
1156 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1158 LOG_ERROR("couldn't read from FT2232");
1161 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1164 receive_pointer += bytes_read;
1170 /* the most signifcant bit is scanned during TAP movement */
1171 if (type != SCAN_IN)
1172 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1176 /* process remaining bits but the last one */
1179 if (type == SCAN_IO)
1181 /* Clock Data Bits In and Out LSB First */
1183 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1185 else if (type == SCAN_OUT)
1187 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1189 /* LOG_DEBUG("added TDI bits (o)"); */
1191 else if (type == SCAN_IN)
1193 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1195 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1197 buffer_write(bits_left - 2);
1198 if (type != SCAN_IN)
1199 buffer_write(buffer[cur_byte]);
1201 if (type != SCAN_OUT)
1205 if (tap_get_end_state() == TAP_DRSHIFT)
1207 if (type == SCAN_IO)
1209 /* Clock Data Bits In and Out LSB First */
1211 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1213 else if (type == SCAN_OUT)
1215 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1217 /* LOG_DEBUG("added TDI bits (o)"); */
1219 else if (type == SCAN_IN)
1221 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1223 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1226 buffer_write(last_bit);
1230 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1231 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1234 /* move from Shift-IR/DR to end state */
1235 if (type != SCAN_OUT)
1237 /* Clock Data to TMS/CS Pin with Read */
1239 /* LOG_DEBUG("added TMS scan (read)"); */
1243 /* Clock Data to TMS/CS Pin (no Read) */
1245 /* LOG_DEBUG("added TMS scan (no read)"); */
1248 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1249 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1252 if (type != SCAN_OUT)
1255 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1257 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1260 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1262 (int)bytes_written);
1263 ft2232_buffer_size = 0;
1265 if (type != SCAN_OUT)
1267 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1269 LOG_ERROR("couldn't read from FT2232");
1272 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1275 receive_pointer += bytes_read;
1281 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1283 int predicted_size = 3;
1284 int num_bytes = (scan_size - 1) / 8;
1286 if (tap_get_state() != TAP_DRSHIFT)
1287 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1289 if (type == SCAN_IN) /* only from device to host */
1291 /* complete bytes */
1292 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1294 /* remaining bits - 1 (up to 7) */
1295 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1297 else /* host to device, or bidirectional */
1299 /* complete bytes */
1300 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1302 /* remaining bits -1 (up to 7) */
1303 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1306 return predicted_size;
1309 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1311 int predicted_size = 0;
1313 if (type != SCAN_OUT)
1315 /* complete bytes */
1316 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1318 /* remaining bits - 1 */
1319 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1321 /* last bit (from TMS scan) */
1322 predicted_size += 1;
1325 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1327 return predicted_size;
1330 static void usbjtag_reset(int trst, int srst)
1332 enum reset_types jtag_reset_config = jtag_get_reset_config();
1335 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1336 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1338 low_output &= ~nTRST; /* switch output low */
1342 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1343 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1345 low_output |= nTRST; /* switch output high */
1350 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1351 low_output &= ~nSRST; /* switch output low */
1353 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1357 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1358 low_output |= nSRST; /* switch output high */
1360 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1363 /* command "set data bits low byte" */
1365 buffer_write(low_output);
1366 buffer_write(low_direction);
1369 static void jtagkey_reset(int trst, int srst)
1371 enum reset_types jtag_reset_config = jtag_get_reset_config();
1374 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1375 high_output &= ~nTRSTnOE;
1377 high_output &= ~nTRST;
1381 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1382 high_output |= nTRSTnOE;
1384 high_output |= nTRST;
1389 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1390 high_output &= ~nSRST;
1392 high_output &= ~nSRSTnOE;
1396 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1397 high_output |= nSRST;
1399 high_output |= nSRSTnOE;
1402 /* command "set data bits high byte" */
1404 buffer_write(high_output);
1405 buffer_write(high_direction);
1406 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1410 static void olimex_jtag_reset(int trst, int srst)
1412 enum reset_types jtag_reset_config = jtag_get_reset_config();
1415 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1416 high_output &= ~nTRSTnOE;
1418 high_output &= ~nTRST;
1422 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1423 high_output |= nTRSTnOE;
1425 high_output |= nTRST;
1430 high_output |= nSRST;
1434 high_output &= ~nSRST;
1437 /* command "set data bits high byte" */
1439 buffer_write(high_output);
1440 buffer_write(high_direction);
1441 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1445 static void axm0432_jtag_reset(int trst, int srst)
1449 tap_set_state(TAP_RESET);
1450 high_output &= ~nTRST;
1454 high_output |= nTRST;
1459 high_output &= ~nSRST;
1463 high_output |= nSRST;
1466 /* command "set data bits low byte" */
1468 buffer_write(high_output);
1469 buffer_write(high_direction);
1470 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1474 static void flyswatter_reset(int trst, int srst)
1478 low_output &= ~nTRST;
1482 low_output |= nTRST;
1487 low_output |= nSRST;
1491 low_output &= ~nSRST;
1494 /* command "set data bits low byte" */
1496 buffer_write(low_output);
1497 buffer_write(low_direction);
1498 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1501 static void turtle_reset(int trst, int srst)
1507 low_output |= nSRST;
1511 low_output &= ~nSRST;
1514 /* command "set data bits low byte" */
1516 buffer_write(low_output);
1517 buffer_write(low_direction);
1518 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1521 static void comstick_reset(int trst, int srst)
1525 high_output &= ~nTRST;
1529 high_output |= nTRST;
1534 high_output &= ~nSRST;
1538 high_output |= nSRST;
1541 /* command "set data bits high byte" */
1543 buffer_write(high_output);
1544 buffer_write(high_direction);
1545 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1549 static void stm32stick_reset(int trst, int srst)
1553 high_output &= ~nTRST;
1557 high_output |= nTRST;
1562 low_output &= ~nSRST;
1566 low_output |= nSRST;
1569 /* command "set data bits low byte" */
1571 buffer_write(low_output);
1572 buffer_write(low_direction);
1574 /* command "set data bits high byte" */
1576 buffer_write(high_output);
1577 buffer_write(high_direction);
1578 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1582 static void sheevaplug_reset(int trst, int srst)
1585 high_output &= ~nTRST;
1587 high_output |= nTRST;
1590 high_output &= ~nSRSTnOE;
1592 high_output |= nSRSTnOE;
1594 /* command "set data bits high byte" */
1596 buffer_write(high_output);
1597 buffer_write(high_direction);
1598 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1601 static void redbee_reset(int trst, int srst)
1605 tap_set_state(TAP_RESET);
1606 high_output &= ~nTRST;
1610 high_output |= nTRST;
1615 high_output &= ~nSRST;
1619 high_output |= nSRST;
1622 /* command "set data bits low byte" */
1624 buffer_write(high_output);
1625 buffer_write(high_direction);
1626 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1627 "high_direction: 0x%2.2x", trst, srst, high_output,
1631 static int ft2232_execute_runtest(struct jtag_command *cmd)
1635 int predicted_size = 0;
1638 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1639 cmd->cmd.runtest->num_cycles,
1640 tap_state_name(cmd->cmd.runtest->end_state));
1642 /* only send the maximum buffer size that FT2232C can handle */
1644 if (tap_get_state() != TAP_IDLE)
1645 predicted_size += 3;
1646 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1647 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1648 predicted_size += 3;
1649 if (tap_get_end_state() != TAP_IDLE)
1650 predicted_size += 3;
1651 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1653 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1654 retval = ERROR_JTAG_QUEUE_FAILED;
1658 if (tap_get_state() != TAP_IDLE)
1660 move_to_state(TAP_IDLE);
1663 i = cmd->cmd.runtest->num_cycles;
1666 /* there are no state transitions in this code, so omit state tracking */
1668 /* command "Clock Data to TMS/CS Pin (no Read)" */
1672 buffer_write((i > 7) ? 6 : (i - 1));
1677 i -= (i > 7) ? 7 : i;
1678 /* LOG_DEBUG("added TMS scan (no read)"); */
1681 ft2232_end_state(cmd->cmd.runtest->end_state);
1683 if (tap_get_state() != tap_get_end_state())
1685 move_to_state(tap_get_end_state());
1689 DEBUG_JTAG_IO("runtest: %i, end in %s",
1690 cmd->cmd.runtest->num_cycles,
1691 tap_state_name(tap_get_end_state()));
1695 static int ft2232_execute_statemove(struct jtag_command *cmd)
1697 int predicted_size = 0;
1698 int retval = ERROR_OK;
1700 DEBUG_JTAG_IO("statemove end in %s",
1701 tap_state_name(cmd->cmd.statemove->end_state));
1703 /* only send the maximum buffer size that FT2232C can handle */
1705 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1707 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1708 retval = ERROR_JTAG_QUEUE_FAILED;
1712 ft2232_end_state(cmd->cmd.statemove->end_state);
1714 /* For TAP_RESET, ignore the current recorded state. It's often
1715 * wrong at server startup, and this transation is critical whenever
1718 if (tap_get_end_state() == TAP_RESET) {
1719 clock_tms(0x4b, 0xff, 5, 0);
1722 /* shortest-path move to desired end state */
1723 } else if (tap_get_state() != tap_get_end_state())
1725 move_to_state(tap_get_end_state());
1733 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1734 * (or SWD) state machine.
1736 static int ft2232_execute_tms(struct jtag_command *cmd)
1738 int retval = ERROR_OK;
1739 unsigned num_bits = cmd->cmd.tms->num_bits;
1740 const uint8_t *bits = cmd->cmd.tms->bits;
1743 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1745 /* only send the maximum buffer size that FT2232C can handle */
1746 count = 3 * DIV_ROUND_UP(num_bits, 4);
1747 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1748 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1749 retval = ERROR_JTAG_QUEUE_FAILED;
1755 /* Shift out in batches of at most 6 bits; there's a report of an
1756 * FT2232 bug in this area, where shifting exactly 7 bits can make
1757 * problems with TMS signaling for the last clock cycle:
1759 * http://developer.intra2net.com/mailarchive/html/
1760 * libftdi/2009/msg00292.html
1762 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1764 * Note that pathmoves in JTAG are not often seven bits, so that
1765 * isn't a particularly likely situation outside of "special"
1766 * signaling such as switching between JTAG and SWD modes.
1769 if (num_bits <= 6) {
1771 buffer_write(num_bits - 1);
1772 buffer_write(*bits & 0x3f);
1776 /* Yes, this is lazy ... we COULD shift out more data
1777 * bits per operation, but doing it in nybbles is easy
1781 buffer_write(*bits & 0xf);
1784 count = (num_bits > 4) ? 4 : num_bits;
1787 buffer_write(count - 1);
1788 buffer_write((*bits >> 4) & 0xf);
1798 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1800 int predicted_size = 0;
1801 int retval = ERROR_OK;
1803 tap_state_t* path = cmd->cmd.pathmove->path;
1804 int num_states = cmd->cmd.pathmove->num_states;
1806 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1807 tap_state_name(tap_get_state()),
1808 tap_state_name(path[num_states-1]));
1810 /* only send the maximum buffer size that FT2232C can handle */
1811 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1812 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1814 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1815 retval = ERROR_JTAG_QUEUE_FAILED;
1821 ft2232_add_pathmove(path, num_states);
1827 static int ft2232_execute_scan(struct jtag_command *cmd)
1830 int scan_size; /* size of IR or DR scan */
1831 int predicted_size = 0;
1832 int retval = ERROR_OK;
1834 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1836 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1838 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1840 predicted_size = ft2232_predict_scan_out(scan_size, type);
1841 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1843 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1844 /* unsent commands before this */
1845 if (first_unsent != cmd)
1846 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1847 retval = ERROR_JTAG_QUEUE_FAILED;
1849 /* current command */
1850 ft2232_end_state(cmd->cmd.scan->end_state);
1851 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1853 first_unsent = cmd->next;
1858 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1860 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1863 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1864 retval = ERROR_JTAG_QUEUE_FAILED;
1868 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1869 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1870 ft2232_end_state(cmd->cmd.scan->end_state);
1871 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1875 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1876 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1877 tap_state_name(tap_get_end_state()));
1882 static int ft2232_execute_reset(struct jtag_command *cmd)
1885 int predicted_size = 0;
1888 DEBUG_JTAG_IO("reset trst: %i srst %i",
1889 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1891 /* only send the maximum buffer size that FT2232C can handle */
1893 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1895 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1896 retval = ERROR_JTAG_QUEUE_FAILED;
1901 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1903 tap_set_state(TAP_RESET);
1906 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1909 DEBUG_JTAG_IO("trst: %i, srst: %i",
1910 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1914 static int ft2232_execute_sleep(struct jtag_command *cmd)
1919 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1921 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1922 retval = ERROR_JTAG_QUEUE_FAILED;
1923 first_unsent = cmd->next;
1924 jtag_sleep(cmd->cmd.sleep->us);
1925 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1927 tap_state_name(tap_get_state()));
1931 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1936 /* this is only allowed while in a stable state. A check for a stable
1937 * state was done in jtag_add_clocks()
1939 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1940 retval = ERROR_JTAG_QUEUE_FAILED;
1941 DEBUG_JTAG_IO("clocks %i while in %s",
1942 cmd->cmd.stableclocks->num_cycles,
1943 tap_state_name(tap_get_state()));
1947 static int ft2232_execute_command(struct jtag_command *cmd)
1953 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1954 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1955 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1956 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1957 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1958 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1959 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1961 retval = ft2232_execute_tms(cmd);
1964 LOG_ERROR("BUG: unknown JTAG command type encountered");
1965 retval = ERROR_JTAG_QUEUE_FAILED;
1971 static int ft2232_execute_queue(void)
1973 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
1976 first_unsent = cmd; /* next command that has to be sent */
1979 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1980 * that wasn't handled by a caller-provided error handler
1984 ft2232_buffer_size = 0;
1985 ft2232_expect_read = 0;
1987 /* blink, if the current layout has that feature */
1993 if (ft2232_execute_command(cmd) != ERROR_OK)
1994 retval = ERROR_JTAG_QUEUE_FAILED;
1995 /* Start reading input before FT2232 TX buffer fills up */
1997 if (ft2232_expect_read > 256)
1999 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2000 retval = ERROR_JTAG_QUEUE_FAILED;
2005 if (require_send > 0)
2006 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2007 retval = ERROR_JTAG_QUEUE_FAILED;
2012 #if BUILD_FT2232_FTD2XX == 1
2013 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2017 char SerialNumber[16];
2018 char Description[64];
2019 DWORD openex_flags = 0;
2020 char* openex_string = NULL;
2021 uint8_t latency_timer;
2023 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
2026 /* Add non-standard Vid/Pid to the linux driver */
2027 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2029 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2033 if (ft2232_device_desc && ft2232_serial)
2035 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2036 ft2232_device_desc = NULL;
2039 if (ft2232_device_desc)
2041 openex_string = ft2232_device_desc;
2042 openex_flags = FT_OPEN_BY_DESCRIPTION;
2044 else if (ft2232_serial)
2046 openex_string = ft2232_serial;
2047 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2051 LOG_ERROR("neither device description nor serial number specified");
2052 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2054 return ERROR_JTAG_INIT_FAILED;
2057 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2058 if (status != FT_OK) {
2059 /* under Win32, the FTD2XX driver appends an "A" to the end
2060 * of the description, if we tried by the desc, then
2061 * try by the alternate "A" description. */
2062 if (openex_string == ft2232_device_desc) {
2063 /* Try the alternate method. */
2064 openex_string = ft2232_device_desc_A;
2065 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2066 if (status == FT_OK) {
2067 /* yea, the "alternate" method worked! */
2069 /* drat, give the user a meaningfull message.
2070 * telling the use we tried *BOTH* methods. */
2071 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
2073 ft2232_device_desc_A);
2078 if (status != FT_OK)
2084 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
2086 return ERROR_JTAG_INIT_FAILED;
2088 LOG_ERROR("unable to open ftdi device: %lu", status);
2089 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2090 if (status == FT_OK)
2092 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2095 for (i = 0; i < num_devices; i++)
2096 desc_array[i] = malloc(64);
2098 desc_array[num_devices] = NULL;
2100 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2102 if (status == FT_OK)
2104 LOG_ERROR("ListDevices: %lu\n", num_devices);
2105 for (i = 0; i < num_devices; i++)
2106 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2109 for (i = 0; i < num_devices; i++)
2110 free(desc_array[i]);
2116 LOG_ERROR("ListDevices: NONE\n");
2118 return ERROR_JTAG_INIT_FAILED;
2121 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2123 LOG_ERROR("unable to set latency timer: %lu", status);
2124 return ERROR_JTAG_INIT_FAILED;
2127 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2129 LOG_ERROR("unable to get latency timer: %lu", status);
2130 return ERROR_JTAG_INIT_FAILED;
2134 LOG_DEBUG("current latency timer: %i", latency_timer);
2137 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2139 LOG_ERROR("unable to set timeouts: %lu", status);
2140 return ERROR_JTAG_INIT_FAILED;
2143 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2145 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
2146 return ERROR_JTAG_INIT_FAILED;
2149 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2151 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
2152 return ERROR_JTAG_INIT_FAILED;
2156 static const char* type_str[] =
2157 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2158 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2159 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2160 ? ftdi_device : FT_DEVICE_UNKNOWN;
2161 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
2162 LOG_INFO("deviceID: %lu", deviceID);
2163 LOG_INFO("SerialNumber: %s", SerialNumber);
2164 LOG_INFO("Description: %s", Description);
2170 static int ft2232_purge_ftd2xx(void)
2174 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2176 LOG_ERROR("error purging ftd2xx device: %lu", status);
2177 return ERROR_JTAG_INIT_FAILED;
2183 #endif /* BUILD_FT2232_FTD2XX == 1 */
2185 #if BUILD_FT2232_LIBFTDI == 1
2186 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2188 uint8_t latency_timer;
2190 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2191 ft2232_layout, vid, pid);
2193 if (ftdi_init(&ftdic) < 0)
2194 return ERROR_JTAG_INIT_FAILED;
2196 /* default to INTERFACE_A */
2197 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2199 if (ftdi_set_interface(&ftdic, channel) < 0)
2201 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2202 return ERROR_JTAG_INIT_FAILED;
2205 /* context, vendor id, product id */
2206 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2210 LOG_WARNING("unable to open ftdi device (trying more): %s",
2213 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2215 return ERROR_JTAG_INIT_FAILED;
2218 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2219 if (ftdi_usb_reset(&ftdic) < 0)
2221 LOG_ERROR("unable to reset ftdi device");
2222 return ERROR_JTAG_INIT_FAILED;
2225 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2227 LOG_ERROR("unable to set latency timer");
2228 return ERROR_JTAG_INIT_FAILED;
2231 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2233 LOG_ERROR("unable to get latency timer");
2234 return ERROR_JTAG_INIT_FAILED;
2238 LOG_DEBUG("current latency timer: %i", latency_timer);
2241 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2243 ftdi_device = ftdic.type;
2244 static const char* type_str[] =
2245 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2246 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2247 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2248 ? ftdi_device : no_of_known_types;
2249 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2253 static int ft2232_purge_libftdi(void)
2255 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2257 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2258 return ERROR_JTAG_INIT_FAILED;
2264 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2266 static int ft2232_init(void)
2270 uint32_t bytes_written;
2271 const struct ft2232_layout* cur_layout = ft2232_layouts;
2274 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2276 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2280 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2283 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2285 ft2232_layout = "usbjtag";
2286 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2289 while (cur_layout->name)
2291 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2293 layout = cur_layout;
2301 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2302 return ERROR_JTAG_INIT_FAILED;
2308 * "more indicates that there are more IDs to try, so we should
2309 * not print an error for an ID mismatch (but for anything
2312 * try_more indicates that the error code returned indicates an
2313 * ID mismatch (and nothing else) and that we should proceeed
2314 * with the next ID pair.
2316 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2319 #if BUILD_FT2232_FTD2XX == 1
2320 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2322 #elif BUILD_FT2232_LIBFTDI == 1
2323 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2324 more, &try_more, cur_layout->channel);
2328 if (!more || !try_more)
2332 ft2232_buffer_size = 0;
2333 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2335 if (layout->init() != ERROR_OK)
2336 return ERROR_JTAG_INIT_FAILED;
2338 if (ft2232_device_is_highspeed())
2340 #ifndef BUILD_FT2232_HIGHSPEED
2341 #if BUILD_FT2232_FTD2XX == 1
2342 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2343 #elif BUILD_FT2232_LIBFTDI == 1
2344 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2347 /* make sure the legacy mode is disabled */
2348 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2349 return ERROR_JTAG_INIT_FAILED;
2352 ft2232_speed(jtag_get_speed());
2354 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2355 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2357 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2358 return ERROR_JTAG_INIT_FAILED;
2361 #if BUILD_FT2232_FTD2XX == 1
2362 return ft2232_purge_ftd2xx();
2363 #elif BUILD_FT2232_LIBFTDI == 1
2364 return ft2232_purge_libftdi();
2370 static int usbjtag_init(void)
2373 uint32_t bytes_written;
2376 low_direction = 0x0b;
2378 if (strcmp(ft2232_layout, "usbjtag") == 0)
2385 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2392 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2394 /* There are multiple revisions of LM3S811 eval boards:
2395 * - Rev B (and older?) boards have no SWO trace support.
2396 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2397 * they should use the "luminary_icdi" layout instead.
2404 low_direction = 0x8b;
2406 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2408 /* Most Luminary eval boards support SWO trace output,
2409 * and should use this "luminary_icdi" layout.
2416 low_direction = 0xcb;
2420 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2421 return ERROR_JTAG_INIT_FAILED;
2424 enum reset_types jtag_reset_config = jtag_get_reset_config();
2425 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2427 low_direction &= ~nTRSTnOE; /* nTRST input */
2428 low_output &= ~nTRST; /* nTRST = 0 */
2432 low_direction |= nTRSTnOE; /* nTRST output */
2433 low_output |= nTRST; /* nTRST = 1 */
2436 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2438 low_direction |= nSRSTnOE; /* nSRST output */
2439 low_output |= nSRST; /* nSRST = 1 */
2443 low_direction &= ~nSRSTnOE; /* nSRST input */
2444 low_output &= ~nSRST; /* nSRST = 0 */
2447 /* initialize low byte for jtag */
2448 buf[0] = 0x80; /* command "set data bits low byte" */
2449 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2450 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2451 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2453 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2455 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2456 return ERROR_JTAG_INIT_FAILED;
2462 static int axm0432_jtag_init(void)
2465 uint32_t bytes_written;
2468 low_direction = 0x2b;
2470 /* initialize low byte for jtag */
2471 buf[0] = 0x80; /* command "set data bits low byte" */
2472 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2473 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2474 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2476 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2478 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2479 return ERROR_JTAG_INIT_FAILED;
2482 if (strcmp(layout->name, "axm0432_jtag") == 0)
2485 nTRSTnOE = 0x0; /* No output enable for TRST*/
2487 nSRSTnOE = 0x0; /* No output enable for SRST*/
2491 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2496 high_direction = 0x0c;
2498 enum reset_types jtag_reset_config = jtag_get_reset_config();
2499 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2501 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2505 high_output |= nTRST;
2508 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2510 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2514 high_output |= nSRST;
2517 /* initialize high port */
2518 buf[0] = 0x82; /* command "set data bits high byte" */
2519 buf[1] = high_output; /* value */
2520 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2521 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2523 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2525 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2526 return ERROR_JTAG_INIT_FAILED;
2532 static int redbee_init(void)
2535 uint32_t bytes_written;
2538 low_direction = 0x2b;
2540 /* initialize low byte for jtag */
2541 /* command "set data bits low byte" */
2543 /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2544 buf[2] = low_direction;
2545 /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2546 buf[1] = low_output;
2547 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2549 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
2550 || (bytes_written != 3))
2552 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2553 return ERROR_JTAG_INIT_FAILED;
2557 nTRSTnOE = 0x0; /* No output enable for TRST*/
2559 nSRSTnOE = 0x0; /* No output enable for SRST*/
2562 high_direction = 0x0c;
2564 enum reset_types jtag_reset_config = jtag_get_reset_config();
2565 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2567 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2571 high_output |= nTRST;
2574 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2576 LOG_ERROR("can't set nSRST to push-pull on redbee");
2580 high_output |= nSRST;
2583 /* initialize high port */
2584 buf[0] = 0x82; /* command "set data bits high byte" */
2585 buf[1] = high_output; /* value */
2586 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2587 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2589 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
2590 || (bytes_written != 3))
2592 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2593 return ERROR_JTAG_INIT_FAILED;
2599 static int jtagkey_init(void)
2602 uint32_t bytes_written;
2605 low_direction = 0x1b;
2607 /* initialize low byte for jtag */
2608 buf[0] = 0x80; /* command "set data bits low byte" */
2609 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2610 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2611 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2613 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2615 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2616 return ERROR_JTAG_INIT_FAILED;
2619 if (strcmp(layout->name, "jtagkey") == 0)
2626 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2627 || (strcmp(layout->name, "oocdlink") == 0))
2636 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2641 high_direction = 0x0f;
2643 enum reset_types jtag_reset_config = jtag_get_reset_config();
2644 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2646 high_output |= nTRSTnOE;
2647 high_output &= ~nTRST;
2651 high_output &= ~nTRSTnOE;
2652 high_output |= nTRST;
2655 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2657 high_output &= ~nSRSTnOE;
2658 high_output |= nSRST;
2662 high_output |= nSRSTnOE;
2663 high_output &= ~nSRST;
2666 /* initialize high port */
2667 buf[0] = 0x82; /* command "set data bits high byte" */
2668 buf[1] = high_output; /* value */
2669 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2670 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2672 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2674 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2675 return ERROR_JTAG_INIT_FAILED;
2681 static int olimex_jtag_init(void)
2684 uint32_t bytes_written;
2687 low_direction = 0x1b;
2689 /* initialize low byte for jtag */
2690 buf[0] = 0x80; /* command "set data bits low byte" */
2691 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2692 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2693 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2695 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2697 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2698 return ERROR_JTAG_INIT_FAILED;
2704 nSRSTnOE = 0x00; /* no output enable for nSRST */
2707 high_direction = 0x0f;
2709 enum reset_types jtag_reset_config = jtag_get_reset_config();
2710 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2712 high_output |= nTRSTnOE;
2713 high_output &= ~nTRST;
2717 high_output &= ~nTRSTnOE;
2718 high_output |= nTRST;
2721 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2723 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2727 high_output &= ~nSRST;
2730 /* turn red LED on */
2731 high_output |= 0x08;
2733 /* initialize high port */
2734 buf[0] = 0x82; /* command "set data bits high byte" */
2735 buf[1] = high_output; /* value */
2736 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2737 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2739 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2741 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2742 return ERROR_JTAG_INIT_FAILED;
2748 static int flyswatter_init(void)
2751 uint32_t bytes_written;
2754 low_direction = 0xfb;
2756 /* initialize low byte for jtag */
2757 buf[0] = 0x80; /* command "set data bits low byte" */
2758 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2759 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2760 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2762 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2764 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2765 return ERROR_JTAG_INIT_FAILED;
2769 nTRSTnOE = 0x0; /* not output enable for nTRST */
2771 nSRSTnOE = 0x00; /* no output enable for nSRST */
2774 high_direction = 0x0c;
2776 /* turn red LED3 on, LED2 off */
2777 high_output |= 0x08;
2779 /* initialize high port */
2780 buf[0] = 0x82; /* command "set data bits high byte" */
2781 buf[1] = high_output; /* value */
2782 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2783 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2785 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2787 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2788 return ERROR_JTAG_INIT_FAILED;
2794 static int turtle_init(void)
2797 uint32_t bytes_written;
2800 low_direction = 0x5b;
2802 /* initialize low byte for jtag */
2803 buf[0] = 0x80; /* command "set data bits low byte" */
2804 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2805 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2806 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2808 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2810 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2811 return ERROR_JTAG_INIT_FAILED;
2817 high_direction = 0x0C;
2819 /* initialize high port */
2820 buf[0] = 0x82; /* command "set data bits high byte" */
2821 buf[1] = high_output;
2822 buf[2] = high_direction;
2823 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2825 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2827 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2828 return ERROR_JTAG_INIT_FAILED;
2834 static int comstick_init(void)
2837 uint32_t bytes_written;
2840 low_direction = 0x0b;
2842 /* initialize low byte for jtag */
2843 buf[0] = 0x80; /* command "set data bits low byte" */
2844 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2845 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2846 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2848 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2850 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2851 return ERROR_JTAG_INIT_FAILED;
2855 nTRSTnOE = 0x00; /* no output enable for nTRST */
2857 nSRSTnOE = 0x00; /* no output enable for nSRST */
2860 high_direction = 0x03;
2862 /* initialize high port */
2863 buf[0] = 0x82; /* command "set data bits high byte" */
2864 buf[1] = high_output;
2865 buf[2] = high_direction;
2866 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2868 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2870 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2871 return ERROR_JTAG_INIT_FAILED;
2877 static int stm32stick_init(void)
2880 uint32_t bytes_written;
2883 low_direction = 0x8b;
2885 /* initialize low byte for jtag */
2886 buf[0] = 0x80; /* command "set data bits low byte" */
2887 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2888 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2889 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2891 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2893 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2894 return ERROR_JTAG_INIT_FAILED;
2898 nTRSTnOE = 0x00; /* no output enable for nTRST */
2900 nSRSTnOE = 0x00; /* no output enable for nSRST */
2903 high_direction = 0x03;
2905 /* initialize high port */
2906 buf[0] = 0x82; /* command "set data bits high byte" */
2907 buf[1] = high_output;
2908 buf[2] = high_direction;
2909 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2911 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2913 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2914 return ERROR_JTAG_INIT_FAILED;
2920 static int sheevaplug_init(void)
2923 uint32_t bytes_written;
2926 low_direction = 0x1b;
2928 /* initialize low byte for jtag */
2929 buf[0] = 0x80; /* command "set data bits low byte" */
2930 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2931 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2932 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2934 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2936 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2937 return ERROR_JTAG_INIT_FAILED;
2946 high_direction = 0x0f;
2948 /* nTRST is always push-pull */
2949 high_output &= ~nTRSTnOE;
2950 high_output |= nTRST;
2952 /* nSRST is always open-drain */
2953 high_output |= nSRSTnOE;
2954 high_output &= ~nSRST;
2956 /* initialize high port */
2957 buf[0] = 0x82; /* command "set data bits high byte" */
2958 buf[1] = high_output; /* value */
2959 buf[2] = high_direction; /* all outputs - xRST */
2960 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2962 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2964 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2965 return ERROR_JTAG_INIT_FAILED;
2971 static int cortino_jtag_init(void)
2974 uint32_t bytes_written;
2977 low_direction = 0x1b;
2979 /* initialize low byte for jtag */
2980 buf[0] = 0x80; /* command "set data bits low byte" */
2981 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2982 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2983 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2985 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2987 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2988 return ERROR_JTAG_INIT_FAILED;
2992 nTRSTnOE = 0x00; /* no output enable for nTRST */
2994 nSRSTnOE = 0x00; /* no output enable for nSRST */
2997 high_direction = 0x03;
2999 /* initialize high port */
3000 buf[0] = 0x82; /* command "set data bits high byte" */
3001 buf[1] = high_output;
3002 buf[2] = high_direction;
3003 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3005 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
3007 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3008 return ERROR_JTAG_INIT_FAILED;
3014 static void olimex_jtag_blink(void)
3016 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3017 * ACBUS3 is bit 3 of the GPIOH port
3019 if (high_output & 0x08)
3021 /* set port pin high */
3022 high_output &= 0x07;
3026 /* set port pin low */
3027 high_output |= 0x08;
3031 buffer_write(high_output);
3032 buffer_write(high_direction);
3035 static void flyswatter_jtag_blink(void)
3038 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3040 high_output ^= 0x0c;
3043 buffer_write(high_output);
3044 buffer_write(high_direction);
3047 static void turtle_jtag_blink(void)
3050 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3052 if (high_output & 0x08)
3062 buffer_write(high_output);
3063 buffer_write(high_direction);
3066 static int ft2232_quit(void)
3068 #if BUILD_FT2232_FTD2XX == 1
3071 status = FT_Close(ftdih);
3072 #elif BUILD_FT2232_LIBFTDI == 1
3073 ftdi_usb_close(&ftdic);
3075 ftdi_deinit(&ftdic);
3078 free(ft2232_buffer);
3079 ft2232_buffer = NULL;
3084 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3090 ft2232_device_desc = strdup(CMD_ARGV[0]);
3091 cp = strchr(ft2232_device_desc, 0);
3092 /* under Win32, the FTD2XX driver appends an "A" to the end
3093 * of the description, this examines the given desc
3094 * and creates the 'missing' _A or non_A variable. */
3095 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
3096 /* it was, so make this the "A" version. */
3097 ft2232_device_desc_A = ft2232_device_desc;
3098 /* and *CREATE* the non-A version. */
3099 strcpy(buf, ft2232_device_desc);
3100 cp = strchr(buf, 0);
3102 ft2232_device_desc = strdup(buf);
3104 /* <space > A not defined
3106 sprintf(buf, "%s A", ft2232_device_desc);
3107 ft2232_device_desc_A = strdup(buf);
3112 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3118 COMMAND_HANDLER(ft2232_handle_serial_command)
3122 ft2232_serial = strdup(CMD_ARGV[0]);
3126 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
3132 COMMAND_HANDLER(ft2232_handle_layout_command)
3137 ft2232_layout = malloc(strlen(CMD_ARGV[0]) + 1);
3138 strcpy(ft2232_layout, CMD_ARGV[0]);
3143 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3145 if (CMD_ARGC > MAX_USB_IDS * 2)
3147 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3148 "(maximum is %d pairs)", MAX_USB_IDS);
3149 CMD_ARGC = MAX_USB_IDS * 2;
3151 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
3153 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3155 return ERROR_COMMAND_SYNTAX_ERROR;
3156 /* remove the incomplete trailing id */
3161 for (i = 0; i < CMD_ARGC; i += 2)
3163 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3164 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3168 * Explicitly terminate, in case there are multiples instances of
3171 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3176 COMMAND_HANDLER(ft2232_handle_latency_command)
3180 ft2232_latency = atoi(CMD_ARGV[0]);
3184 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
3190 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
3194 /* 7 bits of either ones or zeros. */
3195 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3197 while (num_cycles > 0)
3199 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3200 * at most 7 bits per invocation. Here we invoke it potentially
3203 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3205 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
3207 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3208 retval = ERROR_JTAG_QUEUE_FAILED;
3213 /* there are no state transitions in this code, so omit state tracking */
3215 /* command "Clock Data to TMS/CS Pin (no Read)" */
3219 buffer_write(bitcount_per_command - 1);
3221 /* TMS data bits are either all zeros or ones to stay in the current stable state */
3226 num_cycles -= bitcount_per_command;
3232 /* ---------------------------------------------------------------------
3233 * Support for IceBear JTAG adapter from Section5:
3234 * http://section5.ch/icebear
3236 * Author: Sten, debian@sansys-electronic.com
3239 /* Icebear pin layout
3241 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3242 * GND GND | 4 3| n.c.
3243 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3244 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3245 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3246 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3247 * ADBUS2 TDO |14 13| GND GND
3249 * ADBUS0 O L TCK ACBUS0 GND
3250 * ADBUS1 O L TDI ACBUS1 GND
3251 * ADBUS2 I TDO ACBUS2 n.c.
3252 * ADBUS3 O H TMS ACBUS3 n.c.
3258 static int icebear_jtag_init(void) {
3260 uint32_t bytes_written;
3262 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3263 low_output = 0x08; /* high: TMS; low: TCK TDI */
3267 enum reset_types jtag_reset_config = jtag_get_reset_config();
3268 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3269 low_direction &= ~nTRST; /* nTRST high impedance */
3272 low_direction |= nTRST;
3273 low_output |= nTRST;
3276 low_direction |= nSRST;
3277 low_output |= nSRST;
3279 /* initialize low byte for jtag */
3280 buf[0] = 0x80; /* command "set data bits low byte" */
3281 buf[1] = low_output;
3282 buf[2] = low_direction;
3283 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3285 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3286 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3287 return ERROR_JTAG_INIT_FAILED;
3291 high_direction = 0x00;
3294 /* initialize high port */
3295 buf[0] = 0x82; /* command "set data bits high byte" */
3296 buf[1] = high_output; /* value */
3297 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3298 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3300 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3301 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3302 return ERROR_JTAG_INIT_FAILED;
3308 static void icebear_jtag_reset(int trst, int srst) {
3311 low_direction |= nTRST;
3312 low_output &= ~nTRST;
3314 else if (trst == 0) {
3315 enum reset_types jtag_reset_config = jtag_get_reset_config();
3316 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3317 low_direction &= ~nTRST;
3319 low_output |= nTRST;
3323 low_output &= ~nSRST;
3325 else if (srst == 0) {
3326 low_output |= nSRST;
3329 /* command "set data bits low byte" */
3331 buffer_write(low_output);
3332 buffer_write(low_direction);
3334 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3337 /* ---------------------------------------------------------------------
3338 * Support for Signalyzer H2 and Signalyzer H4
3339 * JTAG adapter from Xverve Technologies Inc.
3340 * http://www.signalyzer.com or http://www.xverve.com
3342 * Author: Oleg Seiljus, oleg@signalyzer.com
3344 static unsigned char signalyzer_h_side;
3345 static unsigned int signalyzer_h_adapter_type;
3347 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3349 #if BUILD_FT2232_FTD2XX == 1
3350 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3353 #define SIGNALYZER_COMMAND_ADDR 128
3354 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3356 #define SIGNALYZER_COMMAND_VERSION 0x41
3357 #define SIGNALYZER_COMMAND_RESET 0x42
3358 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3359 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3360 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3361 #define SIGNALYZER_COMMAND_LED_SET 0x53
3362 #define SIGNALYZER_COMMAND_ADC 0x54
3363 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3364 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3365 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3366 #define SIGNALYZER_COMMAND_I2C 0x58
3368 #define SIGNALYZER_CHAN_A 1
3369 #define SIGNALYZER_CHAN_B 2
3370 /* LEDS use channel C */
3371 #define SIGNALYZER_CHAN_C 4
3373 #define SIGNALYZER_LED_GREEN 1
3374 #define SIGNALYZER_LED_RED 2
3376 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3377 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3378 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3379 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3380 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3383 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3385 #if BUILD_FT2232_FTD2XX == 1
3386 return FT_WriteEE(ftdih, address, value);
3387 #elif BUILD_FT2232_LIBFTDI == 1
3392 #if BUILD_FT2232_FTD2XX == 1
3393 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3395 return FT_ReadEE(ftdih, address, value);
3399 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3400 int on_time_ms, int off_time_ms, unsigned char cycles)
3402 unsigned char on_time;
3403 unsigned char off_time;
3405 if (on_time_ms < 0xFFFF)
3406 on_time = (unsigned char)(on_time_ms / 62);
3410 off_time = (unsigned char)(off_time_ms / 62);
3412 #if BUILD_FT2232_FTD2XX == 1
3415 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3416 ((uint32_t)(channel << 8) | led))) != FT_OK)
3418 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3419 return ERROR_JTAG_DEVICE_ERROR;
3422 if ((status = signalyzer_h_ctrl_write(
3423 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3424 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3426 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3427 return ERROR_JTAG_DEVICE_ERROR;
3430 if ((status = signalyzer_h_ctrl_write(
3431 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3432 ((uint32_t)cycles))) != FT_OK)
3434 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3435 return ERROR_JTAG_DEVICE_ERROR;
3438 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3439 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3441 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3442 return ERROR_JTAG_DEVICE_ERROR;
3446 #elif BUILD_FT2232_LIBFTDI == 1
3449 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3450 ((uint32_t)(channel << 8) | led))) < 0)
3452 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3453 ftdi_get_error_string(&ftdic));
3454 return ERROR_JTAG_DEVICE_ERROR;
3457 if ((retval = signalyzer_h_ctrl_write(
3458 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3459 ((uint32_t)(on_time << 8) | off_time))) < 0)
3461 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3462 ftdi_get_error_string(&ftdic));
3463 return ERROR_JTAG_DEVICE_ERROR;
3466 if ((retval = signalyzer_h_ctrl_write(
3467 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3468 (uint32_t)cycles)) < 0)
3470 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3471 ftdi_get_error_string(&ftdic));
3472 return ERROR_JTAG_DEVICE_ERROR;
3475 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3476 SIGNALYZER_COMMAND_LED_SET)) < 0)
3478 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3479 ftdi_get_error_string(&ftdic));
3480 return ERROR_JTAG_DEVICE_ERROR;
3487 static int signalyzer_h_init(void)
3489 #if BUILD_FT2232_FTD2XX == 1
3496 uint16_t read_buf[12] = { 0 };
3498 uint32_t bytes_written;
3500 /* turn on center green led */
3501 signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
3502 0xFFFF, 0x00, 0x00);
3504 /* determine what channel config wants to open
3505 * TODO: change me... current implementation is made to work
3506 * with openocd description parsing.
3508 end_of_desc = strrchr(ft2232_device_desc, 0x00);
3512 signalyzer_h_side = *(end_of_desc - 1);
3513 if (signalyzer_h_side == 'B')
3514 signalyzer_h_side = SIGNALYZER_CHAN_B;
3516 signalyzer_h_side = SIGNALYZER_CHAN_A;
3520 LOG_ERROR("No Channel was specified");
3524 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3527 #if BUILD_FT2232_FTD2XX == 1
3528 /* read signalyzer versionining information */
3529 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3530 SIGNALYZER_COMMAND_VERSION)) != FT_OK)
3532 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3533 return ERROR_JTAG_DEVICE_ERROR;
3536 for (i = 0; i < 10; i++)
3538 if ((status = signalyzer_h_ctrl_read(
3539 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3540 &read_buf[i])) != FT_OK)
3542 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3544 return ERROR_JTAG_DEVICE_ERROR;
3548 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3549 read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3550 read_buf[4], read_buf[5], read_buf[6]);
3552 /* set gpio register */
3553 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3554 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3556 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3557 return ERROR_JTAG_DEVICE_ERROR;
3560 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
3563 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3564 return ERROR_JTAG_DEVICE_ERROR;
3567 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3568 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3570 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3571 return ERROR_JTAG_DEVICE_ERROR;
3574 /* read adapter type information */
3575 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3576 ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
3578 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3579 return ERROR_JTAG_DEVICE_ERROR;
3582 if ((status = signalyzer_h_ctrl_write(
3583 (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
3585 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3586 return ERROR_JTAG_DEVICE_ERROR;
3589 if ((status = signalyzer_h_ctrl_write(
3590 (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
3592 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3593 return ERROR_JTAG_DEVICE_ERROR;
3596 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3597 SIGNALYZER_COMMAND_I2C)) != FT_OK)
3599 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3600 return ERROR_JTAG_DEVICE_ERROR;
3605 if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
3606 &read_buf[0])) != FT_OK)
3608 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
3609 return ERROR_JTAG_DEVICE_ERROR;
3612 if (read_buf[0] != 0x0498)
3613 signalyzer_h_adapter_type = 0x0000;
3616 for (i = 0; i < 4; i++)
3618 if ((status = signalyzer_h_ctrl_read(
3619 (SIGNALYZER_DATA_BUFFER_ADDR + i),
3620 &read_buf[i])) != FT_OK)
3622 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3624 return ERROR_JTAG_DEVICE_ERROR;
3628 signalyzer_h_adapter_type = read_buf[0];
3631 #elif BUILD_FT2232_LIBFTDI == 1
3632 /* currently libftdi does not allow reading individual eeprom
3633 * locations, therefore adapter type cannot be detected.
3634 * override with most common type
3636 signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3639 enum reset_types jtag_reset_config = jtag_get_reset_config();
3641 /* ADAPTOR: EM_LT16_A */
3642 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3644 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3645 "detected. (HW: %2x).", (read_buf[1] >> 8));
3653 low_direction = 0x1b;
3656 high_direction = 0x0;
3658 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3660 low_direction &= ~nTRSTnOE; /* nTRST input */
3661 low_output &= ~nTRST; /* nTRST = 0 */
3665 low_direction |= nTRSTnOE; /* nTRST output */
3666 low_output |= nTRST; /* nTRST = 1 */
3669 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3671 low_direction |= nSRSTnOE; /* nSRST output */
3672 low_output |= nSRST; /* nSRST = 1 */
3676 low_direction &= ~nSRSTnOE; /* nSRST input */
3677 low_output &= ~nSRST; /* nSRST = 0 */
3680 #if BUILD_FT2232_FTD2XX == 1
3681 /* enable power to the module */
3682 if ((status = signalyzer_h_ctrl_write(
3683 SIGNALYZER_DATA_BUFFER_ADDR,
3684 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3687 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3689 return ERROR_JTAG_DEVICE_ERROR;
3692 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3693 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3695 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3697 return ERROR_JTAG_DEVICE_ERROR;
3700 /* set gpio mode register */
3701 if ((status = signalyzer_h_ctrl_write(
3702 SIGNALYZER_DATA_BUFFER_ADDR,
3703 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3705 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3707 return ERROR_JTAG_DEVICE_ERROR;
3710 if ((status = signalyzer_h_ctrl_write(
3711 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3714 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3716 return ERROR_JTAG_DEVICE_ERROR;
3719 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3720 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3722 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3724 return ERROR_JTAG_DEVICE_ERROR;
3727 /* set gpio register */
3728 if ((status = signalyzer_h_ctrl_write(
3729 SIGNALYZER_DATA_BUFFER_ADDR,
3730 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3732 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3734 return ERROR_JTAG_DEVICE_ERROR;
3737 if ((status = signalyzer_h_ctrl_write(
3738 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
3741 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3743 return ERROR_JTAG_DEVICE_ERROR;
3746 if ((status = signalyzer_h_ctrl_write(
3747 SIGNALYZER_COMMAND_ADDR,
3748 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3750 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3752 return ERROR_JTAG_DEVICE_ERROR;
3757 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3758 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3759 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3760 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3761 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
3763 if (signalyzer_h_adapter_type
3764 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
3765 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3766 "detected. (HW: %2x).", (read_buf[1] >> 8));
3767 else if (signalyzer_h_adapter_type
3768 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
3769 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3770 "(ARM JTAG with PSU) detected. (HW: %2x).",
3771 (read_buf[1] >> 8));
3772 else if (signalyzer_h_adapter_type
3773 == SIGNALYZER_MODULE_TYPE_EM_JTAG)
3774 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3775 "detected. (HW: %2x).", (read_buf[1] >> 8));
3776 else if (signalyzer_h_adapter_type
3777 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
3778 LOG_INFO("Signalyzer: EM-JTAG-P "
3779 "(Generic JTAG with PSU) detected. (HW: %2x).",
3780 (read_buf[1] >> 8));
3788 low_direction = 0x1b;
3791 high_direction = 0x1f;
3793 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3795 high_output |= nTRSTnOE;
3796 high_output &= ~nTRST;
3800 high_output &= ~nTRSTnOE;
3801 high_output |= nTRST;
3804 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3806 high_output &= ~nSRSTnOE;
3807 high_output |= nSRST;
3811 high_output |= nSRSTnOE;
3812 high_output &= ~nSRST;
3815 #if BUILD_FT2232_FTD2XX == 1
3816 /* enable power to the module */
3817 if ((status = signalyzer_h_ctrl_write(
3818 SIGNALYZER_DATA_BUFFER_ADDR,
3819 ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
3822 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3824 return ERROR_JTAG_DEVICE_ERROR;
3827 if ((status = signalyzer_h_ctrl_write(
3828 SIGNALYZER_COMMAND_ADDR,
3829 SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
3831 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3833 return ERROR_JTAG_DEVICE_ERROR;
3836 /* set gpio mode register (IO_16 and IO_17 set as analog
3837 * inputs, other is gpio)
3839 if ((status = signalyzer_h_ctrl_write(
3840 SIGNALYZER_DATA_BUFFER_ADDR,
3841 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3843 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3845 return ERROR_JTAG_DEVICE_ERROR;
3848 if ((status = signalyzer_h_ctrl_write(
3849 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
3852 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3854 return ERROR_JTAG_DEVICE_ERROR;
3857 if ((status = signalyzer_h_ctrl_write(
3858 SIGNALYZER_COMMAND_ADDR,
3859 SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
3861 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3863 return ERROR_JTAG_DEVICE_ERROR;
3866 /* set gpio register (all inputs, for -P modules,
3867 * PSU will be turned off)
3869 if ((status = signalyzer_h_ctrl_write(
3870 SIGNALYZER_DATA_BUFFER_ADDR,
3871 (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
3873 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3875 return ERROR_JTAG_DEVICE_ERROR;
3878 if ((status = signalyzer_h_ctrl_write(
3879 SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
3882 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3884 return ERROR_JTAG_DEVICE_ERROR;
3887 if ((status = signalyzer_h_ctrl_write(
3888 SIGNALYZER_COMMAND_ADDR,
3889 SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
3891 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3893 return ERROR_JTAG_DEVICE_ERROR;
3898 else if (signalyzer_h_adapter_type == 0x0000)
3900 LOG_INFO("Signalyzer: No external modules were detected.");
3908 low_direction = 0x1b;
3911 high_direction = 0x0;
3913 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3915 low_direction &= ~nTRSTnOE; /* nTRST input */
3916 low_output &= ~nTRST; /* nTRST = 0 */
3920 low_direction |= nTRSTnOE; /* nTRST output */
3921 low_output |= nTRST; /* nTRST = 1 */
3924 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
3926 low_direction |= nSRSTnOE; /* nSRST output */
3927 low_output |= nSRST; /* nSRST = 1 */
3931 low_direction &= ~nSRSTnOE; /* nSRST input */
3932 low_output &= ~nSRST; /* nSRST = 0 */
3937 LOG_ERROR("Unknown module type is detected: %.4x",
3938 signalyzer_h_adapter_type);
3939 return ERROR_JTAG_DEVICE_ERROR;
3942 /* initialize low byte of controller for jtag operation */
3944 buf[1] = low_output;
3945 buf[2] = low_direction;
3947 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
3948 || (bytes_written != 3))
3950 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3951 return ERROR_JTAG_INIT_FAILED;
3954 #if BUILD_FT2232_FTD2XX == 1
3955 if (ftdi_device == FT_DEVICE_2232H)
3957 /* initialize high byte of controller for jtag operation */
3959 buf[1] = high_output;
3960 buf[2] = high_direction;
3962 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3963 || (bytes_written != 3))
3965 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3966 return ERROR_JTAG_INIT_FAILED;
3969 #elif BUILD_FT2232_LIBFTDI == 1
3970 if (ftdi_device == TYPE_2232H)
3972 /* initialize high byte of controller for jtag operation */
3974 buf[1] = high_output;
3975 buf[2] = high_direction;
3977 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
3978 || (bytes_written != 3))
3980 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3981 return ERROR_JTAG_INIT_FAILED;
3988 static void signalyzer_h_reset(int trst, int srst)
3990 enum reset_types jtag_reset_config = jtag_get_reset_config();
3992 /* ADAPTOR: EM_LT16_A */
3993 if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
3997 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3998 /* switch to output pin (output is low) */
3999 low_direction |= nTRSTnOE;
4001 /* switch output low */
4002 low_output &= ~nTRST;
4006 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4007 /* switch to input pin (high-Z + internal
4008 * and external pullup) */
4009 low_direction &= ~nTRSTnOE;
4011 /* switch output high */
4012 low_output |= nTRST;
4017 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4018 /* switch output low */
4019 low_output &= ~nSRST;
4021 /* switch to output pin (output is low) */
4022 low_direction |= nSRSTnOE;
4026 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4027 /* switch output high */
4028 low_output |= nSRST;
4030 /* switch to input pin (high-Z) */
4031 low_direction &= ~nSRSTnOE;
4034 /* command "set data bits low byte" */
4036 buffer_write(low_output);
4037 buffer_write(low_direction);
4038 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4039 "low_direction: 0x%2.2x",
4040 trst, srst, low_output, low_direction);
4042 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4043 else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4044 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4045 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4046 (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
4050 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4051 high_output &= ~nTRSTnOE;
4053 high_output &= ~nTRST;
4057 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4058 high_output |= nTRSTnOE;
4060 high_output |= nTRST;
4065 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4066 high_output &= ~nSRST;
4068 high_output &= ~nSRSTnOE;
4072 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4073 high_output |= nSRST;
4075 high_output |= nSRSTnOE;
4078 /* command "set data bits high byte" */
4080 buffer_write(high_output);
4081 buffer_write(high_direction);
4082 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4083 "high_direction: 0x%2.2x",
4084 trst, srst, high_output, high_direction);
4086 else if (signalyzer_h_adapter_type == 0x0000)
4090 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4091 /* switch to output pin (output is low) */
4092 low_direction |= nTRSTnOE;
4094 /* switch output low */
4095 low_output &= ~nTRST;
4099 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4100 /* switch to input pin (high-Z + internal
4101 * and external pullup) */
4102 low_direction &= ~nTRSTnOE;
4104 /* switch output high */
4105 low_output |= nTRST;
4110 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4111 /* switch output low */
4112 low_output &= ~nSRST;
4114 /* switch to output pin (output is low) */
4115 low_direction |= nSRSTnOE;
4119 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4120 /* switch output high */
4121 low_output |= nSRST;
4123 /* switch to input pin (high-Z) */
4124 low_direction &= ~nSRSTnOE;
4127 /* command "set data bits low byte" */
4129 buffer_write(low_output);
4130 buffer_write(low_direction);
4131 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4132 "low_direction: 0x%2.2x",
4133 trst, srst, low_output, low_direction);
4137 static void signalyzer_h_blink(void)
4139 signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4142 /********************************************************************
4143 * Support for KT-LINK
4144 * JTAG adapter from KRISTECH
4145 * http://www.kristech.eu
4146 *******************************************************************/
4147 static int ktlink_init(void)
4150 uint32_t bytes_written;
4151 uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
4153 low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
4154 low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
4156 // initialize low port
4157 buf[0] = 0x80; // command "set data bits low byte"
4158 buf[1] = low_output;
4159 buf[2] = low_direction;
4160 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4162 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
4164 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4165 return ERROR_JTAG_INIT_FAILED;
4173 high_output = 0x80; // turn LED on
4174 high_direction = 0xFF; // all outputs
4176 enum reset_types jtag_reset_config = jtag_get_reset_config();
4178 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4179 high_output |= nTRSTnOE;
4180 high_output &= ~nTRST;
4182 high_output &= ~nTRSTnOE;
4183 high_output |= nTRST;
4186 if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4187 high_output &= ~nSRSTnOE;
4188 high_output |= nSRST;
4190 high_output |= nSRSTnOE;
4191 high_output &= ~nSRST;
4194 // initialize high port
4195 buf[0] = 0x82; // command "set data bits high byte"
4196 buf[1] = high_output; // value
4197 buf[2] = high_direction;
4198 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
4200 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
4202 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4203 return ERROR_JTAG_INIT_FAILED;
4209 static void ktlink_reset(int trst, int srst)
4211 enum reset_types jtag_reset_config = jtag_get_reset_config();
4214 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4215 high_output &= ~nTRSTnOE;
4217 high_output &= ~nTRST;
4218 } else if (trst == 0) {
4219 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4220 high_output |= nTRSTnOE;
4222 high_output |= nTRST;
4226 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4227 high_output &= ~nSRST;
4229 high_output &= ~nSRSTnOE;
4230 } else if (srst == 0) {
4231 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4232 high_output |= nSRST;
4234 high_output |= nSRSTnOE;
4237 buffer_write(0x82); // command "set data bits high byte"
4238 buffer_write(high_output);
4239 buffer_write(high_direction);
4240 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
4243 static void ktlink_blink(void)
4245 /* LED connected to ACBUS7 */
4246 if (high_output & 0x80)
4247 high_output &= 0x7F;
4249 high_output |= 0x80;
4251 buffer_write(0x82); // command "set data bits high byte"
4252 buffer_write(high_output);
4253 buffer_write(high_direction);
4256 static const struct command_registration ft2232_command_handlers[] = {
4258 .name = "ft2232_device_desc",
4259 .handler = &ft2232_handle_device_desc_command,
4260 .mode = COMMAND_CONFIG,
4261 .help = "set the USB device description of the FTDI FT2232 device",
4262 .usage = "description_string",
4265 .name = "ft2232_serial",
4266 .handler = &ft2232_handle_serial_command,
4267 .mode = COMMAND_CONFIG,
4268 .help = "set the serial number of the FTDI FT2232 device",
4269 .usage = "serial_string",
4272 .name = "ft2232_layout",
4273 .handler = &ft2232_handle_layout_command,
4274 .mode = COMMAND_CONFIG,
4275 .help = "set the layout of the FT2232 GPIO signals used "
4276 "to control output-enables and reset signals",
4277 .usage = "layout_name",
4280 .name = "ft2232_vid_pid",
4281 .handler = &ft2232_handle_vid_pid_command,
4282 .mode = COMMAND_CONFIG,
4283 .help = "the vendor ID and product ID of the FTDI FT2232 device",
4284 .usage = "(vid pid)* ",
4287 .name = "ft2232_latency",
4288 .handler = &ft2232_handle_latency_command,
4289 .mode = COMMAND_CONFIG,
4290 .help = "set the FT2232 latency timer to a new value",
4293 COMMAND_REGISTRATION_DONE
4296 struct jtag_interface ft2232_interface = {
4298 .supported = DEBUG_CAP_TMS_SEQ,
4299 .commands = ft2232_command_handlers,
4301 .init = ft2232_init,
4302 .quit = ft2232_quit,
4303 .speed = ft2232_speed,
4304 .speed_div = ft2232_speed_div,
4306 .execute_queue = ft2232_execute_queue,