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 =
159 .execute_queue = ft2232_execute_queue,
161 .speed = ft2232_speed,
162 .speed_div = ft2232_speed_div,
165 .register_commands = ft2232_register_commands,
170 int ft2232_write(u8 *buf, int size, u32* bytes_written)
172 #if BUILD_FT2232_FTD2XX == 1
174 DWORD dw_bytes_written;
175 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
177 *bytes_written = dw_bytes_written;
178 LOG_ERROR("FT_Write returned: %lu", status);
179 return ERROR_JTAG_DEVICE_ERROR;
183 *bytes_written = dw_bytes_written;
186 #elif BUILD_FT2232_LIBFTDI == 1
188 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
191 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
192 return ERROR_JTAG_DEVICE_ERROR;
196 *bytes_written = retval;
202 int ft2232_read(u8* buf, int size, u32* bytes_read)
204 #if BUILD_FT2232_FTD2XX == 1
210 while ((*bytes_read < size) && timeout--)
212 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
213 *bytes_read, &dw_bytes_read)) != FT_OK)
216 LOG_ERROR("FT_Read returned: %lu", status);
217 return ERROR_JTAG_DEVICE_ERROR;
219 *bytes_read += dw_bytes_read;
221 #elif BUILD_FT2232_LIBFTDI == 1
226 while ((*bytes_read < size) && timeout--)
228 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
231 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
232 return ERROR_JTAG_DEVICE_ERROR;
234 *bytes_read += retval;
238 if (*bytes_read < size)
240 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
241 return ERROR_JTAG_DEVICE_ERROR;
247 int ft2232_speed(int speed)
253 buf[0] = 0x86; /* command "set divisor" */
254 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
255 buf[2] = (speed >> 8) & 0xff; /* valueH */
257 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
258 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
260 LOG_ERROR("couldn't set FT2232 TCK speed");
267 int ft2232_speed_div(int speed, int *khz)
270 * Take a look in the FT2232 manual,
271 * AN2232C-01 Command Processor for
272 * MPSSE and MCU Host Bus. Chapter 3.8
274 *khz = 6000 / (1+speed);
279 int ft2232_khz(int khz, int *jtag_speed)
282 * Take a look in the FT2232 manual,
283 * AN2232C-01 Command Processor for
284 * MPSSE and MCU Host Bus. Chapter 3.8
286 * We will calc here with a multiplier
287 * of 10 for better rounding later.
290 /* Calc speed, (6000 / khz) - 1 */
291 /* Use 65000 for better rounding */
292 *jtag_speed = (60000 / khz) - 10;
294 /* Add 0.9 for rounding */
297 /* Calc real speed */
298 *jtag_speed = *jtag_speed / 10;
300 /* Check if speed is greater than 0 */
311 int ft2232_register_commands(struct command_context_s *cmd_ctx)
313 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
314 COMMAND_CONFIG, NULL);
315 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
316 COMMAND_CONFIG, NULL);
317 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
318 COMMAND_CONFIG, NULL);
319 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
320 COMMAND_CONFIG, NULL);
321 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
322 COMMAND_CONFIG, NULL);
326 void ft2232_end_state(enum tap_state state)
328 if (tap_move_map[state] != -1)
332 LOG_ERROR("BUG: %i is not a valid end state", state);
337 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
339 int num_bytes = ((scan_size + 7) / 8);
340 int bits_left = scan_size;
343 while(num_bytes-- > 1)
345 buffer[cur_byte] = BUFFER_READ;
350 buffer[cur_byte] = 0x0;
354 buffer[cur_byte] = BUFFER_READ >> 1;
357 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
361 void ft2232_debug_dump_buffer(void)
367 for (i = 0; i < ft2232_buffer_size; i++)
369 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
372 LOG_DEBUG("%s", line);
378 LOG_DEBUG("%s", line);
381 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
391 #ifdef _DEBUG_USB_IO_
392 struct timeval start, inter, inter2, end;
393 struct timeval d_inter, d_inter2, d_end;
396 #ifdef _DEBUG_USB_COMMS_
397 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
398 ft2232_debug_dump_buffer();
401 #ifdef _DEBUG_USB_IO_
402 gettimeofday(&start, NULL);
405 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
407 LOG_ERROR("couldn't write MPSSE commands to FT2232");
411 #ifdef _DEBUG_USB_IO_
412 gettimeofday(&inter, NULL);
415 if (ft2232_expect_read)
418 ft2232_buffer_size = 0;
420 #ifdef _DEBUG_USB_IO_
421 gettimeofday(&inter2, NULL);
424 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
426 LOG_ERROR("couldn't read from FT2232");
430 #ifdef _DEBUG_USB_IO_
431 gettimeofday(&end, NULL);
433 timeval_subtract(&d_inter, &inter, &start);
434 timeval_subtract(&d_inter2, &inter2, &start);
435 timeval_subtract(&d_end, &end, &start);
437 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);
441 ft2232_buffer_size = bytes_read;
443 if (ft2232_expect_read != ft2232_buffer_size)
445 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
446 ft2232_debug_dump_buffer();
451 #ifdef _DEBUG_USB_COMMS_
452 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
453 ft2232_debug_dump_buffer();
457 ft2232_expect_read = 0;
458 ft2232_read_pointer = 0;
460 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
461 * that wasn't handled by a caller-provided error handler
471 type = jtag_scan_type(cmd->cmd.scan);
472 if (type != SCAN_OUT)
474 scan_size = jtag_scan_size(cmd->cmd.scan);
475 buffer = calloc(CEIL(scan_size, 8), 1);
476 ft2232_read_scan(type, buffer, scan_size);
477 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
478 retval = ERROR_JTAG_QUEUE_FAILED;
488 ft2232_buffer_size = 0;
493 void ft2232_add_pathmove(pathmove_command_t *cmd)
495 int num_states = cmd->num_states;
504 int num_states_batch = num_states > 7 ? 7 : num_states;
507 /* command "Clock Data to TMS/CS Pin (no Read)" */
509 /* number of states remaining */
510 BUFFER_ADD = num_states_batch - 1;
512 while (num_states_batch--)
514 if (tap_transitions[cur_state].low == cmd->path[state_count])
515 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
516 else if (tap_transitions[cur_state].high == cmd->path[state_count])
517 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
520 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
524 cur_state = cmd->path[state_count];
529 BUFFER_ADD = tms_byte;
532 end_state = cur_state;
535 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
537 int num_bytes = (scan_size + 7) / 8;
538 int bits_left = scan_size;
542 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
544 /* command "Clock Data to TMS/CS Pin (no Read)" */
551 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
556 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
559 /* LOG_DEBUG("added TMS scan (no read)"); */
562 /* add command for complete bytes */
563 while (num_bytes > 1)
568 /* Clock Data Bytes In and Out LSB First */
570 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
572 else if (type == SCAN_OUT)
574 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
576 /* LOG_DEBUG("added TDI bytes (o)"); */
578 else if (type == SCAN_IN)
580 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
582 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
584 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
585 num_bytes -= thisrun_bytes;
586 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
587 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
590 /* add complete bytes */
591 while(thisrun_bytes-- > 0)
593 BUFFER_ADD = buffer[cur_byte];
598 else /* (type == SCAN_IN) */
600 bits_left -= 8 * (thisrun_bytes);
604 /* the most signifcant bit is scanned during TAP movement */
606 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
610 /* process remaining bits but the last one */
615 /* Clock Data Bits In and Out LSB First */
617 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
619 else if (type == SCAN_OUT)
621 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
623 /* LOG_DEBUG("added TDI bits (o)"); */
625 else if (type == SCAN_IN)
627 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
629 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
631 BUFFER_ADD = bits_left - 2;
633 BUFFER_ADD = buffer[cur_byte];
636 if ((ir_scan && (end_state == TAP_SI)) ||
637 (!ir_scan && (end_state == TAP_SD)))
641 /* Clock Data Bits In and Out LSB First */
643 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
645 else if (type == SCAN_OUT)
647 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
649 /* LOG_DEBUG("added TDI bits (o)"); */
651 else if (type == SCAN_IN)
653 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
655 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
658 BUFFER_ADD = last_bit;
662 /* move from Shift-IR/DR to end state */
663 if (type != SCAN_OUT)
665 /* Clock Data to TMS/CS Pin with Read */
667 /* LOG_DEBUG("added TMS scan (read)"); */
671 /* Clock Data to TMS/CS Pin (no Read) */
673 /* LOG_DEBUG("added TMS scan (no read)"); */
676 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
677 cur_state = end_state;
681 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
683 int num_bytes = (scan_size + 7) / 8;
684 int bits_left = scan_size;
687 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
688 u8 *receive_pointer = receive_buffer;
692 int thisrun_read = 0;
696 LOG_ERROR("BUG: large IR scans are not supported");
700 if (cur_state != TAP_SD)
702 /* command "Clock Data to TMS/CS Pin (no Read)" */
707 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
711 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
713 LOG_ERROR("couldn't write MPSSE commands to FT2232");
716 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
717 ft2232_buffer_size = 0;
719 /* add command for complete bytes */
720 while (num_bytes > 1)
726 /* Clock Data Bytes In and Out LSB First */
728 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
730 else if (type == SCAN_OUT)
732 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
734 /* LOG_DEBUG("added TDI bytes (o)"); */
736 else if (type == SCAN_IN)
738 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
740 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
742 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
743 thisrun_read = thisrun_bytes;
744 num_bytes -= thisrun_bytes;
745 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
746 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
749 /* add complete bytes */
750 while(thisrun_bytes-- > 0)
752 BUFFER_ADD = buffer[cur_byte];
757 else /* (type == SCAN_IN) */
759 bits_left -= 8 * (thisrun_bytes);
762 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
764 LOG_ERROR("couldn't write MPSSE commands to FT2232");
767 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
768 ft2232_buffer_size = 0;
770 if (type != SCAN_OUT)
772 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
774 LOG_ERROR("couldn't read from FT2232");
777 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
778 receive_pointer += bytes_read;
784 /* the most signifcant bit is scanned during TAP movement */
786 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
790 /* process remaining bits but the last one */
795 /* Clock Data Bits In and Out LSB First */
797 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
799 else if (type == SCAN_OUT)
801 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
803 /* LOG_DEBUG("added TDI bits (o)"); */
805 else if (type == SCAN_IN)
807 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
809 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
811 BUFFER_ADD = bits_left - 2;
813 BUFFER_ADD = buffer[cur_byte];
815 if (type != SCAN_OUT)
819 if (end_state == TAP_SD)
823 /* Clock Data Bits In and Out LSB First */
825 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
827 else if (type == SCAN_OUT)
829 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
831 /* LOG_DEBUG("added TDI bits (o)"); */
833 else if (type == SCAN_IN)
835 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
837 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
840 BUFFER_ADD = last_bit;
844 /* move from Shift-IR/DR to end state */
845 if (type != SCAN_OUT)
847 /* Clock Data to TMS/CS Pin with Read */
849 /* LOG_DEBUG("added TMS scan (read)"); */
853 /* Clock Data to TMS/CS Pin (no Read) */
855 /* LOG_DEBUG("added TMS scan (no read)"); */
858 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
859 cur_state = end_state;
862 if (type != SCAN_OUT)
865 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
867 LOG_ERROR("couldn't write MPSSE commands to FT2232");
870 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
871 ft2232_buffer_size = 0;
873 if (type != SCAN_OUT)
875 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
877 LOG_ERROR("couldn't read from FT2232");
880 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
881 receive_pointer += bytes_read;
887 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
889 int predicted_size = 3;
890 int num_bytes = (scan_size - 1) / 8;
892 if (cur_state != TAP_SD)
895 if (type == SCAN_IN) /* only from device to host */
898 predicted_size += (CEIL(num_bytes, 65536)) * 3;
899 /* remaining bits - 1 (up to 7) */
900 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
902 else /* host to device, or bidirectional */
905 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
906 /* remaining bits -1 (up to 7) */
907 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
910 return predicted_size;
913 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
915 int predicted_size = 0;
917 if (type != SCAN_OUT)
920 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
921 /* remaining bits - 1 */
922 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
923 /* last bit (from TMS scan) */
927 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
929 return predicted_size;
932 void usbjtag_reset(int trst, int srst)
936 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
937 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
939 low_output &= ~nTRST; /* switch output low */
943 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
944 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
946 low_output |= nTRST; /* switch output high */
951 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
952 low_output &= ~nSRST; /* switch output low */
954 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
958 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
959 low_output |= nSRST; /* switch output high */
961 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
964 /* command "set data bits low byte" */
966 BUFFER_ADD = low_output;
967 BUFFER_ADD = low_direction;
971 void jtagkey_reset(int trst, int srst)
975 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
976 high_output &= ~nTRSTnOE;
978 high_output &= ~nTRST;
982 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
983 high_output |= nTRSTnOE;
985 high_output |= nTRST;
990 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
991 high_output &= ~nSRST;
993 high_output &= ~nSRSTnOE;
997 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
998 high_output |= nSRST;
1000 high_output |= nSRSTnOE;
1003 /* command "set data bits high byte" */
1005 BUFFER_ADD = high_output;
1006 BUFFER_ADD = high_direction;
1007 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1010 void olimex_jtag_reset(int trst, int srst)
1014 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1015 high_output &= ~nTRSTnOE;
1017 high_output &= ~nTRST;
1021 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1022 high_output |= nTRSTnOE;
1024 high_output |= nTRST;
1029 high_output |= nSRST;
1033 high_output &= ~nSRST;
1036 /* command "set data bits high byte" */
1038 BUFFER_ADD = high_output;
1039 BUFFER_ADD = high_direction;
1040 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1043 void flyswatter_reset(int trst, int srst)
1047 low_output &= ~nTRST;
1051 low_output |= nTRST;
1056 low_output |= nSRST;
1060 low_output &= ~nSRST;
1063 /* command "set data bits low byte" */
1065 BUFFER_ADD = low_output;
1066 BUFFER_ADD = low_direction;
1067 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1070 void turtle_reset(int trst, int srst)
1076 low_output |= nSRST;
1080 low_output &= ~nSRST;
1083 /* command "set data bits low byte" */
1085 BUFFER_ADD = low_output;
1086 BUFFER_ADD = low_direction;
1087 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1090 void comstick_reset(int trst, int srst)
1094 high_output &= ~nTRST;
1098 high_output |= nTRST;
1103 high_output &= ~nSRST;
1107 high_output |= nSRST;
1110 /* command "set data bits high byte" */
1112 BUFFER_ADD = high_output;
1113 BUFFER_ADD = high_direction;
1114 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1117 void stm32stick_reset(int trst, int srst)
1121 high_output &= ~nTRST;
1125 high_output |= nTRST;
1130 low_output &= ~nSRST;
1134 low_output |= nSRST;
1137 /* command "set data bits low byte" */
1139 BUFFER_ADD = low_output;
1140 BUFFER_ADD = low_direction;
1142 /* command "set data bits high byte" */
1144 BUFFER_ADD = high_output;
1145 BUFFER_ADD = high_direction;
1146 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1149 int ft2232_execute_queue()
1151 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1152 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1154 int scan_size; /* size of IR or DR scan */
1155 enum scan_type type;
1157 int predicted_size = 0;
1158 int require_send = 0;
1161 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1162 * that wasn't handled by a caller-provided error handler
1166 ft2232_buffer_size = 0;
1167 ft2232_expect_read = 0;
1169 /* blink, if the current layout has that feature */
1177 case JTAG_END_STATE:
1178 if (cmd->cmd.end_state->end_state != -1)
1179 ft2232_end_state(cmd->cmd.end_state->end_state);
1182 /* only send the maximum buffer size that FT2232C can handle */
1184 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1186 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1187 retval = ERROR_JTAG_QUEUE_FAILED;
1192 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1194 cur_state = TAP_TLR;
1196 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1199 #ifdef _DEBUG_JTAG_IO_
1200 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1204 /* only send the maximum buffer size that FT2232C can handle */
1206 if (cur_state != TAP_RTI)
1207 predicted_size += 3;
1208 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1209 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1210 predicted_size += 3;
1211 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1212 predicted_size += 3;
1213 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1215 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1216 retval = ERROR_JTAG_QUEUE_FAILED;
1220 if (cur_state != TAP_RTI)
1222 /* command "Clock Data to TMS/CS Pin (no Read)" */
1227 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1228 cur_state = TAP_RTI;
1231 i = cmd->cmd.runtest->num_cycles;
1234 /* command "Clock Data to TMS/CS Pin (no Read)" */
1237 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1240 cur_state = TAP_RTI;
1241 i -= (i > 7) ? 7 : i;
1242 /* LOG_DEBUG("added TMS scan (no read)"); */
1244 if (cmd->cmd.runtest->end_state != -1)
1245 ft2232_end_state(cmd->cmd.runtest->end_state);
1246 if (cur_state != end_state)
1248 /* command "Clock Data to TMS/CS Pin (no Read)" */
1253 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1254 cur_state = end_state;
1255 /* LOG_DEBUG("added TMS scan (no read)"); */
1258 #ifdef _DEBUG_JTAG_IO_
1259 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1262 case JTAG_STATEMOVE:
1263 /* only send the maximum buffer size that FT2232C can handle */
1265 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1267 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1268 retval = ERROR_JTAG_QUEUE_FAILED;
1272 if (cmd->cmd.statemove->end_state != -1)
1273 ft2232_end_state(cmd->cmd.statemove->end_state);
1274 /* command "Clock Data to TMS/CS Pin (no Read)" */
1279 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1280 /* LOG_DEBUG("added TMS scan (no read)"); */
1281 cur_state = end_state;
1283 #ifdef _DEBUG_JTAG_IO_
1284 LOG_DEBUG("statemove: %i", end_state);
1288 /* only send the maximum buffer size that FT2232C can handle */
1289 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1290 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1292 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1293 retval = ERROR_JTAG_QUEUE_FAILED;
1297 ft2232_add_pathmove(cmd->cmd.pathmove);
1299 #ifdef _DEBUG_JTAG_IO_
1300 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1304 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1305 type = jtag_scan_type(cmd->cmd.scan);
1306 predicted_size = ft2232_predict_scan_out(scan_size, type);
1307 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1309 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1310 /* unsent commands before this */
1311 if (first_unsent != cmd)
1312 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1313 retval = ERROR_JTAG_QUEUE_FAILED;
1315 /* current command */
1316 if (cmd->cmd.scan->end_state != -1)
1317 ft2232_end_state(cmd->cmd.scan->end_state);
1318 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1320 first_unsent = cmd->next;
1325 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1327 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1328 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1329 retval = ERROR_JTAG_QUEUE_FAILED;
1333 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1334 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1335 if (cmd->cmd.scan->end_state != -1)
1336 ft2232_end_state(cmd->cmd.scan->end_state);
1337 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1341 #ifdef _DEBUG_JTAG_IO_
1342 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1346 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1347 retval = ERROR_JTAG_QUEUE_FAILED;
1348 first_unsent = cmd->next;
1349 jtag_sleep(cmd->cmd.sleep->us);
1350 #ifdef _DEBUG_JTAG_IO_
1351 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1355 LOG_ERROR("BUG: unknown JTAG command type encountered");
1361 if (require_send > 0)
1362 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1363 retval = ERROR_JTAG_QUEUE_FAILED;
1368 #if BUILD_FT2232_FTD2XX == 1
1369 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1372 DWORD openex_flags = 0;
1373 char *openex_string = NULL;
1376 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1377 ft2232_layout, vid, pid);
1380 /* Add non-standard Vid/Pid to the linux driver */
1381 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1383 LOG_WARNING("couldn't add %4.4x:%4.4x",
1388 if (ft2232_device_desc && ft2232_serial)
1390 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1391 ft2232_device_desc = NULL;
1394 if (ft2232_device_desc)
1396 openex_string = ft2232_device_desc;
1397 openex_flags = FT_OPEN_BY_DESCRIPTION;
1399 else if (ft2232_serial)
1401 openex_string = ft2232_serial;
1402 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1406 LOG_ERROR("neither device description nor serial number specified");
1407 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1409 return ERROR_JTAG_INIT_FAILED;
1412 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1417 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1420 return ERROR_JTAG_INIT_FAILED;
1422 LOG_ERROR("unable to open ftdi device: %lu", status);
1423 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1424 if (status == FT_OK)
1426 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1429 for (i = 0; i < num_devices; i++)
1430 desc_array[i] = malloc(64);
1431 desc_array[num_devices] = NULL;
1433 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1435 if (status == FT_OK)
1437 LOG_ERROR("ListDevices: %lu\n", num_devices);
1438 for (i = 0; i < num_devices; i++)
1439 LOG_ERROR("%i: %s", i, desc_array[i]);
1442 for (i = 0; i < num_devices; i++)
1443 free(desc_array[i]);
1448 LOG_ERROR("ListDevices: NONE\n");
1450 return ERROR_JTAG_INIT_FAILED;
1453 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1455 LOG_ERROR("unable to set latency timer: %lu", status);
1456 return ERROR_JTAG_INIT_FAILED;
1459 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1461 LOG_ERROR("unable to get latency timer: %lu", status);
1462 return ERROR_JTAG_INIT_FAILED;
1466 LOG_DEBUG("current latency timer: %i", latency_timer);
1469 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1471 LOG_ERROR("unable to set timeouts: %lu", status);
1472 return ERROR_JTAG_INIT_FAILED;
1475 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1477 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1478 return ERROR_JTAG_INIT_FAILED;
1484 static int ft2232_purge_ftd2xx(void)
1488 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1490 LOG_ERROR("error purging ftd2xx device: %lu", status);
1491 return ERROR_JTAG_INIT_FAILED;
1496 #endif /* BUILD_FT2232_FTD2XX == 1 */
1498 #if BUILD_FT2232_LIBFTDI == 1
1499 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1503 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1504 ft2232_layout, vid, pid);
1506 if (ftdi_init(&ftdic) < 0)
1507 return ERROR_JTAG_INIT_FAILED;
1509 /* context, vendor id, product id */
1510 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1511 ft2232_serial) < 0) {
1513 LOG_WARNING("unable to open ftdi device (trying more): %s",
1516 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1518 return ERROR_JTAG_INIT_FAILED;
1521 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1523 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1524 return ERROR_JTAG_INIT_FAILED;
1527 if (ftdi_usb_reset(&ftdic) < 0)
1529 LOG_ERROR("unable to reset ftdi device");
1530 return ERROR_JTAG_INIT_FAILED;
1533 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1535 LOG_ERROR("unable to set latency timer");
1536 return ERROR_JTAG_INIT_FAILED;
1539 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1541 LOG_ERROR("unable to get latency timer");
1542 return ERROR_JTAG_INIT_FAILED;
1546 LOG_DEBUG("current latency timer: %i", latency_timer);
1549 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1554 static int ft2232_purge_libftdi(void)
1556 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1558 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1559 return ERROR_JTAG_INIT_FAILED;
1564 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1566 int ft2232_init(void)
1571 ft2232_layout_t *cur_layout = ft2232_layouts;
1574 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1576 ft2232_layout = "usbjtag";
1577 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1580 while (cur_layout->name)
1582 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1584 layout = cur_layout;
1592 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1593 return ERROR_JTAG_INIT_FAILED;
1596 for (i = 0; 1; i++) {
1598 * "more indicates that there are more IDs to try, so we should
1599 * not print an error for an ID mismatch (but for anything
1602 * try_more indicates that the error code returned indicates an
1603 * ID mismatch (and nothing else) and that we should proceeed
1604 * with the next ID pair.
1606 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1609 #if BUILD_FT2232_FTD2XX == 1
1610 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1612 #elif BUILD_FT2232_LIBFTDI == 1
1613 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1618 if (!more || !try_more)
1622 ft2232_buffer_size = 0;
1623 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1625 if (layout->init() != ERROR_OK)
1626 return ERROR_JTAG_INIT_FAILED;
1628 ft2232_speed(jtag_speed);
1630 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1631 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1633 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1634 return ERROR_JTAG_INIT_FAILED;
1637 #if BUILD_FT2232_FTD2XX == 1
1638 return ft2232_purge_ftd2xx();
1639 #elif BUILD_FT2232_LIBFTDI == 1
1640 return ft2232_purge_libftdi();
1646 int usbjtag_init(void)
1652 low_direction = 0x0b;
1654 if (strcmp(ft2232_layout, "usbjtag") == 0)
1661 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1668 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1675 low_direction = 0x8b;
1679 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1680 return ERROR_JTAG_INIT_FAILED;
1683 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1685 low_direction &= ~nTRSTnOE; /* nTRST input */
1686 low_output &= ~nTRST; /* nTRST = 0 */
1690 low_direction |= nTRSTnOE; /* nTRST output */
1691 low_output |= nTRST; /* nTRST = 1 */
1694 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1696 low_direction |= nSRSTnOE; /* nSRST output */
1697 low_output |= nSRST; /* nSRST = 1 */
1701 low_direction &= ~nSRSTnOE; /* nSRST input */
1702 low_output &= ~nSRST; /* nSRST = 0 */
1705 /* initialize low byte for jtag */
1706 buf[0] = 0x80; /* command "set data bits low byte" */
1707 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1708 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1709 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1711 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1713 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1714 return ERROR_JTAG_INIT_FAILED;
1720 int jtagkey_init(void)
1726 low_direction = 0x1b;
1728 /* initialize low byte for jtag */
1729 buf[0] = 0x80; /* command "set data bits low byte" */
1730 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1731 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1732 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1734 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1736 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1737 return ERROR_JTAG_INIT_FAILED;
1740 if (strcmp(layout->name, "jtagkey") == 0)
1747 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1748 (strcmp(layout->name, "oocdlink") == 0))
1757 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1762 high_direction = 0x0f;
1764 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1766 high_output |= nTRSTnOE;
1767 high_output &= ~nTRST;
1771 high_output &= ~nTRSTnOE;
1772 high_output |= nTRST;
1775 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1777 high_output &= ~nSRSTnOE;
1778 high_output |= nSRST;
1782 high_output |= nSRSTnOE;
1783 high_output &= ~nSRST;
1786 /* initialize high port */
1787 buf[0] = 0x82; /* command "set data bits high byte" */
1788 buf[1] = high_output; /* value */
1789 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1790 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1792 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1794 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1795 return ERROR_JTAG_INIT_FAILED;
1801 int olimex_jtag_init(void)
1807 low_direction = 0x1b;
1809 /* initialize low byte for jtag */
1810 buf[0] = 0x80; /* command "set data bits low byte" */
1811 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1812 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1813 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1815 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1817 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1818 return ERROR_JTAG_INIT_FAILED;
1824 nSRSTnOE = 0x00; /* no output enable for nSRST */
1827 high_direction = 0x0f;
1829 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1831 high_output |= nTRSTnOE;
1832 high_output &= ~nTRST;
1836 high_output &= ~nTRSTnOE;
1837 high_output |= nTRST;
1840 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1842 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1846 high_output &= ~nSRST;
1849 /* turn red LED on */
1850 high_output |= 0x08;
1852 /* initialize high port */
1853 buf[0] = 0x82; /* command "set data bits high byte" */
1854 buf[1] = high_output; /* value */
1855 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1856 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1858 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1860 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1861 return ERROR_JTAG_INIT_FAILED;
1867 int flyswatter_init(void)
1873 low_direction = 0xfb;
1875 /* initialize low byte for jtag */
1876 buf[0] = 0x80; /* command "set data bits low byte" */
1877 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1878 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1879 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1881 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1883 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1884 return ERROR_JTAG_INIT_FAILED;
1888 nTRSTnOE = 0x0; /* not output enable for nTRST */
1890 nSRSTnOE = 0x00; /* no output enable for nSRST */
1893 high_direction = 0x0c;
1895 /* turn red LED1 on, LED2 off */
1896 high_output |= 0x08;
1898 /* initialize high port */
1899 buf[0] = 0x82; /* command "set data bits high byte" */
1900 buf[1] = high_output; /* value */
1901 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1902 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1904 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1906 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1907 return ERROR_JTAG_INIT_FAILED;
1913 int turtle_init(void)
1919 low_direction = 0x5b;
1921 /* initialize low byte for jtag */
1922 buf[0] = 0x80; /* command "set data bits low byte" */
1923 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1924 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1925 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1927 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1929 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1930 return ERROR_JTAG_INIT_FAILED;
1936 high_direction = 0x0C;
1938 /* initialize high port */
1939 buf[0] = 0x82; /* command "set data bits high byte" */
1940 buf[1] = high_output;
1941 buf[2] = high_direction;
1942 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1944 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1946 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1947 return ERROR_JTAG_INIT_FAILED;
1953 int comstick_init(void)
1959 low_direction = 0x0b;
1961 /* initialize low byte for jtag */
1962 buf[0] = 0x80; /* command "set data bits low byte" */
1963 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1964 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1965 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1967 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1969 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1970 return ERROR_JTAG_INIT_FAILED;
1974 nTRSTnOE = 0x00; /* no output enable for nTRST */
1976 nSRSTnOE = 0x00; /* no output enable for nSRST */
1979 high_direction = 0x03;
1981 /* initialize high port */
1982 buf[0] = 0x82; /* command "set data bits high byte" */
1983 buf[1] = high_output;
1984 buf[2] = high_direction;
1985 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1987 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1989 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1990 return ERROR_JTAG_INIT_FAILED;
1996 int stm32stick_init(void)
2002 low_direction = 0x8b;
2004 /* initialize low byte for jtag */
2005 buf[0] = 0x80; /* command "set data bits low byte" */
2006 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2007 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2008 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2010 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2012 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2013 return ERROR_JTAG_INIT_FAILED;
2017 nTRSTnOE = 0x00; /* no output enable for nTRST */
2019 nSRSTnOE = 0x00; /* no output enable for nSRST */
2022 high_direction = 0x03;
2024 /* initialize high port */
2025 buf[0] = 0x82; /* command "set data bits high byte" */
2026 buf[1] = high_output;
2027 buf[2] = high_direction;
2028 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2030 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2032 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2033 return ERROR_JTAG_INIT_FAILED;
2039 void olimex_jtag_blink(void)
2041 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2042 * ACBUS3 is bit 3 of the GPIOH port
2044 if (high_output & 0x08)
2046 /* set port pin high */
2047 high_output &= 0x07;
2051 /* set port pin low */
2052 high_output |= 0x08;
2056 BUFFER_ADD = high_output;
2057 BUFFER_ADD = high_direction;
2060 void turtle_jtag_blink(void)
2063 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2065 if (high_output & 0x08)
2075 BUFFER_ADD = high_output;
2076 BUFFER_ADD = high_direction;
2080 int ft2232_quit(void)
2082 #if BUILD_FT2232_FTD2XX == 1
2085 status = FT_Close(ftdih);
2086 #elif BUILD_FT2232_LIBFTDI == 1
2087 ftdi_disable_bitbang(&ftdic);
2089 ftdi_usb_close(&ftdic);
2091 ftdi_deinit(&ftdic);
2094 free(ft2232_buffer);
2095 ft2232_buffer = NULL;
2100 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2104 ft2232_device_desc = strdup(args[0]);
2108 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2114 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2118 ft2232_serial = strdup(args[0]);
2122 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2128 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2133 ft2232_layout = malloc(strlen(args[0]) + 1);
2134 strcpy(ft2232_layout, args[0]);
2139 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2143 if (argc > MAX_USB_IDS*2) {
2144 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2145 "(maximum is %d pairs)", MAX_USB_IDS);
2146 argc = MAX_USB_IDS*2;
2148 if (argc < 2 || (argc & 1))
2150 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2155 for (i = 0; i+1 < argc; i += 2) {
2156 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2157 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2160 * Explicitly terminate, in case there are multiples instances of
2163 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2168 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2172 ft2232_latency = atoi(args[0]);
2176 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");