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

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)