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

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)