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_register_commands(struct command_context_s *cmd_ctx);
65 int ft2232_init(void);
66 int ft2232_quit(void);
68 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 char *ft2232_device_desc = NULL;
75 char *ft2232_serial = NULL;
76 char *ft2232_layout = NULL;
77 unsigned char ft2232_latency = 2;
80 /* vid = pid = 0 marks the end of the list */
81 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
82 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
84 typedef struct ft2232_layout_s
88 void(*reset)(int trst, int srst);
92 /* init procedures for supported layouts */
93 int usbjtag_init(void);
94 int jtagkey_init(void);
95 int olimex_jtag_init(void);
96 int flyswatter_init(void);
97 int turtle_init(void);
98 int comstick_init(void);
99 int stm32stick_init(void);
101 /* reset procedures for supported layouts */
102 void usbjtag_reset(int trst, int srst);
103 void jtagkey_reset(int trst, int srst);
104 void olimex_jtag_reset(int trst, int srst);
105 void flyswatter_reset(int trst, int srst);
106 void turtle_reset(int trst, int srst);
107 void comstick_reset(int trst, int srst);
108 void stm32stick_reset(int trst, int srst);
110 /* blink procedures for layouts that support a blinking led */
111 void olimex_jtag_blink(void);
112 void turtle_jtag_blink(void);
114 ft2232_layout_t ft2232_layouts[] =
116 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
117 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
118 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
119 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
120 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
121 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
122 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
123 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
124 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
125 {"comstick", comstick_init, comstick_reset, NULL},
126 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
130 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
132 static ft2232_layout_t *layout;
133 static u8 low_output = 0x0;
134 static u8 low_direction = 0x0;
135 static u8 high_output = 0x0;
136 static u8 high_direction = 0x0;
138 #if BUILD_FT2232_FTD2XX == 1
139 static FT_HANDLE ftdih = NULL;
140 #elif BUILD_FT2232_LIBFTDI == 1
141 static struct ftdi_context ftdic;
144 static u8 *ft2232_buffer = NULL;
145 static int ft2232_buffer_size = 0;
146 static int ft2232_read_pointer = 0;
147 static int ft2232_expect_read = 0;
148 #define FT2232_BUFFER_SIZE 131072
149 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
150 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
152 jtag_interface_t ft2232_interface =
157 .execute_queue = ft2232_execute_queue,
159 .speed = ft2232_speed,
160 .register_commands = ft2232_register_commands,
165 int ft2232_write(u8 *buf, int size, u32* bytes_written)
167 #if BUILD_FT2232_FTD2XX == 1
169 DWORD dw_bytes_written;
170 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
172 *bytes_written = dw_bytes_written;
173 LOG_ERROR("FT_Write returned: %lu", status);
174 return ERROR_JTAG_DEVICE_ERROR;
178 *bytes_written = dw_bytes_written;
181 #elif BUILD_FT2232_LIBFTDI == 1
183 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
186 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
187 return ERROR_JTAG_DEVICE_ERROR;
191 *bytes_written = retval;
197 int ft2232_read(u8* buf, int size, u32* bytes_read)
199 #if BUILD_FT2232_FTD2XX == 1
205 while ((*bytes_read < size) && timeout--)
207 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
208 *bytes_read, &dw_bytes_read)) != FT_OK)
211 LOG_ERROR("FT_Read returned: %lu", status);
212 return ERROR_JTAG_DEVICE_ERROR;
214 *bytes_read += dw_bytes_read;
216 #elif BUILD_FT2232_LIBFTDI == 1
221 while ((*bytes_read < size) && timeout--)
223 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
226 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
227 return ERROR_JTAG_DEVICE_ERROR;
229 *bytes_read += retval;
233 if (*bytes_read < size)
235 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
236 return ERROR_JTAG_DEVICE_ERROR;
242 int ft2232_speed(int speed)
248 buf[0] = 0x86; /* command "set divisor" */
249 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
250 buf[2] = (speed >> 8) & 0xff; /* valueH */
252 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
253 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
255 LOG_ERROR("couldn't set FT2232 TCK speed");
264 int ft2232_register_commands(struct command_context_s *cmd_ctx)
266 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
267 COMMAND_CONFIG, NULL);
268 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
269 COMMAND_CONFIG, NULL);
270 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
271 COMMAND_CONFIG, NULL);
272 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
273 COMMAND_CONFIG, NULL);
274 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
275 COMMAND_CONFIG, NULL);
279 void ft2232_end_state(enum tap_state state)
281 if (tap_move_map[state] != -1)
285 LOG_ERROR("BUG: %i is not a valid end state", state);
290 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
292 int num_bytes = ((scan_size + 7) / 8);
293 int bits_left = scan_size;
296 while(num_bytes-- > 1)
298 buffer[cur_byte] = BUFFER_READ;
303 buffer[cur_byte] = 0x0;
307 buffer[cur_byte] = BUFFER_READ >> 1;
310 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
314 void ft2232_debug_dump_buffer(void)
320 for (i = 0; i < ft2232_buffer_size; i++)
322 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
325 LOG_DEBUG("%s", line);
331 LOG_DEBUG("%s", line);
334 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
344 #ifdef _DEBUG_USB_IO_
345 struct timeval start, inter, inter2, end;
346 struct timeval d_inter, d_inter2, d_end;
349 #ifdef _DEBUG_USB_COMMS_
350 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
351 ft2232_debug_dump_buffer();
354 #ifdef _DEBUG_USB_IO_
355 gettimeofday(&start, NULL);
358 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
360 LOG_ERROR("couldn't write MPSSE commands to FT2232");
364 #ifdef _DEBUG_USB_IO_
365 gettimeofday(&inter, NULL);
368 if (ft2232_expect_read)
371 ft2232_buffer_size = 0;
373 #ifdef _DEBUG_USB_IO_
374 gettimeofday(&inter2, NULL);
377 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
379 LOG_ERROR("couldn't read from FT2232");
383 #ifdef _DEBUG_USB_IO_
384 gettimeofday(&end, NULL);
386 timeval_subtract(&d_inter, &inter, &start);
387 timeval_subtract(&d_inter2, &inter2, &start);
388 timeval_subtract(&d_end, &end, &start);
390 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);
394 ft2232_buffer_size = bytes_read;
396 if (ft2232_expect_read != ft2232_buffer_size)
398 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
399 ft2232_debug_dump_buffer();
404 #ifdef _DEBUG_USB_COMMS_
405 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
406 ft2232_debug_dump_buffer();
410 ft2232_expect_read = 0;
411 ft2232_read_pointer = 0;
413 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
414 * that wasn't handled by a caller-provided error handler
424 type = jtag_scan_type(cmd->cmd.scan);
425 if (type != SCAN_OUT)
427 scan_size = jtag_scan_size(cmd->cmd.scan);
428 buffer = calloc(CEIL(scan_size, 8), 1);
429 ft2232_read_scan(type, buffer, scan_size);
430 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
431 retval = ERROR_JTAG_QUEUE_FAILED;
441 ft2232_buffer_size = 0;
446 void ft2232_add_pathmove(pathmove_command_t *cmd)
448 int num_states = cmd->num_states;
457 int num_states_batch = num_states > 7 ? 7 : num_states;
460 /* command "Clock Data to TMS/CS Pin (no Read)" */
462 /* number of states remaining */
463 BUFFER_ADD = num_states_batch - 1;
465 while (num_states_batch--)
467 if (tap_transitions[cur_state].low == cmd->path[state_count])
468 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
469 else if (tap_transitions[cur_state].high == cmd->path[state_count])
470 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
473 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
477 cur_state = cmd->path[state_count];
482 BUFFER_ADD = tms_byte;
485 end_state = cur_state;
488 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
490 int num_bytes = (scan_size + 7) / 8;
491 int bits_left = scan_size;
495 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
497 /* command "Clock Data to TMS/CS Pin (no Read)" */
504 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
509 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
512 /* LOG_DEBUG("added TMS scan (no read)"); */
515 /* add command for complete bytes */
516 while (num_bytes > 1)
521 /* Clock Data Bytes In and Out LSB First */
523 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
525 else if (type == SCAN_OUT)
527 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
529 /* LOG_DEBUG("added TDI bytes (o)"); */
531 else if (type == SCAN_IN)
533 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
535 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
537 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
538 num_bytes -= thisrun_bytes;
539 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
540 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
543 /* add complete bytes */
544 while(thisrun_bytes-- > 0)
546 BUFFER_ADD = buffer[cur_byte];
551 else /* (type == SCAN_IN) */
553 bits_left -= 8 * (thisrun_bytes);
557 /* the most signifcant bit is scanned during TAP movement */
559 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
563 /* process remaining bits but the last one */
568 /* Clock Data Bits In and Out LSB First */
570 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
572 else if (type == SCAN_OUT)
574 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
576 /* LOG_DEBUG("added TDI bits (o)"); */
578 else if (type == SCAN_IN)
580 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
582 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
584 BUFFER_ADD = bits_left - 2;
586 BUFFER_ADD = buffer[cur_byte];
589 if ((ir_scan && (end_state == TAP_SI)) ||
590 (!ir_scan && (end_state == TAP_SD)))
594 /* Clock Data Bits In and Out LSB First */
596 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
598 else if (type == SCAN_OUT)
600 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
602 /* LOG_DEBUG("added TDI bits (o)"); */
604 else if (type == SCAN_IN)
606 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
608 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
611 BUFFER_ADD = last_bit;
615 /* move from Shift-IR/DR to end state */
616 if (type != SCAN_OUT)
618 /* Clock Data to TMS/CS Pin with Read */
620 /* LOG_DEBUG("added TMS scan (read)"); */
624 /* Clock Data to TMS/CS Pin (no Read) */
626 /* LOG_DEBUG("added TMS scan (no read)"); */
629 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
630 cur_state = end_state;
634 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
636 int num_bytes = (scan_size + 7) / 8;
637 int bits_left = scan_size;
640 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
641 u8 *receive_pointer = receive_buffer;
645 int thisrun_read = 0;
649 LOG_ERROR("BUG: large IR scans are not supported");
653 if (cur_state != TAP_SD)
655 /* command "Clock Data to TMS/CS Pin (no Read)" */
660 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
664 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
666 LOG_ERROR("couldn't write MPSSE commands to FT2232");
669 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
670 ft2232_buffer_size = 0;
672 /* add command for complete bytes */
673 while (num_bytes > 1)
679 /* Clock Data Bytes In and Out LSB First */
681 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
683 else if (type == SCAN_OUT)
685 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
687 /* LOG_DEBUG("added TDI bytes (o)"); */
689 else if (type == SCAN_IN)
691 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
693 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
695 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
696 thisrun_read = thisrun_bytes;
697 num_bytes -= thisrun_bytes;
698 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
699 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
702 /* add complete bytes */
703 while(thisrun_bytes-- > 0)
705 BUFFER_ADD = buffer[cur_byte];
710 else /* (type == SCAN_IN) */
712 bits_left -= 8 * (thisrun_bytes);
715 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
717 LOG_ERROR("couldn't write MPSSE commands to FT2232");
720 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
721 ft2232_buffer_size = 0;
723 if (type != SCAN_OUT)
725 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
727 LOG_ERROR("couldn't read from FT2232");
730 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
731 receive_pointer += bytes_read;
737 /* the most signifcant bit is scanned during TAP movement */
739 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
743 /* process remaining bits but the last one */
748 /* Clock Data Bits In and Out LSB First */
750 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
752 else if (type == SCAN_OUT)
754 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
756 /* LOG_DEBUG("added TDI bits (o)"); */
758 else if (type == SCAN_IN)
760 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
762 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
764 BUFFER_ADD = bits_left - 2;
766 BUFFER_ADD = buffer[cur_byte];
768 if (type != SCAN_OUT)
772 if (end_state == TAP_SD)
776 /* Clock Data Bits In and Out LSB First */
778 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
780 else if (type == SCAN_OUT)
782 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
784 /* LOG_DEBUG("added TDI bits (o)"); */
786 else if (type == SCAN_IN)
788 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
790 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
793 BUFFER_ADD = last_bit;
797 /* move from Shift-IR/DR to end state */
798 if (type != SCAN_OUT)
800 /* Clock Data to TMS/CS Pin with Read */
802 /* LOG_DEBUG("added TMS scan (read)"); */
806 /* Clock Data to TMS/CS Pin (no Read) */
808 /* LOG_DEBUG("added TMS scan (no read)"); */
811 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
812 cur_state = end_state;
815 if (type != SCAN_OUT)
818 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
820 LOG_ERROR("couldn't write MPSSE commands to FT2232");
823 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
824 ft2232_buffer_size = 0;
826 if (type != SCAN_OUT)
828 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
830 LOG_ERROR("couldn't read from FT2232");
833 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
834 receive_pointer += bytes_read;
840 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
842 int predicted_size = 3;
843 int num_bytes = (scan_size - 1) / 8;
845 if (cur_state != TAP_SD)
848 if (type == SCAN_IN) /* only from device to host */
851 predicted_size += (CEIL(num_bytes, 65536)) * 3;
852 /* remaining bits - 1 (up to 7) */
853 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
855 else /* host to device, or bidirectional */
858 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
859 /* remaining bits -1 (up to 7) */
860 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
863 return predicted_size;
866 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
868 int predicted_size = 0;
870 if (type != SCAN_OUT)
873 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
874 /* remaining bits - 1 */
875 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
876 /* last bit (from TMS scan) */
880 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
882 return predicted_size;
885 void usbjtag_reset(int trst, int srst)
889 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
890 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
892 low_output &= ~nTRST; /* switch output low */
896 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
897 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
899 low_output |= nTRST; /* switch output high */
904 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
905 low_output &= ~nSRST; /* switch output low */
907 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
911 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
912 low_output |= nSRST; /* switch output high */
914 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
917 /* command "set data bits low byte" */
919 BUFFER_ADD = low_output;
920 BUFFER_ADD = low_direction;
924 void jtagkey_reset(int trst, int srst)
928 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
929 high_output &= ~nTRSTnOE;
931 high_output &= ~nTRST;
935 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
936 high_output |= nTRSTnOE;
938 high_output |= nTRST;
943 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
944 high_output &= ~nSRST;
946 high_output &= ~nSRSTnOE;
950 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
951 high_output |= nSRST;
953 high_output |= nSRSTnOE;
956 /* command "set data bits high byte" */
958 BUFFER_ADD = high_output;
959 BUFFER_ADD = high_direction;
960 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
963 void olimex_jtag_reset(int trst, int srst)
967 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
968 high_output &= ~nTRSTnOE;
970 high_output &= ~nTRST;
974 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
975 high_output |= nTRSTnOE;
977 high_output |= nTRST;
982 high_output |= nSRST;
986 high_output &= ~nSRST;
989 /* command "set data bits high byte" */
991 BUFFER_ADD = high_output;
992 BUFFER_ADD = high_direction;
993 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
996 void flyswatter_reset(int trst, int srst)
1000 low_output &= ~nTRST;
1004 low_output |= nTRST;
1009 low_output |= nSRST;
1013 low_output &= ~nSRST;
1016 /* command "set data bits low byte" */
1018 BUFFER_ADD = low_output;
1019 BUFFER_ADD = low_direction;
1020 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1023 void turtle_reset(int trst, int srst)
1029 low_output |= nSRST;
1033 low_output &= ~nSRST;
1036 /* command "set data bits low byte" */
1038 BUFFER_ADD = low_output;
1039 BUFFER_ADD = low_direction;
1040 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1043 void comstick_reset(int trst, int srst)
1047 high_output &= ~nTRST;
1051 high_output |= nTRST;
1056 high_output &= ~nSRST;
1060 high_output |= nSRST;
1063 /* command "set data bits high byte" */
1065 BUFFER_ADD = high_output;
1066 BUFFER_ADD = high_direction;
1067 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1070 void stm32stick_reset(int trst, int srst)
1074 high_output &= ~nTRST;
1078 high_output |= nTRST;
1083 low_output &= ~nSRST;
1087 low_output |= nSRST;
1090 /* command "set data bits low byte" */
1092 BUFFER_ADD = low_output;
1093 BUFFER_ADD = low_direction;
1095 /* command "set data bits high byte" */
1097 BUFFER_ADD = high_output;
1098 BUFFER_ADD = high_direction;
1099 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1102 int ft2232_execute_queue()
1104 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1105 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1107 int scan_size; /* size of IR or DR scan */
1108 enum scan_type type;
1110 int predicted_size = 0;
1111 int require_send = 0;
1114 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1115 * that wasn't handled by a caller-provided error handler
1119 ft2232_buffer_size = 0;
1120 ft2232_expect_read = 0;
1122 /* blink, if the current layout has that feature */
1130 case JTAG_END_STATE:
1131 if (cmd->cmd.end_state->end_state != -1)
1132 ft2232_end_state(cmd->cmd.end_state->end_state);
1135 /* only send the maximum buffer size that FT2232C can handle */
1137 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1139 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1140 retval = ERROR_JTAG_QUEUE_FAILED;
1145 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1147 cur_state = TAP_TLR;
1149 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1152 #ifdef _DEBUG_JTAG_IO_
1153 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1157 /* only send the maximum buffer size that FT2232C can handle */
1159 if (cur_state != TAP_RTI)
1160 predicted_size += 3;
1161 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1162 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1163 predicted_size += 3;
1164 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1165 predicted_size += 3;
1166 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1168 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1169 retval = ERROR_JTAG_QUEUE_FAILED;
1173 if (cur_state != TAP_RTI)
1175 /* command "Clock Data to TMS/CS Pin (no Read)" */
1180 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1181 cur_state = TAP_RTI;
1184 i = cmd->cmd.runtest->num_cycles;
1187 /* command "Clock Data to TMS/CS Pin (no Read)" */
1190 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1193 cur_state = TAP_RTI;
1194 i -= (i > 7) ? 7 : i;
1195 /* LOG_DEBUG("added TMS scan (no read)"); */
1197 if (cmd->cmd.runtest->end_state != -1)
1198 ft2232_end_state(cmd->cmd.runtest->end_state);
1199 if (cur_state != end_state)
1201 /* command "Clock Data to TMS/CS Pin (no Read)" */
1206 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1207 cur_state = end_state;
1208 /* LOG_DEBUG("added TMS scan (no read)"); */
1211 #ifdef _DEBUG_JTAG_IO_
1212 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1215 case JTAG_STATEMOVE:
1216 /* only send the maximum buffer size that FT2232C can handle */
1218 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1220 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1221 retval = ERROR_JTAG_QUEUE_FAILED;
1225 if (cmd->cmd.statemove->end_state != -1)
1226 ft2232_end_state(cmd->cmd.statemove->end_state);
1227 /* command "Clock Data to TMS/CS Pin (no Read)" */
1232 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1233 /* LOG_DEBUG("added TMS scan (no read)"); */
1234 cur_state = end_state;
1236 #ifdef _DEBUG_JTAG_IO_
1237 LOG_DEBUG("statemove: %i", end_state);
1241 /* only send the maximum buffer size that FT2232C can handle */
1242 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1243 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1245 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1246 retval = ERROR_JTAG_QUEUE_FAILED;
1250 ft2232_add_pathmove(cmd->cmd.pathmove);
1252 #ifdef _DEBUG_JTAG_IO_
1253 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1257 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1258 type = jtag_scan_type(cmd->cmd.scan);
1259 predicted_size = ft2232_predict_scan_out(scan_size, type);
1260 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1262 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1263 /* unsent commands before this */
1264 if (first_unsent != cmd)
1265 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1266 retval = ERROR_JTAG_QUEUE_FAILED;
1268 /* current command */
1269 if (cmd->cmd.scan->end_state != -1)
1270 ft2232_end_state(cmd->cmd.scan->end_state);
1271 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1273 first_unsent = cmd->next;
1278 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1280 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1281 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1282 retval = ERROR_JTAG_QUEUE_FAILED;
1286 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1287 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1288 if (cmd->cmd.scan->end_state != -1)
1289 ft2232_end_state(cmd->cmd.scan->end_state);
1290 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1294 #ifdef _DEBUG_JTAG_IO_
1295 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1299 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1300 retval = ERROR_JTAG_QUEUE_FAILED;
1301 first_unsent = cmd->next;
1302 jtag_sleep(cmd->cmd.sleep->us);
1303 #ifdef _DEBUG_JTAG_IO_
1304 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1308 LOG_ERROR("BUG: unknown JTAG command type encountered");
1314 if (require_send > 0)
1315 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1316 retval = ERROR_JTAG_QUEUE_FAILED;
1321 #if BUILD_FT2232_FTD2XX == 1
1322 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1325 DWORD openex_flags = 0;
1326 char *openex_string = NULL;
1329 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1330 ft2232_layout, vid, pid);
1333 /* Add non-standard Vid/Pid to the linux driver */
1334 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1336 LOG_WARNING("couldn't add %4.4x:%4.4x",
1341 if (ft2232_device_desc && ft2232_serial)
1343 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1344 ft2232_device_desc = NULL;
1347 if (ft2232_device_desc)
1349 openex_string = ft2232_device_desc;
1350 openex_flags = FT_OPEN_BY_DESCRIPTION;
1352 else if (ft2232_serial)
1354 openex_string = ft2232_serial;
1355 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1359 LOG_ERROR("neither device description nor serial number specified");
1360 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1362 return ERROR_JTAG_INIT_FAILED;
1365 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1370 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1373 return ERROR_JTAG_INIT_FAILED;
1375 LOG_ERROR("unable to open ftdi device: %lu", status);
1376 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1377 if (status == FT_OK)
1379 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1382 for (i = 0; i < num_devices; i++)
1383 desc_array[i] = malloc(64);
1384 desc_array[num_devices] = NULL;
1386 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1388 if (status == FT_OK)
1390 LOG_ERROR("ListDevices: %lu\n", num_devices);
1391 for (i = 0; i < num_devices; i++)
1392 LOG_ERROR("%i: %s", i, desc_array[i]);
1395 for (i = 0; i < num_devices; i++)
1396 free(desc_array[i]);
1401 LOG_ERROR("ListDevices: NONE\n");
1403 return ERROR_JTAG_INIT_FAILED;
1406 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1408 LOG_ERROR("unable to set latency timer: %lu", status);
1409 return ERROR_JTAG_INIT_FAILED;
1412 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1414 LOG_ERROR("unable to get latency timer: %lu", status);
1415 return ERROR_JTAG_INIT_FAILED;
1419 LOG_DEBUG("current latency timer: %i", latency_timer);
1422 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1424 LOG_ERROR("unable to set timeouts: %lu", status);
1425 return ERROR_JTAG_INIT_FAILED;
1428 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1430 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1431 return ERROR_JTAG_INIT_FAILED;
1437 static int ft2232_purge_ftd2xx(void)
1441 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1443 LOG_ERROR("error purging ftd2xx device: %lu", status);
1444 return ERROR_JTAG_INIT_FAILED;
1449 #endif /* BUILD_FT2232_FTD2XX == 1 */
1451 #if BUILD_FT2232_LIBFTDI == 1
1452 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1456 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1457 ft2232_layout, vid, pid);
1459 if (ftdi_init(&ftdic) < 0)
1460 return ERROR_JTAG_INIT_FAILED;
1462 /* context, vendor id, product id */
1463 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1464 ft2232_serial) < 0) {
1466 LOG_WARNING("unable to open ftdi device (trying more): %s",
1469 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1471 return ERROR_JTAG_INIT_FAILED;
1474 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1476 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1477 return ERROR_JTAG_INIT_FAILED;
1480 if (ftdi_usb_reset(&ftdic) < 0)
1482 LOG_ERROR("unable to reset ftdi device");
1483 return ERROR_JTAG_INIT_FAILED;
1486 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1488 LOG_ERROR("unable to set latency timer");
1489 return ERROR_JTAG_INIT_FAILED;
1492 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1494 LOG_ERROR("unable to get latency timer");
1495 return ERROR_JTAG_INIT_FAILED;
1499 LOG_DEBUG("current latency timer: %i", latency_timer);
1502 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1507 static int ft2232_purge_libftdi(void)
1509 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1511 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1512 return ERROR_JTAG_INIT_FAILED;
1517 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1519 int ft2232_init(void)
1524 ft2232_layout_t *cur_layout = ft2232_layouts;
1527 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1529 ft2232_layout = "usbjtag";
1530 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1533 while (cur_layout->name)
1535 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1537 layout = cur_layout;
1545 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1546 return ERROR_JTAG_INIT_FAILED;
1549 for (i = 0; 1; i++) {
1551 * "more indicates that there are more IDs to try, so we should
1552 * not print an error for an ID mismatch (but for anything
1555 * try_more indicates that the error code returned indicates an
1556 * ID mismatch (and nothing else) and that we should proceeed
1557 * with the next ID pair.
1559 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1562 #if BUILD_FT2232_FTD2XX == 1
1563 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1565 #elif BUILD_FT2232_LIBFTDI == 1
1566 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1571 if (!more || !try_more)
1575 ft2232_buffer_size = 0;
1576 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1578 if (layout->init() != ERROR_OK)
1579 return ERROR_JTAG_INIT_FAILED;
1581 ft2232_speed(jtag_speed);
1583 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1584 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1586 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1587 return ERROR_JTAG_INIT_FAILED;
1590 #if BUILD_FT2232_FTD2XX == 1
1591 return ft2232_purge_ftd2xx();
1592 #elif BUILD_FT2232_LIBFTDI == 1
1593 return ft2232_purge_libftdi();
1599 int usbjtag_init(void)
1605 low_direction = 0x0b;
1607 if (strcmp(ft2232_layout, "usbjtag") == 0)
1614 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1621 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1628 low_direction = 0x8b;
1632 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1633 return ERROR_JTAG_INIT_FAILED;
1636 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1638 low_direction &= ~nTRSTnOE; /* nTRST input */
1639 low_output &= ~nTRST; /* nTRST = 0 */
1643 low_direction |= nTRSTnOE; /* nTRST output */
1644 low_output |= nTRST; /* nTRST = 1 */
1647 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1649 low_direction |= nSRSTnOE; /* nSRST output */
1650 low_output |= nSRST; /* nSRST = 1 */
1654 low_direction &= ~nSRSTnOE; /* nSRST input */
1655 low_output &= ~nSRST; /* nSRST = 0 */
1658 /* initialize low byte for jtag */
1659 buf[0] = 0x80; /* command "set data bits low byte" */
1660 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1661 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1662 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1664 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1666 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1667 return ERROR_JTAG_INIT_FAILED;
1673 int jtagkey_init(void)
1679 low_direction = 0x1b;
1681 /* initialize low byte for jtag */
1682 buf[0] = 0x80; /* command "set data bits low byte" */
1683 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1684 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1685 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1687 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1689 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1690 return ERROR_JTAG_INIT_FAILED;
1693 if (strcmp(layout->name, "jtagkey") == 0)
1700 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1701 (strcmp(layout->name, "oocdlink") == 0))
1710 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1715 high_direction = 0x0f;
1717 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1719 high_output |= nTRSTnOE;
1720 high_output &= ~nTRST;
1724 high_output &= ~nTRSTnOE;
1725 high_output |= nTRST;
1728 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1730 high_output &= ~nSRSTnOE;
1731 high_output |= nSRST;
1735 high_output |= nSRSTnOE;
1736 high_output &= ~nSRST;
1739 /* initialize high port */
1740 buf[0] = 0x82; /* command "set data bits high byte" */
1741 buf[1] = high_output; /* value */
1742 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1743 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1745 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1747 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1748 return ERROR_JTAG_INIT_FAILED;
1754 int olimex_jtag_init(void)
1760 low_direction = 0x1b;
1762 /* initialize low byte for jtag */
1763 buf[0] = 0x80; /* command "set data bits low byte" */
1764 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1765 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1766 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1768 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1770 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1771 return ERROR_JTAG_INIT_FAILED;
1777 nSRSTnOE = 0x00; /* no output enable for nSRST */
1780 high_direction = 0x0f;
1782 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1784 high_output |= nTRSTnOE;
1785 high_output &= ~nTRST;
1789 high_output &= ~nTRSTnOE;
1790 high_output |= nTRST;
1793 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1795 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1799 high_output &= ~nSRST;
1802 /* turn red LED on */
1803 high_output |= 0x08;
1805 /* initialize high port */
1806 buf[0] = 0x82; /* command "set data bits high byte" */
1807 buf[1] = high_output; /* value */
1808 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1809 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1811 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1813 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1814 return ERROR_JTAG_INIT_FAILED;
1820 int flyswatter_init(void)
1826 low_direction = 0xfb;
1828 /* initialize low byte for jtag */
1829 buf[0] = 0x80; /* command "set data bits low byte" */
1830 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1831 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1832 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1834 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1836 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1837 return ERROR_JTAG_INIT_FAILED;
1841 nTRSTnOE = 0x0; /* not output enable for nTRST */
1843 nSRSTnOE = 0x00; /* no output enable for nSRST */
1846 high_direction = 0x0c;
1848 /* turn red LED1 on, LED2 off */
1849 high_output |= 0x08;
1851 /* initialize high port */
1852 buf[0] = 0x82; /* command "set data bits high byte" */
1853 buf[1] = high_output; /* value */
1854 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1855 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1857 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1859 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1860 return ERROR_JTAG_INIT_FAILED;
1866 int turtle_init(void)
1872 low_direction = 0x5b;
1874 /* initialize low byte for jtag */
1875 buf[0] = 0x80; /* command "set data bits low byte" */
1876 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1877 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1878 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1880 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1882 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1883 return ERROR_JTAG_INIT_FAILED;
1889 high_direction = 0x0C;
1891 /* initialize high port */
1892 buf[0] = 0x82; /* command "set data bits high byte" */
1893 buf[1] = high_output;
1894 buf[2] = high_direction;
1895 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1897 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1899 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1900 return ERROR_JTAG_INIT_FAILED;
1906 int comstick_init(void)
1912 low_direction = 0x0b;
1914 /* initialize low byte for jtag */
1915 buf[0] = 0x80; /* command "set data bits low byte" */
1916 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1917 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1918 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1920 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1922 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1923 return ERROR_JTAG_INIT_FAILED;
1927 nTRSTnOE = 0x00; /* no output enable for nTRST */
1929 nSRSTnOE = 0x00; /* no output enable for nSRST */
1932 high_direction = 0x03;
1934 /* initialize high port */
1935 buf[0] = 0x82; /* command "set data bits high byte" */
1936 buf[1] = high_output;
1937 buf[2] = high_direction;
1938 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1940 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1942 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1943 return ERROR_JTAG_INIT_FAILED;
1949 int stm32stick_init(void)
1955 low_direction = 0x8b;
1957 /* initialize low byte for jtag */
1958 buf[0] = 0x80; /* command "set data bits low byte" */
1959 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1960 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1961 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1963 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1965 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1966 return ERROR_JTAG_INIT_FAILED;
1970 nTRSTnOE = 0x00; /* no output enable for nTRST */
1972 nSRSTnOE = 0x00; /* no output enable for nSRST */
1975 high_direction = 0x03;
1977 /* initialize high port */
1978 buf[0] = 0x82; /* command "set data bits high byte" */
1979 buf[1] = high_output;
1980 buf[2] = high_direction;
1981 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1983 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1985 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1986 return ERROR_JTAG_INIT_FAILED;
1992 void olimex_jtag_blink(void)
1994 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1995 * ACBUS3 is bit 3 of the GPIOH port
1997 if (high_output & 0x08)
1999 /* set port pin high */
2000 high_output &= 0x07;
2004 /* set port pin low */
2005 high_output |= 0x08;
2009 BUFFER_ADD = high_output;
2010 BUFFER_ADD = high_direction;
2013 void turtle_jtag_blink(void)
2016 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2018 if (high_output & 0x08)
2028 BUFFER_ADD = high_output;
2029 BUFFER_ADD = high_direction;
2033 int ft2232_quit(void)
2035 #if BUILD_FT2232_FTD2XX == 1
2038 status = FT_Close(ftdih);
2039 #elif BUILD_FT2232_LIBFTDI == 1
2040 ftdi_disable_bitbang(&ftdic);
2042 ftdi_usb_close(&ftdic);
2044 ftdi_deinit(&ftdic);
2047 free(ft2232_buffer);
2048 ft2232_buffer = NULL;
2053 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2057 ft2232_device_desc = strdup(args[0]);
2061 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2067 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2071 ft2232_serial = strdup(args[0]);
2075 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2081 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2086 ft2232_layout = malloc(strlen(args[0]) + 1);
2087 strcpy(ft2232_layout, args[0]);
2092 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2096 if (argc > MAX_USB_IDS*2) {
2097 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2098 "(maximum is %d pairs)", MAX_USB_IDS);
2099 argc = MAX_USB_IDS*2;
2101 if (argc < 2 || (argc & 1))
2103 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2108 for (i = 0; i+1 < argc; i += 2) {
2109 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2110 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2113 * Explicitly terminate, in case there are multiples instances of
2116 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2121 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2125 ft2232_latency = atoi(args[0]);
2129 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");