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);
105 int axm0432_jtag_init(void);
107 /* reset procedures for supported layouts */
108 void usbjtag_reset(int trst, int srst);
109 void jtagkey_reset(int trst, int srst);
110 void olimex_jtag_reset(int trst, int srst);
111 void flyswatter_reset(int trst, int srst);
112 void turtle_reset(int trst, int srst);
113 void comstick_reset(int trst, int srst);
114 void stm32stick_reset(int trst, int srst);
115 void axm0432_jtag_reset(int trst, int srst);
117 /* blink procedures for layouts that support a blinking led */
118 void olimex_jtag_blink(void);
119 void turtle_jtag_blink(void);
121 ft2232_layout_t ft2232_layouts[] =
123 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
124 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
125 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
126 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
127 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
128 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
129 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
130 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
131 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
132 {"comstick", comstick_init, comstick_reset, NULL},
133 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
134 {"axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL},
138 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
140 static ft2232_layout_t *layout;
141 static u8 low_output = 0x0;
142 static u8 low_direction = 0x0;
143 static u8 high_output = 0x0;
144 static u8 high_direction = 0x0;
146 #if BUILD_FT2232_FTD2XX == 1
147 static FT_HANDLE ftdih = NULL;
148 #elif BUILD_FT2232_LIBFTDI == 1
149 static struct ftdi_context ftdic;
152 static u8 *ft2232_buffer = NULL;
153 static int ft2232_buffer_size = 0;
154 static int ft2232_read_pointer = 0;
155 static int ft2232_expect_read = 0;
156 #define FT2232_BUFFER_SIZE 131072
157 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
158 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
160 jtag_interface_t ft2232_interface =
163 .execute_queue = ft2232_execute_queue,
164 .speed = ft2232_speed,
165 .speed_div = ft2232_speed_div,
167 .register_commands = ft2232_register_commands,
172 int ft2232_write(u8 *buf, int size, u32* bytes_written)
174 #if BUILD_FT2232_FTD2XX == 1
176 DWORD dw_bytes_written;
177 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
179 *bytes_written = dw_bytes_written;
180 LOG_ERROR("FT_Write returned: %lu", status);
181 return ERROR_JTAG_DEVICE_ERROR;
185 *bytes_written = dw_bytes_written;
188 #elif BUILD_FT2232_LIBFTDI == 1
190 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
193 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
194 return ERROR_JTAG_DEVICE_ERROR;
198 *bytes_written = retval;
204 int ft2232_read(u8* buf, int size, u32* bytes_read)
206 #if BUILD_FT2232_FTD2XX == 1
212 while ((*bytes_read < size) && timeout--)
214 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
215 *bytes_read, &dw_bytes_read)) != FT_OK)
218 LOG_ERROR("FT_Read returned: %lu", status);
219 return ERROR_JTAG_DEVICE_ERROR;
221 *bytes_read += dw_bytes_read;
223 #elif BUILD_FT2232_LIBFTDI == 1
228 while ((*bytes_read < size) && timeout--)
230 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
233 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
234 return ERROR_JTAG_DEVICE_ERROR;
236 *bytes_read += retval;
240 if (*bytes_read < size)
242 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
243 return ERROR_JTAG_DEVICE_ERROR;
249 int ft2232_speed(int speed)
255 buf[0] = 0x86; /* command "set divisor" */
256 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
257 buf[2] = (speed >> 8) & 0xff; /* valueH */
259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
260 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
262 LOG_ERROR("couldn't set FT2232 TCK speed");
269 int ft2232_speed_div(int speed, int *khz)
271 /* Take a look in the FT2232 manual,
272 * AN2232C-01 Command Processor for
273 * MPSSE and MCU Host Bus. Chapter 3.8 */
275 *khz = 6000 / (1+speed);
280 int ft2232_khz(int khz, int *jtag_speed)
284 LOG_ERROR("RCLK not supported");
287 /* Take a look in the FT2232 manual,
288 * AN2232C-01 Command Processor for
289 * MPSSE and MCU Host Bus. Chapter 3.8
291 * We will calc here with a multiplier
292 * of 10 for better rounding later. */
294 /* Calc speed, (6000 / khz) - 1 */
295 /* Use 65000 for better rounding */
296 *jtag_speed = (60000 / khz) - 10;
298 /* Add 0.9 for rounding */
301 /* Calc real speed */
302 *jtag_speed = *jtag_speed / 10;
304 /* Check if speed is greater than 0 */
310 /* Check max value */
311 if (*jtag_speed > 0xFFFF)
313 *jtag_speed = 0xFFFF;
319 int ft2232_register_commands(struct command_context_s *cmd_ctx)
321 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
322 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
323 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
324 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
325 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
326 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
327 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
328 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
329 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
330 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
334 void ft2232_end_state(enum tap_state state)
336 if (tap_move_map[state] != -1)
340 LOG_ERROR("BUG: %i is not a valid end state", state);
345 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
347 int num_bytes = ((scan_size + 7) / 8);
348 int bits_left = scan_size;
351 while(num_bytes-- > 1)
353 buffer[cur_byte] = BUFFER_READ;
358 buffer[cur_byte] = 0x0;
362 buffer[cur_byte] = BUFFER_READ >> 1;
365 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
369 void ft2232_debug_dump_buffer(void)
375 for (i = 0; i < ft2232_buffer_size; i++)
377 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
380 LOG_DEBUG("%s", line);
386 LOG_DEBUG("%s", line);
389 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
399 #ifdef _DEBUG_USB_IO_
400 struct timeval start, inter, inter2, end;
401 struct timeval d_inter, d_inter2, d_end;
404 #ifdef _DEBUG_USB_COMMS_
405 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
406 ft2232_debug_dump_buffer();
409 #ifdef _DEBUG_USB_IO_
410 gettimeofday(&start, NULL);
413 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
415 LOG_ERROR("couldn't write MPSSE commands to FT2232");
419 #ifdef _DEBUG_USB_IO_
420 gettimeofday(&inter, NULL);
423 if (ft2232_expect_read)
426 ft2232_buffer_size = 0;
428 #ifdef _DEBUG_USB_IO_
429 gettimeofday(&inter2, NULL);
432 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
434 LOG_ERROR("couldn't read from FT2232");
438 #ifdef _DEBUG_USB_IO_
439 gettimeofday(&end, NULL);
441 timeval_subtract(&d_inter, &inter, &start);
442 timeval_subtract(&d_inter2, &inter2, &start);
443 timeval_subtract(&d_end, &end, &start);
445 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);
449 ft2232_buffer_size = bytes_read;
451 if (ft2232_expect_read != ft2232_buffer_size)
453 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
454 ft2232_debug_dump_buffer();
459 #ifdef _DEBUG_USB_COMMS_
460 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
461 ft2232_debug_dump_buffer();
465 ft2232_expect_read = 0;
466 ft2232_read_pointer = 0;
468 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
469 * that wasn't handled by a caller-provided error handler
479 type = jtag_scan_type(cmd->cmd.scan);
480 if (type != SCAN_OUT)
482 scan_size = jtag_scan_size(cmd->cmd.scan);
483 buffer = calloc(CEIL(scan_size, 8), 1);
484 ft2232_read_scan(type, buffer, scan_size);
485 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
486 retval = ERROR_JTAG_QUEUE_FAILED;
496 ft2232_buffer_size = 0;
501 void ft2232_add_pathmove(pathmove_command_t *cmd)
503 int num_states = cmd->num_states;
512 int num_states_batch = num_states > 7 ? 7 : num_states;
515 /* command "Clock Data to TMS/CS Pin (no Read)" */
517 /* number of states remaining */
518 BUFFER_ADD = num_states_batch - 1;
520 while (num_states_batch--)
522 if (tap_transitions[cur_state].low == cmd->path[state_count])
523 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
524 else if (tap_transitions[cur_state].high == cmd->path[state_count])
525 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
528 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
532 cur_state = cmd->path[state_count];
537 BUFFER_ADD = tms_byte;
540 end_state = cur_state;
543 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
545 int num_bytes = (scan_size + 7) / 8;
546 int bits_left = scan_size;
550 if (!((!ir_scan && (cur_state == TAP_DRSHIFT)) || (ir_scan && (cur_state == TAP_IRSHIFT))))
552 /* command "Clock Data to TMS/CS Pin (no Read)" */
559 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IRSHIFT);
560 cur_state = TAP_IRSHIFT;
564 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
565 cur_state = TAP_DRSHIFT;
567 /* LOG_DEBUG("added TMS scan (no read)"); */
570 /* add command for complete bytes */
571 while (num_bytes > 1)
576 /* Clock Data Bytes In and Out LSB First */
578 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
580 else if (type == SCAN_OUT)
582 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
584 /* LOG_DEBUG("added TDI bytes (o)"); */
586 else if (type == SCAN_IN)
588 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
590 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
592 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
593 num_bytes -= thisrun_bytes;
594 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
595 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
598 /* add complete bytes */
599 while(thisrun_bytes-- > 0)
601 BUFFER_ADD = buffer[cur_byte];
606 else /* (type == SCAN_IN) */
608 bits_left -= 8 * (thisrun_bytes);
612 /* the most signifcant bit is scanned during TAP movement */
614 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
618 /* process remaining bits but the last one */
623 /* Clock Data Bits In and Out LSB First */
625 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
627 else if (type == SCAN_OUT)
629 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
631 /* LOG_DEBUG("added TDI bits (o)"); */
633 else if (type == SCAN_IN)
635 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
637 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
639 BUFFER_ADD = bits_left - 2;
641 BUFFER_ADD = buffer[cur_byte];
644 if ((ir_scan && (end_state == TAP_IRSHIFT)) ||
645 (!ir_scan && (end_state == TAP_DRSHIFT)))
649 /* Clock Data Bits In and Out LSB First */
651 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
653 else if (type == SCAN_OUT)
655 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
657 /* LOG_DEBUG("added TDI bits (o)"); */
659 else if (type == SCAN_IN)
661 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
663 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
666 BUFFER_ADD = last_bit;
670 /* move from Shift-IR/DR to end state */
671 if (type != SCAN_OUT)
673 /* Clock Data to TMS/CS Pin with Read */
675 /* LOG_DEBUG("added TMS scan (read)"); */
679 /* Clock Data to TMS/CS Pin (no Read) */
681 /* LOG_DEBUG("added TMS scan (no read)"); */
684 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
685 cur_state = end_state;
689 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
691 int num_bytes = (scan_size + 7) / 8;
692 int bits_left = scan_size;
695 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
696 u8 *receive_pointer = receive_buffer;
700 int thisrun_read = 0;
704 LOG_ERROR("BUG: large IR scans are not supported");
708 if (cur_state != TAP_DRSHIFT)
710 /* command "Clock Data to TMS/CS Pin (no Read)" */
715 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
716 cur_state = TAP_DRSHIFT;
719 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
721 LOG_ERROR("couldn't write MPSSE commands to FT2232");
724 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
725 ft2232_buffer_size = 0;
727 /* add command for complete bytes */
728 while (num_bytes > 1)
734 /* Clock Data Bytes In and Out LSB First */
736 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
738 else if (type == SCAN_OUT)
740 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
742 /* LOG_DEBUG("added TDI bytes (o)"); */
744 else if (type == SCAN_IN)
746 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
748 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
750 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
751 thisrun_read = thisrun_bytes;
752 num_bytes -= thisrun_bytes;
753 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
754 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
757 /* add complete bytes */
758 while(thisrun_bytes-- > 0)
760 BUFFER_ADD = buffer[cur_byte];
765 else /* (type == SCAN_IN) */
767 bits_left -= 8 * (thisrun_bytes);
770 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
772 LOG_ERROR("couldn't write MPSSE commands to FT2232");
775 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
776 ft2232_buffer_size = 0;
778 if (type != SCAN_OUT)
780 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
782 LOG_ERROR("couldn't read from FT2232");
785 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
786 receive_pointer += bytes_read;
792 /* the most signifcant bit is scanned during TAP movement */
794 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
798 /* process remaining bits but the last one */
803 /* Clock Data Bits In and Out LSB First */
805 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
807 else if (type == SCAN_OUT)
809 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
811 /* LOG_DEBUG("added TDI bits (o)"); */
813 else if (type == SCAN_IN)
815 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
817 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
819 BUFFER_ADD = bits_left - 2;
821 BUFFER_ADD = buffer[cur_byte];
823 if (type != SCAN_OUT)
827 if (end_state == TAP_DRSHIFT)
831 /* Clock Data Bits In and Out LSB First */
833 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
835 else if (type == SCAN_OUT)
837 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
839 /* LOG_DEBUG("added TDI bits (o)"); */
841 else if (type == SCAN_IN)
843 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
845 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
848 BUFFER_ADD = last_bit;
852 /* move from Shift-IR/DR to end state */
853 if (type != SCAN_OUT)
855 /* Clock Data to TMS/CS Pin with Read */
857 /* LOG_DEBUG("added TMS scan (read)"); */
861 /* Clock Data to TMS/CS Pin (no Read) */
863 /* LOG_DEBUG("added TMS scan (no read)"); */
866 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
867 cur_state = end_state;
870 if (type != SCAN_OUT)
873 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
875 LOG_ERROR("couldn't write MPSSE commands to FT2232");
878 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
879 ft2232_buffer_size = 0;
881 if (type != SCAN_OUT)
883 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
885 LOG_ERROR("couldn't read from FT2232");
888 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
889 receive_pointer += bytes_read;
895 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
897 int predicted_size = 3;
898 int num_bytes = (scan_size - 1) / 8;
900 if (cur_state != TAP_DRSHIFT)
903 if (type == SCAN_IN) /* only from device to host */
906 predicted_size += (CEIL(num_bytes, 65536)) * 3;
907 /* remaining bits - 1 (up to 7) */
908 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
910 else /* host to device, or bidirectional */
913 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
914 /* remaining bits -1 (up to 7) */
915 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
918 return predicted_size;
921 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
923 int predicted_size = 0;
925 if (type != SCAN_OUT)
928 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
929 /* remaining bits - 1 */
930 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
931 /* last bit (from TMS scan) */
935 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
937 return predicted_size;
940 void usbjtag_reset(int trst, int srst)
944 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
945 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
947 low_output &= ~nTRST; /* switch output low */
951 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
952 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
954 low_output |= nTRST; /* switch output high */
959 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
960 low_output &= ~nSRST; /* switch output low */
962 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
966 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
967 low_output |= nSRST; /* switch output high */
969 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
972 /* command "set data bits low byte" */
974 BUFFER_ADD = low_output;
975 BUFFER_ADD = low_direction;
979 void jtagkey_reset(int trst, int srst)
983 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
984 high_output &= ~nTRSTnOE;
986 high_output &= ~nTRST;
990 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
991 high_output |= nTRSTnOE;
993 high_output |= nTRST;
998 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
999 high_output &= ~nSRST;
1001 high_output &= ~nSRSTnOE;
1005 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1006 high_output |= nSRST;
1008 high_output |= nSRSTnOE;
1011 /* command "set data bits high byte" */
1013 BUFFER_ADD = high_output;
1014 BUFFER_ADD = high_direction;
1015 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1018 void olimex_jtag_reset(int trst, int srst)
1022 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1023 high_output &= ~nTRSTnOE;
1025 high_output &= ~nTRST;
1029 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1030 high_output |= nTRSTnOE;
1032 high_output |= nTRST;
1037 high_output |= nSRST;
1041 high_output &= ~nSRST;
1044 /* command "set data bits high byte" */
1046 BUFFER_ADD = high_output;
1047 BUFFER_ADD = high_direction;
1048 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1051 void axm0432_jtag_reset(int trst, int srst)
1055 cur_state = TAP_RESET;
1056 high_output &= ~nTRST;
1060 high_output |= nTRST;
1065 high_output &= ~nSRST;
1069 high_output |= nSRST;
1072 /* command "set data bits low byte" */
1074 BUFFER_ADD = high_output;
1075 BUFFER_ADD = high_direction;
1076 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1079 void flyswatter_reset(int trst, int srst)
1083 low_output &= ~nTRST;
1087 low_output |= nTRST;
1092 low_output |= nSRST;
1096 low_output &= ~nSRST;
1099 /* command "set data bits low byte" */
1101 BUFFER_ADD = low_output;
1102 BUFFER_ADD = low_direction;
1103 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1106 void turtle_reset(int trst, int srst)
1112 low_output |= nSRST;
1116 low_output &= ~nSRST;
1119 /* command "set data bits low byte" */
1121 BUFFER_ADD = low_output;
1122 BUFFER_ADD = low_direction;
1123 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1126 void comstick_reset(int trst, int srst)
1130 high_output &= ~nTRST;
1134 high_output |= nTRST;
1139 high_output &= ~nSRST;
1143 high_output |= nSRST;
1146 /* command "set data bits high byte" */
1148 BUFFER_ADD = high_output;
1149 BUFFER_ADD = high_direction;
1150 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1153 void stm32stick_reset(int trst, int srst)
1157 high_output &= ~nTRST;
1161 high_output |= nTRST;
1166 low_output &= ~nSRST;
1170 low_output |= nSRST;
1173 /* command "set data bits low byte" */
1175 BUFFER_ADD = low_output;
1176 BUFFER_ADD = low_direction;
1178 /* command "set data bits high byte" */
1180 BUFFER_ADD = high_output;
1181 BUFFER_ADD = high_direction;
1182 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1185 int ft2232_execute_queue()
1187 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1188 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1190 int scan_size; /* size of IR or DR scan */
1191 enum scan_type type;
1193 int predicted_size = 0;
1194 int require_send = 0;
1197 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1198 * that wasn't handled by a caller-provided error handler
1202 ft2232_buffer_size = 0;
1203 ft2232_expect_read = 0;
1205 /* blink, if the current layout has that feature */
1213 case JTAG_END_STATE:
1214 if (cmd->cmd.end_state->end_state != -1)
1215 ft2232_end_state(cmd->cmd.end_state->end_state);
1218 /* only send the maximum buffer size that FT2232C can handle */
1220 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1222 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1223 retval = ERROR_JTAG_QUEUE_FAILED;
1228 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1230 cur_state = TAP_RESET;
1232 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1235 #ifdef _DEBUG_JTAG_IO_
1236 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1240 /* only send the maximum buffer size that FT2232C can handle */
1242 if (cur_state != TAP_IDLE)
1243 predicted_size += 3;
1244 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1245 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_IDLE))
1246 predicted_size += 3;
1247 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_IDLE))
1248 predicted_size += 3;
1249 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1251 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1252 retval = ERROR_JTAG_QUEUE_FAILED;
1256 if (cur_state != TAP_IDLE)
1258 /* command "Clock Data to TMS/CS Pin (no Read)" */
1263 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IDLE);
1264 cur_state = TAP_IDLE;
1267 i = cmd->cmd.runtest->num_cycles;
1270 /* command "Clock Data to TMS/CS Pin (no Read)" */
1273 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1276 cur_state = TAP_IDLE;
1277 i -= (i > 7) ? 7 : i;
1278 /* LOG_DEBUG("added TMS scan (no read)"); */
1280 if (cmd->cmd.runtest->end_state != -1)
1281 ft2232_end_state(cmd->cmd.runtest->end_state);
1282 if (cur_state != end_state)
1284 /* command "Clock Data to TMS/CS Pin (no Read)" */
1289 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1290 cur_state = end_state;
1291 /* LOG_DEBUG("added TMS scan (no read)"); */
1294 #ifdef _DEBUG_JTAG_IO_
1295 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1298 case JTAG_STATEMOVE:
1299 /* only send the maximum buffer size that FT2232C can handle */
1301 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1303 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1304 retval = ERROR_JTAG_QUEUE_FAILED;
1308 if (cmd->cmd.statemove->end_state != -1)
1309 ft2232_end_state(cmd->cmd.statemove->end_state);
1310 /* command "Clock Data to TMS/CS Pin (no Read)" */
1315 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1316 /* LOG_DEBUG("added TMS scan (no read)"); */
1317 cur_state = end_state;
1319 #ifdef _DEBUG_JTAG_IO_
1320 LOG_DEBUG("statemove: %i", end_state);
1324 /* only send the maximum buffer size that FT2232C can handle */
1325 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1326 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1328 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1329 retval = ERROR_JTAG_QUEUE_FAILED;
1333 ft2232_add_pathmove(cmd->cmd.pathmove);
1335 #ifdef _DEBUG_JTAG_IO_
1336 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1340 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1341 type = jtag_scan_type(cmd->cmd.scan);
1342 predicted_size = ft2232_predict_scan_out(scan_size, type);
1343 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1345 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1346 /* unsent commands before this */
1347 if (first_unsent != cmd)
1348 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1349 retval = ERROR_JTAG_QUEUE_FAILED;
1351 /* current command */
1352 if (cmd->cmd.scan->end_state != -1)
1353 ft2232_end_state(cmd->cmd.scan->end_state);
1354 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1356 first_unsent = cmd->next;
1361 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1363 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1364 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1365 retval = ERROR_JTAG_QUEUE_FAILED;
1369 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1370 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1371 if (cmd->cmd.scan->end_state != -1)
1372 ft2232_end_state(cmd->cmd.scan->end_state);
1373 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1377 #ifdef _DEBUG_JTAG_IO_
1378 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1382 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1383 retval = ERROR_JTAG_QUEUE_FAILED;
1384 first_unsent = cmd->next;
1385 jtag_sleep(cmd->cmd.sleep->us);
1386 #ifdef _DEBUG_JTAG_IO_
1387 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1391 LOG_ERROR("BUG: unknown JTAG command type encountered");
1397 if (require_send > 0)
1398 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1399 retval = ERROR_JTAG_QUEUE_FAILED;
1404 #if BUILD_FT2232_FTD2XX == 1
1405 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1408 DWORD openex_flags = 0;
1409 char *openex_string = NULL;
1412 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",ft2232_layout, vid, pid);
1415 /* Add non-standard Vid/Pid to the linux driver */
1416 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1418 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1422 if (ft2232_device_desc && ft2232_serial)
1424 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1425 ft2232_device_desc = NULL;
1428 if (ft2232_device_desc)
1430 openex_string = ft2232_device_desc;
1431 openex_flags = FT_OPEN_BY_DESCRIPTION;
1433 else if (ft2232_serial)
1435 openex_string = ft2232_serial;
1436 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1440 LOG_ERROR("neither device description nor serial number specified");
1441 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1443 return ERROR_JTAG_INIT_FAILED;
1446 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1451 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1453 return ERROR_JTAG_INIT_FAILED;
1455 LOG_ERROR("unable to open ftdi device: %lu", status);
1456 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1457 if (status == FT_OK)
1459 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1462 for (i = 0; i < num_devices; i++)
1463 desc_array[i] = malloc(64);
1464 desc_array[num_devices] = NULL;
1466 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1468 if (status == FT_OK)
1470 LOG_ERROR("ListDevices: %lu\n", num_devices);
1471 for (i = 0; i < num_devices; i++)
1472 LOG_ERROR("%i: %s", i, desc_array[i]);
1475 for (i = 0; i < num_devices; i++)
1476 free(desc_array[i]);
1481 LOG_ERROR("ListDevices: NONE\n");
1483 return ERROR_JTAG_INIT_FAILED;
1486 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1488 LOG_ERROR("unable to set latency timer: %lu", status);
1489 return ERROR_JTAG_INIT_FAILED;
1492 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1494 LOG_ERROR("unable to get latency timer: %lu", status);
1495 return ERROR_JTAG_INIT_FAILED;
1499 LOG_DEBUG("current latency timer: %i", latency_timer);
1502 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1504 LOG_ERROR("unable to set timeouts: %lu", status);
1505 return ERROR_JTAG_INIT_FAILED;
1508 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1510 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1511 return ERROR_JTAG_INIT_FAILED;
1517 static int ft2232_purge_ftd2xx(void)
1521 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1523 LOG_ERROR("error purging ftd2xx device: %lu", status);
1524 return ERROR_JTAG_INIT_FAILED;
1529 #endif /* BUILD_FT2232_FTD2XX == 1 */
1531 #if BUILD_FT2232_LIBFTDI == 1
1532 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1536 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1537 ft2232_layout, vid, pid);
1539 if (ftdi_init(&ftdic) < 0)
1540 return ERROR_JTAG_INIT_FAILED;
1542 /* context, vendor id, product id */
1543 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1544 ft2232_serial) < 0) {
1546 LOG_WARNING("unable to open ftdi device (trying more): %s",
1549 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1551 return ERROR_JTAG_INIT_FAILED;
1554 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1556 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1557 return ERROR_JTAG_INIT_FAILED;
1560 if (ftdi_usb_reset(&ftdic) < 0)
1562 LOG_ERROR("unable to reset ftdi device");
1563 return ERROR_JTAG_INIT_FAILED;
1566 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1568 LOG_ERROR("unable to set latency timer");
1569 return ERROR_JTAG_INIT_FAILED;
1572 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1574 LOG_ERROR("unable to get latency timer");
1575 return ERROR_JTAG_INIT_FAILED;
1579 LOG_DEBUG("current latency timer: %i", latency_timer);
1582 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1587 static int ft2232_purge_libftdi(void)
1589 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1591 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1592 return ERROR_JTAG_INIT_FAILED;
1597 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1599 int ft2232_init(void)
1604 ft2232_layout_t *cur_layout = ft2232_layouts;
1607 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1609 ft2232_layout = "usbjtag";
1610 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1613 while (cur_layout->name)
1615 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1617 layout = cur_layout;
1625 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1626 return ERROR_JTAG_INIT_FAILED;
1629 for (i = 0; 1; i++) {
1631 * "more indicates that there are more IDs to try, so we should
1632 * not print an error for an ID mismatch (but for anything
1635 * try_more indicates that the error code returned indicates an
1636 * ID mismatch (and nothing else) and that we should proceeed
1637 * with the next ID pair.
1639 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1642 #if BUILD_FT2232_FTD2XX == 1
1643 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1645 #elif BUILD_FT2232_LIBFTDI == 1
1646 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1651 if (!more || !try_more)
1655 ft2232_buffer_size = 0;
1656 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1658 if (layout->init() != ERROR_OK)
1659 return ERROR_JTAG_INIT_FAILED;
1661 ft2232_speed(jtag_speed);
1663 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1664 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1666 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1667 return ERROR_JTAG_INIT_FAILED;
1670 #if BUILD_FT2232_FTD2XX == 1
1671 return ft2232_purge_ftd2xx();
1672 #elif BUILD_FT2232_LIBFTDI == 1
1673 return ft2232_purge_libftdi();
1679 int usbjtag_init(void)
1685 low_direction = 0x0b;
1687 if (strcmp(ft2232_layout, "usbjtag") == 0)
1694 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1701 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1708 low_direction = 0x8b;
1712 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1713 return ERROR_JTAG_INIT_FAILED;
1716 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1718 low_direction &= ~nTRSTnOE; /* nTRST input */
1719 low_output &= ~nTRST; /* nTRST = 0 */
1723 low_direction |= nTRSTnOE; /* nTRST output */
1724 low_output |= nTRST; /* nTRST = 1 */
1727 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1729 low_direction |= nSRSTnOE; /* nSRST output */
1730 low_output |= nSRST; /* nSRST = 1 */
1734 low_direction &= ~nSRSTnOE; /* nSRST input */
1735 low_output &= ~nSRST; /* nSRST = 0 */
1738 /* initialize low byte for jtag */
1739 buf[0] = 0x80; /* command "set data bits low byte" */
1740 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1741 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1742 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1744 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1746 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1747 return ERROR_JTAG_INIT_FAILED;
1753 int axm0432_jtag_init(void)
1759 low_direction = 0x2b;
1761 /* initialize low byte for jtag */
1762 buf[0] = 0x80; /* command "set data bits low byte" */
1763 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1764 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1765 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1767 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1769 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1770 return ERROR_JTAG_INIT_FAILED;
1773 if (strcmp(layout->name, "axm0432_jtag") == 0)
1776 nTRSTnOE = 0x0; /* No output enable for TRST*/
1778 nSRSTnOE = 0x0; /* No output enable for SRST*/
1782 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1787 high_direction = 0x0c;
1789 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1791 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1795 high_output |= nTRST;
1798 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1800 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1804 high_output |= nSRST;
1807 /* initialize high port */
1808 buf[0] = 0x82; /* command "set data bits high byte" */
1809 buf[1] = high_output; /* value */
1810 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1811 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1813 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1815 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1816 return ERROR_JTAG_INIT_FAILED;
1822 int jtagkey_init(void)
1828 low_direction = 0x1b;
1830 /* initialize low byte for jtag */
1831 buf[0] = 0x80; /* command "set data bits low byte" */
1832 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1833 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1834 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1836 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1838 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1839 return ERROR_JTAG_INIT_FAILED;
1842 if (strcmp(layout->name, "jtagkey") == 0)
1849 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1850 (strcmp(layout->name, "oocdlink") == 0))
1859 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1864 high_direction = 0x0f;
1866 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1868 high_output |= nTRSTnOE;
1869 high_output &= ~nTRST;
1873 high_output &= ~nTRSTnOE;
1874 high_output |= nTRST;
1877 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1879 high_output &= ~nSRSTnOE;
1880 high_output |= nSRST;
1884 high_output |= nSRSTnOE;
1885 high_output &= ~nSRST;
1888 /* initialize high port */
1889 buf[0] = 0x82; /* command "set data bits high byte" */
1890 buf[1] = high_output; /* value */
1891 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1892 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1894 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1896 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1897 return ERROR_JTAG_INIT_FAILED;
1903 int olimex_jtag_init(void)
1909 low_direction = 0x1b;
1911 /* initialize low byte for jtag */
1912 buf[0] = 0x80; /* command "set data bits low byte" */
1913 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1914 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1915 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1917 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1919 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1920 return ERROR_JTAG_INIT_FAILED;
1926 nSRSTnOE = 0x00; /* no output enable for nSRST */
1929 high_direction = 0x0f;
1931 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1933 high_output |= nTRSTnOE;
1934 high_output &= ~nTRST;
1938 high_output &= ~nTRSTnOE;
1939 high_output |= nTRST;
1942 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1944 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1948 high_output &= ~nSRST;
1951 /* turn red LED on */
1952 high_output |= 0x08;
1954 /* initialize high port */
1955 buf[0] = 0x82; /* command "set data bits high byte" */
1956 buf[1] = high_output; /* value */
1957 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1958 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1960 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1962 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1963 return ERROR_JTAG_INIT_FAILED;
1969 int flyswatter_init(void)
1975 low_direction = 0xfb;
1977 /* initialize low byte for jtag */
1978 buf[0] = 0x80; /* command "set data bits low byte" */
1979 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1980 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1981 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1983 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1985 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1986 return ERROR_JTAG_INIT_FAILED;
1990 nTRSTnOE = 0x0; /* not output enable for nTRST */
1992 nSRSTnOE = 0x00; /* no output enable for nSRST */
1995 high_direction = 0x0c;
1997 /* turn red LED1 on, LED2 off */
1998 high_output |= 0x08;
2000 /* initialize high port */
2001 buf[0] = 0x82; /* command "set data bits high byte" */
2002 buf[1] = high_output; /* value */
2003 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2004 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2006 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2008 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2009 return ERROR_JTAG_INIT_FAILED;
2015 int turtle_init(void)
2021 low_direction = 0x5b;
2023 /* initialize low byte for jtag */
2024 buf[0] = 0x80; /* command "set data bits low byte" */
2025 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2026 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2027 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2029 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2031 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2032 return ERROR_JTAG_INIT_FAILED;
2038 high_direction = 0x0C;
2040 /* initialize high port */
2041 buf[0] = 0x82; /* command "set data bits high byte" */
2042 buf[1] = high_output;
2043 buf[2] = high_direction;
2044 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2046 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2048 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2049 return ERROR_JTAG_INIT_FAILED;
2055 int comstick_init(void)
2061 low_direction = 0x0b;
2063 /* initialize low byte for jtag */
2064 buf[0] = 0x80; /* command "set data bits low byte" */
2065 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2066 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2067 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2069 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2071 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2072 return ERROR_JTAG_INIT_FAILED;
2076 nTRSTnOE = 0x00; /* no output enable for nTRST */
2078 nSRSTnOE = 0x00; /* no output enable for nSRST */
2081 high_direction = 0x03;
2083 /* initialize high port */
2084 buf[0] = 0x82; /* command "set data bits high byte" */
2085 buf[1] = high_output;
2086 buf[2] = high_direction;
2087 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2089 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2091 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2092 return ERROR_JTAG_INIT_FAILED;
2098 int stm32stick_init(void)
2104 low_direction = 0x8b;
2106 /* initialize low byte for jtag */
2107 buf[0] = 0x80; /* command "set data bits low byte" */
2108 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2109 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2110 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2112 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2114 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2115 return ERROR_JTAG_INIT_FAILED;
2119 nTRSTnOE = 0x00; /* no output enable for nTRST */
2121 nSRSTnOE = 0x00; /* no output enable for nSRST */
2124 high_direction = 0x03;
2126 /* initialize high port */
2127 buf[0] = 0x82; /* command "set data bits high byte" */
2128 buf[1] = high_output;
2129 buf[2] = high_direction;
2130 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2132 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2134 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2135 return ERROR_JTAG_INIT_FAILED;
2141 void olimex_jtag_blink(void)
2143 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2144 * ACBUS3 is bit 3 of the GPIOH port
2146 if (high_output & 0x08)
2148 /* set port pin high */
2149 high_output &= 0x07;
2153 /* set port pin low */
2154 high_output |= 0x08;
2158 BUFFER_ADD = high_output;
2159 BUFFER_ADD = high_direction;
2162 void turtle_jtag_blink(void)
2165 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2167 if (high_output & 0x08)
2177 BUFFER_ADD = high_output;
2178 BUFFER_ADD = high_direction;
2181 int ft2232_quit(void)
2183 #if BUILD_FT2232_FTD2XX == 1
2186 status = FT_Close(ftdih);
2187 #elif BUILD_FT2232_LIBFTDI == 1
2188 ftdi_disable_bitbang(&ftdic);
2190 ftdi_usb_close(&ftdic);
2192 ftdi_deinit(&ftdic);
2195 free(ft2232_buffer);
2196 ft2232_buffer = NULL;
2201 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2205 ft2232_device_desc = strdup(args[0]);
2209 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2215 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2219 ft2232_serial = strdup(args[0]);
2223 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2229 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2234 ft2232_layout = malloc(strlen(args[0]) + 1);
2235 strcpy(ft2232_layout, args[0]);
2240 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2244 if (argc > MAX_USB_IDS*2) {
2245 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2246 "(maximum is %d pairs)", MAX_USB_IDS);
2247 argc = MAX_USB_IDS*2;
2249 if (argc < 2 || (argc & 1))
2251 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2256 for (i = 0; i+1 < argc; i += 2) {
2257 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2258 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2261 * Explicitly terminate, in case there are multiples instances of
2264 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2269 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2273 ft2232_latency = atoi(args[0]);
2277 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");