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

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)