1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
30 /* This code uses information contained in the MPSSE specification which was
32 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
33 * Hereafter this is called the "MPSSE Spec".
35 * The datasheet for the ftdichip.com's FT2232D part is here:
36 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
43 /* project specific includes */
44 #include "interface.h"
46 #include "time_support.h"
54 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
55 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
56 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
57 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
60 /* FT2232 access library includes */
61 #if BUILD_FT2232_FTD2XX == 1
63 #elif BUILD_FT2232_LIBFTDI == 1
67 /* max TCK for the high speed devices 30000 kHz */
68 #define FTDI_2232H_4232H_MAX_TCK 30000
69 /* max TCK for the full speed devices 6000 kHz */
70 #define FTDI_2232C_MAX_TCK 6000
71 /* this speed value tells that RTCK is requested */
75 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
76 * errors with a retry count of 100. Increasing it solves the problem for me.
79 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
80 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
83 #define LIBFTDI_READ_RETRY_COUNT 2000
85 #ifndef BUILD_FT2232_HIGHSPEED
86 #if BUILD_FT2232_FTD2XX == 1
87 enum { FT_DEVICE_2232H
= 6, FT_DEVICE_4232H
};
88 #elif BUILD_FT2232_LIBFTDI == 1
89 enum { TYPE_2232H
= 4, TYPE_4232H
= 5 };
93 static int ft2232_execute_queue(void);
94 static int ft2232_speed(int speed
);
95 static int ft2232_speed_div(int speed
, int* khz
);
96 static int ft2232_khz(int khz
, int* jtag_speed
);
97 static int ft2232_register_commands(struct command_context_s
* cmd_ctx
);
98 static int ft2232_init(void);
99 static int ft2232_quit(void);
101 static int ft2232_handle_device_desc_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
102 static int ft2232_handle_serial_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
103 static int ft2232_handle_layout_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
104 static int ft2232_handle_vid_pid_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
105 static int ft2232_handle_latency_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
);
108 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
109 * stable state. Calling code must ensure that current state is stable,
110 * that verification is not done in here.
112 * @param num_cycles The number of clocks cycles to send.
113 * @param cmd The command to send.
115 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
117 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
);
119 static char * ft2232_device_desc_A
= NULL
;
120 static char* ft2232_device_desc
= NULL
;
121 static char* ft2232_serial
= NULL
;
122 static char* ft2232_layout
= NULL
;
123 static uint8_t ft2232_latency
= 2;
124 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
126 #define MAX_USB_IDS 8
127 /* vid = pid = 0 marks the end of the list */
128 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
129 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
131 typedef struct ft2232_layout_s
135 void (*reset
)(int trst
, int srst
);
139 /* init procedures for supported layouts */
140 static int usbjtag_init(void);
141 static int jtagkey_init(void);
142 static int olimex_jtag_init(void);
143 static int flyswatter_init(void);
144 static int turtle_init(void);
145 static int comstick_init(void);
146 static int stm32stick_init(void);
147 static int axm0432_jtag_init(void);
148 static int sheevaplug_init(void);
149 static int icebear_jtag_init(void);
150 static int cortino_jtag_init(void);
151 static int signalyzer_h_init(void);
152 static int ktlink_init(void);
154 /* reset procedures for supported layouts */
155 static void usbjtag_reset(int trst
, int srst
);
156 static void jtagkey_reset(int trst
, int srst
);
157 static void olimex_jtag_reset(int trst
, int srst
);
158 static void flyswatter_reset(int trst
, int srst
);
159 static void turtle_reset(int trst
, int srst
);
160 static void comstick_reset(int trst
, int srst
);
161 static void stm32stick_reset(int trst
, int srst
);
162 static void axm0432_jtag_reset(int trst
, int srst
);
163 static void sheevaplug_reset(int trst
, int srst
);
164 static void icebear_jtag_reset(int trst
, int srst
);
165 static void signalyzer_h_reset(int trst
, int srst
);
166 static void ktlink_reset(int trst
, int srst
);
168 /* blink procedures for layouts that support a blinking led */
169 static void olimex_jtag_blink(void);
170 static void flyswatter_jtag_blink(void);
171 static void turtle_jtag_blink(void);
172 static void signalyzer_h_blink(void);
173 static void ktlink_blink(void);
175 static const ft2232_layout_t ft2232_layouts
[] =
177 { "usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
178 { "jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
179 { "jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
180 { "oocdlink", jtagkey_init
, jtagkey_reset
, NULL
},
181 { "signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
182 { "evb_lm3s811", usbjtag_init
, usbjtag_reset
, NULL
},
183 { "luminary_icdi", usbjtag_init
, usbjtag_reset
, NULL
},
184 { "olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
185 { "flyswatter", flyswatter_init
, flyswatter_reset
, flyswatter_jtag_blink
},
186 { "turtelizer2", turtle_init
, turtle_reset
, turtle_jtag_blink
},
187 { "comstick", comstick_init
, comstick_reset
, NULL
},
188 { "stm32stick", stm32stick_init
, stm32stick_reset
, NULL
},
189 { "axm0432_jtag", axm0432_jtag_init
, axm0432_jtag_reset
, NULL
},
190 { "sheevaplug", sheevaplug_init
, sheevaplug_reset
, NULL
},
191 { "icebear", icebear_jtag_init
, icebear_jtag_reset
, NULL
},
192 { "cortino", cortino_jtag_init
, comstick_reset
, NULL
},
193 { "signalyzer-h", signalyzer_h_init
, signalyzer_h_reset
, signalyzer_h_blink
},
194 { "ktlink", ktlink_init
, ktlink_reset
, ktlink_blink
},
195 { NULL
, NULL
, NULL
, NULL
},
198 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
200 static const ft2232_layout_t
*layout
;
201 static uint8_t low_output
= 0x0;
202 static uint8_t low_direction
= 0x0;
203 static uint8_t high_output
= 0x0;
204 static uint8_t high_direction
= 0x0;
206 #if BUILD_FT2232_FTD2XX == 1
207 static FT_HANDLE ftdih
= NULL
;
208 static FT_DEVICE ftdi_device
= 0;
209 #elif BUILD_FT2232_LIBFTDI == 1
210 static struct ftdi_context ftdic
;
211 static enum ftdi_chip_type ftdi_device
;
214 static jtag_command_t
* first_unsent
; /* next command that has to be sent */
215 static int require_send
;
217 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
219 "There is a significant difference between libftdi and libftd2xx. The latter
220 one allows to schedule up to 64*64 bytes of result data while libftdi fails
221 with more than 4*64. As a consequence, the FT2232 driver is forced to
222 perform around 16x more USB transactions for long command streams with TDO
223 capture when running with libftdi."
226 #define FT2232_BUFFER_SIZE 131072
227 a comment would have been nice.
230 #define FT2232_BUFFER_SIZE 131072
232 static uint8_t* ft2232_buffer
= NULL
;
233 static int ft2232_buffer_size
= 0;
234 static int ft2232_read_pointer
= 0;
235 static int ft2232_expect_read
= 0;
238 * Function buffer_write
239 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
240 * @param val is the byte to send.
242 static inline void buffer_write(uint8_t val
)
244 assert(ft2232_buffer
);
245 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
246 ft2232_buffer
[ft2232_buffer_size
++] = val
;
250 * Function buffer_read
251 * returns a byte from the byte buffer.
253 static inline uint8_t buffer_read(void)
255 assert(ft2232_buffer
);
256 assert(ft2232_read_pointer
< ft2232_buffer_size
);
257 return ft2232_buffer
[ft2232_read_pointer
++];
261 * Clocks out \a bit_count bits on the TMS line, starting with the least
262 * significant bit of tms_bits and progressing to more significant bits.
263 * Rigorous state transition logging is done here via tap_set_state().
265 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
266 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
267 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
268 * is often used for this, 0x4b.
270 * @param tms_bits Holds the sequence of bits to send.
271 * @param tms_count Tells how many bits in the sequence.
272 * @param tdi_bit A single bit to pass on to TDI before the first TCK
273 * cycle and held static for the duration of TMS clocking.
275 * See the MPSSE spec referenced above.
277 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
281 int tms_ndx
; /* bit index into tms_byte */
283 assert(tms_count
> 0);
285 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
286 mpsse_cmd
, tms_bits
, tms_count
);
288 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
290 bool bit
= tms_bits
& 1;
293 tms_byte
|= (1 << tms_ndx
);
295 /* always do state transitions in public view */
296 tap_set_state(tap_state_transition(tap_get_state(), bit
));
298 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
303 if (tms_ndx
== 7 || i
== tms_count
-1)
305 buffer_write(mpsse_cmd
);
306 buffer_write(tms_ndx
- 1);
308 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
309 TMS/CS and is held static for the duration of TMS/CS clocking.
311 buffer_write(tms_byte
| (tdi_bit
<< 7));
317 * Function get_tms_buffer_requirements
318 * returns what clock_tms() will consume if called with
321 static inline int get_tms_buffer_requirements(int bit_count
)
323 return ((bit_count
+ 6)/7) * 3;
327 * Function move_to_state
328 * moves the TAP controller from the current state to a
329 * \a goal_state through a path given by tap_get_tms_path(). State transition
330 * logging is performed by delegation to clock_tms().
332 * @param goal_state is the destination state for the move.
334 static void move_to_state(tap_state_t goal_state
)
336 tap_state_t start_state
= tap_get_state();
338 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
339 lookup of the required TMS pattern to move to this state from the
343 /* do the 2 lookups */
344 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
345 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
347 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
349 clock_tms(0x4b, tms_bits
, tms_count
, 0);
352 jtag_interface_t ft2232_interface
=
355 .execute_queue
= ft2232_execute_queue
,
356 .speed
= ft2232_speed
,
357 .speed_div
= ft2232_speed_div
,
359 .register_commands
= ft2232_register_commands
,
364 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
366 #if BUILD_FT2232_FTD2XX == 1
368 DWORD dw_bytes_written
;
369 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
371 *bytes_written
= dw_bytes_written
;
372 LOG_ERROR("FT_Write returned: %lu", status
);
373 return ERROR_JTAG_DEVICE_ERROR
;
377 *bytes_written
= dw_bytes_written
;
380 #elif BUILD_FT2232_LIBFTDI == 1
382 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
385 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
386 return ERROR_JTAG_DEVICE_ERROR
;
390 *bytes_written
= retval
;
396 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
398 #if BUILD_FT2232_FTD2XX == 1
404 while ((*bytes_read
< size
) && timeout
--)
406 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
407 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
410 LOG_ERROR("FT_Read returned: %lu", status
);
411 return ERROR_JTAG_DEVICE_ERROR
;
413 *bytes_read
+= dw_bytes_read
;
416 #elif BUILD_FT2232_LIBFTDI == 1
418 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
421 while ((*bytes_read
< size
) && timeout
--)
423 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
426 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
427 return ERROR_JTAG_DEVICE_ERROR
;
429 *bytes_read
+= retval
;
434 if (*bytes_read
< size
)
436 LOG_ERROR("couldn't read enough bytes from "
437 "FT2232 device (%i < %i)",
438 (unsigned)*bytes_read
,
440 return ERROR_JTAG_DEVICE_ERROR
;
446 static bool ft2232_device_is_highspeed(void)
448 #if BUILD_FT2232_FTD2XX == 1
449 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
450 #elif BUILD_FT2232_LIBFTDI == 1
451 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
456 * Commands that only apply to the FT2232H and FT4232H devices.
457 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
458 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
461 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
463 uint8_t buf
= enable
? 0x96 : 0x97;
464 LOG_DEBUG("%2.2x", buf
);
466 uint32_t bytes_written
;
467 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
468 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
470 LOG_ERROR("couldn't write command to %s adaptive clocking"
471 , enable
? "enable" : "disable");
479 * Enable/disable the clk divide by 5 of the 60MHz master clock.
480 * This result in a JTAG clock speed range of 91.553Hz-6MHz
481 * respective 457.763Hz-30MHz.
483 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
485 uint32_t bytes_written
;
486 uint8_t buf
= enable
? 0x8b : 0x8a;
487 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
488 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
490 LOG_ERROR("couldn't write command to %s clk divide by 5"
491 , enable
? "enable" : "disable");
492 return ERROR_JTAG_INIT_FAILED
;
494 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
495 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
500 static int ft2232_speed(int speed
)
504 uint32_t bytes_written
;
507 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
508 if (ft2232_device_is_highspeed())
509 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
510 else if (enable_adaptive_clocking
)
512 LOG_ERROR("ft2232 device %lu does not support RTCK"
513 , (long unsigned int)ftdi_device
);
517 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
520 buf
[0] = 0x86; /* command "set divisor" */
521 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
522 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
524 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
525 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
527 LOG_ERROR("couldn't set FT2232 TCK speed");
534 static int ft2232_speed_div(int speed
, int* khz
)
536 /* Take a look in the FT2232 manual,
537 * AN2232C-01 Command Processor for
538 * MPSSE and MCU Host Bus. Chapter 3.8 */
540 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
545 static int ft2232_khz(int khz
, int* jtag_speed
)
549 if (ft2232_device_is_highspeed())
551 *jtag_speed
= RTCK_SPEED
;
556 LOG_DEBUG("RCLK not supported");
561 /* Take a look in the FT2232 manual,
562 * AN2232C-01 Command Processor for
563 * MPSSE and MCU Host Bus. Chapter 3.8
565 * We will calc here with a multiplier
566 * of 10 for better rounding later. */
568 /* Calc speed, (ft2232_max_tck / khz) - 1 */
569 /* Use 65000 for better rounding */
570 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
572 /* Add 0.9 for rounding */
575 /* Calc real speed */
576 *jtag_speed
= *jtag_speed
/ 10;
578 /* Check if speed is greater than 0 */
584 /* Check max value */
585 if (*jtag_speed
> 0xFFFF)
587 *jtag_speed
= 0xFFFF;
593 static int ft2232_register_commands(struct command_context_s
* cmd_ctx
)
595 register_command(cmd_ctx
, NULL
, "ft2232_device_desc", ft2232_handle_device_desc_command
,
596 COMMAND_CONFIG
, "the USB device description of the FTDI FT2232 device");
597 register_command(cmd_ctx
, NULL
, "ft2232_serial", ft2232_handle_serial_command
,
598 COMMAND_CONFIG
, "the serial number of the FTDI FT2232 device");
599 register_command(cmd_ctx
, NULL
, "ft2232_layout", ft2232_handle_layout_command
,
600 COMMAND_CONFIG
, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
601 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid", ft2232_handle_vid_pid_command
,
602 COMMAND_CONFIG
, "the vendor ID and product ID of the FTDI FT2232 device");
603 register_command(cmd_ctx
, NULL
, "ft2232_latency", ft2232_handle_latency_command
,
604 COMMAND_CONFIG
, "set the FT2232 latency timer to a new value");
608 static void ft2232_end_state(tap_state_t state
)
610 if (tap_is_state_stable(state
))
611 tap_set_end_state(state
);
614 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
619 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
621 int num_bytes
= (scan_size
+ 7) / 8;
622 int bits_left
= scan_size
;
625 while (num_bytes
-- > 1)
627 buffer
[cur_byte
++] = buffer_read();
631 buffer
[cur_byte
] = 0x0;
633 /* There is one more partial byte left from the clock data in/out instructions */
636 buffer
[cur_byte
] = buffer_read() >> 1;
638 /* 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 */
639 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
642 static void ft2232_debug_dump_buffer(void)
648 for (i
= 0; i
< ft2232_buffer_size
; i
++)
650 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
653 LOG_DEBUG("%s", line
);
659 LOG_DEBUG("%s", line
);
662 static int ft2232_send_and_recv(jtag_command_t
* first
, jtag_command_t
* last
)
669 uint32_t bytes_written
= 0;
670 uint32_t bytes_read
= 0;
672 #ifdef _DEBUG_USB_IO_
673 struct timeval start
, inter
, inter2
, end
;
674 struct timeval d_inter
, d_inter2
, d_end
;
677 #ifdef _DEBUG_USB_COMMS_
678 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
679 ft2232_debug_dump_buffer();
682 #ifdef _DEBUG_USB_IO_
683 gettimeofday(&start
, NULL
);
686 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
688 LOG_ERROR("couldn't write MPSSE commands to FT2232");
692 #ifdef _DEBUG_USB_IO_
693 gettimeofday(&inter
, NULL
);
696 if (ft2232_expect_read
)
698 /* FIXME this "timeout" is never changed ... */
699 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
700 ft2232_buffer_size
= 0;
702 #ifdef _DEBUG_USB_IO_
703 gettimeofday(&inter2
, NULL
);
706 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
708 LOG_ERROR("couldn't read from FT2232");
712 #ifdef _DEBUG_USB_IO_
713 gettimeofday(&end
, NULL
);
715 timeval_subtract(&d_inter
, &inter
, &start
);
716 timeval_subtract(&d_inter2
, &inter2
, &start
);
717 timeval_subtract(&d_end
, &end
, &start
);
719 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
720 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
721 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
722 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
725 ft2232_buffer_size
= bytes_read
;
727 if (ft2232_expect_read
!= ft2232_buffer_size
)
729 LOG_ERROR("ft2232_expect_read (%i) != "
730 "ft2232_buffer_size (%i) "
734 LIBFTDI_READ_RETRY_COUNT
- timeout
);
735 ft2232_debug_dump_buffer();
740 #ifdef _DEBUG_USB_COMMS_
741 LOG_DEBUG("read buffer (%i retries): %i bytes",
742 LIBFTDI_READ_RETRY_COUNT
- timeout
,
744 ft2232_debug_dump_buffer();
748 ft2232_expect_read
= 0;
749 ft2232_read_pointer
= 0;
751 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
752 * that wasn't handled by a caller-provided error handler
762 type
= jtag_scan_type(cmd
->cmd
.scan
);
763 if (type
!= SCAN_OUT
)
765 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
766 buffer
= calloc(CEIL(scan_size
, 8), 1);
767 ft2232_read_scan(type
, buffer
, scan_size
);
768 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
769 retval
= ERROR_JTAG_QUEUE_FAILED
;
781 ft2232_buffer_size
= 0;
787 * Function ft2232_add_pathmove
788 * moves the TAP controller from the current state to a new state through the
789 * given path, where path is an array of tap_state_t's.
791 * @param path is an array of tap_stat_t which gives the states to traverse through
792 * ending with the last state at path[num_states-1]
793 * @param num_states is the count of state steps to move through
795 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
799 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
803 /* this loop verifies that the path is legal and logs each state in the path */
806 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
808 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
810 /* command "Clock Data to TMS/CS Pin (no Read)" */
813 /* number of states remaining */
814 buffer_write(num_states_batch
- 1);
816 while (num_states_batch
--) {
817 /* either TMS=0 or TMS=1 must work ... */
818 if (tap_state_transition(tap_get_state(), false)
819 == path
[state_count
])
820 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
821 else if (tap_state_transition(tap_get_state(), true)
822 == path
[state_count
])
823 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
825 /* ... or else the caller goofed BADLY */
827 LOG_ERROR("BUG: %s -> %s isn't a valid "
828 "TAP state transition",
829 tap_state_name(tap_get_state()),
830 tap_state_name(path
[state_count
]));
834 tap_set_state(path
[state_count
]);
839 buffer_write(tms_byte
);
841 tap_set_end_state(tap_get_state());
844 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
846 int num_bytes
= (scan_size
+ 7) / 8;
847 int bits_left
= scan_size
;
853 if (tap_get_state() != TAP_DRSHIFT
)
855 move_to_state(TAP_DRSHIFT
);
860 if (tap_get_state() != TAP_IRSHIFT
)
862 move_to_state(TAP_IRSHIFT
);
866 /* add command for complete bytes */
867 while (num_bytes
> 1)
872 /* Clock Data Bytes In and Out LSB First */
874 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
876 else if (type
== SCAN_OUT
)
878 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
880 /* LOG_DEBUG("added TDI bytes (o)"); */
882 else if (type
== SCAN_IN
)
884 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
886 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
889 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
890 num_bytes
-= thisrun_bytes
;
892 buffer_write((uint8_t) (thisrun_bytes
- 1));
893 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
897 /* add complete bytes */
898 while (thisrun_bytes
-- > 0)
900 buffer_write(buffer
[cur_byte
++]);
904 else /* (type == SCAN_IN) */
906 bits_left
-= 8 * (thisrun_bytes
);
910 /* the most signifcant bit is scanned during TAP movement */
912 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
916 /* process remaining bits but the last one */
921 /* Clock Data Bits In and Out LSB First */
923 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
925 else if (type
== SCAN_OUT
)
927 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
929 /* LOG_DEBUG("added TDI bits (o)"); */
931 else if (type
== SCAN_IN
)
933 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
935 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
938 buffer_write(bits_left
- 2);
940 buffer_write(buffer
[cur_byte
]);
943 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
944 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
948 /* Clock Data Bits In and Out LSB First */
950 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
952 else if (type
== SCAN_OUT
)
954 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
956 /* LOG_DEBUG("added TDI bits (o)"); */
958 else if (type
== SCAN_IN
)
960 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
962 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
965 buffer_write(last_bit
);
973 /* move from Shift-IR/DR to end state */
974 if (type
!= SCAN_OUT
)
976 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
977 /* This must be coordinated with the bit shifts in ft2232_read_scan */
980 /* Clock Data to TMS/CS Pin with Read */
985 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
986 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
987 /* Clock Data to TMS/CS Pin (no Read) */
991 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
992 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
995 if (tap_get_state() != tap_get_end_state())
997 move_to_state(tap_get_end_state());
1001 static int ft2232_large_scan(scan_command_t
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
1003 int num_bytes
= (scan_size
+ 7) / 8;
1004 int bits_left
= scan_size
;
1007 uint8_t* receive_buffer
= malloc(CEIL(scan_size
, 8));
1008 uint8_t* receive_pointer
= receive_buffer
;
1009 uint32_t bytes_written
;
1010 uint32_t bytes_read
;
1012 int thisrun_read
= 0;
1016 LOG_ERROR("BUG: large IR scans are not supported");
1020 if (tap_get_state() != TAP_DRSHIFT
)
1022 move_to_state(TAP_DRSHIFT
);
1025 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1027 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1030 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1031 ft2232_buffer_size
, (int)bytes_written
);
1032 ft2232_buffer_size
= 0;
1034 /* add command for complete bytes */
1035 while (num_bytes
> 1)
1039 if (type
== SCAN_IO
)
1041 /* Clock Data Bytes In and Out LSB First */
1043 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1045 else if (type
== SCAN_OUT
)
1047 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1049 /* LOG_DEBUG("added TDI bytes (o)"); */
1051 else if (type
== SCAN_IN
)
1053 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1055 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1058 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1059 thisrun_read
= thisrun_bytes
;
1060 num_bytes
-= thisrun_bytes
;
1061 buffer_write((uint8_t) (thisrun_bytes
- 1));
1062 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1064 if (type
!= SCAN_IN
)
1066 /* add complete bytes */
1067 while (thisrun_bytes
-- > 0)
1069 buffer_write(buffer
[cur_byte
]);
1074 else /* (type == SCAN_IN) */
1076 bits_left
-= 8 * (thisrun_bytes
);
1079 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1081 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1084 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1086 (int)bytes_written
);
1087 ft2232_buffer_size
= 0;
1089 if (type
!= SCAN_OUT
)
1091 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1093 LOG_ERROR("couldn't read from FT2232");
1096 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1099 receive_pointer
+= bytes_read
;
1105 /* the most signifcant bit is scanned during TAP movement */
1106 if (type
!= SCAN_IN
)
1107 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1111 /* process remaining bits but the last one */
1114 if (type
== SCAN_IO
)
1116 /* Clock Data Bits In and Out LSB First */
1118 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1120 else if (type
== SCAN_OUT
)
1122 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1124 /* LOG_DEBUG("added TDI bits (o)"); */
1126 else if (type
== SCAN_IN
)
1128 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1130 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1132 buffer_write(bits_left
- 2);
1133 if (type
!= SCAN_IN
)
1134 buffer_write(buffer
[cur_byte
]);
1136 if (type
!= SCAN_OUT
)
1140 if (tap_get_end_state() == TAP_DRSHIFT
)
1142 if (type
== SCAN_IO
)
1144 /* Clock Data Bits In and Out LSB First */
1146 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1148 else if (type
== SCAN_OUT
)
1150 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1152 /* LOG_DEBUG("added TDI bits (o)"); */
1154 else if (type
== SCAN_IN
)
1156 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1158 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1161 buffer_write(last_bit
);
1165 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1166 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1169 /* move from Shift-IR/DR to end state */
1170 if (type
!= SCAN_OUT
)
1172 /* Clock Data to TMS/CS Pin with Read */
1174 /* LOG_DEBUG("added TMS scan (read)"); */
1178 /* Clock Data to TMS/CS Pin (no Read) */
1180 /* LOG_DEBUG("added TMS scan (no read)"); */
1183 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1184 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1187 if (type
!= SCAN_OUT
)
1190 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1192 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1195 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1197 (int)bytes_written
);
1198 ft2232_buffer_size
= 0;
1200 if (type
!= SCAN_OUT
)
1202 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1204 LOG_ERROR("couldn't read from FT2232");
1207 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1210 receive_pointer
+= bytes_read
;
1216 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1218 int predicted_size
= 3;
1219 int num_bytes
= (scan_size
- 1) / 8;
1221 if (tap_get_state() != TAP_DRSHIFT
)
1222 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1224 if (type
== SCAN_IN
) /* only from device to host */
1226 /* complete bytes */
1227 predicted_size
+= CEIL(num_bytes
, 65536) * 3;
1229 /* remaining bits - 1 (up to 7) */
1230 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1232 else /* host to device, or bidirectional */
1234 /* complete bytes */
1235 predicted_size
+= num_bytes
+ CEIL(num_bytes
, 65536) * 3;
1237 /* remaining bits -1 (up to 7) */
1238 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1241 return predicted_size
;
1244 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1246 int predicted_size
= 0;
1248 if (type
!= SCAN_OUT
)
1250 /* complete bytes */
1251 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) - 1) : 0;
1253 /* remaining bits - 1 */
1254 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1256 /* last bit (from TMS scan) */
1257 predicted_size
+= 1;
1260 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1262 return predicted_size
;
1265 static void usbjtag_reset(int trst
, int srst
)
1267 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1270 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1271 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1273 low_output
&= ~nTRST
; /* switch output low */
1277 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1278 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1280 low_output
|= nTRST
; /* switch output high */
1285 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1286 low_output
&= ~nSRST
; /* switch output low */
1288 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1292 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1293 low_output
|= nSRST
; /* switch output high */
1295 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1298 /* command "set data bits low byte" */
1300 buffer_write(low_output
);
1301 buffer_write(low_direction
);
1304 static void jtagkey_reset(int trst
, int srst
)
1306 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1309 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1310 high_output
&= ~nTRSTnOE
;
1312 high_output
&= ~nTRST
;
1316 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1317 high_output
|= nTRSTnOE
;
1319 high_output
|= nTRST
;
1324 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1325 high_output
&= ~nSRST
;
1327 high_output
&= ~nSRSTnOE
;
1331 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1332 high_output
|= nSRST
;
1334 high_output
|= nSRSTnOE
;
1337 /* command "set data bits high byte" */
1339 buffer_write(high_output
);
1340 buffer_write(high_direction
);
1341 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1345 static void olimex_jtag_reset(int trst
, int srst
)
1347 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1350 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1351 high_output
&= ~nTRSTnOE
;
1353 high_output
&= ~nTRST
;
1357 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1358 high_output
|= nTRSTnOE
;
1360 high_output
|= nTRST
;
1365 high_output
|= nSRST
;
1369 high_output
&= ~nSRST
;
1372 /* command "set data bits high byte" */
1374 buffer_write(high_output
);
1375 buffer_write(high_direction
);
1376 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1380 static void axm0432_jtag_reset(int trst
, int srst
)
1384 tap_set_state(TAP_RESET
);
1385 high_output
&= ~nTRST
;
1389 high_output
|= nTRST
;
1394 high_output
&= ~nSRST
;
1398 high_output
|= nSRST
;
1401 /* command "set data bits low byte" */
1403 buffer_write(high_output
);
1404 buffer_write(high_direction
);
1405 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1409 static void flyswatter_reset(int trst
, int srst
)
1413 low_output
&= ~nTRST
;
1417 low_output
|= nTRST
;
1422 low_output
|= nSRST
;
1426 low_output
&= ~nSRST
;
1429 /* command "set data bits low byte" */
1431 buffer_write(low_output
);
1432 buffer_write(low_direction
);
1433 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1436 static void turtle_reset(int trst
, int srst
)
1442 low_output
|= nSRST
;
1446 low_output
&= ~nSRST
;
1449 /* command "set data bits low byte" */
1451 buffer_write(low_output
);
1452 buffer_write(low_direction
);
1453 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1456 static void comstick_reset(int trst
, int srst
)
1460 high_output
&= ~nTRST
;
1464 high_output
|= nTRST
;
1469 high_output
&= ~nSRST
;
1473 high_output
|= nSRST
;
1476 /* command "set data bits high byte" */
1478 buffer_write(high_output
);
1479 buffer_write(high_direction
);
1480 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1484 static void stm32stick_reset(int trst
, int srst
)
1488 high_output
&= ~nTRST
;
1492 high_output
|= nTRST
;
1497 low_output
&= ~nSRST
;
1501 low_output
|= nSRST
;
1504 /* command "set data bits low byte" */
1506 buffer_write(low_output
);
1507 buffer_write(low_direction
);
1509 /* command "set data bits high byte" */
1511 buffer_write(high_output
);
1512 buffer_write(high_direction
);
1513 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1517 static void sheevaplug_reset(int trst
, int srst
)
1520 high_output
&= ~nTRST
;
1522 high_output
|= nTRST
;
1525 high_output
&= ~nSRSTnOE
;
1527 high_output
|= nSRSTnOE
;
1529 /* command "set data bits high byte" */
1531 buffer_write(high_output
);
1532 buffer_write(high_direction
);
1533 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1536 static int ft2232_execute_runtest(jtag_command_t
*cmd
)
1540 int predicted_size
= 0;
1543 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1544 cmd
->cmd
.runtest
->num_cycles
,
1545 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1547 /* only send the maximum buffer size that FT2232C can handle */
1549 if (tap_get_state() != TAP_IDLE
)
1550 predicted_size
+= 3;
1551 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
1552 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1553 predicted_size
+= 3;
1554 if (tap_get_end_state() != TAP_IDLE
)
1555 predicted_size
+= 3;
1556 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1558 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1559 retval
= ERROR_JTAG_QUEUE_FAILED
;
1563 if (tap_get_state() != TAP_IDLE
)
1565 move_to_state(TAP_IDLE
);
1568 i
= cmd
->cmd
.runtest
->num_cycles
;
1571 /* there are no state transitions in this code, so omit state tracking */
1573 /* command "Clock Data to TMS/CS Pin (no Read)" */
1577 buffer_write((i
> 7) ? 6 : (i
- 1));
1582 i
-= (i
> 7) ? 7 : i
;
1583 /* LOG_DEBUG("added TMS scan (no read)"); */
1586 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1588 if (tap_get_state() != tap_get_end_state())
1590 move_to_state(tap_get_end_state());
1594 DEBUG_JTAG_IO("runtest: %i, end in %s",
1595 cmd
->cmd
.runtest
->num_cycles
,
1596 tap_state_name(tap_get_end_state()));
1600 static int ft2232_execute_statemove(jtag_command_t
*cmd
)
1602 int predicted_size
= 0;
1603 int retval
= ERROR_OK
;
1605 DEBUG_JTAG_IO("statemove end in %s",
1606 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1608 /* only send the maximum buffer size that FT2232C can handle */
1610 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1612 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1613 retval
= ERROR_JTAG_QUEUE_FAILED
;
1617 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1619 /* For TAP_RESET, ignore the current recorded state. It's often
1620 * wrong at server startup, and this transation is critical whenever
1623 if (tap_get_end_state() == TAP_RESET
) {
1624 clock_tms(0x4b, 0xff, 5, 0);
1627 /* shortest-path move to desired end state */
1628 } else if (tap_get_state() != tap_get_end_state())
1630 move_to_state(tap_get_end_state());
1637 static int ft2232_execute_pathmove(jtag_command_t
*cmd
)
1639 int predicted_size
= 0;
1640 int retval
= ERROR_OK
;
1642 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1643 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1645 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1646 tap_state_name(tap_get_state()),
1647 tap_state_name(path
[num_states
-1]));
1649 /* only send the maximum buffer size that FT2232C can handle */
1650 predicted_size
= 3 * CEIL(num_states
, 7);
1651 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1653 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1654 retval
= ERROR_JTAG_QUEUE_FAILED
;
1660 ft2232_add_pathmove(path
, num_states
);
1666 static int ft2232_execute_scan(jtag_command_t
*cmd
)
1669 int scan_size
; /* size of IR or DR scan */
1670 int predicted_size
= 0;
1671 int retval
= ERROR_OK
;
1673 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1675 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1677 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1679 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1680 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1682 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1683 /* unsent commands before this */
1684 if (first_unsent
!= cmd
)
1685 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1686 retval
= ERROR_JTAG_QUEUE_FAILED
;
1688 /* current command */
1689 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1690 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1692 first_unsent
= cmd
->next
;
1697 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1699 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1702 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1703 retval
= ERROR_JTAG_QUEUE_FAILED
;
1707 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1708 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1709 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1710 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1714 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1715 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1716 tap_state_name(tap_get_end_state()));
1721 static int ft2232_execute_reset(jtag_command_t
*cmd
)
1724 int predicted_size
= 0;
1727 DEBUG_JTAG_IO("reset trst: %i srst %i",
1728 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1730 /* only send the maximum buffer size that FT2232C can handle */
1732 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1734 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1735 retval
= ERROR_JTAG_QUEUE_FAILED
;
1740 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1742 tap_set_state(TAP_RESET
);
1745 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1748 DEBUG_JTAG_IO("trst: %i, srst: %i",
1749 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1753 static int ft2232_execute_sleep(jtag_command_t
*cmd
)
1758 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
1760 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1761 retval
= ERROR_JTAG_QUEUE_FAILED
;
1762 first_unsent
= cmd
->next
;
1763 jtag_sleep(cmd
->cmd
.sleep
->us
);
1764 DEBUG_JTAG_IO("sleep %i usec while in %s",
1766 tap_state_name(tap_get_state()));
1770 static int ft2232_execute_stableclocks(jtag_command_t
*cmd
)
1775 /* this is only allowed while in a stable state. A check for a stable
1776 * state was done in jtag_add_clocks()
1778 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1779 retval
= ERROR_JTAG_QUEUE_FAILED
;
1780 DEBUG_JTAG_IO("clocks %i while in %s",
1781 cmd
->cmd
.stableclocks
->num_cycles
,
1782 tap_state_name(tap_get_state()));
1786 static int ft2232_execute_command(jtag_command_t
*cmd
)
1793 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1794 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1795 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1796 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1797 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1798 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1799 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1801 LOG_ERROR("BUG: unknown JTAG command type encountered");
1807 static int ft2232_execute_queue()
1809 jtag_command_t
* cmd
= jtag_command_queue
; /* currently processed command */
1812 first_unsent
= cmd
; /* next command that has to be sent */
1815 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1816 * that wasn't handled by a caller-provided error handler
1820 ft2232_buffer_size
= 0;
1821 ft2232_expect_read
= 0;
1823 /* blink, if the current layout has that feature */
1829 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1830 retval
= ERROR_JTAG_QUEUE_FAILED
;
1831 /* Start reading input before FT2232 TX buffer fills up */
1833 if (ft2232_expect_read
> 256)
1835 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1836 retval
= ERROR_JTAG_QUEUE_FAILED
;
1841 if (require_send
> 0)
1842 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1843 retval
= ERROR_JTAG_QUEUE_FAILED
;
1848 #if BUILD_FT2232_FTD2XX == 1
1849 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1853 char SerialNumber
[16];
1854 char Description
[64];
1855 DWORD openex_flags
= 0;
1856 char* openex_string
= NULL
;
1857 uint8_t latency_timer
;
1859 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
1862 /* Add non-standard Vid/Pid to the linux driver */
1863 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1865 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
1869 if (ft2232_device_desc
&& ft2232_serial
)
1871 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1872 ft2232_device_desc
= NULL
;
1875 if (ft2232_device_desc
)
1877 openex_string
= ft2232_device_desc
;
1878 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1880 else if (ft2232_serial
)
1882 openex_string
= ft2232_serial
;
1883 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1887 LOG_ERROR("neither device description nor serial number specified");
1888 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1890 return ERROR_JTAG_INIT_FAILED
;
1893 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1894 if (status
!= FT_OK
) {
1895 /* under Win32, the FTD2XX driver appends an "A" to the end
1896 * of the description, if we tried by the desc, then
1897 * try by the alternate "A" description. */
1898 if (openex_string
== ft2232_device_desc
) {
1899 /* Try the alternate method. */
1900 openex_string
= ft2232_device_desc_A
;
1901 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1902 if (status
== FT_OK
) {
1903 /* yea, the "alternate" method worked! */
1905 /* drat, give the user a meaningfull message.
1906 * telling the use we tried *BOTH* methods. */
1907 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1909 ft2232_device_desc_A
);
1914 if (status
!= FT_OK
)
1920 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
1922 return ERROR_JTAG_INIT_FAILED
;
1924 LOG_ERROR("unable to open ftdi device: %lu", status
);
1925 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1926 if (status
== FT_OK
)
1928 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1931 for (i
= 0; i
< num_devices
; i
++)
1932 desc_array
[i
] = malloc(64);
1934 desc_array
[num_devices
] = NULL
;
1936 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1938 if (status
== FT_OK
)
1940 LOG_ERROR("ListDevices: %lu\n", num_devices
);
1941 for (i
= 0; i
< num_devices
; i
++)
1942 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
1945 for (i
= 0; i
< num_devices
; i
++)
1946 free(desc_array
[i
]);
1952 LOG_ERROR("ListDevices: NONE\n");
1954 return ERROR_JTAG_INIT_FAILED
;
1957 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1959 LOG_ERROR("unable to set latency timer: %lu", status
);
1960 return ERROR_JTAG_INIT_FAILED
;
1963 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1965 LOG_ERROR("unable to get latency timer: %lu", status
);
1966 return ERROR_JTAG_INIT_FAILED
;
1970 LOG_DEBUG("current latency timer: %i", latency_timer
);
1973 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1975 LOG_ERROR("unable to set timeouts: %lu", status
);
1976 return ERROR_JTAG_INIT_FAILED
;
1979 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1981 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
1982 return ERROR_JTAG_INIT_FAILED
;
1985 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
1987 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
1988 return ERROR_JTAG_INIT_FAILED
;
1992 static const char* type_str
[] =
1993 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1994 unsigned no_of_known_types
= sizeof(type_str
) / sizeof(type_str
[0]) - 1;
1995 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
1996 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
1997 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
1998 LOG_INFO("deviceID: %lu", deviceID
);
1999 LOG_INFO("SerialNumber: %s", SerialNumber
);
2000 LOG_INFO("Description: %s", Description
);
2006 static int ft2232_purge_ftd2xx(void)
2010 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
2012 LOG_ERROR("error purging ftd2xx device: %lu", status
);
2013 return ERROR_JTAG_INIT_FAILED
;
2019 #endif /* BUILD_FT2232_FTD2XX == 1 */
2021 #if BUILD_FT2232_LIBFTDI == 1
2022 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
2024 uint8_t latency_timer
;
2026 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2027 ft2232_layout
, vid
, pid
);
2029 if (ftdi_init(&ftdic
) < 0)
2030 return ERROR_JTAG_INIT_FAILED
;
2032 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
2034 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
2035 return ERROR_JTAG_INIT_FAILED
;
2038 /* context, vendor id, product id */
2039 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2043 LOG_WARNING("unable to open ftdi device (trying more): %s",
2046 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2048 return ERROR_JTAG_INIT_FAILED
;
2051 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2052 if (ftdi_usb_reset(&ftdic
) < 0)
2054 LOG_ERROR("unable to reset ftdi device");
2055 return ERROR_JTAG_INIT_FAILED
;
2058 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2060 LOG_ERROR("unable to set latency timer");
2061 return ERROR_JTAG_INIT_FAILED
;
2064 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2066 LOG_ERROR("unable to get latency timer");
2067 return ERROR_JTAG_INIT_FAILED
;
2071 LOG_DEBUG("current latency timer: %i", latency_timer
);
2074 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2076 ftdi_device
= ftdic
.type
;
2077 static const char* type_str
[] =
2078 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2079 unsigned no_of_known_types
= sizeof(type_str
) / sizeof(type_str
[0]) - 1;
2080 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2081 ? ftdi_device
: no_of_known_types
;
2082 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2086 static int ft2232_purge_libftdi(void)
2088 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2090 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2091 return ERROR_JTAG_INIT_FAILED
;
2097 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2099 static int ft2232_init(void)
2103 uint32_t bytes_written
;
2104 const ft2232_layout_t
* cur_layout
= ft2232_layouts
;
2107 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2109 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2113 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2116 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2118 ft2232_layout
= "usbjtag";
2119 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2122 while (cur_layout
->name
)
2124 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2126 layout
= cur_layout
;
2134 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2135 return ERROR_JTAG_INIT_FAILED
;
2141 * "more indicates that there are more IDs to try, so we should
2142 * not print an error for an ID mismatch (but for anything
2145 * try_more indicates that the error code returned indicates an
2146 * ID mismatch (and nothing else) and that we should proceeed
2147 * with the next ID pair.
2149 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2152 #if BUILD_FT2232_FTD2XX == 1
2153 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2155 #elif BUILD_FT2232_LIBFTDI == 1
2156 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2161 if (!more
|| !try_more
)
2165 ft2232_buffer_size
= 0;
2166 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2168 if (layout
->init() != ERROR_OK
)
2169 return ERROR_JTAG_INIT_FAILED
;
2171 if (ft2232_device_is_highspeed())
2173 #ifndef BUILD_FT2232_HIGHSPEED
2174 #if BUILD_FT2232_FTD2XX == 1
2175 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2176 #elif BUILD_FT2232_LIBFTDI == 1
2177 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2180 /* make sure the legacy mode is disabled */
2181 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2182 return ERROR_JTAG_INIT_FAILED
;
2185 ft2232_speed(jtag_get_speed());
2187 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2188 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2190 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2191 return ERROR_JTAG_INIT_FAILED
;
2194 #if BUILD_FT2232_FTD2XX == 1
2195 return ft2232_purge_ftd2xx();
2196 #elif BUILD_FT2232_LIBFTDI == 1
2197 return ft2232_purge_libftdi();
2203 static int usbjtag_init(void)
2206 uint32_t bytes_written
;
2209 low_direction
= 0x0b;
2211 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2218 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2225 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2232 low_direction
= 0x8b;
2234 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2241 low_direction
= 0xcb;
2245 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2246 return ERROR_JTAG_INIT_FAILED
;
2249 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2250 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2252 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2253 low_output
&= ~nTRST
; /* nTRST = 0 */
2257 low_direction
|= nTRSTnOE
; /* nTRST output */
2258 low_output
|= nTRST
; /* nTRST = 1 */
2261 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2263 low_direction
|= nSRSTnOE
; /* nSRST output */
2264 low_output
|= nSRST
; /* nSRST = 1 */
2268 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2269 low_output
&= ~nSRST
; /* nSRST = 0 */
2272 /* initialize low byte for jtag */
2273 buf
[0] = 0x80; /* command "set data bits low byte" */
2274 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2275 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2276 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2278 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2280 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2281 return ERROR_JTAG_INIT_FAILED
;
2287 static int axm0432_jtag_init(void)
2290 uint32_t bytes_written
;
2293 low_direction
= 0x2b;
2295 /* initialize low byte for jtag */
2296 buf
[0] = 0x80; /* command "set data bits low byte" */
2297 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2298 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2299 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2301 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2303 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2304 return ERROR_JTAG_INIT_FAILED
;
2307 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2310 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2312 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2316 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2321 high_direction
= 0x0c;
2323 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2324 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2326 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2330 high_output
|= nTRST
;
2333 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2335 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2339 high_output
|= nSRST
;
2342 /* initialize high port */
2343 buf
[0] = 0x82; /* command "set data bits high byte" */
2344 buf
[1] = high_output
; /* value */
2345 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2346 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2348 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2350 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2351 return ERROR_JTAG_INIT_FAILED
;
2357 static int jtagkey_init(void)
2360 uint32_t bytes_written
;
2363 low_direction
= 0x1b;
2365 /* initialize low byte for jtag */
2366 buf
[0] = 0x80; /* command "set data bits low byte" */
2367 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2368 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2369 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2371 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2373 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2374 return ERROR_JTAG_INIT_FAILED
;
2377 if (strcmp(layout
->name
, "jtagkey") == 0)
2384 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2385 || (strcmp(layout
->name
, "oocdlink") == 0))
2394 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2399 high_direction
= 0x0f;
2401 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2402 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2404 high_output
|= nTRSTnOE
;
2405 high_output
&= ~nTRST
;
2409 high_output
&= ~nTRSTnOE
;
2410 high_output
|= nTRST
;
2413 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2415 high_output
&= ~nSRSTnOE
;
2416 high_output
|= nSRST
;
2420 high_output
|= nSRSTnOE
;
2421 high_output
&= ~nSRST
;
2424 /* initialize high port */
2425 buf
[0] = 0x82; /* command "set data bits high byte" */
2426 buf
[1] = high_output
; /* value */
2427 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2428 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2430 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2432 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2433 return ERROR_JTAG_INIT_FAILED
;
2439 static int olimex_jtag_init(void)
2442 uint32_t bytes_written
;
2445 low_direction
= 0x1b;
2447 /* initialize low byte for jtag */
2448 buf
[0] = 0x80; /* command "set data bits low byte" */
2449 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2450 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2451 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2453 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2455 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2456 return ERROR_JTAG_INIT_FAILED
;
2462 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2465 high_direction
= 0x0f;
2467 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2468 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2470 high_output
|= nTRSTnOE
;
2471 high_output
&= ~nTRST
;
2475 high_output
&= ~nTRSTnOE
;
2476 high_output
|= nTRST
;
2479 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2481 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2485 high_output
&= ~nSRST
;
2488 /* turn red LED on */
2489 high_output
|= 0x08;
2491 /* initialize high port */
2492 buf
[0] = 0x82; /* command "set data bits high byte" */
2493 buf
[1] = high_output
; /* value */
2494 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2495 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2497 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2499 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2500 return ERROR_JTAG_INIT_FAILED
;
2506 static int flyswatter_init(void)
2509 uint32_t bytes_written
;
2512 low_direction
= 0xfb;
2514 /* initialize low byte for jtag */
2515 buf
[0] = 0x80; /* command "set data bits low byte" */
2516 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2517 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2518 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2520 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2522 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2523 return ERROR_JTAG_INIT_FAILED
;
2527 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2529 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2532 high_direction
= 0x0c;
2534 /* turn red LED3 on, LED2 off */
2535 high_output
|= 0x08;
2537 /* initialize high port */
2538 buf
[0] = 0x82; /* command "set data bits high byte" */
2539 buf
[1] = high_output
; /* value */
2540 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2541 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2543 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2545 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2546 return ERROR_JTAG_INIT_FAILED
;
2552 static int turtle_init(void)
2555 uint32_t bytes_written
;
2558 low_direction
= 0x5b;
2560 /* initialize low byte for jtag */
2561 buf
[0] = 0x80; /* command "set data bits low byte" */
2562 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2563 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2564 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2566 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2568 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2569 return ERROR_JTAG_INIT_FAILED
;
2575 high_direction
= 0x0C;
2577 /* initialize high port */
2578 buf
[0] = 0x82; /* command "set data bits high byte" */
2579 buf
[1] = high_output
;
2580 buf
[2] = high_direction
;
2581 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2583 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2585 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2586 return ERROR_JTAG_INIT_FAILED
;
2592 static int comstick_init(void)
2595 uint32_t bytes_written
;
2598 low_direction
= 0x0b;
2600 /* initialize low byte for jtag */
2601 buf
[0] = 0x80; /* command "set data bits low byte" */
2602 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2603 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2604 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2606 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2608 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2609 return ERROR_JTAG_INIT_FAILED
;
2613 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2615 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2618 high_direction
= 0x03;
2620 /* initialize high port */
2621 buf
[0] = 0x82; /* command "set data bits high byte" */
2622 buf
[1] = high_output
;
2623 buf
[2] = high_direction
;
2624 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2626 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2628 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2629 return ERROR_JTAG_INIT_FAILED
;
2635 static int stm32stick_init(void)
2638 uint32_t bytes_written
;
2641 low_direction
= 0x8b;
2643 /* initialize low byte for jtag */
2644 buf
[0] = 0x80; /* command "set data bits low byte" */
2645 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2646 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2647 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2649 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2651 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2652 return ERROR_JTAG_INIT_FAILED
;
2656 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2658 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2661 high_direction
= 0x03;
2663 /* initialize high port */
2664 buf
[0] = 0x82; /* command "set data bits high byte" */
2665 buf
[1] = high_output
;
2666 buf
[2] = high_direction
;
2667 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2669 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2671 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2672 return ERROR_JTAG_INIT_FAILED
;
2678 static int sheevaplug_init(void)
2681 uint32_t bytes_written
;
2684 low_direction
= 0x1b;
2686 /* initialize low byte for jtag */
2687 buf
[0] = 0x80; /* command "set data bits low byte" */
2688 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2689 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2690 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2692 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2694 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2695 return ERROR_JTAG_INIT_FAILED
;
2704 high_direction
= 0x0f;
2706 /* nTRST is always push-pull */
2707 high_output
&= ~nTRSTnOE
;
2708 high_output
|= nTRST
;
2710 /* nSRST is always open-drain */
2711 high_output
|= nSRSTnOE
;
2712 high_output
&= ~nSRST
;
2714 /* initialize high port */
2715 buf
[0] = 0x82; /* command "set data bits high byte" */
2716 buf
[1] = high_output
; /* value */
2717 buf
[2] = high_direction
; /* all outputs - xRST */
2718 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2720 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2722 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2723 return ERROR_JTAG_INIT_FAILED
;
2729 static int cortino_jtag_init(void)
2732 uint32_t bytes_written
;
2735 low_direction
= 0x1b;
2737 /* initialize low byte for jtag */
2738 buf
[0] = 0x80; /* command "set data bits low byte" */
2739 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2740 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2741 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2743 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2745 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2746 return ERROR_JTAG_INIT_FAILED
;
2750 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2752 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2755 high_direction
= 0x03;
2757 /* initialize high port */
2758 buf
[0] = 0x82; /* command "set data bits high byte" */
2759 buf
[1] = high_output
;
2760 buf
[2] = high_direction
;
2761 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2763 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2765 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2766 return ERROR_JTAG_INIT_FAILED
;
2772 static void olimex_jtag_blink(void)
2774 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2775 * ACBUS3 is bit 3 of the GPIOH port
2777 if (high_output
& 0x08)
2779 /* set port pin high */
2780 high_output
&= 0x07;
2784 /* set port pin low */
2785 high_output
|= 0x08;
2789 buffer_write(high_output
);
2790 buffer_write(high_direction
);
2793 static void flyswatter_jtag_blink(void)
2796 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2798 high_output
^= 0x0c;
2801 buffer_write(high_output
);
2802 buffer_write(high_direction
);
2805 static void turtle_jtag_blink(void)
2808 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2810 if (high_output
& 0x08)
2820 buffer_write(high_output
);
2821 buffer_write(high_direction
);
2824 static int ft2232_quit(void)
2826 #if BUILD_FT2232_FTD2XX == 1
2829 status
= FT_Close(ftdih
);
2830 #elif BUILD_FT2232_LIBFTDI == 1
2831 ftdi_usb_close(&ftdic
);
2833 ftdi_deinit(&ftdic
);
2836 free(ft2232_buffer
);
2837 ft2232_buffer
= NULL
;
2842 static int ft2232_handle_device_desc_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2848 ft2232_device_desc
= strdup(args
[0]);
2849 cp
= strchr(ft2232_device_desc
, 0);
2850 /* under Win32, the FTD2XX driver appends an "A" to the end
2851 * of the description, this examines the given desc
2852 * and creates the 'missing' _A or non_A variable. */
2853 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
2854 /* it was, so make this the "A" version. */
2855 ft2232_device_desc_A
= ft2232_device_desc
;
2856 /* and *CREATE* the non-A version. */
2857 strcpy(buf
, ft2232_device_desc
);
2858 cp
= strchr(buf
, 0);
2860 ft2232_device_desc
= strdup(buf
);
2862 /* <space > A not defined
2864 sprintf(buf
, "%s A", ft2232_device_desc
);
2865 ft2232_device_desc_A
= strdup(buf
);
2870 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2876 static int ft2232_handle_serial_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2880 ft2232_serial
= strdup(args
[0]);
2884 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2890 static int ft2232_handle_layout_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2895 ft2232_layout
= malloc(strlen(args
[0]) + 1);
2896 strcpy(ft2232_layout
, args
[0]);
2901 static int ft2232_handle_vid_pid_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2903 if (argc
> MAX_USB_IDS
* 2)
2905 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2906 "(maximum is %d pairs)", MAX_USB_IDS
);
2907 argc
= MAX_USB_IDS
* 2;
2909 if (argc
< 2 || (argc
& 1))
2911 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2913 return ERROR_COMMAND_SYNTAX_ERROR
;
2914 /* remove the incomplete trailing id */
2919 int retval
= ERROR_OK
;
2920 for (i
= 0; i
< argc
; i
+= 2)
2922 COMMAND_PARSE_NUMBER(u16
, args
[i
], ft2232_vid
[i
>> 1]);
2923 COMMAND_PARSE_NUMBER(u16
, args
[i
+ 1], ft2232_pid
[i
>> 1]);
2927 * Explicitly terminate, in case there are multiples instances of
2930 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2935 static int ft2232_handle_latency_command(struct command_context_s
* cmd_ctx
, char* cmd
, char** args
, int argc
)
2939 ft2232_latency
= atoi(args
[0]);
2943 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2949 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
)
2953 /* 7 bits of either ones or zeros. */
2954 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
2956 while (num_cycles
> 0)
2958 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2959 * at most 7 bits per invocation. Here we invoke it potentially
2962 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
2964 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
2966 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2967 retval
= ERROR_JTAG_QUEUE_FAILED
;
2972 /* there are no state transitions in this code, so omit state tracking */
2974 /* command "Clock Data to TMS/CS Pin (no Read)" */
2978 buffer_write(bitcount_per_command
- 1);
2980 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2985 num_cycles
-= bitcount_per_command
;
2991 /* ---------------------------------------------------------------------
2992 * Support for IceBear JTAG adapter from Section5:
2993 * http://section5.ch/icebear
2995 * Author: Sten, debian@sansys-electronic.com
2998 /* Icebear pin layout
3000 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3001 * GND GND | 4 3| n.c.
3002 * ADBUS3 TMS | 6 5| ADBUS6 VCC
3003 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3004 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3005 * ADBUS1 TDI |12 11| ACBUS1 (GND)
3006 * ADBUS2 TDO |14 13| GND GND
3008 * ADBUS0 O L TCK ACBUS0 GND
3009 * ADBUS1 O L TDI ACBUS1 GND
3010 * ADBUS2 I TDO ACBUS2 n.c.
3011 * ADBUS3 O H TMS ACBUS3 n.c.
3017 static int icebear_jtag_init(void) {
3019 uint32_t bytes_written
;
3021 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
3022 low_output
= 0x08; /* high: TMS; low: TCK TDI */
3026 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3027 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
3028 low_direction
&= ~nTRST
; /* nTRST high impedance */
3031 low_direction
|= nTRST
;
3032 low_output
|= nTRST
;
3035 low_direction
|= nSRST
;
3036 low_output
|= nSRST
;
3038 /* initialize low byte for jtag */
3039 buf
[0] = 0x80; /* command "set data bits low byte" */
3040 buf
[1] = low_output
;
3041 buf
[2] = low_direction
;
3042 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3044 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3045 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3046 return ERROR_JTAG_INIT_FAILED
;
3050 high_direction
= 0x00;
3053 /* initialize high port */
3054 buf
[0] = 0x82; /* command "set data bits high byte" */
3055 buf
[1] = high_output
; /* value */
3056 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3057 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3059 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3060 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3061 return ERROR_JTAG_INIT_FAILED
;
3067 static void icebear_jtag_reset(int trst
, int srst
) {
3070 low_direction
|= nTRST
;
3071 low_output
&= ~nTRST
;
3073 else if (trst
== 0) {
3074 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3075 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3076 low_direction
&= ~nTRST
;
3078 low_output
|= nTRST
;
3082 low_output
&= ~nSRST
;
3084 else if (srst
== 0) {
3085 low_output
|= nSRST
;
3088 /* command "set data bits low byte" */
3090 buffer_write(low_output
);
3091 buffer_write(low_direction
);
3093 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3096 /* ---------------------------------------------------------------------
3097 * Support for Signalyzer H2 and Signalyzer H4
3098 * JTAG adapter from Xverve Technologies Inc.
3099 * http://www.signalyzer.com or http://www.xverve.com
3101 * Author: Oleg Seiljus, oleg@signalyzer.com
3103 static unsigned char signalyzer_h_side
;
3104 static unsigned int signalyzer_h_adapter_type
;
3106 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3108 #if BUILD_FT2232_FTD2XX == 1
3109 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3112 #define SIGNALYZER_COMMAND_ADDR 128
3113 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3115 #define SIGNALYZER_COMMAND_VERSION 0x41
3116 #define SIGNALYZER_COMMAND_RESET 0x42
3117 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3118 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3119 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3120 #define SIGNALYZER_COMMAND_LED_SET 0x53
3121 #define SIGNALYZER_COMMAND_ADC 0x54
3122 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3123 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3124 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3125 #define SIGNALYZER_COMMAND_I2C 0x58
3127 #define SIGNALYZER_CHAN_A 1
3128 #define SIGNALYZER_CHAN_B 2
3129 /* LEDS use channel C */
3130 #define SIGNALYZER_CHAN_C 4
3132 #define SIGNALYZER_LED_GREEN 1
3133 #define SIGNALYZER_LED_RED 2
3135 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3136 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3137 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3138 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3139 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3142 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3144 #if BUILD_FT2232_FTD2XX == 1
3145 return FT_WriteEE(ftdih
, address
, value
);
3146 #elif BUILD_FT2232_LIBFTDI == 1
3151 #if BUILD_FT2232_FTD2XX == 1
3152 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3154 return FT_ReadEE(ftdih
, address
, value
);
3158 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3159 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3161 unsigned char on_time
;
3162 unsigned char off_time
;
3164 if (on_time_ms
< 0xFFFF)
3165 on_time
= (unsigned char)(on_time_ms
/ 62);
3169 off_time
= (unsigned char)(off_time_ms
/ 62);
3171 #if BUILD_FT2232_FTD2XX == 1
3174 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3175 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3177 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3178 return ERROR_JTAG_DEVICE_ERROR
;
3181 if ((status
= signalyzer_h_ctrl_write(
3182 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3183 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3185 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3186 return ERROR_JTAG_DEVICE_ERROR
;
3189 if ((status
= signalyzer_h_ctrl_write(
3190 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3191 ((uint32_t)cycles
))) != FT_OK
)
3193 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3194 return ERROR_JTAG_DEVICE_ERROR
;
3197 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3198 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3200 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3201 return ERROR_JTAG_DEVICE_ERROR
;
3205 #elif BUILD_FT2232_LIBFTDI == 1
3208 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3209 ((uint32_t)(channel
<< 8) | led
))) < 0)
3211 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3212 ftdi_get_error_string(&ftdic
));
3213 return ERROR_JTAG_DEVICE_ERROR
;
3216 if ((retval
= signalyzer_h_ctrl_write(
3217 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3218 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3220 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3221 ftdi_get_error_string(&ftdic
));
3222 return ERROR_JTAG_DEVICE_ERROR
;
3225 if ((retval
= signalyzer_h_ctrl_write(
3226 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3227 (uint32_t)cycles
)) < 0)
3229 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3230 ftdi_get_error_string(&ftdic
));
3231 return ERROR_JTAG_DEVICE_ERROR
;
3234 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3235 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3237 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3238 ftdi_get_error_string(&ftdic
));
3239 return ERROR_JTAG_DEVICE_ERROR
;
3246 static int signalyzer_h_init(void)
3248 #if BUILD_FT2232_FTD2XX == 1
3255 uint16_t read_buf
[12] = { 0 };
3257 uint32_t bytes_written
;
3259 /* turn on center green led */
3260 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3261 0xFFFF, 0x00, 0x00);
3263 /* determine what channel config wants to open
3264 * TODO: change me... current implementation is made to work
3265 * with openocd description parsing.
3267 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3271 signalyzer_h_side
= *(end_of_desc
- 1);
3272 if (signalyzer_h_side
== 'B')
3273 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3275 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3279 LOG_ERROR("No Channel was specified");
3283 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3286 #if BUILD_FT2232_FTD2XX == 1
3287 /* read signalyzer versionining information */
3288 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3289 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3291 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3292 return ERROR_JTAG_DEVICE_ERROR
;
3295 for (i
= 0; i
< 10; i
++)
3297 if ((status
= signalyzer_h_ctrl_read(
3298 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3299 &read_buf
[i
])) != FT_OK
)
3301 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3303 return ERROR_JTAG_DEVICE_ERROR
;
3307 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3308 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3309 read_buf
[4], read_buf
[5], read_buf
[6]);
3311 /* set gpio register */
3312 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3313 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3315 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3316 return ERROR_JTAG_DEVICE_ERROR
;
3319 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3322 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3323 return ERROR_JTAG_DEVICE_ERROR
;
3326 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3327 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3329 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3330 return ERROR_JTAG_DEVICE_ERROR
;
3333 /* read adapter type information */
3334 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3335 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3337 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3338 return ERROR_JTAG_DEVICE_ERROR
;
3341 if ((status
= signalyzer_h_ctrl_write(
3342 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3344 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3345 return ERROR_JTAG_DEVICE_ERROR
;
3348 if ((status
= signalyzer_h_ctrl_write(
3349 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3351 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3352 return ERROR_JTAG_DEVICE_ERROR
;
3355 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3356 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3358 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3359 return ERROR_JTAG_DEVICE_ERROR
;
3364 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3365 &read_buf
[0])) != FT_OK
)
3367 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3368 return ERROR_JTAG_DEVICE_ERROR
;
3371 if (read_buf
[0] != 0x0498)
3372 signalyzer_h_adapter_type
= 0x0000;
3375 for (i
= 0; i
< 4; i
++)
3377 if ((status
= signalyzer_h_ctrl_read(
3378 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3379 &read_buf
[i
])) != FT_OK
)
3381 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3383 return ERROR_JTAG_DEVICE_ERROR
;
3387 signalyzer_h_adapter_type
= read_buf
[0];
3390 #elif BUILD_FT2232_LIBFTDI == 1
3391 /* currently libftdi does not allow reading individual eeprom
3392 * locations, therefore adapter type cannot be detected.
3393 * override with most common type
3395 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3398 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3400 /* ADAPTOR: EM_LT16_A */
3401 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3403 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3404 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3412 low_direction
= 0x1b;
3415 high_direction
= 0x0;
3417 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3419 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3420 low_output
&= ~nTRST
; /* nTRST = 0 */
3424 low_direction
|= nTRSTnOE
; /* nTRST output */
3425 low_output
|= nTRST
; /* nTRST = 1 */
3428 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3430 low_direction
|= nSRSTnOE
; /* nSRST output */
3431 low_output
|= nSRST
; /* nSRST = 1 */
3435 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3436 low_output
&= ~nSRST
; /* nSRST = 0 */
3439 #if BUILD_FT2232_FTD2XX == 1
3440 /* enable power to the module */
3441 if ((status
= signalyzer_h_ctrl_write(
3442 SIGNALYZER_DATA_BUFFER_ADDR
,
3443 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3446 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3448 return ERROR_JTAG_DEVICE_ERROR
;
3451 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3452 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3454 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3456 return ERROR_JTAG_DEVICE_ERROR
;
3459 /* set gpio mode register */
3460 if ((status
= signalyzer_h_ctrl_write(
3461 SIGNALYZER_DATA_BUFFER_ADDR
,
3462 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3464 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3466 return ERROR_JTAG_DEVICE_ERROR
;
3469 if ((status
= signalyzer_h_ctrl_write(
3470 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3473 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3475 return ERROR_JTAG_DEVICE_ERROR
;
3478 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3479 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3481 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3483 return ERROR_JTAG_DEVICE_ERROR
;
3486 /* set gpio register */
3487 if ((status
= signalyzer_h_ctrl_write(
3488 SIGNALYZER_DATA_BUFFER_ADDR
,
3489 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3491 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3493 return ERROR_JTAG_DEVICE_ERROR
;
3496 if ((status
= signalyzer_h_ctrl_write(
3497 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3500 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3502 return ERROR_JTAG_DEVICE_ERROR
;
3505 if ((status
= signalyzer_h_ctrl_write(
3506 SIGNALYZER_COMMAND_ADDR
,
3507 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3509 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3511 return ERROR_JTAG_DEVICE_ERROR
;
3516 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3517 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3518 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3519 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3520 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3522 if (signalyzer_h_adapter_type
3523 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3524 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3525 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3526 else if (signalyzer_h_adapter_type
3527 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3528 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3529 "(ARM JTAG with PSU) detected. (HW: %2x).",
3530 (read_buf
[1] >> 8));
3531 else if (signalyzer_h_adapter_type
3532 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3533 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3534 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3535 else if (signalyzer_h_adapter_type
3536 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3537 LOG_INFO("Signalyzer: EM-JTAG-P "
3538 "(Generic JTAG with PSU) detected. (HW: %2x).",
3539 (read_buf
[1] >> 8));
3547 low_direction
= 0x1b;
3550 high_direction
= 0x1f;
3552 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3554 high_output
|= nTRSTnOE
;
3555 high_output
&= ~nTRST
;
3559 high_output
&= ~nTRSTnOE
;
3560 high_output
|= nTRST
;
3563 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3565 high_output
&= ~nSRSTnOE
;
3566 high_output
|= nSRST
;
3570 high_output
|= nSRSTnOE
;
3571 high_output
&= ~nSRST
;
3574 #if BUILD_FT2232_FTD2XX == 1
3575 /* enable power to the module */
3576 if ((status
= signalyzer_h_ctrl_write(
3577 SIGNALYZER_DATA_BUFFER_ADDR
,
3578 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3581 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3583 return ERROR_JTAG_DEVICE_ERROR
;
3586 if ((status
= signalyzer_h_ctrl_write(
3587 SIGNALYZER_COMMAND_ADDR
,
3588 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3590 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3592 return ERROR_JTAG_DEVICE_ERROR
;
3595 /* set gpio mode register (IO_16 and IO_17 set as analog
3596 * inputs, other is gpio)
3598 if ((status
= signalyzer_h_ctrl_write(
3599 SIGNALYZER_DATA_BUFFER_ADDR
,
3600 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3602 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3604 return ERROR_JTAG_DEVICE_ERROR
;
3607 if ((status
= signalyzer_h_ctrl_write(
3608 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3611 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3613 return ERROR_JTAG_DEVICE_ERROR
;
3616 if ((status
= signalyzer_h_ctrl_write(
3617 SIGNALYZER_COMMAND_ADDR
,
3618 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3620 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3622 return ERROR_JTAG_DEVICE_ERROR
;
3625 /* set gpio register (all inputs, for -P modules,
3626 * PSU will be turned off)
3628 if ((status
= signalyzer_h_ctrl_write(
3629 SIGNALYZER_DATA_BUFFER_ADDR
,
3630 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3632 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3634 return ERROR_JTAG_DEVICE_ERROR
;
3637 if ((status
= signalyzer_h_ctrl_write(
3638 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3641 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3643 return ERROR_JTAG_DEVICE_ERROR
;
3646 if ((status
= signalyzer_h_ctrl_write(
3647 SIGNALYZER_COMMAND_ADDR
,
3648 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3650 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3652 return ERROR_JTAG_DEVICE_ERROR
;
3657 else if (signalyzer_h_adapter_type
== 0x0000)
3659 LOG_INFO("Signalyzer: No external modules were detected.");
3667 low_direction
= 0x1b;
3670 high_direction
= 0x0;
3672 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3674 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3675 low_output
&= ~nTRST
; /* nTRST = 0 */
3679 low_direction
|= nTRSTnOE
; /* nTRST output */
3680 low_output
|= nTRST
; /* nTRST = 1 */
3683 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3685 low_direction
|= nSRSTnOE
; /* nSRST output */
3686 low_output
|= nSRST
; /* nSRST = 1 */
3690 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3691 low_output
&= ~nSRST
; /* nSRST = 0 */
3696 LOG_ERROR("Unknown module type is detected: %.4x",
3697 signalyzer_h_adapter_type
);
3698 return ERROR_JTAG_DEVICE_ERROR
;
3701 /* initialize low byte of controller for jtag operation */
3703 buf
[1] = low_output
;
3704 buf
[2] = low_direction
;
3706 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3707 || (bytes_written
!= 3))
3709 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3710 return ERROR_JTAG_INIT_FAILED
;
3713 #if BUILD_FT2232_FTD2XX == 1
3714 if (ftdi_device
== FT_DEVICE_2232H
)
3716 /* initialize high byte of controller for jtag operation */
3718 buf
[1] = high_output
;
3719 buf
[2] = high_direction
;
3721 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3722 || (bytes_written
!= 3))
3724 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3725 return ERROR_JTAG_INIT_FAILED
;
3728 #elif BUILD_FT2232_LIBFTDI == 1
3729 if (ftdi_device
== TYPE_2232H
)
3731 /* initialize high byte of controller for jtag operation */
3733 buf
[1] = high_output
;
3734 buf
[2] = high_direction
;
3736 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3737 || (bytes_written
!= 3))
3739 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3740 return ERROR_JTAG_INIT_FAILED
;
3747 static void signalyzer_h_reset(int trst
, int srst
)
3749 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3751 /* ADAPTOR: EM_LT16_A */
3752 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3756 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3757 /* switch to output pin (output is low) */
3758 low_direction
|= nTRSTnOE
;
3760 /* switch output low */
3761 low_output
&= ~nTRST
;
3765 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3766 /* switch to input pin (high-Z + internal
3767 * and external pullup) */
3768 low_direction
&= ~nTRSTnOE
;
3770 /* switch output high */
3771 low_output
|= nTRST
;
3776 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3777 /* switch output low */
3778 low_output
&= ~nSRST
;
3780 /* switch to output pin (output is low) */
3781 low_direction
|= nSRSTnOE
;
3785 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3786 /* switch output high */
3787 low_output
|= nSRST
;
3789 /* switch to input pin (high-Z) */
3790 low_direction
&= ~nSRSTnOE
;
3793 /* command "set data bits low byte" */
3795 buffer_write(low_output
);
3796 buffer_write(low_direction
);
3797 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3798 "low_direction: 0x%2.2x",
3799 trst
, srst
, low_output
, low_direction
);
3801 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3802 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3803 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3804 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3805 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3809 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3810 high_output
&= ~nTRSTnOE
;
3812 high_output
&= ~nTRST
;
3816 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3817 high_output
|= nTRSTnOE
;
3819 high_output
|= nTRST
;
3824 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3825 high_output
&= ~nSRST
;
3827 high_output
&= ~nSRSTnOE
;
3831 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3832 high_output
|= nSRST
;
3834 high_output
|= nSRSTnOE
;
3837 /* command "set data bits high byte" */
3839 buffer_write(high_output
);
3840 buffer_write(high_direction
);
3841 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
3842 "high_direction: 0x%2.2x",
3843 trst
, srst
, high_output
, high_direction
);
3845 else if (signalyzer_h_adapter_type
== 0x0000)
3849 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3850 /* switch to output pin (output is low) */
3851 low_direction
|= nTRSTnOE
;
3853 /* switch output low */
3854 low_output
&= ~nTRST
;
3858 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3859 /* switch to input pin (high-Z + internal
3860 * and external pullup) */
3861 low_direction
&= ~nTRSTnOE
;
3863 /* switch output high */
3864 low_output
|= nTRST
;
3869 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3870 /* switch output low */
3871 low_output
&= ~nSRST
;
3873 /* switch to output pin (output is low) */
3874 low_direction
|= nSRSTnOE
;
3878 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3879 /* switch output high */
3880 low_output
|= nSRST
;
3882 /* switch to input pin (high-Z) */
3883 low_direction
&= ~nSRSTnOE
;
3886 /* command "set data bits low byte" */
3888 buffer_write(low_output
);
3889 buffer_write(low_direction
);
3890 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3891 "low_direction: 0x%2.2x",
3892 trst
, srst
, low_output
, low_direction
);
3896 static void signalyzer_h_blink(void)
3898 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
3901 /********************************************************************
3902 * Support for KT-LINK
3903 * JTAG adapter from KRISTECH
3904 * http://www.kristech.eu
3905 *******************************************************************/
3906 static int ktlink_init(void)
3909 uint32_t bytes_written
;
3910 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
3912 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
3913 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
3915 // initialize low port
3916 buf
[0] = 0x80; // command "set data bits low byte"
3917 buf
[1] = low_output
;
3918 buf
[2] = low_direction
;
3919 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3921 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3923 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3924 return ERROR_JTAG_INIT_FAILED
;
3932 high_output
= 0x80; // turn LED on
3933 high_direction
= 0xFF; // all outputs
3935 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3937 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3938 high_output
|= nTRSTnOE
;
3939 high_output
&= ~nTRST
;
3941 high_output
&= ~nTRSTnOE
;
3942 high_output
|= nTRST
;
3945 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3946 high_output
&= ~nSRSTnOE
;
3947 high_output
|= nSRST
;
3949 high_output
|= nSRSTnOE
;
3950 high_output
&= ~nSRST
;
3953 // initialize high port
3954 buf
[0] = 0x82; // command "set data bits high byte"
3955 buf
[1] = high_output
; // value
3956 buf
[2] = high_direction
;
3957 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3959 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3961 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3962 return ERROR_JTAG_INIT_FAILED
;
3968 static void ktlink_reset(int trst
, int srst
)
3970 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3973 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3974 high_output
&= ~nTRSTnOE
;
3976 high_output
&= ~nTRST
;
3977 } else if (trst
== 0) {
3978 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3979 high_output
|= nTRSTnOE
;
3981 high_output
|= nTRST
;
3985 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3986 high_output
&= ~nSRST
;
3988 high_output
&= ~nSRSTnOE
;
3989 } else if (srst
== 0) {
3990 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3991 high_output
|= nSRST
;
3993 high_output
|= nSRSTnOE
;
3996 buffer_write(0x82); // command "set data bits high byte"
3997 buffer_write(high_output
);
3998 buffer_write(high_direction
);
3999 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
4002 static void ktlink_blink(void)
4004 /* LED connected to ACBUS7 */
4005 if (high_output
& 0x80)
4006 high_output
&= 0x7F;
4008 high_output
|= 0x80;
4010 buffer_write(0x82); // command "set data bits high byte"
4011 buffer_write(high_output
);
4012 buffer_write(high_direction
);