1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
9 * Dick Hollenbeck <dick@softplc.com> *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
28 /* This code uses information contained in the MPSSE specification which was
30 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
31 * Hereafter this is called the "MPSSE Spec".
33 * The datasheet for the ftdichip.com's FT2232D part is here:
34 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
42 /* project specific includes */
43 #include "interface.h"
45 #include "time_support.h"
53 #if (BUILD_FT2232_FTD2XX==1 && BUILD_FT2232_LIBFTDI==1)
54 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
55 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
56 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
59 /* FT2232 access library includes */
60 #if BUILD_FT2232_FTD2XX == 1
62 #elif BUILD_FT2232_LIBFTDI == 1
66 /* max TCK for the high speed devices 30000 kHz */
67 #define FTDI_2232H_4232H_MAX_TCK 30000
69 static int ft2232_execute_queue(void);
71 static int ft2232_speed(int speed
);
72 static int ft2232_speed_div(int speed
, int* khz
);
73 static int ft2232_khz(int khz
, int* jtag_speed
);
74 static int ft2232_register_commands(struct command_context_s
* cmd_ctx
);
75 static int ft2232_init(void);
76 static int ft2232_quit(void);
78 static int ft2232_handle_device_desc_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
79 static int ft2232_handle_serial_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
80 static int ft2232_handle_layout_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
81 static int ft2232_handle_vid_pid_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
82 static int ft2232_handle_latency_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
86 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
87 * stable state. Calling code must ensure that current state is stable,
88 * that verification is not done in here.
90 * @param num_cycles The number of clocks cycles to send.
91 * @param cmd The command to send.
93 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
95 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
);
97 /* max TCK for the high speed devices 30000 kHz */
98 #define FTDI_2232H_4232H_MAX_TCK 30000
100 static char * ft2232_device_desc_A
= NULL
;
101 static char* ft2232_device_desc
= NULL
;
102 static char* ft2232_serial
= NULL
;
103 static char* ft2232_layout
= NULL
;
104 static uint8_t ft2232_latency
= 2;
105 static unsigned ft2232_max_tck
= 6000;
108 #define MAX_USB_IDS 8
109 /* vid = pid = 0 marks the end of the list */
110 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
111 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
113 typedef struct ft2232_layout_s
117 void (*reset
)(int trst
, int srst
);
121 /* init procedures for supported layouts */
122 static int usbjtag_init(void);
123 static int jtagkey_init(void);
124 static int olimex_jtag_init(void);
125 static int flyswatter_init(void);
126 static int turtle_init(void);
127 static int comstick_init(void);
128 static int stm32stick_init(void);
129 static int axm0432_jtag_init(void);
130 static int sheevaplug_init(void);
131 static int icebear_jtag_init(void);
132 static int cortino_jtag_init(void);
134 /* reset procedures for supported layouts */
135 static void usbjtag_reset(int trst
, int srst
);
136 static void jtagkey_reset(int trst
, int srst
);
137 static void olimex_jtag_reset(int trst
, int srst
);
138 static void flyswatter_reset(int trst
, int srst
);
139 static void turtle_reset(int trst
, int srst
);
140 static void comstick_reset(int trst
, int srst
);
141 static void stm32stick_reset(int trst
, int srst
);
142 static void axm0432_jtag_reset(int trst
, int srst
);
143 static void sheevaplug_reset(int trst
, int srst
);
144 static void icebear_jtag_reset(int trst
, int srst
);
146 /* blink procedures for layouts that support a blinking led */
147 static void olimex_jtag_blink(void);
148 static void flyswatter_jtag_blink(void);
149 static void turtle_jtag_blink(void);
151 static const ft2232_layout_t ft2232_layouts
[] =
153 { "usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
154 { "jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
155 { "jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
156 { "oocdlink", jtagkey_init
, jtagkey_reset
, NULL
},
157 { "signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
158 { "evb_lm3s811", usbjtag_init
, usbjtag_reset
, NULL
},
159 { "olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
160 { "flyswatter", flyswatter_init
, flyswatter_reset
, flyswatter_jtag_blink
},
161 { "turtelizer2", turtle_init
, turtle_reset
, turtle_jtag_blink
},
162 { "comstick", comstick_init
, comstick_reset
, NULL
},
163 { "stm32stick", stm32stick_init
, stm32stick_reset
, NULL
},
164 { "axm0432_jtag", axm0432_jtag_init
, axm0432_jtag_reset
, NULL
},
165 { "sheevaplug", sheevaplug_init
, sheevaplug_reset
, NULL
},
166 { "icebear", icebear_jtag_init
, icebear_jtag_reset
, NULL
},
167 { "cortino", cortino_jtag_init
, comstick_reset
, NULL
},
168 { NULL
, NULL
, NULL
, NULL
},
171 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
173 static const ft2232_layout_t
*layout
;
174 static uint8_t low_output
= 0x0;
175 static uint8_t low_direction
= 0x0;
176 static uint8_t high_output
= 0x0;
177 static uint8_t high_direction
= 0x0;
179 #if BUILD_FT2232_FTD2XX == 1
180 static FT_HANDLE ftdih
= NULL
;
181 static FT_DEVICE ftdi_device
= 0;
182 #elif BUILD_FT2232_LIBFTDI == 1
183 static struct ftdi_context ftdic
;
187 static jtag_command_t
* first_unsent
; /* next command that has to be sent */
188 static int require_send
;
191 /* http://urjtag.wiki.sourceforge.net/Cable+FT2232 says:
193 "There is a significant difference between libftdi and libftd2xx. The latter
194 one allows to schedule up to 64*64 bytes of result data while libftdi fails
195 with more than 4*64. As a consequence, the FT2232 driver is forced to
196 perform around 16x more USB transactions for long command streams with TDO
197 capture when running with libftdi."
200 #define FT2232_BUFFER_SIZE 131072
201 a comment would have been nice.
204 #define FT2232_BUFFER_SIZE 131072
206 static uint8_t* ft2232_buffer
= NULL
;
207 static int ft2232_buffer_size
= 0;
208 static int ft2232_read_pointer
= 0;
209 static int ft2232_expect_read
= 0;
212 * Function buffer_write
213 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
214 * @param val is the byte to send.
216 static inline void buffer_write(uint8_t val
)
218 assert(ft2232_buffer
);
219 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
220 ft2232_buffer
[ft2232_buffer_size
++] = val
;
224 * Function buffer_read
225 * returns a byte from the byte buffer.
227 static inline uint8_t buffer_read(void)
229 assert(ft2232_buffer
);
230 assert(ft2232_read_pointer
< ft2232_buffer_size
);
231 return ft2232_buffer
[ft2232_read_pointer
++];
236 * Clocks out \a bit_count bits on the TMS line, starting with the least
237 * significant bit of tms_bits and progressing to more significant bits.
238 * Rigorous state transition logging is done here via tap_set_state().
240 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
241 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
242 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
243 * is often used for this, 0x4b.
245 * @param tms_bits Holds the sequence of bits to send.
246 * @param tms_count Tells how many bits in the sequence.
247 * @param tdi_bit A single bit to pass on to TDI before the first TCK
248 * cycle and held static for the duration of TMS clocking.
250 * See the MPSSE spec referenced above.
252 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
256 int tms_ndx
; /* bit index into tms_byte */
258 assert(tms_count
> 0);
260 // LOG_DEBUG("mpsse cmd=%02x, tms_bits=0x%08x, bit_count=%d", mpsse_cmd, tms_bits, tms_count);
262 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
264 bool bit
= tms_bits
& 1;
267 tms_byte
|= (1<<tms_ndx
);
269 /* always do state transitions in public view */
270 tap_set_state(tap_state_transition(tap_get_state(), bit
));
272 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
277 if (tms_ndx
==7 || i
==tms_count
-1)
279 buffer_write(mpsse_cmd
);
280 buffer_write(tms_ndx
- 1);
282 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
283 TMS/CS and is held static for the duration of TMS/CS clocking.
285 buffer_write(tms_byte
| (tdi_bit
<< 7));
292 * Function get_tms_buffer_requirements
293 * returns what clock_tms() will consume if called with
296 static inline int get_tms_buffer_requirements(int bit_count
)
298 return ((bit_count
+ 6)/7) * 3;
303 * Function move_to_state
304 * moves the TAP controller from the current state to a
305 * \a goal_state through a path given by tap_get_tms_path(). State transition
306 * logging is performed by delegation to clock_tms().
308 * @param goal_state is the destination state for the move.
310 static void move_to_state(tap_state_t goal_state
)
312 tap_state_t start_state
= tap_get_state();
314 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
315 lookup of the required TMS pattern to move to this state from the
319 /* do the 2 lookups */
320 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
321 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
323 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
325 clock_tms(0x4b, tms_bits
, tms_count
, 0);
329 jtag_interface_t ft2232_interface
=
332 .execute_queue
= ft2232_execute_queue
,
333 .speed
= ft2232_speed
,
334 .speed_div
= ft2232_speed_div
,
336 .register_commands
= ft2232_register_commands
,
341 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
343 #if BUILD_FT2232_FTD2XX == 1
345 DWORD dw_bytes_written
;
346 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
348 *bytes_written
= dw_bytes_written
;
349 LOG_ERROR("FT_Write returned: %lu", status
);
350 return ERROR_JTAG_DEVICE_ERROR
;
354 *bytes_written
= dw_bytes_written
;
357 #elif BUILD_FT2232_LIBFTDI == 1
359 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
362 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
363 return ERROR_JTAG_DEVICE_ERROR
;
367 *bytes_written
= retval
;
374 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
376 #if BUILD_FT2232_FTD2XX == 1
382 while ((*bytes_read
< size
) && timeout
--)
384 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
385 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
388 LOG_ERROR("FT_Read returned: %lu", status
);
389 return ERROR_JTAG_DEVICE_ERROR
;
391 *bytes_read
+= dw_bytes_read
;
394 #elif BUILD_FT2232_LIBFTDI == 1
399 while ((*bytes_read
< size
) && timeout
--)
401 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
404 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
405 return ERROR_JTAG_DEVICE_ERROR
;
407 *bytes_read
+= retval
;
412 if (*bytes_read
< size
)
414 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)",
415 (unsigned int)(*bytes_read
),
417 return ERROR_JTAG_DEVICE_ERROR
;
423 #ifdef BUILD_FTD2XX_HIGHSPEED
424 static bool ft2232_device_is_highspeed(void)
426 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
429 static int ft2232_adaptive_clocking(int speed
)
431 bool use_adaptive_clocking
= FALSE
;
434 if (ft2232_device_is_highspeed())
435 use_adaptive_clocking
= TRUE
;
438 LOG_ERROR("ft2232 device %lu does not support RTCK", ftdi_device
);
443 uint8_t buf
= use_adaptive_clocking
? 0x96 : 0x97;
444 LOG_DEBUG("%2.2x", buf
);
446 uint32_t bytes_written
;
447 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
448 if (ERROR_OK
!= retval
|| bytes_written
!= 1)
450 LOG_ERROR("unable to set adative clocking: %d", retval
);
457 static int ft2232_adaptive_clocking(int speed
)
459 // not implemented on low-speed devices
460 return speed
? ERROR_OK
: -1234;
464 static int ft2232_speed(int speed
)
468 uint32_t bytes_written
;
470 ft2232_adaptive_clocking(speed
);
472 buf
[0] = 0x86; /* command "set divisor" */
473 buf
[1] = speed
& 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
474 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
476 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
477 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
479 LOG_ERROR("couldn't set FT2232 TCK speed");
487 static int ft2232_speed_div(int speed
, int* khz
)
489 /* Take a look in the FT2232 manual,
490 * AN2232C-01 Command Processor for
491 * MPSSE and MCU Host Bus. Chapter 3.8 */
493 *khz
= ft2232_max_tck
/ (1 + speed
);
499 static int ft2232_khz(int khz
, int* jtag_speed
)
503 #ifdef BUILD_FTD2XX_HIGHSPEED
507 LOG_DEBUG("RCLK not supported");
508 LOG_DEBUG("If you have a high-speed FTDI device, then "
509 "OpenOCD may be built with --enable-ftd2xx-highspeed.");
514 /* Take a look in the FT2232 manual,
515 * AN2232C-01 Command Processor for
516 * MPSSE and MCU Host Bus. Chapter 3.8
518 * We will calc here with a multiplier
519 * of 10 for better rounding later. */
521 /* Calc speed, (ft2232_max_tck / khz) - 1 */
522 /* Use 65000 for better rounding */
523 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
525 /* Add 0.9 for rounding */
528 /* Calc real speed */
529 *jtag_speed
= *jtag_speed
/ 10;
531 /* Check if speed is greater than 0 */
537 /* Check max value */
538 if (*jtag_speed
> 0xFFFF)
540 *jtag_speed
= 0xFFFF;
547 static int ft2232_register_commands(struct command_context_s
* cmd_ctx
)
549 register_command(cmd_ctx
, NULL
, "ft2232_device_desc", ft2232_handle_device_desc_command
,
550 COMMAND_CONFIG
, "the USB device description of the FTDI FT2232 device");
551 register_command(cmd_ctx
, NULL
, "ft2232_serial", ft2232_handle_serial_command
,
552 COMMAND_CONFIG
, "the serial number of the FTDI FT2232 device");
553 register_command(cmd_ctx
, NULL
, "ft2232_layout", ft2232_handle_layout_command
,
554 COMMAND_CONFIG
, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
555 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid", ft2232_handle_vid_pid_command
,
556 COMMAND_CONFIG
, "the vendor ID and product ID of the FTDI FT2232 device");
557 register_command(cmd_ctx
, NULL
, "ft2232_latency", ft2232_handle_latency_command
,
558 COMMAND_CONFIG
, "set the FT2232 latency timer to a new value");
563 static void ft2232_end_state(tap_state_t state
)
565 if (tap_is_state_stable(state
))
566 tap_set_end_state(state
);
569 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
574 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
576 int num_bytes
= (scan_size
+ 7) / 8;
577 int bits_left
= scan_size
;
580 while (num_bytes
-- > 1)
582 buffer
[cur_byte
++] = buffer_read();
586 buffer
[cur_byte
] = 0x0;
588 /* There is one more partial byte left from the clock data in/out instructions */
591 buffer
[cur_byte
] = buffer_read() >> 1;
593 /* 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 */
594 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
598 static void ft2232_debug_dump_buffer(void)
604 for (i
= 0; i
< ft2232_buffer_size
; i
++)
606 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
609 LOG_DEBUG("%s", line
);
615 LOG_DEBUG("%s", line
);
619 static int ft2232_send_and_recv(jtag_command_t
* first
, jtag_command_t
* last
)
626 uint32_t bytes_written
=0;
627 uint32_t bytes_read
=0;
629 #ifdef _DEBUG_USB_IO_
630 struct timeval start
, inter
, inter2
, end
;
631 struct timeval d_inter
, d_inter2
, d_end
;
634 #ifdef _DEBUG_USB_COMMS_
635 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
636 ft2232_debug_dump_buffer();
639 #ifdef _DEBUG_USB_IO_
640 gettimeofday(&start
, NULL
);
643 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
645 LOG_ERROR("couldn't write MPSSE commands to FT2232");
649 #ifdef _DEBUG_USB_IO_
650 gettimeofday(&inter
, NULL
);
653 if (ft2232_expect_read
)
656 ft2232_buffer_size
= 0;
658 #ifdef _DEBUG_USB_IO_
659 gettimeofday(&inter2
, NULL
);
662 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
664 LOG_ERROR("couldn't read from FT2232");
668 #ifdef _DEBUG_USB_IO_
669 gettimeofday(&end
, NULL
);
671 timeval_subtract(&d_inter
, &inter
, &start
);
672 timeval_subtract(&d_inter2
, &inter2
, &start
);
673 timeval_subtract(&d_end
, &end
, &start
);
675 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
676 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
677 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
678 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
681 ft2232_buffer_size
= bytes_read
;
683 if (ft2232_expect_read
!= ft2232_buffer_size
)
685 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read
,
688 ft2232_debug_dump_buffer();
693 #ifdef _DEBUG_USB_COMMS_
694 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout
, ft2232_buffer_size
);
695 ft2232_debug_dump_buffer();
699 ft2232_expect_read
= 0;
700 ft2232_read_pointer
= 0;
702 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
703 * that wasn't handled by a caller-provided error handler
713 type
= jtag_scan_type(cmd
->cmd
.scan
);
714 if (type
!= SCAN_OUT
)
716 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
717 buffer
= calloc(CEIL(scan_size
, 8), 1);
718 ft2232_read_scan(type
, buffer
, scan_size
);
719 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
720 retval
= ERROR_JTAG_QUEUE_FAILED
;
732 ft2232_buffer_size
= 0;
739 * Function ft2232_add_pathmove
740 * moves the TAP controller from the current state to a new state through the
741 * given path, where path is an array of tap_state_t's.
743 * @param path is an array of tap_stat_t which gives the states to traverse through
744 * ending with the last state at path[num_states-1]
745 * @param num_states is the count of state steps to move through
747 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
751 tap_state_t walker
= tap_get_state();
753 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
755 /* this loop verifies that the path is legal and logs each state in the path */
756 for (state_ndx
= 0; state_ndx
< num_states
; ++state_ndx
)
758 tap_state_t desired_next_state
= path
[state_ndx
];
760 if (tap_state_transition(walker
, false) == desired_next_state
)
761 ; /* bit within tms_bits at index state_ndx is already zero */
762 else if (tap_state_transition(walker
, true) == desired_next_state
)
763 tms_bits
|= (1<<state_ndx
);
766 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
767 tap_state_name(walker
), tap_state_name(desired_next_state
));
771 walker
= desired_next_state
;
774 clock_tms(0x4b, tms_bits
, num_states
, 0);
776 tap_set_end_state(tap_get_state());
780 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
782 int num_bytes
= (scan_size
+ 7) / 8;
783 int bits_left
= scan_size
;
789 if (tap_get_state() != TAP_DRSHIFT
)
791 move_to_state(TAP_DRSHIFT
);
796 if (tap_get_state() != TAP_IRSHIFT
)
798 move_to_state(TAP_IRSHIFT
);
802 /* add command for complete bytes */
803 while (num_bytes
> 1)
808 /* Clock Data Bytes In and Out LSB First */
810 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
812 else if (type
== SCAN_OUT
)
814 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
816 /* LOG_DEBUG("added TDI bytes (o)"); */
818 else if (type
== SCAN_IN
)
820 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
822 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
825 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
826 num_bytes
-= thisrun_bytes
;
828 buffer_write((uint8_t) (thisrun_bytes
- 1));
829 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
833 /* add complete bytes */
834 while (thisrun_bytes
-- > 0)
836 buffer_write(buffer
[cur_byte
++]);
840 else /* (type == SCAN_IN) */
842 bits_left
-= 8 * (thisrun_bytes
);
846 /* the most signifcant bit is scanned during TAP movement */
848 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
852 /* process remaining bits but the last one */
857 /* Clock Data Bits In and Out LSB First */
859 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
861 else if (type
== SCAN_OUT
)
863 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
865 /* LOG_DEBUG("added TDI bits (o)"); */
867 else if (type
== SCAN_IN
)
869 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
871 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
874 buffer_write(bits_left
- 2);
876 buffer_write(buffer
[cur_byte
]);
879 if (( ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
880 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
884 /* Clock Data Bits In and Out LSB First */
886 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
888 else if (type
== SCAN_OUT
)
890 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
892 /* LOG_DEBUG("added TDI bits (o)"); */
894 else if (type
== SCAN_IN
)
896 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
898 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
901 buffer_write(last_bit
);
909 /* move from Shift-IR/DR to end state */
910 if (type
!= SCAN_OUT
)
912 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
913 /* This must be coordinated with the bit shifts in ft2232_read_scan */
916 /* Clock Data to TMS/CS Pin with Read */
918 /* LOG_DEBUG("added TMS scan (read)"); */
922 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
923 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
924 /* Clock Data to TMS/CS Pin (no Read) */
926 /* LOG_DEBUG("added TMS scan (no read)"); */
929 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
932 if (tap_get_state() != tap_get_end_state())
934 move_to_state(tap_get_end_state());
939 static int ft2232_large_scan(scan_command_t
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
941 int num_bytes
= (scan_size
+ 7) / 8;
942 int bits_left
= scan_size
;
945 uint8_t* receive_buffer
= malloc(CEIL(scan_size
, 8));
946 uint8_t* receive_pointer
= receive_buffer
;
947 uint32_t bytes_written
;
950 int thisrun_read
= 0;
954 LOG_ERROR("BUG: large IR scans are not supported");
958 if (tap_get_state() != TAP_DRSHIFT
)
960 move_to_state(TAP_DRSHIFT
);
963 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
965 LOG_ERROR("couldn't write MPSSE commands to FT2232");
968 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
969 ft2232_buffer_size
, (int)bytes_written
);
970 ft2232_buffer_size
= 0;
972 /* add command for complete bytes */
973 while (num_bytes
> 1)
979 /* Clock Data Bytes In and Out LSB First */
981 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
983 else if (type
== SCAN_OUT
)
985 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
987 /* LOG_DEBUG("added TDI bytes (o)"); */
989 else if (type
== SCAN_IN
)
991 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
993 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
996 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
997 thisrun_read
= thisrun_bytes
;
998 num_bytes
-= thisrun_bytes
;
999 buffer_write((uint8_t) (thisrun_bytes
- 1));
1000 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1002 if (type
!= SCAN_IN
)
1004 /* add complete bytes */
1005 while (thisrun_bytes
-- > 0)
1007 buffer_write(buffer
[cur_byte
]);
1012 else /* (type == SCAN_IN) */
1014 bits_left
-= 8 * (thisrun_bytes
);
1017 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1019 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1022 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1024 (int)bytes_written
);
1025 ft2232_buffer_size
= 0;
1027 if (type
!= SCAN_OUT
)
1029 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1031 LOG_ERROR("couldn't read from FT2232");
1034 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1037 receive_pointer
+= bytes_read
;
1043 /* the most signifcant bit is scanned during TAP movement */
1044 if (type
!= SCAN_IN
)
1045 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1049 /* process remaining bits but the last one */
1052 if (type
== SCAN_IO
)
1054 /* Clock Data Bits In and Out LSB First */
1056 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1058 else if (type
== SCAN_OUT
)
1060 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1062 /* LOG_DEBUG("added TDI bits (o)"); */
1064 else if (type
== SCAN_IN
)
1066 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1068 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1070 buffer_write(bits_left
- 2);
1071 if (type
!= SCAN_IN
)
1072 buffer_write(buffer
[cur_byte
]);
1074 if (type
!= SCAN_OUT
)
1078 if (tap_get_end_state() == TAP_DRSHIFT
)
1080 if (type
== SCAN_IO
)
1082 /* Clock Data Bits In and Out LSB First */
1084 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1086 else if (type
== SCAN_OUT
)
1088 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1090 /* LOG_DEBUG("added TDI bits (o)"); */
1092 else if (type
== SCAN_IN
)
1094 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1096 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1099 buffer_write(last_bit
);
1103 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1104 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1107 /* move from Shift-IR/DR to end state */
1108 if (type
!= SCAN_OUT
)
1110 /* Clock Data to TMS/CS Pin with Read */
1112 /* LOG_DEBUG("added TMS scan (read)"); */
1116 /* Clock Data to TMS/CS Pin (no Read) */
1118 /* LOG_DEBUG("added TMS scan (no read)"); */
1121 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1124 if (type
!= SCAN_OUT
)
1127 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1129 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1132 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1134 (int)bytes_written
);
1135 ft2232_buffer_size
= 0;
1137 if (type
!= SCAN_OUT
)
1139 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1141 LOG_ERROR("couldn't read from FT2232");
1144 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1147 receive_pointer
+= bytes_read
;
1154 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1156 int predicted_size
= 3;
1157 int num_bytes
= (scan_size
- 1) / 8;
1159 if (tap_get_state() != TAP_DRSHIFT
)
1160 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1162 if (type
== SCAN_IN
) /* only from device to host */
1164 /* complete bytes */
1165 predicted_size
+= CEIL(num_bytes
, 65536) * 3;
1167 /* remaining bits - 1 (up to 7) */
1168 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1170 else /* host to device, or bidirectional */
1172 /* complete bytes */
1173 predicted_size
+= num_bytes
+ CEIL(num_bytes
, 65536) * 3;
1175 /* remaining bits -1 (up to 7) */
1176 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1179 return predicted_size
;
1183 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1185 int predicted_size
= 0;
1187 if (type
!= SCAN_OUT
)
1189 /* complete bytes */
1190 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) - 1) : 0;
1192 /* remaining bits - 1 */
1193 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1195 /* last bit (from TMS scan) */
1196 predicted_size
+= 1;
1199 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1201 return predicted_size
;
1205 static void usbjtag_reset(int trst
, int srst
)
1207 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1210 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1211 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1213 low_output
&= ~nTRST
; /* switch output low */
1217 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1218 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1220 low_output
|= nTRST
; /* switch output high */
1225 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1226 low_output
&= ~nSRST
; /* switch output low */
1228 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1232 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1233 low_output
|= nSRST
; /* switch output high */
1235 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1238 /* command "set data bits low byte" */
1240 buffer_write(low_output
);
1241 buffer_write(low_direction
);
1245 static void jtagkey_reset(int trst
, int srst
)
1247 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1250 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1251 high_output
&= ~nTRSTnOE
;
1253 high_output
&= ~nTRST
;
1257 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1258 high_output
|= nTRSTnOE
;
1260 high_output
|= nTRST
;
1265 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1266 high_output
&= ~nSRST
;
1268 high_output
&= ~nSRSTnOE
;
1272 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1273 high_output
|= nSRST
;
1275 high_output
|= nSRSTnOE
;
1278 /* command "set data bits high byte" */
1280 buffer_write(high_output
);
1281 buffer_write(high_direction
);
1282 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1287 static void olimex_jtag_reset(int trst
, int srst
)
1289 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1292 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1293 high_output
&= ~nTRSTnOE
;
1295 high_output
&= ~nTRST
;
1299 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1300 high_output
|= nTRSTnOE
;
1302 high_output
|= nTRST
;
1307 high_output
|= nSRST
;
1311 high_output
&= ~nSRST
;
1314 /* command "set data bits high byte" */
1316 buffer_write(high_output
);
1317 buffer_write(high_direction
);
1318 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1323 static void axm0432_jtag_reset(int trst
, int srst
)
1327 tap_set_state(TAP_RESET
);
1328 high_output
&= ~nTRST
;
1332 high_output
|= nTRST
;
1337 high_output
&= ~nSRST
;
1341 high_output
|= nSRST
;
1344 /* command "set data bits low byte" */
1346 buffer_write(high_output
);
1347 buffer_write(high_direction
);
1348 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1353 static void flyswatter_reset(int trst
, int srst
)
1357 low_output
&= ~nTRST
;
1361 low_output
|= nTRST
;
1366 low_output
|= nSRST
;
1370 low_output
&= ~nSRST
;
1373 /* command "set data bits low byte" */
1375 buffer_write(low_output
);
1376 buffer_write(low_direction
);
1377 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1381 static void turtle_reset(int trst
, int srst
)
1387 low_output
|= nSRST
;
1391 low_output
&= ~nSRST
;
1394 /* command "set data bits low byte" */
1396 buffer_write(low_output
);
1397 buffer_write(low_direction
);
1398 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1402 static void comstick_reset(int trst
, int srst
)
1406 high_output
&= ~nTRST
;
1410 high_output
|= nTRST
;
1415 high_output
&= ~nSRST
;
1419 high_output
|= nSRST
;
1422 /* command "set data bits high byte" */
1424 buffer_write(high_output
);
1425 buffer_write(high_direction
);
1426 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1431 static void stm32stick_reset(int trst
, int srst
)
1435 high_output
&= ~nTRST
;
1439 high_output
|= nTRST
;
1444 low_output
&= ~nSRST
;
1448 low_output
|= nSRST
;
1451 /* command "set data bits low byte" */
1453 buffer_write(low_output
);
1454 buffer_write(low_direction
);
1456 /* command "set data bits high byte" */
1458 buffer_write(high_output
);
1459 buffer_write(high_direction
);
1460 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1466 static void sheevaplug_reset(int trst
, int srst
)
1469 high_output
&= ~nTRST
;
1471 high_output
|= nTRST
;
1474 high_output
&= ~nSRSTnOE
;
1476 high_output
|= nSRSTnOE
;
1478 /* command "set data bits high byte" */
1480 buffer_write(high_output
);
1481 buffer_write(high_direction
);
1482 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1485 static int ft2232_execute_runtest(jtag_command_t
*cmd
)
1489 int predicted_size
= 0;
1492 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1493 cmd
->cmd
.runtest
->num_cycles
,
1494 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1496 /* only send the maximum buffer size that FT2232C can handle */
1498 if (tap_get_state() != TAP_IDLE
)
1499 predicted_size
+= 3;
1500 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
1501 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1502 predicted_size
+= 3;
1503 if (tap_get_end_state() != TAP_IDLE
)
1504 predicted_size
+= 3;
1505 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1507 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1508 retval
= ERROR_JTAG_QUEUE_FAILED
;
1512 if (tap_get_state() != TAP_IDLE
)
1514 move_to_state(TAP_IDLE
);
1517 i
= cmd
->cmd
.runtest
->num_cycles
;
1520 /* there are no state transitions in this code, so omit state tracking */
1522 /* command "Clock Data to TMS/CS Pin (no Read)" */
1526 buffer_write((i
> 7) ? 6 : (i
- 1));
1530 tap_set_state(TAP_IDLE
);
1532 i
-= (i
> 7) ? 7 : i
;
1533 /* LOG_DEBUG("added TMS scan (no read)"); */
1536 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1538 if (tap_get_state() != tap_get_end_state())
1540 move_to_state(tap_get_end_state());
1544 #ifdef _DEBUG_JTAG_IO_
1545 LOG_DEBUG("runtest: %i, end in %s", cmd
->cmd
.runtest
->num_cycles
, tap_state_name(tap_get_end_state()));
1552 static int ft2232_execute_statemove(jtag_command_t
*cmd
)
1554 int predicted_size
= 0;
1555 int retval
= ERROR_OK
;
1557 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1559 /* only send the maximum buffer size that FT2232C can handle */
1561 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1563 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1564 retval
= ERROR_JTAG_QUEUE_FAILED
;
1568 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1570 /* move to end state */
1571 if (tap_get_state() != tap_get_end_state())
1573 move_to_state(tap_get_end_state());
1580 static int ft2232_execute_pathmove(jtag_command_t
*cmd
)
1582 int predicted_size
= 0;
1583 int retval
= ERROR_OK
;
1585 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1586 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1588 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1589 tap_state_name(tap_get_state()),
1590 tap_state_name(path
[num_states
-1])
1593 /* only send the maximum buffer size that FT2232C can handle */
1594 predicted_size
= 3 * CEIL(num_states
, 7);
1595 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1597 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1598 retval
= ERROR_JTAG_QUEUE_FAILED
;
1604 ft2232_add_pathmove(path
, num_states
);
1611 static int ft2232_execute_scan(jtag_command_t
*cmd
)
1614 int scan_size
; /* size of IR or DR scan */
1615 int predicted_size
= 0;
1616 int retval
= ERROR_OK
;
1618 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1620 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1622 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1624 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1625 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1627 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1628 /* unsent commands before this */
1629 if (first_unsent
!= cmd
)
1630 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1631 retval
= ERROR_JTAG_QUEUE_FAILED
;
1633 /* current command */
1634 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1635 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1637 first_unsent
= cmd
->next
;
1642 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1644 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1647 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1648 retval
= ERROR_JTAG_QUEUE_FAILED
;
1652 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1653 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1654 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1655 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1659 #ifdef _DEBUG_JTAG_IO_
1660 LOG_DEBUG("%s scan, %i bits, end in %s", (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1661 tap_state_name(tap_get_end_state()));
1667 static int ft2232_execute_reset(jtag_command_t
*cmd
)
1670 int predicted_size
= 0;
1673 DEBUG_JTAG_IO("reset trst: %i srst %i",
1674 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1676 /* only send the maximum buffer size that FT2232C can handle */
1678 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1680 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1681 retval
= ERROR_JTAG_QUEUE_FAILED
;
1686 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1689 #ifdef _DEBUG_JTAG_IO_
1690 LOG_DEBUG("trst: %i, srst: %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1695 static int ft2232_execute_sleep(jtag_command_t
*cmd
)
1700 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
1702 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1703 retval
= ERROR_JTAG_QUEUE_FAILED
;
1704 first_unsent
= cmd
->next
;
1705 jtag_sleep(cmd
->cmd
.sleep
->us
);
1706 #ifdef _DEBUG_JTAG_IO_
1707 LOG_DEBUG("sleep %i usec while in %s", cmd
->cmd
.sleep
->us
, tap_state_name(tap_get_state()));
1713 static int ft2232_execute_stableclocks(jtag_command_t
*cmd
)
1718 /* this is only allowed while in a stable state. A check for a stable
1719 * state was done in jtag_add_clocks()
1721 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1722 retval
= ERROR_JTAG_QUEUE_FAILED
;
1723 #ifdef _DEBUG_JTAG_IO_
1724 LOG_DEBUG("clocks %i while in %s", cmd
->cmd
.stableclocks
->num_cycles
, tap_state_name(tap_get_state()));
1730 static int ft2232_execute_command(jtag_command_t
*cmd
)
1737 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1738 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1739 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1740 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1741 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1742 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1743 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1745 LOG_ERROR("BUG: unknown JTAG command type encountered");
1751 static int ft2232_execute_queue()
1753 jtag_command_t
* cmd
= jtag_command_queue
; /* currently processed command */
1756 first_unsent
= cmd
; /* next command that has to be sent */
1759 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1760 * that wasn't handled by a caller-provided error handler
1764 ft2232_buffer_size
= 0;
1765 ft2232_expect_read
= 0;
1767 /* blink, if the current layout has that feature */
1773 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1774 retval
= ERROR_JTAG_QUEUE_FAILED
;
1775 /* Start reading input before FT2232 TX buffer fills up */
1777 if (ft2232_expect_read
> 256)
1779 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1780 retval
= ERROR_JTAG_QUEUE_FAILED
;
1785 if (require_send
> 0)
1786 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1787 retval
= ERROR_JTAG_QUEUE_FAILED
;
1793 #if BUILD_FT2232_FTD2XX == 1
1794 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1798 char SerialNumber
[16];
1799 char Description
[64];
1800 DWORD openex_flags
= 0;
1801 char* openex_string
= NULL
;
1802 uint8_t latency_timer
;
1804 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
1807 /* Add non-standard Vid/Pid to the linux driver */
1808 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1810 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
1814 if (ft2232_device_desc
&& ft2232_serial
)
1816 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1817 ft2232_device_desc
= NULL
;
1820 if (ft2232_device_desc
)
1822 openex_string
= ft2232_device_desc
;
1823 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1825 else if (ft2232_serial
)
1827 openex_string
= ft2232_serial
;
1828 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1832 LOG_ERROR("neither device description nor serial number specified");
1833 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1835 return ERROR_JTAG_INIT_FAILED
;
1838 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1839 if (status
!= FT_OK
) {
1840 // under Win32, the FTD2XX driver appends an "A" to the end
1841 // of the description, if we tried by the desc, then
1842 // try by the alternate "A" description.
1843 if (openex_string
== ft2232_device_desc
) {
1844 // Try the alternate method.
1845 openex_string
= ft2232_device_desc_A
;
1846 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1847 if (status
== FT_OK
) {
1848 // yea, the "alternate" method worked!
1850 // drat, give the user a meaningfull message.
1851 // telling the use we tried *BOTH* methods.
1852 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1854 ft2232_device_desc_A
);
1859 if (status
!= FT_OK
)
1865 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
1867 return ERROR_JTAG_INIT_FAILED
;
1869 LOG_ERROR("unable to open ftdi device: %lu", status
);
1870 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1871 if (status
== FT_OK
)
1873 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1876 for (i
= 0; i
< num_devices
; i
++)
1877 desc_array
[i
] = malloc(64);
1879 desc_array
[num_devices
] = NULL
;
1881 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1883 if (status
== FT_OK
)
1885 LOG_ERROR("ListDevices: %lu\n", num_devices
);
1886 for (i
= 0; i
< num_devices
; i
++)
1887 LOG_ERROR("%i: \"%s\"", i
, desc_array
[i
]);
1890 for (i
= 0; i
< num_devices
; i
++)
1891 free(desc_array
[i
]);
1897 LOG_ERROR("ListDevices: NONE\n");
1899 return ERROR_JTAG_INIT_FAILED
;
1902 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1904 LOG_ERROR("unable to set latency timer: %lu", status
);
1905 return ERROR_JTAG_INIT_FAILED
;
1908 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1910 LOG_ERROR("unable to get latency timer: %lu", status
);
1911 return ERROR_JTAG_INIT_FAILED
;
1915 LOG_DEBUG("current latency timer: %i", latency_timer
);
1918 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1920 LOG_ERROR("unable to set timeouts: %lu", status
);
1921 return ERROR_JTAG_INIT_FAILED
;
1924 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1926 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
1927 return ERROR_JTAG_INIT_FAILED
;
1930 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
1932 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
1933 return ERROR_JTAG_INIT_FAILED
;
1937 LOG_INFO("device: %lu", ftdi_device
);
1938 LOG_INFO("deviceID: %lu", deviceID
);
1939 LOG_INFO("SerialNumber: %s", SerialNumber
);
1940 LOG_INFO("Description: %s", Description
);
1942 #ifdef BUILD_FTD2XX_HIGHSPEED
1943 if (ft2232_device_is_highspeed())
1945 ft2232_max_tck
= FTDI_2232H_4232H_MAX_TCK
;
1946 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
1955 static int ft2232_purge_ftd2xx(void)
1959 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1961 LOG_ERROR("error purging ftd2xx device: %lu", status
);
1962 return ERROR_JTAG_INIT_FAILED
;
1969 #endif /* BUILD_FT2232_FTD2XX == 1 */
1971 #if BUILD_FT2232_LIBFTDI == 1
1972 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1974 uint8_t latency_timer
;
1976 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1977 ft2232_layout
, vid
, pid
);
1979 if (ftdi_init(&ftdic
) < 0)
1980 return ERROR_JTAG_INIT_FAILED
;
1982 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
1984 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
1985 return ERROR_JTAG_INIT_FAILED
;
1988 /* context, vendor id, product id */
1989 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
1993 LOG_WARNING("unable to open ftdi device (trying more): %s",
1996 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
1998 return ERROR_JTAG_INIT_FAILED
;
2001 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2002 if (ftdi_usb_reset(&ftdic
) < 0)
2004 LOG_ERROR("unable to reset ftdi device");
2005 return ERROR_JTAG_INIT_FAILED
;
2008 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2010 LOG_ERROR("unable to set latency timer");
2011 return ERROR_JTAG_INIT_FAILED
;
2014 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2016 LOG_ERROR("unable to get latency timer");
2017 return ERROR_JTAG_INIT_FAILED
;
2021 LOG_DEBUG("current latency timer: %i", latency_timer
);
2024 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2030 static int ft2232_purge_libftdi(void)
2032 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2034 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2035 return ERROR_JTAG_INIT_FAILED
;
2042 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2044 static int ft2232_init(void)
2048 uint32_t bytes_written
;
2049 const ft2232_layout_t
* cur_layout
= ft2232_layouts
;
2052 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
)==7)
2054 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2058 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2061 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2063 ft2232_layout
= "usbjtag";
2064 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2067 while (cur_layout
->name
)
2069 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2071 layout
= cur_layout
;
2079 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2080 return ERROR_JTAG_INIT_FAILED
;
2086 * "more indicates that there are more IDs to try, so we should
2087 * not print an error for an ID mismatch (but for anything
2090 * try_more indicates that the error code returned indicates an
2091 * ID mismatch (and nothing else) and that we should proceeed
2092 * with the next ID pair.
2094 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2097 #if BUILD_FT2232_FTD2XX == 1
2098 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2100 #elif BUILD_FT2232_LIBFTDI == 1
2101 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2106 if (!more
|| !try_more
)
2110 ft2232_buffer_size
= 0;
2111 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2113 if (layout
->init() != ERROR_OK
)
2114 return ERROR_JTAG_INIT_FAILED
;
2116 ft2232_speed(jtag_get_speed());
2118 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2119 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2121 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2122 return ERROR_JTAG_INIT_FAILED
;
2125 #if BUILD_FT2232_FTD2XX == 1
2126 return ft2232_purge_ftd2xx();
2127 #elif BUILD_FT2232_LIBFTDI == 1
2128 return ft2232_purge_libftdi();
2135 static int usbjtag_init(void)
2138 uint32_t bytes_written
;
2141 low_direction
= 0x0b;
2143 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2150 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2157 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2164 low_direction
= 0x8b;
2168 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2169 return ERROR_JTAG_INIT_FAILED
;
2172 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2173 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2175 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2176 low_output
&= ~nTRST
; /* nTRST = 0 */
2180 low_direction
|= nTRSTnOE
; /* nTRST output */
2181 low_output
|= nTRST
; /* nTRST = 1 */
2184 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2186 low_direction
|= nSRSTnOE
; /* nSRST output */
2187 low_output
|= nSRST
; /* nSRST = 1 */
2191 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2192 low_output
&= ~nSRST
; /* nSRST = 0 */
2195 /* initialize low byte for jtag */
2196 buf
[0] = 0x80; /* command "set data bits low byte" */
2197 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
2198 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2199 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2201 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2203 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2204 return ERROR_JTAG_INIT_FAILED
;
2211 static int axm0432_jtag_init(void)
2214 uint32_t bytes_written
;
2217 low_direction
= 0x2b;
2219 /* initialize low byte for jtag */
2220 buf
[0] = 0x80; /* command "set data bits low byte" */
2221 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2222 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2223 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2225 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2227 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2228 return ERROR_JTAG_INIT_FAILED
;
2231 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2234 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2236 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2240 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2245 high_direction
= 0x0c;
2247 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2248 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2250 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2254 high_output
|= nTRST
;
2257 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2259 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2263 high_output
|= nSRST
;
2266 /* initialize high port */
2267 buf
[0] = 0x82; /* command "set data bits high byte" */
2268 buf
[1] = high_output
; /* value */
2269 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2270 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2272 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2274 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2275 return ERROR_JTAG_INIT_FAILED
;
2282 static int jtagkey_init(void)
2285 uint32_t bytes_written
;
2288 low_direction
= 0x1b;
2290 /* initialize low byte for jtag */
2291 buf
[0] = 0x80; /* command "set data bits low byte" */
2292 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2293 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2294 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2296 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2298 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2299 return ERROR_JTAG_INIT_FAILED
;
2302 if (strcmp(layout
->name
, "jtagkey") == 0)
2309 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2310 || (strcmp(layout
->name
, "oocdlink") == 0))
2319 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2324 high_direction
= 0x0f;
2326 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2327 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2329 high_output
|= nTRSTnOE
;
2330 high_output
&= ~nTRST
;
2334 high_output
&= ~nTRSTnOE
;
2335 high_output
|= nTRST
;
2338 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2340 high_output
&= ~nSRSTnOE
;
2341 high_output
|= nSRST
;
2345 high_output
|= nSRSTnOE
;
2346 high_output
&= ~nSRST
;
2349 /* initialize high port */
2350 buf
[0] = 0x82; /* command "set data bits high byte" */
2351 buf
[1] = high_output
; /* value */
2352 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2353 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2355 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2357 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2358 return ERROR_JTAG_INIT_FAILED
;
2365 static int olimex_jtag_init(void)
2368 uint32_t bytes_written
;
2371 low_direction
= 0x1b;
2373 /* initialize low byte for jtag */
2374 buf
[0] = 0x80; /* command "set data bits low byte" */
2375 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2376 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2377 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2379 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2381 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2382 return ERROR_JTAG_INIT_FAILED
;
2388 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2391 high_direction
= 0x0f;
2393 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2394 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2396 high_output
|= nTRSTnOE
;
2397 high_output
&= ~nTRST
;
2401 high_output
&= ~nTRSTnOE
;
2402 high_output
|= nTRST
;
2405 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2407 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2411 high_output
&= ~nSRST
;
2414 /* turn red LED on */
2415 high_output
|= 0x08;
2417 /* initialize high port */
2418 buf
[0] = 0x82; /* command "set data bits high byte" */
2419 buf
[1] = high_output
; /* value */
2420 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2421 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2423 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2425 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2426 return ERROR_JTAG_INIT_FAILED
;
2433 static int flyswatter_init(void)
2436 uint32_t bytes_written
;
2439 low_direction
= 0xfb;
2441 /* initialize low byte for jtag */
2442 buf
[0] = 0x80; /* command "set data bits low byte" */
2443 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2444 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2445 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2447 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2449 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2450 return ERROR_JTAG_INIT_FAILED
;
2454 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2456 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2459 high_direction
= 0x0c;
2461 /* turn red LED3 on, LED2 off */
2462 high_output
|= 0x08;
2464 /* initialize high port */
2465 buf
[0] = 0x82; /* command "set data bits high byte" */
2466 buf
[1] = high_output
; /* value */
2467 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2468 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2470 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2472 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2473 return ERROR_JTAG_INIT_FAILED
;
2480 static int turtle_init(void)
2483 uint32_t bytes_written
;
2486 low_direction
= 0x5b;
2488 /* initialize low byte for jtag */
2489 buf
[0] = 0x80; /* command "set data bits low byte" */
2490 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2491 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2492 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2494 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2496 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2497 return ERROR_JTAG_INIT_FAILED
;
2503 high_direction
= 0x0C;
2505 /* initialize high port */
2506 buf
[0] = 0x82; /* command "set data bits high byte" */
2507 buf
[1] = high_output
;
2508 buf
[2] = high_direction
;
2509 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2511 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2513 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2514 return ERROR_JTAG_INIT_FAILED
;
2521 static int comstick_init(void)
2524 uint32_t bytes_written
;
2527 low_direction
= 0x0b;
2529 /* initialize low byte for jtag */
2530 buf
[0] = 0x80; /* command "set data bits low byte" */
2531 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2532 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2533 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2535 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2537 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2538 return ERROR_JTAG_INIT_FAILED
;
2542 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2544 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2547 high_direction
= 0x03;
2549 /* initialize high port */
2550 buf
[0] = 0x82; /* command "set data bits high byte" */
2551 buf
[1] = high_output
;
2552 buf
[2] = high_direction
;
2553 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2555 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2557 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2558 return ERROR_JTAG_INIT_FAILED
;
2565 static int stm32stick_init(void)
2568 uint32_t bytes_written
;
2571 low_direction
= 0x8b;
2573 /* initialize low byte for jtag */
2574 buf
[0] = 0x80; /* command "set data bits low byte" */
2575 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2576 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2577 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2579 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2581 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2582 return ERROR_JTAG_INIT_FAILED
;
2586 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2588 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2591 high_direction
= 0x03;
2593 /* initialize high port */
2594 buf
[0] = 0x82; /* command "set data bits high byte" */
2595 buf
[1] = high_output
;
2596 buf
[2] = high_direction
;
2597 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2599 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2601 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2602 return ERROR_JTAG_INIT_FAILED
;
2609 static int sheevaplug_init(void)
2612 uint32_t bytes_written
;
2615 low_direction
= 0x1b;
2617 /* initialize low byte for jtag */
2618 buf
[0] = 0x80; /* command "set data bits low byte" */
2619 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2620 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2621 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2623 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2625 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2626 return ERROR_JTAG_INIT_FAILED
;
2635 high_direction
= 0x0f;
2637 /* nTRST is always push-pull */
2638 high_output
&= ~nTRSTnOE
;
2639 high_output
|= nTRST
;
2641 /* nSRST is always open-drain */
2642 high_output
|= nSRSTnOE
;
2643 high_output
&= ~nSRST
;
2645 /* initialize high port */
2646 buf
[0] = 0x82; /* command "set data bits high byte" */
2647 buf
[1] = high_output
; /* value */
2648 buf
[2] = high_direction
; /* all outputs - xRST */
2649 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2651 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2653 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2654 return ERROR_JTAG_INIT_FAILED
;
2660 static int cortino_jtag_init(void)
2663 uint32_t bytes_written
;
2666 low_direction
= 0x1b;
2668 /* initialize low byte for jtag */
2669 buf
[0] = 0x80; /* command "set data bits low byte" */
2670 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2671 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2672 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2674 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2676 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2677 return ERROR_JTAG_INIT_FAILED
;
2681 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2683 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2686 high_direction
= 0x03;
2688 /* initialize high port */
2689 buf
[0] = 0x82; /* command "set data bits high byte" */
2690 buf
[1] = high_output
;
2691 buf
[2] = high_direction
;
2692 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2694 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2696 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2697 return ERROR_JTAG_INIT_FAILED
;
2703 static void olimex_jtag_blink(void)
2705 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2706 * ACBUS3 is bit 3 of the GPIOH port
2708 if (high_output
& 0x08)
2710 /* set port pin high */
2711 high_output
&= 0x07;
2715 /* set port pin low */
2716 high_output
|= 0x08;
2720 buffer_write(high_output
);
2721 buffer_write(high_direction
);
2725 static void flyswatter_jtag_blink(void)
2728 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2730 high_output
^= 0x0c;
2733 buffer_write(high_output
);
2734 buffer_write(high_direction
);
2738 static void turtle_jtag_blink(void)
2741 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2743 if (high_output
& 0x08)
2753 buffer_write(high_output
);
2754 buffer_write(high_direction
);
2758 static int ft2232_quit(void)
2760 #if BUILD_FT2232_FTD2XX == 1
2763 status
= FT_Close(ftdih
);
2764 #elif BUILD_FT2232_LIBFTDI == 1
2765 ftdi_usb_close(&ftdic
);
2767 ftdi_deinit(&ftdic
);
2770 free(ft2232_buffer
);
2771 ft2232_buffer
= NULL
;
2777 static int ft2232_handle_device_desc_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2783 ft2232_device_desc
= strdup(args
[0]);
2784 cp
= strchr(ft2232_device_desc
, 0);
2785 // under Win32, the FTD2XX driver appends an "A" to the end
2786 // of the description, this examines the given desc
2787 // and creates the 'missing' _A or non_A variable.
2788 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
2789 // it was, so make this the "A" version.
2790 ft2232_device_desc_A
= ft2232_device_desc
;
2791 // and *CREATE* the non-A version.
2792 strcpy(buf
, ft2232_device_desc
);
2793 cp
= strchr(buf
, 0);
2795 ft2232_device_desc
= strdup(buf
);
2797 // <space>A not defined
2799 sprintf(buf
, "%s A", ft2232_device_desc
);
2800 ft2232_device_desc_A
= strdup(buf
);
2805 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2812 static int ft2232_handle_serial_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2816 ft2232_serial
= strdup(args
[0]);
2820 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2827 static int ft2232_handle_layout_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2832 ft2232_layout
= malloc(strlen(args
[0]) + 1);
2833 strcpy(ft2232_layout
, args
[0]);
2839 static int ft2232_handle_vid_pid_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2841 if (argc
> MAX_USB_IDS
* 2)
2843 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2844 "(maximum is %d pairs)", MAX_USB_IDS
);
2845 argc
= MAX_USB_IDS
* 2;
2847 if (argc
< 2 || (argc
& 1))
2849 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2851 return ERROR_COMMAND_SYNTAX_ERROR
;
2852 // remove the incomplete trailing id
2857 int retval
= ERROR_OK
;
2858 for (i
= 0; i
< argc
; i
+= 2)
2860 retval
= parse_u16(args
[i
], &ft2232_vid
[i
>> 1]);
2861 if (ERROR_OK
!= retval
)
2863 retval
= parse_u16(args
[i
+ 1], &ft2232_pid
[i
>> 1]);
2864 if (ERROR_OK
!= retval
)
2869 * Explicitly terminate, in case there are multiples instances of
2872 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2878 static int ft2232_handle_latency_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2882 ft2232_latency
= atoi(args
[0]);
2886 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2893 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
)
2897 /* 7 bits of either ones or zeros. */
2898 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
2900 while (num_cycles
> 0)
2902 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2903 * at most 7 bits per invocation. Here we invoke it potentially
2906 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
2908 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
2910 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2911 retval
= ERROR_JTAG_QUEUE_FAILED
;
2916 /* there are no state transitions in this code, so omit state tracking */
2918 /* command "Clock Data to TMS/CS Pin (no Read)" */
2922 buffer_write(bitcount_per_command
- 1);
2924 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2929 num_cycles
-= bitcount_per_command
;
2936 /* ---------------------------------------------------------------------
2937 * Support for IceBear JTAG adapter from Section5:
2938 * http://section5.ch/icebear
2940 * Author: Sten, debian@sansys-electronic.com
2943 /* Icebear pin layout
2945 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2946 * GND GND | 4 3| n.c.
2947 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2948 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2949 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2950 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2951 * ADBUS2 TDO |14 13| GND GND
2953 * ADBUS0 O L TCK ACBUS0 GND
2954 * ADBUS1 O L TDI ACBUS1 GND
2955 * ADBUS2 I TDO ACBUS2 n.c.
2956 * ADBUS3 O H TMS ACBUS3 n.c.
2962 static int icebear_jtag_init(void) {
2964 uint32_t bytes_written
;
2966 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
2967 low_output
= 0x08; /* high: TMS; low: TCK TDI */
2971 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2972 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
2973 low_direction
&= ~nTRST
; /* nTRST high impedance */
2976 low_direction
|= nTRST
;
2977 low_output
|= nTRST
;
2980 low_direction
|= nSRST
;
2981 low_output
|= nSRST
;
2983 /* initialize low byte for jtag */
2984 buf
[0] = 0x80; /* command "set data bits low byte" */
2985 buf
[1] = low_output
;
2986 buf
[2] = low_direction
;
2987 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2989 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
2990 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2991 return ERROR_JTAG_INIT_FAILED
;
2995 high_direction
= 0x00;
2998 /* initialize high port */
2999 buf
[0] = 0x82; /* command "set data bits high byte" */
3000 buf
[1] = high_output
; /* value */
3001 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3002 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3004 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3005 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3006 return ERROR_JTAG_INIT_FAILED
;
3012 static void icebear_jtag_reset(int trst
, int srst
) {
3015 low_direction
|= nTRST
;
3016 low_output
&= ~nTRST
;
3018 else if (trst
== 0) {
3019 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3020 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3021 low_direction
&= ~nTRST
;
3023 low_output
|= nTRST
;
3027 low_output
&= ~nSRST
;
3029 else if (srst
== 0) {
3030 low_output
|= nSRST
;
3033 /* command "set data bits low byte" */
3035 buffer_write(low_output
);
3036 buffer_write(low_direction
);
3038 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);