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_speed_div(int speed, int *khz);
65 int ft2232_khz(int khz, int *jtag_speed);
66 int ft2232_register_commands(struct command_context_s *cmd_ctx);
67 int ft2232_init(void);
68 int ft2232_quit(void);
70 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 char *ft2232_device_desc = NULL;
77 char *ft2232_serial = NULL;
78 char *ft2232_layout = NULL;
79 unsigned char ft2232_latency = 2;
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);
101 int stm32stick_init(void);
103 /* reset procedures for supported layouts */
104 void usbjtag_reset(int trst, int srst);
105 void jtagkey_reset(int trst, int srst);
106 void olimex_jtag_reset(int trst, int srst);
107 void flyswatter_reset(int trst, int srst);
108 void turtle_reset(int trst, int srst);
109 void comstick_reset(int trst, int srst);
110 void stm32stick_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},
128 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
132 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
134 static ft2232_layout_t *layout;
135 static u8 low_output = 0x0;
136 static u8 low_direction = 0x0;
137 static u8 high_output = 0x0;
138 static u8 high_direction = 0x0;
140 #if BUILD_FT2232_FTD2XX == 1
141 static FT_HANDLE ftdih = NULL;
142 #elif BUILD_FT2232_LIBFTDI == 1
143 static struct ftdi_context ftdic;
146 static u8 *ft2232_buffer = NULL;
147 static int ft2232_buffer_size = 0;
148 static int ft2232_read_pointer = 0;
149 static int ft2232_expect_read = 0;
150 #define FT2232_BUFFER_SIZE 131072
151 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
152 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
154 jtag_interface_t ft2232_interface =
157 .execute_queue = ft2232_execute_queue,
158 .speed = ft2232_speed,
159 .speed_div = ft2232_speed_div,
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 LOG_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 LOG_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 LOG_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 LOG_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 LOG_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=2.0MHz, ...*/
251 buf[2] = (speed >> 8) & 0xff; /* valueH */
253 LOG_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 LOG_ERROR("couldn't set FT2232 TCK speed");
263 int ft2232_speed_div(int speed, int *khz)
265 /* Take a look in the FT2232 manual,
266 * AN2232C-01 Command Processor for
267 * MPSSE and MCU Host Bus. Chapter 3.8 */
269 *khz = 6000 / (1+speed);
274 int ft2232_khz(int khz, int *jtag_speed)
276 /* Take a look in the FT2232 manual,
277 * AN2232C-01 Command Processor for
278 * MPSSE and MCU Host Bus. Chapter 3.8
280 * We will calc here with a multiplier
281 * of 10 for better rounding later. */
283 /* Calc speed, (6000 / khz) - 1 */
284 /* Use 65000 for better rounding */
285 *jtag_speed = (60000 / khz) - 10;
287 /* Add 0.9 for rounding */
290 /* Calc real speed */
291 *jtag_speed = *jtag_speed / 10;
293 /* Check if speed is greater than 0 */
299 /* Check max value */
300 if (*jtag_speed > 0xFFFF)
302 *jtag_speed = 0xFFFF;
308 int ft2232_register_commands(struct command_context_s *cmd_ctx)
310 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
311 COMMAND_CONFIG, NULL);
312 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
313 COMMAND_CONFIG, NULL);
314 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
315 COMMAND_CONFIG, NULL);
316 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
317 COMMAND_CONFIG, NULL);
318 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
319 COMMAND_CONFIG, NULL);
323 void ft2232_end_state(enum tap_state state)
325 if (tap_move_map[state] != -1)
329 LOG_ERROR("BUG: %i is not a valid end state", state);
334 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
336 int num_bytes = ((scan_size + 7) / 8);
337 int bits_left = scan_size;
340 while(num_bytes-- > 1)
342 buffer[cur_byte] = BUFFER_READ;
347 buffer[cur_byte] = 0x0;
351 buffer[cur_byte] = BUFFER_READ >> 1;
354 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
358 void ft2232_debug_dump_buffer(void)
364 for (i = 0; i < ft2232_buffer_size; i++)
366 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
369 LOG_DEBUG("%s", line);
375 LOG_DEBUG("%s", line);
378 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
388 #ifdef _DEBUG_USB_IO_
389 struct timeval start, inter, inter2, end;
390 struct timeval d_inter, d_inter2, d_end;
393 #ifdef _DEBUG_USB_COMMS_
394 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
395 ft2232_debug_dump_buffer();
398 #ifdef _DEBUG_USB_IO_
399 gettimeofday(&start, NULL);
402 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
404 LOG_ERROR("couldn't write MPSSE commands to FT2232");
408 #ifdef _DEBUG_USB_IO_
409 gettimeofday(&inter, NULL);
412 if (ft2232_expect_read)
415 ft2232_buffer_size = 0;
417 #ifdef _DEBUG_USB_IO_
418 gettimeofday(&inter2, NULL);
421 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
423 LOG_ERROR("couldn't read from FT2232");
427 #ifdef _DEBUG_USB_IO_
428 gettimeofday(&end, NULL);
430 timeval_subtract(&d_inter, &inter, &start);
431 timeval_subtract(&d_inter2, &inter2, &start);
432 timeval_subtract(&d_end, &end, &start);
434 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);
438 ft2232_buffer_size = bytes_read;
440 if (ft2232_expect_read != ft2232_buffer_size)
442 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
443 ft2232_debug_dump_buffer();
448 #ifdef _DEBUG_USB_COMMS_
449 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
450 ft2232_debug_dump_buffer();
454 ft2232_expect_read = 0;
455 ft2232_read_pointer = 0;
457 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
458 * that wasn't handled by a caller-provided error handler
468 type = jtag_scan_type(cmd->cmd.scan);
469 if (type != SCAN_OUT)
471 scan_size = jtag_scan_size(cmd->cmd.scan);
472 buffer = calloc(CEIL(scan_size, 8), 1);
473 ft2232_read_scan(type, buffer, scan_size);
474 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
475 retval = ERROR_JTAG_QUEUE_FAILED;
485 ft2232_buffer_size = 0;
490 void ft2232_add_pathmove(pathmove_command_t *cmd)
492 int num_states = cmd->num_states;
501 int num_states_batch = num_states > 7 ? 7 : num_states;
504 /* command "Clock Data to TMS/CS Pin (no Read)" */
506 /* number of states remaining */
507 BUFFER_ADD = num_states_batch - 1;
509 while (num_states_batch--)
511 if (tap_transitions[cur_state].low == cmd->path[state_count])
512 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
513 else if (tap_transitions[cur_state].high == cmd->path[state_count])
514 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
517 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
521 cur_state = cmd->path[state_count];
526 BUFFER_ADD = tms_byte;
529 end_state = cur_state;
532 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
534 int num_bytes = (scan_size + 7) / 8;
535 int bits_left = scan_size;
539 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
541 /* command "Clock Data to TMS/CS Pin (no Read)" */
548 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
553 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
556 /* LOG_DEBUG("added TMS scan (no read)"); */
559 /* add command for complete bytes */
560 while (num_bytes > 1)
565 /* Clock Data Bytes In and Out LSB First */
567 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
569 else if (type == SCAN_OUT)
571 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
573 /* LOG_DEBUG("added TDI bytes (o)"); */
575 else if (type == SCAN_IN)
577 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
579 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
581 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
582 num_bytes -= thisrun_bytes;
583 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
584 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
587 /* add complete bytes */
588 while(thisrun_bytes-- > 0)
590 BUFFER_ADD = buffer[cur_byte];
595 else /* (type == SCAN_IN) */
597 bits_left -= 8 * (thisrun_bytes);
601 /* the most signifcant bit is scanned during TAP movement */
603 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
607 /* process remaining bits but the last one */
612 /* Clock Data Bits In and Out LSB First */
614 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
616 else if (type == SCAN_OUT)
618 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
620 /* LOG_DEBUG("added TDI bits (o)"); */
622 else if (type == SCAN_IN)
624 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
626 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
628 BUFFER_ADD = bits_left - 2;
630 BUFFER_ADD = buffer[cur_byte];
633 if ((ir_scan && (end_state == TAP_SI)) ||
634 (!ir_scan && (end_state == TAP_SD)))
638 /* Clock Data Bits In and Out LSB First */
640 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
642 else if (type == SCAN_OUT)
644 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
646 /* LOG_DEBUG("added TDI bits (o)"); */
648 else if (type == SCAN_IN)
650 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
652 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
655 BUFFER_ADD = last_bit;
659 /* move from Shift-IR/DR to end state */
660 if (type != SCAN_OUT)
662 /* Clock Data to TMS/CS Pin with Read */
664 /* LOG_DEBUG("added TMS scan (read)"); */
668 /* Clock Data to TMS/CS Pin (no Read) */
670 /* LOG_DEBUG("added TMS scan (no read)"); */
673 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
674 cur_state = end_state;
678 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
680 int num_bytes = (scan_size + 7) / 8;
681 int bits_left = scan_size;
684 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
685 u8 *receive_pointer = receive_buffer;
689 int thisrun_read = 0;
693 LOG_ERROR("BUG: large IR scans are not supported");
697 if (cur_state != TAP_SD)
699 /* command "Clock Data to TMS/CS Pin (no Read)" */
704 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
708 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
710 LOG_ERROR("couldn't write MPSSE commands to FT2232");
713 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
714 ft2232_buffer_size = 0;
716 /* add command for complete bytes */
717 while (num_bytes > 1)
723 /* Clock Data Bytes In and Out LSB First */
725 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
727 else if (type == SCAN_OUT)
729 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
731 /* LOG_DEBUG("added TDI bytes (o)"); */
733 else if (type == SCAN_IN)
735 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
737 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
739 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
740 thisrun_read = thisrun_bytes;
741 num_bytes -= thisrun_bytes;
742 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
743 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
746 /* add complete bytes */
747 while(thisrun_bytes-- > 0)
749 BUFFER_ADD = buffer[cur_byte];
754 else /* (type == SCAN_IN) */
756 bits_left -= 8 * (thisrun_bytes);
759 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
761 LOG_ERROR("couldn't write MPSSE commands to FT2232");
764 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
765 ft2232_buffer_size = 0;
767 if (type != SCAN_OUT)
769 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
771 LOG_ERROR("couldn't read from FT2232");
774 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
775 receive_pointer += bytes_read;
781 /* the most signifcant bit is scanned during TAP movement */
783 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
787 /* process remaining bits but the last one */
792 /* Clock Data Bits In and Out LSB First */
794 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
796 else if (type == SCAN_OUT)
798 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
800 /* LOG_DEBUG("added TDI bits (o)"); */
802 else if (type == SCAN_IN)
804 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
806 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
808 BUFFER_ADD = bits_left - 2;
810 BUFFER_ADD = buffer[cur_byte];
812 if (type != SCAN_OUT)
816 if (end_state == TAP_SD)
820 /* Clock Data Bits In and Out LSB First */
822 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
824 else if (type == SCAN_OUT)
826 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
828 /* LOG_DEBUG("added TDI bits (o)"); */
830 else if (type == SCAN_IN)
832 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
834 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
837 BUFFER_ADD = last_bit;
841 /* move from Shift-IR/DR to end state */
842 if (type != SCAN_OUT)
844 /* Clock Data to TMS/CS Pin with Read */
846 /* LOG_DEBUG("added TMS scan (read)"); */
850 /* Clock Data to TMS/CS Pin (no Read) */
852 /* LOG_DEBUG("added TMS scan (no read)"); */
855 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
856 cur_state = end_state;
859 if (type != SCAN_OUT)
862 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
864 LOG_ERROR("couldn't write MPSSE commands to FT2232");
867 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
868 ft2232_buffer_size = 0;
870 if (type != SCAN_OUT)
872 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
874 LOG_ERROR("couldn't read from FT2232");
877 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
878 receive_pointer += bytes_read;
884 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
886 int predicted_size = 3;
887 int num_bytes = (scan_size - 1) / 8;
889 if (cur_state != TAP_SD)
892 if (type == SCAN_IN) /* only from device to host */
895 predicted_size += (CEIL(num_bytes, 65536)) * 3;
896 /* remaining bits - 1 (up to 7) */
897 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
899 else /* host to device, or bidirectional */
902 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
903 /* remaining bits -1 (up to 7) */
904 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
907 return predicted_size;
910 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
912 int predicted_size = 0;
914 if (type != SCAN_OUT)
917 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
918 /* remaining bits - 1 */
919 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
920 /* last bit (from TMS scan) */
924 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
926 return predicted_size;
929 void usbjtag_reset(int trst, int srst)
933 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
934 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
936 low_output &= ~nTRST; /* switch output low */
940 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
941 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
943 low_output |= nTRST; /* switch output high */
948 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
949 low_output &= ~nSRST; /* switch output low */
951 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
955 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
956 low_output |= nSRST; /* switch output high */
958 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
961 /* command "set data bits low byte" */
963 BUFFER_ADD = low_output;
964 BUFFER_ADD = low_direction;
968 void jtagkey_reset(int trst, int srst)
972 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
973 high_output &= ~nTRSTnOE;
975 high_output &= ~nTRST;
979 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
980 high_output |= nTRSTnOE;
982 high_output |= nTRST;
987 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
988 high_output &= ~nSRST;
990 high_output &= ~nSRSTnOE;
994 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
995 high_output |= nSRST;
997 high_output |= nSRSTnOE;
1000 /* command "set data bits high byte" */
1002 BUFFER_ADD = high_output;
1003 BUFFER_ADD = high_direction;
1004 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1007 void olimex_jtag_reset(int trst, int srst)
1011 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1012 high_output &= ~nTRSTnOE;
1014 high_output &= ~nTRST;
1018 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1019 high_output |= nTRSTnOE;
1021 high_output |= nTRST;
1026 high_output |= nSRST;
1030 high_output &= ~nSRST;
1033 /* command "set data bits high byte" */
1035 BUFFER_ADD = high_output;
1036 BUFFER_ADD = high_direction;
1037 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1040 void flyswatter_reset(int trst, int srst)
1044 low_output &= ~nTRST;
1048 low_output |= nTRST;
1053 low_output |= nSRST;
1057 low_output &= ~nSRST;
1060 /* command "set data bits low byte" */
1062 BUFFER_ADD = low_output;
1063 BUFFER_ADD = low_direction;
1064 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1067 void turtle_reset(int trst, int srst)
1073 low_output |= nSRST;
1077 low_output &= ~nSRST;
1080 /* command "set data bits low byte" */
1082 BUFFER_ADD = low_output;
1083 BUFFER_ADD = low_direction;
1084 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1087 void comstick_reset(int trst, int srst)
1091 high_output &= ~nTRST;
1095 high_output |= nTRST;
1100 high_output &= ~nSRST;
1104 high_output |= nSRST;
1107 /* command "set data bits high byte" */
1109 BUFFER_ADD = high_output;
1110 BUFFER_ADD = high_direction;
1111 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1114 void stm32stick_reset(int trst, int srst)
1118 high_output &= ~nTRST;
1122 high_output |= nTRST;
1127 low_output &= ~nSRST;
1131 low_output |= nSRST;
1134 /* command "set data bits low byte" */
1136 BUFFER_ADD = low_output;
1137 BUFFER_ADD = low_direction;
1139 /* command "set data bits high byte" */
1141 BUFFER_ADD = high_output;
1142 BUFFER_ADD = high_direction;
1143 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1146 int ft2232_execute_queue()
1148 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1149 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1151 int scan_size; /* size of IR or DR scan */
1152 enum scan_type type;
1154 int predicted_size = 0;
1155 int require_send = 0;
1158 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1159 * that wasn't handled by a caller-provided error handler
1163 ft2232_buffer_size = 0;
1164 ft2232_expect_read = 0;
1166 /* blink, if the current layout has that feature */
1174 case JTAG_END_STATE:
1175 if (cmd->cmd.end_state->end_state != -1)
1176 ft2232_end_state(cmd->cmd.end_state->end_state);
1179 /* only send the maximum buffer size that FT2232C can handle */
1181 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1183 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1184 retval = ERROR_JTAG_QUEUE_FAILED;
1189 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1191 cur_state = TAP_TLR;
1193 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1196 #ifdef _DEBUG_JTAG_IO_
1197 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1201 /* only send the maximum buffer size that FT2232C can handle */
1203 if (cur_state != TAP_RTI)
1204 predicted_size += 3;
1205 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1206 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1207 predicted_size += 3;
1208 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1209 predicted_size += 3;
1210 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1212 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1213 retval = ERROR_JTAG_QUEUE_FAILED;
1217 if (cur_state != TAP_RTI)
1219 /* command "Clock Data to TMS/CS Pin (no Read)" */
1224 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1225 cur_state = TAP_RTI;
1228 i = cmd->cmd.runtest->num_cycles;
1231 /* command "Clock Data to TMS/CS Pin (no Read)" */
1234 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1237 cur_state = TAP_RTI;
1238 i -= (i > 7) ? 7 : i;
1239 /* LOG_DEBUG("added TMS scan (no read)"); */
1241 if (cmd->cmd.runtest->end_state != -1)
1242 ft2232_end_state(cmd->cmd.runtest->end_state);
1243 if (cur_state != end_state)
1245 /* command "Clock Data to TMS/CS Pin (no Read)" */
1250 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1251 cur_state = end_state;
1252 /* LOG_DEBUG("added TMS scan (no read)"); */
1255 #ifdef _DEBUG_JTAG_IO_
1256 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1259 case JTAG_STATEMOVE:
1260 /* only send the maximum buffer size that FT2232C can handle */
1262 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1264 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1265 retval = ERROR_JTAG_QUEUE_FAILED;
1269 if (cmd->cmd.statemove->end_state != -1)
1270 ft2232_end_state(cmd->cmd.statemove->end_state);
1271 /* command "Clock Data to TMS/CS Pin (no Read)" */
1276 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1277 /* LOG_DEBUG("added TMS scan (no read)"); */
1278 cur_state = end_state;
1280 #ifdef _DEBUG_JTAG_IO_
1281 LOG_DEBUG("statemove: %i", end_state);
1285 /* only send the maximum buffer size that FT2232C can handle */
1286 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1287 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1289 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1290 retval = ERROR_JTAG_QUEUE_FAILED;
1294 ft2232_add_pathmove(cmd->cmd.pathmove);
1296 #ifdef _DEBUG_JTAG_IO_
1297 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1301 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1302 type = jtag_scan_type(cmd->cmd.scan);
1303 predicted_size = ft2232_predict_scan_out(scan_size, type);
1304 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1306 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1307 /* unsent commands before this */
1308 if (first_unsent != cmd)
1309 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1310 retval = ERROR_JTAG_QUEUE_FAILED;
1312 /* current command */
1313 if (cmd->cmd.scan->end_state != -1)
1314 ft2232_end_state(cmd->cmd.scan->end_state);
1315 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1317 first_unsent = cmd->next;
1322 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1324 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1325 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1326 retval = ERROR_JTAG_QUEUE_FAILED;
1330 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1331 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1332 if (cmd->cmd.scan->end_state != -1)
1333 ft2232_end_state(cmd->cmd.scan->end_state);
1334 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1338 #ifdef _DEBUG_JTAG_IO_
1339 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1343 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1344 retval = ERROR_JTAG_QUEUE_FAILED;
1345 first_unsent = cmd->next;
1346 jtag_sleep(cmd->cmd.sleep->us);
1347 #ifdef _DEBUG_JTAG_IO_
1348 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1352 LOG_ERROR("BUG: unknown JTAG command type encountered");
1358 if (require_send > 0)
1359 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1360 retval = ERROR_JTAG_QUEUE_FAILED;
1365 #if BUILD_FT2232_FTD2XX == 1
1366 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1369 DWORD openex_flags = 0;
1370 char *openex_string = NULL;
1373 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1374 ft2232_layout, vid, pid);
1377 /* Add non-standard Vid/Pid to the linux driver */
1378 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1380 LOG_WARNING("couldn't add %4.4x:%4.4x",
1385 if (ft2232_device_desc && ft2232_serial)
1387 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1388 ft2232_device_desc = NULL;
1391 if (ft2232_device_desc)
1393 openex_string = ft2232_device_desc;
1394 openex_flags = FT_OPEN_BY_DESCRIPTION;
1396 else if (ft2232_serial)
1398 openex_string = ft2232_serial;
1399 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1403 LOG_ERROR("neither device description nor serial number specified");
1404 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1406 return ERROR_JTAG_INIT_FAILED;
1409 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1414 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1417 return ERROR_JTAG_INIT_FAILED;
1419 LOG_ERROR("unable to open ftdi device: %lu", status);
1420 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1421 if (status == FT_OK)
1423 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1426 for (i = 0; i < num_devices; i++)
1427 desc_array[i] = malloc(64);
1428 desc_array[num_devices] = NULL;
1430 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1432 if (status == FT_OK)
1434 LOG_ERROR("ListDevices: %lu\n", num_devices);
1435 for (i = 0; i < num_devices; i++)
1436 LOG_ERROR("%i: %s", i, desc_array[i]);
1439 for (i = 0; i < num_devices; i++)
1440 free(desc_array[i]);
1445 LOG_ERROR("ListDevices: NONE\n");
1447 return ERROR_JTAG_INIT_FAILED;
1450 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1452 LOG_ERROR("unable to set latency timer: %lu", status);
1453 return ERROR_JTAG_INIT_FAILED;
1456 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1458 LOG_ERROR("unable to get latency timer: %lu", status);
1459 return ERROR_JTAG_INIT_FAILED;
1463 LOG_DEBUG("current latency timer: %i", latency_timer);
1466 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1468 LOG_ERROR("unable to set timeouts: %lu", status);
1469 return ERROR_JTAG_INIT_FAILED;
1472 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1474 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1475 return ERROR_JTAG_INIT_FAILED;
1481 static int ft2232_purge_ftd2xx(void)
1485 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1487 LOG_ERROR("error purging ftd2xx device: %lu", status);
1488 return ERROR_JTAG_INIT_FAILED;
1493 #endif /* BUILD_FT2232_FTD2XX == 1 */
1495 #if BUILD_FT2232_LIBFTDI == 1
1496 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1500 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1501 ft2232_layout, vid, pid);
1503 if (ftdi_init(&ftdic) < 0)
1504 return ERROR_JTAG_INIT_FAILED;
1506 /* context, vendor id, product id */
1507 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1508 ft2232_serial) < 0) {
1510 LOG_WARNING("unable to open ftdi device (trying more): %s",
1513 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1515 return ERROR_JTAG_INIT_FAILED;
1518 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1520 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1521 return ERROR_JTAG_INIT_FAILED;
1524 if (ftdi_usb_reset(&ftdic) < 0)
1526 LOG_ERROR("unable to reset ftdi device");
1527 return ERROR_JTAG_INIT_FAILED;
1530 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1532 LOG_ERROR("unable to set latency timer");
1533 return ERROR_JTAG_INIT_FAILED;
1536 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1538 LOG_ERROR("unable to get latency timer");
1539 return ERROR_JTAG_INIT_FAILED;
1543 LOG_DEBUG("current latency timer: %i", latency_timer);
1546 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1551 static int ft2232_purge_libftdi(void)
1553 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1555 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1556 return ERROR_JTAG_INIT_FAILED;
1561 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1563 int ft2232_init(void)
1568 ft2232_layout_t *cur_layout = ft2232_layouts;
1571 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1573 ft2232_layout = "usbjtag";
1574 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1577 while (cur_layout->name)
1579 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1581 layout = cur_layout;
1589 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1590 return ERROR_JTAG_INIT_FAILED;
1593 for (i = 0; 1; i++) {
1595 * "more indicates that there are more IDs to try, so we should
1596 * not print an error for an ID mismatch (but for anything
1599 * try_more indicates that the error code returned indicates an
1600 * ID mismatch (and nothing else) and that we should proceeed
1601 * with the next ID pair.
1603 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1606 #if BUILD_FT2232_FTD2XX == 1
1607 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1609 #elif BUILD_FT2232_LIBFTDI == 1
1610 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1615 if (!more || !try_more)
1619 ft2232_buffer_size = 0;
1620 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1622 if (layout->init() != ERROR_OK)
1623 return ERROR_JTAG_INIT_FAILED;
1625 ft2232_speed(jtag_speed);
1627 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1628 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1630 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1631 return ERROR_JTAG_INIT_FAILED;
1634 #if BUILD_FT2232_FTD2XX == 1
1635 return ft2232_purge_ftd2xx();
1636 #elif BUILD_FT2232_LIBFTDI == 1
1637 return ft2232_purge_libftdi();
1643 int usbjtag_init(void)
1649 low_direction = 0x0b;
1651 if (strcmp(ft2232_layout, "usbjtag") == 0)
1658 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1665 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1672 low_direction = 0x8b;
1676 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1677 return ERROR_JTAG_INIT_FAILED;
1680 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1682 low_direction &= ~nTRSTnOE; /* nTRST input */
1683 low_output &= ~nTRST; /* nTRST = 0 */
1687 low_direction |= nTRSTnOE; /* nTRST output */
1688 low_output |= nTRST; /* nTRST = 1 */
1691 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1693 low_direction |= nSRSTnOE; /* nSRST output */
1694 low_output |= nSRST; /* nSRST = 1 */
1698 low_direction &= ~nSRSTnOE; /* nSRST input */
1699 low_output &= ~nSRST; /* nSRST = 0 */
1702 /* initialize low byte for jtag */
1703 buf[0] = 0x80; /* command "set data bits low byte" */
1704 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1705 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1706 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1708 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1710 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1711 return ERROR_JTAG_INIT_FAILED;
1717 int jtagkey_init(void)
1723 low_direction = 0x1b;
1725 /* initialize low byte for jtag */
1726 buf[0] = 0x80; /* command "set data bits low byte" */
1727 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1728 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1729 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1731 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1733 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1734 return ERROR_JTAG_INIT_FAILED;
1737 if (strcmp(layout->name, "jtagkey") == 0)
1744 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1745 (strcmp(layout->name, "oocdlink") == 0))
1754 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1759 high_direction = 0x0f;
1761 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1763 high_output |= nTRSTnOE;
1764 high_output &= ~nTRST;
1768 high_output &= ~nTRSTnOE;
1769 high_output |= nTRST;
1772 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1774 high_output &= ~nSRSTnOE;
1775 high_output |= nSRST;
1779 high_output |= nSRSTnOE;
1780 high_output &= ~nSRST;
1783 /* initialize high port */
1784 buf[0] = 0x82; /* command "set data bits high byte" */
1785 buf[1] = high_output; /* value */
1786 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1787 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1789 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1791 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1792 return ERROR_JTAG_INIT_FAILED;
1798 int olimex_jtag_init(void)
1804 low_direction = 0x1b;
1806 /* initialize low byte for jtag */
1807 buf[0] = 0x80; /* command "set data bits low byte" */
1808 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1809 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1810 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1812 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1814 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1815 return ERROR_JTAG_INIT_FAILED;
1821 nSRSTnOE = 0x00; /* no output enable for nSRST */
1824 high_direction = 0x0f;
1826 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1828 high_output |= nTRSTnOE;
1829 high_output &= ~nTRST;
1833 high_output &= ~nTRSTnOE;
1834 high_output |= nTRST;
1837 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1839 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1843 high_output &= ~nSRST;
1846 /* turn red LED on */
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 'JTAGkey' layout");
1858 return ERROR_JTAG_INIT_FAILED;
1864 int flyswatter_init(void)
1870 low_direction = 0xfb;
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[12]=out, n[ST]srst=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 'flyswatter' layout");
1881 return ERROR_JTAG_INIT_FAILED;
1885 nTRSTnOE = 0x0; /* not output enable for nTRST */
1887 nSRSTnOE = 0x00; /* no output enable for nSRST */
1890 high_direction = 0x0c;
1892 /* turn red LED1 on, LED2 off */
1893 high_output |= 0x08;
1895 /* initialize high port */
1896 buf[0] = 0x82; /* command "set data bits high byte" */
1897 buf[1] = high_output; /* value */
1898 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1899 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1901 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1903 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1904 return ERROR_JTAG_INIT_FAILED;
1910 int turtle_init(void)
1916 low_direction = 0x5b;
1918 /* initialize low byte for jtag */
1919 buf[0] = 0x80; /* command "set data bits low byte" */
1920 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1921 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1922 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1924 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1926 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1927 return ERROR_JTAG_INIT_FAILED;
1933 high_direction = 0x0C;
1935 /* initialize high port */
1936 buf[0] = 0x82; /* command "set data bits high byte" */
1937 buf[1] = high_output;
1938 buf[2] = high_direction;
1939 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1941 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1943 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1944 return ERROR_JTAG_INIT_FAILED;
1950 int comstick_init(void)
1956 low_direction = 0x0b;
1958 /* initialize low byte for jtag */
1959 buf[0] = 0x80; /* command "set data bits low byte" */
1960 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1961 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1962 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1964 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1966 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1967 return ERROR_JTAG_INIT_FAILED;
1971 nTRSTnOE = 0x00; /* no output enable for nTRST */
1973 nSRSTnOE = 0x00; /* no output enable for nSRST */
1976 high_direction = 0x03;
1978 /* initialize high port */
1979 buf[0] = 0x82; /* command "set data bits high byte" */
1980 buf[1] = high_output;
1981 buf[2] = high_direction;
1982 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1984 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1986 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1987 return ERROR_JTAG_INIT_FAILED;
1993 int stm32stick_init(void)
1999 low_direction = 0x8b;
2001 /* initialize low byte for jtag */
2002 buf[0] = 0x80; /* command "set data bits low byte" */
2003 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2004 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2005 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2007 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2009 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2010 return ERROR_JTAG_INIT_FAILED;
2014 nTRSTnOE = 0x00; /* no output enable for nTRST */
2016 nSRSTnOE = 0x00; /* no output enable for nSRST */
2019 high_direction = 0x03;
2021 /* initialize high port */
2022 buf[0] = 0x82; /* command "set data bits high byte" */
2023 buf[1] = high_output;
2024 buf[2] = high_direction;
2025 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2027 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2029 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2030 return ERROR_JTAG_INIT_FAILED;
2036 void olimex_jtag_blink(void)
2038 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2039 * ACBUS3 is bit 3 of the GPIOH port
2041 if (high_output & 0x08)
2043 /* set port pin high */
2044 high_output &= 0x07;
2048 /* set port pin low */
2049 high_output |= 0x08;
2053 BUFFER_ADD = high_output;
2054 BUFFER_ADD = high_direction;
2057 void turtle_jtag_blink(void)
2060 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2062 if (high_output & 0x08)
2072 BUFFER_ADD = high_output;
2073 BUFFER_ADD = high_direction;
2076 int ft2232_quit(void)
2078 #if BUILD_FT2232_FTD2XX == 1
2081 status = FT_Close(ftdih);
2082 #elif BUILD_FT2232_LIBFTDI == 1
2083 ftdi_disable_bitbang(&ftdic);
2085 ftdi_usb_close(&ftdic);
2087 ftdi_deinit(&ftdic);
2090 free(ft2232_buffer);
2091 ft2232_buffer = NULL;
2096 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2100 ft2232_device_desc = strdup(args[0]);
2104 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2110 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2114 ft2232_serial = strdup(args[0]);
2118 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2124 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2129 ft2232_layout = malloc(strlen(args[0]) + 1);
2130 strcpy(ft2232_layout, args[0]);
2135 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2139 if (argc > MAX_USB_IDS*2) {
2140 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2141 "(maximum is %d pairs)", MAX_USB_IDS);
2142 argc = MAX_USB_IDS*2;
2144 if (argc < 2 || (argc & 1))
2146 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2151 for (i = 0; i+1 < argc; i += 2) {
2152 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2153 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2156 * Explicitly terminate, in case there are multiples instances of
2159 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2164 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2168 ft2232_latency = atoi(args[0]);
2172 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");