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)
892 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
893 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
895 low_output &= ~nTRST; /* switch output low */
899 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
900 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
902 low_output |= nTRST; /* switch output high */
907 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
908 low_output &= ~nSRST; /* switch output low */
910 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
914 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
915 low_output |= nSRST; /* switch output high */
917 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
920 /* command "set data bits low byte" */
922 BUFFER_ADD = low_output;
923 BUFFER_ADD = low_direction;
927 void jtagkey_reset(int trst, int srst)
931 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
932 high_output &= ~nTRSTnOE;
934 high_output &= ~nTRST;
938 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
939 high_output |= nTRSTnOE;
941 high_output |= nTRST;
946 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
947 high_output &= ~nSRST;
949 high_output &= ~nSRSTnOE;
953 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
954 high_output |= nSRST;
956 high_output |= nSRSTnOE;
959 /* command "set data bits high byte" */
961 BUFFER_ADD = high_output;
962 BUFFER_ADD = high_direction;
963 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
966 void olimex_jtag_reset(int trst, int srst)
970 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
971 high_output &= ~nTRSTnOE;
973 high_output &= ~nTRST;
977 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
978 high_output |= nTRSTnOE;
980 high_output |= nTRST;
985 high_output |= nSRST;
989 high_output &= ~nSRST;
992 /* command "set data bits high byte" */
994 BUFFER_ADD = high_output;
995 BUFFER_ADD = high_direction;
996 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
999 void flyswatter_reset(int trst, int srst)
1003 low_output &= ~nTRST;
1007 low_output |= nTRST;
1012 low_output |= nSRST;
1016 low_output &= ~nSRST;
1019 /* command "set data bits low byte" */
1021 BUFFER_ADD = low_output;
1022 BUFFER_ADD = low_direction;
1023 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1026 void turtle_reset(int trst, int srst)
1032 low_output |= nSRST;
1036 low_output &= ~nSRST;
1039 /* command "set data bits low byte" */
1041 BUFFER_ADD = low_output;
1042 BUFFER_ADD = low_direction;
1043 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1046 void comstick_reset(int trst, int srst)
1050 high_output &= ~nTRST;
1054 high_output |= nTRST;
1059 high_output &= ~nSRST;
1063 high_output |= nSRST;
1066 /* command "set data bits high byte" */
1068 BUFFER_ADD = high_output;
1069 BUFFER_ADD = high_direction;
1070 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1073 void stm32stick_reset(int trst, int srst)
1077 high_output &= ~nTRST;
1081 high_output |= nTRST;
1086 low_output &= ~nSRST;
1090 low_output |= nSRST;
1093 /* command "set data bits low byte" */
1095 BUFFER_ADD = low_output;
1096 BUFFER_ADD = low_direction;
1098 /* command "set data bits high byte" */
1100 BUFFER_ADD = high_output;
1101 BUFFER_ADD = high_direction;
1102 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1105 int ft2232_execute_queue()
1107 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1108 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1110 int scan_size; /* size of IR or DR scan */
1111 enum scan_type type;
1113 int predicted_size = 0;
1114 int require_send = 0;
1117 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1118 * that wasn't handled by a caller-provided error handler
1122 ft2232_buffer_size = 0;
1123 ft2232_expect_read = 0;
1125 /* blink, if the current layout has that feature */
1133 case JTAG_END_STATE:
1134 if (cmd->cmd.end_state->end_state != -1)
1135 ft2232_end_state(cmd->cmd.end_state->end_state);
1138 /* only send the maximum buffer size that FT2232C can handle */
1140 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1142 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1143 retval = ERROR_JTAG_QUEUE_FAILED;
1148 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1150 cur_state = TAP_TLR;
1152 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1155 #ifdef _DEBUG_JTAG_IO_
1156 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1160 /* only send the maximum buffer size that FT2232C can handle */
1162 if (cur_state != TAP_RTI)
1163 predicted_size += 3;
1164 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1165 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1166 predicted_size += 3;
1167 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1168 predicted_size += 3;
1169 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1171 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1172 retval = ERROR_JTAG_QUEUE_FAILED;
1176 if (cur_state != TAP_RTI)
1178 /* command "Clock Data to TMS/CS Pin (no Read)" */
1183 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1184 cur_state = TAP_RTI;
1187 i = cmd->cmd.runtest->num_cycles;
1190 /* command "Clock Data to TMS/CS Pin (no Read)" */
1193 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1196 cur_state = TAP_RTI;
1197 i -= (i > 7) ? 7 : i;
1198 /* LOG_DEBUG("added TMS scan (no read)"); */
1200 if (cmd->cmd.runtest->end_state != -1)
1201 ft2232_end_state(cmd->cmd.runtest->end_state);
1202 if (cur_state != end_state)
1204 /* command "Clock Data to TMS/CS Pin (no Read)" */
1209 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1210 cur_state = end_state;
1211 /* LOG_DEBUG("added TMS scan (no read)"); */
1214 #ifdef _DEBUG_JTAG_IO_
1215 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1218 case JTAG_STATEMOVE:
1219 /* only send the maximum buffer size that FT2232C can handle */
1221 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1223 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1224 retval = ERROR_JTAG_QUEUE_FAILED;
1228 if (cmd->cmd.statemove->end_state != -1)
1229 ft2232_end_state(cmd->cmd.statemove->end_state);
1230 /* command "Clock Data to TMS/CS Pin (no Read)" */
1235 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1236 /* LOG_DEBUG("added TMS scan (no read)"); */
1237 cur_state = end_state;
1239 #ifdef _DEBUG_JTAG_IO_
1240 LOG_DEBUG("statemove: %i", end_state);
1244 /* only send the maximum buffer size that FT2232C can handle */
1245 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1246 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1248 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1249 retval = ERROR_JTAG_QUEUE_FAILED;
1253 ft2232_add_pathmove(cmd->cmd.pathmove);
1255 #ifdef _DEBUG_JTAG_IO_
1256 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1260 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1261 type = jtag_scan_type(cmd->cmd.scan);
1262 predicted_size = ft2232_predict_scan_out(scan_size, type);
1263 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1265 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1266 /* unsent commands before this */
1267 if (first_unsent != cmd)
1268 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1269 retval = ERROR_JTAG_QUEUE_FAILED;
1271 /* current command */
1272 if (cmd->cmd.scan->end_state != -1)
1273 ft2232_end_state(cmd->cmd.scan->end_state);
1274 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1276 first_unsent = cmd->next;
1281 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1283 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1284 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1285 retval = ERROR_JTAG_QUEUE_FAILED;
1289 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1290 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1291 if (cmd->cmd.scan->end_state != -1)
1292 ft2232_end_state(cmd->cmd.scan->end_state);
1293 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1297 #ifdef _DEBUG_JTAG_IO_
1298 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1302 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1303 retval = ERROR_JTAG_QUEUE_FAILED;
1304 first_unsent = cmd->next;
1305 jtag_sleep(cmd->cmd.sleep->us);
1306 #ifdef _DEBUG_JTAG_IO_
1307 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1311 LOG_ERROR("BUG: unknown JTAG command type encountered");
1317 if (require_send > 0)
1318 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1319 retval = ERROR_JTAG_QUEUE_FAILED;
1324 #if BUILD_FT2232_FTD2XX == 1
1325 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1328 DWORD openex_flags = 0;
1329 char *openex_string = NULL;
1332 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1333 ft2232_layout, vid, pid);
1336 /* Add non-standard Vid/Pid to the linux driver */
1337 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1339 LOG_WARNING("couldn't add %4.4x:%4.4x",
1344 if (ft2232_device_desc && ft2232_serial)
1346 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1347 ft2232_device_desc = NULL;
1350 if (ft2232_device_desc)
1352 openex_string = ft2232_device_desc;
1353 openex_flags = FT_OPEN_BY_DESCRIPTION;
1355 else if (ft2232_serial)
1357 openex_string = ft2232_serial;
1358 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1362 LOG_ERROR("neither device description nor serial number specified");
1363 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1365 return ERROR_JTAG_INIT_FAILED;
1368 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1373 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1376 return ERROR_JTAG_INIT_FAILED;
1378 LOG_ERROR("unable to open ftdi device: %lu", status);
1379 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1380 if (status == FT_OK)
1382 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1385 for (i = 0; i < num_devices; i++)
1386 desc_array[i] = malloc(64);
1387 desc_array[num_devices] = NULL;
1389 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1391 if (status == FT_OK)
1393 LOG_ERROR("ListDevices: %lu\n", num_devices);
1394 for (i = 0; i < num_devices; i++)
1395 LOG_ERROR("%i: %s", i, desc_array[i]);
1398 for (i = 0; i < num_devices; i++)
1399 free(desc_array[i]);
1404 LOG_ERROR("ListDevices: NONE\n");
1406 return ERROR_JTAG_INIT_FAILED;
1409 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1411 LOG_ERROR("unable to set latency timer: %lu", status);
1412 return ERROR_JTAG_INIT_FAILED;
1415 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1417 LOG_ERROR("unable to get latency timer: %lu", status);
1418 return ERROR_JTAG_INIT_FAILED;
1422 LOG_DEBUG("current latency timer: %i", latency_timer);
1425 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1427 LOG_ERROR("unable to set timeouts: %lu", status);
1428 return ERROR_JTAG_INIT_FAILED;
1431 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1433 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1434 return ERROR_JTAG_INIT_FAILED;
1440 static int ft2232_purge_ftd2xx(void)
1444 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1446 LOG_ERROR("error purging ftd2xx device: %lu", status);
1447 return ERROR_JTAG_INIT_FAILED;
1452 #endif /* BUILD_FT2232_FTD2XX == 1 */
1454 #if BUILD_FT2232_LIBFTDI == 1
1455 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1459 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1460 ft2232_layout, vid, pid);
1462 if (ftdi_init(&ftdic) < 0)
1463 return ERROR_JTAG_INIT_FAILED;
1465 /* context, vendor id, product id */
1466 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1467 ft2232_serial) < 0) {
1469 LOG_WARNING("unable to open ftdi device (trying more): %s",
1472 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1474 return ERROR_JTAG_INIT_FAILED;
1477 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1479 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1480 return ERROR_JTAG_INIT_FAILED;
1483 if (ftdi_usb_reset(&ftdic) < 0)
1485 LOG_ERROR("unable to reset ftdi device");
1486 return ERROR_JTAG_INIT_FAILED;
1489 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1491 LOG_ERROR("unable to set latency timer");
1492 return ERROR_JTAG_INIT_FAILED;
1495 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1497 LOG_ERROR("unable to get latency timer");
1498 return ERROR_JTAG_INIT_FAILED;
1502 LOG_DEBUG("current latency timer: %i", latency_timer);
1505 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1510 static int ft2232_purge_libftdi(void)
1512 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1514 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1515 return ERROR_JTAG_INIT_FAILED;
1520 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1522 int ft2232_init(void)
1527 ft2232_layout_t *cur_layout = ft2232_layouts;
1530 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1532 ft2232_layout = "usbjtag";
1533 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1536 while (cur_layout->name)
1538 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1540 layout = cur_layout;
1548 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1549 return ERROR_JTAG_INIT_FAILED;
1552 for (i = 0; 1; i++) {
1554 * "more indicates that there are more IDs to try, so we should
1555 * not print an error for an ID mismatch (but for anything
1558 * try_more indicates that the error code returned indicates an
1559 * ID mismatch (and nothing else) and that we should proceeed
1560 * with the next ID pair.
1562 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1565 #if BUILD_FT2232_FTD2XX == 1
1566 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1568 #elif BUILD_FT2232_LIBFTDI == 1
1569 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1574 if (!more || !try_more)
1578 ft2232_buffer_size = 0;
1579 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1581 if (layout->init() != ERROR_OK)
1582 return ERROR_JTAG_INIT_FAILED;
1584 ft2232_speed(jtag_speed);
1586 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1587 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1589 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1590 return ERROR_JTAG_INIT_FAILED;
1593 #if BUILD_FT2232_FTD2XX == 1
1594 return ft2232_purge_ftd2xx();
1595 #elif BUILD_FT2232_LIBFTDI == 1
1596 return ft2232_purge_libftdi();
1602 int usbjtag_init(void)
1608 low_direction = 0x0b;
1610 if (strcmp(ft2232_layout, "usbjtag") == 0)
1617 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1624 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1631 low_direction = 0x8b;
1635 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1636 return ERROR_JTAG_INIT_FAILED;
1639 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1641 low_direction &= ~nTRSTnOE; /* nTRST input */
1642 low_output &= ~nTRST; /* nTRST = 0 */
1646 low_direction |= nTRSTnOE; /* nTRST output */
1647 low_output |= nTRST; /* nTRST = 1 */
1650 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1652 low_direction |= nSRSTnOE; /* nSRST output */
1653 low_output |= nSRST; /* nSRST = 1 */
1657 low_direction &= ~nSRSTnOE; /* nSRST input */
1658 low_output &= ~nSRST; /* nSRST = 0 */
1661 /* initialize low byte for jtag */
1662 buf[0] = 0x80; /* command "set data bits low byte" */
1663 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1664 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1665 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1667 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1669 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1670 return ERROR_JTAG_INIT_FAILED;
1676 int jtagkey_init(void)
1682 low_direction = 0x1b;
1684 /* initialize low byte for jtag */
1685 buf[0] = 0x80; /* command "set data bits low byte" */
1686 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1687 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1688 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1690 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1692 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1693 return ERROR_JTAG_INIT_FAILED;
1696 if (strcmp(layout->name, "jtagkey") == 0)
1703 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1704 (strcmp(layout->name, "oocdlink") == 0))
1713 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1718 high_direction = 0x0f;
1720 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1722 high_output |= nTRSTnOE;
1723 high_output &= ~nTRST;
1727 high_output &= ~nTRSTnOE;
1728 high_output |= nTRST;
1731 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1733 high_output &= ~nSRSTnOE;
1734 high_output |= nSRST;
1738 high_output |= nSRSTnOE;
1739 high_output &= ~nSRST;
1742 /* initialize high port */
1743 buf[0] = 0x82; /* command "set data bits high byte" */
1744 buf[1] = high_output; /* value */
1745 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1746 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1748 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1750 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1751 return ERROR_JTAG_INIT_FAILED;
1757 int olimex_jtag_init(void)
1763 low_direction = 0x1b;
1765 /* initialize low byte for jtag */
1766 buf[0] = 0x80; /* command "set data bits low byte" */
1767 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1768 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1769 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1771 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1773 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1774 return ERROR_JTAG_INIT_FAILED;
1780 nSRSTnOE = 0x00; /* no output enable for nSRST */
1783 high_direction = 0x0f;
1785 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1787 high_output |= nTRSTnOE;
1788 high_output &= ~nTRST;
1792 high_output &= ~nTRSTnOE;
1793 high_output |= nTRST;
1796 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1798 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1802 high_output &= ~nSRST;
1805 /* turn red LED on */
1806 high_output |= 0x08;
1808 /* initialize high port */
1809 buf[0] = 0x82; /* command "set data bits high byte" */
1810 buf[1] = high_output; /* value */
1811 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1812 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1814 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1816 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1817 return ERROR_JTAG_INIT_FAILED;
1823 int flyswatter_init(void)
1829 low_direction = 0xfb;
1831 /* initialize low byte for jtag */
1832 buf[0] = 0x80; /* command "set data bits low byte" */
1833 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1834 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1835 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1837 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1839 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1840 return ERROR_JTAG_INIT_FAILED;
1844 nTRSTnOE = 0x0; /* not output enable for nTRST */
1846 nSRSTnOE = 0x00; /* no output enable for nSRST */
1849 high_direction = 0x0c;
1851 /* turn red LED1 on, LED2 off */
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 'flyswatter' layout");
1863 return ERROR_JTAG_INIT_FAILED;
1869 int turtle_init(void)
1875 low_direction = 0x5b;
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=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 'turtelizer2' layout");
1886 return ERROR_JTAG_INIT_FAILED;
1892 high_direction = 0x0C;
1894 /* initialize high port */
1895 buf[0] = 0x82; /* command "set data bits high byte" */
1896 buf[1] = high_output;
1897 buf[2] = high_direction;
1898 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1900 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1902 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1903 return ERROR_JTAG_INIT_FAILED;
1909 int comstick_init(void)
1915 low_direction = 0x0b;
1917 /* initialize low byte for jtag */
1918 buf[0] = 0x80; /* command "set data bits low byte" */
1919 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1920 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1921 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1923 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1925 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1926 return ERROR_JTAG_INIT_FAILED;
1930 nTRSTnOE = 0x00; /* no output enable for nTRST */
1932 nSRSTnOE = 0x00; /* no output enable for nSRST */
1935 high_direction = 0x03;
1937 /* initialize high port */
1938 buf[0] = 0x82; /* command "set data bits high byte" */
1939 buf[1] = high_output;
1940 buf[2] = high_direction;
1941 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1943 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1945 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1946 return ERROR_JTAG_INIT_FAILED;
1952 int stm32stick_init(void)
1958 low_direction = 0x8b;
1960 /* initialize low byte for jtag */
1961 buf[0] = 0x80; /* command "set data bits low byte" */
1962 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1963 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1964 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1966 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1968 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1969 return ERROR_JTAG_INIT_FAILED;
1973 nTRSTnOE = 0x00; /* no output enable for nTRST */
1975 nSRSTnOE = 0x00; /* no output enable for nSRST */
1978 high_direction = 0x03;
1980 /* initialize high port */
1981 buf[0] = 0x82; /* command "set data bits high byte" */
1982 buf[1] = high_output;
1983 buf[2] = high_direction;
1984 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1986 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1988 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1989 return ERROR_JTAG_INIT_FAILED;
1995 void olimex_jtag_blink(void)
1997 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1998 * ACBUS3 is bit 3 of the GPIOH port
2000 if (high_output & 0x08)
2002 /* set port pin high */
2003 high_output &= 0x07;
2007 /* set port pin low */
2008 high_output |= 0x08;
2012 BUFFER_ADD = high_output;
2013 BUFFER_ADD = high_direction;
2016 void turtle_jtag_blink(void)
2019 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2021 if (high_output & 0x08)
2031 BUFFER_ADD = high_output;
2032 BUFFER_ADD = high_direction;
2036 int ft2232_quit(void)
2038 #if BUILD_FT2232_FTD2XX == 1
2041 status = FT_Close(ftdih);
2042 #elif BUILD_FT2232_LIBFTDI == 1
2043 ftdi_disable_bitbang(&ftdic);
2045 ftdi_usb_close(&ftdic);
2047 ftdi_deinit(&ftdic);
2050 free(ft2232_buffer);
2051 ft2232_buffer = NULL;
2056 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2060 ft2232_device_desc = strdup(args[0]);
2064 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2070 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2074 ft2232_serial = strdup(args[0]);
2078 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2084 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2089 ft2232_layout = malloc(strlen(args[0]) + 1);
2090 strcpy(ft2232_layout, args[0]);
2095 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2099 if (argc > MAX_USB_IDS*2) {
2100 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2101 "(maximum is %d pairs)", MAX_USB_IDS);
2102 argc = MAX_USB_IDS*2;
2104 if (argc < 2 || (argc & 1))
2106 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2111 for (i = 0; i+1 < argc; i += 2) {
2112 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2113 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2116 * Explicitly terminate, in case there are multiples instances of
2119 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2124 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2128 ft2232_latency = atoi(args[0]);
2132 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");