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);
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);
112 /* blink procedures for layouts that support a blinking led */
113 void olimex_jtag_blink(void);
114 void turtle_jtag_blink(void);
116 ft2232_layout_t ft2232_layouts[] =
118 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
119 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
120 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
121 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
122 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
123 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
124 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
125 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
126 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
127 {"comstick", comstick_init, comstick_reset, NULL},
131 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
133 static ft2232_layout_t *layout;
134 static u8 low_output = 0x0;
135 static u8 low_direction = 0x0;
136 static u8 high_output = 0x0;
137 static u8 high_direction = 0x0;
139 #if BUILD_FT2232_FTD2XX == 1
140 static FT_HANDLE ftdih = NULL;
141 #elif BUILD_FT2232_LIBFTDI == 1
142 static struct ftdi_context ftdic;
145 static u8 *ft2232_buffer = NULL;
146 static int ft2232_buffer_size = 0;
147 static int ft2232_read_pointer = 0;
148 static int ft2232_expect_read = 0;
149 #define FT2232_BUFFER_SIZE 131072
150 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
151 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
153 jtag_interface_t ft2232_interface =
158 .execute_queue = ft2232_execute_queue,
160 .support_pathmove = 1,
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 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 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 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 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 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 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 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 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]);
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 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 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 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 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 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 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 /* command "Clock Data to TMS/CS Pin (no Read)" */
463 /* number of states remaining */
464 BUFFER_ADD = (num_states % 7) - 1;
466 while (num_states % 7)
468 if (tap_transitions[cur_state].low == cmd->path[state_count])
469 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
470 else if (tap_transitions[cur_state].high == cmd->path[state_count])
471 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
474 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
478 cur_state = cmd->path[state_count];
483 BUFFER_ADD = tms_byte;
486 end_state = cur_state;
489 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
491 int num_bytes = (scan_size + 7) / 8;
492 int bits_left = scan_size;
496 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
498 /* command "Clock Data to TMS/CS Pin (no Read)" */
505 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
510 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
513 //DEBUG("added TMS scan (no read)");
516 /* add command for complete bytes */
517 while (num_bytes > 1)
522 /* Clock Data Bytes In and Out LSB First */
524 //DEBUG("added TDI bytes (io %i)", num_bytes);
526 else if (type == SCAN_OUT)
528 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
530 //DEBUG("added TDI bytes (o)");
532 else if (type == SCAN_IN)
534 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
536 //DEBUG("added TDI bytes (i %i)", num_bytes);
538 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
539 num_bytes -= thisrun_bytes;
540 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
541 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
544 /* add complete bytes */
545 while(thisrun_bytes-- > 0)
547 BUFFER_ADD = buffer[cur_byte];
552 else /* (type == SCAN_IN) */
554 bits_left -= 8 * (thisrun_bytes);
558 /* the most signifcant bit is scanned during TAP movement */
560 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
564 /* process remaining bits but the last one */
569 /* Clock Data Bits In and Out LSB First */
571 //DEBUG("added TDI bits (io) %i", bits_left - 1);
573 else if (type == SCAN_OUT)
575 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
577 //DEBUG("added TDI bits (o)");
579 else if (type == SCAN_IN)
581 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
583 //DEBUG("added TDI bits (i %i)", bits_left - 1);
585 BUFFER_ADD = bits_left - 2;
587 BUFFER_ADD = buffer[cur_byte];
590 if ((ir_scan && (end_state == TAP_SI)) ||
591 (!ir_scan && (end_state == TAP_SD)))
595 /* Clock Data Bits In and Out LSB First */
597 //DEBUG("added TDI bits (io) %i", bits_left - 1);
599 else if (type == SCAN_OUT)
601 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
603 //DEBUG("added TDI bits (o)");
605 else if (type == SCAN_IN)
607 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
609 //DEBUG("added TDI bits (i %i)", bits_left - 1);
612 BUFFER_ADD = last_bit;
616 /* move from Shift-IR/DR to end state */
617 if (type != SCAN_OUT)
619 /* Clock Data to TMS/CS Pin with Read */
621 //DEBUG("added TMS scan (read)");
625 /* Clock Data to TMS/CS Pin (no Read) */
627 //DEBUG("added TMS scan (no read)");
630 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
631 cur_state = end_state;
635 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
637 int num_bytes = (scan_size + 7) / 8;
638 int bits_left = scan_size;
641 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
642 u8 *receive_pointer = receive_buffer;
646 int thisrun_read = 0;
650 ERROR("BUG: large IR scans are not supported");
654 if (cur_state != TAP_SD)
656 /* command "Clock Data to TMS/CS Pin (no Read)" */
661 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
665 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
667 ERROR("couldn't write MPSSE commands to FT2232");
670 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
671 ft2232_buffer_size = 0;
673 /* add command for complete bytes */
674 while (num_bytes > 1)
680 /* Clock Data Bytes In and Out LSB First */
682 //DEBUG("added TDI bytes (io %i)", num_bytes);
684 else if (type == SCAN_OUT)
686 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
688 //DEBUG("added TDI bytes (o)");
690 else if (type == SCAN_IN)
692 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
694 //DEBUG("added TDI bytes (i %i)", num_bytes);
696 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
697 thisrun_read = thisrun_bytes;
698 num_bytes -= thisrun_bytes;
699 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
700 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
703 /* add complete bytes */
704 while(thisrun_bytes-- > 0)
706 BUFFER_ADD = buffer[cur_byte];
711 else /* (type == SCAN_IN) */
713 bits_left -= 8 * (thisrun_bytes);
716 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
718 ERROR("couldn't write MPSSE commands to FT2232");
721 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
722 ft2232_buffer_size = 0;
724 if (type != SCAN_OUT)
726 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
728 ERROR("couldn't read from FT2232");
731 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
732 receive_pointer += bytes_read;
738 /* the most signifcant bit is scanned during TAP movement */
740 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
744 /* process remaining bits but the last one */
749 /* Clock Data Bits In and Out LSB First */
751 //DEBUG("added TDI bits (io) %i", bits_left - 1);
753 else if (type == SCAN_OUT)
755 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
757 //DEBUG("added TDI bits (o)");
759 else if (type == SCAN_IN)
761 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
763 //DEBUG("added TDI bits (i %i)", bits_left - 1);
765 BUFFER_ADD = bits_left - 2;
767 BUFFER_ADD = buffer[cur_byte];
769 if (type != SCAN_OUT)
773 if (end_state == TAP_SD)
777 /* Clock Data Bits In and Out LSB First */
779 //DEBUG("added TDI bits (io) %i", bits_left - 1);
781 else if (type == SCAN_OUT)
783 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
785 //DEBUG("added TDI bits (o)");
787 else if (type == SCAN_IN)
789 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
791 //DEBUG("added TDI bits (i %i)", bits_left - 1);
794 BUFFER_ADD = last_bit;
798 /* move from Shift-IR/DR to end state */
799 if (type != SCAN_OUT)
801 /* Clock Data to TMS/CS Pin with Read */
803 //DEBUG("added TMS scan (read)");
807 /* Clock Data to TMS/CS Pin (no Read) */
809 //DEBUG("added TMS scan (no read)");
812 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
813 cur_state = end_state;
816 if (type != SCAN_OUT)
819 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
821 ERROR("couldn't write MPSSE commands to FT2232");
824 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
825 ft2232_buffer_size = 0;
827 if (type != SCAN_OUT)
829 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
831 ERROR("couldn't read from FT2232");
834 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
835 receive_pointer += bytes_read;
841 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
843 int predicted_size = 3;
844 int num_bytes = (scan_size - 1) / 8;
846 if (cur_state != TAP_SD)
849 if (type == SCAN_IN) /* only from device to host */
852 predicted_size += (CEIL(num_bytes, 65536)) * 3;
853 /* remaining bits - 1 (up to 7) */
854 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
856 else /* host to device, or bidirectional */
859 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
860 /* remaining bits -1 (up to 7) */
861 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
864 return predicted_size;
867 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
869 int predicted_size = 0;
871 if (type != SCAN_OUT)
874 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
875 /* remaining bits - 1 */
876 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
877 /* last bit (from TMS scan) */
881 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
883 return predicted_size;
886 void usbjtag_reset(int trst, int srst)
891 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
892 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
894 low_output &= ~nTRST; /* switch output low */
898 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
899 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
901 low_output |= nTRST; /* switch output high */
906 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
907 low_output &= ~nSRST; /* switch output low */
909 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
913 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
914 low_output |= nSRST; /* switch output high */
916 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
919 /* command "set data bits low byte" */
921 BUFFER_ADD = low_output;
922 BUFFER_ADD = low_direction;
926 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 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)
971 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
972 high_output &= ~nTRSTnOE;
974 high_output &= ~nTRST;
978 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
979 high_output |= nTRSTnOE;
981 high_output |= nTRST;
986 high_output |= nSRST;
990 high_output &= ~nSRST;
993 /* command "set data bits high byte" */
995 BUFFER_ADD = high_output;
996 BUFFER_ADD = high_direction;
997 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1000 void flyswatter_reset(int trst, int srst)
1004 cur_state = TAP_TLR;
1005 low_output &= ~nTRST;
1009 low_output |= nTRST;
1014 low_output |= nSRST;
1018 low_output &= ~nSRST;
1021 /* command "set data bits low byte" */
1023 BUFFER_ADD = low_output;
1024 BUFFER_ADD = low_direction;
1025 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1028 void turtle_reset(int trst, int srst)
1034 low_output |= nSRST;
1038 low_output &= ~nSRST;
1041 /* command "set data bits low byte" */
1043 BUFFER_ADD = low_output;
1044 BUFFER_ADD = low_direction;
1045 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1048 void comstick_reset(int trst, int srst)
1052 cur_state = TAP_TLR;
1053 high_output &= ~nTRST;
1057 high_output |= nTRST;
1062 high_output &= ~nSRST;
1066 high_output |= nSRST;
1069 /* command "set data bits high byte" */
1071 BUFFER_ADD = high_output;
1072 BUFFER_ADD = high_direction;
1073 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1076 int ft2232_execute_queue()
1078 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1079 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1081 int scan_size; /* size of IR or DR scan */
1082 enum scan_type type;
1084 int predicted_size = 0;
1085 int require_send = 0;
1088 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1089 * that wasn't handled by a caller-provided error handler
1093 ft2232_buffer_size = 0;
1094 ft2232_expect_read = 0;
1096 /* blink, if the current layout has that feature */
1104 case JTAG_END_STATE:
1105 if (cmd->cmd.end_state->end_state != -1)
1106 ft2232_end_state(cmd->cmd.end_state->end_state);
1109 /* only send the maximum buffer size that FT2232C can handle */
1111 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1113 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1114 retval = ERROR_JTAG_QUEUE_FAILED;
1119 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1122 #ifdef _DEBUG_JTAG_IO_
1123 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1127 /* only send the maximum buffer size that FT2232C can handle */
1129 if (cur_state != TAP_RTI)
1130 predicted_size += 3;
1131 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1132 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1133 predicted_size += 3;
1134 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1135 predicted_size += 3;
1136 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1138 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1139 retval = ERROR_JTAG_QUEUE_FAILED;
1143 if (cur_state != TAP_RTI)
1145 /* command "Clock Data to TMS/CS Pin (no Read)" */
1150 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1151 cur_state = TAP_RTI;
1154 i = cmd->cmd.runtest->num_cycles;
1157 /* command "Clock Data to TMS/CS Pin (no Read)" */
1160 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1163 cur_state = TAP_RTI;
1164 i -= (i > 7) ? 7 : i;
1165 //DEBUG("added TMS scan (no read)");
1167 if (cmd->cmd.runtest->end_state != -1)
1168 ft2232_end_state(cmd->cmd.runtest->end_state);
1169 if (cur_state != end_state)
1171 /* command "Clock Data to TMS/CS Pin (no Read)" */
1176 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1177 cur_state = end_state;
1178 //DEBUG("added TMS scan (no read)");
1181 #ifdef _DEBUG_JTAG_IO_
1182 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1185 case JTAG_STATEMOVE:
1186 /* only send the maximum buffer size that FT2232C can handle */
1188 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1190 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1191 retval = ERROR_JTAG_QUEUE_FAILED;
1195 if (cmd->cmd.statemove->end_state != -1)
1196 ft2232_end_state(cmd->cmd.statemove->end_state);
1197 /* command "Clock Data to TMS/CS Pin (no Read)" */
1202 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1203 //DEBUG("added TMS scan (no read)");
1204 cur_state = end_state;
1206 #ifdef _DEBUG_JTAG_IO_
1207 DEBUG("statemove: %i", end_state);
1211 /* only send the maximum buffer size that FT2232C can handle */
1212 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1213 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1215 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1216 retval = ERROR_JTAG_QUEUE_FAILED;
1220 ft2232_add_pathmove(cmd->cmd.pathmove);
1222 #ifdef _DEBUG_JTAG_IO_
1223 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1227 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1228 type = jtag_scan_type(cmd->cmd.scan);
1229 predicted_size = ft2232_predict_scan_out(scan_size, type);
1230 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1232 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1233 /* unsent commands before this */
1234 if (first_unsent != cmd)
1235 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1236 retval = ERROR_JTAG_QUEUE_FAILED;
1238 /* current command */
1239 if (cmd->cmd.scan->end_state != -1)
1240 ft2232_end_state(cmd->cmd.scan->end_state);
1241 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1243 first_unsent = cmd->next;
1248 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1250 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1251 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1252 retval = ERROR_JTAG_QUEUE_FAILED;
1256 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1257 //DEBUG("new read size: %i", ft2232_expect_read);
1258 if (cmd->cmd.scan->end_state != -1)
1259 ft2232_end_state(cmd->cmd.scan->end_state);
1260 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1264 #ifdef _DEBUG_JTAG_IO_
1265 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1269 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1270 retval = ERROR_JTAG_QUEUE_FAILED;
1271 first_unsent = cmd->next;
1272 jtag_sleep(cmd->cmd.sleep->us);
1273 #ifdef _DEBUG_JTAG_IO_
1274 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1278 ERROR("BUG: unknown JTAG command type encountered");
1284 if (require_send > 0)
1285 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1286 retval = ERROR_JTAG_QUEUE_FAILED;
1291 #if BUILD_FT2232_FTD2XX == 1
1292 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1295 DWORD openex_flags = 0;
1296 char *openex_string = NULL;
1299 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1300 ft2232_layout, vid, pid);
1303 /* Add non-standard Vid/Pid to the linux driver */
1304 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1306 WARNING("couldn't add %4.4x:%4.4x",
1311 if (ft2232_device_desc && ft2232_serial)
1313 WARNING("can't open by device description and serial number, giving precedence to serial");
1314 ft2232_device_desc = NULL;
1317 if (ft2232_device_desc)
1319 openex_string = ft2232_device_desc;
1320 openex_flags = FT_OPEN_BY_DESCRIPTION;
1322 else if (ft2232_serial)
1324 openex_string = ft2232_serial;
1325 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1329 ERROR("neither device description nor serial number specified");
1330 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1332 return ERROR_JTAG_INIT_FAILED;
1335 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1340 WARNING("unable to open ftdi device (trying more): %lu",
1343 return ERROR_JTAG_INIT_FAILED;
1345 ERROR("unable to open ftdi device: %lu", status);
1346 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1347 if (status == FT_OK)
1349 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1352 for (i = 0; i < num_devices; i++)
1353 desc_array[i] = malloc(64);
1354 desc_array[num_devices] = NULL;
1356 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1358 if (status == FT_OK)
1360 ERROR("ListDevices: %lu\n", num_devices);
1361 for (i = 0; i < num_devices; i++)
1362 ERROR("%i: %s", i, desc_array[i]);
1365 for (i = 0; i < num_devices; i++)
1366 free(desc_array[i]);
1371 printf("ListDevices: NONE\n");
1373 return ERROR_JTAG_INIT_FAILED;
1376 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1378 ERROR("unable to set latency timer: %lu", status);
1379 return ERROR_JTAG_INIT_FAILED;
1382 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1384 ERROR("unable to get latency timer: %lu", status);
1385 return ERROR_JTAG_INIT_FAILED;
1389 DEBUG("current latency timer: %i", latency_timer);
1392 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1394 ERROR("unable to set timeouts: %lu", status);
1395 return ERROR_JTAG_INIT_FAILED;
1398 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1400 ERROR("unable to enable bit i/o mode: %lu", status);
1401 return ERROR_JTAG_INIT_FAILED;
1407 static int ft2232_purge_ftd2xx(void)
1411 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1413 ERROR("error purging ftd2xx device: %lu", status);
1414 return ERROR_JTAG_INIT_FAILED;
1419 #endif /* BUILD_FT2232_FTD2XX == 1 */
1421 #if BUILD_FT2232_LIBFTDI == 1
1422 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1426 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1427 ft2232_layout, vid, pid);
1429 if (ftdi_init(&ftdic) < 0)
1430 return ERROR_JTAG_INIT_FAILED;
1432 /* context, vendor id, product id */
1433 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1434 ft2232_serial) < 0) {
1436 WARNING("unable to open ftdi device (trying more): %s",
1439 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1441 return ERROR_JTAG_INIT_FAILED;
1444 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1446 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1447 return ERROR_JTAG_INIT_FAILED;
1450 if (ftdi_usb_reset(&ftdic) < 0)
1452 ERROR("unable to reset ftdi device");
1453 return ERROR_JTAG_INIT_FAILED;
1456 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1458 ERROR("unable to set latency timer");
1459 return ERROR_JTAG_INIT_FAILED;
1462 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1464 ERROR("unable to get latency timer");
1465 return ERROR_JTAG_INIT_FAILED;
1469 DEBUG("current latency timer: %i", latency_timer);
1472 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1477 static int ft2232_purge_libftdi(void)
1479 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1481 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1482 return ERROR_JTAG_INIT_FAILED;
1487 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1489 int ft2232_init(void)
1494 ft2232_layout_t *cur_layout = ft2232_layouts;
1497 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1499 ft2232_layout = "usbjtag";
1500 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1503 while (cur_layout->name)
1505 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1507 layout = cur_layout;
1515 ERROR("No matching layout found for %s", ft2232_layout);
1516 return ERROR_JTAG_INIT_FAILED;
1519 for (i = 0; 1; i++) {
1521 * "more indicates that there are more IDs to try, so we should
1522 * not print an error for an ID mismatch (but for anything
1525 * try_more indicates that the error code returned indicates an
1526 * ID mismatch (and nothing else) and that we should proceeed
1527 * with the next ID pair.
1529 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1532 #if BUILD_FT2232_FTD2XX == 1
1533 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1535 #elif BUILD_FT2232_LIBFTDI == 1
1536 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1541 if (!more || !try_more)
1545 ft2232_buffer_size = 0;
1546 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1548 if (layout->init() != ERROR_OK)
1549 return ERROR_JTAG_INIT_FAILED;
1551 ft2232_speed(jtag_speed);
1553 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1554 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1556 ERROR("couldn't write to FT2232 to disable loopback");
1557 return ERROR_JTAG_INIT_FAILED;
1560 #if BUILD_FT2232_FTD2XX == 1
1561 return ft2232_purge_ftd2xx();
1562 #elif BUILD_FT2232_LIBFTDI == 1
1563 return ft2232_purge_libftdi();
1569 int usbjtag_init(void)
1575 low_direction = 0x0b;
1577 if (strcmp(ft2232_layout, "usbjtag") == 0)
1584 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1591 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1598 low_direction = 0x8b;
1602 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1603 return ERROR_JTAG_INIT_FAILED;
1606 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1608 low_direction &= ~nTRSTnOE; /* nTRST input */
1609 low_output &= ~nTRST; /* nTRST = 0 */
1613 low_direction |= nTRSTnOE; /* nTRST output */
1614 low_output |= nTRST; /* nTRST = 1 */
1617 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1619 low_direction |= nSRSTnOE; /* nSRST output */
1620 low_output |= nSRST; /* nSRST = 1 */
1624 low_direction &= ~nSRSTnOE; /* nSRST input */
1625 low_output &= ~nSRST; /* nSRST = 0 */
1628 /* initialize low byte for jtag */
1629 buf[0] = 0x80; /* command "set data bits low byte" */
1630 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1631 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1632 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1634 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1636 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1637 return ERROR_JTAG_INIT_FAILED;
1643 int jtagkey_init(void)
1649 low_direction = 0x1b;
1651 /* initialize low byte for jtag */
1652 buf[0] = 0x80; /* command "set data bits low byte" */
1653 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1654 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1655 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1657 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1659 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1660 return ERROR_JTAG_INIT_FAILED;
1663 if (strcmp(layout->name, "jtagkey") == 0)
1670 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1671 (strcmp(layout->name, "oocdlink") == 0))
1680 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1685 high_direction = 0x0f;
1687 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1689 high_output |= nTRSTnOE;
1690 high_output &= ~nTRST;
1694 high_output &= ~nTRSTnOE;
1695 high_output |= nTRST;
1698 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1700 high_output &= ~nSRSTnOE;
1701 high_output |= nSRST;
1705 high_output |= nSRSTnOE;
1706 high_output &= ~nSRST;
1709 /* initialize high port */
1710 buf[0] = 0x82; /* command "set data bits high byte" */
1711 buf[1] = high_output; /* value */
1712 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1713 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1715 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1717 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1718 return ERROR_JTAG_INIT_FAILED;
1724 int olimex_jtag_init(void)
1730 low_direction = 0x1b;
1732 /* initialize low byte for jtag */
1733 buf[0] = 0x80; /* command "set data bits low byte" */
1734 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1735 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1736 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1738 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1740 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1741 return ERROR_JTAG_INIT_FAILED;
1747 nSRSTnOE = 0x00; /* no output enable for nSRST */
1750 high_direction = 0x0f;
1752 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1754 high_output |= nTRSTnOE;
1755 high_output &= ~nTRST;
1759 high_output &= ~nTRSTnOE;
1760 high_output |= nTRST;
1763 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1765 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1769 high_output &= ~nSRST;
1772 /* turn red LED on */
1773 high_output |= 0x08;
1775 /* initialize high port */
1776 buf[0] = 0x82; /* command "set data bits high byte" */
1777 buf[1] = high_output; /* value */
1778 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1779 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1781 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1783 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1784 return ERROR_JTAG_INIT_FAILED;
1790 int flyswatter_init(void)
1796 low_direction = 0xfb;
1798 /* initialize low byte for jtag */
1799 buf[0] = 0x80; /* command "set data bits low byte" */
1800 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1801 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1802 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1804 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1806 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1807 return ERROR_JTAG_INIT_FAILED;
1811 nTRSTnOE = 0x0; /* not output enable for nTRST */
1813 nSRSTnOE = 0x00; /* no output enable for nSRST */
1816 high_direction = 0x0c;
1818 /* turn red LED1 on, LED2 off */
1819 high_output |= 0x08;
1821 /* initialize high port */
1822 buf[0] = 0x82; /* command "set data bits high byte" */
1823 buf[1] = high_output; /* value */
1824 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1825 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1827 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1829 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1830 return ERROR_JTAG_INIT_FAILED;
1836 int turtle_init(void)
1842 low_direction = 0x5b;
1844 /* initialize low byte for jtag */
1845 buf[0] = 0x80; /* command "set data bits low byte" */
1846 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1847 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1848 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1850 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1852 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1853 return ERROR_JTAG_INIT_FAILED;
1859 high_direction = 0x0C;
1861 /* initialize high port */
1862 buf[0] = 0x82; /* command "set data bits high byte" */
1863 buf[1] = high_output;
1864 buf[2] = high_direction;
1865 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1867 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1869 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1870 return ERROR_JTAG_INIT_FAILED;
1876 int comstick_init(void)
1882 low_direction = 0x0b;
1884 /* initialize low byte for jtag */
1885 buf[0] = 0x80; /* command "set data bits low byte" */
1886 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1887 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1888 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1890 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1892 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1893 return ERROR_JTAG_INIT_FAILED;
1897 nTRSTnOE = 0x00; /* no output enable for nTRST */
1899 nSRSTnOE = 0x00; /* no output enable for nSRST */
1902 high_direction = 0x03;
1904 /* initialize high port */
1905 buf[0] = 0x82; /* command "set data bits high byte" */
1906 buf[1] = high_output;
1907 buf[2] = high_direction;
1908 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1910 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1912 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1913 return ERROR_JTAG_INIT_FAILED;
1919 void olimex_jtag_blink(void)
1921 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1922 * ACBUS3 is bit 3 of the GPIOH port
1924 if (high_output & 0x08)
1926 /* set port pin high */
1927 high_output &= 0x07;
1931 /* set port pin low */
1932 high_output |= 0x08;
1936 BUFFER_ADD = high_output;
1937 BUFFER_ADD = high_direction;
1940 void turtle_jtag_blink(void)
1943 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
1945 if (high_output & 0x08)
1955 BUFFER_ADD = high_output;
1956 BUFFER_ADD = high_direction;
1960 int ft2232_quit(void)
1962 #if BUILD_FT2232_FTD2XX == 1
1965 status = FT_Close(ftdih);
1966 #elif BUILD_FT2232_LIBFTDI == 1
1967 ftdi_disable_bitbang(&ftdic);
1969 ftdi_usb_close(&ftdic);
1971 ftdi_deinit(&ftdic);
1974 free(ft2232_buffer);
1979 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1983 ft2232_device_desc = strdup(args[0]);
1987 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1993 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1997 ft2232_serial = strdup(args[0]);
2001 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2007 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2012 ft2232_layout = malloc(strlen(args[0]) + 1);
2013 strcpy(ft2232_layout, args[0]);
2018 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2022 if (argc > MAX_USB_IDS*2) {
2023 WARNING("ignoring extra IDs in ft2232_vid_pid "
2024 "(maximum is %d pairs)", MAX_USB_IDS);
2025 argc = MAX_USB_IDS*2;
2027 if (argc < 2 || (argc & 1))
2029 WARNING("incomplete ft2232_vid_pid configuration directive");
2034 for (i = 0; i+1 < argc; i += 2) {
2035 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2036 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2039 * Explicitly terminate, in case there are multiples instances of
2042 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2047 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2051 ft2232_latency = atoi(args[0]);
2055 ERROR("expected exactly one argument to ft2232_latency <ms>");