Dick Hollenbeck <dick@softplc.com> adds jtag_add_clocks() and implements those in...
[openocd.git] / src / jtag / ft2232.c
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
26
27 #if IS_CYGWIN == 1
28 #include "windows.h"
29 #endif
30
31 #include "replacements.h"
32
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"
39
40 /* system includes */
41 #include <string.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
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
51
52 /* enable this to debug io latency
53 */
54 #if 0
55 #define _DEBUG_USB_IO_
56 #endif
57
58 /* enable this to debug communication
59 */
60 #if 0
61 #define _DEBUG_USB_COMMS_
62 #endif
63
64 int ft2232_execute_queue(void);
65
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);
72
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);
78
79
80 /**
81 * Function ft2232_stableclocks
82 * will send out \a num_cycles on the TCK line while the TAP(s)
83 * are in a stable state. Calling code must ensure that current state is
84 * stable, that verification is not done in here.
85 * @param num_cycles is the count of clocks cycles to send.
86 * @return int - ERROR_OK or ERROR_JTAG_QUEUE_FAILED
87 */
88 static int ft2232_stableclocks(int num_cycles, jtag_command_t *cmd);
89
90
91 char *ft2232_device_desc = NULL;
92 char *ft2232_serial = NULL;
93 char *ft2232_layout = NULL;
94 unsigned char ft2232_latency = 2;
95
96 #define MAX_USB_IDS 8
97 /* vid = pid = 0 marks the end of the list */
98 static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
99 static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
100
101 typedef struct ft2232_layout_s
102 {
103 char* name;
104 int(*init)(void);
105 void(*reset)(int trst, int srst);
106 void(*blink)(void);
107 } ft2232_layout_t;
108
109 /* init procedures for supported layouts */
110 int usbjtag_init(void);
111 int jtagkey_init(void);
112 int olimex_jtag_init(void);
113 int flyswatter_init(void);
114 int turtle_init(void);
115 int comstick_init(void);
116 int stm32stick_init(void);
117 int axm0432_jtag_init(void);
118
119 /* reset procedures for supported layouts */
120 void usbjtag_reset(int trst, int srst);
121 void jtagkey_reset(int trst, int srst);
122 void olimex_jtag_reset(int trst, int srst);
123 void flyswatter_reset(int trst, int srst);
124 void turtle_reset(int trst, int srst);
125 void comstick_reset(int trst, int srst);
126 void stm32stick_reset(int trst, int srst);
127 void axm0432_jtag_reset(int trst, int srst);
128
129 /* blink procedures for layouts that support a blinking led */
130 void olimex_jtag_blink(void);
131 void turtle_jtag_blink(void);
132
133 ft2232_layout_t ft2232_layouts[] =
134 {
135 {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
136 {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
137 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
138 {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
139 {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
140 {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
141 {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
142 {"flyswatter", flyswatter_init, flyswatter_reset, NULL},
143 {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
144 {"comstick", comstick_init, comstick_reset, NULL},
145 {"stm32stick", stm32stick_init, stm32stick_reset, NULL},
146 {"axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL},
147 {NULL, NULL, NULL},
148 };
149
150 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
151
152 static ft2232_layout_t *layout;
153 static u8 low_output = 0x0;
154 static u8 low_direction = 0x0;
155 static u8 high_output = 0x0;
156 static u8 high_direction = 0x0;
157
158 #if BUILD_FT2232_FTD2XX == 1
159 static FT_HANDLE ftdih = NULL;
160 #elif BUILD_FT2232_LIBFTDI == 1
161 static struct ftdi_context ftdic;
162 #endif
163
164
165 static jtag_command_t *first_unsent; /* next command that has to be sent */
166 static int require_send;
167
168 static u8 *ft2232_buffer = NULL;
169 static int ft2232_buffer_size = 0;
170 static int ft2232_read_pointer = 0;
171 static int ft2232_expect_read = 0;
172
173 #define FT2232_BUFFER_SIZE 131072
174 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
175 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
176
177 jtag_interface_t ft2232_interface =
178 {
179 .name = "ft2232",
180 .execute_queue = ft2232_execute_queue,
181 .speed = ft2232_speed,
182 .speed_div = ft2232_speed_div,
183 .khz = ft2232_khz,
184 .register_commands = ft2232_register_commands,
185 .init = ft2232_init,
186 .quit = ft2232_quit,
187 };
188
189 int ft2232_write(u8 *buf, int size, u32* bytes_written)
190 {
191 #if BUILD_FT2232_FTD2XX == 1
192 FT_STATUS status;
193 DWORD dw_bytes_written;
194 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
195 {
196 *bytes_written = dw_bytes_written;
197 LOG_ERROR("FT_Write returned: %lu", status);
198 return ERROR_JTAG_DEVICE_ERROR;
199 }
200 else
201 {
202 *bytes_written = dw_bytes_written;
203 return ERROR_OK;
204 }
205 #elif BUILD_FT2232_LIBFTDI == 1
206 int retval;
207 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
208 {
209 *bytes_written = 0;
210 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
211 return ERROR_JTAG_DEVICE_ERROR;
212 }
213 else
214 {
215 *bytes_written = retval;
216 return ERROR_OK;
217 }
218 #endif
219 }
220
221 int ft2232_read(u8* buf, int size, u32* bytes_read)
222 {
223 #if BUILD_FT2232_FTD2XX == 1
224 DWORD dw_bytes_read;
225 FT_STATUS status;
226 int timeout = 5;
227 *bytes_read = 0;
228
229 while ((*bytes_read < size) && timeout--)
230 {
231 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
232 *bytes_read, &dw_bytes_read)) != FT_OK)
233 {
234 *bytes_read = 0;
235 LOG_ERROR("FT_Read returned: %lu", status);
236 return ERROR_JTAG_DEVICE_ERROR;
237 }
238 *bytes_read += dw_bytes_read;
239 }
240 #elif BUILD_FT2232_LIBFTDI == 1
241 int retval;
242 int timeout = 100;
243 *bytes_read = 0;
244
245 while ((*bytes_read < size) && timeout--)
246 {
247 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
248 {
249 *bytes_read = 0;
250 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
251 return ERROR_JTAG_DEVICE_ERROR;
252 }
253 *bytes_read += retval;
254 }
255 #endif
256
257 if (*bytes_read < size)
258 {
259 LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
260 return ERROR_JTAG_DEVICE_ERROR;
261 }
262
263 return ERROR_OK;
264 }
265
266 int ft2232_speed(int speed)
267 {
268 u8 buf[3];
269 int retval;
270 u32 bytes_written;
271
272 buf[0] = 0x86; /* command "set divisor" */
273 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=2.0MHz, ...*/
274 buf[2] = (speed >> 8) & 0xff; /* valueH */
275
276 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
277 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
278 {
279 LOG_ERROR("couldn't set FT2232 TCK speed");
280 return retval;
281 }
282
283 return ERROR_OK;
284 }
285
286 int ft2232_speed_div(int speed, int *khz)
287 {
288 /* Take a look in the FT2232 manual,
289 * AN2232C-01 Command Processor for
290 * MPSSE and MCU Host Bus. Chapter 3.8 */
291
292 *khz = 6000 / (1+speed);
293
294 return ERROR_OK;
295 }
296
297 int ft2232_khz(int khz, int *jtag_speed)
298 {
299 if (khz==0)
300 {
301 LOG_ERROR("RCLK not supported");
302 return ERROR_FAIL;
303 }
304 /* Take a look in the FT2232 manual,
305 * AN2232C-01 Command Processor for
306 * MPSSE and MCU Host Bus. Chapter 3.8
307 *
308 * We will calc here with a multiplier
309 * of 10 for better rounding later. */
310
311 /* Calc speed, (6000 / khz) - 1 */
312 /* Use 65000 for better rounding */
313 *jtag_speed = (60000 / khz) - 10;
314
315 /* Add 0.9 for rounding */
316 *jtag_speed += 9;
317
318 /* Calc real speed */
319 *jtag_speed = *jtag_speed / 10;
320
321 /* Check if speed is greater than 0 */
322 if (*jtag_speed < 0)
323 {
324 *jtag_speed = 0;
325 }
326
327 /* Check max value */
328 if (*jtag_speed > 0xFFFF)
329 {
330 *jtag_speed = 0xFFFF;
331 }
332
333 return ERROR_OK;
334 }
335
336 int ft2232_register_commands(struct command_context_s *cmd_ctx)
337 {
338 register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
339 COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
340 register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
341 COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
342 register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
343 COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
344 register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
345 COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
346 register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
347 COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
348 return ERROR_OK;
349 }
350
351 void ft2232_end_state(enum tap_state state)
352 {
353 if (tap_move_map[state] != -1)
354 end_state = state;
355 else
356 {
357 LOG_ERROR("BUG: %i is not a valid end state", state);
358 exit(-1);
359 }
360 }
361
362 void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
363 {
364 int num_bytes = (scan_size + 7) / 8;
365 int bits_left = scan_size;
366 int cur_byte = 0;
367
368 while(num_bytes-- > 1)
369 {
370 buffer[cur_byte] = BUFFER_READ;
371 cur_byte++;
372 bits_left -= 8;
373 }
374
375 buffer[cur_byte] = 0x0;
376
377 if (bits_left > 1)
378 {
379 buffer[cur_byte] = BUFFER_READ >> 1;
380 }
381
382 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
383
384 }
385
386 void ft2232_debug_dump_buffer(void)
387 {
388 int i;
389 char line[256];
390 char *line_p = line;
391
392 for (i = 0; i < ft2232_buffer_size; i++)
393 {
394 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
395 if (i % 16 == 15)
396 {
397 LOG_DEBUG("%s", line);
398 line_p = line;
399 }
400 }
401
402 if (line_p != line)
403 LOG_DEBUG("%s", line);
404 }
405
406 int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
407 {
408 jtag_command_t *cmd;
409 u8 *buffer;
410 int scan_size;
411 enum scan_type type;
412 int retval;
413 u32 bytes_written;
414 u32 bytes_read;
415
416 #ifdef _DEBUG_USB_IO_
417 struct timeval start, inter, inter2, end;
418 struct timeval d_inter, d_inter2, d_end;
419 #endif
420
421 #ifdef _DEBUG_USB_COMMS_
422 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
423 ft2232_debug_dump_buffer();
424 #endif
425
426 #ifdef _DEBUG_USB_IO_
427 gettimeofday(&start, NULL);
428 #endif
429
430 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
431 {
432 LOG_ERROR("couldn't write MPSSE commands to FT2232");
433 return retval;
434 }
435
436 #ifdef _DEBUG_USB_IO_
437 gettimeofday(&inter, NULL);
438 #endif
439
440 if (ft2232_expect_read)
441 {
442 int timeout = 100;
443 ft2232_buffer_size = 0;
444
445 #ifdef _DEBUG_USB_IO_
446 gettimeofday(&inter2, NULL);
447 #endif
448
449 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
450 {
451 LOG_ERROR("couldn't read from FT2232");
452 return retval;
453 }
454
455 #ifdef _DEBUG_USB_IO_
456 gettimeofday(&end, NULL);
457
458 timeval_subtract(&d_inter, &inter, &start);
459 timeval_subtract(&d_inter2, &inter2, &start);
460 timeval_subtract(&d_end, &end, &start);
461
462 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);
463 #endif
464
465
466 ft2232_buffer_size = bytes_read;
467
468 if (ft2232_expect_read != ft2232_buffer_size)
469 {
470 LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
471 ft2232_debug_dump_buffer();
472
473 exit(-1);
474 }
475
476 #ifdef _DEBUG_USB_COMMS_
477 LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
478 ft2232_debug_dump_buffer();
479 #endif
480 }
481
482 ft2232_expect_read = 0;
483 ft2232_read_pointer = 0;
484
485 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
486 * that wasn't handled by a caller-provided error handler
487 */
488 retval = ERROR_OK;
489
490 cmd = first;
491 while (cmd != last)
492 {
493 switch (cmd->type)
494 {
495 case JTAG_SCAN:
496 type = jtag_scan_type(cmd->cmd.scan);
497 if (type != SCAN_OUT)
498 {
499 scan_size = jtag_scan_size(cmd->cmd.scan);
500 buffer = calloc(CEIL(scan_size, 8), 1);
501 ft2232_read_scan(type, buffer, scan_size);
502 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
503 retval = ERROR_JTAG_QUEUE_FAILED;
504 free(buffer);
505 }
506 break;
507 default:
508 break;
509 }
510 cmd = cmd->next;
511 }
512
513 ft2232_buffer_size = 0;
514
515 return retval;
516 }
517
518 void ft2232_add_pathmove(pathmove_command_t *cmd)
519 {
520 int num_states = cmd->num_states;
521 u8 tms_byte;
522 int state_count;
523
524 state_count = 0;
525 while (num_states)
526 {
527 int bit_count = 0;
528
529 int num_states_batch = num_states > 7 ? 7 : num_states;
530
531 tms_byte = 0x0;
532 /* command "Clock Data to TMS/CS Pin (no Read)" */
533 BUFFER_ADD = 0x4b;
534 /* number of states remaining */
535 BUFFER_ADD = num_states_batch - 1;
536
537 while (num_states_batch--)
538 {
539 if (tap_transitions[cur_state].low == cmd->path[state_count])
540 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
541 else if (tap_transitions[cur_state].high == cmd->path[state_count])
542 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
543 else
544 {
545 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(cmd->path[state_count]));
546 exit(-1);
547 }
548
549 cur_state = cmd->path[state_count];
550 state_count++;
551 num_states--;
552 }
553
554 BUFFER_ADD = tms_byte;
555 }
556
557 end_state = cur_state;
558 }
559
560 void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
561 {
562 int num_bytes = (scan_size + 7) / 8;
563 int bits_left = scan_size;
564 int cur_byte = 0;
565 int last_bit;
566
567 if (!((!ir_scan && (cur_state == TAP_DRSHIFT)) || (ir_scan && (cur_state == TAP_IRSHIFT))))
568 {
569 /* command "Clock Data to TMS/CS Pin (no Read)" */
570 BUFFER_ADD = 0x4b;
571 /* scan 7 bit */
572 BUFFER_ADD = 0x6;
573 /* TMS data bits */
574 if (ir_scan)
575 {
576 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IRSHIFT);
577 cur_state = TAP_IRSHIFT;
578 }
579 else
580 {
581 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
582 cur_state = TAP_DRSHIFT;
583 }
584 /* LOG_DEBUG("added TMS scan (no read)"); */
585 }
586
587 /* add command for complete bytes */
588 while (num_bytes > 1)
589 {
590 int thisrun_bytes;
591 if (type == SCAN_IO)
592 {
593 /* Clock Data Bytes In and Out LSB First */
594 BUFFER_ADD = 0x39;
595 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
596 }
597 else if (type == SCAN_OUT)
598 {
599 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
600 BUFFER_ADD = 0x19;
601 /* LOG_DEBUG("added TDI bytes (o)"); */
602 }
603 else if (type == SCAN_IN)
604 {
605 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
606 BUFFER_ADD = 0x28;
607 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
608 }
609 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
610 num_bytes -= thisrun_bytes;
611 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
612 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
613 if (type != SCAN_IN)
614 {
615 /* add complete bytes */
616 while(thisrun_bytes-- > 0)
617 {
618 BUFFER_ADD = buffer[cur_byte];
619 cur_byte++;
620 bits_left -= 8;
621 }
622 }
623 else /* (type == SCAN_IN) */
624 {
625 bits_left -= 8 * (thisrun_bytes);
626 }
627 }
628
629 /* the most signifcant bit is scanned during TAP movement */
630 if (type != SCAN_IN)
631 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
632 else
633 last_bit = 0;
634
635 /* process remaining bits but the last one */
636 if (bits_left > 1)
637 {
638 if (type == SCAN_IO)
639 {
640 /* Clock Data Bits In and Out LSB First */
641 BUFFER_ADD = 0x3b;
642 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
643 }
644 else if (type == SCAN_OUT)
645 {
646 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
647 BUFFER_ADD = 0x1b;
648 /* LOG_DEBUG("added TDI bits (o)"); */
649 }
650 else if (type == SCAN_IN)
651 {
652 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
653 BUFFER_ADD = 0x2a;
654 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
655 }
656 BUFFER_ADD = bits_left - 2;
657 if (type != SCAN_IN)
658 BUFFER_ADD = buffer[cur_byte];
659 }
660
661 if ((ir_scan && (end_state == TAP_IRSHIFT)) ||
662 (!ir_scan && (end_state == TAP_DRSHIFT)))
663 {
664 if (type == SCAN_IO)
665 {
666 /* Clock Data Bits In and Out LSB First */
667 BUFFER_ADD = 0x3b;
668 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
669 }
670 else if (type == SCAN_OUT)
671 {
672 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
673 BUFFER_ADD = 0x1b;
674 /* LOG_DEBUG("added TDI bits (o)"); */
675 }
676 else if (type == SCAN_IN)
677 {
678 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
679 BUFFER_ADD = 0x2a;
680 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
681 }
682 BUFFER_ADD = 0x0;
683 BUFFER_ADD = last_bit;
684 }
685 else
686 {
687 /* move from Shift-IR/DR to end state */
688 if (type != SCAN_OUT)
689 {
690 /* Clock Data to TMS/CS Pin with Read */
691 BUFFER_ADD = 0x6b;
692 /* LOG_DEBUG("added TMS scan (read)"); */
693 }
694 else
695 {
696 /* Clock Data to TMS/CS Pin (no Read) */
697 BUFFER_ADD = 0x4b;
698 /* LOG_DEBUG("added TMS scan (no read)"); */
699 }
700 BUFFER_ADD = 0x6;
701 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
702 cur_state = end_state;
703 }
704 }
705
706 int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
707 {
708 int num_bytes = (scan_size + 7) / 8;
709 int bits_left = scan_size;
710 int cur_byte = 0;
711 int last_bit;
712 u8 *receive_buffer = malloc(CEIL(scan_size, 8));
713 u8 *receive_pointer = receive_buffer;
714 u32 bytes_written;
715 u32 bytes_read;
716 int retval;
717 int thisrun_read = 0;
718
719 if (cmd->ir_scan)
720 {
721 LOG_ERROR("BUG: large IR scans are not supported");
722 exit(-1);
723 }
724
725 if (cur_state != TAP_DRSHIFT)
726 {
727 /* command "Clock Data to TMS/CS Pin (no Read)" */
728 BUFFER_ADD = 0x4b;
729 /* scan 7 bit */
730 BUFFER_ADD = 0x6;
731 /* TMS data bits */
732 BUFFER_ADD = TAP_MOVE(cur_state, TAP_DRSHIFT);
733 cur_state = TAP_DRSHIFT;
734 }
735
736 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
737 {
738 LOG_ERROR("couldn't write MPSSE commands to FT2232");
739 exit(-1);
740 }
741 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
742 ft2232_buffer_size = 0;
743
744 /* add command for complete bytes */
745 while (num_bytes > 1)
746 {
747 int thisrun_bytes;
748
749 if (type == SCAN_IO)
750 {
751 /* Clock Data Bytes In and Out LSB First */
752 BUFFER_ADD = 0x39;
753 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
754 }
755 else if (type == SCAN_OUT)
756 {
757 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
758 BUFFER_ADD = 0x19;
759 /* LOG_DEBUG("added TDI bytes (o)"); */
760 }
761 else if (type == SCAN_IN)
762 {
763 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
764 BUFFER_ADD = 0x28;
765 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
766 }
767 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
768 thisrun_read = thisrun_bytes;
769 num_bytes -= thisrun_bytes;
770 BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
771 BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
772 if (type != SCAN_IN)
773 {
774 /* add complete bytes */
775 while(thisrun_bytes-- > 0)
776 {
777 BUFFER_ADD = buffer[cur_byte];
778 cur_byte++;
779 bits_left -= 8;
780 }
781 }
782 else /* (type == SCAN_IN) */
783 {
784 bits_left -= 8 * (thisrun_bytes);
785 }
786
787 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
788 {
789 LOG_ERROR("couldn't write MPSSE commands to FT2232");
790 exit(-1);
791 }
792 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
793 ft2232_buffer_size = 0;
794
795 if (type != SCAN_OUT)
796 {
797 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
798 {
799 LOG_ERROR("couldn't read from FT2232");
800 exit(-1);
801 }
802 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
803 receive_pointer += bytes_read;
804 }
805 }
806
807 thisrun_read = 0;
808
809 /* the most signifcant bit is scanned during TAP movement */
810 if (type != SCAN_IN)
811 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
812 else
813 last_bit = 0;
814
815 /* process remaining bits but the last one */
816 if (bits_left > 1)
817 {
818 if (type == SCAN_IO)
819 {
820 /* Clock Data Bits In and Out LSB First */
821 BUFFER_ADD = 0x3b;
822 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
823 }
824 else if (type == SCAN_OUT)
825 {
826 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
827 BUFFER_ADD = 0x1b;
828 /* LOG_DEBUG("added TDI bits (o)"); */
829 }
830 else if (type == SCAN_IN)
831 {
832 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
833 BUFFER_ADD = 0x2a;
834 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
835 }
836 BUFFER_ADD = bits_left - 2;
837 if (type != SCAN_IN)
838 BUFFER_ADD = buffer[cur_byte];
839
840 if (type != SCAN_OUT)
841 thisrun_read += 2;
842 }
843
844 if (end_state == TAP_DRSHIFT)
845 {
846 if (type == SCAN_IO)
847 {
848 /* Clock Data Bits In and Out LSB First */
849 BUFFER_ADD = 0x3b;
850 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
851 }
852 else if (type == SCAN_OUT)
853 {
854 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
855 BUFFER_ADD = 0x1b;
856 /* LOG_DEBUG("added TDI bits (o)"); */
857 }
858 else if (type == SCAN_IN)
859 {
860 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
861 BUFFER_ADD = 0x2a;
862 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
863 }
864 BUFFER_ADD = 0x0;
865 BUFFER_ADD = last_bit;
866 }
867 else
868 {
869 /* move from Shift-IR/DR to end state */
870 if (type != SCAN_OUT)
871 {
872 /* Clock Data to TMS/CS Pin with Read */
873 BUFFER_ADD = 0x6b;
874 /* LOG_DEBUG("added TMS scan (read)"); */
875 }
876 else
877 {
878 /* Clock Data to TMS/CS Pin (no Read) */
879 BUFFER_ADD = 0x4b;
880 /* LOG_DEBUG("added TMS scan (no read)"); */
881 }
882 BUFFER_ADD = 0x6;
883 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
884 cur_state = end_state;
885 }
886
887 if (type != SCAN_OUT)
888 thisrun_read += 1;
889
890 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
891 {
892 LOG_ERROR("couldn't write MPSSE commands to FT2232");
893 exit(-1);
894 }
895 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
896 ft2232_buffer_size = 0;
897
898 if (type != SCAN_OUT)
899 {
900 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
901 {
902 LOG_ERROR("couldn't read from FT2232");
903 exit(-1);
904 }
905 LOG_DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
906 receive_pointer += bytes_read;
907 }
908
909 return ERROR_OK;
910 }
911
912 int ft2232_predict_scan_out(int scan_size, enum scan_type type)
913 {
914 int predicted_size = 3;
915 int num_bytes = (scan_size - 1) / 8;
916
917 if (cur_state != TAP_DRSHIFT)
918 predicted_size += 3;
919
920 if (type == SCAN_IN) /* only from device to host */
921 {
922 /* complete bytes */
923 predicted_size += CEIL(num_bytes, 65536) * 3;
924 /* remaining bits - 1 (up to 7) */
925 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
926 }
927 else /* host to device, or bidirectional */
928 {
929 /* complete bytes */
930 predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
931 /* remaining bits -1 (up to 7) */
932 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
933 }
934
935 return predicted_size;
936 }
937
938 int ft2232_predict_scan_in(int scan_size, enum scan_type type)
939 {
940 int predicted_size = 0;
941
942 if (type != SCAN_OUT)
943 {
944 /* complete bytes */
945 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
946 /* remaining bits - 1 */
947 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
948 /* last bit (from TMS scan) */
949 predicted_size += 1;
950 }
951
952 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
953
954 return predicted_size;
955 }
956
957 void usbjtag_reset(int trst, int srst)
958 {
959 if (trst == 1)
960 {
961 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
962 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
963 else
964 low_output &= ~nTRST; /* switch output low */
965 }
966 else if (trst == 0)
967 {
968 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
969 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
970 else
971 low_output |= nTRST; /* switch output high */
972 }
973
974 if (srst == 1)
975 {
976 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
977 low_output &= ~nSRST; /* switch output low */
978 else
979 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
980 }
981 else if (srst == 0)
982 {
983 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
984 low_output |= nSRST; /* switch output high */
985 else
986 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
987 }
988
989 /* command "set data bits low byte" */
990 BUFFER_ADD = 0x80;
991 BUFFER_ADD = low_output;
992 BUFFER_ADD = low_direction;
993
994 }
995
996 void jtagkey_reset(int trst, int srst)
997 {
998 if (trst == 1)
999 {
1000 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1001 high_output &= ~nTRSTnOE;
1002 else
1003 high_output &= ~nTRST;
1004 }
1005 else if (trst == 0)
1006 {
1007 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1008 high_output |= nTRSTnOE;
1009 else
1010 high_output |= nTRST;
1011 }
1012
1013 if (srst == 1)
1014 {
1015 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1016 high_output &= ~nSRST;
1017 else
1018 high_output &= ~nSRSTnOE;
1019 }
1020 else if (srst == 0)
1021 {
1022 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1023 high_output |= nSRST;
1024 else
1025 high_output |= nSRSTnOE;
1026 }
1027
1028 /* command "set data bits high byte" */
1029 BUFFER_ADD = 0x82;
1030 BUFFER_ADD = high_output;
1031 BUFFER_ADD = high_direction;
1032 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1033 }
1034
1035 void olimex_jtag_reset(int trst, int srst)
1036 {
1037 if (trst == 1)
1038 {
1039 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1040 high_output &= ~nTRSTnOE;
1041 else
1042 high_output &= ~nTRST;
1043 }
1044 else if (trst == 0)
1045 {
1046 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1047 high_output |= nTRSTnOE;
1048 else
1049 high_output |= nTRST;
1050 }
1051
1052 if (srst == 1)
1053 {
1054 high_output |= nSRST;
1055 }
1056 else if (srst == 0)
1057 {
1058 high_output &= ~nSRST;
1059 }
1060
1061 /* command "set data bits high byte" */
1062 BUFFER_ADD = 0x82;
1063 BUFFER_ADD = high_output;
1064 BUFFER_ADD = high_direction;
1065 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1066 }
1067
1068 void axm0432_jtag_reset(int trst, int srst)
1069 {
1070 if (trst == 1)
1071 {
1072 cur_state = TAP_RESET;
1073 high_output &= ~nTRST;
1074 }
1075 else if (trst == 0)
1076 {
1077 high_output |= nTRST;
1078 }
1079
1080 if (srst == 1)
1081 {
1082 high_output &= ~nSRST;
1083 }
1084 else if (srst == 0)
1085 {
1086 high_output |= nSRST;
1087 }
1088
1089 /* command "set data bits low byte" */
1090 BUFFER_ADD = 0x82;
1091 BUFFER_ADD = high_output;
1092 BUFFER_ADD = high_direction;
1093 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1094 }
1095
1096 void flyswatter_reset(int trst, int srst)
1097 {
1098 if (trst == 1)
1099 {
1100 low_output &= ~nTRST;
1101 }
1102 else if (trst == 0)
1103 {
1104 low_output |= nTRST;
1105 }
1106
1107 if (srst == 1)
1108 {
1109 low_output |= nSRST;
1110 }
1111 else if (srst == 0)
1112 {
1113 low_output &= ~nSRST;
1114 }
1115
1116 /* command "set data bits low byte" */
1117 BUFFER_ADD = 0x80;
1118 BUFFER_ADD = low_output;
1119 BUFFER_ADD = low_direction;
1120 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1121 }
1122
1123 void turtle_reset(int trst, int srst)
1124 {
1125 trst = trst;
1126
1127 if (srst == 1)
1128 {
1129 low_output |= nSRST;
1130 }
1131 else if (srst == 0)
1132 {
1133 low_output &= ~nSRST;
1134 }
1135
1136 /* command "set data bits low byte" */
1137 BUFFER_ADD = 0x80;
1138 BUFFER_ADD = low_output;
1139 BUFFER_ADD = low_direction;
1140 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1141 }
1142
1143 void comstick_reset(int trst, int srst)
1144 {
1145 if (trst == 1)
1146 {
1147 high_output &= ~nTRST;
1148 }
1149 else if (trst == 0)
1150 {
1151 high_output |= nTRST;
1152 }
1153
1154 if (srst == 1)
1155 {
1156 high_output &= ~nSRST;
1157 }
1158 else if (srst == 0)
1159 {
1160 high_output |= nSRST;
1161 }
1162
1163 /* command "set data bits high byte" */
1164 BUFFER_ADD = 0x82;
1165 BUFFER_ADD = high_output;
1166 BUFFER_ADD = high_direction;
1167 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1168 }
1169
1170 void stm32stick_reset(int trst, int srst)
1171 {
1172 if (trst == 1)
1173 {
1174 high_output &= ~nTRST;
1175 }
1176 else if (trst == 0)
1177 {
1178 high_output |= nTRST;
1179 }
1180
1181 if (srst == 1)
1182 {
1183 low_output &= ~nSRST;
1184 }
1185 else if (srst == 0)
1186 {
1187 low_output |= nSRST;
1188 }
1189
1190 /* command "set data bits low byte" */
1191 BUFFER_ADD = 0x80;
1192 BUFFER_ADD = low_output;
1193 BUFFER_ADD = low_direction;
1194
1195 /* command "set data bits high byte" */
1196 BUFFER_ADD = 0x82;
1197 BUFFER_ADD = high_output;
1198 BUFFER_ADD = high_direction;
1199 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1200 }
1201
1202 int ft2232_execute_queue()
1203 {
1204 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1205 u8 *buffer;
1206 int scan_size; /* size of IR or DR scan */
1207 enum scan_type type;
1208 int i;
1209 int predicted_size = 0;
1210 int retval;
1211
1212 first_unsent = cmd; /* next command that has to be sent */
1213 require_send = 0;
1214
1215 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1216 * that wasn't handled by a caller-provided error handler
1217 */
1218 retval = ERROR_OK;
1219
1220 ft2232_buffer_size = 0;
1221 ft2232_expect_read = 0;
1222
1223 /* blink, if the current layout has that feature */
1224 if (layout->blink)
1225 layout->blink();
1226
1227 while (cmd)
1228 {
1229 switch(cmd->type)
1230 {
1231 case JTAG_END_STATE:
1232 if (cmd->cmd.end_state->end_state != -1)
1233 ft2232_end_state(cmd->cmd.end_state->end_state);
1234 break;
1235
1236 case JTAG_RESET:
1237 /* only send the maximum buffer size that FT2232C can handle */
1238 predicted_size = 3;
1239 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1240 {
1241 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1242 retval = ERROR_JTAG_QUEUE_FAILED;
1243 require_send = 0;
1244 first_unsent = cmd;
1245 }
1246
1247 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1248 {
1249 cur_state = TAP_RESET;
1250 }
1251 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1252 require_send = 1;
1253
1254 #ifdef _DEBUG_JTAG_IO_
1255 LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1256 #endif
1257 break;
1258
1259 case JTAG_RUNTEST:
1260 /* only send the maximum buffer size that FT2232C can handle */
1261 predicted_size = 0;
1262 if (cur_state != TAP_IDLE)
1263 predicted_size += 3;
1264 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
1265 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_IDLE))
1266 predicted_size += 3;
1267 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_IDLE))
1268 predicted_size += 3;
1269 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1270 {
1271 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1272 retval = ERROR_JTAG_QUEUE_FAILED;
1273 require_send = 0;
1274 first_unsent = cmd;
1275 }
1276 if (cur_state != TAP_IDLE)
1277 {
1278 /* command "Clock Data to TMS/CS Pin (no Read)" */
1279 BUFFER_ADD = 0x4b;
1280 /* scan 7 bit */
1281 BUFFER_ADD = 0x6;
1282 /* TMS data bits */
1283 BUFFER_ADD = TAP_MOVE(cur_state, TAP_IDLE);
1284 cur_state = TAP_IDLE;
1285 require_send = 1;
1286 }
1287 i = cmd->cmd.runtest->num_cycles;
1288 while (i > 0)
1289 {
1290 /* command "Clock Data to TMS/CS Pin (no Read)" */
1291 BUFFER_ADD = 0x4b;
1292 /* scan 7 bit */
1293 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
1294 /* TMS data bits */
1295 BUFFER_ADD = 0x0;
1296 cur_state = TAP_IDLE;
1297 i -= (i > 7) ? 7 : i;
1298 /* LOG_DEBUG("added TMS scan (no read)"); */
1299 }
1300 if (cmd->cmd.runtest->end_state != -1)
1301 ft2232_end_state(cmd->cmd.runtest->end_state);
1302 if (cur_state != end_state)
1303 {
1304 /* command "Clock Data to TMS/CS Pin (no Read)" */
1305 BUFFER_ADD = 0x4b;
1306 /* scan 7 bit */
1307 BUFFER_ADD = 0x6;
1308 /* TMS data bits */
1309 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1310 cur_state = end_state;
1311 /* LOG_DEBUG("added TMS scan (no read)"); */
1312 }
1313 require_send = 1;
1314 #ifdef _DEBUG_JTAG_IO_
1315 LOG_DEBUG("runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, jtag_state_name(end_state));
1316 #endif
1317 break;
1318
1319 case JTAG_STATEMOVE:
1320 /* only send the maximum buffer size that FT2232C can handle */
1321 predicted_size = 3;
1322 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1323 {
1324 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1325 retval = ERROR_JTAG_QUEUE_FAILED;
1326 require_send = 0;
1327 first_unsent = cmd;
1328 }
1329 if (cmd->cmd.statemove->end_state != -1)
1330 ft2232_end_state(cmd->cmd.statemove->end_state);
1331 /* command "Clock Data to TMS/CS Pin (no Read)" */
1332 BUFFER_ADD = 0x4b;
1333 /* scan 7 bit */
1334 BUFFER_ADD = 0x6;
1335 /* TMS data bits */
1336 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
1337 /* LOG_DEBUG("added TMS scan (no read)"); */
1338 cur_state = end_state;
1339 require_send = 1;
1340 #ifdef _DEBUG_JTAG_IO_
1341 LOG_DEBUG("statemove: %s", jtag_state_name(end_state));
1342 #endif
1343 break;
1344
1345 case JTAG_PATHMOVE:
1346 /* only send the maximum buffer size that FT2232C can handle */
1347 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
1348 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1349 {
1350 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1351 retval = ERROR_JTAG_QUEUE_FAILED;
1352 require_send = 0;
1353 first_unsent = cmd;
1354 }
1355 ft2232_add_pathmove(cmd->cmd.pathmove);
1356 require_send = 1;
1357 #ifdef _DEBUG_JTAG_IO_
1358 LOG_DEBUG("pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
1359 jtag_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
1360 #endif
1361 break;
1362
1363 case JTAG_SCAN:
1364 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1365 type = jtag_scan_type(cmd->cmd.scan);
1366 predicted_size = ft2232_predict_scan_out(scan_size, type);
1367 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1368 {
1369 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1370 /* unsent commands before this */
1371 if (first_unsent != cmd)
1372 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1373 retval = ERROR_JTAG_QUEUE_FAILED;
1374
1375 /* current command */
1376 if (cmd->cmd.scan->end_state != -1)
1377 ft2232_end_state(cmd->cmd.scan->end_state);
1378 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1379 require_send = 0;
1380 first_unsent = cmd->next;
1381 if (buffer)
1382 free(buffer);
1383 break;
1384 }
1385 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1386 {
1387 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
1388 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1389 retval = ERROR_JTAG_QUEUE_FAILED;
1390 require_send = 0;
1391 first_unsent = cmd;
1392 }
1393 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1394 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1395 if (cmd->cmd.scan->end_state != -1)
1396 ft2232_end_state(cmd->cmd.scan->end_state);
1397 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1398 require_send = 1;
1399 if (buffer)
1400 free(buffer);
1401 #ifdef _DEBUG_JTAG_IO_
1402 LOG_DEBUG("%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1403 jtag_state_name(end_state));
1404 #endif
1405 break;
1406
1407 case JTAG_SLEEP:
1408 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1409 retval = ERROR_JTAG_QUEUE_FAILED;
1410 first_unsent = cmd->next;
1411 jtag_sleep(cmd->cmd.sleep->us);
1412 #ifdef _DEBUG_JTAG_IO_
1413 LOG_DEBUG("sleep %i usec while in %s", cmd->cmd.sleep->us, jtag_state_name(cur_state));
1414 #endif
1415 break;
1416
1417 case JTAG_STABLECLOCKS:
1418 /* "if (tap_move_map[cur_state] != -1)" is of no help when cur_state==TAP_IDLE */
1419 switch(cur_state)
1420 {
1421 case TAP_DRSHIFT:
1422 case TAP_IDLE:
1423 case TAP_RESET:
1424 case TAP_DRPAUSE:
1425 case TAP_IRSHIFT:
1426 case TAP_IRPAUSE:
1427 break; /* above stable states are OK */
1428 default:
1429 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
1430 jtag_state_name(cur_state) );
1431 retval = ERROR_JTAG_QUEUE_FAILED;
1432 }
1433
1434 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1435 retval = ERROR_JTAG_QUEUE_FAILED;
1436 #ifdef _DEBUG_JTAG_IO_
1437 LOG_DEBUG("clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, jtag_state_name(cur_state));
1438 #endif
1439 break;
1440
1441 default:
1442 LOG_ERROR("BUG: unknown JTAG command type encountered");
1443 exit(-1);
1444 }
1445 cmd = cmd->next;
1446 }
1447
1448 if (require_send > 0)
1449 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1450 retval = ERROR_JTAG_QUEUE_FAILED;
1451
1452 return retval;
1453 }
1454
1455 #if BUILD_FT2232_FTD2XX == 1
1456 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1457 {
1458 FT_STATUS status;
1459 DWORD openex_flags = 0;
1460 char *openex_string = NULL;
1461 u8 latency_timer;
1462
1463 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",ft2232_layout, vid, pid);
1464
1465 #if IS_WIN32 == 0
1466 /* Add non-standard Vid/Pid to the linux driver */
1467 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1468 {
1469 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1470 }
1471 #endif
1472
1473 if (ft2232_device_desc && ft2232_serial)
1474 {
1475 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1476 ft2232_device_desc = NULL;
1477 }
1478
1479 if (ft2232_device_desc)
1480 {
1481 openex_string = ft2232_device_desc;
1482 openex_flags = FT_OPEN_BY_DESCRIPTION;
1483 }
1484 else if (ft2232_serial)
1485 {
1486 openex_string = ft2232_serial;
1487 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1488 }
1489 else
1490 {
1491 LOG_ERROR("neither device description nor serial number specified");
1492 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1493
1494 return ERROR_JTAG_INIT_FAILED;
1495 }
1496
1497 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1498 {
1499 DWORD num_devices;
1500
1501 if (more) {
1502 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1503 *try_more = 1;
1504 return ERROR_JTAG_INIT_FAILED;
1505 }
1506 LOG_ERROR("unable to open ftdi device: %lu", status);
1507 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1508 if (status == FT_OK)
1509 {
1510 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1511 int i;
1512
1513 for (i = 0; i < num_devices; i++)
1514 desc_array[i] = malloc(64);
1515 desc_array[num_devices] = NULL;
1516
1517 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1518
1519 if (status == FT_OK)
1520 {
1521 LOG_ERROR("ListDevices: %lu\n", num_devices);
1522 for (i = 0; i < num_devices; i++)
1523 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1524 }
1525
1526 for (i = 0; i < num_devices; i++)
1527 free(desc_array[i]);
1528 free(desc_array);
1529 }
1530 else
1531 {
1532 LOG_ERROR("ListDevices: NONE\n");
1533 }
1534 return ERROR_JTAG_INIT_FAILED;
1535 }
1536
1537 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1538 {
1539 LOG_ERROR("unable to set latency timer: %lu", status);
1540 return ERROR_JTAG_INIT_FAILED;
1541 }
1542
1543 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1544 {
1545 LOG_ERROR("unable to get latency timer: %lu", status);
1546 return ERROR_JTAG_INIT_FAILED;
1547 }
1548 else
1549 {
1550 LOG_DEBUG("current latency timer: %i", latency_timer);
1551 }
1552
1553 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1554 {
1555 LOG_ERROR("unable to set timeouts: %lu", status);
1556 return ERROR_JTAG_INIT_FAILED;
1557 }
1558
1559 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1560 {
1561 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1562 return ERROR_JTAG_INIT_FAILED;
1563 }
1564
1565 return ERROR_OK;
1566 }
1567
1568 static int ft2232_purge_ftd2xx(void)
1569 {
1570 FT_STATUS status;
1571
1572 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1573 {
1574 LOG_ERROR("error purging ftd2xx device: %lu", status);
1575 return ERROR_JTAG_INIT_FAILED;
1576 }
1577
1578 return ERROR_OK;
1579 }
1580 #endif /* BUILD_FT2232_FTD2XX == 1 */
1581
1582 #if BUILD_FT2232_LIBFTDI == 1
1583 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1584 {
1585 u8 latency_timer;
1586
1587 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1588 ft2232_layout, vid, pid);
1589
1590 if (ftdi_init(&ftdic) < 0)
1591 return ERROR_JTAG_INIT_FAILED;
1592
1593 /* context, vendor id, product id */
1594 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1595 ft2232_serial) < 0) {
1596 if (more)
1597 LOG_WARNING("unable to open ftdi device (trying more): %s",
1598 ftdic.error_str);
1599 else
1600 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1601 *try_more = 1;
1602 return ERROR_JTAG_INIT_FAILED;
1603 }
1604
1605 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1606 {
1607 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1608 return ERROR_JTAG_INIT_FAILED;
1609 }
1610
1611 if (ftdi_usb_reset(&ftdic) < 0)
1612 {
1613 LOG_ERROR("unable to reset ftdi device");
1614 return ERROR_JTAG_INIT_FAILED;
1615 }
1616
1617 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1618 {
1619 LOG_ERROR("unable to set latency timer");
1620 return ERROR_JTAG_INIT_FAILED;
1621 }
1622
1623 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1624 {
1625 LOG_ERROR("unable to get latency timer");
1626 return ERROR_JTAG_INIT_FAILED;
1627 }
1628 else
1629 {
1630 LOG_DEBUG("current latency timer: %i", latency_timer);
1631 }
1632
1633 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1634
1635 return ERROR_OK;
1636 }
1637
1638 static int ft2232_purge_libftdi(void)
1639 {
1640 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1641 {
1642 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1643 return ERROR_JTAG_INIT_FAILED;
1644 }
1645
1646 return ERROR_OK;
1647 }
1648 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1649
1650 int ft2232_init(void)
1651 {
1652 u8 buf[1];
1653 int retval;
1654 u32 bytes_written;
1655 ft2232_layout_t *cur_layout = ft2232_layouts;
1656 int i;
1657
1658 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1659 {
1660 ft2232_layout = "usbjtag";
1661 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1662 }
1663
1664 while (cur_layout->name)
1665 {
1666 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1667 {
1668 layout = cur_layout;
1669 break;
1670 }
1671 cur_layout++;
1672 }
1673
1674 if (!layout)
1675 {
1676 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1677 return ERROR_JTAG_INIT_FAILED;
1678 }
1679
1680 for (i = 0; 1; i++) {
1681 /*
1682 * "more indicates that there are more IDs to try, so we should
1683 * not print an error for an ID mismatch (but for anything
1684 * else, we should).
1685 *
1686 * try_more indicates that the error code returned indicates an
1687 * ID mismatch (and nothing else) and that we should proceeed
1688 * with the next ID pair.
1689 */
1690 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1691 int try_more = 0;
1692
1693 #if BUILD_FT2232_FTD2XX == 1
1694 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1695 more, &try_more);
1696 #elif BUILD_FT2232_LIBFTDI == 1
1697 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1698 more, &try_more);
1699 #endif
1700 if (retval >= 0)
1701 break;
1702 if (!more || !try_more)
1703 return retval;
1704 }
1705
1706 ft2232_buffer_size = 0;
1707 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1708
1709 if (layout->init() != ERROR_OK)
1710 return ERROR_JTAG_INIT_FAILED;
1711
1712 ft2232_speed(jtag_speed);
1713
1714 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1715 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1716 {
1717 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1718 return ERROR_JTAG_INIT_FAILED;
1719 }
1720
1721 #if BUILD_FT2232_FTD2XX == 1
1722 return ft2232_purge_ftd2xx();
1723 #elif BUILD_FT2232_LIBFTDI == 1
1724 return ft2232_purge_libftdi();
1725 #endif
1726
1727 return ERROR_OK;
1728 }
1729
1730 int usbjtag_init(void)
1731 {
1732 u8 buf[3];
1733 u32 bytes_written;
1734
1735 low_output = 0x08;
1736 low_direction = 0x0b;
1737
1738 if (strcmp(ft2232_layout, "usbjtag") == 0)
1739 {
1740 nTRST = 0x10;
1741 nTRSTnOE = 0x10;
1742 nSRST = 0x40;
1743 nSRSTnOE = 0x40;
1744 }
1745 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1746 {
1747 nTRST = 0x10;
1748 nTRSTnOE = 0x10;
1749 nSRST = 0x20;
1750 nSRSTnOE = 0x20;
1751 }
1752 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1753 {
1754 nTRST = 0x0;
1755 nTRSTnOE = 0x00;
1756 nSRST = 0x20;
1757 nSRSTnOE = 0x20;
1758 low_output = 0x88;
1759 low_direction = 0x8b;
1760 }
1761 else
1762 {
1763 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1764 return ERROR_JTAG_INIT_FAILED;
1765 }
1766
1767 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1768 {
1769 low_direction &= ~nTRSTnOE; /* nTRST input */
1770 low_output &= ~nTRST; /* nTRST = 0 */
1771 }
1772 else
1773 {
1774 low_direction |= nTRSTnOE; /* nTRST output */
1775 low_output |= nTRST; /* nTRST = 1 */
1776 }
1777
1778 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1779 {
1780 low_direction |= nSRSTnOE; /* nSRST output */
1781 low_output |= nSRST; /* nSRST = 1 */
1782 }
1783 else
1784 {
1785 low_direction &= ~nSRSTnOE; /* nSRST input */
1786 low_output &= ~nSRST; /* nSRST = 0 */
1787 }
1788
1789 /* initialize low byte for jtag */
1790 buf[0] = 0x80; /* command "set data bits low byte" */
1791 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1792 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1793 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1794
1795 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1796 {
1797 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1798 return ERROR_JTAG_INIT_FAILED;
1799 }
1800
1801 return ERROR_OK;
1802 }
1803
1804 int axm0432_jtag_init(void)
1805 {
1806 u8 buf[3];
1807 u32 bytes_written;
1808
1809 low_output = 0x08;
1810 low_direction = 0x2b;
1811
1812 /* initialize low byte for jtag */
1813 buf[0] = 0x80; /* command "set data bits low byte" */
1814 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1815 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1816 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1817
1818 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1819 {
1820 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1821 return ERROR_JTAG_INIT_FAILED;
1822 }
1823
1824 if (strcmp(layout->name, "axm0432_jtag") == 0)
1825 {
1826 nTRST = 0x08;
1827 nTRSTnOE = 0x0; /* No output enable for TRST*/
1828 nSRST = 0x04;
1829 nSRSTnOE = 0x0; /* No output enable for SRST*/
1830 }
1831 else
1832 {
1833 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1834 exit(-1);
1835 }
1836
1837 high_output = 0x0;
1838 high_direction = 0x0c;
1839
1840 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1841 {
1842 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1843 }
1844 else
1845 {
1846 high_output |= nTRST;
1847 }
1848
1849 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1850 {
1851 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1852 }
1853 else
1854 {
1855 high_output |= nSRST;
1856 }
1857
1858 /* initialize high port */
1859 buf[0] = 0x82; /* command "set data bits high byte" */
1860 buf[1] = high_output; /* value */
1861 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1862 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1863
1864 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1865 {
1866 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1867 return ERROR_JTAG_INIT_FAILED;
1868 }
1869
1870 return ERROR_OK;
1871 }
1872
1873 int jtagkey_init(void)
1874 {
1875 u8 buf[3];
1876 u32 bytes_written;
1877
1878 low_output = 0x08;
1879 low_direction = 0x1b;
1880
1881 /* initialize low byte for jtag */
1882 buf[0] = 0x80; /* command "set data bits low byte" */
1883 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1884 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1885 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1886
1887 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1888 {
1889 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1890 return ERROR_JTAG_INIT_FAILED;
1891 }
1892
1893 if (strcmp(layout->name, "jtagkey") == 0)
1894 {
1895 nTRST = 0x01;
1896 nTRSTnOE = 0x4;
1897 nSRST = 0x02;
1898 nSRSTnOE = 0x08;
1899 }
1900 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1901 (strcmp(layout->name, "oocdlink") == 0))
1902 {
1903 nTRST = 0x02;
1904 nTRSTnOE = 0x1;
1905 nSRST = 0x08;
1906 nSRSTnOE = 0x04;
1907 }
1908 else
1909 {
1910 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1911 exit(-1);
1912 }
1913
1914 high_output = 0x0;
1915 high_direction = 0x0f;
1916
1917 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1918 {
1919 high_output |= nTRSTnOE;
1920 high_output &= ~nTRST;
1921 }
1922 else
1923 {
1924 high_output &= ~nTRSTnOE;
1925 high_output |= nTRST;
1926 }
1927
1928 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1929 {
1930 high_output &= ~nSRSTnOE;
1931 high_output |= nSRST;
1932 }
1933 else
1934 {
1935 high_output |= nSRSTnOE;
1936 high_output &= ~nSRST;
1937 }
1938
1939 /* initialize high port */
1940 buf[0] = 0x82; /* command "set data bits high byte" */
1941 buf[1] = high_output; /* value */
1942 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1943 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1944
1945 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1946 {
1947 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1948 return ERROR_JTAG_INIT_FAILED;
1949 }
1950
1951 return ERROR_OK;
1952 }
1953
1954 int olimex_jtag_init(void)
1955 {
1956 u8 buf[3];
1957 u32 bytes_written;
1958
1959 low_output = 0x08;
1960 low_direction = 0x1b;
1961
1962 /* initialize low byte for jtag */
1963 buf[0] = 0x80; /* command "set data bits low byte" */
1964 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1965 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1966 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1967
1968 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1969 {
1970 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1971 return ERROR_JTAG_INIT_FAILED;
1972 }
1973
1974 nTRST = 0x01;
1975 nTRSTnOE = 0x4;
1976 nSRST = 0x02;
1977 nSRSTnOE = 0x00; /* no output enable for nSRST */
1978
1979 high_output = 0x0;
1980 high_direction = 0x0f;
1981
1982 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1983 {
1984 high_output |= nTRSTnOE;
1985 high_output &= ~nTRST;
1986 }
1987 else
1988 {
1989 high_output &= ~nTRSTnOE;
1990 high_output |= nTRST;
1991 }
1992
1993 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1994 {
1995 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1996 }
1997 else
1998 {
1999 high_output &= ~nSRST;
2000 }
2001
2002 /* turn red LED on */
2003 high_output |= 0x08;
2004
2005 /* initialize high port */
2006 buf[0] = 0x82; /* command "set data bits high byte" */
2007 buf[1] = high_output; /* value */
2008 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2009 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2010
2011 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2012 {
2013 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2014 return ERROR_JTAG_INIT_FAILED;
2015 }
2016
2017 return ERROR_OK;
2018 }
2019
2020 int flyswatter_init(void)
2021 {
2022 u8 buf[3];
2023 u32 bytes_written;
2024
2025 low_output = 0x18;
2026 low_direction = 0xfb;
2027
2028 /* initialize low byte for jtag */
2029 buf[0] = 0x80; /* command "set data bits low byte" */
2030 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2031 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2032 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2033
2034 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2035 {
2036 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2037 return ERROR_JTAG_INIT_FAILED;
2038 }
2039
2040 nTRST = 0x10;
2041 nTRSTnOE = 0x0; /* not output enable for nTRST */
2042 nSRST = 0x20;
2043 nSRSTnOE = 0x00; /* no output enable for nSRST */
2044
2045 high_output = 0x00;
2046 high_direction = 0x0c;
2047
2048 /* turn red LED1 on, LED2 off */
2049 high_output |= 0x08;
2050
2051 /* initialize high port */
2052 buf[0] = 0x82; /* command "set data bits high byte" */
2053 buf[1] = high_output; /* value */
2054 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2055 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2056
2057 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2058 {
2059 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2060 return ERROR_JTAG_INIT_FAILED;
2061 }
2062
2063 return ERROR_OK;
2064 }
2065
2066 int turtle_init(void)
2067 {
2068 u8 buf[3];
2069 u32 bytes_written;
2070
2071 low_output = 0x08;
2072 low_direction = 0x5b;
2073
2074 /* initialize low byte for jtag */
2075 buf[0] = 0x80; /* command "set data bits low byte" */
2076 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2077 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2078 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2079
2080 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2081 {
2082 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2083 return ERROR_JTAG_INIT_FAILED;
2084 }
2085
2086 nSRST = 0x40;
2087
2088 high_output = 0x00;
2089 high_direction = 0x0C;
2090
2091 /* initialize high port */
2092 buf[0] = 0x82; /* command "set data bits high byte" */
2093 buf[1] = high_output;
2094 buf[2] = high_direction;
2095 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2096
2097 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2098 {
2099 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2100 return ERROR_JTAG_INIT_FAILED;
2101 }
2102
2103 return ERROR_OK;
2104 }
2105
2106 int comstick_init(void)
2107 {
2108 u8 buf[3];
2109 u32 bytes_written;
2110
2111 low_output = 0x08;
2112 low_direction = 0x0b;
2113
2114 /* initialize low byte for jtag */
2115 buf[0] = 0x80; /* command "set data bits low byte" */
2116 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2117 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2118 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2119
2120 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2121 {
2122 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2123 return ERROR_JTAG_INIT_FAILED;
2124 }
2125
2126 nTRST = 0x01;
2127 nTRSTnOE = 0x00; /* no output enable for nTRST */
2128 nSRST = 0x02;
2129 nSRSTnOE = 0x00; /* no output enable for nSRST */
2130
2131 high_output = 0x03;
2132 high_direction = 0x03;
2133
2134 /* initialize high port */
2135 buf[0] = 0x82; /* command "set data bits high byte" */
2136 buf[1] = high_output;
2137 buf[2] = high_direction;
2138 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2139
2140 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2141 {
2142 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2143 return ERROR_JTAG_INIT_FAILED;
2144 }
2145
2146 return ERROR_OK;
2147 }
2148
2149 int stm32stick_init(void)
2150 {
2151 u8 buf[3];
2152 u32 bytes_written;
2153
2154 low_output = 0x88;
2155 low_direction = 0x8b;
2156
2157 /* initialize low byte for jtag */
2158 buf[0] = 0x80; /* command "set data bits low byte" */
2159 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2160 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2161 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2162
2163 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2164 {
2165 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2166 return ERROR_JTAG_INIT_FAILED;
2167 }
2168
2169 nTRST = 0x01;
2170 nTRSTnOE = 0x00; /* no output enable for nTRST */
2171 nSRST = 0x80;
2172 nSRSTnOE = 0x00; /* no output enable for nSRST */
2173
2174 high_output = 0x01;
2175 high_direction = 0x03;
2176
2177 /* initialize high port */
2178 buf[0] = 0x82; /* command "set data bits high byte" */
2179 buf[1] = high_output;
2180 buf[2] = high_direction;
2181 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2182
2183 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2184 {
2185 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2186 return ERROR_JTAG_INIT_FAILED;
2187 }
2188
2189 return ERROR_OK;
2190 }
2191
2192 void olimex_jtag_blink(void)
2193 {
2194 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2195 * ACBUS3 is bit 3 of the GPIOH port
2196 */
2197 if (high_output & 0x08)
2198 {
2199 /* set port pin high */
2200 high_output &= 0x07;
2201 }
2202 else
2203 {
2204 /* set port pin low */
2205 high_output |= 0x08;
2206 }
2207
2208 BUFFER_ADD = 0x82;
2209 BUFFER_ADD = high_output;
2210 BUFFER_ADD = high_direction;
2211 }
2212
2213 void turtle_jtag_blink(void)
2214 {
2215 /*
2216 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2217 */
2218 if (high_output & 0x08)
2219 {
2220 high_output = 0x04;
2221 }
2222 else
2223 {
2224 high_output = 0x08;
2225 }
2226
2227 BUFFER_ADD = 0x82;
2228 BUFFER_ADD = high_output;
2229 BUFFER_ADD = high_direction;
2230 }
2231
2232 int ft2232_quit(void)
2233 {
2234 #if BUILD_FT2232_FTD2XX == 1
2235 FT_STATUS status;
2236
2237 status = FT_Close(ftdih);
2238 #elif BUILD_FT2232_LIBFTDI == 1
2239 ftdi_disable_bitbang(&ftdic);
2240
2241 ftdi_usb_close(&ftdic);
2242
2243 ftdi_deinit(&ftdic);
2244 #endif
2245
2246 free(ft2232_buffer);
2247 ft2232_buffer = NULL;
2248
2249 return ERROR_OK;
2250 }
2251
2252 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2253 {
2254 if (argc == 1)
2255 {
2256 ft2232_device_desc = strdup(args[0]);
2257 }
2258 else
2259 {
2260 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2261 }
2262
2263 return ERROR_OK;
2264 }
2265
2266 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2267 {
2268 if (argc == 1)
2269 {
2270 ft2232_serial = strdup(args[0]);
2271 }
2272 else
2273 {
2274 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2275 }
2276
2277 return ERROR_OK;
2278 }
2279
2280 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2281 {
2282 if (argc == 0)
2283 return ERROR_OK;
2284
2285 ft2232_layout = malloc(strlen(args[0]) + 1);
2286 strcpy(ft2232_layout, args[0]);
2287
2288 return ERROR_OK;
2289 }
2290
2291 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2292 {
2293 int i;
2294
2295 if (argc > MAX_USB_IDS*2) {
2296 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2297 "(maximum is %d pairs)", MAX_USB_IDS);
2298 argc = MAX_USB_IDS*2;
2299 }
2300 if (argc < 2 || (argc & 1))
2301 {
2302 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2303 if (argc < 2)
2304 return ERROR_OK;
2305 }
2306
2307 for (i = 0; i+1 < argc; i += 2) {
2308 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2309 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2310 }
2311 /*
2312 * Explicitly terminate, in case there are multiples instances of
2313 * ft2232_vid_pid.
2314 */
2315 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2316
2317 return ERROR_OK;
2318 }
2319
2320 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2321 {
2322 if (argc == 1)
2323 {
2324 ft2232_latency = atoi(args[0]);
2325 }
2326 else
2327 {
2328 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2329 }
2330
2331 return ERROR_OK;
2332 }
2333
2334
2335 static int ft2232_stableclocks(int num_cycles, jtag_command_t *cmd)
2336 {
2337 int retval = 0;
2338
2339 while (num_cycles > 0)
2340 {
2341 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2342 * at most 7 bits per invocation. Here we invoke it potentially
2343 * several times.
2344 */
2345 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2346
2347 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2348 {
2349 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2350 retval = ERROR_JTAG_QUEUE_FAILED;
2351
2352 first_unsent = cmd;
2353 }
2354
2355 /* command "Clock Data to TMS/CS Pin (no Read)" */
2356 BUFFER_ADD = 0x4b;
2357
2358 /* scan 7 bit */
2359 BUFFER_ADD = bitcount_per_command - 1;
2360
2361 /* TMS data bits are all zeros to stay in the current stable state */
2362 BUFFER_ADD = 0x0;
2363
2364 require_send = 1;
2365
2366 num_cycles -= bitcount_per_command;
2367 }
2368
2369 return retval;
2370 }
2371

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)