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

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)