1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
28 #include <jtag/interface.h>
29 #include <jtag/commands.h>
30 #include "usb_common.h"
36 #define JLINK_WRITE_ENDPOINT 0x02
37 #define JLINK_READ_ENDPOINT 0x81
39 static unsigned int jlink_write_ep
= JLINK_WRITE_ENDPOINT
;
40 static unsigned int jlink_read_ep
= JLINK_READ_ENDPOINT
;
41 static unsigned int jlink_hw_jtag_version
= 2;
43 #define JLINK_USB_TIMEOUT 1000
45 // See Section 1.3.2 of the Segger JLink USB protocol manual
46 /* 2048 is the max value we can use here */
47 //#define JLINK_TAP_BUFFER_SIZE 2048
48 #define JLINK_TAP_BUFFER_SIZE 256
49 //#define JLINK_TAP_BUFFER_SIZE 384
51 #define JLINK_IN_BUFFER_SIZE 2048
52 #define JLINK_OUT_BUFFER_SIZE 2*2048 + 4
53 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
55 /* Global USB buffers */
56 static uint8_t usb_in_buffer
[JLINK_IN_BUFFER_SIZE
];
57 static uint8_t usb_out_buffer
[JLINK_OUT_BUFFER_SIZE
];
58 static uint8_t usb_emu_result_buffer
[JLINK_EMU_RESULT_BUFFER_SIZE
];
60 /* Constants for JLink command */
61 #define EMU_CMD_VERSION 0x01
62 #define EMU_CMD_SET_SPEED 0x05
63 #define EMU_CMD_GET_STATE 0x07
64 #define EMU_CMD_HW_CLOCK 0xc8
65 #define EMU_CMD_HW_TMS0 0xc9
66 #define EMU_CMD_HW_TMS1 0xca
67 #define EMU_CMD_HW_JTAG2 0xce
68 #define EMU_CMD_HW_JTAG3 0xcf
69 #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
70 #define EMU_CMD_HW_RESET0 0xdc
71 #define EMU_CMD_HW_RESET1 0xdd
72 #define EMU_CMD_HW_TRST0 0xde
73 #define EMU_CMD_HW_TRST1 0xdf
74 #define EMU_CMD_GET_CAPS 0xe8
75 #define EMU_CMD_GET_HW_VERSION 0xf0
77 /* bits return from EMU_CMD_GET_CAPS */
78 #define EMU_CAP_GET_HW_VERSION 1
79 #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
81 /* max speed 12MHz v5.0 jlink */
82 #define JLINK_MAX_SPEED 12000
84 /* Queue command functions */
85 static void jlink_end_state(tap_state_t state
);
86 static void jlink_state_move(void);
87 static void jlink_path_move(int num_states
, tap_state_t
*path
);
88 static void jlink_runtest(int num_cycles
);
89 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
90 static void jlink_reset(int trst
, int srst
);
91 static void jlink_simple_command(uint8_t command
);
92 static int jlink_get_status(void);
94 /* J-Link tap buffer functions */
95 static void jlink_tap_init(void);
96 static int jlink_tap_execute(void);
97 static void jlink_tap_ensure_space(int scans
, int bits
);
98 static void jlink_tap_append_step(int tms
, int tdi
);
99 static void jlink_tap_append_scan(int length
, uint8_t *buffer
, struct scan_command
*command
);
101 /* Jlink lowlevel functions */
103 struct usb_dev_handle
* usb_handle
;
106 static struct jlink
*jlink_usb_open(void);
107 static void jlink_usb_close(struct jlink
*jlink
);
108 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
);
109 static int jlink_usb_write(struct jlink
*jlink
, int out_length
);
110 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
);
111 static int jlink_usb_read_emu_result(struct jlink
*jlink
);
113 /* helper functions */
114 static int jlink_get_version_info(void);
116 #ifdef _DEBUG_USB_COMMS_
117 static void jlink_debug_buffer(uint8_t *buffer
, int length
);
120 static enum tap_state jlink_last_state
= TAP_RESET
;
122 static struct jlink
* jlink_handle
;
124 /***************************************************************************/
125 /* External interface implementation */
127 static void jlink_execute_runtest(struct jtag_command
*cmd
)
129 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
130 cmd
->cmd
.runtest
->num_cycles
,
131 cmd
->cmd
.runtest
->end_state
);
133 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
135 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
138 static void jlink_execute_statemove(struct jtag_command
*cmd
)
140 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
142 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
146 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
148 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
149 cmd
->cmd
.pathmove
->num_states
,
150 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
152 jlink_path_move(cmd
->cmd
.pathmove
->num_states
,
153 cmd
->cmd
.pathmove
->path
);
156 static void jlink_execute_scan(struct jtag_command
*cmd
)
162 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd
->cmd
.scan
->end_state
));
164 jlink_end_state(cmd
->cmd
.scan
->end_state
);
166 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
167 DEBUG_JTAG_IO("scan input, length = %d", scan_size
);
169 #ifdef _DEBUG_USB_COMMS_
170 jlink_debug_buffer(buffer
, (scan_size
+ 7) / 8);
172 type
= jtag_scan_type(cmd
->cmd
.scan
);
173 jlink_scan(cmd
->cmd
.scan
->ir_scan
,
174 type
, buffer
, scan_size
, cmd
->cmd
.scan
);
177 static void jlink_execute_reset(struct jtag_command
*cmd
)
179 DEBUG_JTAG_IO("reset trst: %i srst %i",
180 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
183 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
187 static void jlink_execute_sleep(struct jtag_command
*cmd
)
189 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
191 jtag_sleep(cmd
->cmd
.sleep
->us
);
194 static void jlink_execute_command(struct jtag_command
*cmd
)
198 case JTAG_RUNTEST
: jlink_execute_runtest(cmd
); break;
199 case JTAG_STATEMOVE
: jlink_execute_statemove(cmd
); break;
200 case JTAG_PATHMOVE
: jlink_execute_pathmove(cmd
); break;
201 case JTAG_SCAN
: jlink_execute_scan(cmd
); break;
202 case JTAG_RESET
: jlink_execute_reset(cmd
); break;
203 case JTAG_SLEEP
: jlink_execute_sleep(cmd
); break;
205 LOG_ERROR("BUG: unknown JTAG command type encountered");
210 static int jlink_execute_queue(void)
212 struct jtag_command
*cmd
= jtag_command_queue
;
216 jlink_execute_command(cmd
);
220 return jlink_tap_execute();
223 /* Sets speed in kHz. */
224 static int jlink_speed(int speed
)
228 if (speed
> JLINK_MAX_SPEED
)
230 LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
231 speed
, JLINK_MAX_SPEED
);
235 /* check for RTCK setting */
239 usb_out_buffer
[0] = EMU_CMD_SET_SPEED
;
240 usb_out_buffer
[1] = (speed
>> 0) & 0xff;
241 usb_out_buffer
[2] = (speed
>> 8) & 0xff;
243 result
= jlink_usb_write(jlink_handle
, 3);
246 LOG_ERROR("J-Link setting speed failed (%d)", result
);
247 return ERROR_JTAG_DEVICE_ERROR
;
253 static int jlink_speed_div(int speed
, int* khz
)
260 static int jlink_khz(int khz
, int *jtag_speed
)
267 static int jlink_init(void)
271 jlink_handle
= jlink_usb_open();
273 if (jlink_handle
== 0)
275 LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
276 return ERROR_JTAG_INIT_FAILED
;
280 * The next three instructions were added after discovering a problem while using an oscilloscope. For the V8
281 * SAM-ICE dongle (and likely other j-link device variants), the reset line to the target microprocessor was found to
282 * cycle only intermittently during emulator startup (even after encountering the downstream reset instruction later
283 * in the code). This was found to create two issues: 1) In general it is a bad practice to not reset a CPU to a known
284 * state when starting an emulator and 2) something critical happens inside the dongle when it does the first read
285 * following a new USB session. Keeping the processor in reset during the first read collecting version information
286 * seems to prevent errant "J-Link command EMU_CMD_VERSION failed" issues.
289 LOG_INFO("J-Link initialization started / target CPU reset initiated");
290 jlink_simple_command(EMU_CMD_HW_TRST0
);
291 jlink_simple_command(EMU_CMD_HW_RESET0
);
294 jlink_hw_jtag_version
= 2;
296 if (jlink_get_version_info() == ERROR_OK
)
298 /* attempt to get status */
302 LOG_INFO("J-Link JTAG Interface ready");
307 jlink_speed(jtag_get_speed());
309 /* v5/6 jlink seems to have an issue if the first tap move
310 * is not divisible by 8, so we send a TLR on first power up */
311 for (i
= 0; i
< 8; i
++) {
312 jlink_tap_append_step(1, 0);
319 static int jlink_quit(void)
321 jlink_usb_close(jlink_handle
);
325 /***************************************************************************/
326 /* Queue command implementations */
328 static void jlink_end_state(tap_state_t state
)
330 if (tap_is_state_stable(state
))
332 tap_set_end_state(state
);
336 LOG_ERROR("BUG: %i is not a valid end state", state
);
341 /* Goes to the end state. */
342 static void jlink_state_move(void)
346 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
347 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
349 for (i
= 0; i
< tms_scan_bits
; i
++)
351 tms
= (tms_scan
>> i
) & 1;
352 jlink_tap_append_step(tms
, 0);
355 tap_set_state(tap_get_end_state());
358 static void jlink_path_move(int num_states
, tap_state_t
*path
)
362 for (i
= 0; i
< num_states
; i
++)
364 if (path
[i
] == tap_state_transition(tap_get_state(), false))
366 jlink_tap_append_step(0, 0);
368 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
370 jlink_tap_append_step(1, 0);
374 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
378 tap_set_state(path
[i
]);
381 tap_set_end_state(tap_get_state());
384 static void jlink_runtest(int num_cycles
)
388 tap_state_t saved_end_state
= tap_get_end_state();
390 jlink_tap_ensure_space(1,num_cycles
+ 16);
392 /* only do a state_move when we're not already in IDLE */
393 if (tap_get_state() != TAP_IDLE
)
395 jlink_end_state(TAP_IDLE
);
400 /* execute num_cycles */
401 for (i
= 0; i
< num_cycles
; i
++)
403 jlink_tap_append_step(0, 0);
406 /* finish in end_state */
407 jlink_end_state(saved_end_state
);
408 if (tap_get_state() != tap_get_end_state())
414 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
, struct scan_command
*command
)
416 tap_state_t saved_end_state
;
418 jlink_tap_ensure_space(1, scan_size
+ 16);
420 saved_end_state
= tap_get_end_state();
422 /* Move to appropriate scan state */
423 jlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
425 /* Only move if we're not already there */
426 if (tap_get_state() != tap_get_end_state())
429 jlink_end_state(saved_end_state
);
432 jlink_tap_append_scan(scan_size
, buffer
, command
);
434 /* We are in Exit1, go to Pause */
435 jlink_tap_append_step(0, 0);
437 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
439 if (tap_get_state() != tap_get_end_state())
445 static void jlink_reset(int trst
, int srst
)
447 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
449 /* Signals are active low */
452 jlink_simple_command(EMU_CMD_HW_RESET1
);
456 jlink_simple_command(EMU_CMD_HW_RESET0
);
461 jlink_simple_command(EMU_CMD_HW_TRST0
);
466 jlink_simple_command(EMU_CMD_HW_TRST1
);
470 static void jlink_simple_command(uint8_t command
)
474 DEBUG_JTAG_IO("0x%02x", command
);
476 usb_out_buffer
[0] = command
;
477 result
= jlink_usb_write(jlink_handle
, 1);
481 LOG_ERROR("J-Link command 0x%02x failed (%d)", command
, result
);
485 static int jlink_get_status(void)
489 jlink_simple_command(EMU_CMD_GET_STATE
);
491 result
= jlink_usb_read(jlink_handle
, 8);
494 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result
);
495 return ERROR_JTAG_DEVICE_ERROR
;
498 int vref
= usb_in_buffer
[0] + (usb_in_buffer
[1] << 8);
499 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
500 vref
/ 1000, vref
% 1000, \
501 usb_in_buffer
[2], usb_in_buffer
[3], usb_in_buffer
[4], \
502 usb_in_buffer
[5], usb_in_buffer
[6], usb_in_buffer
[7]);
505 LOG_ERROR("Vref too low. Check Target Power\n");
510 static int jlink_get_version_info(void)
514 uint32_t jlink_caps
, jlink_max_size
;
516 /* query hardware version */
517 jlink_simple_command(EMU_CMD_VERSION
);
519 result
= jlink_usb_read(jlink_handle
, 2);
522 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result
);
523 return ERROR_JTAG_DEVICE_ERROR
;
526 len
= buf_get_u32(usb_in_buffer
, 0, 16);
527 if (len
> JLINK_IN_BUFFER_SIZE
)
529 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len
);
530 len
= JLINK_IN_BUFFER_SIZE
;
533 result
= jlink_usb_read(jlink_handle
, len
);
536 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result
);
537 return ERROR_JTAG_DEVICE_ERROR
;
540 usb_in_buffer
[result
] = 0;
541 LOG_INFO("%s", (char *)usb_in_buffer
);
543 /* query hardware capabilities */
544 jlink_simple_command(EMU_CMD_GET_CAPS
);
546 result
= jlink_usb_read(jlink_handle
, 4);
549 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)\n", result
);
550 return ERROR_JTAG_DEVICE_ERROR
;
553 jlink_caps
= buf_get_u32(usb_in_buffer
, 0, 32);
554 LOG_INFO("JLink caps 0x%x", (unsigned)jlink_caps
);
556 if (jlink_caps
& (1 << EMU_CAP_GET_HW_VERSION
))
558 /* query hardware version */
559 jlink_simple_command(EMU_CMD_GET_HW_VERSION
);
561 result
= jlink_usb_read(jlink_handle
, 4);
564 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)\n", result
);
565 return ERROR_JTAG_DEVICE_ERROR
;
568 uint32_t jlink_hw_version
= buf_get_u32(usb_in_buffer
, 0, 32);
569 uint32_t major_revision
= (jlink_hw_version
/ 10000) % 100;
570 if (major_revision
>= 5)
571 jlink_hw_jtag_version
= 3;
573 LOG_INFO("JLink hw version %i", (int)jlink_hw_version
);
576 if (jlink_caps
& (1 << EMU_CAP_GET_MAX_BLOCK_SIZE
))
578 /* query hardware maximum memory block */
579 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK
);
581 result
= jlink_usb_read(jlink_handle
, 4);
584 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)\n", result
);
585 return ERROR_JTAG_DEVICE_ERROR
;
588 jlink_max_size
= buf_get_u32(usb_in_buffer
, 0, 32);
589 LOG_INFO("JLink max mem block %i", (int)jlink_max_size
);
595 COMMAND_HANDLER(jlink_handle_jlink_info_command
)
597 if (jlink_get_version_info() == ERROR_OK
)
599 /* attempt to get status */
606 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command
)
610 command_print(CMD_CTX
, "jlink hw jtag %i", jlink_hw_jtag_version
);
613 int request_version
= atoi(CMD_ARGV
[0]);
614 switch (request_version
) {
616 jlink_hw_jtag_version
= request_version
;
619 return ERROR_COMMAND_SYNTAX_ERROR
;
624 return ERROR_COMMAND_SYNTAX_ERROR
;
630 static const struct command_registration jlink_command_handlers
[] = {
632 .name
= "jlink_info",
633 .handler
= &jlink_handle_jlink_info_command
,
634 .mode
= COMMAND_EXEC
,
635 .help
= "show jlink info",
638 .name
= "jlink_hw_jtag",
639 .handler
= &jlink_handle_jlink_hw_jtag_command
,
640 .mode
= COMMAND_EXEC
,
641 .help
= "access J-Link HW JTAG command version",
644 COMMAND_REGISTRATION_DONE
647 struct jtag_interface jlink_interface
= {
649 .commands
= jlink_command_handlers
,
651 .execute_queue
= jlink_execute_queue
,
652 .speed
= jlink_speed
,
653 .speed_div
= jlink_speed_div
,
659 /***************************************************************************/
660 /* J-Link tap functions */
663 static unsigned tap_length
= 0;
664 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
665 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
666 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
668 struct pending_scan_result
{
669 int first
; /* First bit position in tdo_buffer to read */
670 int length
; /* Number of bits to read */
671 struct scan_command
*command
; /* Corresponding scan command */
675 #define MAX_PENDING_SCAN_RESULTS 256
677 static int pending_scan_results_length
;
678 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
680 static void jlink_tap_init(void)
683 pending_scan_results_length
= 0;
686 static void jlink_tap_ensure_space(int scans
, int bits
)
688 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
689 int available_bits
= JLINK_TAP_BUFFER_SIZE
* 8 - tap_length
- 32;
691 if (scans
> available_scans
|| bits
> available_bits
)
697 static void jlink_tap_append_step(int tms
, int tdi
)
699 int index
= tap_length
/ 8;
701 if (index
>= JLINK_TAP_BUFFER_SIZE
)
703 LOG_ERROR("jlink_tap_append_step: overflow");
704 *(uint32_t *)0xFFFFFFFF = 0;
708 int bit_index
= tap_length
% 8;
709 uint8_t bit
= 1 << bit_index
;
711 // we do not pad TMS, so be sure to initialize all bits
714 tms_buffer
[index
] = tdi_buffer
[index
] = 0;
718 tms_buffer
[index
] |= bit
;
720 tms_buffer
[index
] &= ~bit
;
723 tdi_buffer
[index
] |= bit
;
725 tdi_buffer
[index
] &= ~bit
;
730 static void jlink_tap_append_scan(int length
, uint8_t *buffer
, struct scan_command
*command
)
732 struct pending_scan_result
*pending_scan_result
=
733 &pending_scan_results_buffer
[pending_scan_results_length
];
736 pending_scan_result
->first
= tap_length
;
737 pending_scan_result
->length
= length
;
738 pending_scan_result
->command
= command
;
739 pending_scan_result
->buffer
= buffer
;
741 for (i
= 0; i
< length
; i
++)
743 int tms
= (i
< (length
- 1)) ? 0 : 1;
744 int tdi
= (buffer
[i
/ 8] & (1 << (i
% 8))) != 0;
745 jlink_tap_append_step(tms
, tdi
);
747 pending_scan_results_length
++;
750 /* Pad and send a tap sequence to the device, and receive the answer.
751 * For the purpose of padding we assume that we are in idle or pause state. */
752 static int jlink_tap_execute(void)
761 /* JLink returns an extra NULL in packet when size of incoming
762 * message is a multiple of 64, creates problems with USB comms.
763 * WARNING: This will interfere with tap state counting. */
764 while ((DIV_ROUND_UP(tap_length
, 8) % 64) == 0)
766 jlink_tap_append_step((tap_get_state() == TAP_RESET
)?1:0, 0);
769 // number of full bytes (plus one if some would be left over)
770 byte_length
= DIV_ROUND_UP(tap_length
, 8);
772 bool use_jtag3
= jlink_hw_jtag_version
>= 3;
773 usb_out_buffer
[0] = use_jtag3
? EMU_CMD_HW_JTAG3
: EMU_CMD_HW_JTAG2
;
774 usb_out_buffer
[1] = 0;
775 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
776 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
777 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
778 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
780 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
781 tap_length
, jlink_last_state
);
783 result
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
, byte_length
);
784 if (result
!= byte_length
)
786 LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result
, byte_length
);
788 return ERROR_JTAG_QUEUE_FAILED
;
791 memcpy(tdo_buffer
, usb_in_buffer
, byte_length
);
793 for (i
= 0; i
< pending_scan_results_length
; i
++)
795 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
796 uint8_t *buffer
= pending_scan_result
->buffer
;
797 int length
= pending_scan_result
->length
;
798 int first
= pending_scan_result
->first
;
799 struct scan_command
*command
= pending_scan_result
->command
;
802 buf_set_buf(tdo_buffer
, first
, buffer
, 0, length
);
804 DEBUG_JTAG_IO("pending scan result, length = %d", length
);
806 #ifdef _DEBUG_USB_COMMS_
807 jlink_debug_buffer(buffer
, DIV_ROUND_UP(length
, 8));
810 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
813 return ERROR_JTAG_QUEUE_FAILED
;
816 if (pending_scan_result
->buffer
!= NULL
)
818 free(pending_scan_result
->buffer
);
826 /*****************************************************************************/
827 /* JLink USB low-level functions */
829 static struct jlink
* jlink_usb_open()
833 const uint16_t vids
[] = { VID
, 0 };
834 const uint16_t pids
[] = { PID
, 0 };
835 struct usb_dev_handle
*dev
;
836 if (jtag_usb_open(vids
, pids
, &dev
) != ERROR_OK
)
839 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
840 * AREA!!!!!!!!!!! The behavior of libusb is not completely
841 * consistent across Windows, Linux, and Mac OS X platforms.
842 * The actions taken in the following compiler conditionals may
843 * not agree with published documentation for libusb, but were
844 * found to be necessary through trials and tribulations. Even
845 * little tweaks can break one or more platforms, so if you do
846 * make changes test them carefully on all platforms before
857 /* reopen jlink after usb_reset
858 * on win32 this may take a second or two to re-enumerate */
860 while ((retval
= jtag_usb_open(vids
, pids
, &dev
)) != ERROR_OK
)
868 if (ERROR_OK
!= retval
)
874 /* usb_set_configuration required under win32 */
875 struct usb_device
*udev
= usb_device(dev
);
876 usb_set_configuration(dev
, udev
->config
[0].bConfigurationValue
);
877 usb_claim_interface(dev
, 0);
881 * This makes problems under Mac OS X. And is not needed
882 * under Windows. Hopefully this will not break a linux build
884 usb_set_altinterface(result
->usb_handle
, 0);
886 struct usb_interface
*iface
= udev
->config
->interface
;
887 struct usb_interface_descriptor
*desc
= iface
->altsetting
;
888 for (int i
= 0; i
< desc
->bNumEndpoints
; i
++)
890 uint8_t epnum
= desc
->endpoint
[i
].bEndpointAddress
;
891 bool is_input
= epnum
& 0x80;
892 LOG_DEBUG("usb ep %s %02x", is_input
? "in" : "out", epnum
);
894 jlink_read_ep
= epnum
;
896 jlink_write_ep
= epnum
;
899 struct jlink
*result
= malloc(sizeof(struct jlink
));
900 result
->usb_handle
= dev
;
904 static void jlink_usb_close(struct jlink
*jlink
)
906 usb_close(jlink
->usb_handle
);
910 /* Send a message and receive the reply. */
911 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
)
915 result
= jlink_usb_write(jlink
, out_length
);
916 if (result
!= out_length
)
918 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
920 return ERROR_JTAG_DEVICE_ERROR
;
923 result
= jlink_usb_read(jlink
, in_length
);
924 if ((result
!= in_length
) && (result
!= (in_length
+ 1)))
926 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
928 return ERROR_JTAG_DEVICE_ERROR
;
931 if (jlink_hw_jtag_version
< 3)
934 int result2
= ERROR_OK
;
935 if (result
== in_length
)
937 /* Must read the result from the EMU too */
938 result2
= jlink_usb_read_emu_result(jlink
);
941 LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, result=%d, in_length=%i", result2
,in_length
);
942 /* Try again once, should only happen if (in_length%64 == 0) */
943 result2
= jlink_usb_read_emu_result(jlink
);
946 LOG_ERROR("jlink_usb_read_emu_result failed "
947 "(requested = 1, result=%d)", result2
);
948 return ERROR_JTAG_DEVICE_ERROR
;
952 /* Check the result itself */
953 result2
= usb_emu_result_buffer
[0];
957 /* Save the result, then remove it from return value */
958 result2
= usb_in_buffer
[result
--];
963 LOG_ERROR("jlink_usb_message failed with result=%d)", result2
);
964 return ERROR_JTAG_DEVICE_ERROR
;
970 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
971 static int usb_bulk_with_retries(
972 int (*f
)(usb_dev_handle
*, int, char *, int, int),
973 usb_dev_handle
*dev
, int ep
,
974 char *bytes
, int size
, int timeout
)
976 int tries
= 3, count
= 0;
978 while (tries
&& (count
< size
))
980 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
983 else if ((-ETIMEDOUT
!= result
) || !--tries
)
989 static int wrap_usb_bulk_write(usb_dev_handle
*dev
, int ep
,
990 char *buff
, int size
, int timeout
)
992 /* usb_bulk_write() takes const char *buff */
993 return usb_bulk_write(dev
, ep
, buff
, size
, timeout
);
996 static inline int usb_bulk_write_ex(usb_dev_handle
*dev
, int ep
,
997 char *bytes
, int size
, int timeout
)
999 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
1000 dev
, ep
, bytes
, size
, timeout
);
1003 static inline int usb_bulk_read_ex(usb_dev_handle
*dev
, int ep
,
1004 char *bytes
, int size
, int timeout
)
1006 return usb_bulk_with_retries(&usb_bulk_read
,
1007 dev
, ep
, bytes
, size
, timeout
);
1010 /* Write data from out_buffer to USB. */
1011 static int jlink_usb_write(struct jlink
*jlink
, int out_length
)
1015 if (out_length
> JLINK_OUT_BUFFER_SIZE
)
1017 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)", out_length
, JLINK_OUT_BUFFER_SIZE
);
1021 result
= usb_bulk_write_ex(jlink
->usb_handle
, jlink_write_ep
,
1022 (char *)usb_out_buffer
, out_length
, JLINK_USB_TIMEOUT
);
1024 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length
, result
);
1026 #ifdef _DEBUG_USB_COMMS_
1027 jlink_debug_buffer(usb_out_buffer
, out_length
);
1032 /* Read data from USB into in_buffer. */
1033 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
)
1035 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1036 (char *)usb_in_buffer
, expected_size
, JLINK_USB_TIMEOUT
);
1038 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result
);
1040 #ifdef _DEBUG_USB_COMMS_
1041 jlink_debug_buffer(usb_in_buffer
, result
);
1046 /* Read the result from the previous EMU cmd into result_buffer. */
1047 static int jlink_usb_read_emu_result(struct jlink
*jlink
)
1049 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1050 (char *)usb_emu_result_buffer
, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
1053 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result
);
1055 #ifdef _DEBUG_USB_COMMS_
1056 jlink_debug_buffer(usb_emu_result_buffer
, result
);
1061 #ifdef _DEBUG_USB_COMMS_
1062 #define BYTES_PER_LINE 16
1064 static void jlink_debug_buffer(uint8_t *buffer
, int length
)
1071 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
)
1073 snprintf(line
, 5, "%04x", i
);
1074 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++)
1076 snprintf(s
, 4, " %02x", buffer
[j
]);
1079 LOG_DEBUG("%s", line
);