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

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)