1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
28 #include "replacements.h"
30 /* project specific includes */
34 #include "configuration.h"
35 #include "time_support.h"
42 /* FT2232 access library includes */
43 #if BUILD_FT2232_FTD2XX == 1
45 #elif BUILD_FT2232_LIBFTDI == 1
49 /* enable this to debug io latency
52 #define _DEBUG_USB_IO_
55 /* enable this to debug communication
58 #define _DEBUG_USB_COMMS_
61 int ft2232_execute_queue(void);
63 int ft2232_speed(int speed);
64 int ft2232_register_commands(struct command_context_s *cmd_ctx);
65 int ft2232_init(void);
66 int ft2232_quit(void);
68 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 char *ft2232_device_desc = NULL;
75 char *ft2232_serial = NULL;
76 char *ft2232_layout = NULL;
77 unsigned char ft2232_latency = 2;
80 /* vid = pid = 0 marks the end of the list */
81 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
82 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
84 typedef struct ft2232_layout_s
88 void(*reset)(int trst, int srst);
92 /* init procedures for supported layouts */
93 int usbjtag_init(void);
94 int jtagkey_init(void);
95 int olimex_jtag_init(void);
96 int flyswatter_init(void);
97 int turtle_init(void);
98 int comstick_init(void);
99 int stm32stick_init(void);
101 /* reset procedures for supported layouts */
102 void usbjtag_reset(int trst, int srst);
103 void jtagkey_reset(int trst, int srst);
104 void olimex_jtag_reset(int trst, int srst);
105 void flyswatter_reset(int trst, int srst);
106 void turtle_reset(int trst, int srst);
107 void comstick_reset(int trst, int srst);
108 void stm32stick_reset(int trst, int srst);
110 /* blink procedures for layouts that support a blinking led */
111 void olimex_jtag_blink(void);
112 void turtle_jtag_blink(void);
114 ft2232_layout_t ft2232_layouts[] =
116 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
117 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
118 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
119 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
120 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
121 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
122 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
123 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
124 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
125 {"comstick", comstick_init, comstick_reset, NULL},
126 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
130 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
132 static ft2232_layout_t *layout;
133 static u8 low_output = 0x0;
134 static u8 low_direction = 0x0;
135 static u8 high_output = 0x0;
136 static u8 high_direction = 0x0;
138 #if BUILD_FT2232_FTD2XX == 1
139 static FT_HANDLE ftdih = NULL;
140 #elif BUILD_FT2232_LIBFTDI == 1
141 static struct ftdi_context ftdic;
144 static u8 *ft2232_buffer = NULL;
145 static int ft2232_buffer_size = 0;
146 static int ft2232_read_pointer = 0;
147 static int ft2232_expect_read = 0;
148 #define FT2232_BUFFER_SIZE 131072
149 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
150 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
152 jtag_interface_t ft2232_interface =
157 .execute_queue = ft2232_execute_queue,
159 .speed = ft2232_speed,
160 .register_commands = ft2232_register_commands,
165 int ft2232_write(u8 *buf, int size, u32* bytes_written)
167 #if BUILD_FT2232_FTD2XX == 1
169 DWORD dw_bytes_written;
170 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
172 *bytes_written = dw_bytes_written;
173 LOG_ERROR("FT_Write returned: %lu", status);
174 return ERROR_JTAG_DEVICE_ERROR;
178 *bytes_written = dw_bytes_written;
181 #elif BUILD_FT2232_LIBFTDI == 1
183 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
186 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
187 return ERROR_JTAG_DEVICE_ERROR;
191 *bytes_written = retval;
197 int ft2232_read(u8* buf, int size, u32* bytes_read)
199 #if BUILD_FT2232_FTD2XX == 1
205 while ((*bytes_read < size) && timeout--)
207 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
208 *bytes_read, &dw_bytes_read)) != FT_OK)
211 LOG_ERROR("FT_Read returned: %lu", status);
212 return ERROR_JTAG_DEVICE_ERROR;
214 *bytes_read += dw_bytes_read;
216 #elif BUILD_FT2232_LIBFTDI == 1
221 while ((*bytes_read < size) && timeout--)
223 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
226 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
227 return ERROR_JTAG_DEVICE_ERROR;
229 *bytes_read += retval;
233 if (*bytes_read < size)
235 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
236 return ERROR_JTAG_DEVICE_ERROR;
242 int ft2232_speed(int speed)
248 buf[0] = 0x86; /* command "set divisor" */
249 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
250 buf[2] = (speed >> 8) & 0xff; /* valueH */
252 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
253 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
255 LOG_ERROR("couldn't set FT2232 TCK speed");
262 int ft2232_register_commands(struct command_context_s *cmd_ctx)
264 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
265 COMMAND_CONFIG, NULL);
266 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
267 COMMAND_CONFIG, NULL);
268 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
269 COMMAND_CONFIG, NULL);
270 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
271 COMMAND_CONFIG, NULL);
272 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
273 COMMAND_CONFIG, NULL);
277 void ft2232_end_state(enum tap_state state)
279 if (tap_move_map[state] != -1)
283 LOG_ERROR("BUG: %i is not a valid end state", state);
288 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
290 int num_bytes = ((scan_size + 7) / 8);
291 int bits_left = scan_size;
294 while(num_bytes-- > 1)
296 buffer[cur_byte] = BUFFER_READ;
301 buffer[cur_byte] = 0x0;
305 buffer[cur_byte] = BUFFER_READ >> 1;
308 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
312 void ft2232_debug_dump_buffer(void)
318 for (i = 0; i < ft2232_buffer_size; i++)
320 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
323 LOG_DEBUG("%s", line);
329 LOG_DEBUG("%s", line);
332 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
342 #ifdef _DEBUG_USB_IO_
343 struct timeval start, inter, inter2, end;
344 struct timeval d_inter, d_inter2, d_end;
347 #ifdef _DEBUG_USB_COMMS_
348 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
349 ft2232_debug_dump_buffer();
352 #ifdef _DEBUG_USB_IO_
353 gettimeofday(&start, NULL);
356 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
358 LOG_ERROR("couldn't write MPSSE commands to FT2232");
362 #ifdef _DEBUG_USB_IO_
363 gettimeofday(&inter, NULL);
366 if (ft2232_expect_read)
369 ft2232_buffer_size = 0;
371 #ifdef _DEBUG_USB_IO_
372 gettimeofday(&inter2, NULL);
375 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
377 LOG_ERROR("couldn't read from FT2232");
381 #ifdef _DEBUG_USB_IO_
382 gettimeofday(&end, NULL);
384 timeval_subtract(&d_inter, &inter, &start);
385 timeval_subtract(&d_inter2, &inter2, &start);
386 timeval_subtract(&d_end, &end, &start);
388 LOG_INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
392 ft2232_buffer_size = bytes_read;
394 if (ft2232_expect_read != ft2232_buffer_size)
396 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
397 ft2232_debug_dump_buffer();
402 #ifdef _DEBUG_USB_COMMS_
403 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
404 ft2232_debug_dump_buffer();
408 ft2232_expect_read = 0;
409 ft2232_read_pointer = 0;
411 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
412 * that wasn't handled by a caller-provided error handler
422 type = jtag_scan_type(cmd->cmd.scan);
423 if (type != SCAN_OUT)
425 scan_size = jtag_scan_size(cmd->cmd.scan);
426 buffer = calloc(CEIL(scan_size, 8), 1);
427 ft2232_read_scan(type, buffer, scan_size);
428 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
429 retval = ERROR_JTAG_QUEUE_FAILED;
439 ft2232_buffer_size = 0;
444 void ft2232_add_pathmove(pathmove_command_t *cmd)
446 int num_states = cmd->num_states;
455 int num_states_batch = num_states > 7 ? 7 : num_states;
458 /* command "Clock Data to TMS/CS Pin (no Read)" */
460 /* number of states remaining */
461 BUFFER_ADD = num_states_batch - 1;
463 while (num_states_batch--)
465 if (tap_transitions[cur_state].low == cmd->path[state_count])
466 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
467 else if (tap_transitions[cur_state].high == cmd->path[state_count])
468 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
471 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
475 cur_state = cmd->path[state_count];
480 BUFFER_ADD = tms_byte;
483 end_state = cur_state;
486 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
488 int num_bytes = (scan_size + 7) / 8;
489 int bits_left = scan_size;
493 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
495 /* command "Clock Data to TMS/CS Pin (no Read)" */
502 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
507 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
510 /* LOG_DEBUG("added TMS scan (no read)"); */
513 /* add command for complete bytes */
514 while (num_bytes > 1)
519 /* Clock Data Bytes In and Out LSB First */
521 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
523 else if (type == SCAN_OUT)
525 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
527 /* LOG_DEBUG("added TDI bytes (o)"); */
529 else if (type == SCAN_IN)
531 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
533 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
535 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
536 num_bytes -= thisrun_bytes;
537 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
538 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
541 /* add complete bytes */
542 while(thisrun_bytes-- > 0)
544 BUFFER_ADD = buffer[cur_byte];
549 else /* (type == SCAN_IN) */
551 bits_left -= 8 * (thisrun_bytes);
555 /* the most signifcant bit is scanned during TAP movement */
557 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
561 /* process remaining bits but the last one */
566 /* Clock Data Bits In and Out LSB First */
568 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
570 else if (type == SCAN_OUT)
572 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
574 /* LOG_DEBUG("added TDI bits (o)"); */
576 else if (type == SCAN_IN)
578 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
580 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
582 BUFFER_ADD = bits_left - 2;
584 BUFFER_ADD = buffer[cur_byte];
587 if ((ir_scan && (end_state == TAP_SI)) ||
588 (!ir_scan && (end_state == TAP_SD)))
592 /* Clock Data Bits In and Out LSB First */
594 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
596 else if (type == SCAN_OUT)
598 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
600 /* LOG_DEBUG("added TDI bits (o)"); */
602 else if (type == SCAN_IN)
604 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
606 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
609 BUFFER_ADD = last_bit;
613 /* move from Shift-IR/DR to end state */
614 if (type != SCAN_OUT)
616 /* Clock Data to TMS/CS Pin with Read */
618 /* LOG_DEBUG("added TMS scan (read)"); */
622 /* Clock Data to TMS/CS Pin (no Read) */
624 /* LOG_DEBUG("added TMS scan (no read)"); */
627 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
628 cur_state = end_state;
632 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
634 int num_bytes = (scan_size + 7) / 8;
635 int bits_left = scan_size;
638 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
639 u8 *receive_pointer = receive_buffer;
643 int thisrun_read = 0;
647 LOG_ERROR("BUG: large IR scans are not supported");
651 if (cur_state != TAP_SD)
653 /* command "Clock Data to TMS/CS Pin (no Read)" */
658 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
662 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
664 LOG_ERROR("couldn't write MPSSE commands to FT2232");
667 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
668 ft2232_buffer_size = 0;
670 /* add command for complete bytes */
671 while (num_bytes > 1)
677 /* Clock Data Bytes In and Out LSB First */
679 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
681 else if (type == SCAN_OUT)
683 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
685 /* LOG_DEBUG("added TDI bytes (o)"); */
687 else if (type == SCAN_IN)
689 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
691 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
693 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
694 thisrun_read = thisrun_bytes;
695 num_bytes -= thisrun_bytes;
696 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
697 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
700 /* add complete bytes */
701 while(thisrun_bytes-- > 0)
703 BUFFER_ADD = buffer[cur_byte];
708 else /* (type == SCAN_IN) */
710 bits_left -= 8 * (thisrun_bytes);
713 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
715 LOG_ERROR("couldn't write MPSSE commands to FT2232");
718 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
719 ft2232_buffer_size = 0;
721 if (type != SCAN_OUT)
723 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
725 LOG_ERROR("couldn't read from FT2232");
728 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
729 receive_pointer += bytes_read;
735 /* the most signifcant bit is scanned during TAP movement */
737 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
741 /* process remaining bits but the last one */
746 /* Clock Data Bits In and Out LSB First */
748 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
750 else if (type == SCAN_OUT)
752 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
754 /* LOG_DEBUG("added TDI bits (o)"); */
756 else if (type == SCAN_IN)
758 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
760 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
762 BUFFER_ADD = bits_left - 2;
764 BUFFER_ADD = buffer[cur_byte];
766 if (type != SCAN_OUT)
770 if (end_state == TAP_SD)
774 /* Clock Data Bits In and Out LSB First */
776 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
778 else if (type == SCAN_OUT)
780 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
782 /* LOG_DEBUG("added TDI bits (o)"); */
784 else if (type == SCAN_IN)
786 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
788 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
791 BUFFER_ADD = last_bit;
795 /* move from Shift-IR/DR to end state */
796 if (type != SCAN_OUT)
798 /* Clock Data to TMS/CS Pin with Read */
800 /* LOG_DEBUG("added TMS scan (read)"); */
804 /* Clock Data to TMS/CS Pin (no Read) */
806 /* LOG_DEBUG("added TMS scan (no read)"); */
809 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
810 cur_state = end_state;
813 if (type != SCAN_OUT)
816 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
818 LOG_ERROR("couldn't write MPSSE commands to FT2232");
821 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
822 ft2232_buffer_size = 0;
824 if (type != SCAN_OUT)
826 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
828 LOG_ERROR("couldn't read from FT2232");
831 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
832 receive_pointer += bytes_read;
838 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
840 int predicted_size = 3;
841 int num_bytes = (scan_size - 1) / 8;
843 if (cur_state != TAP_SD)
846 if (type == SCAN_IN) /* only from device to host */
849 predicted_size += (CEIL(num_bytes, 65536)) * 3;
850 /* remaining bits - 1 (up to 7) */
851 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
853 else /* host to device, or bidirectional */
856 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
857 /* remaining bits -1 (up to 7) */
858 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
861 return predicted_size;
864 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
866 int predicted_size = 0;
868 if (type != SCAN_OUT)
871 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
872 /* remaining bits - 1 */
873 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
874 /* last bit (from TMS scan) */
878 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
880 return predicted_size;
883 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)
926 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
927 high_output &= ~nTRSTnOE;
929 high_output &= ~nTRST;
933 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
934 high_output |= nTRSTnOE;
936 high_output |= nTRST;
941 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
942 high_output &= ~nSRST;
944 high_output &= ~nSRSTnOE;
948 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
949 high_output |= nSRST;
951 high_output |= nSRSTnOE;
954 /* command "set data bits high byte" */
956 BUFFER_ADD = high_output;
957 BUFFER_ADD = high_direction;
958 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
961 void olimex_jtag_reset(int trst, int srst)
965 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
966 high_output &= ~nTRSTnOE;
968 high_output &= ~nTRST;
972 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
973 high_output |= nTRSTnOE;
975 high_output |= nTRST;
980 high_output |= nSRST;
984 high_output &= ~nSRST;
987 /* command "set data bits high byte" */
989 BUFFER_ADD = high_output;
990 BUFFER_ADD = high_direction;
991 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
994 void flyswatter_reset(int trst, int srst)
998 low_output &= ~nTRST;
1002 low_output |= nTRST;
1007 low_output |= nSRST;
1011 low_output &= ~nSRST;
1014 /* command "set data bits low byte" */
1016 BUFFER_ADD = low_output;
1017 BUFFER_ADD = low_direction;
1018 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1021 void turtle_reset(int trst, int srst)
1027 low_output |= nSRST;
1031 low_output &= ~nSRST;
1034 /* command "set data bits low byte" */
1036 BUFFER_ADD = low_output;
1037 BUFFER_ADD = low_direction;
1038 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1041 void comstick_reset(int trst, int srst)
1045 high_output &= ~nTRST;
1049 high_output |= nTRST;
1054 high_output &= ~nSRST;
1058 high_output |= nSRST;
1061 /* command "set data bits high byte" */
1063 BUFFER_ADD = high_output;
1064 BUFFER_ADD = high_direction;
1065 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1068 void stm32stick_reset(int trst, int srst)
1072 high_output &= ~nTRST;
1076 high_output |= nTRST;
1081 low_output &= ~nSRST;
1085 low_output |= nSRST;
1088 /* command "set data bits low byte" */
1090 BUFFER_ADD = low_output;
1091 BUFFER_ADD = low_direction;
1093 /* command "set data bits high byte" */
1095 BUFFER_ADD = high_output;
1096 BUFFER_ADD = high_direction;
1097 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1100 int ft2232_execute_queue()
1102 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1103 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1105 int scan_size; /* size of IR or DR scan */
1106 enum scan_type type;
1108 int predicted_size = 0;
1109 int require_send = 0;
1112 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1113 * that wasn't handled by a caller-provided error handler
1117 ft2232_buffer_size = 0;
1118 ft2232_expect_read = 0;
1120 /* blink, if the current layout has that feature */
1128 case JTAG_END_STATE:
1129 if (cmd->cmd.end_state->end_state != -1)
1130 ft2232_end_state(cmd->cmd.end_state->end_state);
1133 /* only send the maximum buffer size that FT2232C can handle */
1135 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1137 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1138 retval = ERROR_JTAG_QUEUE_FAILED;
1143 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1145 cur_state = TAP_TLR;
1147 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1150 #ifdef _DEBUG_JTAG_IO_
1151 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1155 /* only send the maximum buffer size that FT2232C can handle */
1157 if (cur_state != TAP_RTI)
1158 predicted_size += 3;
1159 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1160 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1161 predicted_size += 3;
1162 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1163 predicted_size += 3;
1164 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1166 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1167 retval = ERROR_JTAG_QUEUE_FAILED;
1171 if (cur_state != TAP_RTI)
1173 /* command "Clock Data to TMS/CS Pin (no Read)" */
1178 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1179 cur_state = TAP_RTI;
1182 i = cmd->cmd.runtest->num_cycles;
1185 /* command "Clock Data to TMS/CS Pin (no Read)" */
1188 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1191 cur_state = TAP_RTI;
1192 i -= (i > 7) ? 7 : i;
1193 /* LOG_DEBUG("added TMS scan (no read)"); */
1195 if (cmd->cmd.runtest->end_state != -1)
1196 ft2232_end_state(cmd->cmd.runtest->end_state);
1197 if (cur_state != end_state)
1199 /* command "Clock Data to TMS/CS Pin (no Read)" */
1204 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1205 cur_state = end_state;
1206 /* LOG_DEBUG("added TMS scan (no read)"); */
1209 #ifdef _DEBUG_JTAG_IO_
1210 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1213 case JTAG_STATEMOVE:
1214 /* only send the maximum buffer size that FT2232C can handle */
1216 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1218 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1219 retval = ERROR_JTAG_QUEUE_FAILED;
1223 if (cmd->cmd.statemove->end_state != -1)
1224 ft2232_end_state(cmd->cmd.statemove->end_state);
1225 /* command "Clock Data to TMS/CS Pin (no Read)" */
1230 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1231 /* LOG_DEBUG("added TMS scan (no read)"); */
1232 cur_state = end_state;
1234 #ifdef _DEBUG_JTAG_IO_
1235 LOG_DEBUG("statemove: %i", end_state);
1239 /* only send the maximum buffer size that FT2232C can handle */
1240 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1241 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1243 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1244 retval = ERROR_JTAG_QUEUE_FAILED;
1248 ft2232_add_pathmove(cmd->cmd.pathmove);
1250 #ifdef _DEBUG_JTAG_IO_
1251 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1255 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1256 type = jtag_scan_type(cmd->cmd.scan);
1257 predicted_size = ft2232_predict_scan_out(scan_size, type);
1258 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1260 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1261 /* unsent commands before this */
1262 if (first_unsent != cmd)
1263 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1264 retval = ERROR_JTAG_QUEUE_FAILED;
1266 /* current command */
1267 if (cmd->cmd.scan->end_state != -1)
1268 ft2232_end_state(cmd->cmd.scan->end_state);
1269 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1271 first_unsent = cmd->next;
1276 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1278 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1279 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1280 retval = ERROR_JTAG_QUEUE_FAILED;
1284 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1285 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1286 if (cmd->cmd.scan->end_state != -1)
1287 ft2232_end_state(cmd->cmd.scan->end_state);
1288 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1292 #ifdef _DEBUG_JTAG_IO_
1293 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1297 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1298 retval = ERROR_JTAG_QUEUE_FAILED;
1299 first_unsent = cmd->next;
1300 jtag_sleep(cmd->cmd.sleep->us);
1301 #ifdef _DEBUG_JTAG_IO_
1302 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1306 LOG_ERROR("BUG: unknown JTAG command type encountered");
1312 if (require_send > 0)
1313 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1314 retval = ERROR_JTAG_QUEUE_FAILED;
1319 #if BUILD_FT2232_FTD2XX == 1
1320 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1323 DWORD openex_flags = 0;
1324 char *openex_string = NULL;
1327 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1328 ft2232_layout, vid, pid);
1331 /* Add non-standard Vid/Pid to the linux driver */
1332 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1334 LOG_WARNING("couldn't add %4.4x:%4.4x",
1339 if (ft2232_device_desc && ft2232_serial)
1341 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1342 ft2232_device_desc = NULL;
1345 if (ft2232_device_desc)
1347 openex_string = ft2232_device_desc;
1348 openex_flags = FT_OPEN_BY_DESCRIPTION;
1350 else if (ft2232_serial)
1352 openex_string = ft2232_serial;
1353 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1357 LOG_ERROR("neither device description nor serial number specified");
1358 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1360 return ERROR_JTAG_INIT_FAILED;
1363 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1368 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1371 return ERROR_JTAG_INIT_FAILED;
1373 LOG_ERROR("unable to open ftdi device: %lu", status);
1374 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1375 if (status == FT_OK)
1377 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1380 for (i = 0; i < num_devices; i++)
1381 desc_array[i] = malloc(64);
1382 desc_array[num_devices] = NULL;
1384 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1386 if (status == FT_OK)
1388 LOG_ERROR("ListDevices: %lu\n", num_devices);
1389 for (i = 0; i < num_devices; i++)
1390 LOG_ERROR("%i: %s", i, desc_array[i]);
1393 for (i = 0; i < num_devices; i++)
1394 free(desc_array[i]);
1399 LOG_ERROR("ListDevices: NONE\n");
1401 return ERROR_JTAG_INIT_FAILED;
1404 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1406 LOG_ERROR("unable to set latency timer: %lu", status);
1407 return ERROR_JTAG_INIT_FAILED;
1410 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1412 LOG_ERROR("unable to get latency timer: %lu", status);
1413 return ERROR_JTAG_INIT_FAILED;
1417 LOG_DEBUG("current latency timer: %i", latency_timer);
1420 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1422 LOG_ERROR("unable to set timeouts: %lu", status);
1423 return ERROR_JTAG_INIT_FAILED;
1426 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1428 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1429 return ERROR_JTAG_INIT_FAILED;
1435 static int ft2232_purge_ftd2xx(void)
1439 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1441 LOG_ERROR("error purging ftd2xx device: %lu", status);
1442 return ERROR_JTAG_INIT_FAILED;
1447 #endif /* BUILD_FT2232_FTD2XX == 1 */
1449 #if BUILD_FT2232_LIBFTDI == 1
1450 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1454 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1455 ft2232_layout, vid, pid);
1457 if (ftdi_init(&ftdic) < 0)
1458 return ERROR_JTAG_INIT_FAILED;
1460 /* context, vendor id, product id */
1461 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1462 ft2232_serial) < 0) {
1464 LOG_WARNING("unable to open ftdi device (trying more): %s",
1467 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1469 return ERROR_JTAG_INIT_FAILED;
1472 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1474 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1475 return ERROR_JTAG_INIT_FAILED;
1478 if (ftdi_usb_reset(&ftdic) < 0)
1480 LOG_ERROR("unable to reset ftdi device");
1481 return ERROR_JTAG_INIT_FAILED;
1484 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1486 LOG_ERROR("unable to set latency timer");
1487 return ERROR_JTAG_INIT_FAILED;
1490 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1492 LOG_ERROR("unable to get latency timer");
1493 return ERROR_JTAG_INIT_FAILED;
1497 LOG_DEBUG("current latency timer: %i", latency_timer);
1500 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1505 static int ft2232_purge_libftdi(void)
1507 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1509 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1510 return ERROR_JTAG_INIT_FAILED;
1515 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1517 int ft2232_init(void)
1522 ft2232_layout_t *cur_layout = ft2232_layouts;
1525 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1527 ft2232_layout = "usbjtag";
1528 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1531 while (cur_layout->name)
1533 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1535 layout = cur_layout;
1543 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1544 return ERROR_JTAG_INIT_FAILED;
1547 for (i = 0; 1; i++) {
1549 * "more indicates that there are more IDs to try, so we should
1550 * not print an error for an ID mismatch (but for anything
1553 * try_more indicates that the error code returned indicates an
1554 * ID mismatch (and nothing else) and that we should proceeed
1555 * with the next ID pair.
1557 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1560 #if BUILD_FT2232_FTD2XX == 1
1561 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1563 #elif BUILD_FT2232_LIBFTDI == 1
1564 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1569 if (!more || !try_more)
1573 ft2232_buffer_size = 0;
1574 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1576 if (layout->init() != ERROR_OK)
1577 return ERROR_JTAG_INIT_FAILED;
1579 ft2232_speed(jtag_speed);
1581 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1582 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1584 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1585 return ERROR_JTAG_INIT_FAILED;
1588 #if BUILD_FT2232_FTD2XX == 1
1589 return ft2232_purge_ftd2xx();
1590 #elif BUILD_FT2232_LIBFTDI == 1
1591 return ft2232_purge_libftdi();
1597 int usbjtag_init(void)
1603 low_direction = 0x0b;
1605 if (strcmp(ft2232_layout, "usbjtag") == 0)
1612 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1619 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1626 low_direction = 0x8b;
1630 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1631 return ERROR_JTAG_INIT_FAILED;
1634 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1636 low_direction &= ~nTRSTnOE; /* nTRST input */
1637 low_output &= ~nTRST; /* nTRST = 0 */
1641 low_direction |= nTRSTnOE; /* nTRST output */
1642 low_output |= nTRST; /* nTRST = 1 */
1645 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1647 low_direction |= nSRSTnOE; /* nSRST output */
1648 low_output |= nSRST; /* nSRST = 1 */
1652 low_direction &= ~nSRSTnOE; /* nSRST input */
1653 low_output &= ~nSRST; /* nSRST = 0 */
1656 /* initialize low byte for jtag */
1657 buf[0] = 0x80; /* command "set data bits low byte" */
1658 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1659 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1660 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1662 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1664 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1665 return ERROR_JTAG_INIT_FAILED;
1671 int jtagkey_init(void)
1677 low_direction = 0x1b;
1679 /* initialize low byte for jtag */
1680 buf[0] = 0x80; /* command "set data bits low byte" */
1681 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1682 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1683 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1685 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1687 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1688 return ERROR_JTAG_INIT_FAILED;
1691 if (strcmp(layout->name, "jtagkey") == 0)
1698 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1699 (strcmp(layout->name, "oocdlink") == 0))
1708 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1713 high_direction = 0x0f;
1715 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1717 high_output |= nTRSTnOE;
1718 high_output &= ~nTRST;
1722 high_output &= ~nTRSTnOE;
1723 high_output |= nTRST;
1726 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1728 high_output &= ~nSRSTnOE;
1729 high_output |= nSRST;
1733 high_output |= nSRSTnOE;
1734 high_output &= ~nSRST;
1737 /* initialize high port */
1738 buf[0] = 0x82; /* command "set data bits high byte" */
1739 buf[1] = high_output; /* value */
1740 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1741 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1743 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1745 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1746 return ERROR_JTAG_INIT_FAILED;
1752 int olimex_jtag_init(void)
1758 low_direction = 0x1b;
1760 /* initialize low byte for jtag */
1761 buf[0] = 0x80; /* command "set data bits low byte" */
1762 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1763 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1764 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1766 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1768 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1769 return ERROR_JTAG_INIT_FAILED;
1775 nSRSTnOE = 0x00; /* no output enable for nSRST */
1778 high_direction = 0x0f;
1780 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1782 high_output |= nTRSTnOE;
1783 high_output &= ~nTRST;
1787 high_output &= ~nTRSTnOE;
1788 high_output |= nTRST;
1791 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1793 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1797 high_output &= ~nSRST;
1800 /* turn red LED on */
1801 high_output |= 0x08;
1803 /* initialize high port */
1804 buf[0] = 0x82; /* command "set data bits high byte" */
1805 buf[1] = high_output; /* value */
1806 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1807 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1809 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1811 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1812 return ERROR_JTAG_INIT_FAILED;
1818 int flyswatter_init(void)
1824 low_direction = 0xfb;
1826 /* initialize low byte for jtag */
1827 buf[0] = 0x80; /* command "set data bits low byte" */
1828 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1829 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1830 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1832 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1834 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1835 return ERROR_JTAG_INIT_FAILED;
1839 nTRSTnOE = 0x0; /* not output enable for nTRST */
1841 nSRSTnOE = 0x00; /* no output enable for nSRST */
1844 high_direction = 0x0c;
1846 /* turn red LED1 on, LED2 off */
1847 high_output |= 0x08;
1849 /* initialize high port */
1850 buf[0] = 0x82; /* command "set data bits high byte" */
1851 buf[1] = high_output; /* value */
1852 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1853 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1855 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1857 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1858 return ERROR_JTAG_INIT_FAILED;
1864 int turtle_init(void)
1870 low_direction = 0x5b;
1872 /* initialize low byte for jtag */
1873 buf[0] = 0x80; /* command "set data bits low byte" */
1874 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1875 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1876 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1878 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1880 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1881 return ERROR_JTAG_INIT_FAILED;
1887 high_direction = 0x0C;
1889 /* initialize high port */
1890 buf[0] = 0x82; /* command "set data bits high byte" */
1891 buf[1] = high_output;
1892 buf[2] = high_direction;
1893 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1895 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1897 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1898 return ERROR_JTAG_INIT_FAILED;
1904 int comstick_init(void)
1910 low_direction = 0x0b;
1912 /* initialize low byte for jtag */
1913 buf[0] = 0x80; /* command "set data bits low byte" */
1914 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1915 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1916 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1918 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1920 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1921 return ERROR_JTAG_INIT_FAILED;
1925 nTRSTnOE = 0x00; /* no output enable for nTRST */
1927 nSRSTnOE = 0x00; /* no output enable for nSRST */
1930 high_direction = 0x03;
1932 /* initialize high port */
1933 buf[0] = 0x82; /* command "set data bits high byte" */
1934 buf[1] = high_output;
1935 buf[2] = high_direction;
1936 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1938 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1940 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1941 return ERROR_JTAG_INIT_FAILED;
1947 int stm32stick_init(void)
1953 low_direction = 0x8b;
1955 /* initialize low byte for jtag */
1956 buf[0] = 0x80; /* command "set data bits low byte" */
1957 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1958 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1959 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1961 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1963 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1964 return ERROR_JTAG_INIT_FAILED;
1968 nTRSTnOE = 0x00; /* no output enable for nTRST */
1970 nSRSTnOE = 0x00; /* no output enable for nSRST */
1973 high_direction = 0x03;
1975 /* initialize high port */
1976 buf[0] = 0x82; /* command "set data bits high byte" */
1977 buf[1] = high_output;
1978 buf[2] = high_direction;
1979 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1981 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1983 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
1984 return ERROR_JTAG_INIT_FAILED;
1990 void olimex_jtag_blink(void)
1992 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1993 * ACBUS3 is bit 3 of the GPIOH port
1995 if (high_output & 0x08)
1997 /* set port pin high */
1998 high_output &= 0x07;
2002 /* set port pin low */
2003 high_output |= 0x08;
2007 BUFFER_ADD = high_output;
2008 BUFFER_ADD = high_direction;
2011 void turtle_jtag_blink(void)
2014 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2016 if (high_output & 0x08)
2026 BUFFER_ADD = high_output;
2027 BUFFER_ADD = high_direction;
2031 int ft2232_quit(void)
2033 #if BUILD_FT2232_FTD2XX == 1
2036 status = FT_Close(ftdih);
2037 #elif BUILD_FT2232_LIBFTDI == 1
2038 ftdi_disable_bitbang(&ftdic);
2040 ftdi_usb_close(&ftdic);
2042 ftdi_deinit(&ftdic);
2045 free(ft2232_buffer);
2046 ft2232_buffer = NULL;
2051 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2055 ft2232_device_desc = strdup(args[0]);
2059 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2065 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2069 ft2232_serial = strdup(args[0]);
2073 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2079 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2084 ft2232_layout = malloc(strlen(args[0]) + 1);
2085 strcpy(ft2232_layout, args[0]);
2090 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2094 if (argc > MAX_USB_IDS*2) {
2095 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2096 "(maximum is %d pairs)", MAX_USB_IDS);
2097 argc = MAX_USB_IDS*2;
2099 if (argc < 2 || (argc & 1))
2101 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2106 for (i = 0; i+1 < argc; i += 2) {
2107 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2108 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2111 * Explicitly terminate, in case there are multiples instances of
2114 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2119 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2123 ft2232_latency = atoi(args[0]);
2127 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");