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 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
9 * plagnioj@jcrosoft.com *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
31 #include <jtag/interface.h>
32 #include <jtag/commands.h>
33 #include "libusb_common.h"
35 /* See Segger's public documentation:
36 * Reference manual for J-Link USB Protocol
37 * Document RM08001-R6 Date: June 16, 2009
38 * (Or newer, with some SWD information).
39 * http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
43 * The default pid of the segger is 0x0101
44 * But when you change the USB Address it will also
46 * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address)
49 #define VID 0x1366, 0x1366, 0x1366, 0x1366
50 #define PID 0x0101, 0x0102, 0x0103, 0x0104
52 #define JLINK_WRITE_ENDPOINT 0x02
53 #define JLINK_READ_ENDPOINT 0x81
55 static unsigned int jlink_write_ep
= JLINK_WRITE_ENDPOINT
;
56 static unsigned int jlink_read_ep
= JLINK_READ_ENDPOINT
;
57 static unsigned int jlink_hw_jtag_version
= 2;
59 #define JLINK_USB_TIMEOUT 1000
61 /* See Section 3.3.2 of the Segger JLink USB protocol manual */
62 /* 2048 is the max value we can use here */
63 #define JLINK_TAP_BUFFER_SIZE 2048
64 /*#define JLINK_TAP_BUFFER_SIZE 256*/
65 /*#define JLINK_TAP_BUFFER_SIZE 384*/
67 #define JLINK_IN_BUFFER_SIZE (2048 + 1)
68 #define JLINK_OUT_BUFFER_SIZE (2*2048 + 4)
70 /* Global USB buffers */
71 static uint8_t usb_in_buffer
[JLINK_IN_BUFFER_SIZE
];
72 static uint8_t usb_out_buffer
[JLINK_OUT_BUFFER_SIZE
];
74 /* Constants for JLink command */
75 #define EMU_CMD_VERSION 0x01
76 #define EMU_CMD_RESET_TRST 0x02
77 #define EMU_CMD_RESET_TARGET 0x03
78 #define EMU_CMD_SET_SPEED 0x05
79 #define EMU_CMD_GET_STATE 0x07
80 #define EMU_CMD_SET_KS_POWER 0x08
81 #define EMU_CMD_GET_SPEEDS 0xc0
82 #define EMU_CMD_GET_HW_INFO 0xc1
83 #define EMU_CMD_GET_COUNTERS 0xc2
84 #define EMU_CMD_SELECT_IF 0xc7
85 #define EMU_CMD_HW_CLOCK 0xc8
86 #define EMU_CMD_HW_TMS0 0xc9
87 #define EMU_CMD_HW_TMS1 0xca
88 #define EMU_CMD_HW_DATA0 0xcb
89 #define EMU_CMD_HW_DATA1 0xcc
90 #define EMU_CMD_HW_JTAG 0xcd
91 #define EMU_CMD_HW_JTAG2 0xce
92 #define EMU_CMD_HW_JTAG3 0xcf
93 #define EMU_CMD_HW_RELEASE_RESET_STOP_EX 0xd0
94 #define EMU_CMD_HW_RELEASE_RESET_STOP_TIMED 0xd1
95 #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
96 #define EMU_CMD_HW_JTAG_WRITE 0xd5
97 #define EMU_CMD_HW_JTAG_GET_RESULT 0xd6
98 #define EMU_CMD_HW_RESET0 0xdc
99 #define EMU_CMD_HW_RESET1 0xdd
100 #define EMU_CMD_HW_TRST0 0xde
101 #define EMU_CMD_HW_TRST1 0xdf
102 #define EMU_CMD_GET_CAPS 0xe8
103 #define EMU_CMD_GET_CPU_CAPS 0xe9
104 #define EMU_CMD_EXEC_CPU_CMD 0xea
105 #define EMU_CMD_GET_CAPS_EX 0xed
106 #define EMU_CMD_GET_HW_VERSION 0xf0
107 #define EMU_CMD_WRITE_DCC 0xf1
108 #define EMU_CMD_READ_CONFIG 0xf2
109 #define EMU_CMD_WRITE_CONFIG 0xf3
110 #define EMU_CMD_WRITE_MEM 0xf4
111 #define EMU_CMD_READ_MEM 0xf5
112 #define EMU_CMD_MEASURE_RTCK_REACT 0xf6
113 #define EMU_CMD_WRITE_MEM_ARM79 0xf7
114 #define EMU_CMD_READ_MEM_ARM79 0xf8
116 /* bits return from EMU_CMD_GET_CAPS */
117 #define EMU_CAP_RESERVED_1 0
118 #define EMU_CAP_GET_HW_VERSION 1
119 #define EMU_CAP_WRITE_DCC 2
120 #define EMU_CAP_ADAPTIVE_CLOCKING 3
121 #define EMU_CAP_READ_CONFIG 4
122 #define EMU_CAP_WRITE_CONFIG 5
123 #define EMU_CAP_TRACE 6
124 #define EMU_CAP_WRITE_MEM 7
125 #define EMU_CAP_READ_MEM 8
126 #define EMU_CAP_SPEED_INFO 9
127 #define EMU_CAP_EXEC_CODE 10
128 #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
129 #define EMU_CAP_GET_HW_INFO 12
130 #define EMU_CAP_SET_KS_POWER 13
131 #define EMU_CAP_RESET_STOP_TIMED 14
132 #define EMU_CAP_RESERVED_2 15
133 #define EMU_CAP_MEASURE_RTCK_REACT 16
134 #define EMU_CAP_SELECT_IF 17
135 #define EMU_CAP_RW_MEM_ARM79 18
136 #define EMU_CAP_GET_COUNTERS 19
137 #define EMU_CAP_READ_DCC 20
138 #define EMU_CAP_GET_CPU_CAPS 21
139 #define EMU_CAP_EXEC_CPU_CMD 22
140 #define EMU_CAP_SWO 23
141 #define EMU_CAP_WRITE_DCC_EX 24
142 #define EMU_CAP_UPDATE_FIRMWARE_EX 25
143 #define EMU_CAP_FILE_IO 26
144 #define EMU_CAP_REGISTER 27
145 #define EMU_CAP_INDICATORS 28
146 #define EMU_CAP_TEST_NET_SPEED 29
147 #define EMU_CAP_RAWTRACE 30
148 #define EMU_CAP_RESERVED_3 31
150 static char *jlink_cap_str
[] = {
152 "Supports command EMU_CMD_GET_HARDWARE_VERSION",
153 "Supports command EMU_CMD_WRITE_DCC",
154 "Supports adaptive clocking",
155 "Supports command EMU_CMD_READ_CONFIG",
156 "Supports command EMU_CMD_WRITE_CONFIG",
157 "Supports trace commands",
158 "Supports command EMU_CMD_WRITE_MEM",
159 "Supports command EMU_CMD_READ_MEM",
160 "Supports command EMU_CMD_GET_SPEED",
161 "Supports command EMU_CMD_CODE_...",
162 "Supports command EMU_CMD_GET_MAX_BLOCK_SIZE",
163 "Supports command EMU_CMD_GET_HW_INFO",
164 "Supports command EMU_CMD_SET_KS_POWER",
165 "Supports command EMU_CMD_HW_RELEASE_RESET_STOP_TIMED",
167 "Supports command EMU_CMD_MEASURE_RTCK_REACT",
168 "Supports command EMU_CMD_HW_SELECT_IF",
169 "Supports command EMU_CMD_READ/WRITE_MEM_ARM79",
170 "Supports command EMU_CMD_GET_COUNTERS",
171 "Supports command EMU_CMD_READ_DCC",
172 "Supports command EMU_CMD_GET_CPU_CAPS",
173 "Supports command EMU_CMD_EXEC_CPU_CMD",
174 "Supports command EMU_CMD_SWO",
175 "Supports command EMU_CMD_WRITE_DCC_EX",
176 "Supports command EMU_CMD_UPDATE_FIRMWARE_EX",
177 "Supports command EMU_CMD_FILE_IO",
178 "Supports command EMU_CMD_REGISTER",
179 "Supports command EMU_CMD_INDICATORS",
180 "Supports command EMU_CMD_TEST_NET_SPEED",
181 "Supports command EMU_CMD_RAWTRACE",
185 /* max speed 12MHz v5.0 jlink */
186 #define JLINK_MAX_SPEED 12000
188 /* J-Link hardware versions */
189 #define JLINK_HW_TYPE_JLINK 0
190 #define JLINK_HW_TYPE_JTRACE 1
191 #define JLINK_HW_TYPE_FLASHER 2
192 #define JLINK_HW_TYPE_JLINK_PRO 3
193 #define JLINK_HW_TYPE_MAX 4
195 static char *jlink_hw_type_str
[] = {
202 /* Queue command functions */
203 static void jlink_end_state(tap_state_t state
);
204 static void jlink_state_move(void);
205 static void jlink_path_move(int num_states
, tap_state_t
*path
);
206 static void jlink_runtest(int num_cycles
);
207 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
208 int scan_size
, struct scan_command
*command
);
209 static void jlink_reset(int trst
, int srst
);
210 static void jlink_simple_command(uint8_t command
);
211 static int jlink_get_status(void);
213 /* J-Link tap buffer functions */
214 static void jlink_tap_init(void);
215 static int jlink_tap_execute(void);
216 static void jlink_tap_ensure_space(int scans
, int bits
);
217 static void jlink_tap_append_step(int tms
, int tdi
);
218 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
219 struct scan_command
*command
);
221 /* Jlink lowlevel functions */
223 struct jtag_libusb_device_handle
*usb_handle
;
226 static struct jlink
*jlink_usb_open(void);
227 static void jlink_usb_close(struct jlink
*jlink
);
228 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
);
229 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
);
230 static int jlink_usb_write(struct jlink
*jlink
, int out_length
);
231 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
);
233 /* helper functions */
234 static int jlink_get_version_info(void);
236 #ifdef _DEBUG_USB_COMMS_
237 static void jlink_debug_buffer(uint8_t *buffer
, int length
);
239 static inline void jlink_debug_buffer(uint8_t *buffer
, int length
)
244 static enum tap_state jlink_last_state
= TAP_RESET
;
246 static struct jlink
*jlink_handle
;
248 /* pid could be specified at runtime */
249 static uint16_t vids
[] = { VID
, 0 };
250 static uint16_t pids
[] = { PID
, 0 };
252 static uint32_t jlink_caps
;
253 static uint32_t jlink_hw_type
;
255 /* 256 byte non-volatile memory */
256 struct jlink_config
{
258 /* 0ffset 0x01 to 0x03 */
259 uint8_t reserved_1
[3];
260 uint32_t kickstart_power_on_jtag_pin_19
;
261 /* 0ffset 0x08 to 0x1f */
262 uint8_t reserved_2
[24];
263 /* IP only for J-Link Pro */
264 uint8_t ip_address
[4];
265 uint8_t subnet_mask
[4];
266 /* 0ffset 0x28 to 0x2f */
267 uint8_t reserved_3
[8];
268 uint8_t mac_address
[6];
269 /* 0ffset 0x36 to 0xff */
270 uint8_t reserved_4
[202];
271 } __attribute__ ((packed
));
272 struct jlink_config jlink_cfg
;
274 /***************************************************************************/
275 /* External interface implementation */
277 static void jlink_execute_runtest(struct jtag_command
*cmd
)
279 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
280 cmd
->cmd
.runtest
->num_cycles
,
281 cmd
->cmd
.runtest
->end_state
);
283 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
285 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
288 static void jlink_execute_statemove(struct jtag_command
*cmd
)
290 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
292 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
296 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
298 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
299 cmd
->cmd
.pathmove
->num_states
,
300 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
302 jlink_path_move(cmd
->cmd
.pathmove
->num_states
,
303 cmd
->cmd
.pathmove
->path
);
306 static void jlink_execute_scan(struct jtag_command
*cmd
)
312 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd
->cmd
.scan
->end_state
));
314 jlink_end_state(cmd
->cmd
.scan
->end_state
);
316 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
317 DEBUG_JTAG_IO("scan input, length = %d", scan_size
);
319 jlink_debug_buffer(buffer
, (scan_size
+ 7) / 8);
320 type
= jtag_scan_type(cmd
->cmd
.scan
);
321 jlink_scan(cmd
->cmd
.scan
->ir_scan
,
322 type
, buffer
, scan_size
, cmd
->cmd
.scan
);
325 static void jlink_execute_reset(struct jtag_command
*cmd
)
327 DEBUG_JTAG_IO("reset trst: %i srst %i",
328 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
331 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
335 static void jlink_execute_sleep(struct jtag_command
*cmd
)
337 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
339 jtag_sleep(cmd
->cmd
.sleep
->us
);
342 static void jlink_execute_command(struct jtag_command
*cmd
)
346 jlink_execute_runtest(cmd
);
349 jlink_execute_statemove(cmd
);
352 jlink_execute_pathmove(cmd
);
355 jlink_execute_scan(cmd
);
358 jlink_execute_reset(cmd
);
361 jlink_execute_sleep(cmd
);
364 LOG_ERROR("BUG: unknown JTAG command type encountered");
369 static int jlink_execute_queue(void)
371 struct jtag_command
*cmd
= jtag_command_queue
;
373 while (cmd
!= NULL
) {
374 jlink_execute_command(cmd
);
378 return jlink_tap_execute();
381 /* Sets speed in kHz. */
382 static int jlink_speed(int speed
)
386 if (speed
> JLINK_MAX_SPEED
) {
387 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
388 speed
, JLINK_MAX_SPEED
);
389 speed
= JLINK_MAX_SPEED
;
392 /* check for RTCK setting */
396 usb_out_buffer
[0] = EMU_CMD_SET_SPEED
;
397 usb_out_buffer
[1] = (speed
>> 0) & 0xff;
398 usb_out_buffer
[2] = (speed
>> 8) & 0xff;
400 result
= jlink_usb_write(jlink_handle
, 3);
402 LOG_ERROR("J-Link setting speed failed (%d)", result
);
403 return ERROR_JTAG_DEVICE_ERROR
;
409 static int jlink_speed_div(int speed
, int *khz
)
416 static int jlink_khz(int khz
, int *jtag_speed
)
424 * select transport interface
426 * @param iface [0..31] currently: 0=JTAG, 1=SWD
427 * @returns ERROR_OK or ERROR_ code
429 * @pre jlink_handle must be opened
430 * @pre function may be called only for devices, that have
431 * EMU_CAP_SELECT_IF capability enabled
433 static int jlink_select_interface(int iface
)
435 /* According to Segger's document RM08001-R7 Date: October 8, 2010,
436 * http://www.segger.com/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
437 * section 5.5.3 EMU_CMD_SELECT_IF
438 * > SubCmd 1..31 to select interface (0..31)
440 * The table below states:
444 * This obviosly means that to select TIF_JTAG one should write SubCmd = 1.
446 * In fact, JTAG interface operates when SubCmd=0
448 * It looks like a typo in documentation, because interfaces 0..31 could not
449 * be selected by 1..31 range command.
451 assert(iface
>= 0 && iface
< 32);
454 /* get available interfaces */
455 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
456 usb_out_buffer
[1] = 0xff;
458 result
= jlink_usb_io(jlink_handle
, 2, 4);
459 if (result
!= ERROR_OK
) {
460 LOG_ERROR("J-Link query interface failed (%d)", result
);
461 return ERROR_JTAG_DEVICE_ERROR
;
464 uint32_t iface_mask
= buf_get_u32(usb_in_buffer
, 0, 32);
466 if (!(iface_mask
& (1<<iface
))) {
467 LOG_ERROR("J-Link requesting to select unsupported interface (%" PRIx32
")", iface_mask
);
468 return ERROR_JTAG_DEVICE_ERROR
;
471 /* Select interface */
472 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
473 usb_out_buffer
[1] = iface
;
475 result
= jlink_usb_io(jlink_handle
, 2, 4);
476 if (result
!= ERROR_OK
) {
477 LOG_ERROR("J-Link interface select failed (%d)", result
);
478 return ERROR_JTAG_DEVICE_ERROR
;
484 static int jlink_init(void)
488 jlink_handle
= jlink_usb_open();
490 if (jlink_handle
== 0) {
491 LOG_ERROR("Cannot find jlink Interface! Please check "
492 "connection and permissions.");
493 return ERROR_JTAG_INIT_FAILED
;
497 * The next three instructions were added after discovering a problem
498 * while using an oscilloscope.
499 * For the V8 SAM-ICE dongle (and likely other j-link device variants),
500 * the reset line to the target microprocessor was found to cycle only
501 * intermittently during emulator startup (even after encountering the
502 * downstream reset instruction later in the code).
503 * This was found to create two issues:
504 * 1) In general it is a bad practice to not reset a CPU to a known
505 * state when starting an emulator and
506 * 2) something critical happens inside the dongle when it does the
507 * first read following a new USB session.
508 * Keeping the processor in reset during the first read collecting
509 * version information seems to prevent errant
510 * "J-Link command EMU_CMD_VERSION failed" issues.
513 LOG_INFO("J-Link initialization started / target CPU reset initiated");
514 jlink_simple_command(EMU_CMD_HW_TRST0
);
515 jlink_simple_command(EMU_CMD_HW_RESET0
);
518 jlink_hw_jtag_version
= 2;
520 if (jlink_get_version_info() == ERROR_OK
) {
521 /* attempt to get status */
526 * Some versions of Segger's software do not select JTAG interface by default.
528 * Segger recommends to select interface necessarily as a part of init process,
529 * in case any previous session leaves improper interface selected.
531 * Until SWD implemented, select only JTAG interface here.
533 if (jlink_caps
& (1<<EMU_CAP_SELECT_IF
))
534 jlink_select_interface(0);
536 LOG_INFO("J-Link JTAG Interface ready");
542 /* v5/6 jlink seems to have an issue if the first tap move
543 * is not divisible by 8, so we send a TLR on first power up */
544 for (i
= 0; i
< 8; i
++)
545 jlink_tap_append_step(1, 0);
551 static int jlink_quit(void)
553 jlink_usb_close(jlink_handle
);
557 /***************************************************************************/
558 /* Queue command implementations */
560 static void jlink_end_state(tap_state_t state
)
562 if (tap_is_state_stable(state
))
563 tap_set_end_state(state
);
565 LOG_ERROR("BUG: %i is not a valid end state", state
);
570 /* Goes to the end state. */
571 static void jlink_state_move(void)
575 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
576 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
578 for (i
= 0; i
< tms_scan_bits
; i
++) {
579 tms
= (tms_scan
>> i
) & 1;
580 jlink_tap_append_step(tms
, 0);
583 tap_set_state(tap_get_end_state());
586 static void jlink_path_move(int num_states
, tap_state_t
*path
)
590 for (i
= 0; i
< num_states
; i
++) {
591 if (path
[i
] == tap_state_transition(tap_get_state(), false))
592 jlink_tap_append_step(0, 0);
593 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
594 jlink_tap_append_step(1, 0);
596 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
597 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
601 tap_set_state(path
[i
]);
604 tap_set_end_state(tap_get_state());
607 static void jlink_runtest(int num_cycles
)
611 tap_state_t saved_end_state
= tap_get_end_state();
613 jlink_tap_ensure_space(1, num_cycles
+ 16);
615 /* only do a state_move when we're not already in IDLE */
616 if (tap_get_state() != TAP_IDLE
) {
617 jlink_end_state(TAP_IDLE
);
622 /* execute num_cycles */
623 for (i
= 0; i
< num_cycles
; i
++)
624 jlink_tap_append_step(0, 0);
626 /* finish in end_state */
627 jlink_end_state(saved_end_state
);
628 if (tap_get_state() != tap_get_end_state())
632 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
633 int scan_size
, struct scan_command
*command
)
635 tap_state_t saved_end_state
;
637 jlink_tap_ensure_space(1, scan_size
+ 16);
639 saved_end_state
= tap_get_end_state();
641 /* Move to appropriate scan state */
642 jlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
644 /* Only move if we're not already there */
645 if (tap_get_state() != tap_get_end_state())
648 jlink_end_state(saved_end_state
);
651 jlink_tap_append_scan(scan_size
, buffer
, command
);
653 /* We are in Exit1, go to Pause */
654 jlink_tap_append_step(0, 0);
656 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
658 if (tap_get_state() != tap_get_end_state())
662 static void jlink_reset(int trst
, int srst
)
664 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
666 /* Signals are active low */
668 jlink_simple_command(EMU_CMD_HW_RESET1
);
671 jlink_simple_command(EMU_CMD_HW_RESET0
);
674 jlink_simple_command(EMU_CMD_HW_TRST0
);
677 jlink_simple_command(EMU_CMD_HW_TRST1
);
680 static void jlink_simple_command(uint8_t command
)
684 DEBUG_JTAG_IO("0x%02x", command
);
686 usb_out_buffer
[0] = command
;
687 result
= jlink_usb_write(jlink_handle
, 1);
690 LOG_ERROR("J-Link command 0x%02x failed (%d)", command
, result
);
693 static int jlink_get_status(void)
697 jlink_simple_command(EMU_CMD_GET_STATE
);
699 result
= jlink_usb_read(jlink_handle
, 8);
701 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result
);
702 return ERROR_JTAG_DEVICE_ERROR
;
705 int vref
= usb_in_buffer
[0] + (usb_in_buffer
[1] << 8);
706 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
707 vref
/ 1000, vref
% 1000, \
708 usb_in_buffer
[2], usb_in_buffer
[3], usb_in_buffer
[4], \
709 usb_in_buffer
[5], usb_in_buffer
[6], usb_in_buffer
[7]);
712 LOG_ERROR("Vref too low. Check Target Power");
717 #define jlink_dump_printf(context, expr ...) \
720 command_print(context, expr); \
725 static void jlink_caps_dump(struct command_context
*ctx
)
729 jlink_dump_printf(ctx
, "J-Link Capabilities");
731 for (i
= 1; i
< 31; i
++)
732 if (jlink_caps
& (1 << i
))
733 jlink_dump_printf(ctx
, "%s", jlink_cap_str
[i
]);
736 static void jlink_config_usb_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
741 jlink_dump_printf(ctx
, "USB-Address: 0x%x", cfg
->usb_address
);
744 static void jlink_config_kickstart_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
749 jlink_dump_printf(ctx
, "Kickstart power on JTAG-pin 19: 0x%" PRIx32
,
750 cfg
->kickstart_power_on_jtag_pin_19
);
753 static void jlink_config_mac_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
758 jlink_dump_printf(ctx
, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
759 cfg
->mac_address
[5], cfg
->mac_address
[4],
760 cfg
->mac_address
[3], cfg
->mac_address
[2],
761 cfg
->mac_address
[1], cfg
->mac_address
[0]);
764 static void jlink_config_ip_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
769 jlink_dump_printf(ctx
, "IP Address: %d.%d.%d.%d",
770 cfg
->ip_address
[3], cfg
->ip_address
[2],
771 cfg
->ip_address
[1], cfg
->ip_address
[0]);
772 jlink_dump_printf(ctx
, "Subnet Mask: %d.%d.%d.%d",
773 cfg
->subnet_mask
[3], cfg
->subnet_mask
[2],
774 cfg
->subnet_mask
[1], cfg
->subnet_mask
[0]);
777 static void jlink_config_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
782 jlink_dump_printf(ctx
, "J-Link configuration");
783 jlink_config_usb_address_dump(ctx
, cfg
);
784 jlink_config_kickstart_dump(ctx
, cfg
);
786 if (jlink_hw_type
== JLINK_HW_TYPE_JLINK_PRO
) {
787 jlink_config_ip_dump(ctx
, cfg
);
788 jlink_config_mac_address_dump(ctx
, cfg
);
792 static int jlink_get_config(struct jlink_config
*cfg
)
795 int size
= sizeof(struct jlink_config
);
797 usb_out_buffer
[0] = EMU_CMD_READ_CONFIG
;
798 result
= jlink_usb_io(jlink_handle
, 1, size
);
800 if (result
!= ERROR_OK
) {
801 LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size
, result
);
805 memcpy(cfg
, usb_in_buffer
, size
);
809 static int jlink_set_config(struct jlink_config
*cfg
)
812 int size
= sizeof(struct jlink_config
);
814 jlink_simple_command(EMU_CMD_WRITE_CONFIG
);
816 memcpy(usb_out_buffer
, cfg
, size
);
818 result
= jlink_usb_write(jlink_handle
, size
);
819 if (result
!= size
) {
820 LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result
);
828 * List of unsupported version string markers.
830 * The firmware versions does not correspond directly with
831 * "Software and documentation pack for Windows", it may be
832 * distinguished by the "compile" date in the information string.
834 * For example, version string is:
835 * "J-Link ARM V8 compiled May 3 2012 18:36:22"
839 * The list must be terminated by NULL string.
841 static const char * const unsupported_versions
[] = {
844 NULL
/* End of list */
847 static void jlink_check_supported(const char *str
)
849 const char * const *p
= unsupported_versions
;
851 if (NULL
!= strstr(str
, *p
)) {
853 "Unsupported J-Link firmware version.\n"
854 " Please check http://www.segger.com/j-link-older-versions.html for updates");
861 static int jlink_get_version_info(void)
865 uint32_t jlink_max_size
;
867 /* query hardware version */
868 jlink_simple_command(EMU_CMD_VERSION
);
870 result
= jlink_usb_read(jlink_handle
, 2);
872 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
873 return ERROR_JTAG_DEVICE_ERROR
;
876 len
= buf_get_u32(usb_in_buffer
, 0, 16);
877 if (len
> JLINK_IN_BUFFER_SIZE
) {
878 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len
);
879 len
= JLINK_IN_BUFFER_SIZE
;
882 result
= jlink_usb_read(jlink_handle
, len
);
884 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
885 return ERROR_JTAG_DEVICE_ERROR
;
888 usb_in_buffer
[result
] = 0;
889 LOG_INFO("%s", (char *)usb_in_buffer
);
890 jlink_check_supported((char *)usb_in_buffer
);
892 /* query hardware capabilities */
893 jlink_simple_command(EMU_CMD_GET_CAPS
);
895 result
= jlink_usb_read(jlink_handle
, 4);
897 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result
);
898 return ERROR_JTAG_DEVICE_ERROR
;
901 jlink_caps
= buf_get_u32(usb_in_buffer
, 0, 32);
902 LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps
);
904 if (jlink_caps
& (1 << EMU_CAP_GET_HW_VERSION
)) {
905 /* query hardware version */
906 jlink_simple_command(EMU_CMD_GET_HW_VERSION
);
908 result
= jlink_usb_read(jlink_handle
, 4);
910 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result
);
911 return ERROR_JTAG_DEVICE_ERROR
;
914 uint32_t jlink_hw_version
= buf_get_u32(usb_in_buffer
, 0, 32);
915 uint32_t major_revision
= (jlink_hw_version
/ 10000) % 100;
916 jlink_hw_type
= (jlink_hw_version
/ 1000000) % 100;
917 if (major_revision
>= 5)
918 jlink_hw_jtag_version
= 3;
920 LOG_INFO("J-Link hw version %i", (int)jlink_hw_version
);
922 if (jlink_hw_type
>= JLINK_HW_TYPE_MAX
)
923 LOG_INFO("J-Link hw type uknown 0x%" PRIx32
, jlink_hw_type
);
925 LOG_INFO("J-Link hw type %s", jlink_hw_type_str
[jlink_hw_type
]);
928 if (jlink_caps
& (1 << EMU_CAP_GET_MAX_BLOCK_SIZE
)) {
929 /* query hardware maximum memory block */
930 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK
);
932 result
= jlink_usb_read(jlink_handle
, 4);
934 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result
);
935 return ERROR_JTAG_DEVICE_ERROR
;
938 jlink_max_size
= buf_get_u32(usb_in_buffer
, 0, 32);
939 LOG_INFO("J-Link max mem block %i", (int)jlink_max_size
);
942 if (jlink_caps
& (1 << EMU_CAP_READ_CONFIG
)) {
943 if (jlink_get_config(&jlink_cfg
) != ERROR_OK
)
944 return ERROR_JTAG_DEVICE_ERROR
;
946 jlink_config_dump(NULL
, &jlink_cfg
);
952 COMMAND_HANDLER(jlink_pid_command
)
955 LOG_ERROR("Need exactly one argument to jlink_pid");
959 pids
[0] = strtoul(CMD_ARGV
[0], NULL
, 16);
966 COMMAND_HANDLER(jlink_handle_jlink_info_command
)
968 if (jlink_get_version_info() == ERROR_OK
) {
969 /* attempt to get status */
976 COMMAND_HANDLER(jlink_handle_jlink_caps_command
)
978 jlink_caps_dump(CMD_CTX
);
983 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command
)
987 command_print(CMD_CTX
, "J-Link hw jtag %i", jlink_hw_jtag_version
);
990 int request_version
= atoi(CMD_ARGV
[0]);
991 switch (request_version
) {
994 jlink_hw_jtag_version
= request_version
;
997 return ERROR_COMMAND_SYNTAX_ERROR
;
1002 return ERROR_COMMAND_SYNTAX_ERROR
;
1008 COMMAND_HANDLER(jlink_handle_jlink_kickstart_command
)
1013 jlink_config_kickstart_dump(CMD_CTX
, &jlink_cfg
);
1017 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], kickstart
);
1019 jlink_cfg
.kickstart_power_on_jtag_pin_19
= kickstart
;
1023 COMMAND_HANDLER(jlink_handle_jlink_mac_address_command
)
1031 jlink_config_mac_address_dump(CMD_CTX
, &jlink_cfg
);
1037 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || str
[8] != ':' ||
1038 str
[11] != ':' || str
[14] != ':')) {
1039 command_print(CMD_CTX
, "ethaddr miss format ff:ff:ff:ff:ff:ff");
1040 return ERROR_COMMAND_SYNTAX_ERROR
;
1043 for (i
= 5; i
>= 0; i
--) {
1044 addr
[i
] = strtoul(str
, &e
, 16);
1048 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1049 command_print(CMD_CTX
, "invalid it's zero mac_address");
1050 return ERROR_COMMAND_SYNTAX_ERROR
;
1053 if (!(0x01 & addr
[0])) {
1054 command_print(CMD_CTX
, "invalid it's a multicat mac_address");
1055 return ERROR_COMMAND_SYNTAX_ERROR
;
1058 memcpy(jlink_cfg
.mac_address
, addr
, sizeof(addr
));
1063 static int string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1067 const char *s_save
= s
;
1073 for (i
= 0; i
< 4; i
++) {
1074 lip
[i
] = strtoul(s
, &e
, 10);
1076 if (*e
!= '.' && i
!= 3)
1084 memcpy(ip
, lip
, sizeof(lip
));
1088 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1092 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1096 COMMAND_HANDLER(jlink_handle_jlink_ip_command
)
1098 uint32_t ip_address
;
1099 uint32_t subnet_mask
= 0;
1102 uint8_t subnet_bits
= 24;
1105 jlink_config_ip_dump(CMD_CTX
, &jlink_cfg
);
1109 ret
= string_to_ip(CMD_ARGV
[0], (uint8_t *)&ip_address
, &i
);
1110 if (ret
!= ERROR_OK
)
1113 len
= strlen(CMD_ARGV
[0]);
1115 /* check for this format A.B.C.D/E */
1118 if (CMD_ARGV
[0][i
] != '/')
1119 return ERROR_COMMAND_SYNTAX_ERROR
;
1121 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1124 ret
= string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
);
1125 if (ret
!= ERROR_OK
)
1131 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1132 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1134 cpy_ip(jlink_cfg
.ip_address
, (uint8_t *)&ip_address
);
1135 cpy_ip(jlink_cfg
.subnet_mask
, (uint8_t *)&subnet_mask
);
1140 COMMAND_HANDLER(jlink_handle_jlink_reset_command
)
1142 memset(&jlink_cfg
, 0xff, sizeof(jlink_cfg
));
1146 COMMAND_HANDLER(jlink_handle_jlink_save_command
)
1148 if (!(jlink_caps
& (1 << EMU_CAP_WRITE_CONFIG
))) {
1149 command_print(CMD_CTX
, "J-Link write emulator configuration not supported");
1153 command_print(CMD_CTX
, "The J-Link need to be unpluged and repluged ta have the config effective");
1154 return jlink_set_config(&jlink_cfg
);
1157 COMMAND_HANDLER(jlink_handle_jlink_usb_address_command
)
1162 jlink_config_usb_address_dump(CMD_CTX
, &jlink_cfg
);
1166 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
1168 if (address
> 0x3 && address
!= 0xff) {
1169 command_print(CMD_CTX
, "USB Address must be between 0x00 and 0x03 or 0xff");
1170 return ERROR_COMMAND_SYNTAX_ERROR
;
1173 jlink_cfg
.usb_address
= address
;
1177 COMMAND_HANDLER(jlink_handle_jlink_config_command
)
1179 struct jlink_config cfg
;
1182 if (CMD_ARGC
== 0) {
1183 if (!(jlink_caps
& (1 << EMU_CAP_READ_CONFIG
))) {
1184 command_print(CMD_CTX
, "J-Link read emulator configuration not supported");
1188 ret
= jlink_get_config(&cfg
);
1190 if (ret
!= ERROR_OK
)
1191 command_print(CMD_CTX
, "J-Link read emulator configuration failled");
1193 jlink_config_dump(CMD_CTX
, &jlink_cfg
);
1200 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1202 .name
= "kickstart",
1203 .handler
= &jlink_handle_jlink_kickstart_command
,
1204 .mode
= COMMAND_EXEC
,
1205 .help
= "set Kickstart power on JTAG-pin 19.",
1209 .name
= "mac_address",
1210 .handler
= &jlink_handle_jlink_mac_address_command
,
1211 .mode
= COMMAND_EXEC
,
1212 .help
= "set the MAC Address",
1213 .usage
= "[ff:ff:ff:ff:ff:ff]",
1217 .handler
= &jlink_handle_jlink_ip_command
,
1218 .mode
= COMMAND_EXEC
,
1219 .help
= "set the ip address of the J-Link Pro, "
1220 "where A.B.C.D is the ip, "
1221 "E the bit of the subnet mask, "
1222 "F.G.H.I the subnet mask",
1223 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1227 .handler
= &jlink_handle_jlink_reset_command
,
1228 .mode
= COMMAND_EXEC
,
1229 .help
= "reset the current config",
1233 .handler
= &jlink_handle_jlink_save_command
,
1234 .mode
= COMMAND_EXEC
,
1235 .help
= "save the current config",
1238 .name
= "usb_address",
1239 .handler
= &jlink_handle_jlink_usb_address_command
,
1240 .mode
= COMMAND_EXEC
,
1241 .help
= "set the USB-Address, "
1242 "This will change the product id",
1243 .usage
= "[0x00 to 0x03 or 0xff]",
1245 COMMAND_REGISTRATION_DONE
1248 static const struct command_registration jlink_subcommand_handlers
[] = {
1251 .handler
= &jlink_handle_jlink_caps_command
,
1252 .mode
= COMMAND_EXEC
,
1253 .help
= "show jlink capabilities",
1257 .handler
= &jlink_handle_jlink_info_command
,
1258 .mode
= COMMAND_EXEC
,
1259 .help
= "show jlink info",
1263 .handler
= &jlink_handle_jlink_hw_jtag_command
,
1264 .mode
= COMMAND_EXEC
,
1265 .help
= "access J-Link HW JTAG command version",
1270 .handler
= &jlink_handle_jlink_config_command
,
1271 .mode
= COMMAND_EXEC
,
1272 .help
= "access J-Link configuration, "
1273 "if no argument this will dump the config",
1274 .chain
= jlink_config_subcommand_handlers
,
1278 .handler
= &jlink_pid_command
,
1279 .mode
= COMMAND_CONFIG
,
1280 .help
= "set the pid of the interface we want to use",
1282 COMMAND_REGISTRATION_DONE
1285 static const struct command_registration jlink_command_handlers
[] = {
1288 .mode
= COMMAND_ANY
,
1289 .help
= "perform jlink management",
1290 .chain
= jlink_subcommand_handlers
,
1292 COMMAND_REGISTRATION_DONE
1295 struct jtag_interface jlink_interface
= {
1297 .commands
= jlink_command_handlers
,
1298 .transports
= jtag_only
,
1300 .execute_queue
= jlink_execute_queue
,
1301 .speed
= jlink_speed
,
1302 .speed_div
= jlink_speed_div
,
1308 /***************************************************************************/
1309 /* J-Link tap functions */
1312 static unsigned tap_length
;
1313 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1314 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1315 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1317 struct pending_scan_result
{
1318 int first
; /* First bit position in tdo_buffer to read */
1319 int length
; /* Number of bits to read */
1320 struct scan_command
*command
; /* Corresponding scan command */
1324 #define MAX_PENDING_SCAN_RESULTS 256
1326 static int pending_scan_results_length
;
1327 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1329 static void jlink_tap_init(void)
1332 pending_scan_results_length
= 0;
1335 static void jlink_tap_ensure_space(int scans
, int bits
)
1337 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1338 int available_bits
= JLINK_TAP_BUFFER_SIZE
* 8 - tap_length
- 32;
1340 if (scans
> available_scans
|| bits
> available_bits
)
1341 jlink_tap_execute();
1344 static void jlink_tap_append_step(int tms
, int tdi
)
1346 int index_var
= tap_length
/ 8;
1348 assert(index_var
< JLINK_TAP_BUFFER_SIZE
);
1350 int bit_index
= tap_length
% 8;
1351 uint8_t bit
= 1 << bit_index
;
1353 /* we do not pad TMS, so be sure to initialize all bits */
1355 tms_buffer
[index_var
] = tdi_buffer
[index_var
] = 0;
1358 tms_buffer
[index_var
] |= bit
;
1360 tms_buffer
[index_var
] &= ~bit
;
1363 tdi_buffer
[index_var
] |= bit
;
1365 tdi_buffer
[index_var
] &= ~bit
;
1370 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
1371 struct scan_command
*command
)
1373 struct pending_scan_result
*pending_scan_result
=
1374 &pending_scan_results_buffer
[pending_scan_results_length
];
1377 pending_scan_result
->first
= tap_length
;
1378 pending_scan_result
->length
= length
;
1379 pending_scan_result
->command
= command
;
1380 pending_scan_result
->buffer
= buffer
;
1382 for (i
= 0; i
< length
; i
++) {
1383 int tms
= (i
< (length
- 1)) ? 0 : 1;
1384 int tdi
= (buffer
[i
/ 8] & (1 << (i
% 8))) != 0;
1385 jlink_tap_append_step(tms
, tdi
);
1387 pending_scan_results_length
++;
1390 /* Pad and send a tap sequence to the device, and receive the answer.
1391 * For the purpose of padding we assume that we are in idle or pause state. */
1392 static int jlink_tap_execute(void)
1401 /* JLink returns an extra NULL in packet when size of incoming
1402 * message is a multiple of 64, creates problems with USB comms.
1403 * WARNING: This will interfere with tap state counting. */
1404 while ((DIV_ROUND_UP(tap_length
, 8) % 64) == 0)
1405 jlink_tap_append_step((tap_get_state() == TAP_RESET
) ? 1 : 0, 0);
1407 /* number of full bytes (plus one if some would be left over) */
1408 byte_length
= DIV_ROUND_UP(tap_length
, 8);
1410 bool use_jtag3
= jlink_hw_jtag_version
>= 3;
1411 usb_out_buffer
[0] = use_jtag3
? EMU_CMD_HW_JTAG3
: EMU_CMD_HW_JTAG2
;
1412 usb_out_buffer
[1] = 0;
1413 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1414 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1415 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1416 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1418 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
1419 tap_length
, jlink_last_state
);
1421 result
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1422 use_jtag3
? byte_length
+ 1 : byte_length
);
1423 if (result
!= ERROR_OK
) {
1424 LOG_ERROR("jlink_tap_execute failed USB io (%d)", result
);
1426 return ERROR_JTAG_QUEUE_FAILED
;
1429 result
= use_jtag3
? usb_in_buffer
[byte_length
] : 0;
1431 LOG_ERROR("jlink_tap_execute failed, result %d", result
);
1433 return ERROR_JTAG_QUEUE_FAILED
;
1436 memcpy(tdo_buffer
, usb_in_buffer
, byte_length
);
1438 for (i
= 0; i
< pending_scan_results_length
; i
++) {
1439 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1440 uint8_t *buffer
= pending_scan_result
->buffer
;
1441 int length
= pending_scan_result
->length
;
1442 int first
= pending_scan_result
->first
;
1443 struct scan_command
*command
= pending_scan_result
->command
;
1445 /* Copy to buffer */
1446 buf_set_buf(tdo_buffer
, first
, buffer
, 0, length
);
1448 DEBUG_JTAG_IO("pending scan result, length = %d", length
);
1450 jlink_debug_buffer(buffer
, DIV_ROUND_UP(length
, 8));
1452 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
) {
1454 return ERROR_JTAG_QUEUE_FAILED
;
1457 if (pending_scan_result
->buffer
!= NULL
)
1458 free(pending_scan_result
->buffer
);
1465 /*****************************************************************************/
1466 /* JLink USB low-level functions */
1468 static struct jlink
*jlink_usb_open()
1470 struct jtag_libusb_device_handle
*devh
;
1471 if (jtag_libusb_open(vids
, pids
, &devh
) != ERROR_OK
)
1474 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
1475 * AREA!!!!!!!!!!! The behavior of libusb is not completely
1476 * consistent across Windows, Linux, and Mac OS X platforms.
1477 * The actions taken in the following compiler conditionals may
1478 * not agree with published documentation for libusb, but were
1479 * found to be necessary through trials and tribulations. Even
1480 * little tweaks can break one or more platforms, so if you do
1481 * make changes test them carefully on all platforms before
1487 jtag_libusb_reset_device(devh
);
1492 /* reopen jlink after usb_reset
1493 * on win32 this may take a second or two to re-enumerate */
1495 while ((retval
= jtag_libusb_open(vids
, pids
, &devh
)) != ERROR_OK
) {
1501 if (ERROR_OK
!= retval
)
1507 /* usb_set_configuration required under win32 */
1508 struct jtag_libusb_device
*udev
= jtag_libusb_get_device(devh
);
1509 jtag_libusb_set_configuration(devh
, 0);
1510 jtag_libusb_claim_interface(devh
, 0);
1514 * This makes problems under Mac OS X. And is not needed
1515 * under Windows. Hopefully this will not break a linux build
1517 usb_set_altinterface(result
->usb_handle
, 0);
1520 jtag_libusb_get_endpoints(udev
, &jlink_read_ep
, &jlink_write_ep
);
1522 struct jlink
*result
= malloc(sizeof(struct jlink
));
1523 result
->usb_handle
= devh
;
1527 static void jlink_usb_close(struct jlink
*jlink
)
1529 jtag_libusb_close(jlink
->usb_handle
);
1533 /* Send a message and receive the reply. */
1534 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
)
1538 result
= jlink_usb_write(jlink
, out_length
);
1539 if (result
!= out_length
) {
1540 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1541 out_length
, result
);
1542 return ERROR_JTAG_DEVICE_ERROR
;
1545 result
= jlink_usb_read(jlink
, in_length
);
1546 if (result
!= in_length
) {
1547 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1549 return ERROR_JTAG_DEVICE_ERROR
;
1554 /* calls the given usb_bulk_* function, allowing for the data to
1555 * trickle in with some timeouts */
1556 static int usb_bulk_with_retries(
1557 int (*f
)(jtag_libusb_device_handle
*, int, char *, int, int),
1558 jtag_libusb_device_handle
*dev
, int ep
,
1559 char *bytes
, int size
, int timeout
)
1561 int tries
= 3, count
= 0;
1563 while (tries
&& (count
< size
)) {
1564 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
1567 else if ((-ETIMEDOUT
!= result
) || !--tries
)
1573 static int wrap_usb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
,
1574 char *buff
, int size
, int timeout
)
1576 /* usb_bulk_write() takes const char *buff */
1577 return jtag_libusb_bulk_write(dev
, ep
, buff
, size
, timeout
);
1580 static inline int usb_bulk_write_ex(jtag_libusb_device_handle
*dev
, int ep
,
1581 char *bytes
, int size
, int timeout
)
1583 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
1584 dev
, ep
, bytes
, size
, timeout
);
1587 static inline int usb_bulk_read_ex(jtag_libusb_device_handle
*dev
, int ep
,
1588 char *bytes
, int size
, int timeout
)
1590 return usb_bulk_with_retries(&jtag_libusb_bulk_read
,
1591 dev
, ep
, bytes
, size
, timeout
);
1594 /* Write data from out_buffer to USB. */
1595 static int jlink_usb_write(struct jlink
*jlink
, int out_length
)
1599 if (out_length
> JLINK_OUT_BUFFER_SIZE
) {
1600 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
1601 out_length
, JLINK_OUT_BUFFER_SIZE
);
1605 result
= usb_bulk_write_ex(jlink
->usb_handle
, jlink_write_ep
,
1606 (char *)usb_out_buffer
, out_length
, JLINK_USB_TIMEOUT
);
1608 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
1609 out_length
, result
);
1611 jlink_debug_buffer(usb_out_buffer
, out_length
);
1615 /* Read data from USB into in_buffer. */
1616 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
)
1618 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1619 (char *)usb_in_buffer
, expected_size
, JLINK_USB_TIMEOUT
);
1621 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result
);
1623 jlink_debug_buffer(usb_in_buffer
, result
);
1628 * Send a message and receive the reply - simple messages.
1630 * @param jlink pointer to driver data
1631 * @param out_length data length in @c usb_out_buffer
1632 * @param in_length data length to be read to @c usb_in_buffer
1634 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
)
1638 result
= jlink_usb_write(jlink
, out_length
);
1639 if (result
!= out_length
) {
1640 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1641 out_length
, result
);
1642 return ERROR_JTAG_DEVICE_ERROR
;
1645 result
= jlink_usb_read(jlink
, in_length
);
1646 if (result
!= in_length
) {
1647 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1649 return ERROR_JTAG_DEVICE_ERROR
;
1653 * Section 4.2.4 IN-transaction:
1654 * read dummy 0-byte packet if transaction size is
1655 * multiple of 64 bytes but not max. size of 0x8000
1657 if ((in_length
% 64) == 0 && in_length
!= 0x8000) {
1659 result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1660 &dummy_buffer
, 1, JLINK_USB_TIMEOUT
);
1662 LOG_ERROR("dummy byte read failed");
1663 return ERROR_JTAG_DEVICE_ERROR
;
1669 #ifdef _DEBUG_USB_COMMS_
1670 #define BYTES_PER_LINE 16
1672 static void jlink_debug_buffer(uint8_t *buffer
, int length
)
1679 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
1680 snprintf(line
, 5, "%04x", i
);
1681 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
1682 snprintf(s
, 4, " %02x", buffer
[j
]);
1685 LOG_DEBUG("%s", line
);
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)