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

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)