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
52 /* enable this to debug io latency
55 #define _DEBUG_USB_IO_
58 /* enable this to debug communication
61 #define _DEBUG_USB_COMMS_
64 int ft2232_execute_queue(void);
66 int ft2232_speed(int speed);
67 int ft2232_register_commands(struct command_context_s *cmd_ctx);
68 int ft2232_init(void);
69 int ft2232_quit(void);
71 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 char *ft2232_device_desc = NULL;
78 char *ft2232_serial = NULL;
79 char *ft2232_layout = NULL;
80 unsigned char ft2232_latency = 2;
83 /* vid = pid = 0 marks the end of the list */
84 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
85 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
87 typedef struct ft2232_layout_s
91 void(*reset)(int trst, int srst);
95 /* init procedures for supported layouts */
96 int usbjtag_init(void);
97 int jtagkey_init(void);
98 int olimex_jtag_init(void);
99 int flyswatter_init(void);
100 int turtle_init(void);
101 int comstick_init(void);
102 int stm32stick_init(void);
104 /* reset procedures for supported layouts */
105 void usbjtag_reset(int trst, int srst);
106 void jtagkey_reset(int trst, int srst);
107 void olimex_jtag_reset(int trst, int srst);
108 void flyswatter_reset(int trst, int srst);
109 void turtle_reset(int trst, int srst);
110 void comstick_reset(int trst, int srst);
111 void stm32stick_reset(int trst, int srst);
113 /* blink procedures for layouts that support a blinking led */
114 void olimex_jtag_blink(void);
115 void turtle_jtag_blink(void);
117 ft2232_layout_t ft2232_layouts[] =
119 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
120 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
121 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
122 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
123 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
124 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
125 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
126 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
127 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
128 {"comstick", comstick_init, comstick_reset, NULL},
129 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
133 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
135 static ft2232_layout_t *layout;
136 static u8 low_output = 0x0;
137 static u8 low_direction = 0x0;
138 static u8 high_output = 0x0;
139 static u8 high_direction = 0x0;
141 #if BUILD_FT2232_FTD2XX == 1
142 static FT_HANDLE ftdih = NULL;
143 #elif BUILD_FT2232_LIBFTDI == 1
144 static struct ftdi_context ftdic;
147 static u8 *ft2232_buffer = NULL;
148 static int ft2232_buffer_size = 0;
149 static int ft2232_read_pointer = 0;
150 static int ft2232_expect_read = 0;
151 #define FT2232_BUFFER_SIZE 131072
152 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
153 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
155 jtag_interface_t ft2232_interface =
160 .execute_queue = ft2232_execute_queue,
162 .speed = ft2232_speed,
163 .register_commands = ft2232_register_commands,
168 int ft2232_write(u8 *buf, int size, u32* bytes_written)
170 #if BUILD_FT2232_FTD2XX == 1
172 DWORD dw_bytes_written;
173 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
175 *bytes_written = dw_bytes_written;
176 LOG_ERROR("FT_Write returned: %lu", status);
177 return ERROR_JTAG_DEVICE_ERROR;
181 *bytes_written = dw_bytes_written;
184 #elif BUILD_FT2232_LIBFTDI == 1
186 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
189 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
190 return ERROR_JTAG_DEVICE_ERROR;
194 *bytes_written = retval;
200 int ft2232_read(u8* buf, int size, u32* bytes_read)
202 #if BUILD_FT2232_FTD2XX == 1
208 while ((*bytes_read < size) && timeout--)
210 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
211 *bytes_read, &dw_bytes_read)) != FT_OK)
214 LOG_ERROR("FT_Read returned: %lu", status);
215 return ERROR_JTAG_DEVICE_ERROR;
217 *bytes_read += dw_bytes_read;
219 #elif BUILD_FT2232_LIBFTDI == 1
224 while ((*bytes_read < size) && timeout--)
226 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
229 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
230 return ERROR_JTAG_DEVICE_ERROR;
232 *bytes_read += retval;
236 if (*bytes_read < size)
238 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
239 return ERROR_JTAG_DEVICE_ERROR;
245 int ft2232_speed(int speed)
251 buf[0] = 0x86; /* command "set divisor" */
252 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
253 buf[2] = (speed >> 8) & 0xff; /* valueH */
255 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
256 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
258 LOG_ERROR("couldn't set FT2232 TCK speed");
267 int ft2232_register_commands(struct command_context_s *cmd_ctx)
269 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
270 COMMAND_CONFIG, NULL);
271 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
272 COMMAND_CONFIG, NULL);
273 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
274 COMMAND_CONFIG, NULL);
275 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
276 COMMAND_CONFIG, NULL);
277 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
278 COMMAND_CONFIG, NULL);
282 void ft2232_end_state(enum tap_state state)
284 if (tap_move_map[state] != -1)
288 LOG_ERROR("BUG: %i is not a valid end state", state);
293 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
295 int num_bytes = ((scan_size + 7) / 8);
296 int bits_left = scan_size;
299 while(num_bytes-- > 1)
301 buffer[cur_byte] = BUFFER_READ;
306 buffer[cur_byte] = 0x0;
310 buffer[cur_byte] = BUFFER_READ >> 1;
313 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
317 void ft2232_debug_dump_buffer(void)
323 for (i = 0; i < ft2232_buffer_size; i++)
325 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
328 LOG_DEBUG("%s", line);
334 LOG_DEBUG("%s", line);
337 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
347 #ifdef _DEBUG_USB_IO_
348 struct timeval start, inter, inter2, end;
349 struct timeval d_inter, d_inter2, d_end;
352 #ifdef _DEBUG_USB_COMMS_
353 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
354 ft2232_debug_dump_buffer();
357 #ifdef _DEBUG_USB_IO_
358 gettimeofday(&start, NULL);
361 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
363 LOG_ERROR("couldn't write MPSSE commands to FT2232");
367 #ifdef _DEBUG_USB_IO_
368 gettimeofday(&inter, NULL);
371 if (ft2232_expect_read)
374 ft2232_buffer_size = 0;
376 #ifdef _DEBUG_USB_IO_
377 gettimeofday(&inter2, NULL);
380 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
382 LOG_ERROR("couldn't read from FT2232");
386 #ifdef _DEBUG_USB_IO_
387 gettimeofday(&end, NULL);
389 timeval_subtract(&d_inter, &inter, &start);
390 timeval_subtract(&d_inter2, &inter2, &start);
391 timeval_subtract(&d_end, &end, &start);
393 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);
397 ft2232_buffer_size = bytes_read;
399 if (ft2232_expect_read != ft2232_buffer_size)
401 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
402 ft2232_debug_dump_buffer();
407 #ifdef _DEBUG_USB_COMMS_
408 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
409 ft2232_debug_dump_buffer();
413 ft2232_expect_read = 0;
414 ft2232_read_pointer = 0;
416 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
417 * that wasn't handled by a caller-provided error handler
427 type = jtag_scan_type(cmd->cmd.scan);
428 if (type != SCAN_OUT)
430 scan_size = jtag_scan_size(cmd->cmd.scan);
431 buffer = calloc(CEIL(scan_size, 8), 1);
432 ft2232_read_scan(type, buffer, scan_size);
433 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
434 retval = ERROR_JTAG_QUEUE_FAILED;
444 ft2232_buffer_size = 0;
449 void ft2232_add_pathmove(pathmove_command_t *cmd)
451 int num_states = cmd->num_states;
461 int num_states_batch = num_states > 7 ? 7 : num_states;
463 /* command "Clock Data to TMS/CS Pin (no Read)" */
465 /* number of states remaining */
466 BUFFER_ADD = num_states_batch - 1;
468 while (num_states_batch--)
470 if (tap_transitions[cur_state].low == cmd->path[state_count])
471 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
472 else if (tap_transitions[cur_state].high == cmd->path[state_count])
473 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
476 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
480 cur_state = cmd->path[state_count];
485 BUFFER_ADD = tms_byte;
488 end_state = cur_state;
491 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
493 int num_bytes = (scan_size + 7) / 8;
494 int bits_left = scan_size;
498 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
500 /* command "Clock Data to TMS/CS Pin (no Read)" */
507 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
512 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
515 /* LOG_DEBUG("added TMS scan (no read)"); */
518 /* add command for complete bytes */
519 while (num_bytes > 1)
524 /* Clock Data Bytes In and Out LSB First */
526 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
528 else if (type == SCAN_OUT)
530 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
532 /* LOG_DEBUG("added TDI bytes (o)"); */
534 else if (type == SCAN_IN)
536 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
538 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
540 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
541 num_bytes -= thisrun_bytes;
542 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
543 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
546 /* add complete bytes */
547 while(thisrun_bytes-- > 0)
549 BUFFER_ADD = buffer[cur_byte];
554 else /* (type == SCAN_IN) */
556 bits_left -= 8 * (thisrun_bytes);
560 /* the most signifcant bit is scanned during TAP movement */
562 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
566 /* process remaining bits but the last one */
571 /* Clock Data Bits In and Out LSB First */
573 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
575 else if (type == SCAN_OUT)
577 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
579 /* LOG_DEBUG("added TDI bits (o)"); */
581 else if (type == SCAN_IN)
583 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
585 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
587 BUFFER_ADD = bits_left - 2;
589 BUFFER_ADD = buffer[cur_byte];
592 if ((ir_scan && (end_state == TAP_SI)) ||
593 (!ir_scan && (end_state == TAP_SD)))
597 /* Clock Data Bits In and Out LSB First */
599 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
601 else if (type == SCAN_OUT)
603 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
605 /* LOG_DEBUG("added TDI bits (o)"); */
607 else if (type == SCAN_IN)
609 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
611 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
614 BUFFER_ADD = last_bit;
618 /* move from Shift-IR/DR to end state */
619 if (type != SCAN_OUT)
621 /* Clock Data to TMS/CS Pin with Read */
623 /* LOG_DEBUG("added TMS scan (read)"); */
627 /* Clock Data to TMS/CS Pin (no Read) */
629 /* LOG_DEBUG("added TMS scan (no read)"); */
632 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
633 cur_state = end_state;
637 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
639 int num_bytes = (scan_size + 7) / 8;
640 int bits_left = scan_size;
643 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
644 u8 *receive_pointer = receive_buffer;
648 int thisrun_read = 0;
652 LOG_ERROR("BUG: large IR scans are not supported");
656 if (cur_state != TAP_SD)
658 /* command "Clock Data to TMS/CS Pin (no Read)" */
663 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
667 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
669 LOG_ERROR("couldn't write MPSSE commands to FT2232");
672 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
673 ft2232_buffer_size = 0;
675 /* add command for complete bytes */
676 while (num_bytes > 1)
682 /* Clock Data Bytes In and Out LSB First */
684 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
686 else if (type == SCAN_OUT)
688 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
690 /* LOG_DEBUG("added TDI bytes (o)"); */
692 else if (type == SCAN_IN)
694 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
696 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
698 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
699 thisrun_read = thisrun_bytes;
700 num_bytes -= thisrun_bytes;
701 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
702 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
705 /* add complete bytes */
706 while(thisrun_bytes-- > 0)
708 BUFFER_ADD = buffer[cur_byte];
713 else /* (type == SCAN_IN) */
715 bits_left -= 8 * (thisrun_bytes);
718 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
720 LOG_ERROR("couldn't write MPSSE commands to FT2232");
723 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
724 ft2232_buffer_size = 0;
726 if (type != SCAN_OUT)
728 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
730 LOG_ERROR("couldn't read from FT2232");
733 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
734 receive_pointer += bytes_read;
740 /* the most signifcant bit is scanned during TAP movement */
742 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
746 /* process remaining bits but the last one */
751 /* Clock Data Bits In and Out LSB First */
753 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
755 else if (type == SCAN_OUT)
757 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
759 /* LOG_DEBUG("added TDI bits (o)"); */
761 else if (type == SCAN_IN)
763 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
765 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
767 BUFFER_ADD = bits_left - 2;
769 BUFFER_ADD = buffer[cur_byte];
771 if (type != SCAN_OUT)
775 if (end_state == TAP_SD)
779 /* Clock Data Bits In and Out LSB First */
781 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
783 else if (type == SCAN_OUT)
785 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
787 /* LOG_DEBUG("added TDI bits (o)"); */
789 else if (type == SCAN_IN)
791 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
793 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
796 BUFFER_ADD = last_bit;
800 /* move from Shift-IR/DR to end state */
801 if (type != SCAN_OUT)
803 /* Clock Data to TMS/CS Pin with Read */
805 /* LOG_DEBUG("added TMS scan (read)"); */
809 /* Clock Data to TMS/CS Pin (no Read) */
811 /* LOG_DEBUG("added TMS scan (no read)"); */
814 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
815 cur_state = end_state;
818 if (type != SCAN_OUT)
821 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
823 LOG_ERROR("couldn't write MPSSE commands to FT2232");
826 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
827 ft2232_buffer_size = 0;
829 if (type != SCAN_OUT)
831 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
833 LOG_ERROR("couldn't read from FT2232");
836 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
837 receive_pointer += bytes_read;
843 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
845 int predicted_size = 3;
846 int num_bytes = (scan_size - 1) / 8;
848 if (cur_state != TAP_SD)
851 if (type == SCAN_IN) /* only from device to host */
854 predicted_size += (CEIL(num_bytes, 65536)) * 3;
855 /* remaining bits - 1 (up to 7) */
856 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
858 else /* host to device, or bidirectional */
861 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
862 /* remaining bits -1 (up to 7) */
863 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
866 return predicted_size;
869 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
871 int predicted_size = 0;
873 if (type != SCAN_OUT)
876 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
877 /* remaining bits - 1 */
878 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
879 /* last bit (from TMS scan) */
883 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
885 return predicted_size;
888 void usbjtag_reset(int trst, int srst)
893 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
894 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
896 low_output &= ~nTRST; /* switch output low */
900 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
901 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
903 low_output |= nTRST; /* switch output high */
908 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
909 low_output &= ~nSRST; /* switch output low */
911 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
915 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
916 low_output |= nSRST; /* switch output high */
918 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
921 /* command "set data bits low byte" */
923 BUFFER_ADD = low_output;
924 BUFFER_ADD = low_direction;
928 void jtagkey_reset(int trst, int srst)
933 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
934 high_output &= ~nTRSTnOE;
936 high_output &= ~nTRST;
940 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
941 high_output |= nTRSTnOE;
943 high_output |= nTRST;
948 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
949 high_output &= ~nSRST;
951 high_output &= ~nSRSTnOE;
955 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
956 high_output |= nSRST;
958 high_output |= nSRSTnOE;
961 /* command "set data bits high byte" */
963 BUFFER_ADD = high_output;
964 BUFFER_ADD = high_direction;
965 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
968 void olimex_jtag_reset(int trst, int srst)
973 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
974 high_output &= ~nTRSTnOE;
976 high_output &= ~nTRST;
980 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
981 high_output |= nTRSTnOE;
983 high_output |= nTRST;
988 high_output |= nSRST;
992 high_output &= ~nSRST;
995 /* command "set data bits high byte" */
997 BUFFER_ADD = high_output;
998 BUFFER_ADD = high_direction;
999 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1002 void flyswatter_reset(int trst, int srst)
1006 cur_state = TAP_TLR;
1007 low_output &= ~nTRST;
1011 low_output |= nTRST;
1016 low_output |= nSRST;
1020 low_output &= ~nSRST;
1023 /* command "set data bits low byte" */
1025 BUFFER_ADD = low_output;
1026 BUFFER_ADD = low_direction;
1027 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1030 void turtle_reset(int trst, int srst)
1036 low_output |= nSRST;
1040 low_output &= ~nSRST;
1043 /* command "set data bits low byte" */
1045 BUFFER_ADD = low_output;
1046 BUFFER_ADD = low_direction;
1047 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1050 void comstick_reset(int trst, int srst)
1054 cur_state = TAP_TLR;
1055 high_output &= ~nTRST;
1059 high_output |= nTRST;
1064 high_output &= ~nSRST;
1068 high_output |= nSRST;
1071 /* command "set data bits high byte" */
1073 BUFFER_ADD = high_output;
1074 BUFFER_ADD = high_direction;
1075 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1078 void stm32stick_reset(int trst, int srst)
1082 cur_state = TAP_TLR;
1083 high_output &= ~nTRST;
1087 high_output |= nTRST;
1092 low_output &= ~nSRST;
1096 low_output |= nSRST;
1099 /* command "set data bits low byte" */
1101 BUFFER_ADD = low_output;
1102 BUFFER_ADD = low_direction;
1104 /* command "set data bits high byte" */
1106 BUFFER_ADD = high_output;
1107 BUFFER_ADD = high_direction;
1108 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1111 int ft2232_execute_queue()
1113 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1114 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1116 int scan_size; /* size of IR or DR scan */
1117 enum scan_type type;
1119 int predicted_size = 0;
1120 int require_send = 0;
1123 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1124 * that wasn't handled by a caller-provided error handler
1128 ft2232_buffer_size = 0;
1129 ft2232_expect_read = 0;
1131 /* blink, if the current layout has that feature */
1139 case JTAG_END_STATE:
1140 if (cmd->cmd.end_state->end_state != -1)
1141 ft2232_end_state(cmd->cmd.end_state->end_state);
1144 /* only send the maximum buffer size that FT2232C can handle */
1146 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1148 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1149 retval = ERROR_JTAG_QUEUE_FAILED;
1154 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1157 #ifdef _DEBUG_JTAG_IO_
1158 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1162 /* only send the maximum buffer size that FT2232C can handle */
1164 if (cur_state != TAP_RTI)
1165 predicted_size += 3;
1166 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1167 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1168 predicted_size += 3;
1169 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1170 predicted_size += 3;
1171 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1173 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1174 retval = ERROR_JTAG_QUEUE_FAILED;
1178 if (cur_state != TAP_RTI)
1180 /* command "Clock Data to TMS/CS Pin (no Read)" */
1185 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1186 cur_state = TAP_RTI;
1189 i = cmd->cmd.runtest->num_cycles;
1192 /* command "Clock Data to TMS/CS Pin (no Read)" */
1195 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1198 cur_state = TAP_RTI;
1199 i -= (i > 7) ? 7 : i;
1200 /* LOG_DEBUG("added TMS scan (no read)"); */
1202 if (cmd->cmd.runtest->end_state != -1)
1203 ft2232_end_state(cmd->cmd.runtest->end_state);
1204 if (cur_state != end_state)
1206 /* command "Clock Data to TMS/CS Pin (no Read)" */
1211 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1212 cur_state = end_state;
1213 /* LOG_DEBUG("added TMS scan (no read)"); */
1216 #ifdef _DEBUG_JTAG_IO_
1217 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1220 case JTAG_STATEMOVE:
1221 /* only send the maximum buffer size that FT2232C can handle */
1223 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1225 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1226 retval = ERROR_JTAG_QUEUE_FAILED;
1230 if (cmd->cmd.statemove->end_state != -1)
1231 ft2232_end_state(cmd->cmd.statemove->end_state);
1232 /* command "Clock Data to TMS/CS Pin (no Read)" */
1237 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1238 /* LOG_DEBUG("added TMS scan (no read)"); */
1239 cur_state = end_state;
1241 #ifdef _DEBUG_JTAG_IO_
1242 LOG_DEBUG("statemove: %i", end_state);
1246 /* only send the maximum buffer size that FT2232C can handle */
1247 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1248 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1250 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1251 retval = ERROR_JTAG_QUEUE_FAILED;
1255 ft2232_add_pathmove(cmd->cmd.pathmove);
1257 #ifdef _DEBUG_JTAG_IO_
1258 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1262 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1263 type = jtag_scan_type(cmd->cmd.scan);
1264 predicted_size = ft2232_predict_scan_out(scan_size, type);
1265 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1267 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1268 /* unsent commands before this */
1269 if (first_unsent != cmd)
1270 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1271 retval = ERROR_JTAG_QUEUE_FAILED;
1273 /* current command */
1274 if (cmd->cmd.scan->end_state != -1)
1275 ft2232_end_state(cmd->cmd.scan->end_state);
1276 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1278 first_unsent = cmd->next;
1283 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1285 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1286 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1287 retval = ERROR_JTAG_QUEUE_FAILED;
1291 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1292 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1293 if (cmd->cmd.scan->end_state != -1)
1294 ft2232_end_state(cmd->cmd.scan->end_state);
1295 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1299 #ifdef _DEBUG_JTAG_IO_
1300 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1304 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1305 retval = ERROR_JTAG_QUEUE_FAILED;
1306 first_unsent = cmd->next;
1307 jtag_sleep(cmd->cmd.sleep->us);
1308 #ifdef _DEBUG_JTAG_IO_
1309 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1313 LOG_ERROR("BUG: unknown JTAG command type encountered");
1319 if (require_send > 0)
1320 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1321 retval = ERROR_JTAG_QUEUE_FAILED;
1326 #if BUILD_FT2232_FTD2XX == 1
1327 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1330 DWORD openex_flags = 0;
1331 char *openex_string = NULL;
1334 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1335 ft2232_layout, vid, pid);
1338 /* Add non-standard Vid/Pid to the linux driver */
1339 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1341 LOG_WARNING("couldn't add %4.4x:%4.4x",
1346 if (ft2232_device_desc && ft2232_serial)
1348 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1349 ft2232_device_desc = NULL;
1352 if (ft2232_device_desc)
1354 openex_string = ft2232_device_desc;
1355 openex_flags = FT_OPEN_BY_DESCRIPTION;
1357 else if (ft2232_serial)
1359 openex_string = ft2232_serial;
1360 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1364 LOG_ERROR("neither device description nor serial number specified");
1365 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1367 return ERROR_JTAG_INIT_FAILED;
1370 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1375 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1378 return ERROR_JTAG_INIT_FAILED;
1380 LOG_ERROR("unable to open ftdi device: %lu", status);
1381 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1382 if (status == FT_OK)
1384 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1387 for (i = 0; i < num_devices; i++)
1388 desc_array[i] = malloc(64);
1389 desc_array[num_devices] = NULL;
1391 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1393 if (status == FT_OK)
1395 LOG_ERROR("ListDevices: %lu\n", num_devices);
1396 for (i = 0; i < num_devices; i++)
1397 LOG_ERROR("%i: %s", i, desc_array[i]);
1400 for (i = 0; i < num_devices; i++)
1401 free(desc_array[i]);
1406 LOG_ERROR("ListDevices: NONE\n");
1408 return ERROR_JTAG_INIT_FAILED;
1411 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1413 LOG_ERROR("unable to set latency timer: %lu", status);
1414 return ERROR_JTAG_INIT_FAILED;
1417 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1419 LOG_ERROR("unable to get latency timer: %lu", status);
1420 return ERROR_JTAG_INIT_FAILED;
1424 LOG_DEBUG("current latency timer: %i", latency_timer);
1427 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1429 LOG_ERROR("unable to set timeouts: %lu", status);
1430 return ERROR_JTAG_INIT_FAILED;
1433 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1435 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1436 return ERROR_JTAG_INIT_FAILED;
1442 static int ft2232_purge_ftd2xx(void)
1446 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1448 LOG_ERROR("error purging ftd2xx device: %lu", status);
1449 return ERROR_JTAG_INIT_FAILED;
1454 #endif /* BUILD_FT2232_FTD2XX == 1 */
1456 #if BUILD_FT2232_LIBFTDI == 1
1457 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1461 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1462 ft2232_layout, vid, pid);
1464 if (ftdi_init(&ftdic) < 0)
1465 return ERROR_JTAG_INIT_FAILED;
1467 /* context, vendor id, product id */
1468 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1469 ft2232_serial) < 0) {
1471 LOG_WARNING("unable to open ftdi device (trying more): %s",
1474 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1476 return ERROR_JTAG_INIT_FAILED;
1479 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1481 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1482 return ERROR_JTAG_INIT_FAILED;
1485 if (ftdi_usb_reset(&ftdic) < 0)
1487 LOG_ERROR("unable to reset ftdi device");
1488 return ERROR_JTAG_INIT_FAILED;
1491 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1493 LOG_ERROR("unable to set latency timer");
1494 return ERROR_JTAG_INIT_FAILED;
1497 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1499 LOG_ERROR("unable to get latency timer");
1500 return ERROR_JTAG_INIT_FAILED;
1504 LOG_DEBUG("current latency timer: %i", latency_timer);
1507 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1512 static int ft2232_purge_libftdi(void)
1514 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1516 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1517 return ERROR_JTAG_INIT_FAILED;
1522 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1524 int ft2232_init(void)
1529 ft2232_layout_t *cur_layout = ft2232_layouts;
1532 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1534 ft2232_layout = "usbjtag";
1535 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1538 while (cur_layout->name)
1540 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1542 layout = cur_layout;
1550 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1551 return ERROR_JTAG_INIT_FAILED;
1554 for (i = 0; 1; i++) {
1556 * "more indicates that there are more IDs to try, so we should
1557 * not print an error for an ID mismatch (but for anything
1560 * try_more indicates that the error code returned indicates an
1561 * ID mismatch (and nothing else) and that we should proceeed
1562 * with the next ID pair.
1564 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1567 #if BUILD_FT2232_FTD2XX == 1
1568 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1570 #elif BUILD_FT2232_LIBFTDI == 1
1571 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1576 if (!more || !try_more)
1580 ft2232_buffer_size = 0;
1581 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1583 if (layout->init() != ERROR_OK)
1584 return ERROR_JTAG_INIT_FAILED;
1586 ft2232_speed(jtag_speed);
1588 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1589 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1591 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1592 return ERROR_JTAG_INIT_FAILED;
1595 #if BUILD_FT2232_FTD2XX == 1
1596 return ft2232_purge_ftd2xx();
1597 #elif BUILD_FT2232_LIBFTDI == 1
1598 return ft2232_purge_libftdi();
1604 int usbjtag_init(void)
1610 low_direction = 0x0b;
1612 if (strcmp(ft2232_layout, "usbjtag") == 0)
1619 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1626 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1633 low_direction = 0x8b;
1637 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1638 return ERROR_JTAG_INIT_FAILED;
1641 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1643 low_direction &= ~nTRSTnOE; /* nTRST input */
1644 low_output &= ~nTRST; /* nTRST = 0 */
1648 low_direction |= nTRSTnOE; /* nTRST output */
1649 low_output |= nTRST; /* nTRST = 1 */
1652 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1654 low_direction |= nSRSTnOE; /* nSRST output */
1655 low_output |= nSRST; /* nSRST = 1 */
1659 low_direction &= ~nSRSTnOE; /* nSRST input */
1660 low_output &= ~nSRST; /* nSRST = 0 */
1663 /* initialize low byte for jtag */
1664 buf[0] = 0x80; /* command "set data bits low byte" */
1665 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1666 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1667 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1669 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1671 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1672 return ERROR_JTAG_INIT_FAILED;
1678 int jtagkey_init(void)
1684 low_direction = 0x1b;
1686 /* initialize low byte for jtag */
1687 buf[0] = 0x80; /* command "set data bits low byte" */
1688 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1689 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1690 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1692 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1694 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1695 return ERROR_JTAG_INIT_FAILED;
1698 if (strcmp(layout->name, "jtagkey") == 0)
1705 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1706 (strcmp(layout->name, "oocdlink") == 0))
1715 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1720 high_direction = 0x0f;
1722 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1724 high_output |= nTRSTnOE;
1725 high_output &= ~nTRST;
1729 high_output &= ~nTRSTnOE;
1730 high_output |= nTRST;
1733 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1735 high_output &= ~nSRSTnOE;
1736 high_output |= nSRST;
1740 high_output |= nSRSTnOE;
1741 high_output &= ~nSRST;
1744 /* initialize high port */
1745 buf[0] = 0x82; /* command "set data bits high byte" */
1746 buf[1] = high_output; /* value */
1747 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1748 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1750 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1752 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1753 return ERROR_JTAG_INIT_FAILED;
1759 int olimex_jtag_init(void)
1765 low_direction = 0x1b;
1767 /* initialize low byte for jtag */
1768 buf[0] = 0x80; /* command "set data bits low byte" */
1769 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1770 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1771 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1773 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1775 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1776 return ERROR_JTAG_INIT_FAILED;
1782 nSRSTnOE = 0x00; /* no output enable for nSRST */
1785 high_direction = 0x0f;
1787 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1789 high_output |= nTRSTnOE;
1790 high_output &= ~nTRST;
1794 high_output &= ~nTRSTnOE;
1795 high_output |= nTRST;
1798 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1800 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1804 high_output &= ~nSRST;
1807 /* turn red LED on */
1808 high_output |= 0x08;
1810 /* initialize high port */
1811 buf[0] = 0x82; /* command "set data bits high byte" */
1812 buf[1] = high_output; /* value */
1813 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1814 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1816 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1818 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1819 return ERROR_JTAG_INIT_FAILED;
1825 int flyswatter_init(void)
1831 low_direction = 0xfb;
1833 /* initialize low byte for jtag */
1834 buf[0] = 0x80; /* command "set data bits low byte" */
1835 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1836 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1837 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1839 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1841 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1842 return ERROR_JTAG_INIT_FAILED;
1846 nTRSTnOE = 0x0; /* not output enable for nTRST */
1848 nSRSTnOE = 0x00; /* no output enable for nSRST */
1851 high_direction = 0x0c;
1853 /* turn red LED1 on, LED2 off */
1854 high_output |= 0x08;
1856 /* initialize high port */
1857 buf[0] = 0x82; /* command "set data bits high byte" */
1858 buf[1] = high_output; /* value */
1859 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1860 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1862 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1864 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1865 return ERROR_JTAG_INIT_FAILED;
1871 int turtle_init(void)
1877 low_direction = 0x5b;
1879 /* initialize low byte for jtag */
1880 buf[0] = 0x80; /* command "set data bits low byte" */
1881 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1882 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1883 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1885 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1887 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1888 return ERROR_JTAG_INIT_FAILED;
1894 high_direction = 0x0C;
1896 /* initialize high port */
1897 buf[0] = 0x82; /* command "set data bits high byte" */
1898 buf[1] = high_output;
1899 buf[2] = high_direction;
1900 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1902 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1904 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1905 return ERROR_JTAG_INIT_FAILED;
1911 int comstick_init(void)
1917 low_direction = 0x0b;
1919 /* initialize low byte for jtag */
1920 buf[0] = 0x80; /* command "set data bits low byte" */
1921 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1922 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1923 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1925 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1927 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1928 return ERROR_JTAG_INIT_FAILED;
1932 nTRSTnOE = 0x00; /* no output enable for nTRST */
1934 nSRSTnOE = 0x00; /* no output enable for nSRST */
1937 high_direction = 0x03;
1939 /* initialize high port */
1940 buf[0] = 0x82; /* command "set data bits high byte" */
1941 buf[1] = high_output;
1942 buf[2] = high_direction;
1943 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1945 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1947 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1948 return ERROR_JTAG_INIT_FAILED;
1954 int stm32stick_init(void)
1960 low_direction = 0x8b;
1962 /* initialize low byte for jtag */
1963 buf[0] = 0x80; /* command "set data bits low byte" */
1964 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1965 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1966 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1968 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1970 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1971 return ERROR_JTAG_INIT_FAILED;
1975 nTRSTnOE = 0x00; /* no output enable for nTRST */
1977 nSRSTnOE = 0x00; /* no output enable for nSRST */
1980 high_direction = 0x03;
1982 /* initialize high port */
1983 buf[0] = 0x82; /* command "set data bits high byte" */
1984 buf[1] = high_output;
1985 buf[2] = high_direction;
1986 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1988 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1990 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1991 return ERROR_JTAG_INIT_FAILED;
1997 void olimex_jtag_blink(void)
1999 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2000 * ACBUS3 is bit 3 of the GPIOH port
2002 if (high_output & 0x08)
2004 /* set port pin high */
2005 high_output &= 0x07;
2009 /* set port pin low */
2010 high_output |= 0x08;
2014 BUFFER_ADD = high_output;
2015 BUFFER_ADD = high_direction;
2018 void turtle_jtag_blink(void)
2021 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2023 if (high_output & 0x08)
2033 BUFFER_ADD = high_output;
2034 BUFFER_ADD = high_direction;
2038 int ft2232_quit(void)
2040 #if BUILD_FT2232_FTD2XX == 1
2043 status = FT_Close(ftdih);
2044 #elif BUILD_FT2232_LIBFTDI == 1
2045 ftdi_disable_bitbang(&ftdic);
2047 ftdi_usb_close(&ftdic);
2049 ftdi_deinit(&ftdic);
2052 free(ft2232_buffer);
2053 ft2232_buffer = NULL;
2058 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2062 ft2232_device_desc = strdup(args[0]);
2066 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2072 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2076 ft2232_serial = strdup(args[0]);
2080 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2086 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2091 ft2232_layout = malloc(strlen(args[0]) + 1);
2092 strcpy(ft2232_layout, args[0]);
2097 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2101 if (argc > MAX_USB_IDS*2) {
2102 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2103 "(maximum is %d pairs)", MAX_USB_IDS);
2104 argc = MAX_USB_IDS*2;
2106 if (argc < 2 || (argc & 1))
2108 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2113 for (i = 0; i+1 < argc; i += 2) {
2114 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2115 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2118 * Explicitly terminate, in case there are multiples instances of
2121 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2126 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2130 ft2232_latency = atoi(args[0]);
2134 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");