1 /***************************************************************************
2 * Copyright (C) 2004, 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
43 /* FT2232 access library includes */
44 #if BUILD_FT2232_FTD2XX == 1
46 #elif BUILD_FT2232_LIBFTDI == 1
53 /* enable this to debug io latency
56 #define _DEBUG_USB_IO_
59 /* enable this to debug communication
62 #define _DEBUG_USB_COMMS_
65 int ft2232_execute_queue(void);
67 int ft2232_speed(int speed
);
68 int ft2232_register_commands(struct command_context_s
*cmd_ctx
);
69 int ft2232_init(void);
70 int ft2232_quit(void);
72 int ft2232_handle_device_desc_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
73 int ft2232_handle_serial_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
74 int ft2232_handle_layout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
75 int ft2232_handle_vid_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
77 char *ft2232_device_desc
= NULL
;
78 char *ft2232_serial
= NULL
;
79 char *ft2232_layout
= NULL
;
80 u16 ft2232_vid
= 0x0403;
81 u16 ft2232_pid
= 0x6010;
83 typedef struct ft2232_layout_s
87 void(*reset
)(int trst
, int srst
);
91 /* init procedures for supported layouts */
92 int usbjtag_init(void);
93 int jtagkey_init(void);
94 int olimex_jtag_init(void);
97 /* reset procedures for supported layouts */
98 void usbjtag_reset(int trst
, int srst
);
99 void jtagkey_reset(int trst
, int srst
);
100 void olimex_jtag_reset(int trst
, int srst
);
101 void m5960_reset(int trst
, int srst
);
103 /* blink procedures for layouts that support a blinking led */
104 void olimex_jtag_blink(void);
106 ft2232_layout_t ft2232_layouts
[] =
108 {"usbjtag", usbjtag_init
, usbjtag_reset
, NULL
},
109 {"jtagkey", jtagkey_init
, jtagkey_reset
, NULL
},
110 {"jtagkey_prototype_v1", jtagkey_init
, jtagkey_reset
, NULL
},
111 {"signalyzer", usbjtag_init
, usbjtag_reset
, NULL
},
112 {"olimex-jtag", olimex_jtag_init
, olimex_jtag_reset
, olimex_jtag_blink
},
113 {"m5960", m5960_init
, m5960_reset
, NULL
},
117 static u8 nTRST
, nTRSTnOE
, nSRST
, nSRSTnOE
;
119 static ft2232_layout_t
*layout
;
120 static u8 low_output
= 0x0;
121 static u8 low_direction
= 0x0;
122 static u8 high_output
= 0x0;
123 static u8 high_direction
= 0x0;
125 #if BUILD_FT2232_FTD2XX == 1
126 static FT_HANDLE ftdih
= NULL
;
127 #elif BUILD_FT2232_LIBFTDI == 1
128 static struct ftdi_context ftdic
;
131 static u8
*ft2232_buffer
= NULL
;
132 static int ft2232_buffer_size
= 0;
133 static int ft2232_read_pointer
= 0;
134 static int ft2232_expect_read
= 0;
135 #define FT2232_BUFFER_SIZE 131072
136 #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
137 #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
139 jtag_interface_t ft2232_interface
=
144 .execute_queue
= ft2232_execute_queue
,
146 .support_pathmove
= 1,
148 .speed
= ft2232_speed
,
149 .register_commands
= ft2232_register_commands
,
154 int ft2232_write(u8
*buf
, int size
, u32
* bytes_written
)
156 #if BUILD_FT2232_FTD2XX == 1
158 DWORD dw_bytes_written
;
159 if ((status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
)) != FT_OK
)
161 *bytes_written
= dw_bytes_written
;
162 ERROR("FT_Write returned: %i", status
);
163 return ERROR_JTAG_DEVICE_ERROR
;
167 *bytes_written
= dw_bytes_written
;
170 #elif BUILD_FT2232_LIBFTDI == 1
172 if ((retval
= ftdi_write_data(&ftdic
, buf
, size
)) < 0)
175 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
176 return ERROR_JTAG_DEVICE_ERROR
;
180 *bytes_written
= retval
;
186 int ft2232_read(u8
* buf
, int size
, u32
* bytes_read
)
188 #if BUILD_FT2232_FTD2XX == 1
191 if ((status
= FT_Read(ftdih
, buf
, size
, &dw_bytes_read
)) != FT_OK
)
193 *bytes_read
= dw_bytes_read
;
194 ERROR("FT_Read returned: %i", status
);
195 return ERROR_JTAG_DEVICE_ERROR
;
197 *bytes_read
= dw_bytes_read
;
200 #elif BUILD_FT2232_LIBFTDI == 1
205 while ((*bytes_read
< size
) && timeout
--)
207 if ((retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
, size
- *bytes_read
)) < 0)
210 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic
));
211 return ERROR_JTAG_DEVICE_ERROR
;
213 *bytes_read
+= retval
;
219 int ft2232_speed(int speed
)
225 buf
[0] = 0x86; /* command "set divisor" */
226 buf
[1] = speed
& 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
227 buf
[2] = (speed
>> 8) & 0xff; /* valueH */
229 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
230 if (((retval
= ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
232 ERROR("couldn't set FT2232 TCK speed");
239 int ft2232_register_commands(struct command_context_s
*cmd_ctx
)
241 register_command(cmd_ctx
, NULL
, "ft2232_device_desc", ft2232_handle_device_desc_command
,
242 COMMAND_CONFIG
, NULL
);
243 register_command(cmd_ctx
, NULL
, "ft2232_serial", ft2232_handle_serial_command
,
244 COMMAND_CONFIG
, NULL
);
245 register_command(cmd_ctx
, NULL
, "ft2232_layout", ft2232_handle_layout_command
,
246 COMMAND_CONFIG
, NULL
);
247 register_command(cmd_ctx
, NULL
, "ft2232_vid_pid", ft2232_handle_vid_pid_command
,
248 COMMAND_CONFIG
, NULL
);
252 void ft2232_end_state(state
)
254 if (tap_move_map
[state
] != -1)
258 ERROR("BUG: %i is not a valid end state", state
);
263 void ft2232_read_scan(enum scan_type type
, u8
* buffer
, int scan_size
)
265 int num_bytes
= ((scan_size
+ 7) / 8);
266 int bits_left
= scan_size
;
269 while(num_bytes
-- > 1)
271 buffer
[cur_byte
] = BUFFER_READ
;
276 buffer
[cur_byte
] = 0x0;
280 buffer
[cur_byte
] = BUFFER_READ
>> 1;
283 buffer
[cur_byte
] = (buffer
[cur_byte
] | ((BUFFER_READ
& 0x02) << 6)) >> (8 - bits_left
);
287 void ft2232_debug_dump_buffer(void)
293 for (i
= 0; i
< ft2232_buffer_size
; i
++)
295 line_p
+= snprintf(line_p
, 256 - (line_p
- line
), "%2.2x ", ft2232_buffer
[i
]);
307 int ft2232_send_and_recv(jtag_command_t
*first
, jtag_command_t
*last
)
317 #ifdef _DEBUG_USB_IO_
318 struct timeval start
, inter
, inter2
, end
;
319 struct timeval d_inter
, d_inter2
, d_end
;
322 #ifdef _DEBUG_USB_COMMS_
323 DEBUG("write buffer (size %i):", ft2232_buffer_size
);
324 ft2232_debug_dump_buffer();
327 #ifdef _DEBUG_USB_IO_
328 gettimeofday(&start
, NULL
);
331 if ((retval
= ft2232_write(ft2232_buffer
, ft2232_buffer_size
, &bytes_written
)) != ERROR_OK
)
333 ERROR("couldn't write MPSSE commands to FT2232");
337 #ifdef _DEBUG_USB_IO_
338 gettimeofday(&inter
, NULL
);
341 if (ft2232_expect_read
)
344 ft2232_buffer_size
= 0;
346 #ifdef _DEBUG_USB_IO_
347 gettimeofday(&inter2
, NULL
);
350 if ((retval
= ft2232_read(ft2232_buffer
, ft2232_expect_read
, &bytes_read
)) != ERROR_OK
)
352 ERROR("couldn't read from FT2232");
356 #ifdef _DEBUG_USB_IO_
357 gettimeofday(&end
, NULL
);
359 timeval_subtract(&d_inter
, &inter
, &start
);
360 timeval_subtract(&d_inter2
, &inter2
, &start
);
361 timeval_subtract(&d_end
, &end
, &start
);
363 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
);
367 ft2232_buffer_size
= bytes_read
;
369 if (ft2232_expect_read
!= ft2232_buffer_size
)
371 ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read
, ft2232_buffer_size
, 100 - timeout
);
372 ft2232_debug_dump_buffer();
377 #ifdef _DEBUG_USB_COMMS_
378 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout
, ft2232_buffer_size
);
379 ft2232_debug_dump_buffer();
383 ft2232_expect_read
= 0;
384 ft2232_read_pointer
= 0;
392 type
= jtag_scan_type(cmd
->cmd
.scan
);
393 if (type
!= SCAN_OUT
)
395 scan_size
= jtag_scan_size(cmd
->cmd
.scan
);
396 buffer
= calloc(CEIL(scan_size
, 8), 1);
397 ft2232_read_scan(type
, buffer
, scan_size
);
398 jtag_read_buffer(buffer
, cmd
->cmd
.scan
);
408 ft2232_buffer_size
= 0;
413 void ft2232_add_pathmove(pathmove_command_t
*cmd
)
415 int num_states
= cmd
->num_states
;
425 /* command "Clock Data to TMS/CS Pin (no Read)" */
427 /* number of states remaining */
428 BUFFER_ADD
= (num_states
% 7) - 1;
430 while (num_states
% 7)
432 if (tap_transitions
[cur_state
].low
== cmd
->path
[state_count
])
433 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x0);
434 else if (tap_transitions
[cur_state
].high
== cmd
->path
[state_count
])
435 buf_set_u32(&tms_byte
, bit_count
++, 1, 0x1);
438 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings
[cur_state
], tap_state_strings
[cmd
->path
[state_count
]]);
442 cur_state
= cmd
->path
[state_count
];
447 BUFFER_ADD
= tms_byte
;
450 end_state
= cur_state
;
453 void ft2232_add_scan(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
)
455 int num_bytes
= (scan_size
+ 7) / 8;
456 int bits_left
= scan_size
;
460 if ((!ir_scan
&& (cur_state
!= TAP_SD
)) || (ir_scan
&& (cur_state
!= TAP_SI
)))
462 /* command "Clock Data to TMS/CS Pin (no Read)" */
469 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SI
);
474 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_SD
);
477 //DEBUG("added TMS scan (no read)");
480 /* add command for complete bytes */
485 /* Clock Data Bytes In and Out LSB First */
487 //DEBUG("added TDI bytes (io %i)", num_bytes);
489 else if (type
== SCAN_OUT
)
491 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
493 //DEBUG("added TDI bytes (o)");
495 else if (type
== SCAN_IN
)
497 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
499 //DEBUG("added TDI bytes (i %i)", num_bytes);
501 BUFFER_ADD
= (num_bytes
-2) & 0xff;
502 BUFFER_ADD
= ((num_bytes
-2) >> 8) & 0xff;
506 /* add complete bytes */
507 while(num_bytes
-- > 1)
509 BUFFER_ADD
= buffer
[cur_byte
];
516 bits_left
-= 8 * (num_bytes
- 1);
519 /* the most signifcant bit is scanned during TAP movement */
521 last_bit
= (buffer
[cur_byte
] >> (bits_left
- 1)) & 0x1;
525 /* process remaining bits but the last one */
530 /* Clock Data Bits In and Out LSB First */
532 //DEBUG("added TDI bits (io) %i", bits_left - 1);
534 else if (type
== SCAN_OUT
)
536 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
538 //DEBUG("added TDI bits (o)");
540 else if (type
== SCAN_IN
)
542 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
544 //DEBUG("added TDI bits (i %i)", bits_left - 1);
546 BUFFER_ADD
= bits_left
- 2;
548 BUFFER_ADD
= buffer
[cur_byte
];
551 /* move from Shift-IR/DR to end state */
552 if (type
!= SCAN_OUT
)
554 /* Clock Data to TMS/CS Pin with Read */
556 //DEBUG("added TMS scan (read)");
560 /* Clock Data to TMS/CS Pin (no Read) */
562 //DEBUG("added TMS scan (no read)");
565 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
) | (last_bit
<< 7);
566 cur_state
= end_state
;
570 int ft2232_predict_scan_out(int scan_size
, enum scan_type type
)
572 int predicted_size
= 3;
574 if (cur_state
!= TAP_SD
)
577 if (type
== SCAN_IN
) /* only from device to host */
580 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? 3 : 0;
581 /* remaining bits - 1 (up to 7) */
582 predicted_size
+= ((scan_size
- 1) % 8) ? 2 : 0;
584 else /* host to device, or bidirectional */
587 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) + 3 - 1) : 0;
588 /* remaining bits -1 (up to 7) */
589 predicted_size
+= ((scan_size
- 1) % 8) ? 3 : 0;
592 return predicted_size
;
595 int ft2232_predict_scan_in(int scan_size
, enum scan_type type
)
597 int predicted_size
= 0;
599 if (type
!= SCAN_OUT
)
602 predicted_size
+= (CEIL(scan_size
, 8) > 1) ? (CEIL(scan_size
, 8) - 1) : 0;
603 /* remaining bits - 1 */
604 predicted_size
+= ((scan_size
- 1) % 8) ? 1 : 0;
605 /* last bit (from TMS scan) */
609 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
611 return predicted_size
;
614 void usbjtag_reset(int trst
, int srst
)
619 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
620 low_direction
|= nTRSTnOE
; /* switch to output pin (output is low) */
622 low_output
&= ~nTRST
; /* switch output low */
626 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
627 low_direction
&= ~nTRSTnOE
; /* switch to input pin (high-Z + internal and external pullup) */
629 low_output
|= nTRST
; /* switch output high */
634 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
635 low_output
&= ~nSRST
; /* switch output low */
637 low_direction
|= nSRSTnOE
; /* switch to output pin (output is low) */
641 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
642 low_output
|= nSRST
; /* switch output high */
644 low_direction
&= ~nSRSTnOE
; /* switch to input pin (high-Z) */
647 /* command "set data bits low byte" */
649 BUFFER_ADD
= low_output
;
650 BUFFER_ADD
= low_direction
;
654 void jtagkey_reset(int trst
, int srst
)
659 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
660 high_output
&= ~nTRSTnOE
;
662 high_output
&= ~nTRST
;
666 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
667 high_output
|= nTRSTnOE
;
669 high_output
|= nTRST
;
674 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
675 high_output
&= ~nSRST
;
677 high_output
&= ~nSRSTnOE
;
681 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
682 high_output
|= nSRST
;
684 high_output
|= nSRSTnOE
;
687 /* command "set data bits high byte" */
689 BUFFER_ADD
= high_output
;
690 BUFFER_ADD
= high_direction
;
691 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
694 void olimex_jtag_reset(int trst
, int srst
)
699 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
700 high_output
&= ~nTRSTnOE
;
702 high_output
&= ~nTRST
;
706 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
707 high_output
|= nTRSTnOE
;
709 high_output
|= nTRST
;
714 high_output
|= nSRST
;
718 high_output
&= ~nSRST
;
721 /* command "set data bits high byte" */
723 BUFFER_ADD
= high_output
;
724 BUFFER_ADD
= high_direction
;
725 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
728 void m5960_reset(int trst
, int srst
)
733 low_output
&= ~nTRST
;
746 low_output
&= ~nSRST
;
749 /* command "set data bits low byte" */
751 BUFFER_ADD
= low_output
;
752 BUFFER_ADD
= low_direction
;
753 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst
, srst
, high_output
, high_direction
);
756 int ft2232_execute_queue()
758 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
759 jtag_command_t
*first_unsent
= cmd
; /* next command that has to be sent */
761 int scan_size
; /* size of IR or DR scan */
764 int predicted_size
= 0;
765 int require_send
= 0;
767 ft2232_buffer_size
= 0;
768 ft2232_expect_read
= 0;
770 /* blink, if the current layout has that feature */
779 if (cmd
->cmd
.end_state
->end_state
!= -1)
780 ft2232_end_state(cmd
->cmd
.end_state
->end_state
);
783 /* only send the maximum buffer size that FT2232C can handle */
785 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
787 ft2232_send_and_recv(first_unsent
, cmd
);
792 layout
->reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
795 #ifdef _DEBUG_JTAG_IO_
796 DEBUG("trst: %i, srst: %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
800 /* only send the maximum buffer size that FT2232C can handle */
802 if (cur_state
!= TAP_RTI
)
804 predicted_size
+= 3 * CEIL(cmd
->cmd
.runtest
->num_cycles
, 7);
805 if ((cmd
->cmd
.runtest
->end_state
!= -1) && (cmd
->cmd
.runtest
->end_state
!= TAP_RTI
))
807 if ((cmd
->cmd
.runtest
->end_state
== -1) && (end_state
!= TAP_RTI
))
809 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
811 ft2232_send_and_recv(first_unsent
, cmd
);
815 if (cur_state
!= TAP_RTI
)
817 /* command "Clock Data to TMS/CS Pin (no Read)" */
822 BUFFER_ADD
= TAP_MOVE(cur_state
, TAP_RTI
);
826 i
= cmd
->cmd
.runtest
->num_cycles
;
829 /* command "Clock Data to TMS/CS Pin (no Read)" */
832 BUFFER_ADD
= (i
> 7) ? 6 : (i
- 1);
836 i
-= (i
> 7) ? 7 : i
;
837 //DEBUG("added TMS scan (no read)");
839 if (cmd
->cmd
.runtest
->end_state
!= -1)
840 ft2232_end_state(cmd
->cmd
.runtest
->end_state
);
841 if (cur_state
!= end_state
)
843 /* command "Clock Data to TMS/CS Pin (no Read)" */
848 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
);
849 cur_state
= end_state
;
850 //DEBUG("added TMS scan (no read)");
853 #ifdef _DEBUG_JTAG_IO_
854 DEBUG("runtest: %i, end in %i", cmd
->cmd
.runtest
->num_cycles
, end_state
);
858 /* only send the maximum buffer size that FT2232C can handle */
860 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
862 ft2232_send_and_recv(first_unsent
, cmd
);
866 if (cmd
->cmd
.statemove
->end_state
!= -1)
867 ft2232_end_state(cmd
->cmd
.statemove
->end_state
);
868 /* command "Clock Data to TMS/CS Pin (no Read)" */
873 BUFFER_ADD
= TAP_MOVE(cur_state
, end_state
);
874 //DEBUG("added TMS scan (no read)");
875 cur_state
= end_state
;
877 #ifdef _DEBUG_JTAG_IO_
878 DEBUG("statemove: %i", end_state
);
882 /* only send the maximum buffer size that FT2232C can handle */
883 predicted_size
= 3 * CEIL(cmd
->cmd
.pathmove
->num_states
, 7);
884 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
886 ft2232_send_and_recv(first_unsent
, cmd
);
890 ft2232_add_pathmove(cmd
->cmd
.pathmove
);
892 #ifdef _DEBUG_JTAG_IO_
893 DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
897 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
898 type
= jtag_scan_type(cmd
->cmd
.scan
);
899 predicted_size
= ft2232_predict_scan_out(scan_size
, type
);
900 if (ft2232_buffer_size
+ predicted_size
+ 1 > FT2232_BUFFER_SIZE
)
902 DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent
, cmd
);
903 ft2232_send_and_recv(first_unsent
, cmd
);
907 ft2232_expect_read
+= ft2232_predict_scan_in(scan_size
, type
);
908 //DEBUG("new read size: %i", ft2232_expect_read);
909 if (cmd
->cmd
.scan
->end_state
!= -1)
910 ft2232_end_state(cmd
->cmd
.scan
->end_state
);
911 ft2232_add_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
915 #ifdef _DEBUG_JTAG_IO_
916 DEBUG("%s scan, %i bit, end in %i", (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
, end_state
);
920 ft2232_send_and_recv(first_unsent
, cmd
);
921 first_unsent
= cmd
->next
;
922 jtag_sleep(cmd
->cmd
.sleep
->us
);
923 #ifdef _DEBUG_JTAG_IO_
924 DEBUG("sleep %i usec", cmd
->cmd
.sleep
->us
);
928 ERROR("BUG: unknown JTAG command type encountered");
934 if (require_send
> 0)
935 ft2232_send_and_recv(first_unsent
, cmd
);
940 int ft2232_init(void)
947 #if BUILD_FT2232_FTD2XX == 1
949 DWORD openex_flags
= 0;
950 char *openex_string
= NULL
;
953 ft2232_layout_t
*cur_layout
= ft2232_layouts
;
955 if ((ft2232_layout
== NULL
) || (ft2232_layout
[0] == 0))
957 ft2232_layout
= "usbjtag";
958 WARNING("No ft2232 layout specified, using default 'usbjtag'");
961 while (cur_layout
->name
)
963 if (strcmp(cur_layout
->name
, ft2232_layout
) == 0)
973 ERROR("No matching layout found for %s", ft2232_layout
);
974 return ERROR_JTAG_INIT_FAILED
;
977 #if BUILD_FT2232_FTD2XX == 1
978 DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout
);
979 #elif BUILD_FT2232_LIBFTDI == 1
980 DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout
);
983 #if BUILD_FT2232_FTD2XX == 1
985 /* Add non-standard Vid/Pid to the linux driver */
986 if ((status
= FT_SetVIDPID(ft2232_vid
, ft2232_pid
)) != FT_OK
)
988 WARNING("couldn't add %4.4x:%4.4x", ft2232_vid
, ft2232_pid
);
992 if (ft2232_device_desc
&& ft2232_serial
)
994 WARNING("can't open by device description and serial number, giving precedence to serial");
995 ft2232_device_desc
= NULL
;
998 if (ft2232_device_desc
)
1000 openex_string
= ft2232_device_desc
;
1001 openex_flags
= FT_OPEN_BY_DESCRIPTION
;
1003 else if (ft2232_serial
)
1005 openex_string
= ft2232_serial
;
1006 openex_flags
= FT_OPEN_BY_SERIAL_NUMBER
;
1010 ERROR("neither device description nor serial number specified");
1011 ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1013 return ERROR_JTAG_INIT_FAILED
;
1016 if ((status
= FT_OpenEx(openex_string
, openex_flags
, &ftdih
)) != FT_OK
)
1020 ERROR("unable to open ftdi device: %i", status
);
1021 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
1022 if (status
== FT_OK
)
1024 char **desc_array
= malloc(sizeof(char*) * (num_devices
+ 1));
1027 for (i
= 0; i
< num_devices
; i
++)
1028 desc_array
[i
] = malloc(64);
1029 desc_array
[num_devices
] = NULL
;
1031 status
= FT_ListDevices(desc_array
, &num_devices
, FT_LIST_ALL
| openex_flags
);
1033 if (status
== FT_OK
)
1035 ERROR("ListDevices: %d\n", num_devices
);
1036 for (i
= 0; i
< num_devices
; i
++)
1037 ERROR("%i: %s", i
, desc_array
[i
]);
1040 for (i
= 0; i
< num_devices
; i
++)
1041 free(desc_array
[i
]);
1046 printf("ListDevices: NONE\n");
1048 return ERROR_JTAG_INIT_FAILED
;
1051 if ((status
= FT_SetLatencyTimer(ftdih
, 2)) != FT_OK
)
1053 ERROR("unable to set latency timer: %i", status
);
1054 return ERROR_JTAG_INIT_FAILED
;
1057 if ((status
= FT_GetLatencyTimer(ftdih
, &latency_timer
)) != FT_OK
)
1059 ERROR("unable to get latency timer: %i", status
);
1060 return ERROR_JTAG_INIT_FAILED
;
1064 DEBUG("current latency timer: %i", latency_timer
);
1067 if ((status
= FT_SetTimeouts(ftdih
, 5000, 5000)) != FT_OK
)
1069 ERROR("unable to set timeouts: %i", status
);
1070 return ERROR_JTAG_INIT_FAILED
;
1073 if ((status
= FT_SetBitMode(ftdih
, 0x0b, 2)) != FT_OK
)
1075 ERROR("unable to enable bit i/o mode: %i", status
);
1076 return ERROR_JTAG_INIT_FAILED
;
1078 #elif BUILD_FT2232_LIBFTDI == 1
1079 if (ftdi_init(&ftdic
) < 0)
1080 return ERROR_JTAG_INIT_FAILED
;
1082 /* context, vendor id, product id */
1083 if (ftdi_usb_open(&ftdic
, ft2232_vid
, ft2232_pid
) < 0)
1085 ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
1086 return ERROR_JTAG_INIT_FAILED
;
1089 if (ftdi_usb_reset(&ftdic
) < 0)
1091 ERROR("unable to reset ftdi device");
1092 return ERROR_JTAG_INIT_FAILED
;
1095 if (ftdi_set_latency_timer(&ftdic
, 2) < 0)
1097 ERROR("unable to set latency timer");
1098 return ERROR_JTAG_INIT_FAILED
;
1101 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
1103 ERROR("unable to get latency timer");
1104 return ERROR_JTAG_INIT_FAILED
;
1108 DEBUG("current latency timer: %i", latency_timer
);
1111 ftdic
.bitbang_mode
= 0; /* Reset controller */
1112 ftdi_enable_bitbang(&ftdic
, 0x0b); /* ctx, JTAG I/O mask */
1114 ftdic
.bitbang_mode
= 2; /* MPSSE mode */
1115 ftdi_enable_bitbang(&ftdic
, 0x0b); /* ctx, JTAG I/O mask */
1118 ft2232_buffer_size
= 0;
1119 ft2232_buffer
= malloc(FT2232_BUFFER_SIZE
);
1121 if (layout
->init() != ERROR_OK
)
1122 return ERROR_JTAG_INIT_FAILED
;
1124 ft2232_speed(jtag_speed
);
1126 buf
[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
1127 if (((retval
= ft2232_write(buf
, 1, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 1))
1129 ERROR("couldn't write to FT2232 to disable loopback");
1130 return ERROR_JTAG_INIT_FAILED
;
1133 #if BUILD_FT2232_FTD2XX == 1
1134 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
1136 ERROR("error purging ftd2xx device: %i", status
);
1137 return ERROR_JTAG_INIT_FAILED
;
1139 #elif BUILD_FT2232_LIBFTDI == 1
1140 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
1142 ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
1143 return ERROR_JTAG_INIT_FAILED
;
1150 int usbjtag_init(void)
1156 low_direction
= 0x0b;
1158 if (strcmp(ft2232_layout
, "usbjtag") == 0)
1165 else if (strcmp(ft2232_layout
, "signalyzer") == 0)
1174 ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout
);
1175 return ERROR_JTAG_INIT_FAILED
;
1178 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1180 low_direction
&= ~nTRSTnOE
; /* nTRST input */
1181 low_output
&= ~nTRST
; /* nTRST = 0 */
1185 low_direction
|= nTRSTnOE
; /* nTRST output */
1186 low_output
|= nTRST
; /* nTRST = 1 */
1189 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1191 low_direction
|= nSRSTnOE
; /* nSRST output */
1192 low_output
|= nSRST
; /* nSRST = 1 */
1196 low_direction
&= ~nSRSTnOE
; /* nSRST input */
1197 low_output
&= ~nSRST
; /* nSRST = 0 */
1200 /* initialize low byte for jtag */
1201 buf
[0] = 0x80; /* command "set data bits low byte" */
1202 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
1203 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
1204 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1206 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1208 ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
1209 return ERROR_JTAG_INIT_FAILED
;
1215 int jtagkey_init(void)
1221 low_direction
= 0x1b;
1223 /* initialize low byte for jtag */
1224 buf
[0] = 0x80; /* command "set data bits low byte" */
1225 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1226 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1227 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1229 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1231 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1232 return ERROR_JTAG_INIT_FAILED
;
1235 if (strcmp(layout
->name
, "jtagkey") == 0)
1242 else if (strcmp(layout
->name
, "jtagkey_prototype_v1") == 0)
1251 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1256 high_direction
= 0x0f;
1258 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1260 high_output
|= nTRSTnOE
;
1261 high_output
&= ~nTRST
;
1265 high_output
&= ~nTRSTnOE
;
1266 high_output
|= nTRST
;
1269 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1271 high_output
&= ~nSRSTnOE
;
1272 high_output
|= nSRST
;
1276 high_output
|= nSRSTnOE
;
1277 high_output
&= ~nSRST
;
1280 /* initialize high port */
1281 buf
[0] = 0x82; /* command "set data bits high byte" */
1282 buf
[1] = high_output
; /* value */
1283 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1284 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1286 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1288 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1289 return ERROR_JTAG_INIT_FAILED
;
1295 int olimex_jtag_init(void)
1301 low_direction
= 0x1b;
1303 /* initialize low byte for jtag */
1304 buf
[0] = 0x80; /* command "set data bits low byte" */
1305 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1306 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
1307 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1309 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1311 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1312 return ERROR_JTAG_INIT_FAILED
;
1318 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1321 high_direction
= 0x0f;
1323 if (jtag_reset_config
& RESET_TRST_OPEN_DRAIN
)
1325 high_output
|= nTRSTnOE
;
1326 high_output
&= ~nTRST
;
1330 high_output
&= ~nTRSTnOE
;
1331 high_output
|= nTRST
;
1334 if (jtag_reset_config
& RESET_SRST_PUSH_PULL
)
1336 ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
1340 high_output
&= ~nSRST
;
1343 /* turn red LED on */
1344 high_output
|= 0x08;
1346 /* initialize high port */
1347 buf
[0] = 0x82; /* command "set data bits high byte" */
1348 buf
[1] = high_output
; /* value */
1349 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1350 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1352 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1354 ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
1355 return ERROR_JTAG_INIT_FAILED
;
1361 int m5960_init(void)
1367 low_direction
= 0xfb;
1369 /* initialize low byte for jtag */
1370 buf
[0] = 0x80; /* command "set data bits low byte" */
1371 buf
[1] = low_output
; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
1372 buf
[2] = low_direction
; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
1373 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1375 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1377 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1378 return ERROR_JTAG_INIT_FAILED
;
1382 nTRSTnOE
= 0x0; /* not output enable for nTRST */
1384 nSRSTnOE
= 0x00; /* no output enable for nSRST */
1387 high_direction
= 0x0c;
1389 /* turn red LED1 on, LED2 off */
1390 high_output
|= 0x08;
1392 /* initialize high port */
1393 buf
[0] = 0x82; /* command "set data bits high byte" */
1394 buf
[1] = high_output
; /* value */
1395 buf
[2] = high_direction
; /* all outputs (xRST and xRSTnOE) */
1396 DEBUG("%2.2x %2.2x %2.2x", buf
[0], buf
[1], buf
[2]);
1398 if (((ft2232_write(buf
, 3, &bytes_written
)) != ERROR_OK
) || (bytes_written
!= 3))
1400 ERROR("couldn't initialize FT2232 with 'm5960' layout");
1401 return ERROR_JTAG_INIT_FAILED
;
1407 void olimex_jtag_blink(void)
1409 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
1410 * ACBUS3 is bit 3 of the GPIOH port
1412 if (high_output
& 0x08)
1414 /* set port pin high */
1415 high_output
&= 0x07;
1419 /* set port pin low */
1420 high_output
|= 0x08;
1424 BUFFER_ADD
= high_output
;
1425 BUFFER_ADD
= high_direction
;
1428 int ft2232_quit(void)
1430 #if BUILD_FT2232_FTD2XX == 1
1433 status
= FT_Close(ftdih
);
1434 #elif BUILD_FT2232_LIBFTDI == 1
1435 ftdi_disable_bitbang(&ftdic
);
1437 ftdi_usb_close(&ftdic
);
1439 ftdi_deinit(&ftdic
);
1442 free(ft2232_buffer
);
1447 int ft2232_handle_device_desc_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1451 ft2232_device_desc
= strdup(args
[0]);
1455 ERROR("expected exactly one argument to ft2232_device_desc <description>");
1461 int ft2232_handle_serial_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1465 ft2232_serial
= strdup(args
[0]);
1469 ERROR("expected exactly one argument to ft2232_serial <serial-number>");
1475 int ft2232_handle_layout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1480 ft2232_layout
= malloc(strlen(args
[0]) + 1);
1481 strcpy(ft2232_layout
, args
[0]);
1486 int ft2232_handle_vid_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1490 ft2232_vid
= strtol(args
[0], NULL
, 0);
1491 ft2232_pid
= strtol(args
[1], NULL
, 0);
1495 WARNING("incomplete ft2232_vid_pid configuration directive");
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)