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);
77 char *ft2232_device_desc = NULL;
78 char *ft2232_serial = NULL;
79 char *ft2232_layout = NULL;
82 /* vid = pid = 0 marks the end of the list */
83 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
84 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
86 typedef struct ft2232_layout_s
90 void(*reset)(int trst, int srst);
94 /* init procedures for supported layouts */
95 int usbjtag_init(void);
96 int jtagkey_init(void);
97 int olimex_jtag_init(void);
98 int flyswatter_init(void);
99 int turtle_init(void);
100 int comstick_init(void);
102 /* reset procedures for supported layouts */
103 void usbjtag_reset(int trst, int srst);
104 void jtagkey_reset(int trst, int srst);
105 void olimex_jtag_reset(int trst, int srst);
106 void flyswatter_reset(int trst, int srst);
107 void turtle_reset(int trst, int srst);
108 void comstick_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},
129 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
131 static ft2232_layout_t *layout;
132 static u8 low_output = 0x0;
133 static u8 low_direction = 0x0;
134 static u8 high_output = 0x0;
135 static u8 high_direction = 0x0;
137 #if BUILD_FT2232_FTD2XX == 1
138 static FT_HANDLE ftdih = NULL;
139 #elif BUILD_FT2232_LIBFTDI == 1
140 static struct ftdi_context ftdic;
143 static u8 *ft2232_buffer = NULL;
144 static int ft2232_buffer_size = 0;
145 static int ft2232_read_pointer = 0;
146 static int ft2232_expect_read = 0;
147 #define FT2232_BUFFER_SIZE 131072
148 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
149 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
151 jtag_interface_t ft2232_interface =
156 .execute_queue = ft2232_execute_queue,
158 .support_pathmove = 1,
160 .speed = ft2232_speed,
161 .register_commands = ft2232_register_commands,
166 int ft2232_write(u8 *buf, int size, u32* bytes_written)
168 #if BUILD_FT2232_FTD2XX == 1
170 DWORD dw_bytes_written;
171 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
173 *bytes_written = dw_bytes_written;
174 ERROR("FT_Write returned: %lu", status);
175 return ERROR_JTAG_DEVICE_ERROR;
179 *bytes_written = dw_bytes_written;
182 #elif BUILD_FT2232_LIBFTDI == 1
184 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
187 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
188 return ERROR_JTAG_DEVICE_ERROR;
192 *bytes_written = retval;
198 int ft2232_read(u8* buf, int size, u32* bytes_read)
200 #if BUILD_FT2232_FTD2XX == 1
206 while ((*bytes_read < size) && timeout--)
208 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
209 *bytes_read, &dw_bytes_read)) != FT_OK)
212 ERROR("FT_Read returned: %lu", status);
213 return ERROR_JTAG_DEVICE_ERROR;
215 *bytes_read += dw_bytes_read;
217 #elif BUILD_FT2232_LIBFTDI == 1
222 while ((*bytes_read < size) && timeout--)
224 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
227 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
228 return ERROR_JTAG_DEVICE_ERROR;
230 *bytes_read += retval;
234 if (*bytes_read < size)
236 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
237 return ERROR_JTAG_DEVICE_ERROR;
243 int ft2232_speed(int speed)
249 buf[0] = 0x86; /* command "set divisor" */
250 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
251 buf[2] = (speed >> 8) & 0xff; /* valueH */
253 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
254 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
256 ERROR("couldn't set FT2232 TCK speed");
265 int ft2232_register_commands(struct command_context_s *cmd_ctx)
267 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
268 COMMAND_CONFIG, NULL);
269 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
270 COMMAND_CONFIG, NULL);
271 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
272 COMMAND_CONFIG, NULL);
273 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
274 COMMAND_CONFIG, NULL);
278 void ft2232_end_state(enum tap_state state)
280 if (tap_move_map[state] != -1)
284 ERROR("BUG: %i is not a valid end state", state);
289 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
291 int num_bytes = ((scan_size + 7) / 8);
292 int bits_left = scan_size;
295 while(num_bytes-- > 1)
297 buffer[cur_byte] = BUFFER_READ;
302 buffer[cur_byte] = 0x0;
306 buffer[cur_byte] = BUFFER_READ >> 1;
309 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
313 void ft2232_debug_dump_buffer(void)
319 for (i = 0; i < ft2232_buffer_size; i++)
321 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
333 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
343 #ifdef _DEBUG_USB_IO_
344 struct timeval start, inter, inter2, end;
345 struct timeval d_inter, d_inter2, d_end;
348 #ifdef _DEBUG_USB_COMMS_
349 DEBUG("write buffer (size %i):", ft2232_buffer_size);
350 ft2232_debug_dump_buffer();
353 #ifdef _DEBUG_USB_IO_
354 gettimeofday(&start, NULL);
357 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
359 ERROR("couldn't write MPSSE commands to FT2232");
363 #ifdef _DEBUG_USB_IO_
364 gettimeofday(&inter, NULL);
367 if (ft2232_expect_read)
370 ft2232_buffer_size = 0;
372 #ifdef _DEBUG_USB_IO_
373 gettimeofday(&inter2, NULL);
376 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
378 ERROR("couldn't read from FT2232");
382 #ifdef _DEBUG_USB_IO_
383 gettimeofday(&end, NULL);
385 timeval_subtract(&d_inter, &inter, &start);
386 timeval_subtract(&d_inter2, &inter2, &start);
387 timeval_subtract(&d_end, &end, &start);
389 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);
393 ft2232_buffer_size = bytes_read;
395 if (ft2232_expect_read != ft2232_buffer_size)
397 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
398 ft2232_debug_dump_buffer();
403 #ifdef _DEBUG_USB_COMMS_
404 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
405 ft2232_debug_dump_buffer();
409 ft2232_expect_read = 0;
410 ft2232_read_pointer = 0;
412 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
413 * that wasn't handled by a caller-provided error handler
423 type = jtag_scan_type(cmd->cmd.scan);
424 if (type != SCAN_OUT)
426 scan_size = jtag_scan_size(cmd->cmd.scan);
427 buffer = calloc(CEIL(scan_size, 8), 1);
428 ft2232_read_scan(type, buffer, scan_size);
429 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
430 retval = ERROR_JTAG_QUEUE_FAILED;
440 ft2232_buffer_size = 0;
445 void ft2232_add_pathmove(pathmove_command_t *cmd)
447 int num_states = cmd->num_states;
457 /* command "Clock Data to TMS/CS Pin (no Read)" */
459 /* number of states remaining */
460 BUFFER_ADD = (num_states % 7) - 1;
462 while (num_states % 7)
464 if (tap_transitions[cur_state].low == cmd->path[state_count])
465 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
466 else if (tap_transitions[cur_state].high == cmd->path[state_count])
467 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
470 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
474 cur_state = cmd->path[state_count];
479 BUFFER_ADD = tms_byte;
482 end_state = cur_state;
485 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
487 int num_bytes = (scan_size + 7) / 8;
488 int bits_left = scan_size;
492 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
494 /* command "Clock Data to TMS/CS Pin (no Read)" */
501 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
506 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
509 //DEBUG("added TMS scan (no read)");
512 /* add command for complete bytes */
513 while (num_bytes > 1)
518 /* Clock Data Bytes In and Out LSB First */
520 //DEBUG("added TDI bytes (io %i)", num_bytes);
522 else if (type == SCAN_OUT)
524 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
526 //DEBUG("added TDI bytes (o)");
528 else if (type == SCAN_IN)
530 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
532 //DEBUG("added TDI bytes (i %i)", num_bytes);
534 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
535 num_bytes -= thisrun_bytes;
536 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
537 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
540 /* add complete bytes */
541 while(thisrun_bytes-- > 0)
543 BUFFER_ADD = buffer[cur_byte];
548 else /* (type == SCAN_IN) */
550 bits_left -= 8 * (thisrun_bytes);
554 /* the most signifcant bit is scanned during TAP movement */
556 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
560 /* process remaining bits but the last one */
565 /* Clock Data Bits In and Out LSB First */
567 //DEBUG("added TDI bits (io) %i", bits_left - 1);
569 else if (type == SCAN_OUT)
571 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
573 //DEBUG("added TDI bits (o)");
575 else if (type == SCAN_IN)
577 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
579 //DEBUG("added TDI bits (i %i)", bits_left - 1);
581 BUFFER_ADD = bits_left - 2;
583 BUFFER_ADD = buffer[cur_byte];
586 if ((ir_scan && (end_state == TAP_SI)) ||
587 (!ir_scan && (end_state == TAP_SD)))
591 /* Clock Data Bits In and Out LSB First */
593 //DEBUG("added TDI bits (io) %i", bits_left - 1);
595 else if (type == SCAN_OUT)
597 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
599 //DEBUG("added TDI bits (o)");
601 else if (type == SCAN_IN)
603 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
605 //DEBUG("added TDI bits (i %i)", bits_left - 1);
608 BUFFER_ADD = last_bit;
612 /* move from Shift-IR/DR to end state */
613 if (type != SCAN_OUT)
615 /* Clock Data to TMS/CS Pin with Read */
617 //DEBUG("added TMS scan (read)");
621 /* Clock Data to TMS/CS Pin (no Read) */
623 //DEBUG("added TMS scan (no read)");
626 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
627 cur_state = end_state;
631 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
633 int num_bytes = (scan_size + 7) / 8;
634 int bits_left = scan_size;
637 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
638 u8 *receive_pointer = receive_buffer;
642 int thisrun_read = 0;
646 ERROR("BUG: large IR scans are not supported");
650 if (cur_state != TAP_SD)
652 /* command "Clock Data to TMS/CS Pin (no Read)" */
657 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
661 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
663 ERROR("couldn't write MPSSE commands to FT2232");
666 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
667 ft2232_buffer_size = 0;
669 /* add command for complete bytes */
670 while (num_bytes > 1)
676 /* Clock Data Bytes In and Out LSB First */
678 //DEBUG("added TDI bytes (io %i)", num_bytes);
680 else if (type == SCAN_OUT)
682 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
684 //DEBUG("added TDI bytes (o)");
686 else if (type == SCAN_IN)
688 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
690 //DEBUG("added TDI bytes (i %i)", num_bytes);
692 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
693 thisrun_read = thisrun_bytes;
694 num_bytes -= thisrun_bytes;
695 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
696 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
699 /* add complete bytes */
700 while(thisrun_bytes-- > 0)
702 BUFFER_ADD = buffer[cur_byte];
707 else /* (type == SCAN_IN) */
709 bits_left -= 8 * (thisrun_bytes);
712 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
714 ERROR("couldn't write MPSSE commands to FT2232");
717 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
718 ft2232_buffer_size = 0;
720 if (type != SCAN_OUT)
722 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
724 ERROR("couldn't read from FT2232");
727 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
728 receive_pointer += bytes_read;
734 /* the most signifcant bit is scanned during TAP movement */
736 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
740 /* process remaining bits but the last one */
745 /* Clock Data Bits In and Out LSB First */
747 //DEBUG("added TDI bits (io) %i", bits_left - 1);
749 else if (type == SCAN_OUT)
751 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
753 //DEBUG("added TDI bits (o)");
755 else if (type == SCAN_IN)
757 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
759 //DEBUG("added TDI bits (i %i)", bits_left - 1);
761 BUFFER_ADD = bits_left - 2;
763 BUFFER_ADD = buffer[cur_byte];
765 if (type != SCAN_OUT)
769 if (end_state == TAP_SD)
773 /* Clock Data Bits In and Out LSB First */
775 //DEBUG("added TDI bits (io) %i", bits_left - 1);
777 else if (type == SCAN_OUT)
779 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
781 //DEBUG("added TDI bits (o)");
783 else if (type == SCAN_IN)
785 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
787 //DEBUG("added TDI bits (i %i)", bits_left - 1);
790 BUFFER_ADD = last_bit;
794 /* move from Shift-IR/DR to end state */
795 if (type != SCAN_OUT)
797 /* Clock Data to TMS/CS Pin with Read */
799 //DEBUG("added TMS scan (read)");
803 /* Clock Data to TMS/CS Pin (no Read) */
805 //DEBUG("added TMS scan (no read)");
808 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
809 cur_state = end_state;
812 if (type != SCAN_OUT)
815 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
817 ERROR("couldn't write MPSSE commands to FT2232");
820 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
821 ft2232_buffer_size = 0;
823 if (type != SCAN_OUT)
825 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
827 ERROR("couldn't read from FT2232");
830 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
831 receive_pointer += bytes_read;
837 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
839 int predicted_size = 3;
840 int num_bytes = (scan_size - 1) / 8;
842 if (cur_state != TAP_SD)
845 if (type == SCAN_IN) /* only from device to host */
848 predicted_size += (CEIL(num_bytes, 65536)) * 3;
849 /* remaining bits - 1 (up to 7) */
850 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
852 else /* host to device, or bidirectional */
855 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
856 /* remaining bits -1 (up to 7) */
857 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
860 return predicted_size;
863 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
865 int predicted_size = 0;
867 if (type != SCAN_OUT)
870 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
871 /* remaining bits - 1 */
872 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
873 /* last bit (from TMS scan) */
877 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
879 return predicted_size;
882 void usbjtag_reset(int trst, int srst)
887 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
888 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
890 low_output &= ~nTRST; /* switch output low */
894 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
895 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
897 low_output |= nTRST; /* switch output high */
902 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
903 low_output &= ~nSRST; /* switch output low */
905 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
909 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
910 low_output |= nSRST; /* switch output high */
912 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
915 /* command "set data bits low byte" */
917 BUFFER_ADD = low_output;
918 BUFFER_ADD = low_direction;
922 void jtagkey_reset(int trst, int srst)
927 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
928 high_output &= ~nTRSTnOE;
930 high_output &= ~nTRST;
934 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
935 high_output |= nTRSTnOE;
937 high_output |= nTRST;
942 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
943 high_output &= ~nSRST;
945 high_output &= ~nSRSTnOE;
949 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
950 high_output |= nSRST;
952 high_output |= nSRSTnOE;
955 /* command "set data bits high byte" */
957 BUFFER_ADD = high_output;
958 BUFFER_ADD = high_direction;
959 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
962 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 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 cur_state = TAP_TLR;
1001 low_output &= ~nTRST;
1005 low_output |= nTRST;
1010 low_output |= nSRST;
1014 low_output &= ~nSRST;
1017 /* command "set data bits low byte" */
1019 BUFFER_ADD = low_output;
1020 BUFFER_ADD = low_direction;
1021 DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1024 void turtle_reset(int trst, int srst)
1030 low_output |= nSRST;
1034 low_output &= ~nSRST;
1037 /* command "set data bits low byte" */
1039 BUFFER_ADD = low_output;
1040 BUFFER_ADD = low_direction;
1041 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1044 void comstick_reset(int trst, int srst)
1048 cur_state = TAP_TLR;
1049 high_output &= ~nTRST;
1053 high_output |= nTRST;
1058 high_output &= ~nSRST;
1062 high_output |= nSRST;
1065 /* command "set data bits high byte" */
1067 BUFFER_ADD = high_output;
1068 BUFFER_ADD = high_direction;
1069 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1072 int ft2232_execute_queue()
1074 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1075 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1077 int scan_size; /* size of IR or DR scan */
1078 enum scan_type type;
1080 int predicted_size = 0;
1081 int require_send = 0;
1084 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1085 * that wasn't handled by a caller-provided error handler
1089 ft2232_buffer_size = 0;
1090 ft2232_expect_read = 0;
1092 /* blink, if the current layout has that feature */
1100 case JTAG_END_STATE:
1101 if (cmd->cmd.end_state->end_state != -1)
1102 ft2232_end_state(cmd->cmd.end_state->end_state);
1105 /* only send the maximum buffer size that FT2232C can handle */
1107 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1109 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1110 retval = ERROR_JTAG_QUEUE_FAILED;
1115 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1118 #ifdef _DEBUG_JTAG_IO_
1119 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1123 /* only send the maximum buffer size that FT2232C can handle */
1125 if (cur_state != TAP_RTI)
1126 predicted_size += 3;
1127 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1128 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1129 predicted_size += 3;
1130 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1131 predicted_size += 3;
1132 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1134 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1135 retval = ERROR_JTAG_QUEUE_FAILED;
1139 if (cur_state != TAP_RTI)
1141 /* command "Clock Data to TMS/CS Pin (no Read)" */
1146 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1147 cur_state = TAP_RTI;
1150 i = cmd->cmd.runtest->num_cycles;
1153 /* command "Clock Data to TMS/CS Pin (no Read)" */
1156 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1159 cur_state = TAP_RTI;
1160 i -= (i > 7) ? 7 : i;
1161 //DEBUG("added TMS scan (no read)");
1163 if (cmd->cmd.runtest->end_state != -1)
1164 ft2232_end_state(cmd->cmd.runtest->end_state);
1165 if (cur_state != end_state)
1167 /* command "Clock Data to TMS/CS Pin (no Read)" */
1172 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1173 cur_state = end_state;
1174 //DEBUG("added TMS scan (no read)");
1177 #ifdef _DEBUG_JTAG_IO_
1178 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1181 case JTAG_STATEMOVE:
1182 /* only send the maximum buffer size that FT2232C can handle */
1184 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1186 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1187 retval = ERROR_JTAG_QUEUE_FAILED;
1191 if (cmd->cmd.statemove->end_state != -1)
1192 ft2232_end_state(cmd->cmd.statemove->end_state);
1193 /* command "Clock Data to TMS/CS Pin (no Read)" */
1198 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1199 //DEBUG("added TMS scan (no read)");
1200 cur_state = end_state;
1202 #ifdef _DEBUG_JTAG_IO_
1203 DEBUG("statemove: %i", end_state);
1207 /* only send the maximum buffer size that FT2232C can handle */
1208 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1209 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1211 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1212 retval = ERROR_JTAG_QUEUE_FAILED;
1216 ft2232_add_pathmove(cmd->cmd.pathmove);
1218 #ifdef _DEBUG_JTAG_IO_
1219 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1223 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1224 type = jtag_scan_type(cmd->cmd.scan);
1225 predicted_size = ft2232_predict_scan_out(scan_size, type);
1226 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1228 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1229 /* unsent commands before this */
1230 if (first_unsent != cmd)
1231 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1232 retval = ERROR_JTAG_QUEUE_FAILED;
1234 /* current command */
1235 if (cmd->cmd.scan->end_state != -1)
1236 ft2232_end_state(cmd->cmd.scan->end_state);
1237 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1239 first_unsent = cmd->next;
1244 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1246 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1247 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1248 retval = ERROR_JTAG_QUEUE_FAILED;
1252 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1253 //DEBUG("new read size: %i", ft2232_expect_read);
1254 if (cmd->cmd.scan->end_state != -1)
1255 ft2232_end_state(cmd->cmd.scan->end_state);
1256 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1260 #ifdef _DEBUG_JTAG_IO_
1261 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1265 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1266 retval = ERROR_JTAG_QUEUE_FAILED;
1267 first_unsent = cmd->next;
1268 jtag_sleep(cmd->cmd.sleep->us);
1269 #ifdef _DEBUG_JTAG_IO_
1270 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1274 ERROR("BUG: unknown JTAG command type encountered");
1280 if (require_send > 0)
1281 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1282 retval = ERROR_JTAG_QUEUE_FAILED;
1287 #if BUILD_FT2232_FTD2XX == 1
1288 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1291 DWORD openex_flags = 0;
1292 char *openex_string = NULL;
1295 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1296 ft2232_layout, vid, pid);
1299 /* Add non-standard Vid/Pid to the linux driver */
1300 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1302 WARNING("couldn't add %4.4x:%4.4x",
1307 if (ft2232_device_desc && ft2232_serial)
1309 WARNING("can't open by device description and serial number, giving precedence to serial");
1310 ft2232_device_desc = NULL;
1313 if (ft2232_device_desc)
1315 openex_string = ft2232_device_desc;
1316 openex_flags = FT_OPEN_BY_DESCRIPTION;
1318 else if (ft2232_serial)
1320 openex_string = ft2232_serial;
1321 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1325 ERROR("neither device description nor serial number specified");
1326 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1328 return ERROR_JTAG_INIT_FAILED;
1331 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1336 WARNING("unable to open ftdi device (trying more): %lu",
1339 return ERROR_JTAG_INIT_FAILED;
1341 ERROR("unable to open ftdi device: %lu", status);
1342 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1343 if (status == FT_OK)
1345 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1348 for (i = 0; i < num_devices; i++)
1349 desc_array[i] = malloc(64);
1350 desc_array[num_devices] = NULL;
1352 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1354 if (status == FT_OK)
1356 ERROR("ListDevices: %lu\n", num_devices);
1357 for (i = 0; i < num_devices; i++)
1358 ERROR("%i: %s", i, desc_array[i]);
1361 for (i = 0; i < num_devices; i++)
1362 free(desc_array[i]);
1367 printf("ListDevices: NONE\n");
1369 return ERROR_JTAG_INIT_FAILED;
1372 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1374 ERROR("unable to set latency timer: %lu", status);
1375 return ERROR_JTAG_INIT_FAILED;
1378 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1380 ERROR("unable to get latency timer: %lu", status);
1381 return ERROR_JTAG_INIT_FAILED;
1385 DEBUG("current latency timer: %i", latency_timer);
1388 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1390 ERROR("unable to set timeouts: %lu", status);
1391 return ERROR_JTAG_INIT_FAILED;
1394 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1396 ERROR("unable to enable bit i/o mode: %lu", status);
1397 return ERROR_JTAG_INIT_FAILED;
1403 static int ft2232_purge_ftd2xx(void)
1407 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1409 ERROR("error purging ftd2xx device: %lu", status);
1410 return ERROR_JTAG_INIT_FAILED;
1415 #endif /* BUILD_FT2232_FTD2XX == 1 */
1417 #if BUILD_FT2232_LIBFTDI == 1
1418 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1422 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1423 ft2232_layout, vid, pid);
1425 if (ftdi_init(&ftdic) < 0)
1426 return ERROR_JTAG_INIT_FAILED;
1428 /* context, vendor id, product id */
1429 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1430 ft2232_serial) < 0) {
1432 WARNING("unable to open ftdi device (trying more): %s",
1435 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1437 return ERROR_JTAG_INIT_FAILED;
1440 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1442 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1443 return ERROR_JTAG_INIT_FAILED;
1446 if (ftdi_usb_reset(&ftdic) < 0)
1448 ERROR("unable to reset ftdi device");
1449 return ERROR_JTAG_INIT_FAILED;
1452 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1454 ERROR("unable to set latency timer");
1455 return ERROR_JTAG_INIT_FAILED;
1458 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1460 ERROR("unable to get latency timer");
1461 return ERROR_JTAG_INIT_FAILED;
1465 DEBUG("current latency timer: %i", latency_timer);
1468 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1473 static int ft2232_purge_libftdi(void)
1475 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1477 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1478 return ERROR_JTAG_INIT_FAILED;
1483 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1485 int ft2232_init(void)
1490 ft2232_layout_t *cur_layout = ft2232_layouts;
1493 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1495 ft2232_layout = "usbjtag";
1496 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1499 while (cur_layout->name)
1501 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1503 layout = cur_layout;
1511 ERROR("No matching layout found for %s", ft2232_layout);
1512 return ERROR_JTAG_INIT_FAILED;
1515 for (i = 0; 1; i++) {
1517 * "more indicates that there are more IDs to try, so we should
1518 * not print an error for an ID mismatch (but for anything
1521 * try_more indicates that the error code returned indicates an
1522 * ID mismatch (and nothing else) and that we should proceeed
1523 * with the next ID pair.
1525 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1528 #if BUILD_FT2232_FTD2XX == 1
1529 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1531 #elif BUILD_FT2232_LIBFTDI == 1
1532 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1537 if (!more || !try_more)
1541 ft2232_buffer_size = 0;
1542 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1544 if (layout->init() != ERROR_OK)
1545 return ERROR_JTAG_INIT_FAILED;
1547 ft2232_speed(jtag_speed);
1549 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1550 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1552 ERROR("couldn't write to FT2232 to disable loopback");
1553 return ERROR_JTAG_INIT_FAILED;
1556 #if BUILD_FT2232_FTD2XX == 1
1557 return ft2232_purge_ftd2xx();
1558 #elif BUILD_FT2232_LIBFTDI == 1
1559 return ft2232_purge_libftdi();
1565 int usbjtag_init(void)
1571 low_direction = 0x0b;
1573 if (strcmp(ft2232_layout, "usbjtag") == 0)
1580 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1587 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1594 low_direction = 0x8b;
1598 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1599 return ERROR_JTAG_INIT_FAILED;
1602 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1604 low_direction &= ~nTRSTnOE; /* nTRST input */
1605 low_output &= ~nTRST; /* nTRST = 0 */
1609 low_direction |= nTRSTnOE; /* nTRST output */
1610 low_output |= nTRST; /* nTRST = 1 */
1613 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1615 low_direction |= nSRSTnOE; /* nSRST output */
1616 low_output |= nSRST; /* nSRST = 1 */
1620 low_direction &= ~nSRSTnOE; /* nSRST input */
1621 low_output &= ~nSRST; /* nSRST = 0 */
1624 /* initialize low byte for jtag */
1625 buf[0] = 0x80; /* command "set data bits low byte" */
1626 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1627 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1628 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1630 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1632 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1633 return ERROR_JTAG_INIT_FAILED;
1639 int jtagkey_init(void)
1645 low_direction = 0x1b;
1647 /* initialize low byte for jtag */
1648 buf[0] = 0x80; /* command "set data bits low byte" */
1649 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1650 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1651 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1653 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1655 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1656 return ERROR_JTAG_INIT_FAILED;
1659 if (strcmp(layout->name, "jtagkey") == 0)
1666 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1667 (strcmp(layout->name, "oocdlink") == 0))
1676 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1681 high_direction = 0x0f;
1683 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1685 high_output |= nTRSTnOE;
1686 high_output &= ~nTRST;
1690 high_output &= ~nTRSTnOE;
1691 high_output |= nTRST;
1694 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1696 high_output &= ~nSRSTnOE;
1697 high_output |= nSRST;
1701 high_output |= nSRSTnOE;
1702 high_output &= ~nSRST;
1705 /* initialize high port */
1706 buf[0] = 0x82; /* command "set data bits high byte" */
1707 buf[1] = high_output; /* value */
1708 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1709 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1711 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1713 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1714 return ERROR_JTAG_INIT_FAILED;
1720 int olimex_jtag_init(void)
1726 low_direction = 0x1b;
1728 /* initialize low byte for jtag */
1729 buf[0] = 0x80; /* command "set data bits low byte" */
1730 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1731 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1732 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1734 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1736 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1737 return ERROR_JTAG_INIT_FAILED;
1743 nSRSTnOE = 0x00; /* no output enable for nSRST */
1746 high_direction = 0x0f;
1748 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1750 high_output |= nTRSTnOE;
1751 high_output &= ~nTRST;
1755 high_output &= ~nTRSTnOE;
1756 high_output |= nTRST;
1759 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1761 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1765 high_output &= ~nSRST;
1768 /* turn red LED on */
1769 high_output |= 0x08;
1771 /* initialize high port */
1772 buf[0] = 0x82; /* command "set data bits high byte" */
1773 buf[1] = high_output; /* value */
1774 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1775 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1777 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1779 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1780 return ERROR_JTAG_INIT_FAILED;
1786 int flyswatter_init(void)
1792 low_direction = 0xfb;
1794 /* initialize low byte for jtag */
1795 buf[0] = 0x80; /* command "set data bits low byte" */
1796 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1797 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1798 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1800 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1802 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1803 return ERROR_JTAG_INIT_FAILED;
1807 nTRSTnOE = 0x0; /* not output enable for nTRST */
1809 nSRSTnOE = 0x00; /* no output enable for nSRST */
1812 high_direction = 0x0c;
1814 /* turn red LED1 on, LED2 off */
1815 high_output |= 0x08;
1817 /* initialize high port */
1818 buf[0] = 0x82; /* command "set data bits high byte" */
1819 buf[1] = high_output; /* value */
1820 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1821 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1823 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1825 ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1826 return ERROR_JTAG_INIT_FAILED;
1832 int turtle_init(void)
1838 low_direction = 0x5b;
1840 /* initialize low byte for jtag */
1841 buf[0] = 0x80; /* command "set data bits low byte" */
1842 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1843 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1844 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1846 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1848 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1849 return ERROR_JTAG_INIT_FAILED;
1855 high_direction = 0x0C;
1857 /* initialize high port */
1858 buf[0] = 0x82; /* command "set data bits high byte" */
1859 buf[1] = high_output;
1860 buf[2] = high_direction;
1861 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1863 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1865 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1866 return ERROR_JTAG_INIT_FAILED;
1872 int comstick_init(void)
1878 low_direction = 0x0b;
1880 /* initialize low byte for jtag */
1881 buf[0] = 0x80; /* command "set data bits low byte" */
1882 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1883 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1884 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1886 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1888 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1889 return ERROR_JTAG_INIT_FAILED;
1893 nTRSTnOE = 0x00; /* no output enable for nTRST */
1895 nSRSTnOE = 0x00; /* no output enable for nSRST */
1898 high_direction = 0x03;
1900 /* initialize high port */
1901 buf[0] = 0x82; /* command "set data bits high byte" */
1902 buf[1] = high_output;
1903 buf[2] = high_direction;
1904 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1906 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1908 ERROR("couldn't initialize FT2232 with 'comstick' layout");
1909 return ERROR_JTAG_INIT_FAILED;
1915 void olimex_jtag_blink(void)
1917 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1918 * ACBUS3 is bit 3 of the GPIOH port
1920 if (high_output & 0x08)
1922 /* set port pin high */
1923 high_output &= 0x07;
1927 /* set port pin low */
1928 high_output |= 0x08;
1932 BUFFER_ADD = high_output;
1933 BUFFER_ADD = high_direction;
1936 void turtle_jtag_blink(void)
1939 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
1941 if (high_output & 0x08)
1951 BUFFER_ADD = high_output;
1952 BUFFER_ADD = high_direction;
1956 int ft2232_quit(void)
1958 #if BUILD_FT2232_FTD2XX == 1
1961 status = FT_Close(ftdih);
1962 #elif BUILD_FT2232_LIBFTDI == 1
1963 ftdi_disable_bitbang(&ftdic);
1965 ftdi_usb_close(&ftdic);
1967 ftdi_deinit(&ftdic);
1970 free(ft2232_buffer);
1975 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1979 ft2232_device_desc = strdup(args[0]);
1983 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1989 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1993 ft2232_serial = strdup(args[0]);
1997 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2003 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2008 ft2232_layout = malloc(strlen(args[0]) + 1);
2009 strcpy(ft2232_layout, args[0]);
2014 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2018 if (argc > MAX_USB_IDS*2) {
2019 WARNING("ignoring extra IDs in ft2232_vid_pid "
2020 "(maximum is %d pairs)", MAX_USB_IDS);
2021 argc = MAX_USB_IDS*2;
2023 if (argc < 2 || (argc & 1))
2025 WARNING("incomplete ft2232_vid_pid configuration directive");
2030 for (i = 0; i+1 < argc; i += 2) {
2031 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2032 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2035 * Explicitly terminate, in case there are multiples instances of
2038 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;