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

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)