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 };
94 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
95 * stable state. Calling code must ensure that current state is stable,
96 * that verification is not done in here.
98 * @param num_cycles The number of clocks cycles to send.
99 * @param cmd The command to send.
101 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
103 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
);
105 static char * ft2232_device_desc_A
= NULL
;
106 static char* ft2232_device_desc
= NULL
;
107 static char* ft2232_serial
= NULL
;
108 static char* ft2232_layout
= NULL
;
109 static uint8_t ft2232_latency
= 2;
110 static unsigned ft2232_max_tck
= FTDI_2232C_MAX_TCK
;
112 #define MAX_USB_IDS 8
113 /* vid = pid = 0 marks the end of the list */
114 static uint16_t ft2232_vid
[MAX_USB_IDS
+ 1] = { 0x0403, 0 };
115 static uint16_t ft2232_pid
[MAX_USB_IDS
+ 1] = { 0x6010, 0 };
117 struct ft2232_layout
{
120 void (*reset
)(int trst
, int srst
);
124 /* init procedures for supported layouts */
125 static int usbjtag_init(void);
126 static int jtagkey_init(void);
127 static int olimex_jtag_init(void);
128 static int flyswatter_init(void);
129 static int turtle_init(void);
130 static int comstick_init(void);
131 static int stm32stick_init(void);
132 static int axm0432_jtag_init(void);
133 static int sheevaplug_init(void);
134 static int icebear_jtag_init(void);
135 static int cortino_jtag_init(void);
136 static int signalyzer_h_init(void);
137 static int ktlink_init(void);
139 /* reset procedures for supported layouts */
140 static void usbjtag_reset(int trst
, int srst
);
141 static void jtagkey_reset(int trst
, int srst
);
142 static void olimex_jtag_reset(int trst
, int srst
);
143 static void flyswatter_reset(int trst
, int srst
);
144 static void turtle_reset(int trst
, int srst
);
145 static void comstick_reset(int trst
, int srst
);
146 static void stm32stick_reset(int trst
, int srst
);
147 static void axm0432_jtag_reset(int trst
, int srst
);
148 static void sheevaplug_reset(int trst
, int srst
);
149 static void icebear_jtag_reset(int trst
, int srst
);
150 static void signalyzer_h_reset(int trst
, int srst
);
151 static void ktlink_reset(int trst
, int srst
);
153 /* blink procedures for layouts that support a blinking led */
154 static void olimex_jtag_blink(void);
155 static void flyswatter_jtag_blink(void);
156 static void turtle_jtag_blink(void);
157 static void signalyzer_h_blink(void);
158 static void ktlink_blink(void);
160 static const struct ft2232_layout ft2232_layouts
[] =
162 { "usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
163 { "jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
164 { "jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
165 { "oocdlink", jtagkey_init
, jtagkey_reset
, NULL
},
166 { "signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
167 { "evb_lm3s811", usbjtag_init
, usbjtag_reset
, NULL
},
168 { "luminary_icdi", usbjtag_init
, usbjtag_reset
, NULL
},
169 { "olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
170 { "flyswatter", flyswatter_init
, flyswatter_reset
, flyswatter_jtag_blink
},
171 { "turtelizer2", turtle_init
, turtle_reset
, turtle_jtag_blink
},
172 { "comstick", comstick_init
, comstick_reset
, NULL
},
173 { "stm32stick", stm32stick_init
, stm32stick_reset
, NULL
},
174 { "axm0432_jtag", axm0432_jtag_init
, axm0432_jtag_reset
, NULL
},
175 { "sheevaplug", sheevaplug_init
, sheevaplug_reset
, NULL
},
176 { "icebear", icebear_jtag_init
, icebear_jtag_reset
, NULL
},
177 { "cortino", cortino_jtag_init
, comstick_reset
, NULL
},
178 { "signalyzer-h", signalyzer_h_init
, signalyzer_h_reset
, signalyzer_h_blink
},
179 { "ktlink", ktlink_init
, ktlink_reset
, ktlink_blink
},
180 { NULL
, NULL
, NULL
, NULL
},
183 static uint8_t nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
185 static const struct ft2232_layout
*layout
;
186 static uint8_t low_output
= 0x0;
187 static uint8_t low_direction
= 0x0;
188 static uint8_t high_output
= 0x0;
189 static uint8_t high_direction
= 0x0;
191 #if BUILD_FT2232_FTD2XX == 1
192 static FT_HANDLE ftdih
= NULL
;
193 static FT_DEVICE ftdi_device
= 0;
194 #elif BUILD_FT2232_LIBFTDI == 1
195 static struct ftdi_context ftdic
;
196 static enum ftdi_chip_type ftdi_device
;
199 static jtag_command_t
* first_unsent
; /* next command that has to be sent */
200 static int require_send
;
202 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
204 "There is a significant difference between libftdi and libftd2xx. The latter
205 one allows to schedule up to 64*64 bytes of result data while libftdi fails
206 with more than 4*64. As a consequence, the FT2232 driver is forced to
207 perform around 16x more USB transactions for long command streams with TDO
208 capture when running with libftdi."
211 #define FT2232_BUFFER_SIZE 131072
212 a comment would have been nice.
215 #define FT2232_BUFFER_SIZE 131072
217 static uint8_t* ft2232_buffer
= NULL
;
218 static int ft2232_buffer_size
= 0;
219 static int ft2232_read_pointer
= 0;
220 static int ft2232_expect_read
= 0;
223 * Function buffer_write
224 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
225 * @param val is the byte to send.
227 static inline void buffer_write(uint8_t val
)
229 assert(ft2232_buffer
);
230 assert((unsigned) ft2232_buffer_size
< (unsigned) FT2232_BUFFER_SIZE
);
231 ft2232_buffer
[ft2232_buffer_size
++] = val
;
235 * Function buffer_read
236 * returns a byte from the byte buffer.
238 static inline uint8_t buffer_read(void)
240 assert(ft2232_buffer
);
241 assert(ft2232_read_pointer
< ft2232_buffer_size
);
242 return ft2232_buffer
[ft2232_read_pointer
++];
246 * Clocks out \a bit_count bits on the TMS line, starting with the least
247 * significant bit of tms_bits and progressing to more significant bits.
248 * Rigorous state transition logging is done here via tap_set_state().
250 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
251 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
252 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
253 * is often used for this, 0x4b.
255 * @param tms_bits Holds the sequence of bits to send.
256 * @param tms_count Tells how many bits in the sequence.
257 * @param tdi_bit A single bit to pass on to TDI before the first TCK
258 * cycle and held static for the duration of TMS clocking.
260 * See the MPSSE spec referenced above.
262 static void clock_tms(uint8_t mpsse_cmd
, int tms_bits
, int tms_count
, bool tdi_bit
)
266 int tms_ndx
; /* bit index into tms_byte */
268 assert(tms_count
> 0);
270 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
271 mpsse_cmd
, tms_bits
, tms_count
);
273 for (tms_byte
= tms_ndx
= i
= 0; i
< tms_count
; ++i
, tms_bits
>>=1)
275 bool bit
= tms_bits
& 1;
278 tms_byte
|= (1 << tms_ndx
);
280 /* always do state transitions in public view */
281 tap_set_state(tap_state_transition(tap_get_state(), bit
));
283 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
288 if (tms_ndx
== 7 || i
== tms_count
-1)
290 buffer_write(mpsse_cmd
);
291 buffer_write(tms_ndx
- 1);
293 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
294 TMS/CS and is held static for the duration of TMS/CS clocking.
296 buffer_write(tms_byte
| (tdi_bit
<< 7));
302 * Function get_tms_buffer_requirements
303 * returns what clock_tms() will consume if called with
306 static inline int get_tms_buffer_requirements(int bit_count
)
308 return ((bit_count
+ 6)/7) * 3;
312 * Function move_to_state
313 * moves the TAP controller from the current state to a
314 * \a goal_state through a path given by tap_get_tms_path(). State transition
315 * logging is performed by delegation to clock_tms().
317 * @param goal_state is the destination state for the move.
319 static void move_to_state(tap_state_t goal_state
)
321 tap_state_t start_state
= tap_get_state();
323 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
324 lookup of the required TMS pattern to move to this state from the
328 /* do the 2 lookups */
329 int tms_bits
= tap_get_tms_path(start_state
, goal_state
);
330 int tms_count
= tap_get_tms_path_len(start_state
, goal_state
);
332 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state
), tap_state_name(goal_state
));
334 clock_tms(0x4b, tms_bits
, tms_count
, 0);
337 static int ft2232_write(uint8_t* buf
, int size
, uint32_t* bytes_written
)
339 #if BUILD_FT2232_FTD2XX == 1
341 DWORD dw_bytes_written
;
342 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
344 *bytes_written
= dw_bytes_written
;
345 LOG_ERROR("FT_Write returned: %lu", status
);
346 return ERROR_JTAG_DEVICE_ERROR
;
350 *bytes_written
= dw_bytes_written
;
353 #elif BUILD_FT2232_LIBFTDI == 1
355 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
358 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
359 return ERROR_JTAG_DEVICE_ERROR
;
363 *bytes_written
= retval
;
369 static int ft2232_read(uint8_t* buf
, uint32_t size
, uint32_t* bytes_read
)
371 #if BUILD_FT2232_FTD2XX == 1
377 while ((*bytes_read
< size
) && timeout
--)
379 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
380 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
383 LOG_ERROR("FT_Read returned: %lu", status
);
384 return ERROR_JTAG_DEVICE_ERROR
;
386 *bytes_read
+= dw_bytes_read
;
389 #elif BUILD_FT2232_LIBFTDI == 1
391 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
394 while ((*bytes_read
< size
) && timeout
--)
396 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
399 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
400 return ERROR_JTAG_DEVICE_ERROR
;
402 *bytes_read
+= retval
;
407 if (*bytes_read
< size
)
409 LOG_ERROR("couldn't read enough bytes from "
410 "FT2232 device (%i < %i)",
411 (unsigned)*bytes_read
,
413 return ERROR_JTAG_DEVICE_ERROR
;
419 static bool ft2232_device_is_highspeed(void)
421 #if BUILD_FT2232_FTD2XX == 1
422 return (ftdi_device
== FT_DEVICE_2232H
) || (ftdi_device
== FT_DEVICE_4232H
);
423 #elif BUILD_FT2232_LIBFTDI == 1
424 return (ftdi_device
== TYPE_2232H
|| ftdi_device
== TYPE_4232H
);
429 * Commands that only apply to the FT2232H and FT4232H devices.
430 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
431 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
434 static int ft2232h_ft4232h_adaptive_clocking(bool enable
)
436 uint8_t buf
= enable
? 0x96 : 0x97;
437 LOG_DEBUG("%2.2x", buf
);
439 uint32_t bytes_written
;
440 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
441 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
443 LOG_ERROR("couldn't write command to %s adaptive clocking"
444 , enable
? "enable" : "disable");
452 * Enable/disable the clk divide by 5 of the 60MHz master clock.
453 * This result in a JTAG clock speed range of 91.553Hz-6MHz
454 * respective 457.763Hz-30MHz.
456 static int ft2232h_ft4232h_clk_divide_by_5(bool enable
)
458 uint32_t bytes_written
;
459 uint8_t buf
= enable
? 0x8b : 0x8a;
460 int retval
= ft2232_write(&buf
, 1, &bytes_written
);
461 if ((ERROR_OK
!= retval
) || (bytes_written
!= 1))
463 LOG_ERROR("couldn't write command to %s clk divide by 5"
464 , enable
? "enable" : "disable");
465 return ERROR_JTAG_INIT_FAILED
;
467 ft2232_max_tck
= enable
? FTDI_2232C_MAX_TCK
: FTDI_2232H_4232H_MAX_TCK
;
468 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck
);
473 static int ft2232_speed(int speed
)
477 uint32_t bytes_written
;
480 bool enable_adaptive_clocking
= (RTCK_SPEED
== speed
);
481 if (ft2232_device_is_highspeed())
482 retval
= ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking
);
483 else if (enable_adaptive_clocking
)
485 LOG_ERROR("ft2232 device %lu does not support RTCK"
486 , (long unsigned int)ftdi_device
);
490 if ((enable_adaptive_clocking
) || (ERROR_OK
!= retval
))
493 buf
[0] = 0x86; /* command "set divisor" */
494 buf
[1] = speed
& 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
495 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
497 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
498 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
500 LOG_ERROR("couldn't set FT2232 TCK speed");
507 static int ft2232_speed_div(int speed
, int* khz
)
509 /* Take a look in the FT2232 manual,
510 * AN2232C-01 Command Processor for
511 * MPSSE and MCU Host Bus. Chapter 3.8 */
513 *khz
= (RTCK_SPEED
== speed
) ? 0 : ft2232_max_tck
/ (1 + speed
);
518 static int ft2232_khz(int khz
, int* jtag_speed
)
522 if (ft2232_device_is_highspeed())
524 *jtag_speed
= RTCK_SPEED
;
529 LOG_DEBUG("RCLK not supported");
534 /* Take a look in the FT2232 manual,
535 * AN2232C-01 Command Processor for
536 * MPSSE and MCU Host Bus. Chapter 3.8
538 * We will calc here with a multiplier
539 * of 10 for better rounding later. */
541 /* Calc speed, (ft2232_max_tck / khz) - 1 */
542 /* Use 65000 for better rounding */
543 *jtag_speed
= ((ft2232_max_tck
*10) / khz
) - 10;
545 /* Add 0.9 for rounding */
548 /* Calc real speed */
549 *jtag_speed
= *jtag_speed
/ 10;
551 /* Check if speed is greater than 0 */
557 /* Check max value */
558 if (*jtag_speed
> 0xFFFF)
560 *jtag_speed
= 0xFFFF;
566 static void ft2232_end_state(tap_state_t state
)
568 if (tap_is_state_stable(state
))
569 tap_set_end_state(state
);
572 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state
));
577 static void ft2232_read_scan(enum scan_type type
, uint8_t* buffer
, int scan_size
)
579 int num_bytes
= (scan_size
+ 7) / 8;
580 int bits_left
= scan_size
;
583 while (num_bytes
-- > 1)
585 buffer
[cur_byte
++] = buffer_read();
589 buffer
[cur_byte
] = 0x0;
591 /* There is one more partial byte left from the clock data in/out instructions */
594 buffer
[cur_byte
] = buffer_read() >> 1;
596 /* 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 */
597 buffer
[cur_byte
] = (buffer
[cur_byte
] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left
);
600 static void ft2232_debug_dump_buffer(void)
606 for (i
= 0; i
< ft2232_buffer_size
; i
++)
608 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
611 LOG_DEBUG("%s", line
);
617 LOG_DEBUG("%s", line
);
620 static int ft2232_send_and_recv(jtag_command_t
* first
, jtag_command_t
* last
)
627 uint32_t bytes_written
= 0;
628 uint32_t bytes_read
= 0;
630 #ifdef _DEBUG_USB_IO_
631 struct timeval start
, inter
, inter2
, end
;
632 struct timeval d_inter
, d_inter2
, d_end
;
635 #ifdef _DEBUG_USB_COMMS_
636 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
637 ft2232_debug_dump_buffer();
640 #ifdef _DEBUG_USB_IO_
641 gettimeofday(&start
, NULL
);
644 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
646 LOG_ERROR("couldn't write MPSSE commands to FT2232");
650 #ifdef _DEBUG_USB_IO_
651 gettimeofday(&inter
, NULL
);
654 if (ft2232_expect_read
)
656 /* FIXME this "timeout" is never changed ... */
657 int timeout
= LIBFTDI_READ_RETRY_COUNT
;
658 ft2232_buffer_size
= 0;
660 #ifdef _DEBUG_USB_IO_
661 gettimeofday(&inter2
, NULL
);
664 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
666 LOG_ERROR("couldn't read from FT2232");
670 #ifdef _DEBUG_USB_IO_
671 gettimeofday(&end
, NULL
);
673 timeval_subtract(&d_inter
, &inter
, &start
);
674 timeval_subtract(&d_inter2
, &inter2
, &start
);
675 timeval_subtract(&d_end
, &end
, &start
);
677 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
678 (unsigned)d_inter
.tv_sec
, (unsigned)d_inter
.tv_usec
,
679 (unsigned)d_inter2
.tv_sec
, (unsigned)d_inter2
.tv_usec
,
680 (unsigned)d_end
.tv_sec
, (unsigned)d_end
.tv_usec
);
683 ft2232_buffer_size
= bytes_read
;
685 if (ft2232_expect_read
!= ft2232_buffer_size
)
687 LOG_ERROR("ft2232_expect_read (%i) != "
688 "ft2232_buffer_size (%i) "
692 LIBFTDI_READ_RETRY_COUNT
- timeout
);
693 ft2232_debug_dump_buffer();
698 #ifdef _DEBUG_USB_COMMS_
699 LOG_DEBUG("read buffer (%i retries): %i bytes",
700 LIBFTDI_READ_RETRY_COUNT
- timeout
,
702 ft2232_debug_dump_buffer();
706 ft2232_expect_read
= 0;
707 ft2232_read_pointer
= 0;
709 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
710 * that wasn't handled by a caller-provided error handler
720 type
= jtag_scan_type(cmd
->cmd
.scan
);
721 if (type
!= SCAN_OUT
)
723 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
724 buffer
= calloc(CEIL(scan_size
, 8), 1);
725 ft2232_read_scan(type
, buffer
, scan_size
);
726 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
727 retval
= ERROR_JTAG_QUEUE_FAILED
;
739 ft2232_buffer_size
= 0;
745 * Function ft2232_add_pathmove
746 * moves the TAP controller from the current state to a new state through the
747 * given path, where path is an array of tap_state_t's.
749 * @param path is an array of tap_stat_t which gives the states to traverse through
750 * ending with the last state at path[num_states-1]
751 * @param num_states is the count of state steps to move through
753 static void ft2232_add_pathmove(tap_state_t
* path
, int num_states
)
757 assert((unsigned) num_states
<= 32u); /* tms_bits only holds 32 bits */
761 /* this loop verifies that the path is legal and logs each state in the path */
764 unsigned char tms_byte
= 0; /* zero this on each MPSSE batch */
766 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
768 /* command "Clock Data to TMS/CS Pin (no Read)" */
771 /* number of states remaining */
772 buffer_write(num_states_batch
- 1);
774 while (num_states_batch
--) {
775 /* either TMS=0 or TMS=1 must work ... */
776 if (tap_state_transition(tap_get_state(), false)
777 == path
[state_count
])
778 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
779 else if (tap_state_transition(tap_get_state(), true)
780 == path
[state_count
])
781 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
783 /* ... or else the caller goofed BADLY */
785 LOG_ERROR("BUG: %s -> %s isn't a valid "
786 "TAP state transition",
787 tap_state_name(tap_get_state()),
788 tap_state_name(path
[state_count
]));
792 tap_set_state(path
[state_count
]);
797 buffer_write(tms_byte
);
799 tap_set_end_state(tap_get_state());
802 static void ft2232_add_scan(bool ir_scan
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
804 int num_bytes
= (scan_size
+ 7) / 8;
805 int bits_left
= scan_size
;
811 if (tap_get_state() != TAP_DRSHIFT
)
813 move_to_state(TAP_DRSHIFT
);
818 if (tap_get_state() != TAP_IRSHIFT
)
820 move_to_state(TAP_IRSHIFT
);
824 /* add command for complete bytes */
825 while (num_bytes
> 1)
830 /* Clock Data Bytes In and Out LSB First */
832 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
834 else if (type
== SCAN_OUT
)
836 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
838 /* LOG_DEBUG("added TDI bytes (o)"); */
840 else if (type
== SCAN_IN
)
842 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
844 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
847 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
848 num_bytes
-= thisrun_bytes
;
850 buffer_write((uint8_t) (thisrun_bytes
- 1));
851 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
855 /* add complete bytes */
856 while (thisrun_bytes
-- > 0)
858 buffer_write(buffer
[cur_byte
++]);
862 else /* (type == SCAN_IN) */
864 bits_left
-= 8 * (thisrun_bytes
);
868 /* the most signifcant bit is scanned during TAP movement */
870 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
874 /* process remaining bits but the last one */
879 /* Clock Data Bits In and Out LSB First */
881 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
883 else if (type
== SCAN_OUT
)
885 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
887 /* LOG_DEBUG("added TDI bits (o)"); */
889 else if (type
== SCAN_IN
)
891 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
893 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
896 buffer_write(bits_left
- 2);
898 buffer_write(buffer
[cur_byte
]);
901 if ((ir_scan
&& (tap_get_end_state() == TAP_IRSHIFT
))
902 || (!ir_scan
&& (tap_get_end_state() == TAP_DRSHIFT
)))
906 /* Clock Data Bits In and Out LSB First */
908 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
910 else if (type
== SCAN_OUT
)
912 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
914 /* LOG_DEBUG("added TDI bits (o)"); */
916 else if (type
== SCAN_IN
)
918 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
920 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
923 buffer_write(last_bit
);
931 /* move from Shift-IR/DR to end state */
932 if (type
!= SCAN_OUT
)
934 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
935 /* This must be coordinated with the bit shifts in ft2232_read_scan */
938 /* Clock Data to TMS/CS Pin with Read */
943 tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
944 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
945 /* Clock Data to TMS/CS Pin (no Read) */
949 DEBUG_JTAG_IO("finish %s", (type
== SCAN_OUT
) ? "without read" : "via PAUSE");
950 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
953 if (tap_get_state() != tap_get_end_state())
955 move_to_state(tap_get_end_state());
959 static int ft2232_large_scan(scan_command_t
* cmd
, enum scan_type type
, uint8_t* buffer
, int scan_size
)
961 int num_bytes
= (scan_size
+ 7) / 8;
962 int bits_left
= scan_size
;
965 uint8_t* receive_buffer
= malloc(CEIL(scan_size
, 8));
966 uint8_t* receive_pointer
= receive_buffer
;
967 uint32_t bytes_written
;
970 int thisrun_read
= 0;
974 LOG_ERROR("BUG: large IR scans are not supported");
978 if (tap_get_state() != TAP_DRSHIFT
)
980 move_to_state(TAP_DRSHIFT
);
983 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
985 LOG_ERROR("couldn't write MPSSE commands to FT2232");
988 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
989 ft2232_buffer_size
, (int)bytes_written
);
990 ft2232_buffer_size
= 0;
992 /* add command for complete bytes */
993 while (num_bytes
> 1)
999 /* Clock Data Bytes In and Out LSB First */
1001 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1003 else if (type
== SCAN_OUT
)
1005 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1007 /* LOG_DEBUG("added TDI bytes (o)"); */
1009 else if (type
== SCAN_IN
)
1011 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1013 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1016 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
1017 thisrun_read
= thisrun_bytes
;
1018 num_bytes
-= thisrun_bytes
;
1019 buffer_write((uint8_t) (thisrun_bytes
- 1));
1020 buffer_write((uint8_t) ((thisrun_bytes
- 1) >> 8));
1022 if (type
!= SCAN_IN
)
1024 /* add complete bytes */
1025 while (thisrun_bytes
-- > 0)
1027 buffer_write(buffer
[cur_byte
]);
1032 else /* (type == SCAN_IN) */
1034 bits_left
-= 8 * (thisrun_bytes
);
1037 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1039 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1042 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1044 (int)bytes_written
);
1045 ft2232_buffer_size
= 0;
1047 if (type
!= SCAN_OUT
)
1049 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1051 LOG_ERROR("couldn't read from FT2232");
1054 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1057 receive_pointer
+= bytes_read
;
1063 /* the most signifcant bit is scanned during TAP movement */
1064 if (type
!= SCAN_IN
)
1065 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
1069 /* process remaining bits but the last one */
1072 if (type
== SCAN_IO
)
1074 /* Clock Data Bits In and Out LSB First */
1076 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1078 else if (type
== SCAN_OUT
)
1080 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1082 /* LOG_DEBUG("added TDI bits (o)"); */
1084 else if (type
== SCAN_IN
)
1086 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1088 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1090 buffer_write(bits_left
- 2);
1091 if (type
!= SCAN_IN
)
1092 buffer_write(buffer
[cur_byte
]);
1094 if (type
!= SCAN_OUT
)
1098 if (tap_get_end_state() == TAP_DRSHIFT
)
1100 if (type
== SCAN_IO
)
1102 /* Clock Data Bits In and Out LSB First */
1104 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1106 else if (type
== SCAN_OUT
)
1108 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1110 /* LOG_DEBUG("added TDI bits (o)"); */
1112 else if (type
== SCAN_IN
)
1114 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1116 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1119 buffer_write(last_bit
);
1123 int tms_bits
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1124 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1127 /* move from Shift-IR/DR to end state */
1128 if (type
!= SCAN_OUT
)
1130 /* Clock Data to TMS/CS Pin with Read */
1132 /* LOG_DEBUG("added TMS scan (read)"); */
1136 /* Clock Data to TMS/CS Pin (no Read) */
1138 /* LOG_DEBUG("added TMS scan (no read)"); */
1141 DEBUG_JTAG_IO("finish, %s", (type
== SCAN_OUT
) ? "no read" : "read");
1142 clock_tms(mpsse_cmd
, tms_bits
, tms_count
, last_bit
);
1145 if (type
!= SCAN_OUT
)
1148 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
1150 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1153 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1155 (int)bytes_written
);
1156 ft2232_buffer_size
= 0;
1158 if (type
!= SCAN_OUT
)
1160 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
1162 LOG_ERROR("couldn't read from FT2232");
1165 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1168 receive_pointer
+= bytes_read
;
1174 static int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
1176 int predicted_size
= 3;
1177 int num_bytes
= (scan_size
- 1) / 8;
1179 if (tap_get_state() != TAP_DRSHIFT
)
1180 predicted_size
+= get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT
));
1182 if (type
== SCAN_IN
) /* only from device to host */
1184 /* complete bytes */
1185 predicted_size
+= CEIL(num_bytes
, 65536) * 3;
1187 /* remaining bits - 1 (up to 7) */
1188 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
1190 else /* host to device, or bidirectional */
1192 /* complete bytes */
1193 predicted_size
+= num_bytes
+ CEIL(num_bytes
, 65536) * 3;
1195 /* remaining bits -1 (up to 7) */
1196 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
1199 return predicted_size
;
1202 static int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
1204 int predicted_size
= 0;
1206 if (type
!= SCAN_OUT
)
1208 /* complete bytes */
1209 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) - 1) : 0;
1211 /* remaining bits - 1 */
1212 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
1214 /* last bit (from TMS scan) */
1215 predicted_size
+= 1;
1218 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1220 return predicted_size
;
1223 static void usbjtag_reset(int trst
, int srst
)
1225 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1228 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1229 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
1231 low_output
&= ~nTRST
; /* switch output low */
1235 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1236 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
1238 low_output
|= nTRST
; /* switch output high */
1243 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1244 low_output
&= ~nSRST
; /* switch output low */
1246 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
1250 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1251 low_output
|= nSRST
; /* switch output high */
1253 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
1256 /* command "set data bits low byte" */
1258 buffer_write(low_output
);
1259 buffer_write(low_direction
);
1262 static void jtagkey_reset(int trst
, int srst
)
1264 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1267 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1268 high_output
&= ~nTRSTnOE
;
1270 high_output
&= ~nTRST
;
1274 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1275 high_output
|= nTRSTnOE
;
1277 high_output
|= nTRST
;
1282 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1283 high_output
&= ~nSRST
;
1285 high_output
&= ~nSRSTnOE
;
1289 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1290 high_output
|= nSRST
;
1292 high_output
|= nSRSTnOE
;
1295 /* command "set data bits high byte" */
1297 buffer_write(high_output
);
1298 buffer_write(high_direction
);
1299 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1303 static void olimex_jtag_reset(int trst
, int srst
)
1305 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1308 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1309 high_output
&= ~nTRSTnOE
;
1311 high_output
&= ~nTRST
;
1315 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1316 high_output
|= nTRSTnOE
;
1318 high_output
|= nTRST
;
1323 high_output
|= nSRST
;
1327 high_output
&= ~nSRST
;
1330 /* command "set data bits high byte" */
1332 buffer_write(high_output
);
1333 buffer_write(high_direction
);
1334 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1338 static void axm0432_jtag_reset(int trst
, int srst
)
1342 tap_set_state(TAP_RESET
);
1343 high_output
&= ~nTRST
;
1347 high_output
|= nTRST
;
1352 high_output
&= ~nSRST
;
1356 high_output
|= nSRST
;
1359 /* command "set data bits low byte" */
1361 buffer_write(high_output
);
1362 buffer_write(high_direction
);
1363 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1367 static void flyswatter_reset(int trst
, int srst
)
1371 low_output
&= ~nTRST
;
1375 low_output
|= nTRST
;
1380 low_output
|= nSRST
;
1384 low_output
&= ~nSRST
;
1387 /* command "set data bits low byte" */
1389 buffer_write(low_output
);
1390 buffer_write(low_direction
);
1391 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1394 static void turtle_reset(int trst
, int srst
)
1400 low_output
|= nSRST
;
1404 low_output
&= ~nSRST
;
1407 /* command "set data bits low byte" */
1409 buffer_write(low_output
);
1410 buffer_write(low_direction
);
1411 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1414 static void comstick_reset(int trst
, int srst
)
1418 high_output
&= ~nTRST
;
1422 high_output
|= nTRST
;
1427 high_output
&= ~nSRST
;
1431 high_output
|= nSRST
;
1434 /* command "set data bits high byte" */
1436 buffer_write(high_output
);
1437 buffer_write(high_direction
);
1438 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1442 static void stm32stick_reset(int trst
, int srst
)
1446 high_output
&= ~nTRST
;
1450 high_output
|= nTRST
;
1455 low_output
&= ~nSRST
;
1459 low_output
|= nSRST
;
1462 /* command "set data bits low byte" */
1464 buffer_write(low_output
);
1465 buffer_write(low_direction
);
1467 /* command "set data bits high byte" */
1469 buffer_write(high_output
);
1470 buffer_write(high_direction
);
1471 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,
1475 static void sheevaplug_reset(int trst
, int srst
)
1478 high_output
&= ~nTRST
;
1480 high_output
|= nTRST
;
1483 high_output
&= ~nSRSTnOE
;
1485 high_output
|= nSRSTnOE
;
1487 /* command "set data bits high byte" */
1489 buffer_write(high_output
);
1490 buffer_write(high_direction
);
1491 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1494 static int ft2232_execute_runtest(jtag_command_t
*cmd
)
1498 int predicted_size
= 0;
1501 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1502 cmd
->cmd
.runtest
->num_cycles
,
1503 tap_state_name(cmd
->cmd
.runtest
->end_state
));
1505 /* only send the maximum buffer size that FT2232C can handle */
1507 if (tap_get_state() != TAP_IDLE
)
1508 predicted_size
+= 3;
1509 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
1510 if (cmd
->cmd
.runtest
->end_state
!= TAP_IDLE
)
1511 predicted_size
+= 3;
1512 if (tap_get_end_state() != TAP_IDLE
)
1513 predicted_size
+= 3;
1514 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1516 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1517 retval
= ERROR_JTAG_QUEUE_FAILED
;
1521 if (tap_get_state() != TAP_IDLE
)
1523 move_to_state(TAP_IDLE
);
1526 i
= cmd
->cmd
.runtest
->num_cycles
;
1529 /* there are no state transitions in this code, so omit state tracking */
1531 /* command "Clock Data to TMS/CS Pin (no Read)" */
1535 buffer_write((i
> 7) ? 6 : (i
- 1));
1540 i
-= (i
> 7) ? 7 : i
;
1541 /* LOG_DEBUG("added TMS scan (no read)"); */
1544 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1546 if (tap_get_state() != tap_get_end_state())
1548 move_to_state(tap_get_end_state());
1552 DEBUG_JTAG_IO("runtest: %i, end in %s",
1553 cmd
->cmd
.runtest
->num_cycles
,
1554 tap_state_name(tap_get_end_state()));
1558 static int ft2232_execute_statemove(jtag_command_t
*cmd
)
1560 int predicted_size
= 0;
1561 int retval
= ERROR_OK
;
1563 DEBUG_JTAG_IO("statemove end in %s",
1564 tap_state_name(cmd
->cmd
.statemove
->end_state
));
1566 /* only send the maximum buffer size that FT2232C can handle */
1568 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1570 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1571 retval
= ERROR_JTAG_QUEUE_FAILED
;
1575 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1577 /* For TAP_RESET, ignore the current recorded state. It's often
1578 * wrong at server startup, and this transation is critical whenever
1581 if (tap_get_end_state() == TAP_RESET
) {
1582 clock_tms(0x4b, 0xff, 5, 0);
1585 /* shortest-path move to desired end state */
1586 } else if (tap_get_state() != tap_get_end_state())
1588 move_to_state(tap_get_end_state());
1595 static int ft2232_execute_pathmove(jtag_command_t
*cmd
)
1597 int predicted_size
= 0;
1598 int retval
= ERROR_OK
;
1600 tap_state_t
* path
= cmd
->cmd
.pathmove
->path
;
1601 int num_states
= cmd
->cmd
.pathmove
->num_states
;
1603 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states
,
1604 tap_state_name(tap_get_state()),
1605 tap_state_name(path
[num_states
-1]));
1607 /* only send the maximum buffer size that FT2232C can handle */
1608 predicted_size
= 3 * CEIL(num_states
, 7);
1609 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1611 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1612 retval
= ERROR_JTAG_QUEUE_FAILED
;
1618 ft2232_add_pathmove(path
, num_states
);
1624 static int ft2232_execute_scan(jtag_command_t
*cmd
)
1627 int scan_size
; /* size of IR or DR scan */
1628 int predicted_size
= 0;
1629 int retval
= ERROR_OK
;
1631 enum scan_type type
= jtag_scan_type(cmd
->cmd
.scan
);
1633 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN", type
);
1635 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1637 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1638 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1640 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1641 /* unsent commands before this */
1642 if (first_unsent
!= cmd
)
1643 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1644 retval
= ERROR_JTAG_QUEUE_FAILED
;
1646 /* current command */
1647 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1648 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1650 first_unsent
= cmd
->next
;
1655 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1657 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1660 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1661 retval
= ERROR_JTAG_QUEUE_FAILED
;
1665 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1666 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1667 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1668 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1672 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1673 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1674 tap_state_name(tap_get_end_state()));
1679 static int ft2232_execute_reset(jtag_command_t
*cmd
)
1682 int predicted_size
= 0;
1685 DEBUG_JTAG_IO("reset trst: %i srst %i",
1686 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1688 /* only send the maximum buffer size that FT2232C can handle */
1690 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1692 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1693 retval
= ERROR_JTAG_QUEUE_FAILED
;
1698 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1700 tap_set_state(TAP_RESET
);
1703 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1706 DEBUG_JTAG_IO("trst: %i, srst: %i",
1707 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1711 static int ft2232_execute_sleep(jtag_command_t
*cmd
)
1716 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
1718 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1719 retval
= ERROR_JTAG_QUEUE_FAILED
;
1720 first_unsent
= cmd
->next
;
1721 jtag_sleep(cmd
->cmd
.sleep
->us
);
1722 DEBUG_JTAG_IO("sleep %i usec while in %s",
1724 tap_state_name(tap_get_state()));
1728 static int ft2232_execute_stableclocks(jtag_command_t
*cmd
)
1733 /* this is only allowed while in a stable state. A check for a stable
1734 * state was done in jtag_add_clocks()
1736 if (ft2232_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, cmd
) != ERROR_OK
)
1737 retval
= ERROR_JTAG_QUEUE_FAILED
;
1738 DEBUG_JTAG_IO("clocks %i while in %s",
1739 cmd
->cmd
.stableclocks
->num_cycles
,
1740 tap_state_name(tap_get_state()));
1744 static int ft2232_execute_command(jtag_command_t
*cmd
)
1751 case JTAG_RESET
: retval
= ft2232_execute_reset(cmd
); break;
1752 case JTAG_RUNTEST
: retval
= ft2232_execute_runtest(cmd
); break;
1753 case JTAG_STATEMOVE
: retval
= ft2232_execute_statemove(cmd
); break;
1754 case JTAG_PATHMOVE
: retval
= ft2232_execute_pathmove(cmd
); break;
1755 case JTAG_SCAN
: retval
= ft2232_execute_scan(cmd
); break;
1756 case JTAG_SLEEP
: retval
= ft2232_execute_sleep(cmd
); break;
1757 case JTAG_STABLECLOCKS
: retval
= ft2232_execute_stableclocks(cmd
); break;
1759 LOG_ERROR("BUG: unknown JTAG command type encountered");
1765 static int ft2232_execute_queue(void)
1767 jtag_command_t
* cmd
= jtag_command_queue
; /* currently processed command */
1770 first_unsent
= cmd
; /* next command that has to be sent */
1773 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1774 * that wasn't handled by a caller-provided error handler
1778 ft2232_buffer_size
= 0;
1779 ft2232_expect_read
= 0;
1781 /* blink, if the current layout has that feature */
1787 if (ft2232_execute_command(cmd
) != ERROR_OK
)
1788 retval
= ERROR_JTAG_QUEUE_FAILED
;
1789 /* Start reading input before FT2232 TX buffer fills up */
1791 if (ft2232_expect_read
> 256)
1793 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1794 retval
= ERROR_JTAG_QUEUE_FAILED
;
1799 if (require_send
> 0)
1800 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1801 retval
= ERROR_JTAG_QUEUE_FAILED
;
1806 #if BUILD_FT2232_FTD2XX == 1
1807 static int ft2232_init_ftd2xx(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1811 char SerialNumber
[16];
1812 char Description
[64];
1813 DWORD openex_flags
= 0;
1814 char* openex_string
= NULL
;
1815 uint8_t latency_timer
;
1817 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout
, vid
, pid
);
1820 /* Add non-standard Vid/Pid to the linux driver */
1821 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1823 LOG_WARNING("couldn't add %4.4x:%4.4x", vid
, pid
);
1827 if (ft2232_device_desc
&& ft2232_serial
)
1829 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1830 ft2232_device_desc
= NULL
;
1833 if (ft2232_device_desc
)
1835 openex_string
= ft2232_device_desc
;
1836 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1838 else if (ft2232_serial
)
1840 openex_string
= ft2232_serial
;
1841 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1845 LOG_ERROR("neither device description nor serial number specified");
1846 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1848 return ERROR_JTAG_INIT_FAILED
;
1851 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1852 if (status
!= FT_OK
) {
1853 /* under Win32, the FTD2XX driver appends an "A" to the end
1854 * of the description, if we tried by the desc, then
1855 * try by the alternate "A" description. */
1856 if (openex_string
== ft2232_device_desc
) {
1857 /* Try the alternate method. */
1858 openex_string
= ft2232_device_desc_A
;
1859 status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
);
1860 if (status
== FT_OK
) {
1861 /* yea, the "alternate" method worked! */
1863 /* drat, give the user a meaningfull message.
1864 * telling the use we tried *BOTH* methods. */
1865 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1867 ft2232_device_desc_A
);
1872 if (status
!= FT_OK
)
1878 LOG_WARNING("unable to open ftdi device (trying more): %lu", status
);
1880 return ERROR_JTAG_INIT_FAILED
;
1882 LOG_ERROR("unable to open ftdi device: %lu", status
);
1883 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1884 if (status
== FT_OK
)
1886 char** desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1889 for (i
= 0; i
< num_devices
; i
++)
1890 desc_array
[i
] = malloc(64);
1892 desc_array
[num_devices
] = NULL
;
1894 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1896 if (status
== FT_OK
)
1898 LOG_ERROR("ListDevices: %lu\n", num_devices
);
1899 for (i
= 0; i
< num_devices
; i
++)
1900 LOG_ERROR("%" PRIu32
": \"%s\"", i
, desc_array
[i
]);
1903 for (i
= 0; i
< num_devices
; i
++)
1904 free(desc_array
[i
]);
1910 LOG_ERROR("ListDevices: NONE\n");
1912 return ERROR_JTAG_INIT_FAILED
;
1915 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1917 LOG_ERROR("unable to set latency timer: %lu", status
);
1918 return ERROR_JTAG_INIT_FAILED
;
1921 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1923 LOG_ERROR("unable to get latency timer: %lu", status
);
1924 return ERROR_JTAG_INIT_FAILED
;
1928 LOG_DEBUG("current latency timer: %i", latency_timer
);
1931 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1933 LOG_ERROR("unable to set timeouts: %lu", status
);
1934 return ERROR_JTAG_INIT_FAILED
;
1937 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1939 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
1940 return ERROR_JTAG_INIT_FAILED
;
1943 if ((status
= FT_GetDeviceInfo(ftdih
, &ftdi_device
, &deviceID
, SerialNumber
, Description
, NULL
)) != FT_OK
)
1945 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status
);
1946 return ERROR_JTAG_INIT_FAILED
;
1950 static const char* type_str
[] =
1951 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1952 unsigned no_of_known_types
= sizeof(type_str
) / sizeof(type_str
[0]) - 1;
1953 unsigned type_index
= ((unsigned)ftdi_device
<= no_of_known_types
)
1954 ? ftdi_device
: FT_DEVICE_UNKNOWN
;
1955 LOG_INFO("device: %lu \"%s\"", ftdi_device
, type_str
[type_index
]);
1956 LOG_INFO("deviceID: %lu", deviceID
);
1957 LOG_INFO("SerialNumber: %s", SerialNumber
);
1958 LOG_INFO("Description: %s", Description
);
1964 static int ft2232_purge_ftd2xx(void)
1968 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1970 LOG_ERROR("error purging ftd2xx device: %lu", status
);
1971 return ERROR_JTAG_INIT_FAILED
;
1977 #endif /* BUILD_FT2232_FTD2XX == 1 */
1979 #if BUILD_FT2232_LIBFTDI == 1
1980 static int ft2232_init_libftdi(uint16_t vid
, uint16_t pid
, int more
, int* try_more
)
1982 uint8_t latency_timer
;
1984 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1985 ft2232_layout
, vid
, pid
);
1987 if (ftdi_init(&ftdic
) < 0)
1988 return ERROR_JTAG_INIT_FAILED
;
1990 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
1992 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
1993 return ERROR_JTAG_INIT_FAILED
;
1996 /* context, vendor id, product id */
1997 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
2001 LOG_WARNING("unable to open ftdi device (trying more): %s",
2004 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
2006 return ERROR_JTAG_INIT_FAILED
;
2009 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2010 if (ftdi_usb_reset(&ftdic
) < 0)
2012 LOG_ERROR("unable to reset ftdi device");
2013 return ERROR_JTAG_INIT_FAILED
;
2016 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
2018 LOG_ERROR("unable to set latency timer");
2019 return ERROR_JTAG_INIT_FAILED
;
2022 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
2024 LOG_ERROR("unable to get latency timer");
2025 return ERROR_JTAG_INIT_FAILED
;
2029 LOG_DEBUG("current latency timer: %i", latency_timer
);
2032 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
2034 ftdi_device
= ftdic
.type
;
2035 static const char* type_str
[] =
2036 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2037 unsigned no_of_known_types
= sizeof(type_str
) / sizeof(type_str
[0]) - 1;
2038 unsigned type_index
= ((unsigned)ftdi_device
< no_of_known_types
)
2039 ? ftdi_device
: no_of_known_types
;
2040 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device
, type_str
[type_index
]);
2044 static int ft2232_purge_libftdi(void)
2046 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
2048 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
2049 return ERROR_JTAG_INIT_FAILED
;
2055 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2057 static int ft2232_init(void)
2061 uint32_t bytes_written
;
2062 const struct ft2232_layout
* cur_layout
= ft2232_layouts
;
2065 if (tap_get_tms_path_len(TAP_IRPAUSE
,TAP_IRPAUSE
) == 7)
2067 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2071 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2074 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
2076 ft2232_layout
= "usbjtag";
2077 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2080 while (cur_layout
->name
)
2082 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
2084 layout
= cur_layout
;
2092 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
2093 return ERROR_JTAG_INIT_FAILED
;
2099 * "more indicates that there are more IDs to try, so we should
2100 * not print an error for an ID mismatch (but for anything
2103 * try_more indicates that the error code returned indicates an
2104 * ID mismatch (and nothing else) and that we should proceeed
2105 * with the next ID pair.
2107 int more
= ft2232_vid
[i
+ 1] || ft2232_pid
[i
+ 1];
2110 #if BUILD_FT2232_FTD2XX == 1
2111 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
2113 #elif BUILD_FT2232_LIBFTDI == 1
2114 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
2119 if (!more
|| !try_more
)
2123 ft2232_buffer_size
= 0;
2124 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
2126 if (layout
->init() != ERROR_OK
)
2127 return ERROR_JTAG_INIT_FAILED
;
2129 if (ft2232_device_is_highspeed())
2131 #ifndef BUILD_FT2232_HIGHSPEED
2132 #if BUILD_FT2232_FTD2XX == 1
2133 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2134 #elif BUILD_FT2232_LIBFTDI == 1
2135 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2138 /* make sure the legacy mode is disabled */
2139 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK
)
2140 return ERROR_JTAG_INIT_FAILED
;
2143 ft2232_speed(jtag_get_speed());
2145 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2146 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
2148 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2149 return ERROR_JTAG_INIT_FAILED
;
2152 #if BUILD_FT2232_FTD2XX == 1
2153 return ft2232_purge_ftd2xx();
2154 #elif BUILD_FT2232_LIBFTDI == 1
2155 return ft2232_purge_libftdi();
2161 static int usbjtag_init(void)
2164 uint32_t bytes_written
;
2167 low_direction
= 0x0b;
2169 if (strcmp(ft2232_layout
, "usbjtag") == 0)
2176 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
2183 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
2190 low_direction
= 0x8b;
2192 else if (strcmp(ft2232_layout
, "luminary_icdi") == 0)
2199 low_direction
= 0xcb;
2203 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
2204 return ERROR_JTAG_INIT_FAILED
;
2207 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2208 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2210 low_direction
&= ~nTRSTnOE
; /* nTRST input */
2211 low_output
&= ~nTRST
; /* nTRST = 0 */
2215 low_direction
|= nTRSTnOE
; /* nTRST output */
2216 low_output
|= nTRST
; /* nTRST = 1 */
2219 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2221 low_direction
|= nSRSTnOE
; /* nSRST output */
2222 low_output
|= nSRST
; /* nSRST = 1 */
2226 low_direction
&= ~nSRSTnOE
; /* nSRST input */
2227 low_output
&= ~nSRST
; /* nSRST = 0 */
2230 /* initialize low byte for jtag */
2231 buf
[0] = 0x80; /* command "set data bits low byte" */
2232 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2233 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2234 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2236 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2238 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2239 return ERROR_JTAG_INIT_FAILED
;
2245 static int axm0432_jtag_init(void)
2248 uint32_t bytes_written
;
2251 low_direction
= 0x2b;
2253 /* initialize low byte for jtag */
2254 buf
[0] = 0x80; /* command "set data bits low byte" */
2255 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2256 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2257 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2259 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2261 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2262 return ERROR_JTAG_INIT_FAILED
;
2265 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
2268 nTRSTnOE
= 0x0; /* No output enable for TRST*/
2270 nSRSTnOE
= 0x0; /* No output enable for SRST*/
2274 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2279 high_direction
= 0x0c;
2281 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2282 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2284 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2288 high_output
|= nTRST
;
2291 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2293 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2297 high_output
|= nSRST
;
2300 /* initialize high port */
2301 buf
[0] = 0x82; /* command "set data bits high byte" */
2302 buf
[1] = high_output
; /* value */
2303 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2304 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2306 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2308 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2309 return ERROR_JTAG_INIT_FAILED
;
2315 static int jtagkey_init(void)
2318 uint32_t bytes_written
;
2321 low_direction
= 0x1b;
2323 /* initialize low byte for jtag */
2324 buf
[0] = 0x80; /* command "set data bits low byte" */
2325 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2326 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2327 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2329 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2331 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2332 return ERROR_JTAG_INIT_FAILED
;
2335 if (strcmp(layout
->name
, "jtagkey") == 0)
2342 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
2343 || (strcmp(layout
->name
, "oocdlink") == 0))
2352 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2357 high_direction
= 0x0f;
2359 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2360 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2362 high_output
|= nTRSTnOE
;
2363 high_output
&= ~nTRST
;
2367 high_output
&= ~nTRSTnOE
;
2368 high_output
|= nTRST
;
2371 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2373 high_output
&= ~nSRSTnOE
;
2374 high_output
|= nSRST
;
2378 high_output
|= nSRSTnOE
;
2379 high_output
&= ~nSRST
;
2382 /* initialize high port */
2383 buf
[0] = 0x82; /* command "set data bits high byte" */
2384 buf
[1] = high_output
; /* value */
2385 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2386 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2388 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2390 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2391 return ERROR_JTAG_INIT_FAILED
;
2397 static int olimex_jtag_init(void)
2400 uint32_t bytes_written
;
2403 low_direction
= 0x1b;
2405 /* initialize low byte for jtag */
2406 buf
[0] = 0x80; /* command "set data bits low byte" */
2407 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2408 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2409 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2411 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2413 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2414 return ERROR_JTAG_INIT_FAILED
;
2420 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2423 high_direction
= 0x0f;
2425 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2426 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
2428 high_output
|= nTRSTnOE
;
2429 high_output
&= ~nTRST
;
2433 high_output
&= ~nTRSTnOE
;
2434 high_output
|= nTRST
;
2437 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
2439 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2443 high_output
&= ~nSRST
;
2446 /* turn red LED on */
2447 high_output
|= 0x08;
2449 /* initialize high port */
2450 buf
[0] = 0x82; /* command "set data bits high byte" */
2451 buf
[1] = high_output
; /* value */
2452 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2453 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2455 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
) || (bytes_written
!= 3))
2457 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2458 return ERROR_JTAG_INIT_FAILED
;
2464 static int flyswatter_init(void)
2467 uint32_t bytes_written
;
2470 low_direction
= 0xfb;
2472 /* initialize low byte for jtag */
2473 buf
[0] = 0x80; /* command "set data bits low byte" */
2474 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2475 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2476 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2478 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2480 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2481 return ERROR_JTAG_INIT_FAILED
;
2485 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2487 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2490 high_direction
= 0x0c;
2492 /* turn red LED3 on, LED2 off */
2493 high_output
|= 0x08;
2495 /* initialize high port */
2496 buf
[0] = 0x82; /* command "set data bits high byte" */
2497 buf
[1] = high_output
; /* value */
2498 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2499 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2501 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2503 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2504 return ERROR_JTAG_INIT_FAILED
;
2510 static int turtle_init(void)
2513 uint32_t bytes_written
;
2516 low_direction
= 0x5b;
2518 /* initialize low byte for jtag */
2519 buf
[0] = 0x80; /* command "set data bits low byte" */
2520 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2521 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2522 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2524 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2526 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2527 return ERROR_JTAG_INIT_FAILED
;
2533 high_direction
= 0x0C;
2535 /* initialize high port */
2536 buf
[0] = 0x82; /* command "set data bits high byte" */
2537 buf
[1] = high_output
;
2538 buf
[2] = high_direction
;
2539 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2541 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2543 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2544 return ERROR_JTAG_INIT_FAILED
;
2550 static int comstick_init(void)
2553 uint32_t bytes_written
;
2556 low_direction
= 0x0b;
2558 /* initialize low byte for jtag */
2559 buf
[0] = 0x80; /* command "set data bits low byte" */
2560 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2561 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2562 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2564 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2566 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2567 return ERROR_JTAG_INIT_FAILED
;
2571 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2573 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2576 high_direction
= 0x03;
2578 /* initialize high port */
2579 buf
[0] = 0x82; /* command "set data bits high byte" */
2580 buf
[1] = high_output
;
2581 buf
[2] = high_direction
;
2582 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2584 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2586 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2587 return ERROR_JTAG_INIT_FAILED
;
2593 static int stm32stick_init(void)
2596 uint32_t bytes_written
;
2599 low_direction
= 0x8b;
2601 /* initialize low byte for jtag */
2602 buf
[0] = 0x80; /* command "set data bits low byte" */
2603 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2604 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2605 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2607 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2609 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2610 return ERROR_JTAG_INIT_FAILED
;
2614 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2616 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2619 high_direction
= 0x03;
2621 /* initialize high port */
2622 buf
[0] = 0x82; /* command "set data bits high byte" */
2623 buf
[1] = high_output
;
2624 buf
[2] = high_direction
;
2625 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2627 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2629 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2630 return ERROR_JTAG_INIT_FAILED
;
2636 static int sheevaplug_init(void)
2639 uint32_t bytes_written
;
2642 low_direction
= 0x1b;
2644 /* initialize low byte for jtag */
2645 buf
[0] = 0x80; /* command "set data bits low byte" */
2646 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2647 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2648 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2650 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2652 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2653 return ERROR_JTAG_INIT_FAILED
;
2662 high_direction
= 0x0f;
2664 /* nTRST is always push-pull */
2665 high_output
&= ~nTRSTnOE
;
2666 high_output
|= nTRST
;
2668 /* nSRST is always open-drain */
2669 high_output
|= nSRSTnOE
;
2670 high_output
&= ~nSRST
;
2672 /* initialize high port */
2673 buf
[0] = 0x82; /* command "set data bits high byte" */
2674 buf
[1] = high_output
; /* value */
2675 buf
[2] = high_direction
; /* all outputs - xRST */
2676 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2678 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2680 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2681 return ERROR_JTAG_INIT_FAILED
;
2687 static int cortino_jtag_init(void)
2690 uint32_t bytes_written
;
2693 low_direction
= 0x1b;
2695 /* initialize low byte for jtag */
2696 buf
[0] = 0x80; /* command "set data bits low byte" */
2697 buf
[1] = low_output
; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2698 buf
[2] = low_direction
; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2699 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2701 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2703 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2704 return ERROR_JTAG_INIT_FAILED
;
2708 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2710 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2713 high_direction
= 0x03;
2715 /* initialize high port */
2716 buf
[0] = 0x82; /* command "set data bits high byte" */
2717 buf
[1] = high_output
;
2718 buf
[2] = high_direction
;
2719 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2721 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2723 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2724 return ERROR_JTAG_INIT_FAILED
;
2730 static void olimex_jtag_blink(void)
2732 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2733 * ACBUS3 is bit 3 of the GPIOH port
2735 if (high_output
& 0x08)
2737 /* set port pin high */
2738 high_output
&= 0x07;
2742 /* set port pin low */
2743 high_output
|= 0x08;
2747 buffer_write(high_output
);
2748 buffer_write(high_direction
);
2751 static void flyswatter_jtag_blink(void)
2754 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2756 high_output
^= 0x0c;
2759 buffer_write(high_output
);
2760 buffer_write(high_direction
);
2763 static void turtle_jtag_blink(void)
2766 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2768 if (high_output
& 0x08)
2778 buffer_write(high_output
);
2779 buffer_write(high_direction
);
2782 static int ft2232_quit(void)
2784 #if BUILD_FT2232_FTD2XX == 1
2787 status
= FT_Close(ftdih
);
2788 #elif BUILD_FT2232_LIBFTDI == 1
2789 ftdi_usb_close(&ftdic
);
2791 ftdi_deinit(&ftdic
);
2794 free(ft2232_buffer
);
2795 ft2232_buffer
= NULL
;
2800 COMMAND_HANDLER(ft2232_handle_device_desc_command
)
2806 ft2232_device_desc
= strdup(args
[0]);
2807 cp
= strchr(ft2232_device_desc
, 0);
2808 /* under Win32, the FTD2XX driver appends an "A" to the end
2809 * of the description, this examines the given desc
2810 * and creates the 'missing' _A or non_A variable. */
2811 if ((cp
[-1] == 'A') && (cp
[-2]==' ')) {
2812 /* it was, so make this the "A" version. */
2813 ft2232_device_desc_A
= ft2232_device_desc
;
2814 /* and *CREATE* the non-A version. */
2815 strcpy(buf
, ft2232_device_desc
);
2816 cp
= strchr(buf
, 0);
2818 ft2232_device_desc
= strdup(buf
);
2820 /* <space > A not defined
2822 sprintf(buf
, "%s A", ft2232_device_desc
);
2823 ft2232_device_desc_A
= strdup(buf
);
2828 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2834 COMMAND_HANDLER(ft2232_handle_serial_command
)
2838 ft2232_serial
= strdup(args
[0]);
2842 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2848 COMMAND_HANDLER(ft2232_handle_layout_command
)
2853 ft2232_layout
= malloc(strlen(args
[0]) + 1);
2854 strcpy(ft2232_layout
, args
[0]);
2859 COMMAND_HANDLER(ft2232_handle_vid_pid_command
)
2861 if (argc
> MAX_USB_IDS
* 2)
2863 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2864 "(maximum is %d pairs)", MAX_USB_IDS
);
2865 argc
= MAX_USB_IDS
* 2;
2867 if (argc
< 2 || (argc
& 1))
2869 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2871 return ERROR_COMMAND_SYNTAX_ERROR
;
2872 /* remove the incomplete trailing id */
2877 for (i
= 0; i
< argc
; i
+= 2)
2879 COMMAND_PARSE_NUMBER(u16
, args
[i
], ft2232_vid
[i
>> 1]);
2880 COMMAND_PARSE_NUMBER(u16
, args
[i
+ 1], ft2232_pid
[i
>> 1]);
2884 * Explicitly terminate, in case there are multiples instances of
2887 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2892 COMMAND_HANDLER(ft2232_handle_latency_command
)
2896 ft2232_latency
= atoi(args
[0]);
2900 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2906 static int ft2232_stableclocks(int num_cycles
, jtag_command_t
* cmd
)
2910 /* 7 bits of either ones or zeros. */
2911 uint8_t tms
= (tap_get_state() == TAP_RESET
? 0x7F : 0x00);
2913 while (num_cycles
> 0)
2915 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2916 * at most 7 bits per invocation. Here we invoke it potentially
2919 int bitcount_per_command
= (num_cycles
> 7) ? 7 : num_cycles
;
2921 if (ft2232_buffer_size
+ 3 >= FT2232_BUFFER_SIZE
)
2923 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
2924 retval
= ERROR_JTAG_QUEUE_FAILED
;
2929 /* there are no state transitions in this code, so omit state tracking */
2931 /* command "Clock Data to TMS/CS Pin (no Read)" */
2935 buffer_write(bitcount_per_command
- 1);
2937 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2942 num_cycles
-= bitcount_per_command
;
2948 /* ---------------------------------------------------------------------
2949 * Support for IceBear JTAG adapter from Section5:
2950 * http://section5.ch/icebear
2952 * Author: Sten, debian@sansys-electronic.com
2955 /* Icebear pin layout
2957 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2958 * GND GND | 4 3| n.c.
2959 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2960 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2961 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2962 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2963 * ADBUS2 TDO |14 13| GND GND
2965 * ADBUS0 O L TCK ACBUS0 GND
2966 * ADBUS1 O L TDI ACBUS1 GND
2967 * ADBUS2 I TDO ACBUS2 n.c.
2968 * ADBUS3 O H TMS ACBUS3 n.c.
2974 static int icebear_jtag_init(void) {
2976 uint32_t bytes_written
;
2978 low_direction
= 0x0b; /* output: TCK TDI TMS; input: TDO */
2979 low_output
= 0x08; /* high: TMS; low: TCK TDI */
2983 enum reset_types jtag_reset_config
= jtag_get_reset_config();
2984 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0) {
2985 low_direction
&= ~nTRST
; /* nTRST high impedance */
2988 low_direction
|= nTRST
;
2989 low_output
|= nTRST
;
2992 low_direction
|= nSRST
;
2993 low_output
|= nSRST
;
2995 /* initialize low byte for jtag */
2996 buf
[0] = 0x80; /* command "set data bits low byte" */
2997 buf
[1] = low_output
;
2998 buf
[2] = low_direction
;
2999 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3001 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3002 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3003 return ERROR_JTAG_INIT_FAILED
;
3007 high_direction
= 0x00;
3010 /* initialize high port */
3011 buf
[0] = 0x82; /* command "set data bits high byte" */
3012 buf
[1] = high_output
; /* value */
3013 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
3014 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3016 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3)) {
3017 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3018 return ERROR_JTAG_INIT_FAILED
;
3024 static void icebear_jtag_reset(int trst
, int srst
) {
3027 low_direction
|= nTRST
;
3028 low_output
&= ~nTRST
;
3030 else if (trst
== 0) {
3031 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3032 if ((jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) != 0)
3033 low_direction
&= ~nTRST
;
3035 low_output
|= nTRST
;
3039 low_output
&= ~nSRST
;
3041 else if (srst
== 0) {
3042 low_output
|= nSRST
;
3045 /* command "set data bits low byte" */
3047 buffer_write(low_output
);
3048 buffer_write(low_direction
);
3050 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
3053 /* ---------------------------------------------------------------------
3054 * Support for Signalyzer H2 and Signalyzer H4
3055 * JTAG adapter from Xverve Technologies Inc.
3056 * http://www.signalyzer.com or http://www.xverve.com
3058 * Author: Oleg Seiljus, oleg@signalyzer.com
3060 static unsigned char signalyzer_h_side
;
3061 static unsigned int signalyzer_h_adapter_type
;
3063 static int signalyzer_h_ctrl_write(int address
, unsigned short value
);
3065 #if BUILD_FT2232_FTD2XX == 1
3066 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
);
3069 #define SIGNALYZER_COMMAND_ADDR 128
3070 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3072 #define SIGNALYZER_COMMAND_VERSION 0x41
3073 #define SIGNALYZER_COMMAND_RESET 0x42
3074 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3075 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3076 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3077 #define SIGNALYZER_COMMAND_LED_SET 0x53
3078 #define SIGNALYZER_COMMAND_ADC 0x54
3079 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3080 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3081 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3082 #define SIGNALYZER_COMMAND_I2C 0x58
3084 #define SIGNALYZER_CHAN_A 1
3085 #define SIGNALYZER_CHAN_B 2
3086 /* LEDS use channel C */
3087 #define SIGNALYZER_CHAN_C 4
3089 #define SIGNALYZER_LED_GREEN 1
3090 #define SIGNALYZER_LED_RED 2
3092 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3093 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3094 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3095 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3096 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3099 static int signalyzer_h_ctrl_write(int address
, unsigned short value
)
3101 #if BUILD_FT2232_FTD2XX == 1
3102 return FT_WriteEE(ftdih
, address
, value
);
3103 #elif BUILD_FT2232_LIBFTDI == 1
3108 #if BUILD_FT2232_FTD2XX == 1
3109 static int signalyzer_h_ctrl_read(int address
, unsigned short *value
)
3111 return FT_ReadEE(ftdih
, address
, value
);
3115 static int signalyzer_h_led_set(unsigned char channel
, unsigned char led
,
3116 int on_time_ms
, int off_time_ms
, unsigned char cycles
)
3118 unsigned char on_time
;
3119 unsigned char off_time
;
3121 if (on_time_ms
< 0xFFFF)
3122 on_time
= (unsigned char)(on_time_ms
/ 62);
3126 off_time
= (unsigned char)(off_time_ms
/ 62);
3128 #if BUILD_FT2232_FTD2XX == 1
3131 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3132 ((uint32_t)(channel
<< 8) | led
))) != FT_OK
)
3134 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3135 return ERROR_JTAG_DEVICE_ERROR
;
3138 if ((status
= signalyzer_h_ctrl_write(
3139 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3140 ((uint32_t)(on_time
<< 8) | off_time
))) != FT_OK
)
3142 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3143 return ERROR_JTAG_DEVICE_ERROR
;
3146 if ((status
= signalyzer_h_ctrl_write(
3147 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3148 ((uint32_t)cycles
))) != FT_OK
)
3150 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3151 return ERROR_JTAG_DEVICE_ERROR
;
3154 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3155 SIGNALYZER_COMMAND_LED_SET
)) != FT_OK
)
3157 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3158 return ERROR_JTAG_DEVICE_ERROR
;
3162 #elif BUILD_FT2232_LIBFTDI == 1
3165 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3166 ((uint32_t)(channel
<< 8) | led
))) < 0)
3168 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3169 ftdi_get_error_string(&ftdic
));
3170 return ERROR_JTAG_DEVICE_ERROR
;
3173 if ((retval
= signalyzer_h_ctrl_write(
3174 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1),
3175 ((uint32_t)(on_time
<< 8) | off_time
))) < 0)
3177 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3178 ftdi_get_error_string(&ftdic
));
3179 return ERROR_JTAG_DEVICE_ERROR
;
3182 if ((retval
= signalyzer_h_ctrl_write(
3183 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2),
3184 (uint32_t)cycles
)) < 0)
3186 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3187 ftdi_get_error_string(&ftdic
));
3188 return ERROR_JTAG_DEVICE_ERROR
;
3191 if ((retval
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3192 SIGNALYZER_COMMAND_LED_SET
)) < 0)
3194 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3195 ftdi_get_error_string(&ftdic
));
3196 return ERROR_JTAG_DEVICE_ERROR
;
3203 static int signalyzer_h_init(void)
3205 #if BUILD_FT2232_FTD2XX == 1
3212 uint16_t read_buf
[12] = { 0 };
3214 uint32_t bytes_written
;
3216 /* turn on center green led */
3217 signalyzer_h_led_set(SIGNALYZER_CHAN_C
, SIGNALYZER_LED_GREEN
,
3218 0xFFFF, 0x00, 0x00);
3220 /* determine what channel config wants to open
3221 * TODO: change me... current implementation is made to work
3222 * with openocd description parsing.
3224 end_of_desc
= strrchr(ft2232_device_desc
, 0x00);
3228 signalyzer_h_side
= *(end_of_desc
- 1);
3229 if (signalyzer_h_side
== 'B')
3230 signalyzer_h_side
= SIGNALYZER_CHAN_B
;
3232 signalyzer_h_side
= SIGNALYZER_CHAN_A
;
3236 LOG_ERROR("No Channel was specified");
3240 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_GREEN
,
3243 #if BUILD_FT2232_FTD2XX == 1
3244 /* read signalyzer versionining information */
3245 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3246 SIGNALYZER_COMMAND_VERSION
)) != FT_OK
)
3248 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3249 return ERROR_JTAG_DEVICE_ERROR
;
3252 for (i
= 0; i
< 10; i
++)
3254 if ((status
= signalyzer_h_ctrl_read(
3255 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3256 &read_buf
[i
])) != FT_OK
)
3258 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3260 return ERROR_JTAG_DEVICE_ERROR
;
3264 LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3265 read_buf
[0], read_buf
[1], read_buf
[2], read_buf
[3],
3266 read_buf
[4], read_buf
[5], read_buf
[6]);
3268 /* set gpio register */
3269 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3270 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3272 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3273 return ERROR_JTAG_DEVICE_ERROR
;
3276 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
+ 1,
3279 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3280 return ERROR_JTAG_DEVICE_ERROR
;
3283 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3284 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3286 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3287 return ERROR_JTAG_DEVICE_ERROR
;
3290 /* read adapter type information */
3291 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR
,
3292 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01))) != FT_OK
)
3294 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3295 return ERROR_JTAG_DEVICE_ERROR
;
3298 if ((status
= signalyzer_h_ctrl_write(
3299 (SIGNALYZER_DATA_BUFFER_ADDR
+ 1), 0xA000)) != FT_OK
)
3301 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3302 return ERROR_JTAG_DEVICE_ERROR
;
3305 if ((status
= signalyzer_h_ctrl_write(
3306 (SIGNALYZER_DATA_BUFFER_ADDR
+ 2), 0x0008)) != FT_OK
)
3308 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3309 return ERROR_JTAG_DEVICE_ERROR
;
3312 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3313 SIGNALYZER_COMMAND_I2C
)) != FT_OK
)
3315 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status
);
3316 return ERROR_JTAG_DEVICE_ERROR
;
3321 if ((status
= signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR
,
3322 &read_buf
[0])) != FT_OK
)
3324 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status
);
3325 return ERROR_JTAG_DEVICE_ERROR
;
3328 if (read_buf
[0] != 0x0498)
3329 signalyzer_h_adapter_type
= 0x0000;
3332 for (i
= 0; i
< 4; i
++)
3334 if ((status
= signalyzer_h_ctrl_read(
3335 (SIGNALYZER_DATA_BUFFER_ADDR
+ i
),
3336 &read_buf
[i
])) != FT_OK
)
3338 LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
3340 return ERROR_JTAG_DEVICE_ERROR
;
3344 signalyzer_h_adapter_type
= read_buf
[0];
3347 #elif BUILD_FT2232_LIBFTDI == 1
3348 /* currently libftdi does not allow reading individual eeprom
3349 * locations, therefore adapter type cannot be detected.
3350 * override with most common type
3352 signalyzer_h_adapter_type
= SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
;
3355 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3357 /* ADAPTOR: EM_LT16_A */
3358 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3360 LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3361 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3369 low_direction
= 0x1b;
3372 high_direction
= 0x0;
3374 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3376 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3377 low_output
&= ~nTRST
; /* nTRST = 0 */
3381 low_direction
|= nTRSTnOE
; /* nTRST output */
3382 low_output
|= nTRST
; /* nTRST = 1 */
3385 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3387 low_direction
|= nSRSTnOE
; /* nSRST output */
3388 low_output
|= nSRST
; /* nSRST = 1 */
3392 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3393 low_output
&= ~nSRST
; /* nSRST = 0 */
3396 #if BUILD_FT2232_FTD2XX == 1
3397 /* enable power to the module */
3398 if ((status
= signalyzer_h_ctrl_write(
3399 SIGNALYZER_DATA_BUFFER_ADDR
,
3400 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3403 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3405 return ERROR_JTAG_DEVICE_ERROR
;
3408 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3409 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3411 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3413 return ERROR_JTAG_DEVICE_ERROR
;
3416 /* set gpio mode register */
3417 if ((status
= signalyzer_h_ctrl_write(
3418 SIGNALYZER_DATA_BUFFER_ADDR
,
3419 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3421 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3423 return ERROR_JTAG_DEVICE_ERROR
;
3426 if ((status
= signalyzer_h_ctrl_write(
3427 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3430 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3432 return ERROR_JTAG_DEVICE_ERROR
;
3435 if ((status
= signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR
,
3436 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3438 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3440 return ERROR_JTAG_DEVICE_ERROR
;
3443 /* set gpio register */
3444 if ((status
= signalyzer_h_ctrl_write(
3445 SIGNALYZER_DATA_BUFFER_ADDR
,
3446 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3448 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3450 return ERROR_JTAG_DEVICE_ERROR
;
3453 if ((status
= signalyzer_h_ctrl_write(
3454 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x4040))
3457 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3459 return ERROR_JTAG_DEVICE_ERROR
;
3462 if ((status
= signalyzer_h_ctrl_write(
3463 SIGNALYZER_COMMAND_ADDR
,
3464 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3466 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3468 return ERROR_JTAG_DEVICE_ERROR
;
3473 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3474 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3475 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3476 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3477 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3479 if (signalyzer_h_adapter_type
3480 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
)
3481 LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3482 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3483 else if (signalyzer_h_adapter_type
3484 == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
)
3485 LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3486 "(ARM JTAG with PSU) detected. (HW: %2x).",
3487 (read_buf
[1] >> 8));
3488 else if (signalyzer_h_adapter_type
3489 == SIGNALYZER_MODULE_TYPE_EM_JTAG
)
3490 LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3491 "detected. (HW: %2x).", (read_buf
[1] >> 8));
3492 else if (signalyzer_h_adapter_type
3493 == SIGNALYZER_MODULE_TYPE_EM_JTAG_P
)
3494 LOG_INFO("Signalyzer: EM-JTAG-P "
3495 "(Generic JTAG with PSU) detected. (HW: %2x).",
3496 (read_buf
[1] >> 8));
3504 low_direction
= 0x1b;
3507 high_direction
= 0x1f;
3509 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3511 high_output
|= nTRSTnOE
;
3512 high_output
&= ~nTRST
;
3516 high_output
&= ~nTRSTnOE
;
3517 high_output
|= nTRST
;
3520 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3522 high_output
&= ~nSRSTnOE
;
3523 high_output
|= nSRST
;
3527 high_output
|= nSRSTnOE
;
3528 high_output
&= ~nSRST
;
3531 #if BUILD_FT2232_FTD2XX == 1
3532 /* enable power to the module */
3533 if ((status
= signalyzer_h_ctrl_write(
3534 SIGNALYZER_DATA_BUFFER_ADDR
,
3535 ((uint32_t)(signalyzer_h_side
<< 8) | 0x01)))
3538 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3540 return ERROR_JTAG_DEVICE_ERROR
;
3543 if ((status
= signalyzer_h_ctrl_write(
3544 SIGNALYZER_COMMAND_ADDR
,
3545 SIGNALYZER_COMMAND_POWERCONTROL_SET
)) != FT_OK
)
3547 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3549 return ERROR_JTAG_DEVICE_ERROR
;
3552 /* set gpio mode register (IO_16 and IO_17 set as analog
3553 * inputs, other is gpio)
3555 if ((status
= signalyzer_h_ctrl_write(
3556 SIGNALYZER_DATA_BUFFER_ADDR
,
3557 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3559 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3561 return ERROR_JTAG_DEVICE_ERROR
;
3564 if ((status
= signalyzer_h_ctrl_write(
3565 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0060))
3568 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3570 return ERROR_JTAG_DEVICE_ERROR
;
3573 if ((status
= signalyzer_h_ctrl_write(
3574 SIGNALYZER_COMMAND_ADDR
,
3575 SIGNALYZER_COMMAND_GPIO_MODE
)) != FT_OK
)
3577 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3579 return ERROR_JTAG_DEVICE_ERROR
;
3582 /* set gpio register (all inputs, for -P modules,
3583 * PSU will be turned off)
3585 if ((status
= signalyzer_h_ctrl_write(
3586 SIGNALYZER_DATA_BUFFER_ADDR
,
3587 (uint32_t)(signalyzer_h_side
<< 8))) != FT_OK
)
3589 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3591 return ERROR_JTAG_DEVICE_ERROR
;
3594 if ((status
= signalyzer_h_ctrl_write(
3595 SIGNALYZER_DATA_BUFFER_ADDR
+ 1, 0x0000))
3598 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3600 return ERROR_JTAG_DEVICE_ERROR
;
3603 if ((status
= signalyzer_h_ctrl_write(
3604 SIGNALYZER_COMMAND_ADDR
,
3605 SIGNALYZER_COMMAND_GPIO_STATE
)) != FT_OK
)
3607 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
3609 return ERROR_JTAG_DEVICE_ERROR
;
3614 else if (signalyzer_h_adapter_type
== 0x0000)
3616 LOG_INFO("Signalyzer: No external modules were detected.");
3624 low_direction
= 0x1b;
3627 high_direction
= 0x0;
3629 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3631 low_direction
&= ~nTRSTnOE
; /* nTRST input */
3632 low_output
&= ~nTRST
; /* nTRST = 0 */
3636 low_direction
|= nTRSTnOE
; /* nTRST output */
3637 low_output
|= nTRST
; /* nTRST = 1 */
3640 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3642 low_direction
|= nSRSTnOE
; /* nSRST output */
3643 low_output
|= nSRST
; /* nSRST = 1 */
3647 low_direction
&= ~nSRSTnOE
; /* nSRST input */
3648 low_output
&= ~nSRST
; /* nSRST = 0 */
3653 LOG_ERROR("Unknown module type is detected: %.4x",
3654 signalyzer_h_adapter_type
);
3655 return ERROR_JTAG_DEVICE_ERROR
;
3658 /* initialize low byte of controller for jtag operation */
3660 buf
[1] = low_output
;
3661 buf
[2] = low_direction
;
3663 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
)
3664 || (bytes_written
!= 3))
3666 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3667 return ERROR_JTAG_INIT_FAILED
;
3670 #if BUILD_FT2232_FTD2XX == 1
3671 if (ftdi_device
== FT_DEVICE_2232H
)
3673 /* initialize high byte of controller for jtag operation */
3675 buf
[1] = high_output
;
3676 buf
[2] = high_direction
;
3678 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3679 || (bytes_written
!= 3))
3681 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3682 return ERROR_JTAG_INIT_FAILED
;
3685 #elif BUILD_FT2232_LIBFTDI == 1
3686 if (ftdi_device
== TYPE_2232H
)
3688 /* initialize high byte of controller for jtag operation */
3690 buf
[1] = high_output
;
3691 buf
[2] = high_direction
;
3693 if ((ft2232_write(buf
, 3, &bytes_written
) != ERROR_OK
)
3694 || (bytes_written
!= 3))
3696 LOG_ERROR("couldn't initialize Signalyzer-H layout");
3697 return ERROR_JTAG_INIT_FAILED
;
3704 static void signalyzer_h_reset(int trst
, int srst
)
3706 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3708 /* ADAPTOR: EM_LT16_A */
3709 if (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_LT16_A
)
3713 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3714 /* switch to output pin (output is low) */
3715 low_direction
|= nTRSTnOE
;
3717 /* switch output low */
3718 low_output
&= ~nTRST
;
3722 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3723 /* switch to input pin (high-Z + internal
3724 * and external pullup) */
3725 low_direction
&= ~nTRSTnOE
;
3727 /* switch output high */
3728 low_output
|= nTRST
;
3733 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3734 /* switch output low */
3735 low_output
&= ~nSRST
;
3737 /* switch to output pin (output is low) */
3738 low_direction
|= nSRSTnOE
;
3742 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3743 /* switch output high */
3744 low_output
|= nSRST
;
3746 /* switch to input pin (high-Z) */
3747 low_direction
&= ~nSRSTnOE
;
3750 /* command "set data bits low byte" */
3752 buffer_write(low_output
);
3753 buffer_write(low_direction
);
3754 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3755 "low_direction: 0x%2.2x",
3756 trst
, srst
, low_output
, low_direction
);
3758 /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3759 else if ((signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG
) ||
3760 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P
) ||
3761 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG
) ||
3762 (signalyzer_h_adapter_type
== SIGNALYZER_MODULE_TYPE_EM_JTAG_P
))
3766 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3767 high_output
&= ~nTRSTnOE
;
3769 high_output
&= ~nTRST
;
3773 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3774 high_output
|= nTRSTnOE
;
3776 high_output
|= nTRST
;
3781 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3782 high_output
&= ~nSRST
;
3784 high_output
&= ~nSRSTnOE
;
3788 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3789 high_output
|= nSRST
;
3791 high_output
|= nSRSTnOE
;
3794 /* command "set data bits high byte" */
3796 buffer_write(high_output
);
3797 buffer_write(high_direction
);
3798 LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
3799 "high_direction: 0x%2.2x",
3800 trst
, srst
, high_output
, high_direction
);
3802 else if (signalyzer_h_adapter_type
== 0x0000)
3806 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3807 /* switch to output pin (output is low) */
3808 low_direction
|= nTRSTnOE
;
3810 /* switch output low */
3811 low_output
&= ~nTRST
;
3815 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3816 /* switch to input pin (high-Z + internal
3817 * and external pullup) */
3818 low_direction
&= ~nTRSTnOE
;
3820 /* switch output high */
3821 low_output
|= nTRST
;
3826 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3827 /* switch output low */
3828 low_output
&= ~nSRST
;
3830 /* switch to output pin (output is low) */
3831 low_direction
|= nSRSTnOE
;
3835 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3836 /* switch output high */
3837 low_output
|= nSRST
;
3839 /* switch to input pin (high-Z) */
3840 low_direction
&= ~nSRSTnOE
;
3843 /* command "set data bits low byte" */
3845 buffer_write(low_output
);
3846 buffer_write(low_direction
);
3847 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
3848 "low_direction: 0x%2.2x",
3849 trst
, srst
, low_output
, low_direction
);
3853 static void signalyzer_h_blink(void)
3855 signalyzer_h_led_set(signalyzer_h_side
, SIGNALYZER_LED_RED
, 100, 0, 1);
3858 /********************************************************************
3859 * Support for KT-LINK
3860 * JTAG adapter from KRISTECH
3861 * http://www.kristech.eu
3862 *******************************************************************/
3863 static int ktlink_init(void)
3866 uint32_t bytes_written
;
3867 uint8_t swd_en
= 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
3869 low_output
= 0x08 | swd_en
; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
3870 low_direction
= 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
3872 // initialize low port
3873 buf
[0] = 0x80; // command "set data bits low byte"
3874 buf
[1] = low_output
;
3875 buf
[2] = low_direction
;
3876 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3878 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3880 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3881 return ERROR_JTAG_INIT_FAILED
;
3889 high_output
= 0x80; // turn LED on
3890 high_direction
= 0xFF; // all outputs
3892 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3894 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
) {
3895 high_output
|= nTRSTnOE
;
3896 high_output
&= ~nTRST
;
3898 high_output
&= ~nTRSTnOE
;
3899 high_output
|= nTRST
;
3902 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
) {
3903 high_output
&= ~nSRSTnOE
;
3904 high_output
|= nSRST
;
3906 high_output
|= nSRSTnOE
;
3907 high_output
&= ~nSRST
;
3910 // initialize high port
3911 buf
[0] = 0x82; // command "set data bits high byte"
3912 buf
[1] = high_output
; // value
3913 buf
[2] = high_direction
;
3914 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
3916 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
3918 LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
3919 return ERROR_JTAG_INIT_FAILED
;
3925 static void ktlink_reset(int trst
, int srst
)
3927 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3930 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3931 high_output
&= ~nTRSTnOE
;
3933 high_output
&= ~nTRST
;
3934 } else if (trst
== 0) {
3935 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
3936 high_output
|= nTRSTnOE
;
3938 high_output
|= nTRST
;
3942 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3943 high_output
&= ~nSRST
;
3945 high_output
&= ~nSRSTnOE
;
3946 } else if (srst
== 0) {
3947 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
3948 high_output
|= nSRST
;
3950 high_output
|= nSRSTnOE
;
3953 buffer_write(0x82); // command "set data bits high byte"
3954 buffer_write(high_output
);
3955 buffer_write(high_direction
);
3956 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
,high_direction
);
3959 static void ktlink_blink(void)
3961 /* LED connected to ACBUS7 */
3962 if (high_output
& 0x80)
3963 high_output
&= 0x7F;
3965 high_output
|= 0x80;
3967 buffer_write(0x82); // command "set data bits high byte"
3968 buffer_write(high_output
);
3969 buffer_write(high_direction
);
3972 static int ft2232_register_commands(struct command_context_s
* cmd_ctx
)
3974 register_command(cmd_ctx
, NULL
, "ft2232_device_desc",
3975 ft2232_handle_device_desc_command
, COMMAND_CONFIG
,
3976 "the USB device description of the FTDI FT2232 device");
3977 register_command(cmd_ctx
, NULL
, "ft2232_serial",
3978 ft2232_handle_serial_command
, COMMAND_CONFIG
,
3979 "the serial number of the FTDI FT2232 device");
3980 register_command(cmd_ctx
, NULL
, "ft2232_layout",
3981 ft2232_handle_layout_command
, COMMAND_CONFIG
,
3982 "the layout of the FT2232 GPIO signals used "
3983 "to control output-enables and reset signals");
3984 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid",
3985 ft2232_handle_vid_pid_command
, COMMAND_CONFIG
,
3986 "the vendor ID and product ID of the FTDI FT2232 device");
3987 register_command(cmd_ctx
, NULL
, "ft2232_latency",
3988 ft2232_handle_latency_command
, COMMAND_CONFIG
,
3989 "set the FT2232 latency timer to a new value");
3995 struct jtag_interface ft2232_interface
= {
3997 .register_commands
= &ft2232_register_commands
,
3998 .init
= &ft2232_init
,
3999 .quit
= &ft2232_quit
,
4000 .speed
= &ft2232_speed
,
4001 .speed_div
= &ft2232_speed_div
,
4003 .execute_queue
= &ft2232_execute_queue
,