- Added support for native MinGW builds (thanks to Spencer Oliver and Michael Fischer...
[openocd.git] / src / jtag / ftd2xx.c
1 /***************************************************************************
2 * Copyright (C) 2004 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #if IS_CYGWIN == 1
25 #include "windows.h"
26 #undef ERROR
27 #endif
28
29 #include "replacements.h"
30
31 /* project specific includes */
32 #include "log.h"
33 #include "types.h"
34 #include "jtag.h"
35 #include "configuration.h"
36 #include "time_support.h"
37
38 /* system includes */
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <ftd2xx.h>
43
44 #include <sys/time.h>
45 #include <time.h>
46
47 /* enable this to debug io latency
48 */
49 #if 0
50 #define _DEBUG_USB_IO_
51 #endif
52
53 /* enable this to debug communication
54 */
55 #if 0
56 #define _DEBUG_USB_COMMS_
57 #endif
58
59 /* enable this to work around ftd2xx deadlock
60 */
61 #if 0
62 #define _FTD2XX_QUEUE_DELAY_
63 #endif
64
65 int ftd2xx_execute_queue(void);
66
67 int ftd2xx_speed(int speed);
68 int ftd2xx_register_commands(struct command_context_s *cmd_ctx);
69 int ftd2xx_init(void);
70 int ftd2xx_quit(void);
71
72 int ftd2xx_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ftd2xx_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ftd2xx_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75
76 char *ftd2xx_device_desc = NULL;
77 char *ftd2xx_layout = NULL;
78 u16 ftd2xx_vid = 0x0403;
79 u16 ftd2xx_pid = 0x6010;
80
81 typedef struct ftd2xx_layout_s
82 {
83 char* name;
84 int(*init)(void);
85 void(*reset)(int trst, int srst);
86 } ftd2xx_layout_t;
87
88 int usbjtag_init(void);
89 int jtagkey_init(void);
90 void usbjtag_reset(int trst, int srst);
91 void jtagkey_reset(int trst, int srst);
92
93 ftd2xx_layout_t ftd2xx_layouts[] =
94 {
95 {"usbjtag", usbjtag_init, usbjtag_reset},
96 {"jtagkey", jtagkey_init, jtagkey_reset},
97 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset},
98 {NULL, NULL, NULL},
99 };
100
101 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
102
103 static ftd2xx_layout_t *layout;
104 static u8 low_output = 0x0;
105 static u8 low_direction = 0x0;
106 static u8 high_output = 0x0;
107 static u8 high_direction = 0x0;
108 static FT_HANDLE ftdih = NULL;
109
110 static u8 *ftd2xx_buffer = NULL;
111 static int ftd2xx_buffer_size = 0;
112 static int ftd2xx_read_pointer = 0;
113 static int ftd2xx_expect_read = 0;
114 #define FTD2XX_BUFFER_SIZE 131072
115 #define BUFFER_ADD ftd2xx_buffer[ftd2xx_buffer_size++]
116 #define BUFFER_READ ftd2xx_buffer[ftd2xx_read_pointer++]
117
118 jtag_interface_t ftd2xx_interface =
119 {
120
121 .name = "ftd2xx",
122
123 .execute_queue = ftd2xx_execute_queue,
124
125 .support_statemove = 1,
126
127 .speed = ftd2xx_speed,
128 .register_commands = ftd2xx_register_commands,
129 .init = ftd2xx_init,
130 .quit = ftd2xx_quit,
131 };
132
133 int ftd2xx_speed(int speed)
134 {
135 u8 buf[3];
136 FT_STATUS status;
137 DWORD bytes_written;
138
139 buf[0] = 0x86; /* command "set divisor" */
140 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
141 buf[2] = (speed >> 8) & 0xff; /* valueH */
142
143 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
144 if (((status = FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
145 {
146 ERROR("couldn't write to ftdi device: %i", status);
147 return status;
148 }
149
150 return ERROR_OK;
151 }
152
153 int ftd2xx_register_commands(struct command_context_s *cmd_ctx)
154 {
155 register_command(cmd_ctx, NULL, "ftd2xx_device_desc", ftd2xx_handle_device_desc_command,
156 COMMAND_CONFIG, NULL);
157 register_command(cmd_ctx, NULL, "ftd2xx_layout", ftd2xx_handle_layout_command,
158 COMMAND_CONFIG, NULL);
159 register_command(cmd_ctx, NULL, "ftd2xx_vid_pid", ftd2xx_handle_vid_pid_command,
160 COMMAND_CONFIG, NULL);
161 return ERROR_OK;
162 }
163
164 void ftd2xx_end_state(state)
165 {
166 if (tap_move_map[state] != -1)
167 end_state = state;
168 else
169 {
170 ERROR("BUG: %i is not a valid end state", state);
171 exit(-1);
172 }
173 }
174
175 void ftd2xx_read_scan(enum scan_type type, u8* buffer, int scan_size)
176 {
177 int num_bytes = ((scan_size + 7) / 8);
178 int bits_left = scan_size;
179 int cur_byte = 0;
180
181 while(num_bytes-- > 1)
182 {
183 buffer[cur_byte] = BUFFER_READ;
184 cur_byte++;
185 bits_left -= 8;
186 }
187
188 buffer[cur_byte] = 0x0;
189
190 if (bits_left > 1)
191 {
192 buffer[cur_byte] = BUFFER_READ >> 1;
193 }
194
195 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
196
197 }
198
199 void ftd2xx_debug_dump_buffer(void)
200 {
201 int i;
202 char line[256];
203 char *line_p = line;
204
205 for (i = 0; i < ftd2xx_buffer_size; i++)
206 {
207 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ftd2xx_buffer[i]);
208 if (i % 16 == 15)
209 {
210 DEBUG("%s", line);
211 line_p = line;
212 }
213 }
214
215 if (line_p != line)
216 DEBUG("%s", line);
217 }
218
219 int ftd2xx_send_and_recv(jtag_command_t *first, jtag_command_t *last)
220 {
221 jtag_command_t *cmd;
222 u8 *buffer;
223 int scan_size;
224 enum scan_type type;
225 FT_STATUS status;
226 DWORD bytes_written;
227 DWORD bytes_read;
228
229 #ifdef _DEBUG_USB_IO_
230 struct timeval start, inter, inter2, end;
231 struct timeval d_inter, d_inter2, d_end;
232 #endif
233
234 #ifdef _DEBUG_USB_COMMS_
235 DEBUG("write buffer (size %i):", ftd2xx_buffer_size);
236 ftd2xx_debug_dump_buffer();
237 #endif
238
239 #ifdef _DEBUG_USB_IO_
240 gettimeofday(&start, NULL);
241 #endif
242
243 if ((status = FT_Write(ftdih, ftd2xx_buffer, ftd2xx_buffer_size, &bytes_written)) != FT_OK)
244 {
245 ERROR("couldn't write to ftdi device: %i", status);
246 exit(-1);
247 }
248
249 #ifdef _DEBUG_USB_IO_
250 gettimeofday(&inter, NULL);
251 #endif
252
253 if (ftd2xx_expect_read)
254 {
255 int timeout = 100;
256 ftd2xx_buffer_size = 0;
257
258 #ifdef _FTD2XX_QUEUE_DELAY_
259 DWORD inrxqueue = 0;
260 while (inrxqueue < ftd2xx_expect_read)
261 {
262 FT_GetQueueStatus(ftdih, &inrxqueue);
263 if (inrxqueue >= ftd2xx_expect_read)
264 break;
265 usleep(1000);
266 };
267 #endif
268
269 #ifdef _DEBUG_USB_IO_
270 gettimeofday(&inter2, NULL);
271 #endif
272
273 if ((status = FT_Read(ftdih, ftd2xx_buffer, ftd2xx_expect_read, &bytes_read)) != FT_OK)
274 {
275 ERROR("couldn't read from ftdi device: %i", status);
276 exit(-1);
277 }
278
279 #ifdef _DEBUG_USB_IO_
280 gettimeofday(&end, NULL);
281
282 timeval_subtract(&d_inter, &inter, &start);
283 timeval_subtract(&d_inter2, &inter2, &start);
284 timeval_subtract(&d_end, &end, &start);
285
286 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);
287 #endif
288
289
290 ftd2xx_buffer_size = bytes_read;
291
292 if (ftd2xx_expect_read != ftd2xx_buffer_size)
293 {
294 ERROR("ftd2xx_expect_read (%i) != ftd2xx_buffer_size (%i) (%i retries)", ftd2xx_expect_read, ftd2xx_buffer_size, 100 - timeout);
295 ftd2xx_debug_dump_buffer();
296
297 exit(-1);
298 }
299
300 #ifdef _DEBUG_USB_COMMS_
301 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ftd2xx_buffer_size);
302 ftd2xx_debug_dump_buffer();
303 #endif
304 }
305
306 ftd2xx_expect_read = 0;
307 ftd2xx_read_pointer = 0;
308
309 cmd = first;
310 while (cmd != last)
311 {
312 switch (cmd->type)
313 {
314 case JTAG_SCAN:
315 type = jtag_scan_type(cmd->cmd.scan);
316 if (type != SCAN_OUT)
317 {
318 scan_size = jtag_scan_size(cmd->cmd.scan);
319 buffer = calloc(CEIL(scan_size, 8), 1);
320 ftd2xx_read_scan(type, buffer, scan_size);
321 jtag_read_buffer(buffer, cmd->cmd.scan);
322 free(buffer);
323 }
324 break;
325 default:
326 break;
327 }
328 cmd = cmd->next;
329 }
330
331 ftd2xx_buffer_size = 0;
332
333 return ERROR_OK;
334 }
335
336 void ftd2xx_add_pathmove(pathmove_command_t *cmd)
337 {
338 int num_states = cmd->num_states;
339 u8 tms_byte;
340 int state_count;
341
342 state_count = 0;
343 while (num_states)
344 {
345 tms_byte = 0x0;
346 int bit_count = 0;
347
348 /* command "Clock Data to TMS/CS Pin (no Read)" */
349 BUFFER_ADD = 0x4b;
350 /* number of states remaining */
351 BUFFER_ADD = (num_states % 7) - 1;
352
353 while (num_states % 7)
354 {
355 if (tap_transitions[cur_state].low == cmd->path[state_count])
356 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
357 else if (tap_transitions[cur_state].high == cmd->path[state_count])
358 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
359 else
360 {
361 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
362 exit(-1);
363 }
364
365 cur_state = cmd->path[state_count];
366 state_count++;
367 num_states--;
368 }
369
370 BUFFER_ADD = tms_byte;
371 }
372
373 end_state = cur_state;
374 }
375
376 void ftd2xx_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
377 {
378 int num_bytes = (scan_size + 7) / 8;
379 int bits_left = scan_size;
380 int cur_byte = 0;
381 int last_bit;
382
383 if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
384 {
385 /* command "Clock Data to TMS/CS Pin (no Read)" */
386 BUFFER_ADD = 0x4b;
387 /* scan 7 bit */
388 BUFFER_ADD = 0x6;
389 /* TMS data bits */
390 if (ir_scan)
391 {
392 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
393 cur_state = TAP_SI;
394 }
395 else
396 {
397 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
398 cur_state = TAP_SD;
399 }
400 //DEBUG("added TMS scan (no read)");
401 }
402
403 /* add command for complete bytes */
404 if (num_bytes > 1)
405 {
406 if (type == SCAN_IO)
407 {
408 /* Clock Data Bytes In and Out LSB First */
409 BUFFER_ADD = 0x39;
410 //DEBUG("added TDI bytes (io %i)", num_bytes);
411 }
412 else if (type == SCAN_OUT)
413 {
414 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
415 BUFFER_ADD = 0x19;
416 //DEBUG("added TDI bytes (o)");
417 }
418 else if (type == SCAN_IN)
419 {
420 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
421 BUFFER_ADD = 0x28;
422 //DEBUG("added TDI bytes (i %i)", num_bytes);
423 }
424 BUFFER_ADD = (num_bytes-2) & 0xff;
425 BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
426 }
427 if (type != SCAN_IN)
428 {
429 /* add complete bytes */
430 while(num_bytes-- > 1)
431 {
432 BUFFER_ADD = buffer[cur_byte];
433 cur_byte++;
434 bits_left -= 8;
435 }
436 }
437 if (type == SCAN_IN)
438 {
439 bits_left -= 8 * (num_bytes - 1);
440 }
441
442 /* the most signifcant bit is scanned during TAP movement */
443 if (type != SCAN_IN)
444 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
445 else
446 last_bit = 0;
447
448 /* process remaining bits but the last one */
449 if (bits_left > 1)
450 {
451 if (type == SCAN_IO)
452 {
453 /* Clock Data Bits In and Out LSB First */
454 BUFFER_ADD = 0x3b;
455 //DEBUG("added TDI bits (io) %i", bits_left - 1);
456 }
457 else if (type == SCAN_OUT)
458 {
459 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
460 BUFFER_ADD = 0x1b;
461 //DEBUG("added TDI bits (o)");
462 }
463 else if (type == SCAN_IN)
464 {
465 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
466 BUFFER_ADD = 0x2a;
467 //DEBUG("added TDI bits (i %i)", bits_left - 1);
468 }
469 BUFFER_ADD = bits_left - 2;
470 if (type != SCAN_IN)
471 BUFFER_ADD = buffer[cur_byte];
472 }
473
474 /* move from Shift-IR/DR to end state */
475 if (type != SCAN_OUT)
476 {
477 /* Clock Data to TMS/CS Pin with Read */
478 BUFFER_ADD = 0x6b;
479 //DEBUG("added TMS scan (read)");
480 }
481 else
482 {
483 /* Clock Data to TMS/CS Pin (no Read) */
484 BUFFER_ADD = 0x4b;
485 //DEBUG("added TMS scan (no read)");
486 }
487 BUFFER_ADD = 0x6;
488 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
489 cur_state = end_state;
490
491 }
492
493 int ftd2xx_predict_scan_out(int scan_size, enum scan_type type)
494 {
495 int predicted_size = 3;
496
497 if (cur_state != TAP_SD)
498 predicted_size += 3;
499
500 if (type == SCAN_IN) /* only from device to host */
501 {
502 /* complete bytes */
503 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
504 /* remaining bits - 1 (up to 7) */
505 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
506 }
507 else /* host to device, or bidirectional */
508 {
509 /* complete bytes */
510 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
511 /* remaining bits -1 (up to 7) */
512 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
513 }
514
515 return predicted_size;
516 }
517
518 int ftd2xx_predict_scan_in(int scan_size, enum scan_type type)
519 {
520 int predicted_size = 0;
521
522 if (type != SCAN_OUT)
523 {
524 /* complete bytes */
525 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
526 /* remaining bits - 1 */
527 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
528 /* last bit (from TMS scan) */
529 predicted_size += 1;
530 }
531
532 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
533
534 return predicted_size;
535 }
536
537 void usbjtag_reset(int trst, int srst)
538 {
539 if (trst == 1)
540 {
541 cur_state = TAP_TLR;
542 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
543 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
544 else
545 low_output &= ~nTRST; /* switch output low */
546 }
547 else if (trst == 0)
548 {
549 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
550 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
551 else
552 low_output |= nTRST; /* switch output high */
553 }
554
555 if (srst == 1)
556 {
557 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
558 low_output &= ~nSRST; /* switch output low */
559 else
560 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
561 }
562 else if (srst == 0)
563 {
564 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
565 low_output |= nSRST; /* switch output high */
566 else
567 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
568 }
569
570 /* command "set data bits low byte" */
571 BUFFER_ADD = 0x80;
572 BUFFER_ADD = low_output;
573 BUFFER_ADD = low_direction;
574
575 }
576
577 void jtagkey_reset(int trst, int srst)
578 {
579 if (trst == 1)
580 {
581 cur_state = TAP_TLR;
582 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
583 high_output &= ~nTRSTnOE;
584 else
585 high_output &= ~nTRST;
586 }
587 else if (trst == 0)
588 {
589 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
590 high_output |= nTRSTnOE;
591 else
592 high_output |= nTRST;
593 }
594
595 if (srst == 1)
596 {
597 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
598 high_output &= ~nSRST;
599 else
600 high_output &= ~nSRSTnOE;
601 }
602 else if (srst == 0)
603 {
604 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
605 high_output |= nSRST;
606 else
607 high_output |= nSRSTnOE;
608 }
609
610 /* command "set data bits high byte" */
611 BUFFER_ADD = 0x82;
612 BUFFER_ADD = high_output;
613 BUFFER_ADD = high_direction;
614 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
615 }
616
617 int ftd2xx_execute_queue()
618 {
619 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
620 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
621 u8 *buffer;
622 int scan_size; /* size of IR or DR scan */
623 enum scan_type type;
624 int i;
625 int predicted_size = 0;
626 int require_send = 0;
627
628 ftd2xx_buffer_size = 0;
629 ftd2xx_expect_read = 0;
630
631 while (cmd)
632 {
633 switch(cmd->type)
634 {
635 case JTAG_END_STATE:
636 if (cmd->cmd.end_state->end_state != -1)
637 ftd2xx_end_state(cmd->cmd.end_state->end_state);
638 break;
639 case JTAG_RESET:
640 /* only send the maximum buffer size that FT2232C can handle */
641 predicted_size = 3;
642 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
643 {
644 ftd2xx_send_and_recv(first_unsent, cmd);
645 require_send = 0;
646 first_unsent = cmd;
647 }
648
649 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
650 require_send = 1;
651
652 #ifdef _DEBUG_JTAG_IO_
653 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
654 #endif
655 break;
656 case JTAG_RUNTEST:
657 /* only send the maximum buffer size that FT2232C can handle */
658 predicted_size = 0;
659 if (cur_state != TAP_RTI)
660 predicted_size += 3;
661 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
662 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
663 predicted_size += 3;
664 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
665 predicted_size += 3;
666 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
667 {
668 ftd2xx_send_and_recv(first_unsent, cmd);
669 require_send = 0;
670 first_unsent = cmd;
671 }
672 if (cur_state != TAP_RTI)
673 {
674 /* command "Clock Data to TMS/CS Pin (no Read)" */
675 BUFFER_ADD = 0x4b;
676 /* scan 7 bit */
677 BUFFER_ADD = 0x6;
678 /* TMS data bits */
679 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
680 cur_state = TAP_RTI;
681 require_send = 1;
682 }
683 i = cmd->cmd.runtest->num_cycles;
684 while (i > 0)
685 {
686 /* command "Clock Data to TMS/CS Pin (no Read)" */
687 BUFFER_ADD = 0x4b;
688 /* scan 7 bit */
689 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
690 /* TMS data bits */
691 BUFFER_ADD = 0x0;
692 cur_state = TAP_RTI;
693 i -= (i > 7) ? 7 : i;
694 //DEBUG("added TMS scan (no read)");
695 }
696 if (cmd->cmd.runtest->end_state != -1)
697 ftd2xx_end_state(cmd->cmd.runtest->end_state);
698 if (cur_state != end_state)
699 {
700 /* command "Clock Data to TMS/CS Pin (no Read)" */
701 BUFFER_ADD = 0x4b;
702 /* scan 7 bit */
703 BUFFER_ADD = 0x6;
704 /* TMS data bits */
705 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
706 cur_state = end_state;
707 //DEBUG("added TMS scan (no read)");
708 }
709 require_send = 1;
710 #ifdef _DEBUG_JTAG_IO_
711 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
712 #endif
713 break;
714 case JTAG_STATEMOVE:
715 /* only send the maximum buffer size that FT2232C can handle */
716 predicted_size = 3;
717 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
718 {
719 ftd2xx_send_and_recv(first_unsent, cmd);
720 require_send = 0;
721 first_unsent = cmd;
722 }
723 if (cmd->cmd.statemove->end_state != -1)
724 ftd2xx_end_state(cmd->cmd.statemove->end_state);
725 /* command "Clock Data to TMS/CS Pin (no Read)" */
726 BUFFER_ADD = 0x4b;
727 /* scan 7 bit */
728 BUFFER_ADD = 0x6;
729 /* TMS data bits */
730 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
731 //DEBUG("added TMS scan (no read)");
732 cur_state = end_state;
733 require_send = 1;
734 #ifdef _DEBUG_JTAG_IO_
735 DEBUG("statemove: %i", end_state);
736 #endif
737 break;
738 case JTAG_PATHMOVE:
739 /* only send the maximum buffer size that FT2232C can handle */
740 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
741 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
742 {
743 ftd2xx_send_and_recv(first_unsent, cmd);
744 require_send = 0;
745 first_unsent = cmd;
746 }
747 ftd2xx_add_pathmove(cmd->cmd.pathmove);
748 require_send = 1;
749 #ifdef _DEBUG_JTAG_IO_
750 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
751 #endif
752 break;
753 case JTAG_SCAN:
754 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
755 type = jtag_scan_type(cmd->cmd.scan);
756 predicted_size = ftd2xx_predict_scan_out(scan_size, type);
757 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
758 {
759 DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
760 ftd2xx_send_and_recv(first_unsent, cmd);
761 require_send = 0;
762 first_unsent = cmd;
763 }
764 ftd2xx_expect_read += ftd2xx_predict_scan_in(scan_size, type);
765 //DEBUG("new read size: %i", ftd2xx_expect_read);
766 if (cmd->cmd.scan->end_state != -1)
767 ftd2xx_end_state(cmd->cmd.scan->end_state);
768 ftd2xx_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
769 require_send = 1;
770 if (buffer)
771 free(buffer);
772 #ifdef _DEBUG_JTAG_IO_
773 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
774 #endif
775 break;
776 case JTAG_SLEEP:
777 ftd2xx_send_and_recv(first_unsent, cmd);
778 first_unsent = cmd->next;
779 jtag_sleep(cmd->cmd.sleep->us);
780 #ifdef _DEBUG_JTAG_IO_
781 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
782 #endif
783 break;
784 default:
785 ERROR("BUG: unknown JTAG command type encountered");
786 exit(-1);
787 }
788 cmd = cmd->next;
789 }
790
791 if (require_send > 0)
792 ftd2xx_send_and_recv(first_unsent, cmd);
793
794 return ERROR_OK;
795 }
796
797 int ftd2xx_init(void)
798 {
799 u8 latency_timer;
800 FT_STATUS status;
801 DWORD num_devices;
802
803 ftd2xx_layout_t *cur_layout = ftd2xx_layouts;
804
805 if ((ftd2xx_layout == NULL) || (ftd2xx_layout[0] == 0))
806 {
807 ftd2xx_layout = "usbjtag";
808 WARNING("No ftd2xx layout specified, using default 'usbjtag'");
809 }
810
811 while (cur_layout->name)
812 {
813 if (strcmp(cur_layout->name, ftd2xx_layout) == 0)
814 {
815 layout = cur_layout;
816 break;
817 }
818 cur_layout++;
819 }
820
821 if (!layout)
822 {
823 ERROR("No matching layout found for %s", ftd2xx_layout);
824 return ERROR_JTAG_INIT_FAILED;
825 }
826
827 if (ftd2xx_device_desc == NULL)
828 {
829 WARNING("no ftd2xx device description specified, using default 'Dual RS232'");
830 ftd2xx_device_desc = "Dual RS232";
831 }
832
833 #if IS_WIN32 == 0
834 /* Add JTAGkey Vid/Pid to the linux driver */
835 if ((status = FT_SetVIDPID(ftd2xx_vid, ftd2xx_pid)) != FT_OK)
836 {
837 WARNING("couldn't add %4.4x:%4.4x", ftd2xx_vid, ftd2xx_pid);
838 }
839 #endif
840
841 if ((status = FT_OpenEx(ftd2xx_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
842 {
843 ERROR("unable to open ftdi device: %i", status);
844 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
845 if (status == FT_OK)
846 {
847 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
848 int i;
849
850 for (i = 0; i < num_devices; i++)
851 desc_array[i] = malloc(64);
852 desc_array[num_devices] = NULL;
853
854 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
855
856 if (status == FT_OK)
857 {
858 ERROR("ListDevices: %d\n", num_devices);
859 for (i = 0; i < num_devices; i++)
860 ERROR("%i: %s", i, desc_array[i]);
861 }
862
863 for (i = 0; i < num_devices; i++)
864 free(desc_array[i]);
865 free(desc_array);
866 }
867 else
868 {
869 printf("ListDevices: NONE\n");
870 }
871 return ERROR_JTAG_INIT_FAILED;
872 }
873
874 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
875 {
876 ERROR("unable to set latency timer: %i", status);
877 return ERROR_JTAG_INIT_FAILED;
878 }
879
880 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
881 {
882 ERROR("unable to get latency timer: %i", status);
883 return ERROR_JTAG_INIT_FAILED;
884 }
885 else
886 {
887 DEBUG("current latency timer: %i", latency_timer);
888 }
889
890 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
891 {
892 ERROR("unable to enable bit i/o mode: %i", status);
893 return ERROR_JTAG_INIT_FAILED;
894 }
895
896 ftd2xx_buffer_size = 0;
897 ftd2xx_buffer = malloc(FTD2XX_BUFFER_SIZE);
898
899 if (layout->init() != ERROR_OK)
900 return ERROR_JTAG_INIT_FAILED;
901
902 ftd2xx_speed(jtag_speed);
903
904 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
905 {
906 ERROR("error purging ftd2xx device: %i", status);
907 return ERROR_JTAG_INIT_FAILED;
908 }
909
910 return ERROR_OK;
911 }
912
913 int usbjtag_init(void)
914 {
915 u8 buf[3];
916 FT_STATUS status;
917 DWORD bytes_written;
918
919 low_output = 0x08;
920 low_direction = 0x0b;
921
922 nTRST = 0x10;
923 nTRSTnOE = 0x10;
924 nSRST = 0x40;
925 nSRSTnOE = 0x40;
926
927 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
928 {
929 low_direction &= ~nTRSTnOE; /* nTRST input */
930 low_output &= ~nTRST; /* nTRST = 0 */
931 }
932 else
933 {
934 low_direction |= nTRSTnOE; /* nTRST output */
935 low_output |= nTRST; /* nTRST = 1 */
936 }
937
938 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
939 {
940 low_direction |= nSRSTnOE; /* nSRST output */
941 low_output |= nSRST; /* nSRST = 1 */
942 }
943 else
944 {
945 low_direction &= ~nSRSTnOE; /* nSRST input */
946 low_output &= ~nSRST; /* nSRST = 0 */
947 }
948
949 /* initialize low byte for jtag */
950 buf[0] = 0x80; /* command "set data bits low byte" */
951 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
952 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
953 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
954
955 if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
956 {
957 ERROR("couldn't write to ftdi device: %i", status);
958 return ERROR_JTAG_INIT_FAILED;
959 }
960
961 return ERROR_OK;
962 }
963
964 int jtagkey_init(void)
965 {
966 u8 buf[3];
967 FT_STATUS status;
968 DWORD bytes_written;
969
970 low_output = 0x08;
971 low_direction = 0x1b;
972
973 /* initialize low byte for jtag */
974 buf[0] = 0x80; /* command "set data bits low byte" */
975 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
976 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
977 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
978
979 if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
980 {
981 ERROR("couldn't write to ftdi device: %i", status);
982 return ERROR_JTAG_INIT_FAILED;
983 }
984
985 if (strcmp(layout->name, "jtagkey") == 0)
986 {
987 nTRST = 0x01;
988 nTRSTnOE = 0x4;
989 nSRST = 0x02;
990 nSRSTnOE = 0x08;
991 }
992 else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
993 {
994 nTRST = 0x02;
995 nTRSTnOE = 0x1;
996 nSRST = 0x08;
997 nSRSTnOE = 0x04;
998 }
999 else
1000 {
1001 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1002 exit(-1);
1003 }
1004
1005 high_output = 0x0;
1006 high_direction = 0x0f;
1007
1008 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1009 {
1010 high_output |= nTRSTnOE;
1011 high_output &= ~nTRST;
1012 }
1013 else
1014 {
1015 high_output &= ~nTRSTnOE;
1016 high_output |= nTRST;
1017 }
1018
1019 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1020 {
1021 high_output &= ~nSRSTnOE;
1022 high_output |= nSRST;
1023 }
1024 else
1025 {
1026 high_output |= nSRSTnOE;
1027 high_output &= ~nSRST;
1028 }
1029
1030 /* initialize high port */
1031 buf[0] = 0x82; /* command "set data bits low byte" */
1032 buf[1] = high_output; /* value */
1033 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1034 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1035
1036 if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
1037 {
1038 ERROR("couldn't write to ftdi device: %i", status);
1039 return ERROR_JTAG_INIT_FAILED;
1040 }
1041
1042 return ERROR_OK;
1043 }
1044
1045 int ftd2xx_quit(void)
1046 {
1047 FT_STATUS status;
1048
1049 status = FT_Close(ftdih);
1050
1051 free(ftd2xx_buffer);
1052
1053 return ERROR_OK;
1054 }
1055
1056 int ftd2xx_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1057 {
1058 if (argc == 1)
1059 {
1060 ftd2xx_device_desc = strdup(args[0]);
1061 }
1062 else
1063 {
1064 ERROR("expected exactly one argument to ftd2xx_device_desc <description>");
1065 }
1066
1067 return ERROR_OK;
1068 }
1069
1070 int ftd2xx_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1071 {
1072 if (argc == 0)
1073 return ERROR_OK;
1074
1075 ftd2xx_layout = malloc(strlen(args[0]) + 1);
1076 strcpy(ftd2xx_layout, args[0]);
1077
1078 return ERROR_OK;
1079 }
1080
1081 int ftd2xx_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1082 {
1083 if (argc >= 2)
1084 {
1085 ftd2xx_vid = strtol(args[0], NULL, 0);
1086 ftd2xx_pid = strtol(args[1], NULL, 0);
1087 }
1088 else
1089 {
1090 WARNING("incomplete ftd2xx_vid_pid configuration directive");
1091 }
1092
1093 return ERROR_OK;
1094 }

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)