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 */
302 int ft2232_register_commands(struct command_context_s *cmd_ctx)
304 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
305 COMMAND_CONFIG, NULL);
306 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
307 COMMAND_CONFIG, NULL);
308 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
309 COMMAND_CONFIG, NULL);
310 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
311 COMMAND_CONFIG, NULL);
312 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
313 COMMAND_CONFIG, NULL);
317 void ft2232_end_state(enum tap_state state)
319 if (tap_move_map[state] != -1)
323 LOG_ERROR("BUG: %i is not a valid end state", state);
328 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
330 int num_bytes = ((scan_size + 7) / 8);
331 int bits_left = scan_size;
334 while(num_bytes-- > 1)
336 buffer[cur_byte] = BUFFER_READ;
341 buffer[cur_byte] = 0x0;
345 buffer[cur_byte] = BUFFER_READ >> 1;
348 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
352 void ft2232_debug_dump_buffer(void)
358 for (i = 0; i < ft2232_buffer_size; i++)
360 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
363 LOG_DEBUG("%s", line);
369 LOG_DEBUG("%s", line);
372 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
382 #ifdef _DEBUG_USB_IO_
383 struct timeval start, inter, inter2, end;
384 struct timeval d_inter, d_inter2, d_end;
387 #ifdef _DEBUG_USB_COMMS_
388 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
389 ft2232_debug_dump_buffer();
392 #ifdef _DEBUG_USB_IO_
393 gettimeofday(&start, NULL);
396 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
398 LOG_ERROR("couldn't write MPSSE commands to FT2232");
402 #ifdef _DEBUG_USB_IO_
403 gettimeofday(&inter, NULL);
406 if (ft2232_expect_read)
409 ft2232_buffer_size = 0;
411 #ifdef _DEBUG_USB_IO_
412 gettimeofday(&inter2, NULL);
415 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
417 LOG_ERROR("couldn't read from FT2232");
421 #ifdef _DEBUG_USB_IO_
422 gettimeofday(&end, NULL);
424 timeval_subtract(&d_inter, &inter, &start);
425 timeval_subtract(&d_inter2, &inter2, &start);
426 timeval_subtract(&d_end, &end, &start);
428 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);
432 ft2232_buffer_size = bytes_read;
434 if (ft2232_expect_read != ft2232_buffer_size)
436 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
437 ft2232_debug_dump_buffer();
442 #ifdef _DEBUG_USB_COMMS_
443 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
444 ft2232_debug_dump_buffer();
448 ft2232_expect_read = 0;
449 ft2232_read_pointer = 0;
451 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
452 * that wasn't handled by a caller-provided error handler
462 type = jtag_scan_type(cmd->cmd.scan);
463 if (type != SCAN_OUT)
465 scan_size = jtag_scan_size(cmd->cmd.scan);
466 buffer = calloc(CEIL(scan_size, 8), 1);
467 ft2232_read_scan(type, buffer, scan_size);
468 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
469 retval = ERROR_JTAG_QUEUE_FAILED;
479 ft2232_buffer_size = 0;
484 void ft2232_add_pathmove(pathmove_command_t *cmd)
486 int num_states = cmd->num_states;
495 int num_states_batch = num_states > 7 ? 7 : num_states;
498 /* command "Clock Data to TMS/CS Pin (no Read)" */
500 /* number of states remaining */
501 BUFFER_ADD = num_states_batch - 1;
503 while (num_states_batch--)
505 if (tap_transitions[cur_state].low == cmd->path[state_count])
506 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
507 else if (tap_transitions[cur_state].high == cmd->path[state_count])
508 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
511 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
515 cur_state = cmd->path[state_count];
520 BUFFER_ADD = tms_byte;
523 end_state = cur_state;
526 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
528 int num_bytes = (scan_size + 7) / 8;
529 int bits_left = scan_size;
533 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
535 /* command "Clock Data to TMS/CS Pin (no Read)" */
542 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
547 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
550 /* LOG_DEBUG("added TMS scan (no read)"); */
553 /* add command for complete bytes */
554 while (num_bytes > 1)
559 /* Clock Data Bytes In and Out LSB First */
561 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
563 else if (type == SCAN_OUT)
565 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
567 /* LOG_DEBUG("added TDI bytes (o)"); */
569 else if (type == SCAN_IN)
571 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
573 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
575 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
576 num_bytes -= thisrun_bytes;
577 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
578 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
581 /* add complete bytes */
582 while(thisrun_bytes-- > 0)
584 BUFFER_ADD = buffer[cur_byte];
589 else /* (type == SCAN_IN) */
591 bits_left -= 8 * (thisrun_bytes);
595 /* the most signifcant bit is scanned during TAP movement */
597 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
601 /* process remaining bits but the last one */
606 /* Clock Data Bits In and Out LSB First */
608 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
610 else if (type == SCAN_OUT)
612 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
614 /* LOG_DEBUG("added TDI bits (o)"); */
616 else if (type == SCAN_IN)
618 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
620 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
622 BUFFER_ADD = bits_left - 2;
624 BUFFER_ADD = buffer[cur_byte];
627 if ((ir_scan && (end_state == TAP_SI)) ||
628 (!ir_scan && (end_state == TAP_SD)))
632 /* Clock Data Bits In and Out LSB First */
634 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
636 else if (type == SCAN_OUT)
638 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
640 /* LOG_DEBUG("added TDI bits (o)"); */
642 else if (type == SCAN_IN)
644 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
646 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
649 BUFFER_ADD = last_bit;
653 /* move from Shift-IR/DR to end state */
654 if (type != SCAN_OUT)
656 /* Clock Data to TMS/CS Pin with Read */
658 /* LOG_DEBUG("added TMS scan (read)"); */
662 /* Clock Data to TMS/CS Pin (no Read) */
664 /* LOG_DEBUG("added TMS scan (no read)"); */
667 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
668 cur_state = end_state;
672 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
674 int num_bytes = (scan_size + 7) / 8;
675 int bits_left = scan_size;
678 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
679 u8 *receive_pointer = receive_buffer;
683 int thisrun_read = 0;
687 LOG_ERROR("BUG: large IR scans are not supported");
691 if (cur_state != TAP_SD)
693 /* command "Clock Data to TMS/CS Pin (no Read)" */
698 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
702 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
704 LOG_ERROR("couldn't write MPSSE commands to FT2232");
707 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
708 ft2232_buffer_size = 0;
710 /* add command for complete bytes */
711 while (num_bytes > 1)
717 /* Clock Data Bytes In and Out LSB First */
719 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
721 else if (type == SCAN_OUT)
723 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
725 /* LOG_DEBUG("added TDI bytes (o)"); */
727 else if (type == SCAN_IN)
729 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
731 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
733 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
734 thisrun_read = thisrun_bytes;
735 num_bytes -= thisrun_bytes;
736 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
737 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
740 /* add complete bytes */
741 while(thisrun_bytes-- > 0)
743 BUFFER_ADD = buffer[cur_byte];
748 else /* (type == SCAN_IN) */
750 bits_left -= 8 * (thisrun_bytes);
753 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
755 LOG_ERROR("couldn't write MPSSE commands to FT2232");
758 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
759 ft2232_buffer_size = 0;
761 if (type != SCAN_OUT)
763 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
765 LOG_ERROR("couldn't read from FT2232");
768 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
769 receive_pointer += bytes_read;
775 /* the most signifcant bit is scanned during TAP movement */
777 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
781 /* process remaining bits but the last one */
786 /* Clock Data Bits In and Out LSB First */
788 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
790 else if (type == SCAN_OUT)
792 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
794 /* LOG_DEBUG("added TDI bits (o)"); */
796 else if (type == SCAN_IN)
798 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
800 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
802 BUFFER_ADD = bits_left - 2;
804 BUFFER_ADD = buffer[cur_byte];
806 if (type != SCAN_OUT)
810 if (end_state == TAP_SD)
814 /* Clock Data Bits In and Out LSB First */
816 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
818 else if (type == SCAN_OUT)
820 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
822 /* LOG_DEBUG("added TDI bits (o)"); */
824 else if (type == SCAN_IN)
826 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
828 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
831 BUFFER_ADD = last_bit;
835 /* move from Shift-IR/DR to end state */
836 if (type != SCAN_OUT)
838 /* Clock Data to TMS/CS Pin with Read */
840 /* LOG_DEBUG("added TMS scan (read)"); */
844 /* Clock Data to TMS/CS Pin (no Read) */
846 /* LOG_DEBUG("added TMS scan (no read)"); */
849 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
850 cur_state = end_state;
853 if (type != SCAN_OUT)
856 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
858 LOG_ERROR("couldn't write MPSSE commands to FT2232");
861 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
862 ft2232_buffer_size = 0;
864 if (type != SCAN_OUT)
866 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
868 LOG_ERROR("couldn't read from FT2232");
871 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
872 receive_pointer += bytes_read;
878 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
880 int predicted_size = 3;
881 int num_bytes = (scan_size - 1) / 8;
883 if (cur_state != TAP_SD)
886 if (type == SCAN_IN) /* only from device to host */
889 predicted_size += (CEIL(num_bytes, 65536)) * 3;
890 /* remaining bits - 1 (up to 7) */
891 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
893 else /* host to device, or bidirectional */
896 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
897 /* remaining bits -1 (up to 7) */
898 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
901 return predicted_size;
904 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
906 int predicted_size = 0;
908 if (type != SCAN_OUT)
911 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
912 /* remaining bits - 1 */
913 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
914 /* last bit (from TMS scan) */
918 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
920 return predicted_size;
923 void usbjtag_reset(int trst, int srst)
927 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
928 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
930 low_output &= ~nTRST; /* switch output low */
934 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
935 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
937 low_output |= nTRST; /* switch output high */
942 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
943 low_output &= ~nSRST; /* switch output low */
945 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
949 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
950 low_output |= nSRST; /* switch output high */
952 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
955 /* command "set data bits low byte" */
957 BUFFER_ADD = low_output;
958 BUFFER_ADD = low_direction;
962 void jtagkey_reset(int trst, int srst)
966 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
967 high_output &= ~nTRSTnOE;
969 high_output &= ~nTRST;
973 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
974 high_output |= nTRSTnOE;
976 high_output |= nTRST;
981 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
982 high_output &= ~nSRST;
984 high_output &= ~nSRSTnOE;
988 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
989 high_output |= nSRST;
991 high_output |= nSRSTnOE;
994 /* command "set data bits high byte" */
996 BUFFER_ADD = high_output;
997 BUFFER_ADD = high_direction;
998 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1001 void olimex_jtag_reset(int trst, int srst)
1005 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1006 high_output &= ~nTRSTnOE;
1008 high_output &= ~nTRST;
1012 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1013 high_output |= nTRSTnOE;
1015 high_output |= nTRST;
1020 high_output |= nSRST;
1024 high_output &= ~nSRST;
1027 /* command "set data bits high byte" */
1029 BUFFER_ADD = high_output;
1030 BUFFER_ADD = high_direction;
1031 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1034 void flyswatter_reset(int trst, int srst)
1038 low_output &= ~nTRST;
1042 low_output |= nTRST;
1047 low_output |= nSRST;
1051 low_output &= ~nSRST;
1054 /* command "set data bits low byte" */
1056 BUFFER_ADD = low_output;
1057 BUFFER_ADD = low_direction;
1058 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1061 void turtle_reset(int trst, int srst)
1067 low_output |= nSRST;
1071 low_output &= ~nSRST;
1074 /* command "set data bits low byte" */
1076 BUFFER_ADD = low_output;
1077 BUFFER_ADD = low_direction;
1078 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1081 void comstick_reset(int trst, int srst)
1085 high_output &= ~nTRST;
1089 high_output |= nTRST;
1094 high_output &= ~nSRST;
1098 high_output |= nSRST;
1101 /* command "set data bits high byte" */
1103 BUFFER_ADD = high_output;
1104 BUFFER_ADD = high_direction;
1105 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1108 void stm32stick_reset(int trst, int srst)
1112 high_output &= ~nTRST;
1116 high_output |= nTRST;
1121 low_output &= ~nSRST;
1125 low_output |= nSRST;
1128 /* command "set data bits low byte" */
1130 BUFFER_ADD = low_output;
1131 BUFFER_ADD = low_direction;
1133 /* command "set data bits high byte" */
1135 BUFFER_ADD = high_output;
1136 BUFFER_ADD = high_direction;
1137 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1140 int ft2232_execute_queue()
1142 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1143 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1145 int scan_size; /* size of IR or DR scan */
1146 enum scan_type type;
1148 int predicted_size = 0;
1149 int require_send = 0;
1152 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1153 * that wasn't handled by a caller-provided error handler
1157 ft2232_buffer_size = 0;
1158 ft2232_expect_read = 0;
1160 /* blink, if the current layout has that feature */
1168 case JTAG_END_STATE:
1169 if (cmd->cmd.end_state->end_state != -1)
1170 ft2232_end_state(cmd->cmd.end_state->end_state);
1173 /* only send the maximum buffer size that FT2232C can handle */
1175 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1177 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1178 retval = ERROR_JTAG_QUEUE_FAILED;
1183 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1185 cur_state = TAP_TLR;
1187 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1190 #ifdef _DEBUG_JTAG_IO_
1191 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1195 /* only send the maximum buffer size that FT2232C can handle */
1197 if (cur_state != TAP_RTI)
1198 predicted_size += 3;
1199 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1200 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1201 predicted_size += 3;
1202 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1203 predicted_size += 3;
1204 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1206 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1207 retval = ERROR_JTAG_QUEUE_FAILED;
1211 if (cur_state != TAP_RTI)
1213 /* command "Clock Data to TMS/CS Pin (no Read)" */
1218 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1219 cur_state = TAP_RTI;
1222 i = cmd->cmd.runtest->num_cycles;
1225 /* command "Clock Data to TMS/CS Pin (no Read)" */
1228 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1231 cur_state = TAP_RTI;
1232 i -= (i > 7) ? 7 : i;
1233 /* LOG_DEBUG("added TMS scan (no read)"); */
1235 if (cmd->cmd.runtest->end_state != -1)
1236 ft2232_end_state(cmd->cmd.runtest->end_state);
1237 if (cur_state != end_state)
1239 /* command "Clock Data to TMS/CS Pin (no Read)" */
1244 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1245 cur_state = end_state;
1246 /* LOG_DEBUG("added TMS scan (no read)"); */
1249 #ifdef _DEBUG_JTAG_IO_
1250 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1253 case JTAG_STATEMOVE:
1254 /* only send the maximum buffer size that FT2232C can handle */
1256 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1258 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1259 retval = ERROR_JTAG_QUEUE_FAILED;
1263 if (cmd->cmd.statemove->end_state != -1)
1264 ft2232_end_state(cmd->cmd.statemove->end_state);
1265 /* command "Clock Data to TMS/CS Pin (no Read)" */
1270 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1271 /* LOG_DEBUG("added TMS scan (no read)"); */
1272 cur_state = end_state;
1274 #ifdef _DEBUG_JTAG_IO_
1275 LOG_DEBUG("statemove: %i", end_state);
1279 /* only send the maximum buffer size that FT2232C can handle */
1280 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1281 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1283 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1284 retval = ERROR_JTAG_QUEUE_FAILED;
1288 ft2232_add_pathmove(cmd->cmd.pathmove);
1290 #ifdef _DEBUG_JTAG_IO_
1291 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1295 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1296 type = jtag_scan_type(cmd->cmd.scan);
1297 predicted_size = ft2232_predict_scan_out(scan_size, type);
1298 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1300 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1301 /* unsent commands before this */
1302 if (first_unsent != cmd)
1303 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1304 retval = ERROR_JTAG_QUEUE_FAILED;
1306 /* current command */
1307 if (cmd->cmd.scan->end_state != -1)
1308 ft2232_end_state(cmd->cmd.scan->end_state);
1309 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1311 first_unsent = cmd->next;
1316 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1318 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1319 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1320 retval = ERROR_JTAG_QUEUE_FAILED;
1324 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1325 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1326 if (cmd->cmd.scan->end_state != -1)
1327 ft2232_end_state(cmd->cmd.scan->end_state);
1328 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1332 #ifdef _DEBUG_JTAG_IO_
1333 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1337 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1338 retval = ERROR_JTAG_QUEUE_FAILED;
1339 first_unsent = cmd->next;
1340 jtag_sleep(cmd->cmd.sleep->us);
1341 #ifdef _DEBUG_JTAG_IO_
1342 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1346 LOG_ERROR("BUG: unknown JTAG command type encountered");
1352 if (require_send > 0)
1353 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1354 retval = ERROR_JTAG_QUEUE_FAILED;
1359 #if BUILD_FT2232_FTD2XX == 1
1360 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1363 DWORD openex_flags = 0;
1364 char *openex_string = NULL;
1367 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1368 ft2232_layout, vid, pid);
1371 /* Add non-standard Vid/Pid to the linux driver */
1372 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1374 LOG_WARNING("couldn't add %4.4x:%4.4x",
1379 if (ft2232_device_desc && ft2232_serial)
1381 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1382 ft2232_device_desc = NULL;
1385 if (ft2232_device_desc)
1387 openex_string = ft2232_device_desc;
1388 openex_flags = FT_OPEN_BY_DESCRIPTION;
1390 else if (ft2232_serial)
1392 openex_string = ft2232_serial;
1393 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1397 LOG_ERROR("neither device description nor serial number specified");
1398 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1400 return ERROR_JTAG_INIT_FAILED;
1403 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1408 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1411 return ERROR_JTAG_INIT_FAILED;
1413 LOG_ERROR("unable to open ftdi device: %lu", status);
1414 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1415 if (status == FT_OK)
1417 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1420 for (i = 0; i < num_devices; i++)
1421 desc_array[i] = malloc(64);
1422 desc_array[num_devices] = NULL;
1424 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1426 if (status == FT_OK)
1428 LOG_ERROR("ListDevices: %lu\n", num_devices);
1429 for (i = 0; i < num_devices; i++)
1430 LOG_ERROR("%i: %s", i, desc_array[i]);
1433 for (i = 0; i < num_devices; i++)
1434 free(desc_array[i]);
1439 LOG_ERROR("ListDevices: NONE\n");
1441 return ERROR_JTAG_INIT_FAILED;
1444 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1446 LOG_ERROR("unable to set latency timer: %lu", status);
1447 return ERROR_JTAG_INIT_FAILED;
1450 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1452 LOG_ERROR("unable to get latency timer: %lu", status);
1453 return ERROR_JTAG_INIT_FAILED;
1457 LOG_DEBUG("current latency timer: %i", latency_timer);
1460 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1462 LOG_ERROR("unable to set timeouts: %lu", status);
1463 return ERROR_JTAG_INIT_FAILED;
1466 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1468 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1469 return ERROR_JTAG_INIT_FAILED;
1475 static int ft2232_purge_ftd2xx(void)
1479 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1481 LOG_ERROR("error purging ftd2xx device: %lu", status);
1482 return ERROR_JTAG_INIT_FAILED;
1487 #endif /* BUILD_FT2232_FTD2XX == 1 */
1489 #if BUILD_FT2232_LIBFTDI == 1
1490 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1494 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1495 ft2232_layout, vid, pid);
1497 if (ftdi_init(&ftdic) < 0)
1498 return ERROR_JTAG_INIT_FAILED;
1500 /* context, vendor id, product id */
1501 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1502 ft2232_serial) < 0) {
1504 LOG_WARNING("unable to open ftdi device (trying more): %s",
1507 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1509 return ERROR_JTAG_INIT_FAILED;
1512 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1514 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1515 return ERROR_JTAG_INIT_FAILED;
1518 if (ftdi_usb_reset(&ftdic) < 0)
1520 LOG_ERROR("unable to reset ftdi device");
1521 return ERROR_JTAG_INIT_FAILED;
1524 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1526 LOG_ERROR("unable to set latency timer");
1527 return ERROR_JTAG_INIT_FAILED;
1530 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1532 LOG_ERROR("unable to get latency timer");
1533 return ERROR_JTAG_INIT_FAILED;
1537 LOG_DEBUG("current latency timer: %i", latency_timer);
1540 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1545 static int ft2232_purge_libftdi(void)
1547 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1549 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1550 return ERROR_JTAG_INIT_FAILED;
1555 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1557 int ft2232_init(void)
1562 ft2232_layout_t *cur_layout = ft2232_layouts;
1565 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1567 ft2232_layout = "usbjtag";
1568 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1571 while (cur_layout->name)
1573 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1575 layout = cur_layout;
1583 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1584 return ERROR_JTAG_INIT_FAILED;
1587 for (i = 0; 1; i++) {
1589 * "more indicates that there are more IDs to try, so we should
1590 * not print an error for an ID mismatch (but for anything
1593 * try_more indicates that the error code returned indicates an
1594 * ID mismatch (and nothing else) and that we should proceeed
1595 * with the next ID pair.
1597 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1600 #if BUILD_FT2232_FTD2XX == 1
1601 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1603 #elif BUILD_FT2232_LIBFTDI == 1
1604 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1609 if (!more || !try_more)
1613 ft2232_buffer_size = 0;
1614 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1616 if (layout->init() != ERROR_OK)
1617 return ERROR_JTAG_INIT_FAILED;
1619 ft2232_speed(jtag_speed);
1621 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1622 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1624 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1625 return ERROR_JTAG_INIT_FAILED;
1628 #if BUILD_FT2232_FTD2XX == 1
1629 return ft2232_purge_ftd2xx();
1630 #elif BUILD_FT2232_LIBFTDI == 1
1631 return ft2232_purge_libftdi();
1637 int usbjtag_init(void)
1643 low_direction = 0x0b;
1645 if (strcmp(ft2232_layout, "usbjtag") == 0)
1652 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1659 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1666 low_direction = 0x8b;
1670 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1671 return ERROR_JTAG_INIT_FAILED;
1674 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1676 low_direction &= ~nTRSTnOE; /* nTRST input */
1677 low_output &= ~nTRST; /* nTRST = 0 */
1681 low_direction |= nTRSTnOE; /* nTRST output */
1682 low_output |= nTRST; /* nTRST = 1 */
1685 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1687 low_direction |= nSRSTnOE; /* nSRST output */
1688 low_output |= nSRST; /* nSRST = 1 */
1692 low_direction &= ~nSRSTnOE; /* nSRST input */
1693 low_output &= ~nSRST; /* nSRST = 0 */
1696 /* initialize low byte for jtag */
1697 buf[0] = 0x80; /* command "set data bits low byte" */
1698 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1699 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1700 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1702 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1704 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1705 return ERROR_JTAG_INIT_FAILED;
1711 int jtagkey_init(void)
1717 low_direction = 0x1b;
1719 /* initialize low byte for jtag */
1720 buf[0] = 0x80; /* command "set data bits low byte" */
1721 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1722 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1723 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1725 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1727 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1728 return ERROR_JTAG_INIT_FAILED;
1731 if (strcmp(layout->name, "jtagkey") == 0)
1738 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1739 (strcmp(layout->name, "oocdlink") == 0))
1748 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1753 high_direction = 0x0f;
1755 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1757 high_output |= nTRSTnOE;
1758 high_output &= ~nTRST;
1762 high_output &= ~nTRSTnOE;
1763 high_output |= nTRST;
1766 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1768 high_output &= ~nSRSTnOE;
1769 high_output |= nSRST;
1773 high_output |= nSRSTnOE;
1774 high_output &= ~nSRST;
1777 /* initialize high port */
1778 buf[0] = 0x82; /* command "set data bits high byte" */
1779 buf[1] = high_output; /* value */
1780 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1781 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1783 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1785 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1786 return ERROR_JTAG_INIT_FAILED;
1792 int olimex_jtag_init(void)
1798 low_direction = 0x1b;
1800 /* initialize low byte for jtag */
1801 buf[0] = 0x80; /* command "set data bits low byte" */
1802 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1803 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1804 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1806 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1808 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1809 return ERROR_JTAG_INIT_FAILED;
1815 nSRSTnOE = 0x00; /* no output enable for nSRST */
1818 high_direction = 0x0f;
1820 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1822 high_output |= nTRSTnOE;
1823 high_output &= ~nTRST;
1827 high_output &= ~nTRSTnOE;
1828 high_output |= nTRST;
1831 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1833 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1837 high_output &= ~nSRST;
1840 /* turn red LED on */
1841 high_output |= 0x08;
1843 /* initialize high port */
1844 buf[0] = 0x82; /* command "set data bits high byte" */
1845 buf[1] = high_output; /* value */
1846 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1847 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1849 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1851 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1852 return ERROR_JTAG_INIT_FAILED;
1858 int flyswatter_init(void)
1864 low_direction = 0xfb;
1866 /* initialize low byte for jtag */
1867 buf[0] = 0x80; /* command "set data bits low byte" */
1868 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1869 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1870 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1872 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1874 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1875 return ERROR_JTAG_INIT_FAILED;
1879 nTRSTnOE = 0x0; /* not output enable for nTRST */
1881 nSRSTnOE = 0x00; /* no output enable for nSRST */
1884 high_direction = 0x0c;
1886 /* turn red LED1 on, LED2 off */
1887 high_output |= 0x08;
1889 /* initialize high port */
1890 buf[0] = 0x82; /* command "set data bits high byte" */
1891 buf[1] = high_output; /* value */
1892 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1893 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1895 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1897 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1898 return ERROR_JTAG_INIT_FAILED;
1904 int turtle_init(void)
1910 low_direction = 0x5b;
1912 /* initialize low byte for jtag */
1913 buf[0] = 0x80; /* command "set data bits low byte" */
1914 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1915 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1916 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1918 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1920 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1921 return ERROR_JTAG_INIT_FAILED;
1927 high_direction = 0x0C;
1929 /* initialize high port */
1930 buf[0] = 0x82; /* command "set data bits high byte" */
1931 buf[1] = high_output;
1932 buf[2] = high_direction;
1933 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1935 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1937 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1938 return ERROR_JTAG_INIT_FAILED;
1944 int comstick_init(void)
1950 low_direction = 0x0b;
1952 /* initialize low byte for jtag */
1953 buf[0] = 0x80; /* command "set data bits low byte" */
1954 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1955 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1956 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1958 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1960 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1961 return ERROR_JTAG_INIT_FAILED;
1965 nTRSTnOE = 0x00; /* no output enable for nTRST */
1967 nSRSTnOE = 0x00; /* no output enable for nSRST */
1970 high_direction = 0x03;
1972 /* initialize high port */
1973 buf[0] = 0x82; /* command "set data bits high byte" */
1974 buf[1] = high_output;
1975 buf[2] = high_direction;
1976 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1978 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1980 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1981 return ERROR_JTAG_INIT_FAILED;
1987 int stm32stick_init(void)
1993 low_direction = 0x8b;
1995 /* initialize low byte for jtag */
1996 buf[0] = 0x80; /* command "set data bits low byte" */
1997 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1998 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1999 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2001 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2003 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2004 return ERROR_JTAG_INIT_FAILED;
2008 nTRSTnOE = 0x00; /* no output enable for nTRST */
2010 nSRSTnOE = 0x00; /* no output enable for nSRST */
2013 high_direction = 0x03;
2015 /* initialize high port */
2016 buf[0] = 0x82; /* command "set data bits high byte" */
2017 buf[1] = high_output;
2018 buf[2] = high_direction;
2019 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2021 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2023 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2024 return ERROR_JTAG_INIT_FAILED;
2030 void olimex_jtag_blink(void)
2032 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2033 * ACBUS3 is bit 3 of the GPIOH port
2035 if (high_output & 0x08)
2037 /* set port pin high */
2038 high_output &= 0x07;
2042 /* set port pin low */
2043 high_output |= 0x08;
2047 BUFFER_ADD = high_output;
2048 BUFFER_ADD = high_direction;
2051 void turtle_jtag_blink(void)
2054 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2056 if (high_output & 0x08)
2066 BUFFER_ADD = high_output;
2067 BUFFER_ADD = high_direction;
2070 int ft2232_quit(void)
2072 #if BUILD_FT2232_FTD2XX == 1
2075 status = FT_Close(ftdih);
2076 #elif BUILD_FT2232_LIBFTDI == 1
2077 ftdi_disable_bitbang(&ftdic);
2079 ftdi_usb_close(&ftdic);
2081 ftdi_deinit(&ftdic);
2084 free(ft2232_buffer);
2085 ft2232_buffer = NULL;
2090 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2094 ft2232_device_desc = strdup(args[0]);
2098 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2104 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2108 ft2232_serial = strdup(args[0]);
2112 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2118 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2123 ft2232_layout = malloc(strlen(args[0]) + 1);
2124 strcpy(ft2232_layout, args[0]);
2129 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2133 if (argc > MAX_USB_IDS*2) {
2134 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2135 "(maximum is %d pairs)", MAX_USB_IDS);
2136 argc = MAX_USB_IDS*2;
2138 if (argc < 2 || (argc & 1))
2140 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2145 for (i = 0; i+1 < argc; i += 2) {
2146 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2147 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2150 * Explicitly terminate, in case there are multiples instances of
2153 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2158 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2162 ft2232_latency = atoi(args[0]);
2166 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");