John McCarthy <jgmcc@magma.ca> formatting fix of debug output
[openocd.git] / src / jtag / ft2232.c
blobad5e63782486639ece2c697e2b3d7da463439c38
1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #if IS_CYGWIN == 1
28 #include "windows.h"
29 #endif
31 #include "replacements.h"
33 /* project specific includes */
34 #include "log.h"
35 #include "types.h"
36 #include "jtag.h"
37 #include "configuration.h"
38 #include "time_support.h"
40 /* system includes */
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
45 /* FT2232 access library includes */
46 #if BUILD_FT2232_FTD2XX == 1
47 #include <ftd2xx.h>
48 #elif BUILD_FT2232_LIBFTDI == 1
49 #include <ftdi.h>
50 #endif
52 /* enable this to debug io latency
54 #if 0
55 #define _DEBUG_USB_IO_
56 #endif
58 /* enable this to debug communication
60 #if 0
61 #define _DEBUG_USB_COMMS_
62 #endif
64 int ft2232_execute_queue(void);
66 int ft2232_speed(int speed);
67 int ft2232_speed_div(int speed, int *khz);
68 int ft2232_khz(int khz, int *jtag_speed);
69 int ft2232_register_commands(struct command_context_s *cmd_ctx);
70 int ft2232_init(void);
71 int ft2232_quit(void);
73 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 char *ft2232_device_desc = NULL;
80 char *ft2232_serial = NULL;
81 char *ft2232_layout = NULL;
82 unsigned char ft2232_latency = 2;
84 #define MAX_USB_IDS 8
85 /* vid = pid = 0 marks the end of the list */
86 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
87 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
89 typedef struct ft2232_layout_s
91 char* name;
92 int(*init)(void);
93 void(*reset)(int trst, int srst);
94 void(*blink)(void);
95 } ft2232_layout_t;
97 /* init procedures for supported layouts */
98 int usbjtag_init(void);
99 int jtagkey_init(void);
100 int olimex_jtag_init(void);
101 int flyswatter_init(void);
102 int turtle_init(void);
103 int comstick_init(void);
104 int stm32stick_init(void);
105 int axm0432_jtag_init(void);
107 /* reset procedures for supported layouts */
108 void usbjtag_reset(int trst, int srst);
109 void jtagkey_reset(int trst, int srst);
110 void olimex_jtag_reset(int trst, int srst);
111 void flyswatter_reset(int trst, int srst);
112 void turtle_reset(int trst, int srst);
113 void comstick_reset(int trst, int srst);
114 void stm32stick_reset(int trst, int srst);
115 void axm0432_jtag_reset(int trst, int srst);
117 /* blink procedures for layouts that support a blinking led */
118 void olimex_jtag_blink(void);
119 void turtle_jtag_blink(void);
121 ft2232_layout_t ft2232_layouts[] =
123 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
124 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
125 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
126 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
127 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
128 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
129 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
130 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
131 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
132 {"comstick", comstick_init, comstick_reset, NULL},
133 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
134 {"axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL},
135 {NULL, NULL, NULL},
138 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
140 static ft2232_layout_t *layout;
141 static u8 low_output = 0x0;
142 static u8 low_direction = 0x0;
143 static u8 high_output = 0x0;
144 static u8 high_direction = 0x0;
146 #if BUILD_FT2232_FTD2XX == 1
147 static FT_HANDLE ftdih = NULL;
148 #elif BUILD_FT2232_LIBFTDI == 1
149 static struct ftdi_context ftdic;
150 #endif
152 static u8 *ft2232_buffer = NULL;
153 static int ft2232_buffer_size = 0;
154 static int ft2232_read_pointer = 0;
155 static int ft2232_expect_read = 0;
156 #define FT2232_BUFFER_SIZE 131072
157 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
158 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
160 jtag_interface_t ft2232_interface =
162 .name = "ft2232",
163 .execute_queue = ft2232_execute_queue,
164 .speed = ft2232_speed,
165 .speed_div = ft2232_speed_div,
166 .khz = ft2232_khz,
167 .register_commands = ft2232_register_commands,
168 .init = ft2232_init,
169 .quit = ft2232_quit,
172 int ft2232_write(u8 *buf, int size, u32* bytes_written)
174 #if BUILD_FT2232_FTD2XX == 1
175 FT_STATUS status;
176 DWORD dw_bytes_written;
177 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
179 *bytes_written = dw_bytes_written;
180 LOG_ERROR("FT_Write returned: %lu", status);
181 return ERROR_JTAG_DEVICE_ERROR;
183 else
185 *bytes_written = dw_bytes_written;
186 return ERROR_OK;
188 #elif BUILD_FT2232_LIBFTDI == 1
189 int retval;
190 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
192 *bytes_written = 0;
193 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
194 return ERROR_JTAG_DEVICE_ERROR;
196 else
198 *bytes_written = retval;
199 return ERROR_OK;
201 #endif
204 int ft2232_read(u8* buf, int size, u32* bytes_read)
206 #if BUILD_FT2232_FTD2XX == 1
207 DWORD dw_bytes_read;
208 FT_STATUS status;
209 int timeout = 5;
210 *bytes_read = 0;
212 while ((*bytes_read < size) && timeout--)
214 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
215 *bytes_read, &dw_bytes_read)) != FT_OK)
217 *bytes_read = 0;
218 LOG_ERROR("FT_Read returned: %lu", status);
219 return ERROR_JTAG_DEVICE_ERROR;
221 *bytes_read += dw_bytes_read;
223 #elif BUILD_FT2232_LIBFTDI == 1
224 int retval;
225 int timeout = 100;
226 *bytes_read = 0;
228 while ((*bytes_read < size) && timeout--)
230 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
232 *bytes_read = 0;
233 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
234 return ERROR_JTAG_DEVICE_ERROR;
236 *bytes_read += retval;
238 #endif
240 if (*bytes_read < size)
242 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
243 return ERROR_JTAG_DEVICE_ERROR;
246 return ERROR_OK;
249 int ft2232_speed(int speed)
251 u8 buf[3];
252 int retval;
253 u32 bytes_written;
255 buf[0] = 0x86; /* command "set divisor" */
256 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
257 buf[2] = (speed >> 8) & 0xff; /* valueH */
259 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
260 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
262 LOG_ERROR("couldn't set FT2232 TCK speed");
263 return retval;
266 return ERROR_OK;
269 int ft2232_speed_div(int speed, int *khz)
271 /* Take a look in the FT2232 manual,
272 * AN2232C-01 Command Processor for
273 * MPSSE and MCU Host Bus. Chapter 3.8 */
275 *khz = 6000 / (1+speed);
277 return ERROR_OK;
280 int ft2232_khz(int khz, int *jtag_speed)
282 if (khz==0)
284 LOG_ERROR("RCLK not supported");
285 return ERROR_FAIL;
287 /* Take a look in the FT2232 manual,
288 * AN2232C-01 Command Processor for
289 * MPSSE and MCU Host Bus. Chapter 3.8
291 * We will calc here with a multiplier
292 * of 10 for better rounding later. */
294 /* Calc speed, (6000 / khz) - 1 */
295 /* Use 65000 for better rounding */
296 *jtag_speed = (60000 / khz) - 10;
298 /* Add 0.9 for rounding */
299 *jtag_speed += 9;
301 /* Calc real speed */
302 *jtag_speed = *jtag_speed / 10;
304 /* Check if speed is greater than 0 */
305 if (*jtag_speed < 0)
307 *jtag_speed = 0;
310 /* Check max value */
311 if (*jtag_speed > 0xFFFF)
313 *jtag_speed = 0xFFFF;
316 return ERROR_OK;
319 int ft2232_register_commands(struct command_context_s *cmd_ctx)
321 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
322 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
323 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
324 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
325 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
326 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
327 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
328 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
329 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
330 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
331 return ERROR_OK;
334 void ft2232_end_state(enum tap_state state)
336 if (tap_move_map[state] != -1)
337 end_state = state;
338 else
340 LOG_ERROR("BUG: %i is not a valid end state", state);
341 exit(-1);
345 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
347 int num_bytes = ((scan_size + 7) / 8);
348 int bits_left = scan_size;
349 int cur_byte = 0;
351 while(num_bytes-- > 1)
353 buffer[cur_byte] = BUFFER_READ;
354 cur_byte++;
355 bits_left -= 8;
358 buffer[cur_byte] = 0x0;
360 if (bits_left > 1)
362 buffer[cur_byte] = BUFFER_READ >> 1;
365 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
369 void ft2232_debug_dump_buffer(void)
371 int i;
372 char line[256];
373 char *line_p = line;
375 for (i = 0; i < ft2232_buffer_size; i++)
377 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
378 if (i % 16 == 15)
380 LOG_DEBUG("%s", line);
381 line_p = line;
385 if (line_p != line)
386 LOG_DEBUG("%s", line);
389 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
391 jtag_command_t *cmd;
392 u8 *buffer;
393 int scan_size;
394 enum scan_type type;
395 int retval;
396 u32 bytes_written;
397 u32 bytes_read;
399 #ifdef _DEBUG_USB_IO_
400 struct timeval start, inter, inter2, end;
401 struct timeval d_inter, d_inter2, d_end;
402 #endif
404 #ifdef _DEBUG_USB_COMMS_
405 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
406 ft2232_debug_dump_buffer();
407 #endif
409 #ifdef _DEBUG_USB_IO_
410 gettimeofday(&start, NULL);
411 #endif
413 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
415 LOG_ERROR("couldn't write MPSSE commands to FT2232");
416 return retval;
419 #ifdef _DEBUG_USB_IO_
420 gettimeofday(&inter, NULL);
421 #endif
423 if (ft2232_expect_read)
425 int timeout = 100;
426 ft2232_buffer_size = 0;
428 #ifdef _DEBUG_USB_IO_
429 gettimeofday(&inter2, NULL);
430 #endif
432 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
434 LOG_ERROR("couldn't read from FT2232");
435 return retval;
438 #ifdef _DEBUG_USB_IO_
439 gettimeofday(&end, NULL);
441 timeval_subtract(&d_inter, &inter, &start);
442 timeval_subtract(&d_inter2, &inter2, &start);
443 timeval_subtract(&d_end, &end, &start);
445 LOG_INFO("inter: %i.%06i, inter2: %i.%06i end: %i.%06i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
446 #endif
449 ft2232_buffer_size = bytes_read;
451 if (ft2232_expect_read != ft2232_buffer_size)
453 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
454 ft2232_debug_dump_buffer();
456 exit(-1);
459 #ifdef _DEBUG_USB_COMMS_
460 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
461 ft2232_debug_dump_buffer();
462 #endif
465 ft2232_expect_read = 0;
466 ft2232_read_pointer = 0;
468 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
469 * that wasn't handled by a caller-provided error handler
471 retval = ERROR_OK;
473 cmd = first;
474 while (cmd != last)
476 switch (cmd->type)
478 case JTAG_SCAN:
479 type = jtag_scan_type(cmd->cmd.scan);
480 if (type != SCAN_OUT)
482 scan_size = jtag_scan_size(cmd->cmd.scan);
483 buffer = calloc(CEIL(scan_size, 8), 1);
484 ft2232_read_scan(type, buffer, scan_size);
485 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
486 retval = ERROR_JTAG_QUEUE_FAILED;
487 free(buffer);
489 break;
490 default:
491 break;
493 cmd = cmd->next;
496 ft2232_buffer_size = 0;
498 return retval;
501 void ft2232_add_pathmove(pathmove_command_t *cmd)
503 int num_states = cmd->num_states;
504 u8 tms_byte;
505 int state_count;
507 state_count = 0;
508 while (num_states)
510 int bit_count = 0;
512 int num_states_batch = num_states > 7 ? 7 : num_states;
514 tms_byte = 0x0;
515 /* command "Clock Data to TMS/CS Pin (no Read)" */
516 BUFFER_ADD = 0x4b;
517 /* number of states remaining */
518 BUFFER_ADD = num_states_batch - 1;
520 while (num_states_batch--)
522 if (tap_transitions[cur_state].low == cmd->path[state_count])
523 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
524 else if (tap_transitions[cur_state].high == cmd->path[state_count])
525 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
526 else
528 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(cmd->path[state_count]));
529 exit(-1);
532 cur_state = cmd->path[state_count];
533 state_count++;
534 num_states--;
537 BUFFER_ADD = tms_byte;
540 end_state = cur_state;
543 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
545 int num_bytes = (scan_size + 7) / 8;
546 int bits_left = scan_size;
547 int cur_byte = 0;
548 int last_bit;
550 if (!((!ir_scan && (cur_state == TAP_DRSHIFT)) || (ir_scan && (cur_state == TAP_IRSHIFT))))
552 /* command "Clock Data to TMS/CS Pin (no Read)" */
553 BUFFER_ADD = 0x4b;
554 /* scan 7 bit */
555 BUFFER_ADD = 0x6;
556 /* TMS data bits */
557 if (ir_scan)
559 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IRSHIFT);
560 cur_state = TAP_IRSHIFT;
562 else
564 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
565 cur_state = TAP_DRSHIFT;
567 /* LOG_DEBUG("added TMS scan (no read)"); */
570 /* add command for complete bytes */
571 while (num_bytes > 1)
573 int thisrun_bytes;
574 if (type == SCAN_IO)
576 /* Clock Data Bytes In and Out LSB First */
577 BUFFER_ADD = 0x39;
578 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
580 else if (type == SCAN_OUT)
582 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
583 BUFFER_ADD = 0x19;
584 /* LOG_DEBUG("added TDI bytes (o)"); */
586 else if (type == SCAN_IN)
588 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
589 BUFFER_ADD = 0x28;
590 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
592 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
593 num_bytes -= thisrun_bytes;
594 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
595 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
596 if (type != SCAN_IN)
598 /* add complete bytes */
599 while(thisrun_bytes-- > 0)
601 BUFFER_ADD = buffer[cur_byte];
602 cur_byte++;
603 bits_left -= 8;
606 else /* (type == SCAN_IN) */
608 bits_left -= 8 * (thisrun_bytes);
612 /* the most signifcant bit is scanned during TAP movement */
613 if (type != SCAN_IN)
614 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
615 else
616 last_bit = 0;
618 /* process remaining bits but the last one */
619 if (bits_left > 1)
621 if (type == SCAN_IO)
623 /* Clock Data Bits In and Out LSB First */
624 BUFFER_ADD = 0x3b;
625 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
627 else if (type == SCAN_OUT)
629 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
630 BUFFER_ADD = 0x1b;
631 /* LOG_DEBUG("added TDI bits (o)"); */
633 else if (type == SCAN_IN)
635 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
636 BUFFER_ADD = 0x2a;
637 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
639 BUFFER_ADD = bits_left - 2;
640 if (type != SCAN_IN)
641 BUFFER_ADD = buffer[cur_byte];
644 if ((ir_scan && (end_state == TAP_IRSHIFT)) ||
645 (!ir_scan && (end_state == TAP_DRSHIFT)))
647 if (type == SCAN_IO)
649 /* Clock Data Bits In and Out LSB First */
650 BUFFER_ADD = 0x3b;
651 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
653 else if (type == SCAN_OUT)
655 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
656 BUFFER_ADD = 0x1b;
657 /* LOG_DEBUG("added TDI bits (o)"); */
659 else if (type == SCAN_IN)
661 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
662 BUFFER_ADD = 0x2a;
663 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
665 BUFFER_ADD = 0x0;
666 BUFFER_ADD = last_bit;
668 else
670 /* move from Shift-IR/DR to end state */
671 if (type != SCAN_OUT)
673 /* Clock Data to TMS/CS Pin with Read */
674 BUFFER_ADD = 0x6b;
675 /* LOG_DEBUG("added TMS scan (read)"); */
677 else
679 /* Clock Data to TMS/CS Pin (no Read) */
680 BUFFER_ADD = 0x4b;
681 /* LOG_DEBUG("added TMS scan (no read)"); */
683 BUFFER_ADD = 0x6;
684 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
685 cur_state = end_state;
689 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
691 int num_bytes = (scan_size + 7) / 8;
692 int bits_left = scan_size;
693 int cur_byte = 0;
694 int last_bit;
695 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
696 u8 *receive_pointer = receive_buffer;
697 u32 bytes_written;
698 u32 bytes_read;
699 int retval;
700 int thisrun_read = 0;
702 if (cmd->ir_scan)
704 LOG_ERROR("BUG: large IR scans are not supported");
705 exit(-1);
708 if (cur_state != TAP_DRSHIFT)
710 /* command "Clock Data to TMS/CS Pin (no Read)" */
711 BUFFER_ADD = 0x4b;
712 /* scan 7 bit */
713 BUFFER_ADD = 0x6;
714 /* TMS data bits */
715 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
716 cur_state = TAP_DRSHIFT;
719 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
721 LOG_ERROR("couldn't write MPSSE commands to FT2232");
722 exit(-1);
724 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
725 ft2232_buffer_size = 0;
727 /* add command for complete bytes */
728 while (num_bytes > 1)
730 int thisrun_bytes;
732 if (type == SCAN_IO)
734 /* Clock Data Bytes In and Out LSB First */
735 BUFFER_ADD = 0x39;
736 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
738 else if (type == SCAN_OUT)
740 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
741 BUFFER_ADD = 0x19;
742 /* LOG_DEBUG("added TDI bytes (o)"); */
744 else if (type == SCAN_IN)
746 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
747 BUFFER_ADD = 0x28;
748 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
750 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
751 thisrun_read = thisrun_bytes;
752 num_bytes -= thisrun_bytes;
753 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
754 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
755 if (type != SCAN_IN)
757 /* add complete bytes */
758 while(thisrun_bytes-- > 0)
760 BUFFER_ADD = buffer[cur_byte];
761 cur_byte++;
762 bits_left -= 8;
765 else /* (type == SCAN_IN) */
767 bits_left -= 8 * (thisrun_bytes);
770 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
772 LOG_ERROR("couldn't write MPSSE commands to FT2232");
773 exit(-1);
775 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
776 ft2232_buffer_size = 0;
778 if (type != SCAN_OUT)
780 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
782 LOG_ERROR("couldn't read from FT2232");
783 exit(-1);
785 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
786 receive_pointer += bytes_read;
790 thisrun_read = 0;
792 /* the most signifcant bit is scanned during TAP movement */
793 if (type != SCAN_IN)
794 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
795 else
796 last_bit = 0;
798 /* process remaining bits but the last one */
799 if (bits_left > 1)
801 if (type == SCAN_IO)
803 /* Clock Data Bits In and Out LSB First */
804 BUFFER_ADD = 0x3b;
805 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
807 else if (type == SCAN_OUT)
809 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
810 BUFFER_ADD = 0x1b;
811 /* LOG_DEBUG("added TDI bits (o)"); */
813 else if (type == SCAN_IN)
815 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
816 BUFFER_ADD = 0x2a;
817 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
819 BUFFER_ADD = bits_left - 2;
820 if (type != SCAN_IN)
821 BUFFER_ADD = buffer[cur_byte];
823 if (type != SCAN_OUT)
824 thisrun_read += 2;
827 if (end_state == TAP_DRSHIFT)
829 if (type == SCAN_IO)
831 /* Clock Data Bits In and Out LSB First */
832 BUFFER_ADD = 0x3b;
833 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
835 else if (type == SCAN_OUT)
837 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
838 BUFFER_ADD = 0x1b;
839 /* LOG_DEBUG("added TDI bits (o)"); */
841 else if (type == SCAN_IN)
843 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
844 BUFFER_ADD = 0x2a;
845 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
847 BUFFER_ADD = 0x0;
848 BUFFER_ADD = last_bit;
850 else
852 /* move from Shift-IR/DR to end state */
853 if (type != SCAN_OUT)
855 /* Clock Data to TMS/CS Pin with Read */
856 BUFFER_ADD = 0x6b;
857 /* LOG_DEBUG("added TMS scan (read)"); */
859 else
861 /* Clock Data to TMS/CS Pin (no Read) */
862 BUFFER_ADD = 0x4b;
863 /* LOG_DEBUG("added TMS scan (no read)"); */
865 BUFFER_ADD = 0x6;
866 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
867 cur_state = end_state;
870 if (type != SCAN_OUT)
871 thisrun_read += 1;
873 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
875 LOG_ERROR("couldn't write MPSSE commands to FT2232");
876 exit(-1);
878 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
879 ft2232_buffer_size = 0;
881 if (type != SCAN_OUT)
883 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
885 LOG_ERROR("couldn't read from FT2232");
886 exit(-1);
888 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
889 receive_pointer += bytes_read;
892 return ERROR_OK;
895 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
897 int predicted_size = 3;
898 int num_bytes = (scan_size - 1) / 8;
900 if (cur_state != TAP_DRSHIFT)
901 predicted_size += 3;
903 if (type == SCAN_IN) /* only from device to host */
905 /* complete bytes */
906 predicted_size += (CEIL(num_bytes, 65536)) * 3;
907 /* remaining bits - 1 (up to 7) */
908 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
910 else /* host to device, or bidirectional */
912 /* complete bytes */
913 predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
914 /* remaining bits -1 (up to 7) */
915 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
918 return predicted_size;
921 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
923 int predicted_size = 0;
925 if (type != SCAN_OUT)
927 /* complete bytes */
928 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
929 /* remaining bits - 1 */
930 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
931 /* last bit (from TMS scan) */
932 predicted_size += 1;
935 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
937 return predicted_size;
940 void usbjtag_reset(int trst, int srst)
942 if (trst == 1)
944 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
945 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
946 else
947 low_output &= ~nTRST; /* switch output low */
949 else if (trst == 0)
951 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
952 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
953 else
954 low_output |= nTRST; /* switch output high */
957 if (srst == 1)
959 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
960 low_output &= ~nSRST; /* switch output low */
961 else
962 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
964 else if (srst == 0)
966 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
967 low_output |= nSRST; /* switch output high */
968 else
969 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
972 /* command "set data bits low byte" */
973 BUFFER_ADD = 0x80;
974 BUFFER_ADD = low_output;
975 BUFFER_ADD = low_direction;
979 void jtagkey_reset(int trst, int srst)
981 if (trst == 1)
983 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
984 high_output &= ~nTRSTnOE;
985 else
986 high_output &= ~nTRST;
988 else if (trst == 0)
990 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
991 high_output |= nTRSTnOE;
992 else
993 high_output |= nTRST;
996 if (srst == 1)
998 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
999 high_output &= ~nSRST;
1000 else
1001 high_output &= ~nSRSTnOE;
1003 else if (srst == 0)
1005 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1006 high_output |= nSRST;
1007 else
1008 high_output |= nSRSTnOE;
1011 /* command "set data bits high byte" */
1012 BUFFER_ADD = 0x82;
1013 BUFFER_ADD = high_output;
1014 BUFFER_ADD = high_direction;
1015 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1018 void olimex_jtag_reset(int trst, int srst)
1020 if (trst == 1)
1022 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1023 high_output &= ~nTRSTnOE;
1024 else
1025 high_output &= ~nTRST;
1027 else if (trst == 0)
1029 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1030 high_output |= nTRSTnOE;
1031 else
1032 high_output |= nTRST;
1035 if (srst == 1)
1037 high_output |= nSRST;
1039 else if (srst == 0)
1041 high_output &= ~nSRST;
1044 /* command "set data bits high byte" */
1045 BUFFER_ADD = 0x82;
1046 BUFFER_ADD = high_output;
1047 BUFFER_ADD = high_direction;
1048 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1051 void axm0432_jtag_reset(int trst, int srst)
1053 if (trst == 1)
1055 cur_state = TAP_RESET;
1056 high_output &= ~nTRST;
1058 else if (trst == 0)
1060 high_output |= nTRST;
1063 if (srst == 1)
1065 high_output &= ~nSRST;
1067 else if (srst == 0)
1069 high_output |= nSRST;
1072 /* command "set data bits low byte" */
1073 BUFFER_ADD = 0x82;
1074 BUFFER_ADD = high_output;
1075 BUFFER_ADD = high_direction;
1076 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1079 void flyswatter_reset(int trst, int srst)
1081 if (trst == 1)
1083 low_output &= ~nTRST;
1085 else if (trst == 0)
1087 low_output |= nTRST;
1090 if (srst == 1)
1092 low_output |= nSRST;
1094 else if (srst == 0)
1096 low_output &= ~nSRST;
1099 /* command "set data bits low byte" */
1100 BUFFER_ADD = 0x80;
1101 BUFFER_ADD = low_output;
1102 BUFFER_ADD = low_direction;
1103 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1106 void turtle_reset(int trst, int srst)
1108 trst = trst;
1110 if (srst == 1)
1112 low_output |= nSRST;
1114 else if (srst == 0)
1116 low_output &= ~nSRST;
1119 /* command "set data bits low byte" */
1120 BUFFER_ADD = 0x80;
1121 BUFFER_ADD = low_output;
1122 BUFFER_ADD = low_direction;
1123 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1126 void comstick_reset(int trst, int srst)
1128 if (trst == 1)
1130 high_output &= ~nTRST;
1132 else if (trst == 0)
1134 high_output |= nTRST;
1137 if (srst == 1)
1139 high_output &= ~nSRST;
1141 else if (srst == 0)
1143 high_output |= nSRST;
1146 /* command "set data bits high byte" */
1147 BUFFER_ADD = 0x82;
1148 BUFFER_ADD = high_output;
1149 BUFFER_ADD = high_direction;
1150 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1153 void stm32stick_reset(int trst, int srst)
1155 if (trst == 1)
1157 high_output &= ~nTRST;
1159 else if (trst == 0)
1161 high_output |= nTRST;
1164 if (srst == 1)
1166 low_output &= ~nSRST;
1168 else if (srst == 0)
1170 low_output |= nSRST;
1173 /* command "set data bits low byte" */
1174 BUFFER_ADD = 0x80;
1175 BUFFER_ADD = low_output;
1176 BUFFER_ADD = low_direction;
1178 /* command "set data bits high byte" */
1179 BUFFER_ADD = 0x82;
1180 BUFFER_ADD = high_output;
1181 BUFFER_ADD = high_direction;
1182 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1185 int ft2232_execute_queue()
1187 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1188 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
1189 u8 *buffer;
1190 int scan_size; /* size of IR or DR scan */
1191 enum scan_type type;
1192 int i;
1193 int predicted_size = 0;
1194 int require_send = 0;
1195 int retval;
1197 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1198 * that wasn't handled by a caller-provided error handler
1200 retval = ERROR_OK;
1202 ft2232_buffer_size = 0;
1203 ft2232_expect_read = 0;
1205 /* blink, if the current layout has that feature */
1206 if (layout->blink)
1207 layout->blink();
1209 while (cmd)
1211 switch(cmd->type)
1213 case JTAG_END_STATE:
1214 if (cmd->cmd.end_state->end_state != -1)
1215 ft2232_end_state(cmd->cmd.end_state->end_state);
1216 break;
1217 case JTAG_RESET:
1218 /* only send the maximum buffer size that FT2232C can handle */
1219 predicted_size = 3;
1220 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1222 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1223 retval = ERROR_JTAG_QUEUE_FAILED;
1224 require_send = 0;
1225 first_unsent = cmd;
1228 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1230 cur_state = TAP_RESET;
1232 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1233 require_send = 1;
1235 #ifdef _DEBUG_JTAG_IO_
1236 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1237 #endif
1238 break;
1239 case JTAG_RUNTEST:
1240 /* only send the maximum buffer size that FT2232C can handle */
1241 predicted_size = 0;
1242 if (cur_state != TAP_IDLE)
1243 predicted_size += 3;
1244 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1245 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_IDLE))
1246 predicted_size += 3;
1247 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_IDLE))
1248 predicted_size += 3;
1249 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1251 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1252 retval = ERROR_JTAG_QUEUE_FAILED;
1253 require_send = 0;
1254 first_unsent = cmd;
1256 if (cur_state != TAP_IDLE)
1258 /* command "Clock Data to TMS/CS Pin (no Read)" */
1259 BUFFER_ADD = 0x4b;
1260 /* scan 7 bit */
1261 BUFFER_ADD = 0x6;
1262 /* TMS data bits */
1263 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IDLE);
1264 cur_state = TAP_IDLE;
1265 require_send = 1;
1267 i = cmd->cmd.runtest->num_cycles;
1268 while (i > 0)
1270 /* command "Clock Data to TMS/CS Pin (no Read)" */
1271 BUFFER_ADD = 0x4b;
1272 /* scan 7 bit */
1273 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1274 /* TMS data bits */
1275 BUFFER_ADD = 0x0;
1276 cur_state = TAP_IDLE;
1277 i -= (i > 7) ? 7 : i;
1278 /* LOG_DEBUG("added TMS scan (no read)"); */
1280 if (cmd->cmd.runtest->end_state != -1)
1281 ft2232_end_state(cmd->cmd.runtest->end_state);
1282 if (cur_state != end_state)
1284 /* command "Clock Data to TMS/CS Pin (no Read)" */
1285 BUFFER_ADD = 0x4b;
1286 /* scan 7 bit */
1287 BUFFER_ADD = 0x6;
1288 /* TMS data bits */
1289 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1290 cur_state = end_state;
1291 /* LOG_DEBUG("added TMS scan (no read)"); */
1293 require_send = 1;
1294 #ifdef _DEBUG_JTAG_IO_
1295 LOG_DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
1296 #endif
1297 break;
1298 case JTAG_STATEMOVE:
1299 /* only send the maximum buffer size that FT2232C can handle */
1300 predicted_size = 3;
1301 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1303 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1304 retval = ERROR_JTAG_QUEUE_FAILED;
1305 require_send = 0;
1306 first_unsent = cmd;
1308 if (cmd->cmd.statemove->end_state != -1)
1309 ft2232_end_state(cmd->cmd.statemove->end_state);
1310 /* command "Clock Data to TMS/CS Pin (no Read)" */
1311 BUFFER_ADD = 0x4b;
1312 /* scan 7 bit */
1313 BUFFER_ADD = 0x6;
1314 /* TMS data bits */
1315 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1316 /* LOG_DEBUG("added TMS scan (no read)"); */
1317 cur_state = end_state;
1318 require_send = 1;
1319 #ifdef _DEBUG_JTAG_IO_
1320 LOG_DEBUG("statemove: %i", end_state);
1321 #endif
1322 break;
1323 case JTAG_PATHMOVE:
1324 /* only send the maximum buffer size that FT2232C can handle */
1325 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1326 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1328 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1329 retval = ERROR_JTAG_QUEUE_FAILED;
1330 require_send = 0;
1331 first_unsent = cmd;
1333 ft2232_add_pathmove(cmd->cmd.pathmove);
1334 require_send = 1;
1335 #ifdef _DEBUG_JTAG_IO_
1336 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1337 #endif
1338 break;
1339 case JTAG_SCAN:
1340 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1341 type = jtag_scan_type(cmd->cmd.scan);
1342 predicted_size = ft2232_predict_scan_out(scan_size, type);
1343 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1345 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1346 /* unsent commands before this */
1347 if (first_unsent != cmd)
1348 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1349 retval = ERROR_JTAG_QUEUE_FAILED;
1351 /* current command */
1352 if (cmd->cmd.scan->end_state != -1)
1353 ft2232_end_state(cmd->cmd.scan->end_state);
1354 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1355 require_send = 0;
1356 first_unsent = cmd->next;
1357 if (buffer)
1358 free(buffer);
1359 break;
1361 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1363 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1364 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1365 retval = ERROR_JTAG_QUEUE_FAILED;
1366 require_send = 0;
1367 first_unsent = cmd;
1369 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1370 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1371 if (cmd->cmd.scan->end_state != -1)
1372 ft2232_end_state(cmd->cmd.scan->end_state);
1373 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1374 require_send = 1;
1375 if (buffer)
1376 free(buffer);
1377 #ifdef _DEBUG_JTAG_IO_
1378 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
1379 #endif
1380 break;
1381 case JTAG_SLEEP:
1382 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1383 retval = ERROR_JTAG_QUEUE_FAILED;
1384 first_unsent = cmd->next;
1385 jtag_sleep(cmd->cmd.sleep->us);
1386 #ifdef _DEBUG_JTAG_IO_
1387 LOG_DEBUG("sleep %i usec", cmd->cmd.sleep->us);
1388 #endif
1389 break;
1390 default:
1391 LOG_ERROR("BUG: unknown JTAG command type encountered");
1392 exit(-1);
1394 cmd = cmd->next;
1397 if (require_send > 0)
1398 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1399 retval = ERROR_JTAG_QUEUE_FAILED;
1401 return retval;
1404 #if BUILD_FT2232_FTD2XX == 1
1405 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1407 FT_STATUS status;
1408 DWORD openex_flags = 0;
1409 char *openex_string = NULL;
1410 u8 latency_timer;
1412 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",ft2232_layout, vid, pid);
1414 #if IS_WIN32 == 0
1415 /* Add non-standard Vid/Pid to the linux driver */
1416 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1418 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1420 #endif
1422 if (ft2232_device_desc && ft2232_serial)
1424 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1425 ft2232_device_desc = NULL;
1428 if (ft2232_device_desc)
1430 openex_string = ft2232_device_desc;
1431 openex_flags = FT_OPEN_BY_DESCRIPTION;
1433 else if (ft2232_serial)
1435 openex_string = ft2232_serial;
1436 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1438 else
1440 LOG_ERROR("neither device description nor serial number specified");
1441 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1443 return ERROR_JTAG_INIT_FAILED;
1446 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1448 DWORD num_devices;
1450 if (more) {
1451 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1452 *try_more = 1;
1453 return ERROR_JTAG_INIT_FAILED;
1455 LOG_ERROR("unable to open ftdi device: %lu", status);
1456 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1457 if (status == FT_OK)
1459 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1460 int i;
1462 for (i = 0; i < num_devices; i++)
1463 desc_array[i] = malloc(64);
1464 desc_array[num_devices] = NULL;
1466 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1468 if (status == FT_OK)
1470 LOG_ERROR("ListDevices: %lu\n", num_devices);
1471 for (i = 0; i < num_devices; i++)
1472 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1475 for (i = 0; i < num_devices; i++)
1476 free(desc_array[i]);
1477 free(desc_array);
1479 else
1481 LOG_ERROR("ListDevices: NONE\n");
1483 return ERROR_JTAG_INIT_FAILED;
1486 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1488 LOG_ERROR("unable to set latency timer: %lu", status);
1489 return ERROR_JTAG_INIT_FAILED;
1492 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1494 LOG_ERROR("unable to get latency timer: %lu", status);
1495 return ERROR_JTAG_INIT_FAILED;
1497 else
1499 LOG_DEBUG("current latency timer: %i", latency_timer);
1502 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1504 LOG_ERROR("unable to set timeouts: %lu", status);
1505 return ERROR_JTAG_INIT_FAILED;
1508 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1510 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1511 return ERROR_JTAG_INIT_FAILED;
1514 return ERROR_OK;
1517 static int ft2232_purge_ftd2xx(void)
1519 FT_STATUS status;
1521 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1523 LOG_ERROR("error purging ftd2xx device: %lu", status);
1524 return ERROR_JTAG_INIT_FAILED;
1527 return ERROR_OK;
1529 #endif /* BUILD_FT2232_FTD2XX == 1 */
1531 #if BUILD_FT2232_LIBFTDI == 1
1532 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1534 u8 latency_timer;
1536 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1537 ft2232_layout, vid, pid);
1539 if (ftdi_init(&ftdic) < 0)
1540 return ERROR_JTAG_INIT_FAILED;
1542 /* context, vendor id, product id */
1543 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1544 ft2232_serial) < 0) {
1545 if (more)
1546 LOG_WARNING("unable to open ftdi device (trying more): %s",
1547 ftdic.error_str);
1548 else
1549 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1550 *try_more = 1;
1551 return ERROR_JTAG_INIT_FAILED;
1554 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1556 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1557 return ERROR_JTAG_INIT_FAILED;
1560 if (ftdi_usb_reset(&ftdic) < 0)
1562 LOG_ERROR("unable to reset ftdi device");
1563 return ERROR_JTAG_INIT_FAILED;
1566 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1568 LOG_ERROR("unable to set latency timer");
1569 return ERROR_JTAG_INIT_FAILED;
1572 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1574 LOG_ERROR("unable to get latency timer");
1575 return ERROR_JTAG_INIT_FAILED;
1577 else
1579 LOG_DEBUG("current latency timer: %i", latency_timer);
1582 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1584 return ERROR_OK;
1587 static int ft2232_purge_libftdi(void)
1589 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1591 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1592 return ERROR_JTAG_INIT_FAILED;
1595 return ERROR_OK;
1597 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1599 int ft2232_init(void)
1601 u8 buf[1];
1602 int retval;
1603 u32 bytes_written;
1604 ft2232_layout_t *cur_layout = ft2232_layouts;
1605 int i;
1607 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1609 ft2232_layout = "usbjtag";
1610 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1613 while (cur_layout->name)
1615 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1617 layout = cur_layout;
1618 break;
1620 cur_layout++;
1623 if (!layout)
1625 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1626 return ERROR_JTAG_INIT_FAILED;
1629 for (i = 0; 1; i++) {
1631 * "more indicates that there are more IDs to try, so we should
1632 * not print an error for an ID mismatch (but for anything
1633 * else, we should).
1635 * try_more indicates that the error code returned indicates an
1636 * ID mismatch (and nothing else) and that we should proceeed
1637 * with the next ID pair.
1639 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1640 int try_more = 0;
1642 #if BUILD_FT2232_FTD2XX == 1
1643 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1644 more, &try_more);
1645 #elif BUILD_FT2232_LIBFTDI == 1
1646 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1647 more, &try_more);
1648 #endif
1649 if (retval >= 0)
1650 break;
1651 if (!more || !try_more)
1652 return retval;
1655 ft2232_buffer_size = 0;
1656 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1658 if (layout->init() != ERROR_OK)
1659 return ERROR_JTAG_INIT_FAILED;
1661 ft2232_speed(jtag_speed);
1663 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1664 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1666 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1667 return ERROR_JTAG_INIT_FAILED;
1670 #if BUILD_FT2232_FTD2XX == 1
1671 return ft2232_purge_ftd2xx();
1672 #elif BUILD_FT2232_LIBFTDI == 1
1673 return ft2232_purge_libftdi();
1674 #endif
1676 return ERROR_OK;
1679 int usbjtag_init(void)
1681 u8 buf[3];
1682 u32 bytes_written;
1684 low_output = 0x08;
1685 low_direction = 0x0b;
1687 if (strcmp(ft2232_layout, "usbjtag") == 0)
1689 nTRST = 0x10;
1690 nTRSTnOE = 0x10;
1691 nSRST = 0x40;
1692 nSRSTnOE = 0x40;
1694 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1696 nTRST = 0x10;
1697 nTRSTnOE = 0x10;
1698 nSRST = 0x20;
1699 nSRSTnOE = 0x20;
1701 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1703 nTRST = 0x0;
1704 nTRSTnOE = 0x00;
1705 nSRST = 0x20;
1706 nSRSTnOE = 0x20;
1707 low_output = 0x88;
1708 low_direction = 0x8b;
1710 else
1712 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1713 return ERROR_JTAG_INIT_FAILED;
1716 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1718 low_direction &= ~nTRSTnOE; /* nTRST input */
1719 low_output &= ~nTRST; /* nTRST = 0 */
1721 else
1723 low_direction |= nTRSTnOE; /* nTRST output */
1724 low_output |= nTRST; /* nTRST = 1 */
1727 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1729 low_direction |= nSRSTnOE; /* nSRST output */
1730 low_output |= nSRST; /* nSRST = 1 */
1732 else
1734 low_direction &= ~nSRSTnOE; /* nSRST input */
1735 low_output &= ~nSRST; /* nSRST = 0 */
1738 /* initialize low byte for jtag */
1739 buf[0] = 0x80; /* command "set data bits low byte" */
1740 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1741 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1742 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1744 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1746 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1747 return ERROR_JTAG_INIT_FAILED;
1750 return ERROR_OK;
1753 int axm0432_jtag_init(void)
1755 u8 buf[3];
1756 u32 bytes_written;
1758 low_output = 0x08;
1759 low_direction = 0x2b;
1761 /* initialize low byte for jtag */
1762 buf[0] = 0x80; /* command "set data bits low byte" */
1763 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1764 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1765 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1767 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1769 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1770 return ERROR_JTAG_INIT_FAILED;
1773 if (strcmp(layout->name, "axm0432_jtag") == 0)
1775 nTRST = 0x08;
1776 nTRSTnOE = 0x0; /* No output enable for TRST*/
1777 nSRST = 0x04;
1778 nSRSTnOE = 0x0; /* No output enable for SRST*/
1780 else
1782 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1783 exit(-1);
1786 high_output = 0x0;
1787 high_direction = 0x0c;
1789 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1791 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1793 else
1795 high_output |= nTRST;
1798 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1800 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1802 else
1804 high_output |= nSRST;
1807 /* initialize high port */
1808 buf[0] = 0x82; /* command "set data bits high byte" */
1809 buf[1] = high_output; /* value */
1810 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1811 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1813 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1815 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1816 return ERROR_JTAG_INIT_FAILED;
1819 return ERROR_OK;
1822 int jtagkey_init(void)
1824 u8 buf[3];
1825 u32 bytes_written;
1827 low_output = 0x08;
1828 low_direction = 0x1b;
1830 /* initialize low byte for jtag */
1831 buf[0] = 0x80; /* command "set data bits low byte" */
1832 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1833 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1834 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1836 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1838 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1839 return ERROR_JTAG_INIT_FAILED;
1842 if (strcmp(layout->name, "jtagkey") == 0)
1844 nTRST = 0x01;
1845 nTRSTnOE = 0x4;
1846 nSRST = 0x02;
1847 nSRSTnOE = 0x08;
1849 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1850 (strcmp(layout->name, "oocdlink") == 0))
1852 nTRST = 0x02;
1853 nTRSTnOE = 0x1;
1854 nSRST = 0x08;
1855 nSRSTnOE = 0x04;
1857 else
1859 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1860 exit(-1);
1863 high_output = 0x0;
1864 high_direction = 0x0f;
1866 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1868 high_output |= nTRSTnOE;
1869 high_output &= ~nTRST;
1871 else
1873 high_output &= ~nTRSTnOE;
1874 high_output |= nTRST;
1877 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1879 high_output &= ~nSRSTnOE;
1880 high_output |= nSRST;
1882 else
1884 high_output |= nSRSTnOE;
1885 high_output &= ~nSRST;
1888 /* initialize high port */
1889 buf[0] = 0x82; /* command "set data bits high byte" */
1890 buf[1] = high_output; /* value */
1891 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1892 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1894 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1896 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1897 return ERROR_JTAG_INIT_FAILED;
1900 return ERROR_OK;
1903 int olimex_jtag_init(void)
1905 u8 buf[3];
1906 u32 bytes_written;
1908 low_output = 0x08;
1909 low_direction = 0x1b;
1911 /* initialize low byte for jtag */
1912 buf[0] = 0x80; /* command "set data bits low byte" */
1913 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1914 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1915 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1917 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1919 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1920 return ERROR_JTAG_INIT_FAILED;
1923 nTRST = 0x01;
1924 nTRSTnOE = 0x4;
1925 nSRST = 0x02;
1926 nSRSTnOE = 0x00; /* no output enable for nSRST */
1928 high_output = 0x0;
1929 high_direction = 0x0f;
1931 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1933 high_output |= nTRSTnOE;
1934 high_output &= ~nTRST;
1936 else
1938 high_output &= ~nTRSTnOE;
1939 high_output |= nTRST;
1942 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1944 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1946 else
1948 high_output &= ~nSRST;
1951 /* turn red LED on */
1952 high_output |= 0x08;
1954 /* initialize high port */
1955 buf[0] = 0x82; /* command "set data bits high byte" */
1956 buf[1] = high_output; /* value */
1957 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1958 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1960 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1962 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1963 return ERROR_JTAG_INIT_FAILED;
1966 return ERROR_OK;
1969 int flyswatter_init(void)
1971 u8 buf[3];
1972 u32 bytes_written;
1974 low_output = 0x18;
1975 low_direction = 0xfb;
1977 /* initialize low byte for jtag */
1978 buf[0] = 0x80; /* command "set data bits low byte" */
1979 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1980 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1981 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1983 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1985 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
1986 return ERROR_JTAG_INIT_FAILED;
1989 nTRST = 0x10;
1990 nTRSTnOE = 0x0; /* not output enable for nTRST */
1991 nSRST = 0x20;
1992 nSRSTnOE = 0x00; /* no output enable for nSRST */
1994 high_output = 0x00;
1995 high_direction = 0x0c;
1997 /* turn red LED1 on, LED2 off */
1998 high_output |= 0x08;
2000 /* initialize high port */
2001 buf[0] = 0x82; /* command "set data bits high byte" */
2002 buf[1] = high_output; /* value */
2003 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2004 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2006 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2008 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2009 return ERROR_JTAG_INIT_FAILED;
2012 return ERROR_OK;
2015 int turtle_init(void)
2017 u8 buf[3];
2018 u32 bytes_written;
2020 low_output = 0x08;
2021 low_direction = 0x5b;
2023 /* initialize low byte for jtag */
2024 buf[0] = 0x80; /* command "set data bits low byte" */
2025 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2026 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2027 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2029 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2031 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2032 return ERROR_JTAG_INIT_FAILED;
2035 nSRST = 0x40;
2037 high_output = 0x00;
2038 high_direction = 0x0C;
2040 /* initialize high port */
2041 buf[0] = 0x82; /* command "set data bits high byte" */
2042 buf[1] = high_output;
2043 buf[2] = high_direction;
2044 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2046 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2048 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2049 return ERROR_JTAG_INIT_FAILED;
2052 return ERROR_OK;
2055 int comstick_init(void)
2057 u8 buf[3];
2058 u32 bytes_written;
2060 low_output = 0x08;
2061 low_direction = 0x0b;
2063 /* initialize low byte for jtag */
2064 buf[0] = 0x80; /* command "set data bits low byte" */
2065 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2066 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2067 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2069 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2071 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2072 return ERROR_JTAG_INIT_FAILED;
2075 nTRST = 0x01;
2076 nTRSTnOE = 0x00; /* no output enable for nTRST */
2077 nSRST = 0x02;
2078 nSRSTnOE = 0x00; /* no output enable for nSRST */
2080 high_output = 0x03;
2081 high_direction = 0x03;
2083 /* initialize high port */
2084 buf[0] = 0x82; /* command "set data bits high byte" */
2085 buf[1] = high_output;
2086 buf[2] = high_direction;
2087 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2089 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2091 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2092 return ERROR_JTAG_INIT_FAILED;
2095 return ERROR_OK;
2098 int stm32stick_init(void)
2100 u8 buf[3];
2101 u32 bytes_written;
2103 low_output = 0x88;
2104 low_direction = 0x8b;
2106 /* initialize low byte for jtag */
2107 buf[0] = 0x80; /* command "set data bits low byte" */
2108 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2109 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2110 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2112 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2114 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2115 return ERROR_JTAG_INIT_FAILED;
2118 nTRST = 0x01;
2119 nTRSTnOE = 0x00; /* no output enable for nTRST */
2120 nSRST = 0x80;
2121 nSRSTnOE = 0x00; /* no output enable for nSRST */
2123 high_output = 0x01;
2124 high_direction = 0x03;
2126 /* initialize high port */
2127 buf[0] = 0x82; /* command "set data bits high byte" */
2128 buf[1] = high_output;
2129 buf[2] = high_direction;
2130 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2132 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2134 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2135 return ERROR_JTAG_INIT_FAILED;
2138 return ERROR_OK;
2141 void olimex_jtag_blink(void)
2143 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2144 * ACBUS3 is bit 3 of the GPIOH port
2146 if (high_output & 0x08)
2148 /* set port pin high */
2149 high_output &= 0x07;
2151 else
2153 /* set port pin low */
2154 high_output |= 0x08;
2157 BUFFER_ADD = 0x82;
2158 BUFFER_ADD = high_output;
2159 BUFFER_ADD = high_direction;
2162 void turtle_jtag_blink(void)
2165 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2167 if (high_output & 0x08)
2169 high_output = 0x04;
2171 else
2173 high_output = 0x08;
2176 BUFFER_ADD = 0x82;
2177 BUFFER_ADD = high_output;
2178 BUFFER_ADD = high_direction;
2181 int ft2232_quit(void)
2183 #if BUILD_FT2232_FTD2XX == 1
2184 FT_STATUS status;
2186 status = FT_Close(ftdih);
2187 #elif BUILD_FT2232_LIBFTDI == 1
2188 ftdi_disable_bitbang(&ftdic);
2190 ftdi_usb_close(&ftdic);
2192 ftdi_deinit(&ftdic);
2193 #endif
2195 free(ft2232_buffer);
2196 ft2232_buffer = NULL;
2198 return ERROR_OK;
2201 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2203 if (argc == 1)
2205 ft2232_device_desc = strdup(args[0]);
2207 else
2209 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2212 return ERROR_OK;
2215 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2217 if (argc == 1)
2219 ft2232_serial = strdup(args[0]);
2221 else
2223 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2226 return ERROR_OK;
2229 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2231 if (argc == 0)
2232 return ERROR_OK;
2234 ft2232_layout = malloc(strlen(args[0]) + 1);
2235 strcpy(ft2232_layout, args[0]);
2237 return ERROR_OK;
2240 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2242 int i;
2244 if (argc > MAX_USB_IDS*2) {
2245 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2246 "(maximum is %d pairs)", MAX_USB_IDS);
2247 argc = MAX_USB_IDS*2;
2249 if (argc < 2 || (argc & 1))
2251 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2252 if (argc < 2)
2253 return ERROR_OK;
2256 for (i = 0; i+1 < argc; i += 2) {
2257 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2258 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2261 * Explicitly terminate, in case there are multiples instances of
2262 * ft2232_vid_pid.
2264 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2266 return ERROR_OK;
2269 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2271 if (argc == 1)
2273 ft2232_latency = atoi(args[0]);
2275 else
2277 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2280 return ERROR_OK;