1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
28 #include "replacements.h"
30 /* project specific includes */
34 #include "configuration.h"
35 #include "time_support.h"
42 /* FT2232 access library includes */
43 #if BUILD_FT2232_FTD2XX == 1
45 #elif BUILD_FT2232_LIBFTDI == 1
49 /* enable this to debug io latency
52 #define _DEBUG_USB_IO_
55 /* enable this to debug communication
58 #define _DEBUG_USB_COMMS_
61 int ft2232_execute_queue(void);
63 int ft2232_speed(int speed);
64 int ft2232_speed_div(int speed, int *khz);
65 int ft2232_khz(int khz, int *jtag_speed);
66 int ft2232_register_commands(struct command_context_s *cmd_ctx);
67 int ft2232_init(void);
68 int ft2232_quit(void);
70 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 char *ft2232_device_desc = NULL;
77 char *ft2232_serial = NULL;
78 char *ft2232_layout = NULL;
79 unsigned char ft2232_latency = 2;
82 /* vid = pid = 0 marks the end of the list */
83 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
84 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
86 typedef struct ft2232_layout_s
90 void(*reset)(int trst, int srst);
94 /* init procedures for supported layouts */
95 int usbjtag_init(void);
96 int jtagkey_init(void);
97 int olimex_jtag_init(void);
98 int flyswatter_init(void);
99 int turtle_init(void);
100 int comstick_init(void);
101 int stm32stick_init(void);
103 /* reset procedures for supported layouts */
104 void usbjtag_reset(int trst, int srst);
105 void jtagkey_reset(int trst, int srst);
106 void olimex_jtag_reset(int trst, int srst);
107 void flyswatter_reset(int trst, int srst);
108 void turtle_reset(int trst, int srst);
109 void comstick_reset(int trst, int srst);
110 void stm32stick_reset(int trst, int srst);
112 /* blink procedures for layouts that support a blinking led */
113 void olimex_jtag_blink(void);
114 void turtle_jtag_blink(void);
116 ft2232_layout_t ft2232_layouts[] =
118 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
119 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
120 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
121 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
122 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
123 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
124 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
125 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
126 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
127 {"comstick", comstick_init, comstick_reset, NULL},
128 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
132 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
134 static ft2232_layout_t *layout;
135 static u8 low_output = 0x0;
136 static u8 low_direction = 0x0;
137 static u8 high_output = 0x0;
138 static u8 high_direction = 0x0;
140 #if BUILD_FT2232_FTD2XX == 1
141 static FT_HANDLE ftdih = NULL;
142 #elif BUILD_FT2232_LIBFTDI == 1
143 static struct ftdi_context ftdic;
146 static u8 *ft2232_buffer = NULL;
147 static int ft2232_buffer_size = 0;
148 static int ft2232_read_pointer = 0;
149 static int ft2232_expect_read = 0;
150 #define FT2232_BUFFER_SIZE 131072
151 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
152 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
154 jtag_interface_t ft2232_interface =
157 .execute_queue = ft2232_execute_queue,
158 .speed = ft2232_speed,
159 .speed_div = ft2232_speed_div,
161 .register_commands = ft2232_register_commands,
166 int ft2232_write(u8 *buf, int size, u32* bytes_written)
168 #if BUILD_FT2232_FTD2XX == 1
170 DWORD dw_bytes_written;
171 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
173 *bytes_written = dw_bytes_written;
174 LOG_ERROR("FT_Write returned: %lu", status);
175 return ERROR_JTAG_DEVICE_ERROR;
179 *bytes_written = dw_bytes_written;
182 #elif BUILD_FT2232_LIBFTDI == 1
184 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
187 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
188 return ERROR_JTAG_DEVICE_ERROR;
192 *bytes_written = retval;
198 int ft2232_read(u8* buf, int size, u32* bytes_read)
200 #if BUILD_FT2232_FTD2XX == 1
206 while ((*bytes_read < size) && timeout--)
208 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
209 *bytes_read, &dw_bytes_read)) != FT_OK)
212 LOG_ERROR("FT_Read returned: %lu", status);
213 return ERROR_JTAG_DEVICE_ERROR;
215 *bytes_read += dw_bytes_read;
217 #elif BUILD_FT2232_LIBFTDI == 1
222 while ((*bytes_read < size) && timeout--)
224 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
227 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
228 return ERROR_JTAG_DEVICE_ERROR;
230 *bytes_read += retval;
234 if (*bytes_read < size)
236 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
237 return ERROR_JTAG_DEVICE_ERROR;
243 int ft2232_speed(int speed)
249 buf[0] = 0x86; /* command "set divisor" */
250 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
251 buf[2] = (speed >> 8) & 0xff; /* valueH */
253 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
254 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
256 LOG_ERROR("couldn't set FT2232 TCK speed");
263 int ft2232_speed_div(int speed, int *khz)
265 /* Take a look in the FT2232 manual,
266 * AN2232C-01 Command Processor for
267 * MPSSE and MCU Host Bus. Chapter 3.8 */
269 *khz = 6000 / (1+speed);
274 int ft2232_khz(int khz, int *jtag_speed)
278 LOG_ERROR("RCLK not supported");
281 /* Take a look in the FT2232 manual,
282 * AN2232C-01 Command Processor for
283 * MPSSE and MCU Host Bus. Chapter 3.8
285 * We will calc here with a multiplier
286 * of 10 for better rounding later. */
288 /* Calc speed, (6000 / khz) - 1 */
289 /* Use 65000 for better rounding */
290 *jtag_speed = (60000 / khz) - 10;
292 /* Add 0.9 for rounding */
295 /* Calc real speed */
296 *jtag_speed = *jtag_speed / 10;
298 /* Check if speed is greater than 0 */
304 /* Check max value */
305 if (*jtag_speed > 0xFFFF)
307 *jtag_speed = 0xFFFF;
313 int ft2232_register_commands(struct command_context_s *cmd_ctx)
315 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
316 COMMAND_CONFIG, NULL);
317 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
318 COMMAND_CONFIG, NULL);
319 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
320 COMMAND_CONFIG, NULL);
321 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
322 COMMAND_CONFIG, NULL);
323 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
324 COMMAND_CONFIG, NULL);
328 void ft2232_end_state(enum tap_state state)
330 if (tap_move_map[state] != -1)
334 LOG_ERROR("BUG: %i is not a valid end state", state);
339 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
341 int num_bytes = ((scan_size + 7) / 8);
342 int bits_left = scan_size;
345 while(num_bytes-- > 1)
347 buffer[cur_byte] = BUFFER_READ;
352 buffer[cur_byte] = 0x0;
356 buffer[cur_byte] = BUFFER_READ >> 1;
359 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
363 void ft2232_debug_dump_buffer(void)
369 for (i = 0; i < ft2232_buffer_size; i++)
371 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
374 LOG_DEBUG("%s", line);
380 LOG_DEBUG("%s", line);
383 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
393 #ifdef _DEBUG_USB_IO_
394 struct timeval start, inter, inter2, end;
395 struct timeval d_inter, d_inter2, d_end;
398 #ifdef _DEBUG_USB_COMMS_
399 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
400 ft2232_debug_dump_buffer();
403 #ifdef _DEBUG_USB_IO_
404 gettimeofday(&start, NULL);
407 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
409 LOG_ERROR("couldn't write MPSSE commands to FT2232");
413 #ifdef _DEBUG_USB_IO_
414 gettimeofday(&inter, NULL);
417 if (ft2232_expect_read)
420 ft2232_buffer_size = 0;
422 #ifdef _DEBUG_USB_IO_
423 gettimeofday(&inter2, NULL);
426 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
428 LOG_ERROR("couldn't read from FT2232");
432 #ifdef _DEBUG_USB_IO_
433 gettimeofday(&end, NULL);
435 timeval_subtract(&d_inter, &inter, &start);
436 timeval_subtract(&d_inter2, &inter2, &start);
437 timeval_subtract(&d_end, &end, &start);
439 LOG_INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
443 ft2232_buffer_size = bytes_read;
445 if (ft2232_expect_read != ft2232_buffer_size)
447 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
448 ft2232_debug_dump_buffer();
453 #ifdef _DEBUG_USB_COMMS_
454 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
455 ft2232_debug_dump_buffer();
459 ft2232_expect_read = 0;
460 ft2232_read_pointer = 0;
462 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
463 * that wasn't handled by a caller-provided error handler
473 type = jtag_scan_type(cmd->cmd.scan);
474 if (type != SCAN_OUT)
476 scan_size = jtag_scan_size(cmd->cmd.scan);
477 buffer = calloc(CEIL(scan_size, 8), 1);
478 ft2232_read_scan(type, buffer, scan_size);
479 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
480 retval = ERROR_JTAG_QUEUE_FAILED;
490 ft2232_buffer_size = 0;
495 void ft2232_add_pathmove(pathmove_command_t *cmd)
497 int num_states = cmd->num_states;
506 int num_states_batch = num_states > 7 ? 7 : num_states;
509 /* command "Clock Data to TMS/CS Pin (no Read)" */
511 /* number of states remaining */
512 BUFFER_ADD = num_states_batch - 1;
514 while (num_states_batch--)
516 if (tap_transitions[cur_state].low == cmd->path[state_count])
517 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
518 else if (tap_transitions[cur_state].high == cmd->path[state_count])
519 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
522 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
526 cur_state = cmd->path[state_count];
531 BUFFER_ADD = tms_byte;
534 end_state = cur_state;
537 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
539 int num_bytes = (scan_size + 7) / 8;
540 int bits_left = scan_size;
544 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
546 /* command "Clock Data to TMS/CS Pin (no Read)" */
553 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
558 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
561 /* LOG_DEBUG("added TMS scan (no read)"); */
564 /* add command for complete bytes */
565 while (num_bytes > 1)
570 /* Clock Data Bytes In and Out LSB First */
572 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
574 else if (type == SCAN_OUT)
576 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
578 /* LOG_DEBUG("added TDI bytes (o)"); */
580 else if (type == SCAN_IN)
582 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
584 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
586 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
587 num_bytes -= thisrun_bytes;
588 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
589 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
592 /* add complete bytes */
593 while(thisrun_bytes-- > 0)
595 BUFFER_ADD = buffer[cur_byte];
600 else /* (type == SCAN_IN) */
602 bits_left -= 8 * (thisrun_bytes);
606 /* the most signifcant bit is scanned during TAP movement */
608 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
612 /* process remaining bits but the last one */
617 /* Clock Data Bits In and Out LSB First */
619 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
621 else if (type == SCAN_OUT)
623 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
625 /* LOG_DEBUG("added TDI bits (o)"); */
627 else if (type == SCAN_IN)
629 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
631 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
633 BUFFER_ADD = bits_left - 2;
635 BUFFER_ADD = buffer[cur_byte];
638 if ((ir_scan && (end_state == TAP_SI)) ||
639 (!ir_scan && (end_state == TAP_SD)))
643 /* Clock Data Bits In and Out LSB First */
645 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
647 else if (type == SCAN_OUT)
649 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
651 /* LOG_DEBUG("added TDI bits (o)"); */
653 else if (type == SCAN_IN)
655 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
657 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
660 BUFFER_ADD = last_bit;
664 /* move from Shift-IR/DR to end state */
665 if (type != SCAN_OUT)
667 /* Clock Data to TMS/CS Pin with Read */
669 /* LOG_DEBUG("added TMS scan (read)"); */
673 /* Clock Data to TMS/CS Pin (no Read) */
675 /* LOG_DEBUG("added TMS scan (no read)"); */
678 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
679 cur_state = end_state;
683 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
685 int num_bytes = (scan_size + 7) / 8;
686 int bits_left = scan_size;
689 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
690 u8 *receive_pointer = receive_buffer;
694 int thisrun_read = 0;
698 LOG_ERROR("BUG: large IR scans are not supported");
702 if (cur_state != TAP_SD)
704 /* command "Clock Data to TMS/CS Pin (no Read)" */
709 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
713 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
715 LOG_ERROR("couldn't write MPSSE commands to FT2232");
718 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
719 ft2232_buffer_size = 0;
721 /* add command for complete bytes */
722 while (num_bytes > 1)
728 /* Clock Data Bytes In and Out LSB First */
730 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
732 else if (type == SCAN_OUT)
734 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
736 /* LOG_DEBUG("added TDI bytes (o)"); */
738 else if (type == SCAN_IN)
740 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
742 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
744 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
745 thisrun_read = thisrun_bytes;
746 num_bytes -= thisrun_bytes;
747 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
748 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
751 /* add complete bytes */
752 while(thisrun_bytes-- > 0)
754 BUFFER_ADD = buffer[cur_byte];
759 else /* (type == SCAN_IN) */
761 bits_left -= 8 * (thisrun_bytes);
764 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
766 LOG_ERROR("couldn't write MPSSE commands to FT2232");
769 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
770 ft2232_buffer_size = 0;
772 if (type != SCAN_OUT)
774 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
776 LOG_ERROR("couldn't read from FT2232");
779 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
780 receive_pointer += bytes_read;
786 /* the most signifcant bit is scanned during TAP movement */
788 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
792 /* process remaining bits but the last one */
797 /* Clock Data Bits In and Out LSB First */
799 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
801 else if (type == SCAN_OUT)
803 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
805 /* LOG_DEBUG("added TDI bits (o)"); */
807 else if (type == SCAN_IN)
809 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
811 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
813 BUFFER_ADD = bits_left - 2;
815 BUFFER_ADD = buffer[cur_byte];
817 if (type != SCAN_OUT)
821 if (end_state == TAP_SD)
825 /* Clock Data Bits In and Out LSB First */
827 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
829 else if (type == SCAN_OUT)
831 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
833 /* LOG_DEBUG("added TDI bits (o)"); */
835 else if (type == SCAN_IN)
837 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
839 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
842 BUFFER_ADD = last_bit;
846 /* move from Shift-IR/DR to end state */
847 if (type != SCAN_OUT)
849 /* Clock Data to TMS/CS Pin with Read */
851 /* LOG_DEBUG("added TMS scan (read)"); */
855 /* Clock Data to TMS/CS Pin (no Read) */
857 /* LOG_DEBUG("added TMS scan (no read)"); */
860 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
861 cur_state = end_state;
864 if (type != SCAN_OUT)
867 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
869 LOG_ERROR("couldn't write MPSSE commands to FT2232");
872 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
873 ft2232_buffer_size = 0;
875 if (type != SCAN_OUT)
877 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
879 LOG_ERROR("couldn't read from FT2232");
882 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
883 receive_pointer += bytes_read;
889 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
891 int predicted_size = 3;
892 int num_bytes = (scan_size - 1) / 8;
894 if (cur_state != TAP_SD)
897 if (type == SCAN_IN) /* only from device to host */
900 predicted_size += (CEIL(num_bytes, 65536)) * 3;
901 /* remaining bits - 1 (up to 7) */
902 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
904 else /* host to device, or bidirectional */
907 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
908 /* remaining bits -1 (up to 7) */
909 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
912 return predicted_size;
915 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
917 int predicted_size = 0;
919 if (type != SCAN_OUT)
922 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
923 /* remaining bits - 1 */
924 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
925 /* last bit (from TMS scan) */
929 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
931 return predicted_size;
934 void usbjtag_reset(int trst, int srst)
938 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
939 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
941 low_output &= ~nTRST; /* switch output low */
945 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
946 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
948 low_output |= nTRST; /* switch output high */
953 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
954 low_output &= ~nSRST; /* switch output low */
956 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
960 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
961 low_output |= nSRST; /* switch output high */
963 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
966 /* command "set data bits low byte" */
968 BUFFER_ADD = low_output;
969 BUFFER_ADD = low_direction;
973 void jtagkey_reset(int trst, int srst)
977 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
978 high_output &= ~nTRSTnOE;
980 high_output &= ~nTRST;
984 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
985 high_output |= nTRSTnOE;
987 high_output |= nTRST;
992 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
993 high_output &= ~nSRST;
995 high_output &= ~nSRSTnOE;
999 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1000 high_output |= nSRST;
1002 high_output |= nSRSTnOE;
1005 /* command "set data bits high byte" */
1007 BUFFER_ADD = high_output;
1008 BUFFER_ADD = high_direction;
1009 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1012 void olimex_jtag_reset(int trst, int srst)
1016 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1017 high_output &= ~nTRSTnOE;
1019 high_output &= ~nTRST;
1023 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1024 high_output |= nTRSTnOE;
1026 high_output |= nTRST;
1031 high_output |= nSRST;
1035 high_output &= ~nSRST;
1038 /* command "set data bits high byte" */
1040 BUFFER_ADD = high_output;
1041 BUFFER_ADD = high_direction;
1042 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1045 void flyswatter_reset(int trst, int srst)
1049 low_output &= ~nTRST;
1053 low_output |= nTRST;
1058 low_output |= nSRST;
1062 low_output &= ~nSRST;
1065 /* command "set data bits low byte" */
1067 BUFFER_ADD = low_output;
1068 BUFFER_ADD = low_direction;
1069 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1072 void turtle_reset(int trst, int srst)
1078 low_output |= nSRST;
1082 low_output &= ~nSRST;
1085 /* command "set data bits low byte" */
1087 BUFFER_ADD = low_output;
1088 BUFFER_ADD = low_direction;
1089 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1092 void comstick_reset(int trst, int srst)
1096 high_output &= ~nTRST;
1100 high_output |= nTRST;
1105 high_output &= ~nSRST;
1109 high_output |= nSRST;
1112 /* command "set data bits high byte" */
1114 BUFFER_ADD = high_output;
1115 BUFFER_ADD = high_direction;
1116 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1119 void stm32stick_reset(int trst, int srst)
1123 high_output &= ~nTRST;
1127 high_output |= nTRST;
1132 low_output &= ~nSRST;
1136 low_output |= nSRST;
1139 /* command "set data bits low byte" */
1141 BUFFER_ADD = low_output;
1142 BUFFER_ADD = low_direction;
1144 /* command "set data bits high byte" */
1146 BUFFER_ADD = high_output;
1147 BUFFER_ADD = high_direction;
1148 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1151 int ft2232_execute_queue()
1153 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1154 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1156 int scan_size; /* size of IR or DR scan */
1157 enum scan_type type;
1159 int predicted_size = 0;
1160 int require_send = 0;
1163 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1164 * that wasn't handled by a caller-provided error handler
1168 ft2232_buffer_size = 0;
1169 ft2232_expect_read = 0;
1171 /* blink, if the current layout has that feature */
1179 case JTAG_END_STATE:
1180 if (cmd->cmd.end_state->end_state != -1)
1181 ft2232_end_state(cmd->cmd.end_state->end_state);
1184 /* only send the maximum buffer size that FT2232C can handle */
1186 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1188 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1189 retval = ERROR_JTAG_QUEUE_FAILED;
1194 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1196 cur_state = TAP_TLR;
1198 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1201 #ifdef _DEBUG_JTAG_IO_
1202 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1206 /* only send the maximum buffer size that FT2232C can handle */
1208 if (cur_state != TAP_RTI)
1209 predicted_size += 3;
1210 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1211 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1212 predicted_size += 3;
1213 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1214 predicted_size += 3;
1215 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1217 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1218 retval = ERROR_JTAG_QUEUE_FAILED;
1222 if (cur_state != TAP_RTI)
1224 /* command "Clock Data to TMS/CS Pin (no Read)" */
1229 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1230 cur_state = TAP_RTI;
1233 i = cmd->cmd.runtest->num_cycles;
1236 /* command "Clock Data to TMS/CS Pin (no Read)" */
1239 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1242 cur_state = TAP_RTI;
1243 i -= (i > 7) ? 7 : i;
1244 /* LOG_DEBUG("added TMS scan (no read)"); */
1246 if (cmd->cmd.runtest->end_state != -1)
1247 ft2232_end_state(cmd->cmd.runtest->end_state);
1248 if (cur_state != end_state)
1250 /* command "Clock Data to TMS/CS Pin (no Read)" */
1255 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1256 cur_state = end_state;
1257 /* LOG_DEBUG("added TMS scan (no read)"); */
1260 #ifdef _DEBUG_JTAG_IO_
1261 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1264 case JTAG_STATEMOVE:
1265 /* only send the maximum buffer size that FT2232C can handle */
1267 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1269 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1270 retval = ERROR_JTAG_QUEUE_FAILED;
1274 if (cmd->cmd.statemove->end_state != -1)
1275 ft2232_end_state(cmd->cmd.statemove->end_state);
1276 /* command "Clock Data to TMS/CS Pin (no Read)" */
1281 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1282 /* LOG_DEBUG("added TMS scan (no read)"); */
1283 cur_state = end_state;
1285 #ifdef _DEBUG_JTAG_IO_
1286 LOG_DEBUG("statemove: %i", end_state);
1290 /* only send the maximum buffer size that FT2232C can handle */
1291 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1292 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1294 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1295 retval = ERROR_JTAG_QUEUE_FAILED;
1299 ft2232_add_pathmove(cmd->cmd.pathmove);
1301 #ifdef _DEBUG_JTAG_IO_
1302 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1306 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1307 type = jtag_scan_type(cmd->cmd.scan);
1308 predicted_size = ft2232_predict_scan_out(scan_size, type);
1309 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1311 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1312 /* unsent commands before this */
1313 if (first_unsent != cmd)
1314 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1315 retval = ERROR_JTAG_QUEUE_FAILED;
1317 /* current command */
1318 if (cmd->cmd.scan->end_state != -1)
1319 ft2232_end_state(cmd->cmd.scan->end_state);
1320 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1322 first_unsent = cmd->next;
1327 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1329 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1330 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1331 retval = ERROR_JTAG_QUEUE_FAILED;
1335 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1336 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1337 if (cmd->cmd.scan->end_state != -1)
1338 ft2232_end_state(cmd->cmd.scan->end_state);
1339 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1343 #ifdef _DEBUG_JTAG_IO_
1344 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1348 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1349 retval = ERROR_JTAG_QUEUE_FAILED;
1350 first_unsent = cmd->next;
1351 jtag_sleep(cmd->cmd.sleep->us);
1352 #ifdef _DEBUG_JTAG_IO_
1353 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1357 LOG_ERROR("BUG: unknown JTAG command type encountered");
1363 if (require_send > 0)
1364 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1365 retval = ERROR_JTAG_QUEUE_FAILED;
1370 #if BUILD_FT2232_FTD2XX == 1
1371 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1374 DWORD openex_flags = 0;
1375 char *openex_string = NULL;
1378 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1379 ft2232_layout, vid, pid);
1382 /* Add non-standard Vid/Pid to the linux driver */
1383 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1385 LOG_WARNING("couldn't add %4.4x:%4.4x",
1390 if (ft2232_device_desc && ft2232_serial)
1392 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1393 ft2232_device_desc = NULL;
1396 if (ft2232_device_desc)
1398 openex_string = ft2232_device_desc;
1399 openex_flags = FT_OPEN_BY_DESCRIPTION;
1401 else if (ft2232_serial)
1403 openex_string = ft2232_serial;
1404 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1408 LOG_ERROR("neither device description nor serial number specified");
1409 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1411 return ERROR_JTAG_INIT_FAILED;
1414 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1419 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1422 return ERROR_JTAG_INIT_FAILED;
1424 LOG_ERROR("unable to open ftdi device: %lu", status);
1425 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1426 if (status == FT_OK)
1428 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1431 for (i = 0; i < num_devices; i++)
1432 desc_array[i] = malloc(64);
1433 desc_array[num_devices] = NULL;
1435 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1437 if (status == FT_OK)
1439 LOG_ERROR("ListDevices: %lu\n", num_devices);
1440 for (i = 0; i < num_devices; i++)
1441 LOG_ERROR("%i: %s", i, desc_array[i]);
1444 for (i = 0; i < num_devices; i++)
1445 free(desc_array[i]);
1450 LOG_ERROR("ListDevices: NONE\n");
1452 return ERROR_JTAG_INIT_FAILED;
1455 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1457 LOG_ERROR("unable to set latency timer: %lu", status);
1458 return ERROR_JTAG_INIT_FAILED;
1461 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1463 LOG_ERROR("unable to get latency timer: %lu", status);
1464 return ERROR_JTAG_INIT_FAILED;
1468 LOG_DEBUG("current latency timer: %i", latency_timer);
1471 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1473 LOG_ERROR("unable to set timeouts: %lu", status);
1474 return ERROR_JTAG_INIT_FAILED;
1477 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1479 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1480 return ERROR_JTAG_INIT_FAILED;
1486 static int ft2232_purge_ftd2xx(void)
1490 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1492 LOG_ERROR("error purging ftd2xx device: %lu", status);
1493 return ERROR_JTAG_INIT_FAILED;
1498 #endif /* BUILD_FT2232_FTD2XX == 1 */
1500 #if BUILD_FT2232_LIBFTDI == 1
1501 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1505 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1506 ft2232_layout, vid, pid);
1508 if (ftdi_init(&ftdic) < 0)
1509 return ERROR_JTAG_INIT_FAILED;
1511 /* context, vendor id, product id */
1512 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1513 ft2232_serial) < 0) {
1515 LOG_WARNING("unable to open ftdi device (trying more): %s",
1518 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1520 return ERROR_JTAG_INIT_FAILED;
1523 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1525 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1526 return ERROR_JTAG_INIT_FAILED;
1529 if (ftdi_usb_reset(&ftdic) < 0)
1531 LOG_ERROR("unable to reset ftdi device");
1532 return ERROR_JTAG_INIT_FAILED;
1535 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1537 LOG_ERROR("unable to set latency timer");
1538 return ERROR_JTAG_INIT_FAILED;
1541 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1543 LOG_ERROR("unable to get latency timer");
1544 return ERROR_JTAG_INIT_FAILED;
1548 LOG_DEBUG("current latency timer: %i", latency_timer);
1551 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1556 static int ft2232_purge_libftdi(void)
1558 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1560 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1561 return ERROR_JTAG_INIT_FAILED;
1566 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1568 int ft2232_init(void)
1573 ft2232_layout_t *cur_layout = ft2232_layouts;
1576 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1578 ft2232_layout = "usbjtag";
1579 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1582 while (cur_layout->name)
1584 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1586 layout = cur_layout;
1594 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1595 return ERROR_JTAG_INIT_FAILED;
1598 for (i = 0; 1; i++) {
1600 * "more indicates that there are more IDs to try, so we should
1601 * not print an error for an ID mismatch (but for anything
1604 * try_more indicates that the error code returned indicates an
1605 * ID mismatch (and nothing else) and that we should proceeed
1606 * with the next ID pair.
1608 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1611 #if BUILD_FT2232_FTD2XX == 1
1612 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1614 #elif BUILD_FT2232_LIBFTDI == 1
1615 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1620 if (!more || !try_more)
1624 ft2232_buffer_size = 0;
1625 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1627 if (layout->init() != ERROR_OK)
1628 return ERROR_JTAG_INIT_FAILED;
1630 ft2232_speed(jtag_speed);
1632 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1633 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1635 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1636 return ERROR_JTAG_INIT_FAILED;
1639 #if BUILD_FT2232_FTD2XX == 1
1640 return ft2232_purge_ftd2xx();
1641 #elif BUILD_FT2232_LIBFTDI == 1
1642 return ft2232_purge_libftdi();
1648 int usbjtag_init(void)
1654 low_direction = 0x0b;
1656 if (strcmp(ft2232_layout, "usbjtag") == 0)
1663 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1670 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1677 low_direction = 0x8b;
1681 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1682 return ERROR_JTAG_INIT_FAILED;
1685 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1687 low_direction &= ~nTRSTnOE; /* nTRST input */
1688 low_output &= ~nTRST; /* nTRST = 0 */
1692 low_direction |= nTRSTnOE; /* nTRST output */
1693 low_output |= nTRST; /* nTRST = 1 */
1696 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1698 low_direction |= nSRSTnOE; /* nSRST output */
1699 low_output |= nSRST; /* nSRST = 1 */
1703 low_direction &= ~nSRSTnOE; /* nSRST input */
1704 low_output &= ~nSRST; /* nSRST = 0 */
1707 /* initialize low byte for jtag */
1708 buf[0] = 0x80; /* command "set data bits low byte" */
1709 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1710 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1711 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1713 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1715 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1716 return ERROR_JTAG_INIT_FAILED;
1722 int jtagkey_init(void)
1728 low_direction = 0x1b;
1730 /* initialize low byte for jtag */
1731 buf[0] = 0x80; /* command "set data bits low byte" */
1732 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1733 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1734 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1736 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1738 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1739 return ERROR_JTAG_INIT_FAILED;
1742 if (strcmp(layout->name, "jtagkey") == 0)
1749 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1750 (strcmp(layout->name, "oocdlink") == 0))
1759 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1764 high_direction = 0x0f;
1766 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1768 high_output |= nTRSTnOE;
1769 high_output &= ~nTRST;
1773 high_output &= ~nTRSTnOE;
1774 high_output |= nTRST;
1777 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1779 high_output &= ~nSRSTnOE;
1780 high_output |= nSRST;
1784 high_output |= nSRSTnOE;
1785 high_output &= ~nSRST;
1788 /* initialize high port */
1789 buf[0] = 0x82; /* command "set data bits high byte" */
1790 buf[1] = high_output; /* value */
1791 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1792 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1794 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1796 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1797 return ERROR_JTAG_INIT_FAILED;
1803 int olimex_jtag_init(void)
1809 low_direction = 0x1b;
1811 /* initialize low byte for jtag */
1812 buf[0] = 0x80; /* command "set data bits low byte" */
1813 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1814 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1815 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1817 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1819 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1820 return ERROR_JTAG_INIT_FAILED;
1826 nSRSTnOE = 0x00; /* no output enable for nSRST */
1829 high_direction = 0x0f;
1831 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1833 high_output |= nTRSTnOE;
1834 high_output &= ~nTRST;
1838 high_output &= ~nTRSTnOE;
1839 high_output |= nTRST;
1842 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1844 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1848 high_output &= ~nSRST;
1851 /* turn red LED on */
1852 high_output |= 0x08;
1854 /* initialize high port */
1855 buf[0] = 0x82; /* command "set data bits high byte" */
1856 buf[1] = high_output; /* value */
1857 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1858 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1860 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1862 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1863 return ERROR_JTAG_INIT_FAILED;
1869 int flyswatter_init(void)
1875 low_direction = 0xfb;
1877 /* initialize low byte for jtag */
1878 buf[0] = 0x80; /* command "set data bits low byte" */
1879 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1880 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1881 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1883 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1885 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1886 return ERROR_JTAG_INIT_FAILED;
1890 nTRSTnOE = 0x0; /* not output enable for nTRST */
1892 nSRSTnOE = 0x00; /* no output enable for nSRST */
1895 high_direction = 0x0c;
1897 /* turn red LED1 on, LED2 off */
1898 high_output |= 0x08;
1900 /* initialize high port */
1901 buf[0] = 0x82; /* command "set data bits high byte" */
1902 buf[1] = high_output; /* value */
1903 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1904 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1906 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1908 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1909 return ERROR_JTAG_INIT_FAILED;
1915 int turtle_init(void)
1921 low_direction = 0x5b;
1923 /* initialize low byte for jtag */
1924 buf[0] = 0x80; /* command "set data bits low byte" */
1925 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1926 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1927 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1929 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1931 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1932 return ERROR_JTAG_INIT_FAILED;
1938 high_direction = 0x0C;
1940 /* initialize high port */
1941 buf[0] = 0x82; /* command "set data bits high byte" */
1942 buf[1] = high_output;
1943 buf[2] = high_direction;
1944 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1946 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1948 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1949 return ERROR_JTAG_INIT_FAILED;
1955 int comstick_init(void)
1961 low_direction = 0x0b;
1963 /* initialize low byte for jtag */
1964 buf[0] = 0x80; /* command "set data bits low byte" */
1965 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1966 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1967 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1969 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1971 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1972 return ERROR_JTAG_INIT_FAILED;
1976 nTRSTnOE = 0x00; /* no output enable for nTRST */
1978 nSRSTnOE = 0x00; /* no output enable for nSRST */
1981 high_direction = 0x03;
1983 /* initialize high port */
1984 buf[0] = 0x82; /* command "set data bits high byte" */
1985 buf[1] = high_output;
1986 buf[2] = high_direction;
1987 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1989 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1991 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1992 return ERROR_JTAG_INIT_FAILED;
1998 int stm32stick_init(void)
2004 low_direction = 0x8b;
2006 /* initialize low byte for jtag */
2007 buf[0] = 0x80; /* command "set data bits low byte" */
2008 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2009 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2010 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2012 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2014 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2015 return ERROR_JTAG_INIT_FAILED;
2019 nTRSTnOE = 0x00; /* no output enable for nTRST */
2021 nSRSTnOE = 0x00; /* no output enable for nSRST */
2024 high_direction = 0x03;
2026 /* initialize high port */
2027 buf[0] = 0x82; /* command "set data bits high byte" */
2028 buf[1] = high_output;
2029 buf[2] = high_direction;
2030 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2032 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2034 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2035 return ERROR_JTAG_INIT_FAILED;
2041 void olimex_jtag_blink(void)
2043 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2044 * ACBUS3 is bit 3 of the GPIOH port
2046 if (high_output & 0x08)
2048 /* set port pin high */
2049 high_output &= 0x07;
2053 /* set port pin low */
2054 high_output |= 0x08;
2058 BUFFER_ADD = high_output;
2059 BUFFER_ADD = high_direction;
2062 void turtle_jtag_blink(void)
2065 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2067 if (high_output & 0x08)
2077 BUFFER_ADD = high_output;
2078 BUFFER_ADD = high_direction;
2081 int ft2232_quit(void)
2083 #if BUILD_FT2232_FTD2XX == 1
2086 status = FT_Close(ftdih);
2087 #elif BUILD_FT2232_LIBFTDI == 1
2088 ftdi_disable_bitbang(&ftdic);
2090 ftdi_usb_close(&ftdic);
2092 ftdi_deinit(&ftdic);
2095 free(ft2232_buffer);
2096 ft2232_buffer = NULL;
2101 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2105 ft2232_device_desc = strdup(args[0]);
2109 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2115 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2119 ft2232_serial = strdup(args[0]);
2123 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2129 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2134 ft2232_layout = malloc(strlen(args[0]) + 1);
2135 strcpy(ft2232_layout, args[0]);
2140 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2144 if (argc > MAX_USB_IDS*2) {
2145 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2146 "(maximum is %d pairs)", MAX_USB_IDS);
2147 argc = MAX_USB_IDS*2;
2149 if (argc < 2 || (argc & 1))
2151 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2156 for (i = 0; i+1 < argc; i += 2) {
2157 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2158 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2161 * Explicitly terminate, in case there are multiples instances of
2164 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2169 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2173 ft2232_latency = atoi(args[0]);
2177 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");