xvsf player fixes by Dick Hollenbeck <dick@softplc.com>
[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 /* this is only allowed while in a stable state. A check for a stable
1419 * state was done in jtag_add_clocks()
1420 */
1421 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1422 retval = ERROR_JTAG_QUEUE_FAILED;
1423 #ifdef _DEBUG_JTAG_IO_
1424 LOG_DEBUG("clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, jtag_state_name(cur_state));
1425 #endif
1426 break;
1427
1428 default:
1429 LOG_ERROR("BUG: unknown JTAG command type encountered");
1430 exit(-1);
1431 }
1432 cmd = cmd->next;
1433 }
1434
1435 if (require_send > 0)
1436 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1437 retval = ERROR_JTAG_QUEUE_FAILED;
1438
1439 return retval;
1440 }
1441
1442 #if BUILD_FT2232_FTD2XX == 1
1443 static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
1444 {
1445 FT_STATUS status;
1446 DWORD openex_flags = 0;
1447 char *openex_string = NULL;
1448 u8 latency_timer;
1449
1450 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",ft2232_layout, vid, pid);
1451
1452 #if IS_WIN32 == 0
1453 /* Add non-standard Vid/Pid to the linux driver */
1454 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1455 {
1456 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1457 }
1458 #endif
1459
1460 if (ft2232_device_desc && ft2232_serial)
1461 {
1462 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1463 ft2232_device_desc = NULL;
1464 }
1465
1466 if (ft2232_device_desc)
1467 {
1468 openex_string = ft2232_device_desc;
1469 openex_flags = FT_OPEN_BY_DESCRIPTION;
1470 }
1471 else if (ft2232_serial)
1472 {
1473 openex_string = ft2232_serial;
1474 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1475 }
1476 else
1477 {
1478 LOG_ERROR("neither device description nor serial number specified");
1479 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1480
1481 return ERROR_JTAG_INIT_FAILED;
1482 }
1483
1484 if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
1485 {
1486 DWORD num_devices;
1487
1488 if (more) {
1489 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1490 *try_more = 1;
1491 return ERROR_JTAG_INIT_FAILED;
1492 }
1493 LOG_ERROR("unable to open ftdi device: %lu", status);
1494 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1495 if (status == FT_OK)
1496 {
1497 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
1498 int i;
1499
1500 for (i = 0; i < num_devices; i++)
1501 desc_array[i] = malloc(64);
1502 desc_array[num_devices] = NULL;
1503
1504 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1505
1506 if (status == FT_OK)
1507 {
1508 LOG_ERROR("ListDevices: %lu\n", num_devices);
1509 for (i = 0; i < num_devices; i++)
1510 LOG_ERROR("%i: \"%s\"", i, desc_array[i]);
1511 }
1512
1513 for (i = 0; i < num_devices; i++)
1514 free(desc_array[i]);
1515 free(desc_array);
1516 }
1517 else
1518 {
1519 LOG_ERROR("ListDevices: NONE\n");
1520 }
1521 return ERROR_JTAG_INIT_FAILED;
1522 }
1523
1524 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1525 {
1526 LOG_ERROR("unable to set latency timer: %lu", status);
1527 return ERROR_JTAG_INIT_FAILED;
1528 }
1529
1530 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1531 {
1532 LOG_ERROR("unable to get latency timer: %lu", status);
1533 return ERROR_JTAG_INIT_FAILED;
1534 }
1535 else
1536 {
1537 LOG_DEBUG("current latency timer: %i", latency_timer);
1538 }
1539
1540 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1541 {
1542 LOG_ERROR("unable to set timeouts: %lu", status);
1543 return ERROR_JTAG_INIT_FAILED;
1544 }
1545
1546 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1547 {
1548 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1549 return ERROR_JTAG_INIT_FAILED;
1550 }
1551
1552 return ERROR_OK;
1553 }
1554
1555 static int ft2232_purge_ftd2xx(void)
1556 {
1557 FT_STATUS status;
1558
1559 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1560 {
1561 LOG_ERROR("error purging ftd2xx device: %lu", status);
1562 return ERROR_JTAG_INIT_FAILED;
1563 }
1564
1565 return ERROR_OK;
1566 }
1567 #endif /* BUILD_FT2232_FTD2XX == 1 */
1568
1569 #if BUILD_FT2232_LIBFTDI == 1
1570 static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
1571 {
1572 u8 latency_timer;
1573
1574 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1575 ft2232_layout, vid, pid);
1576
1577 if (ftdi_init(&ftdic) < 0)
1578 return ERROR_JTAG_INIT_FAILED;
1579
1580 /* context, vendor id, product id */
1581 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1582 ft2232_serial) < 0) {
1583 if (more)
1584 LOG_WARNING("unable to open ftdi device (trying more): %s",
1585 ftdic.error_str);
1586 else
1587 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
1588 *try_more = 1;
1589 return ERROR_JTAG_INIT_FAILED;
1590 }
1591
1592 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1593 {
1594 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1595 return ERROR_JTAG_INIT_FAILED;
1596 }
1597
1598 if (ftdi_usb_reset(&ftdic) < 0)
1599 {
1600 LOG_ERROR("unable to reset ftdi device");
1601 return ERROR_JTAG_INIT_FAILED;
1602 }
1603
1604 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
1605 {
1606 LOG_ERROR("unable to set latency timer");
1607 return ERROR_JTAG_INIT_FAILED;
1608 }
1609
1610 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
1611 {
1612 LOG_ERROR("unable to get latency timer");
1613 return ERROR_JTAG_INIT_FAILED;
1614 }
1615 else
1616 {
1617 LOG_DEBUG("current latency timer: %i", latency_timer);
1618 }
1619
1620 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
1621
1622 return ERROR_OK;
1623 }
1624
1625 static int ft2232_purge_libftdi(void)
1626 {
1627 if (ftdi_usb_purge_buffers(&ftdic) < 0)
1628 {
1629 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
1630 return ERROR_JTAG_INIT_FAILED;
1631 }
1632
1633 return ERROR_OK;
1634 }
1635 #endif /* BUILD_FT2232_LIBFTDI == 1 */
1636
1637 int ft2232_init(void)
1638 {
1639 u8 buf[1];
1640 int retval;
1641 u32 bytes_written;
1642 ft2232_layout_t *cur_layout = ft2232_layouts;
1643 int i;
1644
1645 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
1646 {
1647 ft2232_layout = "usbjtag";
1648 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
1649 }
1650
1651 while (cur_layout->name)
1652 {
1653 if (strcmp(cur_layout->name, ft2232_layout) == 0)
1654 {
1655 layout = cur_layout;
1656 break;
1657 }
1658 cur_layout++;
1659 }
1660
1661 if (!layout)
1662 {
1663 LOG_ERROR("No matching layout found for %s", ft2232_layout);
1664 return ERROR_JTAG_INIT_FAILED;
1665 }
1666
1667 for (i = 0; 1; i++) {
1668 /*
1669 * "more indicates that there are more IDs to try, so we should
1670 * not print an error for an ID mismatch (but for anything
1671 * else, we should).
1672 *
1673 * try_more indicates that the error code returned indicates an
1674 * ID mismatch (and nothing else) and that we should proceeed
1675 * with the next ID pair.
1676 */
1677 int more = ft2232_vid[i+1] || ft2232_pid[i+1];
1678 int try_more = 0;
1679
1680 #if BUILD_FT2232_FTD2XX == 1
1681 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
1682 more, &try_more);
1683 #elif BUILD_FT2232_LIBFTDI == 1
1684 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
1685 more, &try_more);
1686 #endif
1687 if (retval >= 0)
1688 break;
1689 if (!more || !try_more)
1690 return retval;
1691 }
1692
1693 ft2232_buffer_size = 0;
1694 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
1695
1696 if (layout->init() != ERROR_OK)
1697 return ERROR_JTAG_INIT_FAILED;
1698
1699 ft2232_speed(jtag_speed);
1700
1701 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1702 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
1703 {
1704 LOG_ERROR("couldn't write to FT2232 to disable loopback");
1705 return ERROR_JTAG_INIT_FAILED;
1706 }
1707
1708 #if BUILD_FT2232_FTD2XX == 1
1709 return ft2232_purge_ftd2xx();
1710 #elif BUILD_FT2232_LIBFTDI == 1
1711 return ft2232_purge_libftdi();
1712 #endif
1713
1714 return ERROR_OK;
1715 }
1716
1717 int usbjtag_init(void)
1718 {
1719 u8 buf[3];
1720 u32 bytes_written;
1721
1722 low_output = 0x08;
1723 low_direction = 0x0b;
1724
1725 if (strcmp(ft2232_layout, "usbjtag") == 0)
1726 {
1727 nTRST = 0x10;
1728 nTRSTnOE = 0x10;
1729 nSRST = 0x40;
1730 nSRSTnOE = 0x40;
1731 }
1732 else if (strcmp(ft2232_layout, "signalyzer") == 0)
1733 {
1734 nTRST = 0x10;
1735 nTRSTnOE = 0x10;
1736 nSRST = 0x20;
1737 nSRSTnOE = 0x20;
1738 }
1739 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
1740 {
1741 nTRST = 0x0;
1742 nTRSTnOE = 0x00;
1743 nSRST = 0x20;
1744 nSRSTnOE = 0x20;
1745 low_output = 0x88;
1746 low_direction = 0x8b;
1747 }
1748 else
1749 {
1750 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
1751 return ERROR_JTAG_INIT_FAILED;
1752 }
1753
1754 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1755 {
1756 low_direction &= ~nTRSTnOE; /* nTRST input */
1757 low_output &= ~nTRST; /* nTRST = 0 */
1758 }
1759 else
1760 {
1761 low_direction |= nTRSTnOE; /* nTRST output */
1762 low_output |= nTRST; /* nTRST = 1 */
1763 }
1764
1765 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1766 {
1767 low_direction |= nSRSTnOE; /* nSRST output */
1768 low_output |= nSRST; /* nSRST = 1 */
1769 }
1770 else
1771 {
1772 low_direction &= ~nSRSTnOE; /* nSRST input */
1773 low_output &= ~nSRST; /* nSRST = 0 */
1774 }
1775
1776 /* initialize low byte for jtag */
1777 buf[0] = 0x80; /* command "set data bits low byte" */
1778 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1779 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1780 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1781
1782 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1783 {
1784 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1785 return ERROR_JTAG_INIT_FAILED;
1786 }
1787
1788 return ERROR_OK;
1789 }
1790
1791 int axm0432_jtag_init(void)
1792 {
1793 u8 buf[3];
1794 u32 bytes_written;
1795
1796 low_output = 0x08;
1797 low_direction = 0x2b;
1798
1799 /* initialize low byte for jtag */
1800 buf[0] = 0x80; /* command "set data bits low byte" */
1801 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1802 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1803 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1804
1805 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1806 {
1807 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1808 return ERROR_JTAG_INIT_FAILED;
1809 }
1810
1811 if (strcmp(layout->name, "axm0432_jtag") == 0)
1812 {
1813 nTRST = 0x08;
1814 nTRSTnOE = 0x0; /* No output enable for TRST*/
1815 nSRST = 0x04;
1816 nSRSTnOE = 0x0; /* No output enable for SRST*/
1817 }
1818 else
1819 {
1820 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
1821 exit(-1);
1822 }
1823
1824 high_output = 0x0;
1825 high_direction = 0x0c;
1826
1827 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1828 {
1829 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
1830 }
1831 else
1832 {
1833 high_output |= nTRST;
1834 }
1835
1836 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1837 {
1838 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
1839 }
1840 else
1841 {
1842 high_output |= nSRST;
1843 }
1844
1845 /* initialize high port */
1846 buf[0] = 0x82; /* command "set data bits high byte" */
1847 buf[1] = high_output; /* value */
1848 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1849 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1850
1851 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1852 {
1853 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
1854 return ERROR_JTAG_INIT_FAILED;
1855 }
1856
1857 return ERROR_OK;
1858 }
1859
1860 int jtagkey_init(void)
1861 {
1862 u8 buf[3];
1863 u32 bytes_written;
1864
1865 low_output = 0x08;
1866 low_direction = 0x1b;
1867
1868 /* initialize low byte for jtag */
1869 buf[0] = 0x80; /* command "set data bits low byte" */
1870 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1871 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1872 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1873
1874 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1875 {
1876 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1877 return ERROR_JTAG_INIT_FAILED;
1878 }
1879
1880 if (strcmp(layout->name, "jtagkey") == 0)
1881 {
1882 nTRST = 0x01;
1883 nTRSTnOE = 0x4;
1884 nSRST = 0x02;
1885 nSRSTnOE = 0x08;
1886 }
1887 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
1888 (strcmp(layout->name, "oocdlink") == 0))
1889 {
1890 nTRST = 0x02;
1891 nTRSTnOE = 0x1;
1892 nSRST = 0x08;
1893 nSRSTnOE = 0x04;
1894 }
1895 else
1896 {
1897 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
1898 exit(-1);
1899 }
1900
1901 high_output = 0x0;
1902 high_direction = 0x0f;
1903
1904 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1905 {
1906 high_output |= nTRSTnOE;
1907 high_output &= ~nTRST;
1908 }
1909 else
1910 {
1911 high_output &= ~nTRSTnOE;
1912 high_output |= nTRST;
1913 }
1914
1915 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1916 {
1917 high_output &= ~nSRSTnOE;
1918 high_output |= nSRST;
1919 }
1920 else
1921 {
1922 high_output |= nSRSTnOE;
1923 high_output &= ~nSRST;
1924 }
1925
1926 /* initialize high port */
1927 buf[0] = 0x82; /* command "set data bits high byte" */
1928 buf[1] = high_output; /* value */
1929 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1930 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1931
1932 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1933 {
1934 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1935 return ERROR_JTAG_INIT_FAILED;
1936 }
1937
1938 return ERROR_OK;
1939 }
1940
1941 int olimex_jtag_init(void)
1942 {
1943 u8 buf[3];
1944 u32 bytes_written;
1945
1946 low_output = 0x08;
1947 low_direction = 0x1b;
1948
1949 /* initialize low byte for jtag */
1950 buf[0] = 0x80; /* command "set data bits low byte" */
1951 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1952 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1953 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1954
1955 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1956 {
1957 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1958 return ERROR_JTAG_INIT_FAILED;
1959 }
1960
1961 nTRST = 0x01;
1962 nTRSTnOE = 0x4;
1963 nSRST = 0x02;
1964 nSRSTnOE = 0x00; /* no output enable for nSRST */
1965
1966 high_output = 0x0;
1967 high_direction = 0x0f;
1968
1969 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1970 {
1971 high_output |= nTRSTnOE;
1972 high_output &= ~nTRST;
1973 }
1974 else
1975 {
1976 high_output &= ~nTRSTnOE;
1977 high_output |= nTRST;
1978 }
1979
1980 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1981 {
1982 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1983 }
1984 else
1985 {
1986 high_output &= ~nSRST;
1987 }
1988
1989 /* turn red LED on */
1990 high_output |= 0x08;
1991
1992 /* initialize high port */
1993 buf[0] = 0x82; /* command "set data bits high byte" */
1994 buf[1] = high_output; /* value */
1995 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1996 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1997
1998 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
1999 {
2000 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2001 return ERROR_JTAG_INIT_FAILED;
2002 }
2003
2004 return ERROR_OK;
2005 }
2006
2007 int flyswatter_init(void)
2008 {
2009 u8 buf[3];
2010 u32 bytes_written;
2011
2012 low_output = 0x18;
2013 low_direction = 0xfb;
2014
2015 /* initialize low byte for jtag */
2016 buf[0] = 0x80; /* command "set data bits low byte" */
2017 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2018 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
2019 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2020
2021 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2022 {
2023 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2024 return ERROR_JTAG_INIT_FAILED;
2025 }
2026
2027 nTRST = 0x10;
2028 nTRSTnOE = 0x0; /* not output enable for nTRST */
2029 nSRST = 0x20;
2030 nSRSTnOE = 0x00; /* no output enable for nSRST */
2031
2032 high_output = 0x00;
2033 high_direction = 0x0c;
2034
2035 /* turn red LED1 on, LED2 off */
2036 high_output |= 0x08;
2037
2038 /* initialize high port */
2039 buf[0] = 0x82; /* command "set data bits high byte" */
2040 buf[1] = high_output; /* value */
2041 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2042 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2043
2044 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2045 {
2046 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2047 return ERROR_JTAG_INIT_FAILED;
2048 }
2049
2050 return ERROR_OK;
2051 }
2052
2053 int turtle_init(void)
2054 {
2055 u8 buf[3];
2056 u32 bytes_written;
2057
2058 low_output = 0x08;
2059 low_direction = 0x5b;
2060
2061 /* initialize low byte for jtag */
2062 buf[0] = 0x80; /* command "set data bits low byte" */
2063 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2064 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2065 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2066
2067 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2068 {
2069 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2070 return ERROR_JTAG_INIT_FAILED;
2071 }
2072
2073 nSRST = 0x40;
2074
2075 high_output = 0x00;
2076 high_direction = 0x0C;
2077
2078 /* initialize high port */
2079 buf[0] = 0x82; /* command "set data bits high byte" */
2080 buf[1] = high_output;
2081 buf[2] = high_direction;
2082 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2083
2084 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2085 {
2086 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2087 return ERROR_JTAG_INIT_FAILED;
2088 }
2089
2090 return ERROR_OK;
2091 }
2092
2093 int comstick_init(void)
2094 {
2095 u8 buf[3];
2096 u32 bytes_written;
2097
2098 low_output = 0x08;
2099 low_direction = 0x0b;
2100
2101 /* initialize low byte for jtag */
2102 buf[0] = 0x80; /* command "set data bits low byte" */
2103 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2104 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2105 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2106
2107 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2108 {
2109 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2110 return ERROR_JTAG_INIT_FAILED;
2111 }
2112
2113 nTRST = 0x01;
2114 nTRSTnOE = 0x00; /* no output enable for nTRST */
2115 nSRST = 0x02;
2116 nSRSTnOE = 0x00; /* no output enable for nSRST */
2117
2118 high_output = 0x03;
2119 high_direction = 0x03;
2120
2121 /* initialize high port */
2122 buf[0] = 0x82; /* command "set data bits high byte" */
2123 buf[1] = high_output;
2124 buf[2] = high_direction;
2125 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2126
2127 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2128 {
2129 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2130 return ERROR_JTAG_INIT_FAILED;
2131 }
2132
2133 return ERROR_OK;
2134 }
2135
2136 int stm32stick_init(void)
2137 {
2138 u8 buf[3];
2139 u32 bytes_written;
2140
2141 low_output = 0x88;
2142 low_direction = 0x8b;
2143
2144 /* initialize low byte for jtag */
2145 buf[0] = 0x80; /* command "set data bits low byte" */
2146 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
2147 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
2148 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2149
2150 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2151 {
2152 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2153 return ERROR_JTAG_INIT_FAILED;
2154 }
2155
2156 nTRST = 0x01;
2157 nTRSTnOE = 0x00; /* no output enable for nTRST */
2158 nSRST = 0x80;
2159 nSRSTnOE = 0x00; /* no output enable for nSRST */
2160
2161 high_output = 0x01;
2162 high_direction = 0x03;
2163
2164 /* initialize high port */
2165 buf[0] = 0x82; /* command "set data bits high byte" */
2166 buf[1] = high_output;
2167 buf[2] = high_direction;
2168 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2169
2170 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2171 {
2172 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2173 return ERROR_JTAG_INIT_FAILED;
2174 }
2175
2176 return ERROR_OK;
2177 }
2178
2179 void olimex_jtag_blink(void)
2180 {
2181 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2182 * ACBUS3 is bit 3 of the GPIOH port
2183 */
2184 if (high_output & 0x08)
2185 {
2186 /* set port pin high */
2187 high_output &= 0x07;
2188 }
2189 else
2190 {
2191 /* set port pin low */
2192 high_output |= 0x08;
2193 }
2194
2195 BUFFER_ADD = 0x82;
2196 BUFFER_ADD = high_output;
2197 BUFFER_ADD = high_direction;
2198 }
2199
2200 void turtle_jtag_blink(void)
2201 {
2202 /*
2203 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2204 */
2205 if (high_output & 0x08)
2206 {
2207 high_output = 0x04;
2208 }
2209 else
2210 {
2211 high_output = 0x08;
2212 }
2213
2214 BUFFER_ADD = 0x82;
2215 BUFFER_ADD = high_output;
2216 BUFFER_ADD = high_direction;
2217 }
2218
2219 int ft2232_quit(void)
2220 {
2221 #if BUILD_FT2232_FTD2XX == 1
2222 FT_STATUS status;
2223
2224 status = FT_Close(ftdih);
2225 #elif BUILD_FT2232_LIBFTDI == 1
2226 ftdi_disable_bitbang(&ftdic);
2227
2228 ftdi_usb_close(&ftdic);
2229
2230 ftdi_deinit(&ftdic);
2231 #endif
2232
2233 free(ft2232_buffer);
2234 ft2232_buffer = NULL;
2235
2236 return ERROR_OK;
2237 }
2238
2239 int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2240 {
2241 if (argc == 1)
2242 {
2243 ft2232_device_desc = strdup(args[0]);
2244 }
2245 else
2246 {
2247 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2248 }
2249
2250 return ERROR_OK;
2251 }
2252
2253 int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2254 {
2255 if (argc == 1)
2256 {
2257 ft2232_serial = strdup(args[0]);
2258 }
2259 else
2260 {
2261 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2262 }
2263
2264 return ERROR_OK;
2265 }
2266
2267 int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2268 {
2269 if (argc == 0)
2270 return ERROR_OK;
2271
2272 ft2232_layout = malloc(strlen(args[0]) + 1);
2273 strcpy(ft2232_layout, args[0]);
2274
2275 return ERROR_OK;
2276 }
2277
2278 int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2279 {
2280 int i;
2281
2282 if (argc > MAX_USB_IDS*2) {
2283 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2284 "(maximum is %d pairs)", MAX_USB_IDS);
2285 argc = MAX_USB_IDS*2;
2286 }
2287 if (argc < 2 || (argc & 1))
2288 {
2289 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2290 if (argc < 2)
2291 return ERROR_OK;
2292 }
2293
2294 for (i = 0; i+1 < argc; i += 2) {
2295 ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
2296 ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
2297 }
2298 /*
2299 * Explicitly terminate, in case there are multiples instances of
2300 * ft2232_vid_pid.
2301 */
2302 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2303
2304 return ERROR_OK;
2305 }
2306
2307 int ft2232_handle_latency_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2308 {
2309 if (argc == 1)
2310 {
2311 ft2232_latency = atoi(args[0]);
2312 }
2313 else
2314 {
2315 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2316 }
2317
2318 return ERROR_OK;
2319 }
2320
2321
2322 static int ft2232_stableclocks(int num_cycles, jtag_command_t *cmd)
2323 {
2324 int retval = 0;
2325
2326 /* 7 bits of either ones or zeros. */
2327 u8 tms = (cur_state == TAP_RESET ? 0x7F : 0x00);
2328
2329 while (num_cycles > 0)
2330 {
2331 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2332 * at most 7 bits per invocation. Here we invoke it potentially
2333 * several times.
2334 * see: http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
2335 */
2336 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2337
2338 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2339 {
2340 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2341 retval = ERROR_JTAG_QUEUE_FAILED;
2342
2343 first_unsent = cmd;
2344 }
2345
2346 /* command "Clock Data to TMS/CS Pin (no Read)" */
2347 BUFFER_ADD = 0x4b;
2348
2349 /* scan 7 bit */
2350 BUFFER_ADD = bitcount_per_command - 1;
2351
2352 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2353 BUFFER_ADD = tms;
2354
2355 require_send = 1;
2356
2357 num_cycles -= bitcount_per_command;
2358 }
2359
2360 return retval;
2361 }
2362

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)