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 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
43 /* FT2232 access library includes */
44 #if BUILD_FT2232_FTD2XX == 1
46 #elif BUILD_FT2232_LIBFTDI == 1
53 /* enable this to debug io latency
56 #define _DEBUG_USB_IO_
59 /* enable this to debug communication
62 #define _DEBUG_USB_COMMS_
65 int ft2232_execute_queue(void);
67 int ft2232_speed(int speed);
68 int ft2232_register_commands(struct command_context_s *cmd_ctx);
69 int ft2232_init(void);
70 int ft2232_quit(void);
72 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 char *ft2232_device_desc = NULL;
78 char *ft2232_serial = NULL;
79 char *ft2232_layout = NULL;
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);
99 int turtle_init(void);
101 /* reset procedures for supported layouts */
102 void usbjtag_reset(int trst, int srst);
103 void jtagkey_reset(int trst, int srst);
104 void olimex_jtag_reset(int trst, int srst);
105 void m5960_reset(int trst, int srst);
106 void turtle_reset(int trst, int srst);
108 /* blink procedures for layouts that support a blinking led */
109 void olimex_jtag_blink(void);
110 void turtle_jtag_blink(void);
112 ft2232_layout_t ft2232_layouts[] =
114 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
115 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
116 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
117 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
118 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
119 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
120 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
121 {"m5960", m5960_init, m5960_reset, NULL},
122 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
126 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
128 static ft2232_layout_t *layout;
129 static u8 low_output = 0x0;
130 static u8 low_direction = 0x0;
131 static u8 high_output = 0x0;
132 static u8 high_direction = 0x0;
134 #if BUILD_FT2232_FTD2XX == 1
135 static FT_HANDLE ftdih = NULL;
136 #elif BUILD_FT2232_LIBFTDI == 1
137 static struct ftdi_context ftdic;
140 static u8 *ft2232_buffer = NULL;
141 static int ft2232_buffer_size = 0;
142 static int ft2232_read_pointer = 0;
143 static int ft2232_expect_read = 0;
144 #define FT2232_BUFFER_SIZE 131072
145 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
146 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
148 jtag_interface_t ft2232_interface =
153 .execute_queue = ft2232_execute_queue,
155 .support_pathmove = 1,
157 .speed = ft2232_speed,
158 .register_commands = ft2232_register_commands,
163 int ft2232_write(u8 *buf, int size, u32* bytes_written)
165 #if BUILD_FT2232_FTD2XX == 1
167 DWORD dw_bytes_written;
168 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
170 *bytes_written = dw_bytes_written;
171 ERROR("FT_Write returned: %lu", status);
172 return ERROR_JTAG_DEVICE_ERROR;
176 *bytes_written = dw_bytes_written;
179 #elif BUILD_FT2232_LIBFTDI == 1
181 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
184 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
185 return ERROR_JTAG_DEVICE_ERROR;
189 *bytes_written = retval;
195 int ft2232_read(u8* buf, int size, u32* bytes_read)
197 #if BUILD_FT2232_FTD2XX == 1
203 while ((*bytes_read < size) && timeout--)
205 if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
208 ERROR("FT_Read returned: %lu", status);
209 return ERROR_JTAG_DEVICE_ERROR;
211 *bytes_read += dw_bytes_read;
213 #elif BUILD_FT2232_LIBFTDI == 1
218 while ((*bytes_read < size) && timeout--)
220 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
223 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
224 return ERROR_JTAG_DEVICE_ERROR;
226 *bytes_read += retval;
230 if (*bytes_read < size)
232 ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
233 return ERROR_JTAG_DEVICE_ERROR;
239 int ft2232_speed(int speed)
245 buf[0] = 0x86; /* command "set divisor" */
246 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
247 buf[2] = (speed >> 8) & 0xff; /* valueH */
249 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
250 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
252 ERROR("couldn't set FT2232 TCK speed");
259 int ft2232_register_commands(struct command_context_s *cmd_ctx)
261 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
262 COMMAND_CONFIG, NULL);
263 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
264 COMMAND_CONFIG, NULL);
265 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
266 COMMAND_CONFIG, NULL);
267 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
268 COMMAND_CONFIG, NULL);
272 void ft2232_end_state(state)
274 if (tap_move_map[state] != -1)
278 ERROR("BUG: %i is not a valid end state", state);
283 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
285 int num_bytes = ((scan_size + 7) / 8);
286 int bits_left = scan_size;
289 while(num_bytes-- > 1)
291 buffer[cur_byte] = BUFFER_READ;
296 buffer[cur_byte] = 0x0;
300 buffer[cur_byte] = BUFFER_READ >> 1;
303 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
307 void ft2232_debug_dump_buffer(void)
313 for (i = 0; i < ft2232_buffer_size; i++)
315 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
327 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
337 #ifdef _DEBUG_USB_IO_
338 struct timeval start, inter, inter2, end;
339 struct timeval d_inter, d_inter2, d_end;
342 #ifdef _DEBUG_USB_COMMS_
343 DEBUG("write buffer (size %i):", ft2232_buffer_size);
344 ft2232_debug_dump_buffer();
347 #ifdef _DEBUG_USB_IO_
348 gettimeofday(&start, NULL);
351 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
353 ERROR("couldn't write MPSSE commands to FT2232");
357 #ifdef _DEBUG_USB_IO_
358 gettimeofday(&inter, NULL);
361 if (ft2232_expect_read)
364 ft2232_buffer_size = 0;
366 #ifdef _DEBUG_USB_IO_
367 gettimeofday(&inter2, NULL);
370 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
372 ERROR("couldn't read from FT2232");
376 #ifdef _DEBUG_USB_IO_
377 gettimeofday(&end, NULL);
379 timeval_subtract(&d_inter, &inter, &start);
380 timeval_subtract(&d_inter2, &inter2, &start);
381 timeval_subtract(&d_end, &end, &start);
383 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);
387 ft2232_buffer_size = bytes_read;
389 if (ft2232_expect_read != ft2232_buffer_size)
391 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
392 ft2232_debug_dump_buffer();
397 #ifdef _DEBUG_USB_COMMS_
398 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
399 ft2232_debug_dump_buffer();
403 ft2232_expect_read = 0;
404 ft2232_read_pointer = 0;
406 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
407 * that wasn't handled by a caller-provided error handler
417 type = jtag_scan_type(cmd->cmd.scan);
418 if (type != SCAN_OUT)
420 scan_size = jtag_scan_size(cmd->cmd.scan);
421 buffer = calloc(CEIL(scan_size, 8), 1);
422 ft2232_read_scan(type, buffer, scan_size);
423 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
424 retval = ERROR_JTAG_QUEUE_FAILED;
434 ft2232_buffer_size = 0;
439 void ft2232_add_pathmove(pathmove_command_t *cmd)
441 int num_states = cmd->num_states;
451 /* command "Clock Data to TMS/CS Pin (no Read)" */
453 /* number of states remaining */
454 BUFFER_ADD = (num_states % 7) - 1;
456 while (num_states % 7)
458 if (tap_transitions[cur_state].low == cmd->path[state_count])
459 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
460 else if (tap_transitions[cur_state].high == cmd->path[state_count])
461 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
464 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
468 cur_state = cmd->path[state_count];
473 BUFFER_ADD = tms_byte;
476 end_state = cur_state;
479 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
481 int num_bytes = (scan_size + 7) / 8;
482 int bits_left = scan_size;
486 if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
488 /* command "Clock Data to TMS/CS Pin (no Read)" */
495 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
500 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
503 //DEBUG("added TMS scan (no read)");
506 /* add command for complete bytes */
507 while (num_bytes > 1)
512 /* Clock Data Bytes In and Out LSB First */
514 //DEBUG("added TDI bytes (io %i)", num_bytes);
516 else if (type == SCAN_OUT)
518 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
520 //DEBUG("added TDI bytes (o)");
522 else if (type == SCAN_IN)
524 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
526 //DEBUG("added TDI bytes (i %i)", num_bytes);
528 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
529 num_bytes -= thisrun_bytes;
530 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
531 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
534 /* add complete bytes */
535 while(thisrun_bytes-- > 0)
537 BUFFER_ADD = buffer[cur_byte];
542 else /* (type == SCAN_IN) */
544 bits_left -= 8 * (thisrun_bytes);
548 /* the most signifcant bit is scanned during TAP movement */
550 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
554 /* process remaining bits but the last one */
559 /* Clock Data Bits In and Out LSB First */
561 //DEBUG("added TDI bits (io) %i", bits_left - 1);
563 else if (type == SCAN_OUT)
565 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
567 //DEBUG("added TDI bits (o)");
569 else if (type == SCAN_IN)
571 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
573 //DEBUG("added TDI bits (i %i)", bits_left - 1);
575 BUFFER_ADD = bits_left - 2;
577 BUFFER_ADD = buffer[cur_byte];
580 if ((ir_scan && (end_state == TAP_SI)) ||
581 (!ir_scan && (end_state == TAP_SD)))
585 /* Clock Data Bits In and Out LSB First */
587 //DEBUG("added TDI bits (io) %i", bits_left - 1);
589 else if (type == SCAN_OUT)
591 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
593 //DEBUG("added TDI bits (o)");
595 else if (type == SCAN_IN)
597 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
599 //DEBUG("added TDI bits (i %i)", bits_left - 1);
602 BUFFER_ADD = last_bit;
606 /* move from Shift-IR/DR to end state */
607 if (type != SCAN_OUT)
609 /* Clock Data to TMS/CS Pin with Read */
611 //DEBUG("added TMS scan (read)");
615 /* Clock Data to TMS/CS Pin (no Read) */
617 //DEBUG("added TMS scan (no read)");
620 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
621 cur_state = end_state;
625 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
627 int num_bytes = (scan_size + 7) / 8;
628 int bits_left = scan_size;
631 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
632 u8 *receive_pointer = receive_buffer;
636 int thisrun_read = 0;
640 ERROR("BUG: large IR scans are not supported");
644 if (cur_state != TAP_SD)
646 /* command "Clock Data to TMS/CS Pin (no Read)" */
651 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
655 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
657 ERROR("couldn't write MPSSE commands to FT2232");
660 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
661 ft2232_buffer_size = 0;
663 /* add command for complete bytes */
664 while (num_bytes > 1)
670 /* Clock Data Bytes In and Out LSB First */
672 //DEBUG("added TDI bytes (io %i)", num_bytes);
674 else if (type == SCAN_OUT)
676 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
678 //DEBUG("added TDI bytes (o)");
680 else if (type == SCAN_IN)
682 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
684 //DEBUG("added TDI bytes (i %i)", num_bytes);
686 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
687 thisrun_read = thisrun_bytes;
688 num_bytes -= thisrun_bytes;
689 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
690 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
693 /* add complete bytes */
694 while(thisrun_bytes-- > 0)
696 BUFFER_ADD = buffer[cur_byte];
701 else /* (type == SCAN_IN) */
703 bits_left -= 8 * (thisrun_bytes);
706 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
708 ERROR("couldn't write MPSSE commands to FT2232");
711 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
712 ft2232_buffer_size = 0;
714 if (type != SCAN_OUT)
716 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
718 ERROR("couldn't read from FT2232");
721 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
722 receive_pointer += bytes_read;
728 /* the most signifcant bit is scanned during TAP movement */
730 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
734 /* process remaining bits but the last one */
739 /* Clock Data Bits In and Out LSB First */
741 //DEBUG("added TDI bits (io) %i", bits_left - 1);
743 else if (type == SCAN_OUT)
745 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
747 //DEBUG("added TDI bits (o)");
749 else if (type == SCAN_IN)
751 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
753 //DEBUG("added TDI bits (i %i)", bits_left - 1);
755 BUFFER_ADD = bits_left - 2;
757 BUFFER_ADD = buffer[cur_byte];
759 if (type != SCAN_OUT)
763 if (end_state == TAP_SD)
767 /* Clock Data Bits In and Out LSB First */
769 //DEBUG("added TDI bits (io) %i", bits_left - 1);
771 else if (type == SCAN_OUT)
773 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
775 //DEBUG("added TDI bits (o)");
777 else if (type == SCAN_IN)
779 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
781 //DEBUG("added TDI bits (i %i)", bits_left - 1);
784 BUFFER_ADD = last_bit;
788 /* move from Shift-IR/DR to end state */
789 if (type != SCAN_OUT)
791 /* Clock Data to TMS/CS Pin with Read */
793 //DEBUG("added TMS scan (read)");
797 /* Clock Data to TMS/CS Pin (no Read) */
799 //DEBUG("added TMS scan (no read)");
802 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
803 cur_state = end_state;
806 if (type != SCAN_OUT)
809 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
811 ERROR("couldn't write MPSSE commands to FT2232");
814 DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
815 ft2232_buffer_size = 0;
817 if (type != SCAN_OUT)
819 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
821 ERROR("couldn't read from FT2232");
824 DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
825 receive_pointer += bytes_read;
831 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
833 int predicted_size = 3;
834 int num_bytes = (scan_size - 1) / 8;
836 if (cur_state != TAP_SD)
839 if (type == SCAN_IN) /* only from device to host */
842 predicted_size += (CEIL(num_bytes, 65536)) * 3;
843 /* remaining bits - 1 (up to 7) */
844 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
846 else /* host to device, or bidirectional */
849 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
850 /* remaining bits -1 (up to 7) */
851 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
854 return predicted_size;
857 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
859 int predicted_size = 0;
861 if (type != SCAN_OUT)
864 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
865 /* remaining bits - 1 */
866 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
867 /* last bit (from TMS scan) */
871 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
873 return predicted_size;
876 void usbjtag_reset(int trst, int srst)
881 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
882 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
884 low_output &= ~nTRST; /* switch output low */
888 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
889 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
891 low_output |= nTRST; /* switch output high */
896 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
897 low_output &= ~nSRST; /* switch output low */
899 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
903 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
904 low_output |= nSRST; /* switch output high */
906 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
909 /* command "set data bits low byte" */
911 BUFFER_ADD = low_output;
912 BUFFER_ADD = low_direction;
916 void jtagkey_reset(int trst, int srst)
921 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
922 high_output &= ~nTRSTnOE;
924 high_output &= ~nTRST;
928 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
929 high_output |= nTRSTnOE;
931 high_output |= nTRST;
936 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
937 high_output &= ~nSRST;
939 high_output &= ~nSRSTnOE;
943 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
944 high_output |= nSRST;
946 high_output |= nSRSTnOE;
949 /* command "set data bits high byte" */
951 BUFFER_ADD = high_output;
952 BUFFER_ADD = high_direction;
953 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
956 void olimex_jtag_reset(int trst, int srst)
961 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
962 high_output &= ~nTRSTnOE;
964 high_output &= ~nTRST;
968 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
969 high_output |= nTRSTnOE;
971 high_output |= nTRST;
976 high_output |= nSRST;
980 high_output &= ~nSRST;
983 /* command "set data bits high byte" */
985 BUFFER_ADD = high_output;
986 BUFFER_ADD = high_direction;
987 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
990 void m5960_reset(int trst, int srst)
995 low_output &= ~nTRST;
1004 low_output |= nSRST;
1008 low_output &= ~nSRST;
1011 /* command "set data bits low byte" */
1013 BUFFER_ADD = low_output;
1014 BUFFER_ADD = low_direction;
1015 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1018 void turtle_reset(int trst, int srst)
1024 low_output |= nSRST;
1028 low_output &= ~nSRST;
1031 /* command "set data bits high byte" */
1033 BUFFER_ADD = low_output;
1034 BUFFER_ADD = low_direction;
1035 DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1038 int ft2232_execute_queue()
1040 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1041 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1043 int scan_size; /* size of IR or DR scan */
1044 enum scan_type type;
1046 int predicted_size = 0;
1047 int require_send = 0;
1050 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1051 * that wasn't handled by a caller-provided error handler
1055 ft2232_buffer_size = 0;
1056 ft2232_expect_read = 0;
1058 /* blink, if the current layout has that feature */
1066 case JTAG_END_STATE:
1067 if (cmd->cmd.end_state->end_state != -1)
1068 ft2232_end_state(cmd->cmd.end_state->end_state);
1071 /* only send the maximum buffer size that FT2232C can handle */
1073 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1075 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1076 retval = ERROR_JTAG_QUEUE_FAILED;
1081 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1084 #ifdef _DEBUG_JTAG_IO_
1085 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1089 /* only send the maximum buffer size that FT2232C can handle */
1091 if (cur_state != TAP_RTI)
1092 predicted_size += 3;
1093 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1094 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
1095 predicted_size += 3;
1096 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
1097 predicted_size += 3;
1098 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1100 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1101 retval = ERROR_JTAG_QUEUE_FAILED;
1105 if (cur_state != TAP_RTI)
1107 /* command "Clock Data to TMS/CS Pin (no Read)" */
1112 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
1113 cur_state = TAP_RTI;
1116 i = cmd->cmd.runtest->num_cycles;
1119 /* command "Clock Data to TMS/CS Pin (no Read)" */
1122 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1125 cur_state = TAP_RTI;
1126 i -= (i > 7) ? 7 : i;
1127 //DEBUG("added TMS scan (no read)");
1129 if (cmd->cmd.runtest->end_state != -1)
1130 ft2232_end_state(cmd->cmd.runtest->end_state);
1131 if (cur_state != end_state)
1133 /* command "Clock Data to TMS/CS Pin (no Read)" */
1138 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1139 cur_state = end_state;
1140 //DEBUG("added TMS scan (no read)");
1143 #ifdef _DEBUG_JTAG_IO_
1144 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1147 case JTAG_STATEMOVE:
1148 /* only send the maximum buffer size that FT2232C can handle */
1150 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1152 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1153 retval = ERROR_JTAG_QUEUE_FAILED;
1157 if (cmd->cmd.statemove->end_state != -1)
1158 ft2232_end_state(cmd->cmd.statemove->end_state);
1159 /* command "Clock Data to TMS/CS Pin (no Read)" */
1164 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1165 //DEBUG("added TMS scan (no read)");
1166 cur_state = end_state;
1168 #ifdef _DEBUG_JTAG_IO_
1169 DEBUG("statemove: %i", end_state);
1173 /* only send the maximum buffer size that FT2232C can handle */
1174 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1175 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1177 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1178 retval = ERROR_JTAG_QUEUE_FAILED;
1182 ft2232_add_pathmove(cmd->cmd.pathmove);
1184 #ifdef _DEBUG_JTAG_IO_
1185 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1189 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1190 type = jtag_scan_type(cmd->cmd.scan);
1191 predicted_size = ft2232_predict_scan_out(scan_size, type);
1192 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1194 DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1195 /* unsent commands before this */
1196 if (first_unsent != cmd)
1197 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1198 retval = ERROR_JTAG_QUEUE_FAILED;
1200 /* current command */
1201 if (cmd->cmd.scan->end_state != -1)
1202 ft2232_end_state(cmd->cmd.scan->end_state);
1203 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1205 first_unsent = cmd->next;
1210 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1212 DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1213 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1214 retval = ERROR_JTAG_QUEUE_FAILED;
1218 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1219 //DEBUG("new read size: %i", ft2232_expect_read);
1220 if (cmd->cmd.scan->end_state != -1)
1221 ft2232_end_state(cmd->cmd.scan->end_state);
1222 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1226 #ifdef _DEBUG_JTAG_IO_
1227 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1231 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1232 retval = ERROR_JTAG_QUEUE_FAILED;
1233 first_unsent = cmd->next;
1234 jtag_sleep(cmd->cmd.sleep->us);
1235 #ifdef _DEBUG_JTAG_IO_
1236 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1240 ERROR("BUG: unknown JTAG command type encountered");
1246 if (require_send > 0)
1247 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1248 retval = ERROR_JTAG_QUEUE_FAILED;
1253 #if BUILD_FT2232_FTD2XX == 1
1254 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1257 DWORD openex_flags = 0;
1258 char *openex_string = NULL;
1261 DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1262 ft2232_layout, vid, pid);
1265 /* Add non-standard Vid/Pid to the linux driver */
1266 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1268 WARNING("couldn't add %4.4x:%4.4x",
1273 if (ft2232_device_desc && ft2232_serial)
1275 WARNING("can't open by device description and serial number, giving precedence to serial");
1276 ft2232_device_desc = NULL;
1279 if (ft2232_device_desc)
1281 openex_string = ft2232_device_desc;
1282 openex_flags = FT_OPEN_BY_DESCRIPTION;
1284 else if (ft2232_serial)
1286 openex_string = ft2232_serial;
1287 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1291 ERROR("neither device description nor serial number specified");
1292 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1294 return ERROR_JTAG_INIT_FAILED;
1297 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1302 WARNING("unable to open ftdi device (trying more): %lu",
1305 return ERROR_JTAG_INIT_FAILED;
1307 ERROR("unable to open ftdi device: %lu", status);
1308 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1309 if (status == FT_OK)
1311 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1314 for (i = 0; i < num_devices; i++)
1315 desc_array[i] = malloc(64);
1316 desc_array[num_devices] = NULL;
1318 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1320 if (status == FT_OK)
1322 ERROR("ListDevices: %lu\n", num_devices);
1323 for (i = 0; i < num_devices; i++)
1324 ERROR("%i: %s", i, desc_array[i]);
1327 for (i = 0; i < num_devices; i++)
1328 free(desc_array[i]);
1333 printf("ListDevices: NONE\n");
1335 return ERROR_JTAG_INIT_FAILED;
1338 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
1340 ERROR("unable to set latency timer: %lu", status);
1341 return ERROR_JTAG_INIT_FAILED;
1344 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1346 ERROR("unable to get latency timer: %lu", status);
1347 return ERROR_JTAG_INIT_FAILED;
1351 DEBUG("current latency timer: %i", latency_timer);
1354 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1356 ERROR("unable to set timeouts: %lu", status);
1357 return ERROR_JTAG_INIT_FAILED;
1360 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1362 ERROR("unable to enable bit i/o mode: %lu", status);
1363 return ERROR_JTAG_INIT_FAILED;
1369 static int ft2232_purge_ftd2xx(void)
1373 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1375 ERROR("error purging ftd2xx device: %lu", status);
1376 return ERROR_JTAG_INIT_FAILED;
1381 #endif /* BUILD_FT2232_FTD2XX == 1 */
1383 #if BUILD_FT2232_LIBFTDI == 1
1384 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1388 DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1389 ft2232_layout, vid, pid);
1391 if (ftdi_init(&ftdic) < 0)
1392 return ERROR_JTAG_INIT_FAILED;
1394 /* context, vendor id, product id */
1395 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1396 ft2232_serial) < 0) {
1398 WARNING("unable to open ftdi device (trying more): %s",
1401 ERROR("unable to open ftdi device: %s", ftdic.error_str);
1403 return ERROR_JTAG_INIT_FAILED;
1406 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1408 ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1409 return ERROR_JTAG_INIT_FAILED;
1412 if (ftdi_usb_reset(&ftdic) < 0)
1414 ERROR("unable to reset ftdi device");
1415 return ERROR_JTAG_INIT_FAILED;
1418 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
1420 ERROR("unable to set latency timer");
1421 return ERROR_JTAG_INIT_FAILED;
1424 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1426 ERROR("unable to get latency timer");
1427 return ERROR_JTAG_INIT_FAILED;
1431 DEBUG("current latency timer: %i", latency_timer);
1434 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1439 static int ft2232_purge_libftdi(void)
1441 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1443 ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1444 return ERROR_JTAG_INIT_FAILED;
1449 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1451 int ft2232_init(void)
1456 ft2232_layout_t *cur_layout = ft2232_layouts;
1459 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1461 ft2232_layout = "usbjtag";
1462 WARNING("No ft2232 layout specified, using default 'usbjtag'");
1465 while (cur_layout->name)
1467 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1469 layout = cur_layout;
1477 ERROR("No matching layout found for %s", ft2232_layout);
1478 return ERROR_JTAG_INIT_FAILED;
1481 for (i = 0; 1; i++) {
1483 * "more indicates that there are more IDs to try, so we should
1484 * not print an error for an ID mismatch (but for anything
1487 * try_more indicates that the error code returned indicates an
1488 * ID mismatch (and nothing else) and that we should proceeed
1489 * with the next ID pair.
1491 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1494 #if BUILD_FT2232_FTD2XX == 1
1495 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1497 #elif BUILD_FT2232_LIBFTDI == 1
1498 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1503 if (!more || !try_more)
1507 ft2232_buffer_size = 0;
1508 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1510 if (layout->init() != ERROR_OK)
1511 return ERROR_JTAG_INIT_FAILED;
1513 ft2232_speed(jtag_speed);
1515 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1516 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1518 ERROR("couldn't write to FT2232 to disable loopback");
1519 return ERROR_JTAG_INIT_FAILED;
1522 #if BUILD_FT2232_FTD2XX == 1
1523 return ft2232_purge_ftd2xx();
1524 #elif BUILD_FT2232_LIBFTDI == 1
1525 return ft2232_purge_libftdi();
1531 int usbjtag_init(void)
1537 low_direction = 0x0b;
1539 if (strcmp(ft2232_layout, "usbjtag") == 0)
1546 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1553 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1560 low_direction = 0x8b;
1564 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1565 return ERROR_JTAG_INIT_FAILED;
1568 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1570 low_direction &= ~nTRSTnOE; /* nTRST input */
1571 low_output &= ~nTRST; /* nTRST = 0 */
1575 low_direction |= nTRSTnOE; /* nTRST output */
1576 low_output |= nTRST; /* nTRST = 1 */
1579 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1581 low_direction |= nSRSTnOE; /* nSRST output */
1582 low_output |= nSRST; /* nSRST = 1 */
1586 low_direction &= ~nSRSTnOE; /* nSRST input */
1587 low_output &= ~nSRST; /* nSRST = 0 */
1590 /* initialize low byte for jtag */
1591 buf[0] = 0x80; /* command "set data bits low byte" */
1592 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1593 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1594 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1596 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1598 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1599 return ERROR_JTAG_INIT_FAILED;
1605 int jtagkey_init(void)
1611 low_direction = 0x1b;
1613 /* initialize low byte for jtag */
1614 buf[0] = 0x80; /* command "set data bits low byte" */
1615 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1616 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1617 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1619 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1621 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1622 return ERROR_JTAG_INIT_FAILED;
1625 if (strcmp(layout->name, "jtagkey") == 0)
1632 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1633 (strcmp(layout->name, "oocdlink") == 0))
1642 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1647 high_direction = 0x0f;
1649 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1651 high_output |= nTRSTnOE;
1652 high_output &= ~nTRST;
1656 high_output &= ~nTRSTnOE;
1657 high_output |= nTRST;
1660 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1662 high_output &= ~nSRSTnOE;
1663 high_output |= nSRST;
1667 high_output |= nSRSTnOE;
1668 high_output &= ~nSRST;
1671 /* initialize high port */
1672 buf[0] = 0x82; /* command "set data bits high byte" */
1673 buf[1] = high_output; /* value */
1674 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1675 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1677 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1679 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1680 return ERROR_JTAG_INIT_FAILED;
1686 int olimex_jtag_init(void)
1692 low_direction = 0x1b;
1694 /* initialize low byte for jtag */
1695 buf[0] = 0x80; /* command "set data bits low byte" */
1696 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1697 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1698 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1700 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1702 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1703 return ERROR_JTAG_INIT_FAILED;
1709 nSRSTnOE = 0x00; /* no output enable for nSRST */
1712 high_direction = 0x0f;
1714 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1716 high_output |= nTRSTnOE;
1717 high_output &= ~nTRST;
1721 high_output &= ~nTRSTnOE;
1722 high_output |= nTRST;
1725 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1727 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1731 high_output &= ~nSRST;
1734 /* turn red LED on */
1735 high_output |= 0x08;
1737 /* initialize high port */
1738 buf[0] = 0x82; /* command "set data bits high byte" */
1739 buf[1] = high_output; /* value */
1740 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1741 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1743 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1745 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1746 return ERROR_JTAG_INIT_FAILED;
1752 int m5960_init(void)
1758 low_direction = 0xfb;
1760 /* initialize low byte for jtag */
1761 buf[0] = 0x80; /* command "set data bits low byte" */
1762 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1763 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1764 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1766 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1768 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1769 return ERROR_JTAG_INIT_FAILED;
1773 nTRSTnOE = 0x0; /* not output enable for nTRST */
1775 nSRSTnOE = 0x00; /* no output enable for nSRST */
1778 high_direction = 0x0c;
1780 /* turn red LED1 on, LED2 off */
1781 high_output |= 0x08;
1783 /* initialize high port */
1784 buf[0] = 0x82; /* command "set data bits high byte" */
1785 buf[1] = high_output; /* value */
1786 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1787 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1789 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1791 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1792 return ERROR_JTAG_INIT_FAILED;
1798 int turtle_init(void)
1804 low_direction = 0x5b;
1806 /* initialize low byte for jtag */
1807 buf[0] = 0x80; /* command "set data bits low byte" */
1808 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1809 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1810 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1812 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1814 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1815 return ERROR_JTAG_INIT_FAILED;
1821 high_direction = 0x0C;
1823 /* initialize high port */
1824 buf[0] = 0x82; /* command "set data bits high byte" */
1825 buf[1] = high_output;
1826 buf[2] = high_direction;
1827 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1829 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1831 ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
1832 return ERROR_JTAG_INIT_FAILED;
1838 void olimex_jtag_blink(void)
1840 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1841 * ACBUS3 is bit 3 of the GPIOH port
1843 if (high_output & 0x08)
1845 /* set port pin high */
1846 high_output &= 0x07;
1850 /* set port pin low */
1851 high_output |= 0x08;
1855 BUFFER_ADD = high_output;
1856 BUFFER_ADD = high_direction;
1859 void turtle_jtag_blink(void)
1862 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
1864 if (high_output & 0x08)
1874 BUFFER_ADD = high_output;
1875 BUFFER_ADD = high_direction;
1879 int ft2232_quit(void)
1881 #if BUILD_FT2232_FTD2XX == 1
1884 status = FT_Close(ftdih);
1885 #elif BUILD_FT2232_LIBFTDI == 1
1886 ftdi_disable_bitbang(&ftdic);
1888 ftdi_usb_close(&ftdic);
1890 ftdi_deinit(&ftdic);
1893 free(ft2232_buffer);
1898 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1902 ft2232_device_desc = strdup(args[0]);
1906 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1912 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1916 ft2232_serial = strdup(args[0]);
1920 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
1926 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1931 ft2232_layout = malloc(strlen(args[0]) + 1);
1932 strcpy(ft2232_layout, args[0]);
1937 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1941 if (argc > MAX_USB_IDS*2) {
1942 WARNING("ignoring extra IDs in ft2232_vid_pid "
1943 "(maximum is %d pairs)", MAX_USB_IDS);
1944 argc = MAX_USB_IDS*2;
1946 if (argc < 2 || (argc & 1))
1948 WARNING("incomplete ft2232_vid_pid configuration directive");
1953 for (i = 0; i+1 < argc; i += 2) {
1954 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
1955 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
1958 * Explicitly terminate, in case there are multiples instances of
1961 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;