1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
31 #include "replacements.h"
33 /* project specific includes */
37 #include "configuration.h"
38 #include "time_support.h"
45 /* FT2232 access library includes */
46 #if BUILD_FT2232_FTD2XX == 1
48 #elif BUILD_FT2232_LIBFTDI == 1
52 /* enable this to debug io latency
55 #define _DEBUG_USB_IO_
58 /* enable this to debug communication
61 #define _DEBUG_USB_COMMS_
64 int ft2232_execute_queue(void);
66 int ft2232_speed(int speed);
67 int ft2232_speed_div(int speed, int *khz);
68 int ft2232_khz(int khz, int *jtag_speed);
69 int ft2232_register_commands(struct command_context_s *cmd_ctx);
70 int ft2232_init(void);
71 int ft2232_quit(void);
73 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 char *ft2232_device_desc = NULL;
80 char *ft2232_serial = NULL;
81 char *ft2232_layout = NULL;
82 unsigned char ft2232_latency = 2;
85 /* vid = pid = 0 marks the end of the list */
86 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
87 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
89 typedef struct ft2232_layout_s
93 void(*reset)(int trst, int srst);
97 /* init procedures for supported layouts */
98 int usbjtag_init(void);
99 int jtagkey_init(void);
100 int olimex_jtag_init(void);
101 int flyswatter_init(void);
102 int turtle_init(void);
103 int comstick_init(void);
104 int stm32stick_init(void);
106 /* reset procedures for supported layouts */
107 void usbjtag_reset(int trst, int srst);
108 void jtagkey_reset(int trst, int srst);
109 void olimex_jtag_reset(int trst, int srst);
110 void flyswatter_reset(int trst, int srst);
111 void turtle_reset(int trst, int srst);
112 void comstick_reset(int trst, int srst);
113 void stm32stick_reset(int trst, int srst);
115 /* blink procedures for layouts that support a blinking led */
116 void olimex_jtag_blink(void);
117 void turtle_jtag_blink(void);
119 ft2232_layout_t ft2232_layouts[] =
121 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
122 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
123 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
124 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
125 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
126 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
127 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
128 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
129 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
130 {"comstick", comstick_init, comstick_reset, NULL},
131 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
135 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
137 static ft2232_layout_t *layout;
138 static u8 low_output = 0x0;
139 static u8 low_direction = 0x0;
140 static u8 high_output = 0x0;
141 static u8 high_direction = 0x0;
143 #if BUILD_FT2232_FTD2XX == 1
144 static FT_HANDLE ftdih = NULL;
145 #elif BUILD_FT2232_LIBFTDI == 1
146 static struct ftdi_context ftdic;
149 static u8 *ft2232_buffer = NULL;
150 static int ft2232_buffer_size = 0;
151 static int ft2232_read_pointer = 0;
152 static int ft2232_expect_read = 0;
153 #define FT2232_BUFFER_SIZE 131072
154 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
155 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
157 jtag_interface_t ft2232_interface =
160 .execute_queue = ft2232_execute_queue,
161 .speed = ft2232_speed,
162 .speed_div = ft2232_speed_div,
164 .register_commands = ft2232_register_commands,
169 int ft2232_write(u8 *buf, int size, u32* bytes_written)
171 #if BUILD_FT2232_FTD2XX == 1
173 DWORD dw_bytes_written;
174 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
176 *bytes_written = dw_bytes_written;
177 LOG_ERROR("FT_Write returned: %lu", status);
178 return ERROR_JTAG_DEVICE_ERROR;
182 *bytes_written = dw_bytes_written;
185 #elif BUILD_FT2232_LIBFTDI == 1
187 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
190 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
191 return ERROR_JTAG_DEVICE_ERROR;
195 *bytes_written = retval;
201 int ft2232_read(u8* buf, int size, u32* bytes_read)
203 #if BUILD_FT2232_FTD2XX == 1
209 while ((*bytes_read < size) && timeout--)
211 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
212 *bytes_read, &dw_bytes_read)) != FT_OK)
215 LOG_ERROR("FT_Read returned: %lu", status);
216 return ERROR_JTAG_DEVICE_ERROR;
218 *bytes_read += dw_bytes_read;
220 #elif BUILD_FT2232_LIBFTDI == 1
225 while ((*bytes_read < size) && timeout--)
227 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
230 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
231 return ERROR_JTAG_DEVICE_ERROR;
233 *bytes_read += retval;
237 if (*bytes_read < size)
239 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
240 return ERROR_JTAG_DEVICE_ERROR;
246 int ft2232_speed(int speed)
252 buf[0] = 0x86; /* command "set divisor" */
253 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
254 buf[2] = (speed >> 8) & 0xff; /* valueH */
256 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
257 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
259 LOG_ERROR("couldn't set FT2232 TCK speed");
266 int ft2232_speed_div(int speed, int *khz)
268 /* Take a look in the FT2232 manual,
269 * AN2232C-01 Command Processor for
270 * MPSSE and MCU Host Bus. Chapter 3.8 */
272 *khz = 6000 / (1+speed);
277 int ft2232_khz(int khz, int *jtag_speed)
281 LOG_ERROR("RCLK not supported");
284 /* Take a look in the FT2232 manual,
285 * AN2232C-01 Command Processor for
286 * MPSSE and MCU Host Bus. Chapter 3.8
288 * We will calc here with a multiplier
289 * of 10 for better rounding later. */
291 /* Calc speed, (6000 / khz) - 1 */
292 /* Use 65000 for better rounding */
293 *jtag_speed = (60000 / khz) - 10;
295 /* Add 0.9 for rounding */
298 /* Calc real speed */
299 *jtag_speed = *jtag_speed / 10;
301 /* Check if speed is greater than 0 */
307 /* Check max value */
308 if (*jtag_speed > 0xFFFF)
310 *jtag_speed = 0xFFFF;
316 int ft2232_register_commands(struct command_context_s *cmd_ctx)
318 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
319 COMMAND_CONFIG, NULL);
320 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
321 COMMAND_CONFIG, NULL);
322 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
323 COMMAND_CONFIG, NULL);
324 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
325 COMMAND_CONFIG, NULL);
326 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
327 COMMAND_CONFIG, NULL);
331 void ft2232_end_state(enum tap_state state)
333 if (tap_move_map[state] != -1)
337 LOG_ERROR("BUG: %i is not a valid end state", state);
342 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
344 int num_bytes = ((scan_size + 7) / 8);
345 int bits_left = scan_size;
348 while(num_bytes-- > 1)
350 buffer[cur_byte] = BUFFER_READ;
355 buffer[cur_byte] = 0x0;
359 buffer[cur_byte] = BUFFER_READ >> 1;
362 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
366 void ft2232_debug_dump_buffer(void)
372 for (i = 0; i < ft2232_buffer_size; i++)
374 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
377 LOG_DEBUG("%s", line);
383 LOG_DEBUG("%s", line);
386 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
396 #ifdef _DEBUG_USB_IO_
397 struct timeval start, inter, inter2, end;
398 struct timeval d_inter, d_inter2, d_end;
401 #ifdef _DEBUG_USB_COMMS_
402 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
403 ft2232_debug_dump_buffer();
406 #ifdef _DEBUG_USB_IO_
407 gettimeofday(&start, NULL);
410 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
412 LOG_ERROR("couldn't write MPSSE commands to FT2232");
416 #ifdef _DEBUG_USB_IO_
417 gettimeofday(&inter, NULL);
420 if (ft2232_expect_read)
423 ft2232_buffer_size = 0;
425 #ifdef _DEBUG_USB_IO_
426 gettimeofday(&inter2, NULL);
429 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
431 LOG_ERROR("couldn't read from FT2232");
435 #ifdef _DEBUG_USB_IO_
436 gettimeofday(&end, NULL);
438 timeval_subtract(&d_inter, &inter, &start);
439 timeval_subtract(&d_inter2, &inter2, &start);
440 timeval_subtract(&d_end, &end, &start);
442 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);
446 ft2232_buffer_size = bytes_read;
448 if (ft2232_expect_read != ft2232_buffer_size)
450 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
451 ft2232_debug_dump_buffer();
456 #ifdef _DEBUG_USB_COMMS_
457 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
458 ft2232_debug_dump_buffer();
462 ft2232_expect_read = 0;
463 ft2232_read_pointer = 0;
465 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
466 * that wasn't handled by a caller-provided error handler
476 type = jtag_scan_type(cmd->cmd.scan);
477 if (type != SCAN_OUT)
479 scan_size = jtag_scan_size(cmd->cmd.scan);
480 buffer = calloc(CEIL(scan_size, 8), 1);
481 ft2232_read_scan(type, buffer, scan_size);
482 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
483 retval = ERROR_JTAG_QUEUE_FAILED;
493 ft2232_buffer_size = 0;
498 void ft2232_add_pathmove(pathmove_command_t *cmd)
500 int num_states = cmd->num_states;
509 int num_states_batch = num_states > 7 ? 7 : num_states;
512 /* command "Clock Data to TMS/CS Pin (no Read)" */
514 /* number of states remaining */
515 BUFFER_ADD = num_states_batch - 1;
517 while (num_states_batch--)
519 if (tap_transitions[cur_state].low == cmd->path[state_count])
520 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
521 else if (tap_transitions[cur_state].high == cmd->path[state_count])
522 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
525 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
529 cur_state = cmd->path[state_count];
534 BUFFER_ADD = tms_byte;
537 end_state = cur_state;
540 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
542 int num_bytes = (scan_size + 7) / 8;
543 int bits_left = scan_size;
547 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
549 /* command "Clock Data to TMS/CS Pin (no Read)" */
556 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
561 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
564 /* LOG_DEBUG("added TMS scan (no read)"); */
567 /* add command for complete bytes */
568 while (num_bytes > 1)
573 /* Clock Data Bytes In and Out LSB First */
575 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
577 else if (type == SCAN_OUT)
579 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
581 /* LOG_DEBUG("added TDI bytes (o)"); */
583 else if (type == SCAN_IN)
585 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
587 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
589 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
590 num_bytes -= thisrun_bytes;
591 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
592 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
595 /* add complete bytes */
596 while(thisrun_bytes-- > 0)
598 BUFFER_ADD = buffer[cur_byte];
603 else /* (type == SCAN_IN) */
605 bits_left -= 8 * (thisrun_bytes);
609 /* the most signifcant bit is scanned during TAP movement */
611 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
615 /* process remaining bits but the last one */
620 /* Clock Data Bits In and Out LSB First */
622 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
624 else if (type == SCAN_OUT)
626 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
628 /* LOG_DEBUG("added TDI bits (o)"); */
630 else if (type == SCAN_IN)
632 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
634 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
636 BUFFER_ADD = bits_left - 2;
638 BUFFER_ADD = buffer[cur_byte];
641 if ((ir_scan && (end_state == TAP_SI)) ||
642 (!ir_scan && (end_state == TAP_SD)))
646 /* Clock Data Bits In and Out LSB First */
648 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
650 else if (type == SCAN_OUT)
652 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
654 /* LOG_DEBUG("added TDI bits (o)"); */
656 else if (type == SCAN_IN)
658 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
660 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
663 BUFFER_ADD = last_bit;
667 /* move from Shift-IR/DR to end state */
668 if (type != SCAN_OUT)
670 /* Clock Data to TMS/CS Pin with Read */
672 /* LOG_DEBUG("added TMS scan (read)"); */
676 /* Clock Data to TMS/CS Pin (no Read) */
678 /* LOG_DEBUG("added TMS scan (no read)"); */
681 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
682 cur_state = end_state;
686 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
688 int num_bytes = (scan_size + 7) / 8;
689 int bits_left = scan_size;
692 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
693 u8 *receive_pointer = receive_buffer;
697 int thisrun_read = 0;
701 LOG_ERROR("BUG: large IR scans are not supported");
705 if (cur_state != TAP_SD)
707 /* command "Clock Data to TMS/CS Pin (no Read)" */
712 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
716 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
718 LOG_ERROR("couldn't write MPSSE commands to FT2232");
721 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
722 ft2232_buffer_size = 0;
724 /* add command for complete bytes */
725 while (num_bytes > 1)
731 /* Clock Data Bytes In and Out LSB First */
733 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
735 else if (type == SCAN_OUT)
737 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
739 /* LOG_DEBUG("added TDI bytes (o)"); */
741 else if (type == SCAN_IN)
743 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
745 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
747 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
748 thisrun_read = thisrun_bytes;
749 num_bytes -= thisrun_bytes;
750 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
751 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
754 /* add complete bytes */
755 while(thisrun_bytes-- > 0)
757 BUFFER_ADD = buffer[cur_byte];
762 else /* (type == SCAN_IN) */
764 bits_left -= 8 * (thisrun_bytes);
767 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
769 LOG_ERROR("couldn't write MPSSE commands to FT2232");
772 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
773 ft2232_buffer_size = 0;
775 if (type != SCAN_OUT)
777 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
779 LOG_ERROR("couldn't read from FT2232");
782 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
783 receive_pointer += bytes_read;
789 /* the most signifcant bit is scanned during TAP movement */
791 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
795 /* process remaining bits but the last one */
800 /* Clock Data Bits In and Out LSB First */
802 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
804 else if (type == SCAN_OUT)
806 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
808 /* LOG_DEBUG("added TDI bits (o)"); */
810 else if (type == SCAN_IN)
812 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
814 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
816 BUFFER_ADD = bits_left - 2;
818 BUFFER_ADD = buffer[cur_byte];
820 if (type != SCAN_OUT)
824 if (end_state == TAP_SD)
828 /* Clock Data Bits In and Out LSB First */
830 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
832 else if (type == SCAN_OUT)
834 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
836 /* LOG_DEBUG("added TDI bits (o)"); */
838 else if (type == SCAN_IN)
840 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
842 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
845 BUFFER_ADD = last_bit;
849 /* move from Shift-IR/DR to end state */
850 if (type != SCAN_OUT)
852 /* Clock Data to TMS/CS Pin with Read */
854 /* LOG_DEBUG("added TMS scan (read)"); */
858 /* Clock Data to TMS/CS Pin (no Read) */
860 /* LOG_DEBUG("added TMS scan (no read)"); */
863 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
864 cur_state = end_state;
867 if (type != SCAN_OUT)
870 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
872 LOG_ERROR("couldn't write MPSSE commands to FT2232");
875 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
876 ft2232_buffer_size = 0;
878 if (type != SCAN_OUT)
880 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
882 LOG_ERROR("couldn't read from FT2232");
885 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
886 receive_pointer += bytes_read;
892 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
894 int predicted_size = 3;
895 int num_bytes = (scan_size - 1) / 8;
897 if (cur_state != TAP_SD)
900 if (type == SCAN_IN) /* only from device to host */
903 predicted_size += (CEIL(num_bytes, 65536)) * 3;
904 /* remaining bits - 1 (up to 7) */
905 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
907 else /* host to device, or bidirectional */
910 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
911 /* remaining bits -1 (up to 7) */
912 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
915 return predicted_size;
918 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
920 int predicted_size = 0;
922 if (type != SCAN_OUT)
925 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
926 /* remaining bits - 1 */
927 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
928 /* last bit (from TMS scan) */
932 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
934 return predicted_size;
937 void usbjtag_reset(int trst, int srst)
941 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
942 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
944 low_output &= ~nTRST; /* switch output low */
948 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
949 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
951 low_output |= nTRST; /* switch output high */
956 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
957 low_output &= ~nSRST; /* switch output low */
959 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
963 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
964 low_output |= nSRST; /* switch output high */
966 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
969 /* command "set data bits low byte" */
971 BUFFER_ADD = low_output;
972 BUFFER_ADD = low_direction;
976 void jtagkey_reset(int trst, int srst)
980 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
981 high_output &= ~nTRSTnOE;
983 high_output &= ~nTRST;
987 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
988 high_output |= nTRSTnOE;
990 high_output |= nTRST;
995 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
996 high_output &= ~nSRST;
998 high_output &= ~nSRSTnOE;
1002 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1003 high_output |= nSRST;
1005 high_output |= nSRSTnOE;
1008 /* command "set data bits high byte" */
1010 BUFFER_ADD = high_output;
1011 BUFFER_ADD = high_direction;
1012 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1015 void olimex_jtag_reset(int trst, int srst)
1019 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1020 high_output &= ~nTRSTnOE;
1022 high_output &= ~nTRST;
1026 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1027 high_output |= nTRSTnOE;
1029 high_output |= nTRST;
1034 high_output |= nSRST;
1038 high_output &= ~nSRST;
1041 /* command "set data bits high byte" */
1043 BUFFER_ADD = high_output;
1044 BUFFER_ADD = high_direction;
1045 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1048 void flyswatter_reset(int trst, int srst)
1052 low_output &= ~nTRST;
1056 low_output |= nTRST;
1061 low_output |= nSRST;
1065 low_output &= ~nSRST;
1068 /* command "set data bits low byte" */
1070 BUFFER_ADD = low_output;
1071 BUFFER_ADD = low_direction;
1072 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1075 void turtle_reset(int trst, int srst)
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;
1092 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1095 void comstick_reset(int trst, int srst)
1099 high_output &= ~nTRST;
1103 high_output |= nTRST;
1108 high_output &= ~nSRST;
1112 high_output |= nSRST;
1115 /* command "set data bits high byte" */
1117 BUFFER_ADD = high_output;
1118 BUFFER_ADD = high_direction;
1119 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1122 void stm32stick_reset(int trst, int srst)
1126 high_output &= ~nTRST;
1130 high_output |= nTRST;
1135 low_output &= ~nSRST;
1139 low_output |= nSRST;
1142 /* command "set data bits low byte" */
1144 BUFFER_ADD = low_output;
1145 BUFFER_ADD = low_direction;
1147 /* command "set data bits high byte" */
1149 BUFFER_ADD = high_output;
1150 BUFFER_ADD = high_direction;
1151 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1154 int ft2232_execute_queue()
1156 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1157 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1159 int scan_size; /* size of IR or DR scan */
1160 enum scan_type type;
1162 int predicted_size = 0;
1163 int require_send = 0;
1166 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1167 * that wasn't handled by a caller-provided error handler
1171 ft2232_buffer_size = 0;
1172 ft2232_expect_read = 0;
1174 /* blink, if the current layout has that feature */
1182 case JTAG_END_STATE:
1183 if (cmd->cmd.end_state->end_state != -1)
1184 ft2232_end_state(cmd->cmd.end_state->end_state);
1187 /* only send the maximum buffer size that FT2232C can handle */
1189 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1191 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1192 retval = ERROR_JTAG_QUEUE_FAILED;
1197 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1199 cur_state = TAP_TLR;
1201 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1204 #ifdef _DEBUG_JTAG_IO_
1205 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1209 /* only send the maximum buffer size that FT2232C can handle */
1211 if (cur_state != TAP_RTI)
1212 predicted_size += 3;
1213 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1214 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1215 predicted_size += 3;
1216 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1217 predicted_size += 3;
1218 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1220 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1221 retval = ERROR_JTAG_QUEUE_FAILED;
1225 if (cur_state != TAP_RTI)
1227 /* command "Clock Data to TMS/CS Pin (no Read)" */
1232 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1233 cur_state = TAP_RTI;
1236 i = cmd->cmd.runtest->num_cycles;
1239 /* command "Clock Data to TMS/CS Pin (no Read)" */
1242 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1245 cur_state = TAP_RTI;
1246 i -= (i > 7) ? 7 : i;
1247 /* LOG_DEBUG("added TMS scan (no read)"); */
1249 if (cmd->cmd.runtest->end_state != -1)
1250 ft2232_end_state(cmd->cmd.runtest->end_state);
1251 if (cur_state != end_state)
1253 /* command "Clock Data to TMS/CS Pin (no Read)" */
1258 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1259 cur_state = end_state;
1260 /* LOG_DEBUG("added TMS scan (no read)"); */
1263 #ifdef _DEBUG_JTAG_IO_
1264 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1267 case JTAG_STATEMOVE:
1268 /* only send the maximum buffer size that FT2232C can handle */
1270 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1272 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1273 retval = ERROR_JTAG_QUEUE_FAILED;
1277 if (cmd->cmd.statemove->end_state != -1)
1278 ft2232_end_state(cmd->cmd.statemove->end_state);
1279 /* command "Clock Data to TMS/CS Pin (no Read)" */
1284 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1285 /* LOG_DEBUG("added TMS scan (no read)"); */
1286 cur_state = end_state;
1288 #ifdef _DEBUG_JTAG_IO_
1289 LOG_DEBUG("statemove: %i", end_state);
1293 /* only send the maximum buffer size that FT2232C can handle */
1294 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1295 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1297 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1298 retval = ERROR_JTAG_QUEUE_FAILED;
1302 ft2232_add_pathmove(cmd->cmd.pathmove);
1304 #ifdef _DEBUG_JTAG_IO_
1305 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1309 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1310 type = jtag_scan_type(cmd->cmd.scan);
1311 predicted_size = ft2232_predict_scan_out(scan_size, type);
1312 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1314 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1315 /* unsent commands before this */
1316 if (first_unsent != cmd)
1317 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1318 retval = ERROR_JTAG_QUEUE_FAILED;
1320 /* current command */
1321 if (cmd->cmd.scan->end_state != -1)
1322 ft2232_end_state(cmd->cmd.scan->end_state);
1323 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1325 first_unsent = cmd->next;
1330 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1332 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1333 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1334 retval = ERROR_JTAG_QUEUE_FAILED;
1338 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1339 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1340 if (cmd->cmd.scan->end_state != -1)
1341 ft2232_end_state(cmd->cmd.scan->end_state);
1342 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1346 #ifdef _DEBUG_JTAG_IO_
1347 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1351 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1352 retval = ERROR_JTAG_QUEUE_FAILED;
1353 first_unsent = cmd->next;
1354 jtag_sleep(cmd->cmd.sleep->us);
1355 #ifdef _DEBUG_JTAG_IO_
1356 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1360 LOG_ERROR("BUG: unknown JTAG command type encountered");
1366 if (require_send > 0)
1367 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1368 retval = ERROR_JTAG_QUEUE_FAILED;
1373 #if BUILD_FT2232_FTD2XX == 1
1374 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1377 DWORD openex_flags = 0;
1378 char *openex_string = NULL;
1381 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1382 ft2232_layout, vid, pid);
1385 /* Add non-standard Vid/Pid to the linux driver */
1386 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1388 LOG_WARNING("couldn't add %4.4x:%4.4x",
1393 if (ft2232_device_desc && ft2232_serial)
1395 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1396 ft2232_device_desc = NULL;
1399 if (ft2232_device_desc)
1401 openex_string = ft2232_device_desc;
1402 openex_flags = FT_OPEN_BY_DESCRIPTION;
1404 else if (ft2232_serial)
1406 openex_string = ft2232_serial;
1407 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1411 LOG_ERROR("neither device description nor serial number specified");
1412 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1414 return ERROR_JTAG_INIT_FAILED;
1417 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1422 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1425 return ERROR_JTAG_INIT_FAILED;
1427 LOG_ERROR("unable to open ftdi device: %lu", status);
1428 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1429 if (status == FT_OK)
1431 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1434 for (i = 0; i < num_devices; i++)
1435 desc_array[i] = malloc(64);
1436 desc_array[num_devices] = NULL;
1438 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1440 if (status == FT_OK)
1442 LOG_ERROR("ListDevices: %lu\n", num_devices);
1443 for (i = 0; i < num_devices; i++)
1444 LOG_ERROR("%i: %s", i, desc_array[i]);
1447 for (i = 0; i < num_devices; i++)
1448 free(desc_array[i]);
1453 LOG_ERROR("ListDevices: NONE\n");
1455 return ERROR_JTAG_INIT_FAILED;
1458 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1460 LOG_ERROR("unable to set latency timer: %lu", status);
1461 return ERROR_JTAG_INIT_FAILED;
1464 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1466 LOG_ERROR("unable to get latency timer: %lu", status);
1467 return ERROR_JTAG_INIT_FAILED;
1471 LOG_DEBUG("current latency timer: %i", latency_timer);
1474 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1476 LOG_ERROR("unable to set timeouts: %lu", status);
1477 return ERROR_JTAG_INIT_FAILED;
1480 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1482 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1483 return ERROR_JTAG_INIT_FAILED;
1489 static int ft2232_purge_ftd2xx(void)
1493 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1495 LOG_ERROR("error purging ftd2xx device: %lu", status);
1496 return ERROR_JTAG_INIT_FAILED;
1501 #endif /* BUILD_FT2232_FTD2XX == 1 */
1503 #if BUILD_FT2232_LIBFTDI == 1
1504 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1508 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1509 ft2232_layout, vid, pid);
1511 if (ftdi_init(&ftdic) < 0)
1512 return ERROR_JTAG_INIT_FAILED;
1514 /* context, vendor id, product id */
1515 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1516 ft2232_serial) < 0) {
1518 LOG_WARNING("unable to open ftdi device (trying more): %s",
1521 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1523 return ERROR_JTAG_INIT_FAILED;
1526 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1528 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1529 return ERROR_JTAG_INIT_FAILED;
1532 if (ftdi_usb_reset(&ftdic) < 0)
1534 LOG_ERROR("unable to reset ftdi device");
1535 return ERROR_JTAG_INIT_FAILED;
1538 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1540 LOG_ERROR("unable to set latency timer");
1541 return ERROR_JTAG_INIT_FAILED;
1544 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1546 LOG_ERROR("unable to get latency timer");
1547 return ERROR_JTAG_INIT_FAILED;
1551 LOG_DEBUG("current latency timer: %i", latency_timer);
1554 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1559 static int ft2232_purge_libftdi(void)
1561 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1563 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1564 return ERROR_JTAG_INIT_FAILED;
1569 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1571 int ft2232_init(void)
1576 ft2232_layout_t *cur_layout = ft2232_layouts;
1579 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1581 ft2232_layout = "usbjtag";
1582 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1585 while (cur_layout->name)
1587 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1589 layout = cur_layout;
1597 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1598 return ERROR_JTAG_INIT_FAILED;
1601 for (i = 0; 1; i++) {
1603 * "more indicates that there are more IDs to try, so we should
1604 * not print an error for an ID mismatch (but for anything
1607 * try_more indicates that the error code returned indicates an
1608 * ID mismatch (and nothing else) and that we should proceeed
1609 * with the next ID pair.
1611 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1614 #if BUILD_FT2232_FTD2XX == 1
1615 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1617 #elif BUILD_FT2232_LIBFTDI == 1
1618 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1623 if (!more || !try_more)
1627 ft2232_buffer_size = 0;
1628 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1630 if (layout->init() != ERROR_OK)
1631 return ERROR_JTAG_INIT_FAILED;
1633 ft2232_speed(jtag_speed);
1635 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1636 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1638 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1639 return ERROR_JTAG_INIT_FAILED;
1642 #if BUILD_FT2232_FTD2XX == 1
1643 return ft2232_purge_ftd2xx();
1644 #elif BUILD_FT2232_LIBFTDI == 1
1645 return ft2232_purge_libftdi();
1651 int usbjtag_init(void)
1657 low_direction = 0x0b;
1659 if (strcmp(ft2232_layout, "usbjtag") == 0)
1666 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1673 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1680 low_direction = 0x8b;
1684 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1685 return ERROR_JTAG_INIT_FAILED;
1688 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1690 low_direction &= ~nTRSTnOE; /* nTRST input */
1691 low_output &= ~nTRST; /* nTRST = 0 */
1695 low_direction |= nTRSTnOE; /* nTRST output */
1696 low_output |= nTRST; /* nTRST = 1 */
1699 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1701 low_direction |= nSRSTnOE; /* nSRST output */
1702 low_output |= nSRST; /* nSRST = 1 */
1706 low_direction &= ~nSRSTnOE; /* nSRST input */
1707 low_output &= ~nSRST; /* nSRST = 0 */
1710 /* initialize low byte for jtag */
1711 buf[0] = 0x80; /* command "set data bits low byte" */
1712 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1713 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1714 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1716 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1718 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1719 return ERROR_JTAG_INIT_FAILED;
1725 int jtagkey_init(void)
1731 low_direction = 0x1b;
1733 /* initialize low byte for jtag */
1734 buf[0] = 0x80; /* command "set data bits low byte" */
1735 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1736 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1737 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1739 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1741 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1742 return ERROR_JTAG_INIT_FAILED;
1745 if (strcmp(layout->name, "jtagkey") == 0)
1752 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1753 (strcmp(layout->name, "oocdlink") == 0))
1762 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1767 high_direction = 0x0f;
1769 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1771 high_output |= nTRSTnOE;
1772 high_output &= ~nTRST;
1776 high_output &= ~nTRSTnOE;
1777 high_output |= nTRST;
1780 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1782 high_output &= ~nSRSTnOE;
1783 high_output |= nSRST;
1787 high_output |= nSRSTnOE;
1788 high_output &= ~nSRST;
1791 /* initialize high port */
1792 buf[0] = 0x82; /* command "set data bits high byte" */
1793 buf[1] = high_output; /* value */
1794 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1795 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1797 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1799 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1800 return ERROR_JTAG_INIT_FAILED;
1806 int olimex_jtag_init(void)
1812 low_direction = 0x1b;
1814 /* initialize low byte for jtag */
1815 buf[0] = 0x80; /* command "set data bits low byte" */
1816 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1817 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1818 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1820 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1822 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1823 return ERROR_JTAG_INIT_FAILED;
1829 nSRSTnOE = 0x00; /* no output enable for nSRST */
1832 high_direction = 0x0f;
1834 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1836 high_output |= nTRSTnOE;
1837 high_output &= ~nTRST;
1841 high_output &= ~nTRSTnOE;
1842 high_output |= nTRST;
1845 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1847 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1851 high_output &= ~nSRST;
1854 /* turn red LED on */
1855 high_output |= 0x08;
1857 /* initialize high port */
1858 buf[0] = 0x82; /* command "set data bits high byte" */
1859 buf[1] = high_output; /* value */
1860 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1861 LOG_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 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1866 return ERROR_JTAG_INIT_FAILED;
1872 int flyswatter_init(void)
1878 low_direction = 0xfb;
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[12]=out, n[ST]srst=out */
1884 LOG_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 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1889 return ERROR_JTAG_INIT_FAILED;
1893 nTRSTnOE = 0x0; /* not output enable for nTRST */
1895 nSRSTnOE = 0x00; /* no output enable for nSRST */
1898 high_direction = 0x0c;
1900 /* turn red LED1 on, LED2 off */
1901 high_output |= 0x08;
1903 /* initialize high port */
1904 buf[0] = 0x82; /* command "set data bits high byte" */
1905 buf[1] = high_output; /* value */
1906 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1907 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1909 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1911 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1912 return ERROR_JTAG_INIT_FAILED;
1918 int turtle_init(void)
1924 low_direction = 0x5b;
1926 /* initialize low byte for jtag */
1927 buf[0] = 0x80; /* command "set data bits low byte" */
1928 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1929 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1930 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1932 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1934 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1935 return ERROR_JTAG_INIT_FAILED;
1941 high_direction = 0x0C;
1943 /* initialize high port */
1944 buf[0] = 0x82; /* command "set data bits high byte" */
1945 buf[1] = high_output;
1946 buf[2] = high_direction;
1947 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1949 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1951 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1952 return ERROR_JTAG_INIT_FAILED;
1958 int comstick_init(void)
1964 low_direction = 0x0b;
1966 /* initialize low byte for jtag */
1967 buf[0] = 0x80; /* command "set data bits low byte" */
1968 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1969 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1970 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1972 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1974 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1975 return ERROR_JTAG_INIT_FAILED;
1979 nTRSTnOE = 0x00; /* no output enable for nTRST */
1981 nSRSTnOE = 0x00; /* no output enable for nSRST */
1984 high_direction = 0x03;
1986 /* initialize high port */
1987 buf[0] = 0x82; /* command "set data bits high byte" */
1988 buf[1] = high_output;
1989 buf[2] = high_direction;
1990 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1992 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1994 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1995 return ERROR_JTAG_INIT_FAILED;
2001 int stm32stick_init(void)
2007 low_direction = 0x8b;
2009 /* initialize low byte for jtag */
2010 buf[0] = 0x80; /* command "set data bits low byte" */
2011 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2012 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2013 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2015 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2017 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2018 return ERROR_JTAG_INIT_FAILED;
2022 nTRSTnOE = 0x00; /* no output enable for nTRST */
2024 nSRSTnOE = 0x00; /* no output enable for nSRST */
2027 high_direction = 0x03;
2029 /* initialize high port */
2030 buf[0] = 0x82; /* command "set data bits high byte" */
2031 buf[1] = high_output;
2032 buf[2] = high_direction;
2033 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2035 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2037 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2038 return ERROR_JTAG_INIT_FAILED;
2044 void olimex_jtag_blink(void)
2046 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2047 * ACBUS3 is bit 3 of the GPIOH port
2049 if (high_output & 0x08)
2051 /* set port pin high */
2052 high_output &= 0x07;
2056 /* set port pin low */
2057 high_output |= 0x08;
2061 BUFFER_ADD = high_output;
2062 BUFFER_ADD = high_direction;
2065 void turtle_jtag_blink(void)
2068 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2070 if (high_output & 0x08)
2080 BUFFER_ADD = high_output;
2081 BUFFER_ADD = high_direction;
2084 int ft2232_quit(void)
2086 #if BUILD_FT2232_FTD2XX == 1
2089 status = FT_Close(ftdih);
2090 #elif BUILD_FT2232_LIBFTDI == 1
2091 ftdi_disable_bitbang(&ftdic);
2093 ftdi_usb_close(&ftdic);
2095 ftdi_deinit(&ftdic);
2098 free(ft2232_buffer);
2099 ft2232_buffer = NULL;
2104 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2108 ft2232_device_desc = strdup(args[0]);
2112 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2118 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2122 ft2232_serial = strdup(args[0]);
2126 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2132 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2137 ft2232_layout = malloc(strlen(args[0]) + 1);
2138 strcpy(ft2232_layout, args[0]);
2143 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2147 if (argc > MAX_USB_IDS*2) {
2148 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2149 "(maximum is %d pairs)", MAX_USB_IDS);
2150 argc = MAX_USB_IDS*2;
2152 if (argc < 2 || (argc & 1))
2154 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2159 for (i = 0; i+1 < argc; i += 2) {
2160 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2161 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2164 * Explicitly terminate, in case there are multiples instances of
2167 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2172 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2176 ft2232_latency = atoi(args[0]);
2180 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");