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 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
43 /* FT2232 access library includes */
44 #if BUILD_FT2232_FTD2XX == 1
46 #elif BUILD_FT2232_LIBFTDI == 1
53 /* enable this to debug io latency
56 #define _DEBUG_USB_IO_
59 /* enable this to debug communication
62 #define _DEBUG_USB_COMMS_
65 int ft2232_execute_queue(void);
67 int ft2232_speed(int speed);
68 int ft2232_register_commands(struct command_context_s *cmd_ctx);
69 int ft2232_init(void);
70 int ft2232_quit(void);
72 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
78 char *ft2232_device_desc = NULL;
79 char *ft2232_serial = NULL;
80 char *ft2232_layout = NULL;
81 unsigned char ft2232_latency = 2;
84 /* vid = pid = 0 marks the end of the list */
85 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
86 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
88 typedef struct ft2232_layout_s
92 void(*reset)(int trst, int srst);
96 /* init procedures for supported layouts */
97 int usbjtag_init(void);
98 int jtagkey_init(void);
99 int olimex_jtag_init(void);
100 int flyswatter_init(void);
101 int turtle_init(void);
102 int comstick_init(void);
103 int stm32stick_init(void);
105 /* reset procedures for supported layouts */
106 void usbjtag_reset(int trst, int srst);
107 void jtagkey_reset(int trst, int srst);
108 void olimex_jtag_reset(int trst, int srst);
109 void flyswatter_reset(int trst, int srst);
110 void turtle_reset(int trst, int srst);
111 void comstick_reset(int trst, int srst);
112 void stm32stick_reset(int trst, int srst);
114 /* blink procedures for layouts that support a blinking led */
115 void olimex_jtag_blink(void);
116 void turtle_jtag_blink(void);
118 ft2232_layout_t ft2232_layouts[] =
120 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
121 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
122 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
123 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
124 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
125 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
126 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
127 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
128 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
129 {"comstick", comstick_init, comstick_reset, NULL},
130 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
134 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
136 static ft2232_layout_t *layout;
137 static u8 low_output = 0x0;
138 static u8 low_direction = 0x0;
139 static u8 high_output = 0x0;
140 static u8 high_direction = 0x0;
142 #if BUILD_FT2232_FTD2XX == 1
143 static FT_HANDLE ftdih = NULL;
144 #elif BUILD_FT2232_LIBFTDI == 1
145 static struct ftdi_context ftdic;
148 static u8 *ft2232_buffer = NULL;
149 static int ft2232_buffer_size = 0;
150 static int ft2232_read_pointer = 0;
151 static int ft2232_expect_read = 0;
152 #define FT2232_BUFFER_SIZE 131072
153 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
154 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
156 jtag_interface_t ft2232_interface =
161 .execute_queue = ft2232_execute_queue,
163 .speed = ft2232_speed,
164 .register_commands = ft2232_register_commands,
169 int ft2232_write(u8 *buf, int size, u32* bytes_written)
171 #if BUILD_FT2232_FTD2XX == 1
173 DWORD dw_bytes_written;
174 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
176 *bytes_written = dw_bytes_written;
177 ERROR("FT_Write returned: %lu", status);
178 return ERROR_JTAG_DEVICE_ERROR;
182 *bytes_written = dw_bytes_written;
185 #elif BUILD_FT2232_LIBFTDI == 1
187 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
190 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
191 return ERROR_JTAG_DEVICE_ERROR;
195 *bytes_written = retval;
201 int ft2232_read(u8* buf, int size, u32* bytes_read)
203 #if BUILD_FT2232_FTD2XX == 1
209 while ((*bytes_read < size) && timeout--)
211 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
212 *bytes_read, &dw_bytes_read)) != FT_OK)
215 ERROR("FT_Read returned: %lu", status);
216 return ERROR_JTAG_DEVICE_ERROR;
218 *bytes_read += dw_bytes_read;
220 #elif BUILD_FT2232_LIBFTDI == 1
225 while ((*bytes_read < size) && timeout--)
227 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
230 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
231 return ERROR_JTAG_DEVICE_ERROR;
233 *bytes_read += retval;
237 if (*bytes_read < size)
239 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
240 return ERROR_JTAG_DEVICE_ERROR;
246 int ft2232_speed(int speed)
252 buf[0] = 0x86; /* command "set divisor" */
253 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
254 buf[2] = (speed >> 8) & 0xff; /* valueH */
256 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
257 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
259 ERROR("couldn't set FT2232 TCK speed");
268 int ft2232_register_commands(struct command_context_s *cmd_ctx)
270 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
271 COMMAND_CONFIG, NULL);
272 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
273 COMMAND_CONFIG, NULL);
274 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
275 COMMAND_CONFIG, NULL);
276 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
277 COMMAND_CONFIG, NULL);
278 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
279 COMMAND_CONFIG, NULL);
283 void ft2232_end_state(enum tap_state state)
285 if (tap_move_map[state] != -1)
289 ERROR("BUG: %i is not a valid end state", state);
294 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
296 int num_bytes = ((scan_size + 7) / 8);
297 int bits_left = scan_size;
300 while(num_bytes-- > 1)
302 buffer[cur_byte] = BUFFER_READ;
307 buffer[cur_byte] = 0x0;
311 buffer[cur_byte] = BUFFER_READ >> 1;
314 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
318 void ft2232_debug_dump_buffer(void)
324 for (i = 0; i < ft2232_buffer_size; i++)
326 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
338 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
348 #ifdef _DEBUG_USB_IO_
349 struct timeval start, inter, inter2, end;
350 struct timeval d_inter, d_inter2, d_end;
353 #ifdef _DEBUG_USB_COMMS_
354 DEBUG("write buffer (size %i):", ft2232_buffer_size);
355 ft2232_debug_dump_buffer();
358 #ifdef _DEBUG_USB_IO_
359 gettimeofday(&start, NULL);
362 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
364 ERROR("couldn't write MPSSE commands to FT2232");
368 #ifdef _DEBUG_USB_IO_
369 gettimeofday(&inter, NULL);
372 if (ft2232_expect_read)
375 ft2232_buffer_size = 0;
377 #ifdef _DEBUG_USB_IO_
378 gettimeofday(&inter2, NULL);
381 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
383 ERROR("couldn't read from FT2232");
387 #ifdef _DEBUG_USB_IO_
388 gettimeofday(&end, NULL);
390 timeval_subtract(&d_inter, &inter, &start);
391 timeval_subtract(&d_inter2, &inter2, &start);
392 timeval_subtract(&d_end, &end, &start);
394 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);
398 ft2232_buffer_size = bytes_read;
400 if (ft2232_expect_read != ft2232_buffer_size)
402 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
403 ft2232_debug_dump_buffer();
408 #ifdef _DEBUG_USB_COMMS_
409 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
410 ft2232_debug_dump_buffer();
414 ft2232_expect_read = 0;
415 ft2232_read_pointer = 0;
417 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
418 * that wasn't handled by a caller-provided error handler
428 type = jtag_scan_type(cmd->cmd.scan);
429 if (type != SCAN_OUT)
431 scan_size = jtag_scan_size(cmd->cmd.scan);
432 buffer = calloc(CEIL(scan_size, 8), 1);
433 ft2232_read_scan(type, buffer, scan_size);
434 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
435 retval = ERROR_JTAG_QUEUE_FAILED;
445 ft2232_buffer_size = 0;
450 void ft2232_add_pathmove(pathmove_command_t *cmd)
452 int num_states = cmd->num_states;
462 /* command "Clock Data to TMS/CS Pin (no Read)" */
464 /* number of states remaining */
465 BUFFER_ADD = (num_states % 7) - 1;
467 while (num_states % 7)
469 if (tap_transitions[cur_state].low == cmd->path[state_count])
470 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
471 else if (tap_transitions[cur_state].high == cmd->path[state_count])
472 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
475 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
479 cur_state = cmd->path[state_count];
484 BUFFER_ADD = tms_byte;
487 end_state = cur_state;
490 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
492 int num_bytes = (scan_size + 7) / 8;
493 int bits_left = scan_size;
497 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
499 /* command "Clock Data to TMS/CS Pin (no Read)" */
506 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
511 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
514 //DEBUG("added TMS scan (no read)");
517 /* add command for complete bytes */
518 while (num_bytes > 1)
523 /* Clock Data Bytes In and Out LSB First */
525 //DEBUG("added TDI bytes (io %i)", num_bytes);
527 else if (type == SCAN_OUT)
529 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
531 //DEBUG("added TDI bytes (o)");
533 else if (type == SCAN_IN)
535 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
537 //DEBUG("added TDI bytes (i %i)", num_bytes);
539 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
540 num_bytes -= thisrun_bytes;
541 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
542 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
545 /* add complete bytes */
546 while(thisrun_bytes-- > 0)
548 BUFFER_ADD = buffer[cur_byte];
553 else /* (type == SCAN_IN) */
555 bits_left -= 8 * (thisrun_bytes);
559 /* the most signifcant bit is scanned during TAP movement */
561 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
565 /* process remaining bits but the last one */
570 /* Clock Data Bits In and Out LSB First */
572 //DEBUG("added TDI bits (io) %i", bits_left - 1);
574 else if (type == SCAN_OUT)
576 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
578 //DEBUG("added TDI bits (o)");
580 else if (type == SCAN_IN)
582 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
584 //DEBUG("added TDI bits (i %i)", bits_left - 1);
586 BUFFER_ADD = bits_left - 2;
588 BUFFER_ADD = buffer[cur_byte];
591 if ((ir_scan && (end_state == TAP_SI)) ||
592 (!ir_scan && (end_state == TAP_SD)))
596 /* Clock Data Bits In and Out LSB First */
598 //DEBUG("added TDI bits (io) %i", bits_left - 1);
600 else if (type == SCAN_OUT)
602 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
604 //DEBUG("added TDI bits (o)");
606 else if (type == SCAN_IN)
608 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
610 //DEBUG("added TDI bits (i %i)", bits_left - 1);
613 BUFFER_ADD = last_bit;
617 /* move from Shift-IR/DR to end state */
618 if (type != SCAN_OUT)
620 /* Clock Data to TMS/CS Pin with Read */
622 //DEBUG("added TMS scan (read)");
626 /* Clock Data to TMS/CS Pin (no Read) */
628 //DEBUG("added TMS scan (no read)");
631 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
632 cur_state = end_state;
636 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
638 int num_bytes = (scan_size + 7) / 8;
639 int bits_left = scan_size;
642 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
643 u8 *receive_pointer = receive_buffer;
647 int thisrun_read = 0;
651 ERROR("BUG: large IR scans are not supported");
655 if (cur_state != TAP_SD)
657 /* command "Clock Data to TMS/CS Pin (no Read)" */
662 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
666 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
668 ERROR("couldn't write MPSSE commands to FT2232");
671 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
672 ft2232_buffer_size = 0;
674 /* add command for complete bytes */
675 while (num_bytes > 1)
681 /* Clock Data Bytes In and Out LSB First */
683 //DEBUG("added TDI bytes (io %i)", num_bytes);
685 else if (type == SCAN_OUT)
687 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
689 //DEBUG("added TDI bytes (o)");
691 else if (type == SCAN_IN)
693 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
695 //DEBUG("added TDI bytes (i %i)", num_bytes);
697 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
698 thisrun_read = thisrun_bytes;
699 num_bytes -= thisrun_bytes;
700 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
701 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
704 /* add complete bytes */
705 while(thisrun_bytes-- > 0)
707 BUFFER_ADD = buffer[cur_byte];
712 else /* (type == SCAN_IN) */
714 bits_left -= 8 * (thisrun_bytes);
717 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
719 ERROR("couldn't write MPSSE commands to FT2232");
722 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
723 ft2232_buffer_size = 0;
725 if (type != SCAN_OUT)
727 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
729 ERROR("couldn't read from FT2232");
732 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
733 receive_pointer += bytes_read;
739 /* the most signifcant bit is scanned during TAP movement */
741 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
745 /* process remaining bits but the last one */
750 /* Clock Data Bits In and Out LSB First */
752 //DEBUG("added TDI bits (io) %i", bits_left - 1);
754 else if (type == SCAN_OUT)
756 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
758 //DEBUG("added TDI bits (o)");
760 else if (type == SCAN_IN)
762 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
764 //DEBUG("added TDI bits (i %i)", bits_left - 1);
766 BUFFER_ADD = bits_left - 2;
768 BUFFER_ADD = buffer[cur_byte];
770 if (type != SCAN_OUT)
774 if (end_state == TAP_SD)
778 /* Clock Data Bits In and Out LSB First */
780 //DEBUG("added TDI bits (io) %i", bits_left - 1);
782 else if (type == SCAN_OUT)
784 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
786 //DEBUG("added TDI bits (o)");
788 else if (type == SCAN_IN)
790 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
792 //DEBUG("added TDI bits (i %i)", bits_left - 1);
795 BUFFER_ADD = last_bit;
799 /* move from Shift-IR/DR to end state */
800 if (type != SCAN_OUT)
802 /* Clock Data to TMS/CS Pin with Read */
804 //DEBUG("added TMS scan (read)");
808 /* Clock Data to TMS/CS Pin (no Read) */
810 //DEBUG("added TMS scan (no read)");
813 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
814 cur_state = end_state;
817 if (type != SCAN_OUT)
820 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
822 ERROR("couldn't write MPSSE commands to FT2232");
825 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
826 ft2232_buffer_size = 0;
828 if (type != SCAN_OUT)
830 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
832 ERROR("couldn't read from FT2232");
835 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
836 receive_pointer += bytes_read;
842 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
844 int predicted_size = 3;
845 int num_bytes = (scan_size - 1) / 8;
847 if (cur_state != TAP_SD)
850 if (type == SCAN_IN) /* only from device to host */
853 predicted_size += (CEIL(num_bytes, 65536)) * 3;
854 /* remaining bits - 1 (up to 7) */
855 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
857 else /* host to device, or bidirectional */
860 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
861 /* remaining bits -1 (up to 7) */
862 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
865 return predicted_size;
868 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
870 int predicted_size = 0;
872 if (type != SCAN_OUT)
875 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
876 /* remaining bits - 1 */
877 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
878 /* last bit (from TMS scan) */
882 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
884 return predicted_size;
887 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)
932 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
933 high_output &= ~nTRSTnOE;
935 high_output &= ~nTRST;
939 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
940 high_output |= nTRSTnOE;
942 high_output |= nTRST;
947 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
948 high_output &= ~nSRST;
950 high_output &= ~nSRSTnOE;
954 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
955 high_output |= nSRST;
957 high_output |= nSRSTnOE;
960 /* command "set data bits high byte" */
962 BUFFER_ADD = high_output;
963 BUFFER_ADD = high_direction;
964 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
967 void olimex_jtag_reset(int trst, int srst)
972 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
973 high_output &= ~nTRSTnOE;
975 high_output &= ~nTRST;
979 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
980 high_output |= nTRSTnOE;
982 high_output |= nTRST;
987 high_output |= nSRST;
991 high_output &= ~nSRST;
994 /* command "set data bits high byte" */
996 BUFFER_ADD = high_output;
997 BUFFER_ADD = high_direction;
998 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1001 void flyswatter_reset(int trst, int srst)
1005 cur_state = TAP_TLR;
1006 low_output &= ~nTRST;
1010 low_output |= nTRST;
1015 low_output |= nSRST;
1019 low_output &= ~nSRST;
1022 /* command "set data bits low byte" */
1024 BUFFER_ADD = low_output;
1025 BUFFER_ADD = low_direction;
1026 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1029 void turtle_reset(int trst, int srst)
1035 low_output |= nSRST;
1039 low_output &= ~nSRST;
1042 /* command "set data bits low byte" */
1044 BUFFER_ADD = low_output;
1045 BUFFER_ADD = low_direction;
1046 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1049 void comstick_reset(int trst, int srst)
1053 cur_state = TAP_TLR;
1054 high_output &= ~nTRST;
1058 high_output |= nTRST;
1063 high_output &= ~nSRST;
1067 high_output |= nSRST;
1070 /* command "set data bits high byte" */
1072 BUFFER_ADD = high_output;
1073 BUFFER_ADD = high_direction;
1074 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1077 void stm32stick_reset(int trst, int srst)
1081 cur_state = TAP_TLR;
1082 high_output &= ~nTRST;
1086 high_output |= nTRST;
1091 low_output &= ~nSRST;
1095 low_output |= nSRST;
1098 /* command "set data bits low byte" */
1100 BUFFER_ADD = low_output;
1101 BUFFER_ADD = low_direction;
1103 /* command "set data bits high byte" */
1105 BUFFER_ADD = high_output;
1106 BUFFER_ADD = high_direction;
1107 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1110 int ft2232_execute_queue()
1112 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1113 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1115 int scan_size; /* size of IR or DR scan */
1116 enum scan_type type;
1118 int predicted_size = 0;
1119 int require_send = 0;
1122 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1123 * that wasn't handled by a caller-provided error handler
1127 ft2232_buffer_size = 0;
1128 ft2232_expect_read = 0;
1130 /* blink, if the current layout has that feature */
1138 case JTAG_END_STATE:
1139 if (cmd->cmd.end_state->end_state != -1)
1140 ft2232_end_state(cmd->cmd.end_state->end_state);
1143 /* only send the maximum buffer size that FT2232C can handle */
1145 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1147 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1148 retval = ERROR_JTAG_QUEUE_FAILED;
1153 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1156 #ifdef _DEBUG_JTAG_IO_
1157 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1161 /* only send the maximum buffer size that FT2232C can handle */
1163 if (cur_state != TAP_RTI)
1164 predicted_size += 3;
1165 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1166 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1167 predicted_size += 3;
1168 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1169 predicted_size += 3;
1170 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1172 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1173 retval = ERROR_JTAG_QUEUE_FAILED;
1177 if (cur_state != TAP_RTI)
1179 /* command "Clock Data to TMS/CS Pin (no Read)" */
1184 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1185 cur_state = TAP_RTI;
1188 i = cmd->cmd.runtest->num_cycles;
1191 /* command "Clock Data to TMS/CS Pin (no Read)" */
1194 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1197 cur_state = TAP_RTI;
1198 i -= (i > 7) ? 7 : i;
1199 //DEBUG("added TMS scan (no read)");
1201 if (cmd->cmd.runtest->end_state != -1)
1202 ft2232_end_state(cmd->cmd.runtest->end_state);
1203 if (cur_state != end_state)
1205 /* command "Clock Data to TMS/CS Pin (no Read)" */
1210 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1211 cur_state = end_state;
1212 //DEBUG("added TMS scan (no read)");
1215 #ifdef _DEBUG_JTAG_IO_
1216 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1219 case JTAG_STATEMOVE:
1220 /* only send the maximum buffer size that FT2232C can handle */
1222 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1224 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1225 retval = ERROR_JTAG_QUEUE_FAILED;
1229 if (cmd->cmd.statemove->end_state != -1)
1230 ft2232_end_state(cmd->cmd.statemove->end_state);
1231 /* command "Clock Data to TMS/CS Pin (no Read)" */
1236 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1237 //DEBUG("added TMS scan (no read)");
1238 cur_state = end_state;
1240 #ifdef _DEBUG_JTAG_IO_
1241 DEBUG("statemove: %i", end_state);
1245 /* only send the maximum buffer size that FT2232C can handle */
1246 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1247 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1249 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1250 retval = ERROR_JTAG_QUEUE_FAILED;
1254 ft2232_add_pathmove(cmd->cmd.pathmove);
1256 #ifdef _DEBUG_JTAG_IO_
1257 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1261 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1262 type = jtag_scan_type(cmd->cmd.scan);
1263 predicted_size = ft2232_predict_scan_out(scan_size, type);
1264 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1266 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1267 /* unsent commands before this */
1268 if (first_unsent != cmd)
1269 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1270 retval = ERROR_JTAG_QUEUE_FAILED;
1272 /* current command */
1273 if (cmd->cmd.scan->end_state != -1)
1274 ft2232_end_state(cmd->cmd.scan->end_state);
1275 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1277 first_unsent = cmd->next;
1282 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1284 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1285 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1286 retval = ERROR_JTAG_QUEUE_FAILED;
1290 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1291 //DEBUG("new read size: %i", ft2232_expect_read);
1292 if (cmd->cmd.scan->end_state != -1)
1293 ft2232_end_state(cmd->cmd.scan->end_state);
1294 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1298 #ifdef _DEBUG_JTAG_IO_
1299 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1303 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1304 retval = ERROR_JTAG_QUEUE_FAILED;
1305 first_unsent = cmd->next;
1306 jtag_sleep(cmd->cmd.sleep->us);
1307 #ifdef _DEBUG_JTAG_IO_
1308 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1312 ERROR("BUG: unknown JTAG command type encountered");
1318 if (require_send > 0)
1319 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1320 retval = ERROR_JTAG_QUEUE_FAILED;
1325 #if BUILD_FT2232_FTD2XX == 1
1326 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1329 DWORD openex_flags = 0;
1330 char *openex_string = NULL;
1333 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1334 ft2232_layout, vid, pid);
1337 /* Add non-standard Vid/Pid to the linux driver */
1338 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1340 WARNING("couldn't add %4.4x:%4.4x",
1345 if (ft2232_device_desc && ft2232_serial)
1347 WARNING("can't open by device description and serial number, giving precedence to serial");
1348 ft2232_device_desc = NULL;
1351 if (ft2232_device_desc)
1353 openex_string = ft2232_device_desc;
1354 openex_flags = FT_OPEN_BY_DESCRIPTION;
1356 else if (ft2232_serial)
1358 openex_string = ft2232_serial;
1359 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1363 ERROR("neither device description nor serial number specified");
1364 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1366 return ERROR_JTAG_INIT_FAILED;
1369 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1374 WARNING("unable to open ftdi device (trying more): %lu",
1377 return ERROR_JTAG_INIT_FAILED;
1379 ERROR("unable to open ftdi device: %lu", status);
1380 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1381 if (status == FT_OK)
1383 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1386 for (i = 0; i < num_devices; i++)
1387 desc_array[i] = malloc(64);
1388 desc_array[num_devices] = NULL;
1390 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1392 if (status == FT_OK)
1394 ERROR("ListDevices: %lu\n", num_devices);
1395 for (i = 0; i < num_devices; i++)
1396 ERROR("%i: %s", i, desc_array[i]);
1399 for (i = 0; i < num_devices; i++)
1400 free(desc_array[i]);
1405 ERROR("ListDevices: NONE\n");
1407 return ERROR_JTAG_INIT_FAILED;
1410 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1412 ERROR("unable to set latency timer: %lu", status);
1413 return ERROR_JTAG_INIT_FAILED;
1416 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1418 ERROR("unable to get latency timer: %lu", status);
1419 return ERROR_JTAG_INIT_FAILED;
1423 DEBUG("current latency timer: %i", latency_timer);
1426 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1428 ERROR("unable to set timeouts: %lu", status);
1429 return ERROR_JTAG_INIT_FAILED;
1432 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1434 ERROR("unable to enable bit i/o mode: %lu", status);
1435 return ERROR_JTAG_INIT_FAILED;
1441 static int ft2232_purge_ftd2xx(void)
1445 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1447 ERROR("error purging ftd2xx device: %lu", status);
1448 return ERROR_JTAG_INIT_FAILED;
1453 #endif /* BUILD_FT2232_FTD2XX == 1 */
1455 #if BUILD_FT2232_LIBFTDI == 1
1456 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1460 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1461 ft2232_layout, vid, pid);
1463 if (ftdi_init(&ftdic) < 0)
1464 return ERROR_JTAG_INIT_FAILED;
1466 /* context, vendor id, product id */
1467 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1468 ft2232_serial) < 0) {
1470 WARNING("unable to open ftdi device (trying more): %s",
1473 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1475 return ERROR_JTAG_INIT_FAILED;
1478 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1480 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1481 return ERROR_JTAG_INIT_FAILED;
1484 if (ftdi_usb_reset(&ftdic) < 0)
1486 ERROR("unable to reset ftdi device");
1487 return ERROR_JTAG_INIT_FAILED;
1490 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1492 ERROR("unable to set latency timer");
1493 return ERROR_JTAG_INIT_FAILED;
1496 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1498 ERROR("unable to get latency timer");
1499 return ERROR_JTAG_INIT_FAILED;
1503 DEBUG("current latency timer: %i", latency_timer);
1506 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1511 static int ft2232_purge_libftdi(void)
1513 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1515 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1516 return ERROR_JTAG_INIT_FAILED;
1521 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1523 int ft2232_init(void)
1528 ft2232_layout_t *cur_layout = ft2232_layouts;
1531 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1533 ft2232_layout = "usbjtag";
1534 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1537 while (cur_layout->name)
1539 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1541 layout = cur_layout;
1549 ERROR("No matching layout found for %s", ft2232_layout);
1550 return ERROR_JTAG_INIT_FAILED;
1553 for (i = 0; 1; i++) {
1555 * "more indicates that there are more IDs to try, so we should
1556 * not print an error for an ID mismatch (but for anything
1559 * try_more indicates that the error code returned indicates an
1560 * ID mismatch (and nothing else) and that we should proceeed
1561 * with the next ID pair.
1563 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1566 #if BUILD_FT2232_FTD2XX == 1
1567 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1569 #elif BUILD_FT2232_LIBFTDI == 1
1570 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1575 if (!more || !try_more)
1579 ft2232_buffer_size = 0;
1580 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1582 if (layout->init() != ERROR_OK)
1583 return ERROR_JTAG_INIT_FAILED;
1585 ft2232_speed(jtag_speed);
1587 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1588 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1590 ERROR("couldn't write to FT2232 to disable loopback");
1591 return ERROR_JTAG_INIT_FAILED;
1594 #if BUILD_FT2232_FTD2XX == 1
1595 return ft2232_purge_ftd2xx();
1596 #elif BUILD_FT2232_LIBFTDI == 1
1597 return ft2232_purge_libftdi();
1603 int usbjtag_init(void)
1609 low_direction = 0x0b;
1611 if (strcmp(ft2232_layout, "usbjtag") == 0)
1618 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1625 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1632 low_direction = 0x8b;
1636 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1637 return ERROR_JTAG_INIT_FAILED;
1640 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1642 low_direction &= ~nTRSTnOE; /* nTRST input */
1643 low_output &= ~nTRST; /* nTRST = 0 */
1647 low_direction |= nTRSTnOE; /* nTRST output */
1648 low_output |= nTRST; /* nTRST = 1 */
1651 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1653 low_direction |= nSRSTnOE; /* nSRST output */
1654 low_output |= nSRST; /* nSRST = 1 */
1658 low_direction &= ~nSRSTnOE; /* nSRST input */
1659 low_output &= ~nSRST; /* nSRST = 0 */
1662 /* initialize low byte for jtag */
1663 buf[0] = 0x80; /* command "set data bits low byte" */
1664 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1665 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1666 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1668 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1670 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1671 return ERROR_JTAG_INIT_FAILED;
1677 int jtagkey_init(void)
1683 low_direction = 0x1b;
1685 /* initialize low byte for jtag */
1686 buf[0] = 0x80; /* command "set data bits low byte" */
1687 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1688 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1689 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1691 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1693 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1694 return ERROR_JTAG_INIT_FAILED;
1697 if (strcmp(layout->name, "jtagkey") == 0)
1704 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1705 (strcmp(layout->name, "oocdlink") == 0))
1714 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1719 high_direction = 0x0f;
1721 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1723 high_output |= nTRSTnOE;
1724 high_output &= ~nTRST;
1728 high_output &= ~nTRSTnOE;
1729 high_output |= nTRST;
1732 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1734 high_output &= ~nSRSTnOE;
1735 high_output |= nSRST;
1739 high_output |= nSRSTnOE;
1740 high_output &= ~nSRST;
1743 /* initialize high port */
1744 buf[0] = 0x82; /* command "set data bits high byte" */
1745 buf[1] = high_output; /* value */
1746 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1747 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1749 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1751 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1752 return ERROR_JTAG_INIT_FAILED;
1758 int olimex_jtag_init(void)
1764 low_direction = 0x1b;
1766 /* initialize low byte for jtag */
1767 buf[0] = 0x80; /* command "set data bits low byte" */
1768 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1769 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1770 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1772 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1774 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1775 return ERROR_JTAG_INIT_FAILED;
1781 nSRSTnOE = 0x00; /* no output enable for nSRST */
1784 high_direction = 0x0f;
1786 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1788 high_output |= nTRSTnOE;
1789 high_output &= ~nTRST;
1793 high_output &= ~nTRSTnOE;
1794 high_output |= nTRST;
1797 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1799 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1803 high_output &= ~nSRST;
1806 /* turn red LED on */
1807 high_output |= 0x08;
1809 /* initialize high port */
1810 buf[0] = 0x82; /* command "set data bits high byte" */
1811 buf[1] = high_output; /* value */
1812 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1813 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1815 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1817 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1818 return ERROR_JTAG_INIT_FAILED;
1824 int flyswatter_init(void)
1830 low_direction = 0xfb;
1832 /* initialize low byte for jtag */
1833 buf[0] = 0x80; /* command "set data bits low byte" */
1834 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1835 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1836 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1838 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1840 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1841 return ERROR_JTAG_INIT_FAILED;
1845 nTRSTnOE = 0x0; /* not output enable for nTRST */
1847 nSRSTnOE = 0x00; /* no output enable for nSRST */
1850 high_direction = 0x0c;
1852 /* turn red LED1 on, LED2 off */
1853 high_output |= 0x08;
1855 /* initialize high port */
1856 buf[0] = 0x82; /* command "set data bits high byte" */
1857 buf[1] = high_output; /* value */
1858 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1859 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1861 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1863 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1864 return ERROR_JTAG_INIT_FAILED;
1870 int turtle_init(void)
1876 low_direction = 0x5b;
1878 /* initialize low byte for jtag */
1879 buf[0] = 0x80; /* command "set data bits low byte" */
1880 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1881 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1882 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1884 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1886 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1887 return ERROR_JTAG_INIT_FAILED;
1893 high_direction = 0x0C;
1895 /* initialize high port */
1896 buf[0] = 0x82; /* command "set data bits high byte" */
1897 buf[1] = high_output;
1898 buf[2] = high_direction;
1899 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1901 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1903 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1904 return ERROR_JTAG_INIT_FAILED;
1910 int comstick_init(void)
1916 low_direction = 0x0b;
1918 /* initialize low byte for jtag */
1919 buf[0] = 0x80; /* command "set data bits low byte" */
1920 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1921 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1922 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1924 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1926 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1927 return ERROR_JTAG_INIT_FAILED;
1931 nTRSTnOE = 0x00; /* no output enable for nTRST */
1933 nSRSTnOE = 0x00; /* no output enable for nSRST */
1936 high_direction = 0x03;
1938 /* initialize high port */
1939 buf[0] = 0x82; /* command "set data bits high byte" */
1940 buf[1] = high_output;
1941 buf[2] = high_direction;
1942 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1944 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1946 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1947 return ERROR_JTAG_INIT_FAILED;
1953 int stm32stick_init(void)
1959 low_direction = 0x8b;
1961 /* initialize low byte for jtag */
1962 buf[0] = 0x80; /* command "set data bits low byte" */
1963 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1964 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1965 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1967 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1969 ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1970 return ERROR_JTAG_INIT_FAILED;
1974 nTRSTnOE = 0x00; /* no output enable for nTRST */
1976 nSRSTnOE = 0x00; /* no output enable for nSRST */
1979 high_direction = 0x03;
1981 /* initialize high port */
1982 buf[0] = 0x82; /* command "set data bits high byte" */
1983 buf[1] = high_output;
1984 buf[2] = high_direction;
1985 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1987 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1989 ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1990 return ERROR_JTAG_INIT_FAILED;
1996 void olimex_jtag_blink(void)
1998 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1999 * ACBUS3 is bit 3 of the GPIOH port
2001 if (high_output & 0x08)
2003 /* set port pin high */
2004 high_output &= 0x07;
2008 /* set port pin low */
2009 high_output |= 0x08;
2013 BUFFER_ADD = high_output;
2014 BUFFER_ADD = high_direction;
2017 void turtle_jtag_blink(void)
2020 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2022 if (high_output & 0x08)
2032 BUFFER_ADD = high_output;
2033 BUFFER_ADD = high_direction;
2037 int ft2232_quit(void)
2039 #if BUILD_FT2232_FTD2XX == 1
2042 status = FT_Close(ftdih);
2043 #elif BUILD_FT2232_LIBFTDI == 1
2044 ftdi_disable_bitbang(&ftdic);
2046 ftdi_usb_close(&ftdic);
2048 ftdi_deinit(&ftdic);
2051 free(ft2232_buffer);
2052 ft2232_buffer = NULL;
2057 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2061 ft2232_device_desc = strdup(args[0]);
2065 ERROR("expected exactly one argument to ft2232_device_desc <description>");
2071 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2075 ft2232_serial = strdup(args[0]);
2079 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2085 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2090 ft2232_layout = malloc(strlen(args[0]) + 1);
2091 strcpy(ft2232_layout, args[0]);
2096 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2100 if (argc > MAX_USB_IDS*2) {
2101 WARNING("ignoring extra IDs in ft2232_vid_pid "
2102 "(maximum is %d pairs)", MAX_USB_IDS);
2103 argc = MAX_USB_IDS*2;
2105 if (argc < 2 || (argc & 1))
2107 WARNING("incomplete ft2232_vid_pid configuration directive");
2112 for (i = 0; i+1 < argc; i += 2) {
2113 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2114 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2117 * Explicitly terminate, in case there are multiples instances of
2120 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2125 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2129 ft2232_latency = atoi(args[0]);
2133 ERROR("expected exactly one argument to ft2232_latency <ms>");