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 *jtag_speed = (60000 / khz) - 10;
293 /* Add 0.5 for rounding */
296 /* Calc real speed */
297 *jtag_speed = *jtag_speed / 10;
299 /* Check if speed is greater than 0 */
310 int ft2232_register_commands(struct command_context_s *cmd_ctx)
312 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
313 COMMAND_CONFIG, NULL);
314 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
315 COMMAND_CONFIG, NULL);
316 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
317 COMMAND_CONFIG, NULL);
318 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
319 COMMAND_CONFIG, NULL);
320 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
321 COMMAND_CONFIG, NULL);
325 void ft2232_end_state(enum tap_state state)
327 if (tap_move_map[state] != -1)
331 LOG_ERROR("BUG: %i is not a valid end state", state);
336 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
338 int num_bytes = ((scan_size + 7) / 8);
339 int bits_left = scan_size;
342 while(num_bytes-- > 1)
344 buffer[cur_byte] = BUFFER_READ;
349 buffer[cur_byte] = 0x0;
353 buffer[cur_byte] = BUFFER_READ >> 1;
356 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
360 void ft2232_debug_dump_buffer(void)
366 for (i = 0; i < ft2232_buffer_size; i++)
368 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
371 LOG_DEBUG("%s", line);
377 LOG_DEBUG("%s", line);
380 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
390 #ifdef _DEBUG_USB_IO_
391 struct timeval start, inter, inter2, end;
392 struct timeval d_inter, d_inter2, d_end;
395 #ifdef _DEBUG_USB_COMMS_
396 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
397 ft2232_debug_dump_buffer();
400 #ifdef _DEBUG_USB_IO_
401 gettimeofday(&start, NULL);
404 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
406 LOG_ERROR("couldn't write MPSSE commands to FT2232");
410 #ifdef _DEBUG_USB_IO_
411 gettimeofday(&inter, NULL);
414 if (ft2232_expect_read)
417 ft2232_buffer_size = 0;
419 #ifdef _DEBUG_USB_IO_
420 gettimeofday(&inter2, NULL);
423 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
425 LOG_ERROR("couldn't read from FT2232");
429 #ifdef _DEBUG_USB_IO_
430 gettimeofday(&end, NULL);
432 timeval_subtract(&d_inter, &inter, &start);
433 timeval_subtract(&d_inter2, &inter2, &start);
434 timeval_subtract(&d_end, &end, &start);
436 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);
440 ft2232_buffer_size = bytes_read;
442 if (ft2232_expect_read != ft2232_buffer_size)
444 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
445 ft2232_debug_dump_buffer();
450 #ifdef _DEBUG_USB_COMMS_
451 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
452 ft2232_debug_dump_buffer();
456 ft2232_expect_read = 0;
457 ft2232_read_pointer = 0;
459 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
460 * that wasn't handled by a caller-provided error handler
470 type = jtag_scan_type(cmd->cmd.scan);
471 if (type != SCAN_OUT)
473 scan_size = jtag_scan_size(cmd->cmd.scan);
474 buffer = calloc(CEIL(scan_size, 8), 1);
475 ft2232_read_scan(type, buffer, scan_size);
476 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
477 retval = ERROR_JTAG_QUEUE_FAILED;
487 ft2232_buffer_size = 0;
492 void ft2232_add_pathmove(pathmove_command_t *cmd)
494 int num_states = cmd->num_states;
503 int num_states_batch = num_states > 7 ? 7 : num_states;
506 /* command "Clock Data to TMS/CS Pin (no Read)" */
508 /* number of states remaining */
509 BUFFER_ADD = num_states_batch - 1;
511 while (num_states_batch--)
513 if (tap_transitions[cur_state].low == cmd->path[state_count])
514 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
515 else if (tap_transitions[cur_state].high == cmd->path[state_count])
516 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
519 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
523 cur_state = cmd->path[state_count];
528 BUFFER_ADD = tms_byte;
531 end_state = cur_state;
534 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
536 int num_bytes = (scan_size + 7) / 8;
537 int bits_left = scan_size;
541 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
543 /* command "Clock Data to TMS/CS Pin (no Read)" */
550 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
555 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
558 /* LOG_DEBUG("added TMS scan (no read)"); */
561 /* add command for complete bytes */
562 while (num_bytes > 1)
567 /* Clock Data Bytes In and Out LSB First */
569 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
571 else if (type == SCAN_OUT)
573 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
575 /* LOG_DEBUG("added TDI bytes (o)"); */
577 else if (type == SCAN_IN)
579 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
581 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
583 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
584 num_bytes -= thisrun_bytes;
585 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
586 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
589 /* add complete bytes */
590 while(thisrun_bytes-- > 0)
592 BUFFER_ADD = buffer[cur_byte];
597 else /* (type == SCAN_IN) */
599 bits_left -= 8 * (thisrun_bytes);
603 /* the most signifcant bit is scanned during TAP movement */
605 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
609 /* process remaining bits but the last one */
614 /* Clock Data Bits In and Out LSB First */
616 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
618 else if (type == SCAN_OUT)
620 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
622 /* LOG_DEBUG("added TDI bits (o)"); */
624 else if (type == SCAN_IN)
626 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
628 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
630 BUFFER_ADD = bits_left - 2;
632 BUFFER_ADD = buffer[cur_byte];
635 if ((ir_scan && (end_state == TAP_SI)) ||
636 (!ir_scan && (end_state == TAP_SD)))
640 /* Clock Data Bits In and Out LSB First */
642 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
644 else if (type == SCAN_OUT)
646 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
648 /* LOG_DEBUG("added TDI bits (o)"); */
650 else if (type == SCAN_IN)
652 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
654 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
657 BUFFER_ADD = last_bit;
661 /* move from Shift-IR/DR to end state */
662 if (type != SCAN_OUT)
664 /* Clock Data to TMS/CS Pin with Read */
666 /* LOG_DEBUG("added TMS scan (read)"); */
670 /* Clock Data to TMS/CS Pin (no Read) */
672 /* LOG_DEBUG("added TMS scan (no read)"); */
675 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
676 cur_state = end_state;
680 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
682 int num_bytes = (scan_size + 7) / 8;
683 int bits_left = scan_size;
686 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
687 u8 *receive_pointer = receive_buffer;
691 int thisrun_read = 0;
695 LOG_ERROR("BUG: large IR scans are not supported");
699 if (cur_state != TAP_SD)
701 /* command "Clock Data to TMS/CS Pin (no Read)" */
706 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
710 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
712 LOG_ERROR("couldn't write MPSSE commands to FT2232");
715 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
716 ft2232_buffer_size = 0;
718 /* add command for complete bytes */
719 while (num_bytes > 1)
725 /* Clock Data Bytes In and Out LSB First */
727 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
729 else if (type == SCAN_OUT)
731 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
733 /* LOG_DEBUG("added TDI bytes (o)"); */
735 else if (type == SCAN_IN)
737 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
739 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
741 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
742 thisrun_read = thisrun_bytes;
743 num_bytes -= thisrun_bytes;
744 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
745 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
748 /* add complete bytes */
749 while(thisrun_bytes-- > 0)
751 BUFFER_ADD = buffer[cur_byte];
756 else /* (type == SCAN_IN) */
758 bits_left -= 8 * (thisrun_bytes);
761 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
763 LOG_ERROR("couldn't write MPSSE commands to FT2232");
766 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
767 ft2232_buffer_size = 0;
769 if (type != SCAN_OUT)
771 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
773 LOG_ERROR("couldn't read from FT2232");
776 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
777 receive_pointer += bytes_read;
783 /* the most signifcant bit is scanned during TAP movement */
785 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
789 /* process remaining bits but the last one */
794 /* Clock Data Bits In and Out LSB First */
796 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
798 else if (type == SCAN_OUT)
800 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
802 /* LOG_DEBUG("added TDI bits (o)"); */
804 else if (type == SCAN_IN)
806 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
808 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
810 BUFFER_ADD = bits_left - 2;
812 BUFFER_ADD = buffer[cur_byte];
814 if (type != SCAN_OUT)
818 if (end_state == TAP_SD)
822 /* Clock Data Bits In and Out LSB First */
824 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
826 else if (type == SCAN_OUT)
828 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
830 /* LOG_DEBUG("added TDI bits (o)"); */
832 else if (type == SCAN_IN)
834 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
836 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
839 BUFFER_ADD = last_bit;
843 /* move from Shift-IR/DR to end state */
844 if (type != SCAN_OUT)
846 /* Clock Data to TMS/CS Pin with Read */
848 /* LOG_DEBUG("added TMS scan (read)"); */
852 /* Clock Data to TMS/CS Pin (no Read) */
854 /* LOG_DEBUG("added TMS scan (no read)"); */
857 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
858 cur_state = end_state;
861 if (type != SCAN_OUT)
864 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
866 LOG_ERROR("couldn't write MPSSE commands to FT2232");
869 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
870 ft2232_buffer_size = 0;
872 if (type != SCAN_OUT)
874 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
876 LOG_ERROR("couldn't read from FT2232");
879 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
880 receive_pointer += bytes_read;
886 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
888 int predicted_size = 3;
889 int num_bytes = (scan_size - 1) / 8;
891 if (cur_state != TAP_SD)
894 if (type == SCAN_IN) /* only from device to host */
897 predicted_size += (CEIL(num_bytes, 65536)) * 3;
898 /* remaining bits - 1 (up to 7) */
899 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
901 else /* host to device, or bidirectional */
904 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
905 /* remaining bits -1 (up to 7) */
906 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
909 return predicted_size;
912 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
914 int predicted_size = 0;
916 if (type != SCAN_OUT)
919 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
920 /* remaining bits - 1 */
921 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
922 /* last bit (from TMS scan) */
926 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
928 return predicted_size;
931 void usbjtag_reset(int trst, int srst)
935 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
936 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
938 low_output &= ~nTRST; /* switch output low */
942 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
943 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
945 low_output |= nTRST; /* switch output high */
950 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
951 low_output &= ~nSRST; /* switch output low */
953 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
957 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
958 low_output |= nSRST; /* switch output high */
960 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
963 /* command "set data bits low byte" */
965 BUFFER_ADD = low_output;
966 BUFFER_ADD = low_direction;
970 void jtagkey_reset(int trst, int srst)
974 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
975 high_output &= ~nTRSTnOE;
977 high_output &= ~nTRST;
981 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
982 high_output |= nTRSTnOE;
984 high_output |= nTRST;
989 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
990 high_output &= ~nSRST;
992 high_output &= ~nSRSTnOE;
996 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
997 high_output |= nSRST;
999 high_output |= nSRSTnOE;
1002 /* command "set data bits high byte" */
1004 BUFFER_ADD = high_output;
1005 BUFFER_ADD = high_direction;
1006 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1009 void olimex_jtag_reset(int trst, int srst)
1013 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1014 high_output &= ~nTRSTnOE;
1016 high_output &= ~nTRST;
1020 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1021 high_output |= nTRSTnOE;
1023 high_output |= nTRST;
1028 high_output |= nSRST;
1032 high_output &= ~nSRST;
1035 /* command "set data bits high byte" */
1037 BUFFER_ADD = high_output;
1038 BUFFER_ADD = high_direction;
1039 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1042 void flyswatter_reset(int trst, int srst)
1046 low_output &= ~nTRST;
1050 low_output |= nTRST;
1055 low_output |= nSRST;
1059 low_output &= ~nSRST;
1062 /* command "set data bits low byte" */
1064 BUFFER_ADD = low_output;
1065 BUFFER_ADD = low_direction;
1066 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1069 void turtle_reset(int trst, int srst)
1075 low_output |= nSRST;
1079 low_output &= ~nSRST;
1082 /* command "set data bits low byte" */
1084 BUFFER_ADD = low_output;
1085 BUFFER_ADD = low_direction;
1086 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1089 void comstick_reset(int trst, int srst)
1093 high_output &= ~nTRST;
1097 high_output |= nTRST;
1102 high_output &= ~nSRST;
1106 high_output |= nSRST;
1109 /* command "set data bits high byte" */
1111 BUFFER_ADD = high_output;
1112 BUFFER_ADD = high_direction;
1113 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1116 void stm32stick_reset(int trst, int srst)
1120 high_output &= ~nTRST;
1124 high_output |= nTRST;
1129 low_output &= ~nSRST;
1133 low_output |= nSRST;
1136 /* command "set data bits low byte" */
1138 BUFFER_ADD = low_output;
1139 BUFFER_ADD = low_direction;
1141 /* command "set data bits high byte" */
1143 BUFFER_ADD = high_output;
1144 BUFFER_ADD = high_direction;
1145 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1148 int ft2232_execute_queue()
1150 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1151 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1153 int scan_size; /* size of IR or DR scan */
1154 enum scan_type type;
1156 int predicted_size = 0;
1157 int require_send = 0;
1160 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1161 * that wasn't handled by a caller-provided error handler
1165 ft2232_buffer_size = 0;
1166 ft2232_expect_read = 0;
1168 /* blink, if the current layout has that feature */
1176 case JTAG_END_STATE:
1177 if (cmd->cmd.end_state->end_state != -1)
1178 ft2232_end_state(cmd->cmd.end_state->end_state);
1181 /* only send the maximum buffer size that FT2232C can handle */
1183 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1185 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1186 retval = ERROR_JTAG_QUEUE_FAILED;
1191 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1193 cur_state = TAP_TLR;
1195 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1198 #ifdef _DEBUG_JTAG_IO_
1199 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1203 /* only send the maximum buffer size that FT2232C can handle */
1205 if (cur_state != TAP_RTI)
1206 predicted_size += 3;
1207 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1208 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1209 predicted_size += 3;
1210 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1211 predicted_size += 3;
1212 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1214 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1215 retval = ERROR_JTAG_QUEUE_FAILED;
1219 if (cur_state != TAP_RTI)
1221 /* command "Clock Data to TMS/CS Pin (no Read)" */
1226 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1227 cur_state = TAP_RTI;
1230 i = cmd->cmd.runtest->num_cycles;
1233 /* command "Clock Data to TMS/CS Pin (no Read)" */
1236 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1239 cur_state = TAP_RTI;
1240 i -= (i > 7) ? 7 : i;
1241 /* LOG_DEBUG("added TMS scan (no read)"); */
1243 if (cmd->cmd.runtest->end_state != -1)
1244 ft2232_end_state(cmd->cmd.runtest->end_state);
1245 if (cur_state != end_state)
1247 /* command "Clock Data to TMS/CS Pin (no Read)" */
1252 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1253 cur_state = end_state;
1254 /* LOG_DEBUG("added TMS scan (no read)"); */
1257 #ifdef _DEBUG_JTAG_IO_
1258 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1261 case JTAG_STATEMOVE:
1262 /* only send the maximum buffer size that FT2232C can handle */
1264 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1266 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1267 retval = ERROR_JTAG_QUEUE_FAILED;
1271 if (cmd->cmd.statemove->end_state != -1)
1272 ft2232_end_state(cmd->cmd.statemove->end_state);
1273 /* command "Clock Data to TMS/CS Pin (no Read)" */
1278 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1279 /* LOG_DEBUG("added TMS scan (no read)"); */
1280 cur_state = end_state;
1282 #ifdef _DEBUG_JTAG_IO_
1283 LOG_DEBUG("statemove: %i", end_state);
1287 /* only send the maximum buffer size that FT2232C can handle */
1288 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1289 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1291 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1292 retval = ERROR_JTAG_QUEUE_FAILED;
1296 ft2232_add_pathmove(cmd->cmd.pathmove);
1298 #ifdef _DEBUG_JTAG_IO_
1299 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1303 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1304 type = jtag_scan_type(cmd->cmd.scan);
1305 predicted_size = ft2232_predict_scan_out(scan_size, type);
1306 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1308 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1309 /* unsent commands before this */
1310 if (first_unsent != cmd)
1311 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1312 retval = ERROR_JTAG_QUEUE_FAILED;
1314 /* current command */
1315 if (cmd->cmd.scan->end_state != -1)
1316 ft2232_end_state(cmd->cmd.scan->end_state);
1317 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1319 first_unsent = cmd->next;
1324 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1326 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1327 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1328 retval = ERROR_JTAG_QUEUE_FAILED;
1332 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1333 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1334 if (cmd->cmd.scan->end_state != -1)
1335 ft2232_end_state(cmd->cmd.scan->end_state);
1336 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1340 #ifdef _DEBUG_JTAG_IO_
1341 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1345 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1346 retval = ERROR_JTAG_QUEUE_FAILED;
1347 first_unsent = cmd->next;
1348 jtag_sleep(cmd->cmd.sleep->us);
1349 #ifdef _DEBUG_JTAG_IO_
1350 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1354 LOG_ERROR("BUG: unknown JTAG command type encountered");
1360 if (require_send > 0)
1361 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1362 retval = ERROR_JTAG_QUEUE_FAILED;
1367 #if BUILD_FT2232_FTD2XX == 1
1368 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1371 DWORD openex_flags = 0;
1372 char *openex_string = NULL;
1375 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1376 ft2232_layout, vid, pid);
1379 /* Add non-standard Vid/Pid to the linux driver */
1380 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1382 LOG_WARNING("couldn't add %4.4x:%4.4x",
1387 if (ft2232_device_desc && ft2232_serial)
1389 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1390 ft2232_device_desc = NULL;
1393 if (ft2232_device_desc)
1395 openex_string = ft2232_device_desc;
1396 openex_flags = FT_OPEN_BY_DESCRIPTION;
1398 else if (ft2232_serial)
1400 openex_string = ft2232_serial;
1401 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1405 LOG_ERROR("neither device description nor serial number specified");
1406 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1408 return ERROR_JTAG_INIT_FAILED;
1411 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1416 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1419 return ERROR_JTAG_INIT_FAILED;
1421 LOG_ERROR("unable to open ftdi device: %lu", status);
1422 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1423 if (status == FT_OK)
1425 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1428 for (i = 0; i < num_devices; i++)
1429 desc_array[i] = malloc(64);
1430 desc_array[num_devices] = NULL;
1432 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1434 if (status == FT_OK)
1436 LOG_ERROR("ListDevices: %lu\n", num_devices);
1437 for (i = 0; i < num_devices; i++)
1438 LOG_ERROR("%i: %s", i, desc_array[i]);
1441 for (i = 0; i < num_devices; i++)
1442 free(desc_array[i]);
1447 LOG_ERROR("ListDevices: NONE\n");
1449 return ERROR_JTAG_INIT_FAILED;
1452 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1454 LOG_ERROR("unable to set latency timer: %lu", status);
1455 return ERROR_JTAG_INIT_FAILED;
1458 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1460 LOG_ERROR("unable to get latency timer: %lu", status);
1461 return ERROR_JTAG_INIT_FAILED;
1465 LOG_DEBUG("current latency timer: %i", latency_timer);
1468 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1470 LOG_ERROR("unable to set timeouts: %lu", status);
1471 return ERROR_JTAG_INIT_FAILED;
1474 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1476 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1477 return ERROR_JTAG_INIT_FAILED;
1483 static int ft2232_purge_ftd2xx(void)
1487 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1489 LOG_ERROR("error purging ftd2xx device: %lu", status);
1490 return ERROR_JTAG_INIT_FAILED;
1495 #endif /* BUILD_FT2232_FTD2XX == 1 */
1497 #if BUILD_FT2232_LIBFTDI == 1
1498 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1502 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1503 ft2232_layout, vid, pid);
1505 if (ftdi_init(&ftdic) < 0)
1506 return ERROR_JTAG_INIT_FAILED;
1508 /* context, vendor id, product id */
1509 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1510 ft2232_serial) < 0) {
1512 LOG_WARNING("unable to open ftdi device (trying more): %s",
1515 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1517 return ERROR_JTAG_INIT_FAILED;
1520 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1522 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1523 return ERROR_JTAG_INIT_FAILED;
1526 if (ftdi_usb_reset(&ftdic) < 0)
1528 LOG_ERROR("unable to reset ftdi device");
1529 return ERROR_JTAG_INIT_FAILED;
1532 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1534 LOG_ERROR("unable to set latency timer");
1535 return ERROR_JTAG_INIT_FAILED;
1538 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1540 LOG_ERROR("unable to get latency timer");
1541 return ERROR_JTAG_INIT_FAILED;
1545 LOG_DEBUG("current latency timer: %i", latency_timer);
1548 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1553 static int ft2232_purge_libftdi(void)
1555 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1557 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1558 return ERROR_JTAG_INIT_FAILED;
1563 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1565 int ft2232_init(void)
1570 ft2232_layout_t *cur_layout = ft2232_layouts;
1573 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1575 ft2232_layout = "usbjtag";
1576 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1579 while (cur_layout->name)
1581 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1583 layout = cur_layout;
1591 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1592 return ERROR_JTAG_INIT_FAILED;
1595 for (i = 0; 1; i++) {
1597 * "more indicates that there are more IDs to try, so we should
1598 * not print an error for an ID mismatch (but for anything
1601 * try_more indicates that the error code returned indicates an
1602 * ID mismatch (and nothing else) and that we should proceeed
1603 * with the next ID pair.
1605 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1608 #if BUILD_FT2232_FTD2XX == 1
1609 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1611 #elif BUILD_FT2232_LIBFTDI == 1
1612 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1617 if (!more || !try_more)
1621 ft2232_buffer_size = 0;
1622 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1624 if (layout->init() != ERROR_OK)
1625 return ERROR_JTAG_INIT_FAILED;
1627 ft2232_speed(jtag_speed);
1629 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1630 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1632 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1633 return ERROR_JTAG_INIT_FAILED;
1636 #if BUILD_FT2232_FTD2XX == 1
1637 return ft2232_purge_ftd2xx();
1638 #elif BUILD_FT2232_LIBFTDI == 1
1639 return ft2232_purge_libftdi();
1645 int usbjtag_init(void)
1651 low_direction = 0x0b;
1653 if (strcmp(ft2232_layout, "usbjtag") == 0)
1660 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1667 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1674 low_direction = 0x8b;
1678 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1679 return ERROR_JTAG_INIT_FAILED;
1682 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1684 low_direction &= ~nTRSTnOE; /* nTRST input */
1685 low_output &= ~nTRST; /* nTRST = 0 */
1689 low_direction |= nTRSTnOE; /* nTRST output */
1690 low_output |= nTRST; /* nTRST = 1 */
1693 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1695 low_direction |= nSRSTnOE; /* nSRST output */
1696 low_output |= nSRST; /* nSRST = 1 */
1700 low_direction &= ~nSRSTnOE; /* nSRST input */
1701 low_output &= ~nSRST; /* nSRST = 0 */
1704 /* initialize low byte for jtag */
1705 buf[0] = 0x80; /* command "set data bits low byte" */
1706 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1707 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1708 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1710 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1712 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1713 return ERROR_JTAG_INIT_FAILED;
1719 int jtagkey_init(void)
1725 low_direction = 0x1b;
1727 /* initialize low byte for jtag */
1728 buf[0] = 0x80; /* command "set data bits low byte" */
1729 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1730 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1731 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1733 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1735 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1736 return ERROR_JTAG_INIT_FAILED;
1739 if (strcmp(layout->name, "jtagkey") == 0)
1746 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1747 (strcmp(layout->name, "oocdlink") == 0))
1756 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1761 high_direction = 0x0f;
1763 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1765 high_output |= nTRSTnOE;
1766 high_output &= ~nTRST;
1770 high_output &= ~nTRSTnOE;
1771 high_output |= nTRST;
1774 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1776 high_output &= ~nSRSTnOE;
1777 high_output |= nSRST;
1781 high_output |= nSRSTnOE;
1782 high_output &= ~nSRST;
1785 /* initialize high port */
1786 buf[0] = 0x82; /* command "set data bits high byte" */
1787 buf[1] = high_output; /* value */
1788 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1789 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1791 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1793 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1794 return ERROR_JTAG_INIT_FAILED;
1800 int olimex_jtag_init(void)
1806 low_direction = 0x1b;
1808 /* initialize low byte for jtag */
1809 buf[0] = 0x80; /* command "set data bits low byte" */
1810 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1811 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1812 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1814 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1816 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1817 return ERROR_JTAG_INIT_FAILED;
1823 nSRSTnOE = 0x00; /* no output enable for nSRST */
1826 high_direction = 0x0f;
1828 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1830 high_output |= nTRSTnOE;
1831 high_output &= ~nTRST;
1835 high_output &= ~nTRSTnOE;
1836 high_output |= nTRST;
1839 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1841 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1845 high_output &= ~nSRST;
1848 /* turn red LED on */
1849 high_output |= 0x08;
1851 /* initialize high port */
1852 buf[0] = 0x82; /* command "set data bits high byte" */
1853 buf[1] = high_output; /* value */
1854 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1855 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1857 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1859 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1860 return ERROR_JTAG_INIT_FAILED;
1866 int flyswatter_init(void)
1872 low_direction = 0xfb;
1874 /* initialize low byte for jtag */
1875 buf[0] = 0x80; /* command "set data bits low byte" */
1876 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1877 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1878 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1880 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1882 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1883 return ERROR_JTAG_INIT_FAILED;
1887 nTRSTnOE = 0x0; /* not output enable for nTRST */
1889 nSRSTnOE = 0x00; /* no output enable for nSRST */
1892 high_direction = 0x0c;
1894 /* turn red LED1 on, LED2 off */
1895 high_output |= 0x08;
1897 /* initialize high port */
1898 buf[0] = 0x82; /* command "set data bits high byte" */
1899 buf[1] = high_output; /* value */
1900 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1901 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1903 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1905 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1906 return ERROR_JTAG_INIT_FAILED;
1912 int turtle_init(void)
1918 low_direction = 0x5b;
1920 /* initialize low byte for jtag */
1921 buf[0] = 0x80; /* command "set data bits low byte" */
1922 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1923 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1924 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1926 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1928 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1929 return ERROR_JTAG_INIT_FAILED;
1935 high_direction = 0x0C;
1937 /* initialize high port */
1938 buf[0] = 0x82; /* command "set data bits high byte" */
1939 buf[1] = high_output;
1940 buf[2] = high_direction;
1941 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1943 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1945 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1946 return ERROR_JTAG_INIT_FAILED;
1952 int comstick_init(void)
1958 low_direction = 0x0b;
1960 /* initialize low byte for jtag */
1961 buf[0] = 0x80; /* command "set data bits low byte" */
1962 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1963 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1964 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1966 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1968 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1969 return ERROR_JTAG_INIT_FAILED;
1973 nTRSTnOE = 0x00; /* no output enable for nTRST */
1975 nSRSTnOE = 0x00; /* no output enable for nSRST */
1978 high_direction = 0x03;
1980 /* initialize high port */
1981 buf[0] = 0x82; /* command "set data bits high byte" */
1982 buf[1] = high_output;
1983 buf[2] = high_direction;
1984 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1986 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1988 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
1989 return ERROR_JTAG_INIT_FAILED;
1995 int stm32stick_init(void)
2001 low_direction = 0x8b;
2003 /* initialize low byte for jtag */
2004 buf[0] = 0x80; /* command "set data bits low byte" */
2005 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2006 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2007 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2009 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2011 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2012 return ERROR_JTAG_INIT_FAILED;
2016 nTRSTnOE = 0x00; /* no output enable for nTRST */
2018 nSRSTnOE = 0x00; /* no output enable for nSRST */
2021 high_direction = 0x03;
2023 /* initialize high port */
2024 buf[0] = 0x82; /* command "set data bits high byte" */
2025 buf[1] = high_output;
2026 buf[2] = high_direction;
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 'stm32stick' layout");
2032 return ERROR_JTAG_INIT_FAILED;
2038 void olimex_jtag_blink(void)
2040 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2041 * ACBUS3 is bit 3 of the GPIOH port
2043 if (high_output & 0x08)
2045 /* set port pin high */
2046 high_output &= 0x07;
2050 /* set port pin low */
2051 high_output |= 0x08;
2055 BUFFER_ADD = high_output;
2056 BUFFER_ADD = high_direction;
2059 void turtle_jtag_blink(void)
2062 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2064 if (high_output & 0x08)
2074 BUFFER_ADD = high_output;
2075 BUFFER_ADD = high_direction;
2079 int ft2232_quit(void)
2081 #if BUILD_FT2232_FTD2XX == 1
2084 status = FT_Close(ftdih);
2085 #elif BUILD_FT2232_LIBFTDI == 1
2086 ftdi_disable_bitbang(&ftdic);
2088 ftdi_usb_close(&ftdic);
2090 ftdi_deinit(&ftdic);
2093 free(ft2232_buffer);
2094 ft2232_buffer = NULL;
2099 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2103 ft2232_device_desc = strdup(args[0]);
2107 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2113 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2117 ft2232_serial = strdup(args[0]);
2121 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2127 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2132 ft2232_layout = malloc(strlen(args[0]) + 1);
2133 strcpy(ft2232_layout, args[0]);
2138 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2142 if (argc > MAX_USB_IDS*2) {
2143 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2144 "(maximum is %d pairs)", MAX_USB_IDS);
2145 argc = MAX_USB_IDS*2;
2147 if (argc < 2 || (argc & 1))
2149 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2154 for (i = 0; i+1 < argc; i += 2) {
2155 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2156 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2159 * Explicitly terminate, in case there are multiples instances of
2162 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2167 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2171 ft2232_latency = atoi(args[0]);
2175 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");