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

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)