1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
31 #include "replacements.h"
33 /* project specific includes */
37 #include "configuration.h"
38 #include "time_support.h"
45 /* FT2232 access library includes */
46 #if BUILD_FT2232_FTD2XX == 1
48 #elif BUILD_FT2232_LIBFTDI == 1
52 /* enable this to debug io latency
55 #define _DEBUG_USB_IO_
58 /* enable this to debug communication
61 #define _DEBUG_USB_COMMS_
64 int ft2232_execute_queue(void);
66 int ft2232_speed(int speed
);
67 int ft2232_speed_div(int speed
, int *khz
);
68 int ft2232_khz(int khz
, int *jtag_speed
);
69 int ft2232_register_commands(struct command_context_s
*cmd_ctx
);
70 int ft2232_init(void);
71 int ft2232_quit(void);
73 int ft2232_handle_device_desc_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
74 int ft2232_handle_serial_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
75 int ft2232_handle_layout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
76 int ft2232_handle_vid_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
77 int ft2232_handle_latency_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
79 char *ft2232_device_desc
= NULL
;
80 char *ft2232_serial
= NULL
;
81 char *ft2232_layout
= NULL
;
82 unsigned char ft2232_latency
= 2;
85 /* vid = pid = 0 marks the end of the list */
86 static u16 ft2232_vid
[MAX_USB_IDS
+1] = { 0x0403, 0 };
87 static u16 ft2232_pid
[MAX_USB_IDS
+1] = { 0x6010, 0 };
89 typedef struct ft2232_layout_s
93 void(*reset
)(int trst
, int srst
);
97 /* init procedures for supported layouts */
98 int usbjtag_init(void);
99 int jtagkey_init(void);
100 int olimex_jtag_init(void);
101 int flyswatter_init(void);
102 int turtle_init(void);
103 int comstick_init(void);
104 int stm32stick_init(void);
105 int axm0432_jtag_init(void);
107 /* reset procedures for supported layouts */
108 void usbjtag_reset(int trst
, int srst
);
109 void jtagkey_reset(int trst
, int srst
);
110 void olimex_jtag_reset(int trst
, int srst
);
111 void flyswatter_reset(int trst
, int srst
);
112 void turtle_reset(int trst
, int srst
);
113 void comstick_reset(int trst
, int srst
);
114 void stm32stick_reset(int trst
, int srst
);
115 void axm0432_jtag_reset(int trst
, int srst
);
118 /* blink procedures for layouts that support a blinking led */
119 void olimex_jtag_blink(void);
120 void turtle_jtag_blink(void);
122 ft2232_layout_t ft2232_layouts
[] =
124 {"usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
125 {"jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
126 {"jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
127 {"oocdlink", jtagkey_init
, jtagkey_reset
, NULL
},
128 {"signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
129 {"evb_lm3s811", usbjtag_init
, usbjtag_reset
, NULL
},
130 {"olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
131 {"flyswatter", flyswatter_init
, flyswatter_reset
, NULL
},
132 {"turtelizer2", turtle_init
, turtle_reset
, turtle_jtag_blink
},
133 {"comstick", comstick_init
, comstick_reset
, NULL
},
134 {"stm32stick", stm32stick_init
, stm32stick_reset
, NULL
},
135 {"axm0432_jtag", axm0432_jtag_init
, axm0432_jtag_reset
, NULL
},
139 static u8 nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
141 static ft2232_layout_t
*layout
;
142 static u8 low_output
= 0x0;
143 static u8 low_direction
= 0x0;
144 static u8 high_output
= 0x0;
145 static u8 high_direction
= 0x0;
147 #if BUILD_FT2232_FTD2XX == 1
148 static FT_HANDLE ftdih
= NULL
;
149 #elif BUILD_FT2232_LIBFTDI == 1
150 static struct ftdi_context ftdic
;
153 static u8
*ft2232_buffer
= NULL
;
154 static int ft2232_buffer_size
= 0;
155 static int ft2232_read_pointer
= 0;
156 static int ft2232_expect_read
= 0;
157 #define FT2232_BUFFER_SIZE 131072
158 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
159 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
161 jtag_interface_t ft2232_interface
=
164 .execute_queue
= ft2232_execute_queue
,
165 .speed
= ft2232_speed
,
166 .speed_div
= ft2232_speed_div
,
168 .register_commands
= ft2232_register_commands
,
173 int ft2232_write(u8
*buf
, int size
, u32
* bytes_written
)
175 #if BUILD_FT2232_FTD2XX == 1
177 DWORD dw_bytes_written
;
178 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
180 *bytes_written
= dw_bytes_written
;
181 LOG_ERROR("FT_Write returned: %lu", status
);
182 return ERROR_JTAG_DEVICE_ERROR
;
186 *bytes_written
= dw_bytes_written
;
189 #elif BUILD_FT2232_LIBFTDI == 1
191 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
194 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
195 return ERROR_JTAG_DEVICE_ERROR
;
199 *bytes_written
= retval
;
205 int ft2232_read(u8
* buf
, int size
, u32
* bytes_read
)
207 #if BUILD_FT2232_FTD2XX == 1
213 while ((*bytes_read
< size
) && timeout
--)
215 if ((status
= FT_Read(ftdih
, buf
+ *bytes_read
, size
-
216 *bytes_read
, &dw_bytes_read
)) != FT_OK
)
219 LOG_ERROR("FT_Read returned: %lu", status
);
220 return ERROR_JTAG_DEVICE_ERROR
;
222 *bytes_read
+= dw_bytes_read
;
224 #elif BUILD_FT2232_LIBFTDI == 1
229 while ((*bytes_read
< size
) && timeout
--)
231 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
234 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
235 return ERROR_JTAG_DEVICE_ERROR
;
237 *bytes_read
+= retval
;
241 if (*bytes_read
< size
)
243 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read
, size
);
244 return ERROR_JTAG_DEVICE_ERROR
;
250 int ft2232_speed(int speed
)
256 buf
[0] = 0x86; /* command "set divisor" */
257 buf
[1] = speed
& 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
258 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
260 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
261 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
263 LOG_ERROR("couldn't set FT2232 TCK speed");
270 int ft2232_speed_div(int speed
, int *khz
)
272 /* Take a look in the FT2232 manual,
273 * AN2232C-01 Command Processor for
274 * MPSSE and MCU Host Bus. Chapter 3.8 */
276 *khz
= 6000 / (1+speed
);
281 int ft2232_khz(int khz
, int *jtag_speed
)
285 LOG_ERROR("RCLK not supported");
288 /* Take a look in the FT2232 manual,
289 * AN2232C-01 Command Processor for
290 * MPSSE and MCU Host Bus. Chapter 3.8
292 * We will calc here with a multiplier
293 * of 10 for better rounding later. */
295 /* Calc speed, (6000 / khz) - 1 */
296 /* Use 65000 for better rounding */
297 *jtag_speed
= (60000 / khz
) - 10;
299 /* Add 0.9 for rounding */
302 /* Calc real speed */
303 *jtag_speed
= *jtag_speed
/ 10;
305 /* Check if speed is greater than 0 */
311 /* Check max value */
312 if (*jtag_speed
> 0xFFFF)
314 *jtag_speed
= 0xFFFF;
320 int ft2232_register_commands(struct command_context_s
*cmd_ctx
)
322 register_command(cmd_ctx
, NULL
, "ft2232_device_desc", ft2232_handle_device_desc_command
,
323 COMMAND_CONFIG
, "the USB device description of the FTDI FT2232 device");
324 register_command(cmd_ctx
, NULL
, "ft2232_serial", ft2232_handle_serial_command
,
325 COMMAND_CONFIG
, "the serial number of the FTDI FT2232 device");
326 register_command(cmd_ctx
, NULL
, "ft2232_layout", ft2232_handle_layout_command
,
327 COMMAND_CONFIG
, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
328 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid", ft2232_handle_vid_pid_command
,
329 COMMAND_CONFIG
, "the vendor ID and product ID of the FTDI FT2232 device");
330 register_command(cmd_ctx
, NULL
, "ft2232_latency", ft2232_handle_latency_command
,
331 COMMAND_CONFIG
, "set the FT2232 latency timer to a new value");
335 void ft2232_end_state(enum tap_state state
)
337 if (tap_move_map
[state
] != -1)
341 LOG_ERROR("BUG: %i is not a valid end state", state
);
346 void ft2232_read_scan(enum scan_type type
, u8
* buffer
, int scan_size
)
348 int num_bytes
= ((scan_size
+ 7) / 8);
349 int bits_left
= scan_size
;
352 while(num_bytes
-- > 1)
354 buffer
[cur_byte
] = BUFFER_READ
;
359 buffer
[cur_byte
] = 0x0;
363 buffer
[cur_byte
] = BUFFER_READ
>> 1;
366 buffer
[cur_byte
] = (buffer
[cur_byte
] | ((BUFFER_READ
& 0x02) << 6)) >> (8 - bits_left
);
370 void ft2232_debug_dump_buffer(void)
376 for (i
= 0; i
< ft2232_buffer_size
; i
++)
378 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
381 LOG_DEBUG("%s", line
);
387 LOG_DEBUG("%s", line
);
390 int ft2232_send_and_recv(jtag_command_t
*first
, jtag_command_t
*last
)
400 #ifdef _DEBUG_USB_IO_
401 struct timeval start
, inter
, inter2
, end
;
402 struct timeval d_inter
, d_inter2
, d_end
;
405 #ifdef _DEBUG_USB_COMMS_
406 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size
);
407 ft2232_debug_dump_buffer();
410 #ifdef _DEBUG_USB_IO_
411 gettimeofday(&start
, NULL
);
414 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
416 LOG_ERROR("couldn't write MPSSE commands to FT2232");
420 #ifdef _DEBUG_USB_IO_
421 gettimeofday(&inter
, NULL
);
424 if (ft2232_expect_read
)
427 ft2232_buffer_size
= 0;
429 #ifdef _DEBUG_USB_IO_
430 gettimeofday(&inter2
, NULL
);
433 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
435 LOG_ERROR("couldn't read from FT2232");
439 #ifdef _DEBUG_USB_IO_
440 gettimeofday(&end
, NULL
);
442 timeval_subtract(&d_inter
, &inter
, &start
);
443 timeval_subtract(&d_inter2
, &inter2
, &start
);
444 timeval_subtract(&d_end
, &end
, &start
);
446 LOG_INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter
.tv_sec
, d_inter
.tv_usec
, d_inter2
.tv_sec
, d_inter2
.tv_usec
, d_end
.tv_sec
, d_end
.tv_usec
);
450 ft2232_buffer_size
= bytes_read
;
452 if (ft2232_expect_read
!= ft2232_buffer_size
)
454 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read
, ft2232_buffer_size
, 100 - timeout
);
455 ft2232_debug_dump_buffer();
460 #ifdef _DEBUG_USB_COMMS_
461 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout
, ft2232_buffer_size
);
462 ft2232_debug_dump_buffer();
466 ft2232_expect_read
= 0;
467 ft2232_read_pointer
= 0;
469 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
470 * that wasn't handled by a caller-provided error handler
480 type
= jtag_scan_type(cmd
->cmd
.scan
);
481 if (type
!= SCAN_OUT
)
483 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
484 buffer
= calloc(CEIL(scan_size
, 8), 1);
485 ft2232_read_scan(type
, buffer
, scan_size
);
486 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
487 retval
= ERROR_JTAG_QUEUE_FAILED
;
497 ft2232_buffer_size
= 0;
502 void ft2232_add_pathmove(pathmove_command_t
*cmd
)
504 int num_states
= cmd
->num_states
;
513 int num_states_batch
= num_states
> 7 ? 7 : num_states
;
516 /* command "Clock Data to TMS/CS Pin (no Read)" */
518 /* number of states remaining */
519 BUFFER_ADD
= num_states_batch
- 1;
521 while (num_states_batch
--)
523 if (tap_transitions
[cur_state
].low
== cmd
->path
[state_count
])
524 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
525 else if (tap_transitions
[cur_state
].high
== cmd
->path
[state_count
])
526 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
529 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[cmd
->path
[state_count
]]);
533 cur_state
= cmd
->path
[state_count
];
538 BUFFER_ADD
= tms_byte
;
541 end_state
= cur_state
;
544 void ft2232_add_scan(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
)
546 int num_bytes
= (scan_size
+ 7) / 8;
547 int bits_left
= scan_size
;
551 if (!((!ir_scan
&& (cur_state
== TAP_SD
)) || (ir_scan
&& (cur_state
== TAP_SI
))))
553 /* command "Clock Data to TMS/CS Pin (no Read)" */
560 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SI
);
565 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SD
);
568 /* LOG_DEBUG("added TMS scan (no read)"); */
571 /* add command for complete bytes */
572 while (num_bytes
> 1)
577 /* Clock Data Bytes In and Out LSB First */
579 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
581 else if (type
== SCAN_OUT
)
583 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
585 /* LOG_DEBUG("added TDI bytes (o)"); */
587 else if (type
== SCAN_IN
)
589 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
591 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
593 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
594 num_bytes
-= thisrun_bytes
;
595 BUFFER_ADD
= (thisrun_bytes
- 1) & 0xff;
596 BUFFER_ADD
= ((thisrun_bytes
- 1) >> 8) & 0xff;
599 /* add complete bytes */
600 while(thisrun_bytes
-- > 0)
602 BUFFER_ADD
= buffer
[cur_byte
];
607 else /* (type == SCAN_IN) */
609 bits_left
-= 8 * (thisrun_bytes
);
613 /* the most signifcant bit is scanned during TAP movement */
615 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
619 /* process remaining bits but the last one */
624 /* Clock Data Bits In and Out LSB First */
626 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
628 else if (type
== SCAN_OUT
)
630 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
632 /* LOG_DEBUG("added TDI bits (o)"); */
634 else if (type
== SCAN_IN
)
636 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
638 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
640 BUFFER_ADD
= bits_left
- 2;
642 BUFFER_ADD
= buffer
[cur_byte
];
645 if ((ir_scan
&& (end_state
== TAP_SI
)) ||
646 (!ir_scan
&& (end_state
== TAP_SD
)))
650 /* Clock Data Bits In and Out LSB First */
652 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
654 else if (type
== SCAN_OUT
)
656 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
658 /* LOG_DEBUG("added TDI bits (o)"); */
660 else if (type
== SCAN_IN
)
662 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
664 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
667 BUFFER_ADD
= last_bit
;
671 /* move from Shift-IR/DR to end state */
672 if (type
!= SCAN_OUT
)
674 /* Clock Data to TMS/CS Pin with Read */
676 /* LOG_DEBUG("added TMS scan (read)"); */
680 /* Clock Data to TMS/CS Pin (no Read) */
682 /* LOG_DEBUG("added TMS scan (no read)"); */
685 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
) | (last_bit
<< 7);
686 cur_state
= end_state
;
690 int ft2232_large_scan(scan_command_t
*cmd
, enum scan_type type
, u8
*buffer
, int scan_size
)
692 int num_bytes
= (scan_size
+ 7) / 8;
693 int bits_left
= scan_size
;
696 u8
*receive_buffer
= malloc(CEIL(scan_size
, 8));
697 u8
*receive_pointer
= receive_buffer
;
701 int thisrun_read
= 0;
705 LOG_ERROR("BUG: large IR scans are not supported");
709 if (cur_state
!= TAP_SD
)
711 /* command "Clock Data to TMS/CS Pin (no Read)" */
716 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SD
);
720 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
722 LOG_ERROR("couldn't write MPSSE commands to FT2232");
725 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
726 ft2232_buffer_size
= 0;
728 /* add command for complete bytes */
729 while (num_bytes
> 1)
735 /* Clock Data Bytes In and Out LSB First */
737 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
739 else if (type
== SCAN_OUT
)
741 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
743 /* LOG_DEBUG("added TDI bytes (o)"); */
745 else if (type
== SCAN_IN
)
747 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
749 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
751 thisrun_bytes
= (num_bytes
> 65537) ? 65536 : (num_bytes
- 1);
752 thisrun_read
= thisrun_bytes
;
753 num_bytes
-= thisrun_bytes
;
754 BUFFER_ADD
= (thisrun_bytes
- 1) & 0xff;
755 BUFFER_ADD
= ((thisrun_bytes
- 1) >> 8) & 0xff;
758 /* add complete bytes */
759 while(thisrun_bytes
-- > 0)
761 BUFFER_ADD
= buffer
[cur_byte
];
766 else /* (type == SCAN_IN) */
768 bits_left
-= 8 * (thisrun_bytes
);
771 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
773 LOG_ERROR("couldn't write MPSSE commands to FT2232");
776 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
777 ft2232_buffer_size
= 0;
779 if (type
!= SCAN_OUT
)
781 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
783 LOG_ERROR("couldn't read from FT2232");
786 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read
, bytes_read
);
787 receive_pointer
+= bytes_read
;
793 /* the most signifcant bit is scanned during TAP movement */
795 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
799 /* process remaining bits but the last one */
804 /* Clock Data Bits In and Out LSB First */
806 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
808 else if (type
== SCAN_OUT
)
810 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
812 /* LOG_DEBUG("added TDI bits (o)"); */
814 else if (type
== SCAN_IN
)
816 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
818 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
820 BUFFER_ADD
= bits_left
- 2;
822 BUFFER_ADD
= buffer
[cur_byte
];
824 if (type
!= SCAN_OUT
)
828 if (end_state
== TAP_SD
)
832 /* Clock Data Bits In and Out LSB First */
834 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
836 else if (type
== SCAN_OUT
)
838 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
840 /* LOG_DEBUG("added TDI bits (o)"); */
842 else if (type
== SCAN_IN
)
844 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
846 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
849 BUFFER_ADD
= last_bit
;
853 /* move from Shift-IR/DR to end state */
854 if (type
!= SCAN_OUT
)
856 /* Clock Data to TMS/CS Pin with Read */
858 /* LOG_DEBUG("added TMS scan (read)"); */
862 /* Clock Data to TMS/CS Pin (no Read) */
864 /* LOG_DEBUG("added TMS scan (no read)"); */
867 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
) | (last_bit
<< 7);
868 cur_state
= end_state
;
871 if (type
!= SCAN_OUT
)
874 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
876 LOG_ERROR("couldn't write MPSSE commands to FT2232");
879 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size
, bytes_written
);
880 ft2232_buffer_size
= 0;
882 if (type
!= SCAN_OUT
)
884 if ((retval
= ft2232_read(receive_pointer
, thisrun_read
, &bytes_read
)) != ERROR_OK
)
886 LOG_ERROR("couldn't read from FT2232");
889 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read
, bytes_read
);
890 receive_pointer
+= bytes_read
;
896 int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
898 int predicted_size
= 3;
899 int num_bytes
= (scan_size
- 1) / 8;
901 if (cur_state
!= TAP_SD
)
904 if (type
== SCAN_IN
) /* only from device to host */
907 predicted_size
+= (CEIL(num_bytes
, 65536)) * 3;
908 /* remaining bits - 1 (up to 7) */
909 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
911 else /* host to device, or bidirectional */
914 predicted_size
+= num_bytes
+ (CEIL(num_bytes
, 65536)) * 3;
915 /* remaining bits -1 (up to 7) */
916 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
919 return predicted_size
;
922 int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
924 int predicted_size
= 0;
926 if (type
!= SCAN_OUT
)
929 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) - 1) : 0;
930 /* remaining bits - 1 */
931 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
932 /* last bit (from TMS scan) */
936 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
938 return predicted_size
;
941 void usbjtag_reset(int trst
, int srst
)
945 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
946 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
948 low_output
&= ~nTRST
; /* switch output low */
952 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
953 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
955 low_output
|= nTRST
; /* switch output high */
960 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
961 low_output
&= ~nSRST
; /* switch output low */
963 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
967 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
968 low_output
|= nSRST
; /* switch output high */
970 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
973 /* command "set data bits low byte" */
975 BUFFER_ADD
= low_output
;
976 BUFFER_ADD
= low_direction
;
980 void jtagkey_reset(int trst
, int srst
)
984 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
985 high_output
&= ~nTRSTnOE
;
987 high_output
&= ~nTRST
;
991 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
992 high_output
|= nTRSTnOE
;
994 high_output
|= nTRST
;
999 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1000 high_output
&= ~nSRST
;
1002 high_output
&= ~nSRSTnOE
;
1006 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1007 high_output
|= nSRST
;
1009 high_output
|= nSRSTnOE
;
1012 /* command "set data bits high byte" */
1014 BUFFER_ADD
= high_output
;
1015 BUFFER_ADD
= high_direction
;
1016 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1019 void olimex_jtag_reset(int trst
, int srst
)
1023 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1024 high_output
&= ~nTRSTnOE
;
1026 high_output
&= ~nTRST
;
1030 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1031 high_output
|= nTRSTnOE
;
1033 high_output
|= nTRST
;
1038 high_output
|= nSRST
;
1042 high_output
&= ~nSRST
;
1045 /* command "set data bits high byte" */
1047 BUFFER_ADD
= high_output
;
1048 BUFFER_ADD
= high_direction
;
1049 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1053 void axm0432_jtag_reset(int trst
, int srst
)
1057 cur_state
= TAP_TLR
;
1058 high_output
&= ~nTRST
;
1062 high_output
|= nTRST
;
1067 high_output
&= ~nSRST
;
1071 high_output
|= nSRST
;
1074 /* command "set data bits low byte" */
1076 BUFFER_ADD
= high_output
;
1077 BUFFER_ADD
= high_direction
;
1078 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1084 void flyswatter_reset(int trst
, int srst
)
1088 low_output
&= ~nTRST
;
1092 low_output
|= nTRST
;
1097 low_output
|= nSRST
;
1101 low_output
&= ~nSRST
;
1104 /* command "set data bits low byte" */
1106 BUFFER_ADD
= low_output
;
1107 BUFFER_ADD
= low_direction
;
1108 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst
, srst
, low_output
, low_direction
);
1111 void turtle_reset(int trst
, int srst
)
1117 low_output
|= nSRST
;
1121 low_output
&= ~nSRST
;
1124 /* command "set data bits low byte" */
1126 BUFFER_ADD
= low_output
;
1127 BUFFER_ADD
= low_direction
;
1128 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst
, low_output
, low_direction
);
1131 void comstick_reset(int trst
, int srst
)
1135 high_output
&= ~nTRST
;
1139 high_output
|= nTRST
;
1144 high_output
&= ~nSRST
;
1148 high_output
|= nSRST
;
1151 /* command "set data bits high byte" */
1153 BUFFER_ADD
= high_output
;
1154 BUFFER_ADD
= high_direction
;
1155 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1158 void stm32stick_reset(int trst
, int srst
)
1162 high_output
&= ~nTRST
;
1166 high_output
|= nTRST
;
1171 low_output
&= ~nSRST
;
1175 low_output
|= nSRST
;
1178 /* command "set data bits low byte" */
1180 BUFFER_ADD
= low_output
;
1181 BUFFER_ADD
= low_direction
;
1183 /* command "set data bits high byte" */
1185 BUFFER_ADD
= high_output
;
1186 BUFFER_ADD
= high_direction
;
1187 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
1190 int ft2232_execute_queue()
1192 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
1193 jtag_command_t
*first_unsent
= cmd
; /* next command that has to be sent */
1195 int scan_size
; /* size of IR or DR scan */
1196 enum scan_type type
;
1198 int predicted_size
= 0;
1199 int require_send
= 0;
1202 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1203 * that wasn't handled by a caller-provided error handler
1207 ft2232_buffer_size
= 0;
1208 ft2232_expect_read
= 0;
1210 /* blink, if the current layout has that feature */
1218 case JTAG_END_STATE
:
1219 if (cmd
->cmd
.end_state
->end_state
!= -1)
1220 ft2232_end_state(cmd
->cmd
.end_state
->end_state
);
1223 /* only send the maximum buffer size that FT2232C can handle */
1225 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1227 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1228 retval
= ERROR_JTAG_QUEUE_FAILED
;
1233 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_reset_config
& RESET_SRST_PULLS_TRST
)))
1235 cur_state
= TAP_TLR
;
1237 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1240 #ifdef _DEBUG_JTAG_IO_
1241 LOG_DEBUG("trst: %i, srst: %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1245 /* only send the maximum buffer size that FT2232C can handle */
1247 if (cur_state
!= TAP_RTI
)
1248 predicted_size
+= 3;
1249 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
1250 if ((cmd
->cmd
.runtest
->end_state
!= -1) && (cmd
->cmd
.runtest
->end_state
!= TAP_RTI
))
1251 predicted_size
+= 3;
1252 if ((cmd
->cmd
.runtest
->end_state
== -1) && (end_state
!= TAP_RTI
))
1253 predicted_size
+= 3;
1254 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1256 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1257 retval
= ERROR_JTAG_QUEUE_FAILED
;
1261 if (cur_state
!= TAP_RTI
)
1263 /* command "Clock Data to TMS/CS Pin (no Read)" */
1268 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_RTI
);
1269 cur_state
= TAP_RTI
;
1272 i
= cmd
->cmd
.runtest
->num_cycles
;
1275 /* command "Clock Data to TMS/CS Pin (no Read)" */
1278 BUFFER_ADD
= (i
> 7) ? 6 : (i
- 1);
1281 cur_state
= TAP_RTI
;
1282 i
-= (i
> 7) ? 7 : i
;
1283 /* LOG_DEBUG("added TMS scan (no read)"); */
1285 if (cmd
->cmd
.runtest
->end_state
!= -1)
1286 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
1287 if (cur_state
!= end_state
)
1289 /* command "Clock Data to TMS/CS Pin (no Read)" */
1294 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
);
1295 cur_state
= end_state
;
1296 /* LOG_DEBUG("added TMS scan (no read)"); */
1299 #ifdef _DEBUG_JTAG_IO_
1300 LOG_DEBUG("runtest: %i, end in %i", cmd
->cmd
.runtest
->num_cycles
, end_state
);
1303 case JTAG_STATEMOVE
:
1304 /* only send the maximum buffer size that FT2232C can handle */
1306 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1308 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1309 retval
= ERROR_JTAG_QUEUE_FAILED
;
1313 if (cmd
->cmd
.statemove
->end_state
!= -1)
1314 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
1315 /* command "Clock Data to TMS/CS Pin (no Read)" */
1320 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
);
1321 /* LOG_DEBUG("added TMS scan (no read)"); */
1322 cur_state
= end_state
;
1324 #ifdef _DEBUG_JTAG_IO_
1325 LOG_DEBUG("statemove: %i", end_state
);
1329 /* only send the maximum buffer size that FT2232C can handle */
1330 predicted_size
= 3 * CEIL(cmd
->cmd
.pathmove
->num_states
, 7);
1331 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1333 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1334 retval
= ERROR_JTAG_QUEUE_FAILED
;
1338 ft2232_add_pathmove(cmd
->cmd
.pathmove
);
1340 #ifdef _DEBUG_JTAG_IO_
1341 LOG_DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1345 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1346 type
= jtag_scan_type(cmd
->cmd
.scan
);
1347 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
1348 if ((predicted_size
+ 1) > FT2232_BUFFER_SIZE
)
1350 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1351 /* unsent commands before this */
1352 if (first_unsent
!= cmd
)
1353 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1354 retval
= ERROR_JTAG_QUEUE_FAILED
;
1356 /* current command */
1357 if (cmd
->cmd
.scan
->end_state
!= -1)
1358 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1359 ft2232_large_scan(cmd
->cmd
.scan
, type
, buffer
, scan_size
);
1361 first_unsent
= cmd
->next
;
1366 else if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
1368 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent
, cmd
);
1369 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1370 retval
= ERROR_JTAG_QUEUE_FAILED
;
1374 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
1375 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1376 if (cmd
->cmd
.scan
->end_state
!= -1)
1377 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
1378 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
1382 #ifdef _DEBUG_JTAG_IO_
1383 LOG_DEBUG("%s scan, %i bit, end in %i", (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
, end_state
);
1387 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1388 retval
= ERROR_JTAG_QUEUE_FAILED
;
1389 first_unsent
= cmd
->next
;
1390 jtag_sleep(cmd
->cmd
.sleep
->us
);
1391 #ifdef _DEBUG_JTAG_IO_
1392 LOG_DEBUG("sleep %i usec", cmd
->cmd
.sleep
->us
);
1396 LOG_ERROR("BUG: unknown JTAG command type encountered");
1402 if (require_send
> 0)
1403 if (ft2232_send_and_recv(first_unsent
, cmd
) != ERROR_OK
)
1404 retval
= ERROR_JTAG_QUEUE_FAILED
;
1409 #if BUILD_FT2232_FTD2XX == 1
1410 static int ft2232_init_ftd2xx(u16 vid
, u16 pid
, int more
, int *try_more
)
1413 DWORD openex_flags
= 0;
1414 char *openex_string
= NULL
;
1417 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
1418 ft2232_layout
, vid
, pid
);
1421 /* Add non-standard Vid/Pid to the linux driver */
1422 if ((status
= FT_SetVIDPID(vid
, pid
)) != FT_OK
)
1424 LOG_WARNING("couldn't add %4.4x:%4.4x",
1429 if (ft2232_device_desc
&& ft2232_serial
)
1431 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1432 ft2232_device_desc
= NULL
;
1435 if (ft2232_device_desc
)
1437 openex_string
= ft2232_device_desc
;
1438 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1440 else if (ft2232_serial
)
1442 openex_string
= ft2232_serial
;
1443 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1447 LOG_ERROR("neither device description nor serial number specified");
1448 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1450 return ERROR_JTAG_INIT_FAILED
;
1453 if ((status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
)) != FT_OK
)
1458 LOG_WARNING("unable to open ftdi device (trying more): %lu",
1461 return ERROR_JTAG_INIT_FAILED
;
1463 LOG_ERROR("unable to open ftdi device: %lu", status
);
1464 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1465 if (status
== FT_OK
)
1467 char **desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1470 for (i
= 0; i
< num_devices
; i
++)
1471 desc_array
[i
] = malloc(64);
1472 desc_array
[num_devices
] = NULL
;
1474 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1476 if (status
== FT_OK
)
1478 LOG_ERROR("ListDevices: %lu\n", num_devices
);
1479 for (i
= 0; i
< num_devices
; i
++)
1480 LOG_ERROR("%i: %s", i
, desc_array
[i
]);
1483 for (i
= 0; i
< num_devices
; i
++)
1484 free(desc_array
[i
]);
1489 LOG_ERROR("ListDevices: NONE\n");
1491 return ERROR_JTAG_INIT_FAILED
;
1494 if ((status
= FT_SetLatencyTimer(ftdih
, ft2232_latency
)) != FT_OK
)
1496 LOG_ERROR("unable to set latency timer: %lu", status
);
1497 return ERROR_JTAG_INIT_FAILED
;
1500 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1502 LOG_ERROR("unable to get latency timer: %lu", status
);
1503 return ERROR_JTAG_INIT_FAILED
;
1507 LOG_DEBUG("current latency timer: %i", latency_timer
);
1510 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1512 LOG_ERROR("unable to set timeouts: %lu", status
);
1513 return ERROR_JTAG_INIT_FAILED
;
1516 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1518 LOG_ERROR("unable to enable bit i/o mode: %lu", status
);
1519 return ERROR_JTAG_INIT_FAILED
;
1525 static int ft2232_purge_ftd2xx(void)
1529 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1531 LOG_ERROR("error purging ftd2xx device: %lu", status
);
1532 return ERROR_JTAG_INIT_FAILED
;
1537 #endif /* BUILD_FT2232_FTD2XX == 1 */
1539 #if BUILD_FT2232_LIBFTDI == 1
1540 static int ft2232_init_libftdi(u16 vid
, u16 pid
, int more
, int *try_more
)
1544 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1545 ft2232_layout
, vid
, pid
);
1547 if (ftdi_init(&ftdic
) < 0)
1548 return ERROR_JTAG_INIT_FAILED
;
1550 /* context, vendor id, product id */
1551 if (ftdi_usb_open_desc(&ftdic
, vid
, pid
, ft2232_device_desc
,
1552 ft2232_serial
) < 0) {
1554 LOG_WARNING("unable to open ftdi device (trying more): %s",
1557 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
1559 return ERROR_JTAG_INIT_FAILED
;
1562 if (ftdi_set_interface(&ftdic
, INTERFACE_A
) < 0)
1564 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic
.error_str
);
1565 return ERROR_JTAG_INIT_FAILED
;
1568 if (ftdi_usb_reset(&ftdic
) < 0)
1570 LOG_ERROR("unable to reset ftdi device");
1571 return ERROR_JTAG_INIT_FAILED
;
1574 if (ftdi_set_latency_timer(&ftdic
, ft2232_latency
) < 0)
1576 LOG_ERROR("unable to set latency timer");
1577 return ERROR_JTAG_INIT_FAILED
;
1580 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
1582 LOG_ERROR("unable to get latency timer");
1583 return ERROR_JTAG_INIT_FAILED
;
1587 LOG_DEBUG("current latency timer: %i", latency_timer
);
1590 ftdi_set_bitmode(&ftdic
, 0x0b, 2); /* ctx, JTAG I/O mask */
1595 static int ft2232_purge_libftdi(void)
1597 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
1599 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
1600 return ERROR_JTAG_INIT_FAILED
;
1605 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1607 int ft2232_init(void)
1612 ft2232_layout_t
*cur_layout
= ft2232_layouts
;
1615 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
1617 ft2232_layout
= "usbjtag";
1618 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1621 while (cur_layout
->name
)
1623 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
1625 layout
= cur_layout
;
1633 LOG_ERROR("No matching layout found for %s", ft2232_layout
);
1634 return ERROR_JTAG_INIT_FAILED
;
1637 for (i
= 0; 1; i
++) {
1639 * "more indicates that there are more IDs to try, so we should
1640 * not print an error for an ID mismatch (but for anything
1643 * try_more indicates that the error code returned indicates an
1644 * ID mismatch (and nothing else) and that we should proceeed
1645 * with the next ID pair.
1647 int more
= ft2232_vid
[i
+1] || ft2232_pid
[i
+1];
1650 #if BUILD_FT2232_FTD2XX == 1
1651 retval
= ft2232_init_ftd2xx(ft2232_vid
[i
], ft2232_pid
[i
],
1653 #elif BUILD_FT2232_LIBFTDI == 1
1654 retval
= ft2232_init_libftdi(ft2232_vid
[i
], ft2232_pid
[i
],
1659 if (!more
|| !try_more
)
1663 ft2232_buffer_size
= 0;
1664 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
1666 if (layout
->init() != ERROR_OK
)
1667 return ERROR_JTAG_INIT_FAILED
;
1669 ft2232_speed(jtag_speed
);
1671 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1672 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
1674 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1675 return ERROR_JTAG_INIT_FAILED
;
1678 #if BUILD_FT2232_FTD2XX == 1
1679 return ft2232_purge_ftd2xx();
1680 #elif BUILD_FT2232_LIBFTDI == 1
1681 return ft2232_purge_libftdi();
1687 int usbjtag_init(void)
1693 low_direction
= 0x0b;
1695 if (strcmp(ft2232_layout
, "usbjtag") == 0)
1702 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
1709 else if (strcmp(ft2232_layout
, "evb_lm3s811") == 0)
1716 low_direction
= 0x8b;
1720 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
1721 return ERROR_JTAG_INIT_FAILED
;
1724 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1726 low_direction
&= ~nTRSTnOE
; /* nTRST input */
1727 low_output
&= ~nTRST
; /* nTRST = 0 */
1731 low_direction
|= nTRSTnOE
; /* nTRST output */
1732 low_output
|= nTRST
; /* nTRST = 1 */
1735 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1737 low_direction
|= nSRSTnOE
; /* nSRST output */
1738 low_output
|= nSRST
; /* nSRST = 1 */
1742 low_direction
&= ~nSRSTnOE
; /* nSRST input */
1743 low_output
&= ~nSRST
; /* nSRST = 0 */
1746 /* initialize low byte for jtag */
1747 buf
[0] = 0x80; /* command "set data bits low byte" */
1748 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1749 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1750 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1752 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1754 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1755 return ERROR_JTAG_INIT_FAILED
;
1762 int axm0432_jtag_init(void)
1770 low_direction
= 0x2b;
1772 /* initialize low byte for jtag */
1773 buf
[0] = 0x80; /* command "set data bits low byte" */
1774 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1775 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1776 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1778 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1780 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1781 return ERROR_JTAG_INIT_FAILED
;
1785 if (strcmp(layout
->name
, "axm0432_jtag") == 0)
1788 nTRSTnOE
= 0x0; /* No output enable for TRST*/
1790 nSRSTnOE
= 0x0; /* No output enable for SRST*/
1794 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1799 high_direction
= 0x0c;
1801 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1803 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1807 high_output
|= nTRST
;
1810 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1812 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1816 high_output
|= nSRST
;
1819 /* initialize high port */
1820 buf
[0] = 0x82; /* command "set data bits high byte" */
1821 buf
[1] = high_output
; /* value */
1822 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1823 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1825 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1827 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1828 return ERROR_JTAG_INIT_FAILED
;
1837 int jtagkey_init(void)
1843 low_direction
= 0x1b;
1845 /* initialize low byte for jtag */
1846 buf
[0] = 0x80; /* command "set data bits low byte" */
1847 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1848 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1849 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1851 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1853 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1854 return ERROR_JTAG_INIT_FAILED
;
1857 if (strcmp(layout
->name
, "jtagkey") == 0)
1864 else if ((strcmp(layout
->name
, "jtagkey_prototype_v1") == 0) ||
1865 (strcmp(layout
->name
, "oocdlink") == 0))
1874 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1879 high_direction
= 0x0f;
1881 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1883 high_output
|= nTRSTnOE
;
1884 high_output
&= ~nTRST
;
1888 high_output
&= ~nTRSTnOE
;
1889 high_output
|= nTRST
;
1892 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1894 high_output
&= ~nSRSTnOE
;
1895 high_output
|= nSRST
;
1899 high_output
|= nSRSTnOE
;
1900 high_output
&= ~nSRST
;
1903 /* initialize high port */
1904 buf
[0] = 0x82; /* command "set data bits high byte" */
1905 buf
[1] = high_output
; /* value */
1906 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1907 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1909 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1911 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1912 return ERROR_JTAG_INIT_FAILED
;
1918 int olimex_jtag_init(void)
1924 low_direction
= 0x1b;
1926 /* initialize low byte for jtag */
1927 buf
[0] = 0x80; /* command "set data bits low byte" */
1928 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1929 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1930 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1932 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1934 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1935 return ERROR_JTAG_INIT_FAILED
;
1941 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1944 high_direction
= 0x0f;
1946 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1948 high_output
|= nTRSTnOE
;
1949 high_output
&= ~nTRST
;
1953 high_output
&= ~nTRSTnOE
;
1954 high_output
|= nTRST
;
1957 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1959 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1963 high_output
&= ~nSRST
;
1966 /* turn red LED on */
1967 high_output
|= 0x08;
1969 /* initialize high port */
1970 buf
[0] = 0x82; /* command "set data bits high byte" */
1971 buf
[1] = high_output
; /* value */
1972 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1973 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1975 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1977 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1978 return ERROR_JTAG_INIT_FAILED
;
1984 int flyswatter_init(void)
1990 low_direction
= 0xfb;
1992 /* initialize low byte for jtag */
1993 buf
[0] = 0x80; /* command "set data bits low byte" */
1994 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1995 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1996 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1998 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2000 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2001 return ERROR_JTAG_INIT_FAILED
;
2005 nTRSTnOE
= 0x0; /* not output enable for nTRST */
2007 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2010 high_direction
= 0x0c;
2012 /* turn red LED1 on, LED2 off */
2013 high_output
|= 0x08;
2015 /* initialize high port */
2016 buf
[0] = 0x82; /* command "set data bits high byte" */
2017 buf
[1] = high_output
; /* value */
2018 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
2019 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2021 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2023 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2024 return ERROR_JTAG_INIT_FAILED
;
2030 int turtle_init(void)
2036 low_direction
= 0x5b;
2038 /* initialize low byte for jtag */
2039 buf
[0] = 0x80; /* command "set data bits low byte" */
2040 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2041 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2042 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2044 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2046 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2047 return ERROR_JTAG_INIT_FAILED
;
2053 high_direction
= 0x0C;
2055 /* initialize high port */
2056 buf
[0] = 0x82; /* command "set data bits high byte" */
2057 buf
[1] = high_output
;
2058 buf
[2] = high_direction
;
2059 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2061 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2063 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2064 return ERROR_JTAG_INIT_FAILED
;
2070 int comstick_init(void)
2076 low_direction
= 0x0b;
2078 /* initialize low byte for jtag */
2079 buf
[0] = 0x80; /* command "set data bits low byte" */
2080 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2081 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2082 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2084 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2086 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2087 return ERROR_JTAG_INIT_FAILED
;
2091 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2093 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2096 high_direction
= 0x03;
2098 /* initialize high port */
2099 buf
[0] = 0x82; /* command "set data bits high byte" */
2100 buf
[1] = high_output
;
2101 buf
[2] = high_direction
;
2102 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2104 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2106 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2107 return ERROR_JTAG_INIT_FAILED
;
2113 int stm32stick_init(void)
2119 low_direction
= 0x8b;
2121 /* initialize low byte for jtag */
2122 buf
[0] = 0x80; /* command "set data bits low byte" */
2123 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2124 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2125 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2127 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2129 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2130 return ERROR_JTAG_INIT_FAILED
;
2134 nTRSTnOE
= 0x00; /* no output enable for nTRST */
2136 nSRSTnOE
= 0x00; /* no output enable for nSRST */
2139 high_direction
= 0x03;
2141 /* initialize high port */
2142 buf
[0] = 0x82; /* command "set data bits high byte" */
2143 buf
[1] = high_output
;
2144 buf
[2] = high_direction
;
2145 LOG_DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
2147 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
2149 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2150 return ERROR_JTAG_INIT_FAILED
;
2156 void olimex_jtag_blink(void)
2158 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2159 * ACBUS3 is bit 3 of the GPIOH port
2161 if (high_output
& 0x08)
2163 /* set port pin high */
2164 high_output
&= 0x07;
2168 /* set port pin low */
2169 high_output
|= 0x08;
2173 BUFFER_ADD
= high_output
;
2174 BUFFER_ADD
= high_direction
;
2177 void turtle_jtag_blink(void)
2180 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2182 if (high_output
& 0x08)
2192 BUFFER_ADD
= high_output
;
2193 BUFFER_ADD
= high_direction
;
2196 int ft2232_quit(void)
2198 #if BUILD_FT2232_FTD2XX == 1
2201 status
= FT_Close(ftdih
);
2202 #elif BUILD_FT2232_LIBFTDI == 1
2203 ftdi_disable_bitbang(&ftdic
);
2205 ftdi_usb_close(&ftdic
);
2207 ftdi_deinit(&ftdic
);
2210 free(ft2232_buffer
);
2211 ft2232_buffer
= NULL
;
2216 int ft2232_handle_device_desc_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2220 ft2232_device_desc
= strdup(args
[0]);
2224 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2230 int ft2232_handle_serial_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2234 ft2232_serial
= strdup(args
[0]);
2238 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2244 int ft2232_handle_layout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2249 ft2232_layout
= malloc(strlen(args
[0]) + 1);
2250 strcpy(ft2232_layout
, args
[0]);
2255 int ft2232_handle_vid_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2259 if (argc
> MAX_USB_IDS
*2) {
2260 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2261 "(maximum is %d pairs)", MAX_USB_IDS
);
2262 argc
= MAX_USB_IDS
*2;
2264 if (argc
< 2 || (argc
& 1))
2266 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2271 for (i
= 0; i
+1 < argc
; i
+= 2) {
2272 ft2232_vid
[i
>> 1] = strtol(args
[i
], NULL
, 0);
2273 ft2232_pid
[i
>> 1] = strtol(args
[i
+1], NULL
, 0);
2276 * Explicitly terminate, in case there are multiples instances of
2279 ft2232_vid
[i
>> 1] = ft2232_pid
[i
>> 1] = 0;
2284 int ft2232_handle_latency_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2288 ft2232_latency
= atoi(args
[0]);
2292 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)