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 u16 ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
111 static u16 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
, u32
* 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
, u32 size
, u32
* 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)", *bytes_read
, size
);
415 return ERROR_JTAG_DEVICE_ERROR
;
421 #ifdef BUILD_FTD2XX_HIGHSPEED
422 static bool ft2232_device_is_highspeed(void)
424 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
427 static int ft2232_adaptive_clocking(int speed
)
429 bool use_adaptive_clocking
= FALSE
;
432 if (ft2232_device_is_highspeed())
433 use_adaptive_clocking
= TRUE
;
436 LOG_ERROR("ft2232 device %lu does not support RTCK", ftdi_device
);
441 uint8_t buf
= use_adaptive_clocking
? 0x96 : 0x97;
442 LOG_DEBUG("%2.2x", buf
);
445 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
446 if (ERROR_OK
!= retval
|| bytes_written
!= 1)
448 LOG_ERROR("unable to set adative clocking: %d", retval
);
455 static int ft2232_adaptive_clocking(int speed
)
457 // not implemented on low-speed devices
458 return speed
? ERROR_OK
: -1234;
462 static int ft2232_speed(int speed
)
468 ft2232_adaptive_clocking(speed
);
470 buf
[0] = 0x86; /* command "set divisor" */
471 buf
[1] = speed
& 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
472 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
474 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
475 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
477 LOG_ERROR("couldn't set FT2232 TCK speed");
485 static int ft2232_speed_div(int speed
, int* khz
)
487 /* Take a look in the FT2232 manual,
488 * AN2232C-01 Command Processor for
489 * MPSSE and MCU Host Bus. Chapter 3.8 */
491 *khz
= ft2232_max_tck
/ (1 + speed
);
497 static int ft2232_khz(int khz
, int* jtag_speed
)
501 #ifdef BUILD_FTD2XX_HIGHSPEED
505 LOG_DEBUG("RCLK not supported");
506 LOG_DEBUG("If you have a high-speed FTDI device, then "
507 "OpenOCD may be built with --enable-ftd2xx-highspeed.");
512 /* Take a look in the FT2232 manual,
513 * AN2232C-01 Command Processor for
514 * MPSSE and MCU Host Bus. Chapter 3.8
516 * We will calc here with a multiplier
517 * of 10 for better rounding later. */
519 /* Calc speed, (ft2232_max_tck / khz) - 1 */
520 /* Use 65000 for better rounding */
521 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
523 /* Add 0.9 for rounding */
526 /* Calc real speed */
527 *jtag_speed
= *jtag_speed
/ 10;
529 /* Check if speed is greater than 0 */
535 /* Check max value */
536 if (*jtag_speed
> 0xFFFF)
538 *jtag_speed
= 0xFFFF;
545 static int ft2232_register_commands(struct command_context_s
* cmd_ctx
)
547 register_command(cmd_ctx
, NULL
, "ft2232_device_desc", ft2232_handle_device_desc_command
,
548 COMMAND_CONFIG
, "the USB device description of the FTDI FT2232 device");
549 register_command(cmd_ctx
, NULL
, "ft2232_serial", ft2232_handle_serial_command
,
550 COMMAND_CONFIG
, "the serial number of the FTDI FT2232 device");
551 register_command(cmd_ctx
, NULL
, "ft2232_layout", ft2232_handle_layout_command
,
552 COMMAND_CONFIG
, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
553 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid", ft2232_handle_vid_pid_command
,
554 COMMAND_CONFIG
, "the vendor ID and product ID of the FTDI FT2232 device");
555 register_command(cmd_ctx
, NULL
, "ft2232_latency", ft2232_handle_latency_command
,
556 COMMAND_CONFIG
, "set the FT2232 latency timer to a new value");
561 static void ft2232_end_state(tap_state_t state
)
563 if (tap_is_state_stable(state
))
564 tap_set_end_state(state
);
567 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
572 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
574 int num_bytes
= (scan_size
+ 7) / 8;
575 int bits_left
= scan_size
;
578 while (num_bytes
-- > 1)
580 buffer
[cur_byte
++] = buffer_read();
584 buffer
[cur_byte
] = 0x0;
586 /* There is one more partial byte left from the clock data in/out instructions */
589 buffer
[cur_byte
] = buffer_read() >> 1;
591 /* 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 */
592 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
596 static void ft2232_debug_dump_buffer(void)
602 for (i
= 0; i
< ft2232_buffer_size
; i
++)
604 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
607 LOG_DEBUG("%s", line
);
613 LOG_DEBUG("%s", line
);
617 static int ft2232_send_and_recv(jtag_command_t
* first
, jtag_command_t
* last
)
627 #ifdef _DEBUG_USB_IO_
628 struct timeval start
, inter
, inter2
, end
;
629 struct timeval d_inter
, d_inter2
, d_end
;
632 #ifdef _DEBUG_USB_COMMS_
633 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
634 ft2232_debug_dump_buffer();
637 #ifdef _DEBUG_USB_IO_
638 gettimeofday(&start
, NULL
);
641 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
643 LOG_ERROR("couldn't write MPSSE commands to FT2232");
647 #ifdef _DEBUG_USB_IO_
648 gettimeofday(&inter
, NULL
);
651 if (ft2232_expect_read
)
654 ft2232_buffer_size
= 0;
656 #ifdef _DEBUG_USB_IO_
657 gettimeofday(&inter2
, NULL
);
660 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
662 LOG_ERROR("couldn't read from FT2232");
666 #ifdef _DEBUG_USB_IO_
667 gettimeofday(&end
, NULL
);
669 timeval_subtract(&d_inter
, &inter
, &start
);
670 timeval_subtract(&d_inter2
, &inter2
, &start
);
671 timeval_subtract(&d_end
, &end
, &start
);
673 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
674 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
675 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
676 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
679 ft2232_buffer_size
= bytes_read
;
681 if (ft2232_expect_read
!= ft2232_buffer_size
)
683 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read
,
686 ft2232_debug_dump_buffer();
691 #ifdef _DEBUG_USB_COMMS_
692 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout
, ft2232_buffer_size
);
693 ft2232_debug_dump_buffer();
697 ft2232_expect_read
= 0;
698 ft2232_read_pointer
= 0;
700 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
701 * that wasn't handled by a caller-provided error handler
711 type
= jtag_scan_type(cmd
->cmd
.scan
);
712 if (type
!= SCAN_OUT
)
714 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
715 buffer
= calloc(CEIL(scan_size
, 8), 1);
716 ft2232_read_scan(type
, buffer
, scan_size
);
717 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
718 retval
= ERROR_JTAG_QUEUE_FAILED
;
730 ft2232_buffer_size
= 0;
737 * Function ft2232_add_pathmove
738 * moves the TAP controller from the current state to a new state through the
739 * given path, where path is an array of tap_state_t's.
741 * @param path is an array of tap_stat_t which gives the states to traverse through
742 * ending with the last state at path[num_states-1]
743 * @param num_states is the count of state steps to move through
745 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
749 tap_state_t walker
= tap_get_state();
751 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
753 /* this loop verifies that the path is legal and logs each state in the path */
754 for (state_ndx
= 0; state_ndx
< num_states
; ++state_ndx
)
756 tap_state_t desired_next_state
= path
[state_ndx
];
758 if (tap_state_transition(walker
, false) == desired_next_state
)
759 ; /* bit within tms_bits at index state_ndx is already zero */
760 else if (tap_state_transition(walker
, true) == desired_next_state
)
761 tms_bits
|= (1<<state_ndx
);
764 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
765 tap_state_name(walker
), tap_state_name(desired_next_state
));
769 walker
= desired_next_state
;
772 clock_tms(0x4b, tms_bits
, num_states
, 0);
774 tap_set_end_state(tap_get_state());
778 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
780 int num_bytes
= (scan_size
+ 7) / 8;
781 int bits_left
= scan_size
;
787 if (tap_get_state() != TAP_DRSHIFT
)
789 move_to_state(TAP_DRSHIFT
);
794 if (tap_get_state() != TAP_IRSHIFT
)
796 move_to_state(TAP_IRSHIFT
);
800 /* add command for complete bytes */
801 while (num_bytes
> 1)
806 /* Clock Data Bytes In and Out LSB First */
808 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
810 else if (type
== SCAN_OUT
)
812 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
814 /* LOG_DEBUG("added TDI bytes (o)"); */
816 else if (type
== SCAN_IN
)
818 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
820 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
823 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
824 num_bytes
-= thisrun_bytes
;
826 buffer_write((uint8_t) (thisrun_bytes
- 1));
827 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
831 /* add complete bytes */
832 while (thisrun_bytes
-- > 0)
834 buffer_write(buffer
[cur_byte
++]);
838 else /* (type == SCAN_IN) */
840 bits_left
-= 8 * (thisrun_bytes
);
844 /* the most signifcant bit is scanned during TAP movement */
846 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
850 /* process remaining bits but the last one */
855 /* Clock Data Bits In and Out LSB First */
857 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
859 else if (type
== SCAN_OUT
)
861 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
863 /* LOG_DEBUG("added TDI bits (o)"); */
865 else if (type
== SCAN_IN
)
867 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
869 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
872 buffer_write(bits_left
- 2);
874 buffer_write(buffer
[cur_byte
]);
877 if (( ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
878 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
882 /* Clock Data Bits In and Out LSB First */
884 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
886 else if (type
== SCAN_OUT
)
888 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
890 /* LOG_DEBUG("added TDI bits (o)"); */
892 else if (type
== SCAN_IN
)
894 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
896 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
899 buffer_write(last_bit
);
907 /* move from Shift-IR/DR to end state */
908 if (type
!= SCAN_OUT
)
910 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
911 /* This must be coordinated with the bit shifts in ft2232_read_scan */
914 /* Clock Data to TMS/CS Pin with Read */
916 /* LOG_DEBUG("added TMS scan (read)"); */
920 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
921 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
922 /* Clock Data to TMS/CS Pin (no Read) */
924 /* LOG_DEBUG("added TMS scan (no read)"); */
927 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
930 if (tap_get_state() != tap_get_end_state())
932 move_to_state(tap_get_end_state());
937 static int ft2232_large_scan(scan_command_t
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
939 int num_bytes
= (scan_size
+ 7) / 8;
940 int bits_left
= scan_size
;
943 uint8_t* receive_buffer
= malloc(CEIL(scan_size
, 8));
944 uint8_t* receive_pointer
= receive_buffer
;
948 int thisrun_read
= 0;
952 LOG_ERROR("BUG: large IR scans are not supported");
956 if (tap_get_state() != TAP_DRSHIFT
)
958 move_to_state(TAP_DRSHIFT
);
961 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
963 LOG_ERROR("couldn't write MPSSE commands to FT2232");
966 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
967 ft2232_buffer_size
= 0;
969 /* add command for complete bytes */
970 while (num_bytes
> 1)
976 /* Clock Data Bytes In and Out LSB First */
978 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
980 else if (type
== SCAN_OUT
)
982 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
984 /* LOG_DEBUG("added TDI bytes (o)"); */
986 else if (type
== SCAN_IN
)
988 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
990 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
993 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
994 thisrun_read
= thisrun_bytes
;
995 num_bytes
-= thisrun_bytes
;
996 buffer_write((uint8_t) (thisrun_bytes
- 1));
997 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1001 /* add complete bytes */
1002 while (thisrun_bytes
-- > 0)
1004 buffer_write(buffer
[cur_byte
]);
1009 else /* (type == SCAN_IN) */
1011 bits_left
-= 8 * (thisrun_bytes
);
1014 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1016 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1019 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
1020 ft2232_buffer_size
= 0;
1022 if (type
!= SCAN_OUT
)
1024 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1026 LOG_ERROR("couldn't read from FT2232");
1029 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read
, bytes_read
);
1030 receive_pointer
+= bytes_read
;
1036 /* the most signifcant bit is scanned during TAP movement */
1037 if (type
!= SCAN_IN
)
1038 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1042 /* process remaining bits but the last one */
1045 if (type
== SCAN_IO
)
1047 /* Clock Data Bits In and Out LSB First */
1049 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1051 else if (type
== SCAN_OUT
)
1053 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1055 /* LOG_DEBUG("added TDI bits (o)"); */
1057 else if (type
== SCAN_IN
)
1059 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1061 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1063 buffer_write(bits_left
- 2);
1064 if (type
!= SCAN_IN
)
1065 buffer_write(buffer
[cur_byte
]);
1067 if (type
!= SCAN_OUT
)
1071 if (tap_get_end_state() == TAP_DRSHIFT
)
1073 if (type
== SCAN_IO
)
1075 /* Clock Data Bits In and Out LSB First */
1077 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1079 else if (type
== SCAN_OUT
)
1081 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1083 /* LOG_DEBUG("added TDI bits (o)"); */
1085 else if (type
== SCAN_IN
)
1087 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1089 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1092 buffer_write(last_bit
);
1096 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1097 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1100 /* move from Shift-IR/DR to end state */
1101 if (type
!= SCAN_OUT
)
1103 /* Clock Data to TMS/CS Pin with Read */
1105 /* LOG_DEBUG("added TMS scan (read)"); */
1109 /* Clock Data to TMS/CS Pin (no Read) */
1111 /* LOG_DEBUG("added TMS scan (no read)"); */
1114 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1117 if (type
!= SCAN_OUT
)
1120 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1122 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1125 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
1126 ft2232_buffer_size
= 0;
1128 if (type
!= SCAN_OUT
)
1130 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1132 LOG_ERROR("couldn't read from FT2232");
1135 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read
, bytes_read
);
1136 receive_pointer
+= bytes_read
;
1143 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1145 int predicted_size
= 3;
1146 int num_bytes
= (scan_size
- 1) / 8;
1148 if (tap_get_state() != TAP_DRSHIFT
)
1149 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1151 if (type
== SCAN_IN
) /* only from device to host */
1153 /* complete bytes */
1154 predicted_size
+= CEIL(num_bytes
, 65536) * 3;
1156 /* remaining bits - 1 (up to 7) */
1157 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1159 else /* host to device, or bidirectional */
1161 /* complete bytes */
1162 predicted_size
+= num_bytes
+ CEIL(num_bytes
, 65536) * 3;
1164 /* remaining bits -1 (up to 7) */
1165 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1168 return predicted_size
;
1172 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1174 int predicted_size
= 0;
1176 if (type
!= SCAN_OUT
)
1178 /* complete bytes */
1179 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) - 1) : 0;
1181 /* remaining bits - 1 */
1182 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1184 /* last bit (from TMS scan) */
1185 predicted_size
+= 1;
1188 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1190 return predicted_size
;
1194 static void usbjtag_reset(int trst
, int srst
)
1196 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1199 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1200 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1202 low_output
&= ~nTRST
; /* switch output low */
1206 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1207 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1209 low_output
|= nTRST
; /* switch output high */
1214 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1215 low_output
&= ~nSRST
; /* switch output low */
1217 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1221 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1222 low_output
|= nSRST
; /* switch output high */
1224 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1227 /* command "set data bits low byte" */
1229 buffer_write(low_output
);
1230 buffer_write(low_direction
);
1234 static void jtagkey_reset(int trst
, int srst
)
1236 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1239 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1240 high_output
&= ~nTRSTnOE
;
1242 high_output
&= ~nTRST
;
1246 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1247 high_output
|= nTRSTnOE
;
1249 high_output
|= nTRST
;
1254 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1255 high_output
&= ~nSRST
;
1257 high_output
&= ~nSRSTnOE
;
1261 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1262 high_output
|= nSRST
;
1264 high_output
|= nSRSTnOE
;
1267 /* command "set data bits high byte" */
1269 buffer_write(high_output
);
1270 buffer_write(high_direction
);
1271 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1276 static void olimex_jtag_reset(int trst
, int srst
)
1278 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1281 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1282 high_output
&= ~nTRSTnOE
;
1284 high_output
&= ~nTRST
;
1288 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1289 high_output
|= nTRSTnOE
;
1291 high_output
|= nTRST
;
1296 high_output
|= nSRST
;
1300 high_output
&= ~nSRST
;
1303 /* command "set data bits high byte" */
1305 buffer_write(high_output
);
1306 buffer_write(high_direction
);
1307 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1312 static void axm0432_jtag_reset(int trst
, int srst
)
1316 tap_set_state(TAP_RESET
);
1317 high_output
&= ~nTRST
;
1321 high_output
|= nTRST
;
1326 high_output
&= ~nSRST
;
1330 high_output
|= nSRST
;
1333 /* command "set data bits low byte" */
1335 buffer_write(high_output
);
1336 buffer_write(high_direction
);
1337 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1342 static void flyswatter_reset(int trst
, int srst
)
1346 low_output
&= ~nTRST
;
1350 low_output
|= nTRST
;
1355 low_output
|= nSRST
;
1359 low_output
&= ~nSRST
;
1362 /* command "set data bits low byte" */
1364 buffer_write(low_output
);
1365 buffer_write(low_direction
);
1366 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1370 static void turtle_reset(int trst
, int srst
)
1376 low_output
|= nSRST
;
1380 low_output
&= ~nSRST
;
1383 /* command "set data bits low byte" */
1385 buffer_write(low_output
);
1386 buffer_write(low_direction
);
1387 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1391 static void comstick_reset(int trst
, int srst
)
1395 high_output
&= ~nTRST
;
1399 high_output
|= nTRST
;
1404 high_output
&= ~nSRST
;
1408 high_output
|= nSRST
;
1411 /* command "set data bits high byte" */
1413 buffer_write(high_output
);
1414 buffer_write(high_direction
);
1415 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1420 static void stm32stick_reset(int trst
, int srst
)
1424 high_output
&= ~nTRST
;
1428 high_output
|= nTRST
;
1433 low_output
&= ~nSRST
;
1437 low_output
|= nSRST
;
1440 /* command "set data bits low byte" */
1442 buffer_write(low_output
);
1443 buffer_write(low_direction
);
1445 /* command "set data bits high byte" */
1447 buffer_write(high_output
);
1448 buffer_write(high_direction
);
1449 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1455 static void sheevaplug_reset(int trst
, int srst
)
1458 high_output
&= ~nTRST
;
1460 high_output
|= nTRST
;
1463 high_output
&= ~nSRSTnOE
;
1465 high_output
|= nSRSTnOE
;
1467 /* command "set data bits high byte" */
1469 buffer_write(high_output
);
1470 buffer_write(high_direction
);
1471 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1474 static int ft2232_execute_runtest(jtag_command_t
*cmd
)
1478 int predicted_size
= 0;
1481 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1482 cmd
->cmd
.runtest
->num_cycles
,
1483 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1485 /* only send the maximum buffer size that FT2232C can handle */
1487 if (tap_get_state() != TAP_IDLE
)
1488 predicted_size
+= 3;
1489 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
1490 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1491 predicted_size
+= 3;
1492 if (tap_get_end_state() != TAP_IDLE
)
1493 predicted_size
+= 3;
1494 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1496 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1497 retval
= ERROR_JTAG_QUEUE_FAILED
;
1501 if (tap_get_state() != TAP_IDLE
)
1503 move_to_state(TAP_IDLE
);
1506 i
= cmd
->cmd
.runtest
->num_cycles
;
1509 /* there are no state transitions in this code, so omit state tracking */
1511 /* command "Clock Data to TMS/CS Pin (no Read)" */
1515 buffer_write((i
> 7) ? 6 : (i
- 1));
1519 tap_set_state(TAP_IDLE
);
1521 i
-= (i
> 7) ? 7 : i
;
1522 /* LOG_DEBUG("added TMS scan (no read)"); */
1525 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1527 if (tap_get_state() != tap_get_end_state())
1529 move_to_state(tap_get_end_state());
1533 #ifdef _DEBUG_JTAG_IO_
1534 LOG_DEBUG("runtest: %i, end in %s", cmd
->cmd
.runtest
->num_cycles
, tap_state_name(tap_get_end_state()));
1541 static int ft2232_execute_statemove(jtag_command_t
*cmd
)
1543 int predicted_size
= 0;
1544 int retval
= ERROR_OK
;
1546 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1548 /* only send the maximum buffer size that FT2232C can handle */
1550 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1552 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1553 retval
= ERROR_JTAG_QUEUE_FAILED
;
1557 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1559 /* move to end state */
1560 if (tap_get_state() != tap_get_end_state())
1562 move_to_state(tap_get_end_state());
1569 static int ft2232_execute_pathmove(jtag_command_t
*cmd
)
1571 int predicted_size
= 0;
1572 int retval
= ERROR_OK
;
1574 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1575 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1577 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1578 tap_state_name(tap_get_state()),
1579 tap_state_name(path
[num_states
-1])
1582 /* only send the maximum buffer size that FT2232C can handle */
1583 predicted_size
= 3 * CEIL(num_states
, 7);
1584 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1586 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1587 retval
= ERROR_JTAG_QUEUE_FAILED
;
1593 ft2232_add_pathmove(path
, num_states
);
1600 static int ft2232_execute_scan(jtag_command_t
*cmd
)
1603 int scan_size
; /* size of IR or DR scan */
1604 int predicted_size
= 0;
1605 int retval
= ERROR_OK
;
1607 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1609 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1611 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1613 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1614 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1616 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1617 /* unsent commands before this */
1618 if (first_unsent
!= cmd
)
1619 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1620 retval
= ERROR_JTAG_QUEUE_FAILED
;
1622 /* current command */
1623 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1624 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1626 first_unsent
= cmd
->next
;
1631 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1633 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1636 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1637 retval
= ERROR_JTAG_QUEUE_FAILED
;
1641 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1642 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1643 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1644 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1648 #ifdef _DEBUG_JTAG_IO_
1649 LOG_DEBUG("%s scan, %i bits, end in %s", (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1650 tap_state_name(tap_get_end_state()));
1656 static int ft2232_execute_reset(jtag_command_t
*cmd
)
1659 int predicted_size
= 0;
1662 DEBUG_JTAG_IO("reset trst: %i srst %i",
1663 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1665 /* only send the maximum buffer size that FT2232C can handle */
1667 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1669 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1670 retval
= ERROR_JTAG_QUEUE_FAILED
;
1675 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1678 #ifdef _DEBUG_JTAG_IO_
1679 LOG_DEBUG("trst: %i, srst: %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1684 static int ft2232_execute_sleep(jtag_command_t
*cmd
)
1689 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
1691 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1692 retval
= ERROR_JTAG_QUEUE_FAILED
;
1693 first_unsent
= cmd
->next
;
1694 jtag_sleep(cmd
->cmd
.sleep
->us
);
1695 #ifdef _DEBUG_JTAG_IO_
1696 LOG_DEBUG("sleep %i usec while in %s", cmd
->cmd
.sleep
->us
, tap_state_name(tap_get_state()));
1702 static int ft2232_execute_stableclocks(jtag_command_t
*cmd
)
1707 /* this is only allowed while in a stable state. A check for a stable
1708 * state was done in jtag_add_clocks()
1710 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1711 retval
= ERROR_JTAG_QUEUE_FAILED
;
1712 #ifdef _DEBUG_JTAG_IO_
1713 LOG_DEBUG("clocks %i while in %s", cmd
->cmd
.stableclocks
->num_cycles
, tap_state_name(tap_get_state()));
1719 static int ft2232_execute_command(jtag_command_t
*cmd
)
1726 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1727 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1728 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1729 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1730 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1731 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1732 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1734 LOG_ERROR("BUG: unknown JTAG command type encountered");
1740 static int ft2232_execute_queue()
1742 jtag_command_t
* cmd
= jtag_command_queue
; /* currently processed command */
1745 first_unsent
= cmd
; /* next command that has to be sent */
1748 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1749 * that wasn't handled by a caller-provided error handler
1753 ft2232_buffer_size
= 0;
1754 ft2232_expect_read
= 0;
1756 /* blink, if the current layout has that feature */
1762 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1763 retval
= ERROR_JTAG_QUEUE_FAILED
;
1764 /* Start reading input before FT2232 TX buffer fills up */
1766 if (ft2232_expect_read
> 256)
1768 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1769 retval
= ERROR_JTAG_QUEUE_FAILED
;
1774 if (require_send
> 0)
1775 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1776 retval
= ERROR_JTAG_QUEUE_FAILED
;
1782 #if BUILD_FT2232_FTD2XX == 1
1783 static int ft2232_init_ftd2xx(u16 vid
, u16 pid
, int more
, int* try_more
)
1787 char SerialNumber
[16];
1788 char Description
[64];
1789 DWORD openex_flags
= 0;
1790 char* openex_string
= NULL
;
1791 uint8_t latency_timer
;
1793 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
1796 /* Add non-standard Vid/Pid to the linux driver */
1797 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1799 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
1803 if (ft2232_device_desc
&& ft2232_serial
)
1805 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1806 ft2232_device_desc
= NULL
;
1809 if (ft2232_device_desc
)
1811 openex_string
= ft2232_device_desc
;
1812 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1814 else if (ft2232_serial
)
1816 openex_string
= ft2232_serial
;
1817 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1821 LOG_ERROR("neither device description nor serial number specified");
1822 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1824 return ERROR_JTAG_INIT_FAILED
;
1827 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1828 if (status
!= FT_OK
) {
1829 // under Win32, the FTD2XX driver appends an "A" to the end
1830 // of the description, if we tried by the desc, then
1831 // try by the alternate "A" description.
1832 if (openex_string
== ft2232_device_desc
) {
1833 // Try the alternate method.
1834 openex_string
= ft2232_device_desc_A
;
1835 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1836 if (status
== FT_OK
) {
1837 // yea, the "alternate" method worked!
1839 // drat, give the user a meaningfull message.
1840 // telling the use we tried *BOTH* methods.
1841 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1843 ft2232_device_desc_A
);
1848 if (status
!= FT_OK
)
1854 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
1856 return ERROR_JTAG_INIT_FAILED
;
1858 LOG_ERROR("unable to open ftdi device: %lu", status
);
1859 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1860 if (status
== FT_OK
)
1862 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1865 for (i
= 0; i
< num_devices
; i
++)
1866 desc_array
[i
] = malloc(64);
1868 desc_array
[num_devices
] = NULL
;
1870 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1872 if (status
== FT_OK
)
1874 LOG_ERROR("ListDevices: %lu\n", num_devices
);
1875 for (i
= 0; i
< num_devices
; i
++)
1876 LOG_ERROR("%i: \"%s\"", i
, desc_array
[i
]);
1879 for (i
= 0; i
< num_devices
; i
++)
1880 free(desc_array
[i
]);
1886 LOG_ERROR("ListDevices: NONE\n");
1888 return ERROR_JTAG_INIT_FAILED
;
1891 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1893 LOG_ERROR("unable to set latency timer: %lu", status
);
1894 return ERROR_JTAG_INIT_FAILED
;
1897 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1899 LOG_ERROR("unable to get latency timer: %lu", status
);
1900 return ERROR_JTAG_INIT_FAILED
;
1904 LOG_DEBUG("current latency timer: %i", latency_timer
);
1907 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1909 LOG_ERROR("unable to set timeouts: %lu", status
);
1910 return ERROR_JTAG_INIT_FAILED
;
1913 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1915 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
1916 return ERROR_JTAG_INIT_FAILED
;
1919 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
1921 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
1922 return ERROR_JTAG_INIT_FAILED
;
1926 LOG_INFO("device: %lu", ftdi_device
);
1927 LOG_INFO("deviceID: %lu", deviceID
);
1928 LOG_INFO("SerialNumber: %s", SerialNumber
);
1929 LOG_INFO("Description: %s", Description
);
1931 #ifdef BUILD_FTD2XX_HIGHSPEED
1932 if (ft2232_device_is_highspeed())
1934 ft2232_max_tck
= FTDI_2232H_4232H_MAX_TCK
;
1935 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
1944 static int ft2232_purge_ftd2xx(void)
1948 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1950 LOG_ERROR("error purging ftd2xx device: %lu", status
);
1951 return ERROR_JTAG_INIT_FAILED
;
1958 #endif /* BUILD_FT2232_FTD2XX == 1 */
1960 #if BUILD_FT2232_LIBFTDI == 1
1961 static int ft2232_init_libftdi(u16 vid
, u16 pid
, int more
, int* try_more
)
1963 uint8_t latency_timer
;
1965 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1966 ft2232_layout
, vid
, pid
);
1968 if (ftdi_init(&ftdic
) < 0)
1969 return ERROR_JTAG_INIT_FAILED
;
1971 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
1973 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
1974 return ERROR_JTAG_INIT_FAILED
;
1977 /* context, vendor id, product id */
1978 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
1982 LOG_WARNING("unable to open ftdi device (trying more): %s",
1985 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
1987 return ERROR_JTAG_INIT_FAILED
;
1990 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
1991 if (ftdi_usb_reset(&ftdic
) < 0)
1993 LOG_ERROR("unable to reset ftdi device");
1994 return ERROR_JTAG_INIT_FAILED
;
1997 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
1999 LOG_ERROR("unable to set latency timer");
2000 return ERROR_JTAG_INIT_FAILED
;
2003 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2005 LOG_ERROR("unable to get latency timer");
2006 return ERROR_JTAG_INIT_FAILED
;
2010 LOG_DEBUG("current latency timer: %i", latency_timer
);
2013 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2019 static int ft2232_purge_libftdi(void)
2021 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2023 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2024 return ERROR_JTAG_INIT_FAILED
;
2031 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2033 static int ft2232_init(void)
2038 const ft2232_layout_t
* cur_layout
= ft2232_layouts
;
2041 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
)==7)
2043 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2047 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2050 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2052 ft2232_layout
= "usbjtag";
2053 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2056 while (cur_layout
->name
)
2058 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2060 layout
= cur_layout
;
2068 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2069 return ERROR_JTAG_INIT_FAILED
;
2075 * "more indicates that there are more IDs to try, so we should
2076 * not print an error for an ID mismatch (but for anything
2079 * try_more indicates that the error code returned indicates an
2080 * ID mismatch (and nothing else) and that we should proceeed
2081 * with the next ID pair.
2083 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2086 #if BUILD_FT2232_FTD2XX == 1
2087 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2089 #elif BUILD_FT2232_LIBFTDI == 1
2090 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2095 if (!more
|| !try_more
)
2099 ft2232_buffer_size
= 0;
2100 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2102 if (layout
->init() != ERROR_OK
)
2103 return ERROR_JTAG_INIT_FAILED
;
2105 ft2232_speed(jtag_get_speed());
2107 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2108 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2110 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2111 return ERROR_JTAG_INIT_FAILED
;
2114 #if BUILD_FT2232_FTD2XX == 1
2115 return ft2232_purge_ftd2xx();
2116 #elif BUILD_FT2232_LIBFTDI == 1
2117 return ft2232_purge_libftdi();
2124 static int usbjtag_init(void)
2130 low_direction
= 0x0b;
2132 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2139 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2146 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2153 low_direction
= 0x8b;
2157 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2158 return ERROR_JTAG_INIT_FAILED
;
2161 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2162 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2164 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2165 low_output
&= ~nTRST
; /* nTRST = 0 */
2169 low_direction
|= nTRSTnOE
; /* nTRST output */
2170 low_output
|= nTRST
; /* nTRST = 1 */
2173 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2175 low_direction
|= nSRSTnOE
; /* nSRST output */
2176 low_output
|= nSRST
; /* nSRST = 1 */
2180 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2181 low_output
&= ~nSRST
; /* nSRST = 0 */
2184 /* initialize low byte for jtag */
2185 buf
[0] = 0x80; /* command "set data bits low byte" */
2186 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
2187 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2188 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2190 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2192 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2193 return ERROR_JTAG_INIT_FAILED
;
2200 static int axm0432_jtag_init(void)
2206 low_direction
= 0x2b;
2208 /* initialize low byte for jtag */
2209 buf
[0] = 0x80; /* command "set data bits low byte" */
2210 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2211 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2212 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2214 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2216 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2217 return ERROR_JTAG_INIT_FAILED
;
2220 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2223 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2225 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2229 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2234 high_direction
= 0x0c;
2236 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2237 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2239 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2243 high_output
|= nTRST
;
2246 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2248 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2252 high_output
|= nSRST
;
2255 /* initialize high port */
2256 buf
[0] = 0x82; /* command "set data bits high byte" */
2257 buf
[1] = high_output
; /* value */
2258 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2261 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2263 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2264 return ERROR_JTAG_INIT_FAILED
;
2271 static int jtagkey_init(void)
2277 low_direction
= 0x1b;
2279 /* initialize low byte for jtag */
2280 buf
[0] = 0x80; /* command "set data bits low byte" */
2281 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2282 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2283 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2285 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2287 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2288 return ERROR_JTAG_INIT_FAILED
;
2291 if (strcmp(layout
->name
, "jtagkey") == 0)
2298 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2299 || (strcmp(layout
->name
, "oocdlink") == 0))
2308 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2313 high_direction
= 0x0f;
2315 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2316 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2318 high_output
|= nTRSTnOE
;
2319 high_output
&= ~nTRST
;
2323 high_output
&= ~nTRSTnOE
;
2324 high_output
|= nTRST
;
2327 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2329 high_output
&= ~nSRSTnOE
;
2330 high_output
|= nSRST
;
2334 high_output
|= nSRSTnOE
;
2335 high_output
&= ~nSRST
;
2338 /* initialize high port */
2339 buf
[0] = 0x82; /* command "set data bits high byte" */
2340 buf
[1] = high_output
; /* value */
2341 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2342 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2344 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2346 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2347 return ERROR_JTAG_INIT_FAILED
;
2354 static int olimex_jtag_init(void)
2360 low_direction
= 0x1b;
2362 /* initialize low byte for jtag */
2363 buf
[0] = 0x80; /* command "set data bits low byte" */
2364 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2365 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2366 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2368 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2370 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2371 return ERROR_JTAG_INIT_FAILED
;
2377 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2380 high_direction
= 0x0f;
2382 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2383 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2385 high_output
|= nTRSTnOE
;
2386 high_output
&= ~nTRST
;
2390 high_output
&= ~nTRSTnOE
;
2391 high_output
|= nTRST
;
2394 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2396 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2400 high_output
&= ~nSRST
;
2403 /* turn red LED on */
2404 high_output
|= 0x08;
2406 /* initialize high port */
2407 buf
[0] = 0x82; /* command "set data bits high byte" */
2408 buf
[1] = high_output
; /* value */
2409 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2410 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2412 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2414 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2415 return ERROR_JTAG_INIT_FAILED
;
2422 static int flyswatter_init(void)
2428 low_direction
= 0xfb;
2430 /* initialize low byte for jtag */
2431 buf
[0] = 0x80; /* command "set data bits low byte" */
2432 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2433 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2434 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2436 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2438 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2439 return ERROR_JTAG_INIT_FAILED
;
2443 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2445 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2448 high_direction
= 0x0c;
2450 /* turn red LED3 on, LED2 off */
2451 high_output
|= 0x08;
2453 /* initialize high port */
2454 buf
[0] = 0x82; /* command "set data bits high byte" */
2455 buf
[1] = high_output
; /* value */
2456 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2457 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2459 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2461 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2462 return ERROR_JTAG_INIT_FAILED
;
2469 static int turtle_init(void)
2475 low_direction
= 0x5b;
2477 /* initialize low byte for jtag */
2478 buf
[0] = 0x80; /* command "set data bits low byte" */
2479 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2480 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2481 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2483 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2485 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2486 return ERROR_JTAG_INIT_FAILED
;
2492 high_direction
= 0x0C;
2494 /* initialize high port */
2495 buf
[0] = 0x82; /* command "set data bits high byte" */
2496 buf
[1] = high_output
;
2497 buf
[2] = high_direction
;
2498 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2500 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2502 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2503 return ERROR_JTAG_INIT_FAILED
;
2510 static int comstick_init(void)
2516 low_direction
= 0x0b;
2518 /* initialize low byte for jtag */
2519 buf
[0] = 0x80; /* command "set data bits low byte" */
2520 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2521 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2522 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2524 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2526 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2527 return ERROR_JTAG_INIT_FAILED
;
2531 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2533 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2536 high_direction
= 0x03;
2538 /* initialize high port */
2539 buf
[0] = 0x82; /* command "set data bits high byte" */
2540 buf
[1] = high_output
;
2541 buf
[2] = high_direction
;
2542 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2544 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2546 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2547 return ERROR_JTAG_INIT_FAILED
;
2554 static int stm32stick_init(void)
2560 low_direction
= 0x8b;
2562 /* initialize low byte for jtag */
2563 buf
[0] = 0x80; /* command "set data bits low byte" */
2564 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2565 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2566 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2568 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2570 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2571 return ERROR_JTAG_INIT_FAILED
;
2575 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2577 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2580 high_direction
= 0x03;
2582 /* initialize high port */
2583 buf
[0] = 0x82; /* command "set data bits high byte" */
2584 buf
[1] = high_output
;
2585 buf
[2] = high_direction
;
2586 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2588 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2590 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2591 return ERROR_JTAG_INIT_FAILED
;
2598 static int sheevaplug_init(void)
2604 low_direction
= 0x1b;
2606 /* initialize low byte for jtag */
2607 buf
[0] = 0x80; /* command "set data bits low byte" */
2608 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2609 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
2610 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2612 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2614 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2615 return ERROR_JTAG_INIT_FAILED
;
2624 high_direction
= 0x0f;
2626 /* nTRST is always push-pull */
2627 high_output
&= ~nTRSTnOE
;
2628 high_output
|= nTRST
;
2630 /* nSRST is always open-drain */
2631 high_output
|= nSRSTnOE
;
2632 high_output
&= ~nSRST
;
2634 /* initialize high port */
2635 buf
[0] = 0x82; /* command "set data bits high byte" */
2636 buf
[1] = high_output
; /* value */
2637 buf
[2] = high_direction
; /* all outputs - xRST */
2638 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2640 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2642 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2643 return ERROR_JTAG_INIT_FAILED
;
2649 static int cortino_jtag_init(void)
2655 low_direction
= 0x1b;
2657 /* initialize low byte for jtag */
2658 buf
[0] = 0x80; /* command "set data bits low byte" */
2659 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2660 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2661 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2663 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2665 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2666 return ERROR_JTAG_INIT_FAILED
;
2670 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2672 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2675 high_direction
= 0x03;
2677 /* initialize high port */
2678 buf
[0] = 0x82; /* command "set data bits high byte" */
2679 buf
[1] = high_output
;
2680 buf
[2] = high_direction
;
2681 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2683 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2685 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2686 return ERROR_JTAG_INIT_FAILED
;
2692 static void olimex_jtag_blink(void)
2694 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2695 * ACBUS3 is bit 3 of the GPIOH port
2697 if (high_output
& 0x08)
2699 /* set port pin high */
2700 high_output
&= 0x07;
2704 /* set port pin low */
2705 high_output
|= 0x08;
2709 buffer_write(high_output
);
2710 buffer_write(high_direction
);
2714 static void flyswatter_jtag_blink(void)
2717 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2719 high_output
^= 0x0c;
2722 buffer_write(high_output
);
2723 buffer_write(high_direction
);
2727 static void turtle_jtag_blink(void)
2730 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2732 if (high_output
& 0x08)
2742 buffer_write(high_output
);
2743 buffer_write(high_direction
);
2747 static int ft2232_quit(void)
2749 #if BUILD_FT2232_FTD2XX == 1
2752 status
= FT_Close(ftdih
);
2753 #elif BUILD_FT2232_LIBFTDI == 1
2754 ftdi_usb_close(&ftdic
);
2756 ftdi_deinit(&ftdic
);
2759 free(ft2232_buffer
);
2760 ft2232_buffer
= NULL
;
2766 static int ft2232_handle_device_desc_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2772 ft2232_device_desc
= strdup(args
[0]);
2773 cp
= strchr(ft2232_device_desc
, 0);
2774 // under Win32, the FTD2XX driver appends an "A" to the end
2775 // of the description, this examines the given desc
2776 // and creates the 'missing' _A or non_A variable.
2777 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
2778 // it was, so make this the "A" version.
2779 ft2232_device_desc_A
= ft2232_device_desc
;
2780 // and *CREATE* the non-A version.
2781 strcpy(buf
, ft2232_device_desc
);
2782 cp
= strchr(buf
, 0);
2784 ft2232_device_desc
= strdup(buf
);
2786 // <space>A not defined
2788 sprintf(buf
, "%s A", ft2232_device_desc
);
2789 ft2232_device_desc_A
= strdup(buf
);
2794 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2801 static int ft2232_handle_serial_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2805 ft2232_serial
= strdup(args
[0]);
2809 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2816 static int ft2232_handle_layout_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2821 ft2232_layout
= malloc(strlen(args
[0]) + 1);
2822 strcpy(ft2232_layout
, args
[0]);
2828 static int ft2232_handle_vid_pid_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2830 if (argc
> MAX_USB_IDS
* 2)
2832 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2833 "(maximum is %d pairs)", MAX_USB_IDS
);
2834 argc
= MAX_USB_IDS
* 2;
2836 if (argc
< 2 || (argc
& 1))
2838 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2840 return ERROR_COMMAND_SYNTAX_ERROR
;
2841 // remove the incomplete trailing id
2846 int retval
= ERROR_OK
;
2847 for (i
= 0; i
< argc
; i
+= 2)
2849 retval
= parse_u16(args
[i
], &ft2232_vid
[i
>> 1]);
2850 if (ERROR_OK
!= retval
)
2852 retval
= parse_u16(args
[i
+ 1], &ft2232_pid
[i
>> 1]);
2853 if (ERROR_OK
!= retval
)
2858 * Explicitly terminate, in case there are multiples instances of
2861 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2867 static int ft2232_handle_latency_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2871 ft2232_latency
= atoi(args
[0]);
2875 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2882 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
)
2886 /* 7 bits of either ones or zeros. */
2887 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
2889 while (num_cycles
> 0)
2891 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2892 * at most 7 bits per invocation. Here we invoke it potentially
2895 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
2897 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
2899 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2900 retval
= ERROR_JTAG_QUEUE_FAILED
;
2905 /* there are no state transitions in this code, so omit state tracking */
2907 /* command "Clock Data to TMS/CS Pin (no Read)" */
2911 buffer_write(bitcount_per_command
- 1);
2913 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2918 num_cycles
-= bitcount_per_command
;
2925 /* ---------------------------------------------------------------------
2926 * Support for IceBear JTAG adapter from Section5:
2927 * http://section5.ch/icebear
2929 * Author: Sten, debian@sansys-electronic.com
2932 /* Icebear pin layout
2934 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2935 * GND GND | 4 3| n.c.
2936 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2937 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2938 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2939 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2940 * ADBUS2 TDO |14 13| GND GND
2942 * ADBUS0 O L TCK ACBUS0 GND
2943 * ADBUS1 O L TDI ACBUS1 GND
2944 * ADBUS2 I TDO ACBUS2 n.c.
2945 * ADBUS3 O H TMS ACBUS3 n.c.
2951 static int icebear_jtag_init(void) {
2955 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
2956 low_output
= 0x08; /* high: TMS; low: TCK TDI */
2960 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2961 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
2962 low_direction
&= ~nTRST
; /* nTRST high impedance */
2965 low_direction
|= nTRST
;
2966 low_output
|= nTRST
;
2969 low_direction
|= nSRST
;
2970 low_output
|= nSRST
;
2972 /* initialize low byte for jtag */
2973 buf
[0] = 0x80; /* command "set data bits low byte" */
2974 buf
[1] = low_output
;
2975 buf
[2] = low_direction
;
2976 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2978 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
2979 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
2980 return ERROR_JTAG_INIT_FAILED
;
2984 high_direction
= 0x00;
2987 /* initialize high port */
2988 buf
[0] = 0x82; /* command "set data bits high byte" */
2989 buf
[1] = high_output
; /* value */
2990 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2991 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2993 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
2994 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
2995 return ERROR_JTAG_INIT_FAILED
;
3001 static void icebear_jtag_reset(int trst
, int srst
) {
3004 low_direction
|= nTRST
;
3005 low_output
&= ~nTRST
;
3007 else if (trst
== 0) {
3008 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3009 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3010 low_direction
&= ~nTRST
;
3012 low_output
|= nTRST
;
3016 low_output
&= ~nSRST
;
3018 else if (srst
== 0) {
3019 low_output
|= nSRST
;
3022 /* command "set data bits low byte" */
3024 buffer_write(low_output
);
3025 buffer_write(low_direction
);
3027 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);