f173ed783e697791a14947addc410644cc6d0a74
[openocd.git] / src / jtag / jlink.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include "interface.h"
29 #include "commands.h"
30 #include "usb_common.h"
31
32
33 #define VID 0x1366
34 #define PID 0x0101
35
36 #define JLINK_WRITE_ENDPOINT 0x02
37 #define JLINK_READ_ENDPOINT 0x81
38
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;
42
43 #define JLINK_USB_TIMEOUT 1000
44
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
50
51 #define JLINK_IN_BUFFER_SIZE 2048
52 #define JLINK_OUT_BUFFER_SIZE 2*2048 + 4
53 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
54
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];
59
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
76
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
80
81 /* max speed 12MHz v5.0 jlink */
82 #define JLINK_MAX_SPEED 12000
83
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);
93
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);
100
101 /* Jlink lowlevel functions */
102 struct jlink {
103 struct usb_dev_handle* usb_handle;
104 };
105
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);
112
113 /* helper functions */
114 static int jlink_get_version_info(void);
115
116 #ifdef _DEBUG_USB_COMMS_
117 static void jlink_debug_buffer(uint8_t *buffer, int length);
118 #endif
119
120 static enum tap_state jlink_last_state = TAP_RESET;
121
122 static struct jlink* jlink_handle;
123
124 /***************************************************************************/
125 /* External interface implementation */
126
127 static void jlink_execute_runtest(struct jtag_command *cmd)
128 {
129 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
130 cmd->cmd.runtest->num_cycles,
131 cmd->cmd.runtest->end_state);
132
133 jlink_end_state(cmd->cmd.runtest->end_state);
134
135 jlink_runtest(cmd->cmd.runtest->num_cycles);
136 }
137
138 static void jlink_execute_statemove(struct jtag_command *cmd)
139 {
140 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
141
142 jlink_end_state(cmd->cmd.statemove->end_state);
143 jlink_state_move();
144 }
145
146 static void jlink_execute_pathmove(struct jtag_command *cmd)
147 {
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]);
151
152 jlink_path_move(cmd->cmd.pathmove->num_states,
153 cmd->cmd.pathmove->path);
154 }
155
156 static void jlink_execute_scan(struct jtag_command *cmd)
157 {
158 int scan_size;
159 enum scan_type type;
160 uint8_t *buffer;
161
162 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
163
164 jlink_end_state(cmd->cmd.scan->end_state);
165
166 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
167 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
168
169 #ifdef _DEBUG_USB_COMMS_
170 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
171 #endif
172 type = jtag_scan_type(cmd->cmd.scan);
173 jlink_scan(cmd->cmd.scan->ir_scan,
174 type, buffer, scan_size, cmd->cmd.scan);
175 }
176
177 static void jlink_execute_reset(struct jtag_command *cmd)
178 {
179 DEBUG_JTAG_IO("reset trst: %i srst %i",
180 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
181
182 jlink_tap_execute();
183 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
184 jlink_tap_execute();
185 }
186
187 static void jlink_execute_sleep(struct jtag_command *cmd)
188 {
189 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
190 jlink_tap_execute();
191 jtag_sleep(cmd->cmd.sleep->us);
192 }
193
194 static void jlink_execute_command(struct jtag_command *cmd)
195 {
196 switch (cmd->type)
197 {
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;
204 default:
205 LOG_ERROR("BUG: unknown JTAG command type encountered");
206 exit(-1);
207 }
208 }
209
210 static int jlink_execute_queue(void)
211 {
212 struct jtag_command *cmd = jtag_command_queue;
213
214 while (cmd != NULL)
215 {
216 jlink_execute_command(cmd);
217 cmd = cmd->next;
218 }
219
220 return jlink_tap_execute();
221 }
222
223 /* Sets speed in kHz. */
224 static int jlink_speed(int speed)
225 {
226 int result;
227
228 if (speed > JLINK_MAX_SPEED)
229 {
230 LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
231 speed, JLINK_MAX_SPEED);
232 return ERROR_OK;
233 }
234
235 /* check for RTCK setting */
236 if (speed == 0)
237 speed = -1;
238
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;
242
243 result = jlink_usb_write(jlink_handle, 3);
244 if (result != 3)
245 {
246 LOG_ERROR("J-Link setting speed failed (%d)", result);
247 return ERROR_JTAG_DEVICE_ERROR;
248 }
249
250 return ERROR_OK;
251 }
252
253 static int jlink_speed_div(int speed, int* khz)
254 {
255 *khz = speed;
256
257 return ERROR_OK;
258 }
259
260 static int jlink_khz(int khz, int *jtag_speed)
261 {
262 *jtag_speed = khz;
263
264 return ERROR_OK;
265 }
266
267 static int jlink_init(void)
268 {
269 int i;
270
271 jlink_handle = jlink_usb_open();
272
273 if (jlink_handle == 0)
274 {
275 LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
276 return ERROR_JTAG_INIT_FAILED;
277 }
278
279 /*
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.
287 */
288
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);
292 usleep(1000);
293
294 jlink_hw_jtag_version = 2;
295
296 if (jlink_get_version_info() == ERROR_OK)
297 {
298 /* attempt to get status */
299 jlink_get_status();
300 }
301
302 LOG_INFO("J-Link JTAG Interface ready");
303
304 jlink_reset(0, 0);
305 jtag_sleep(3000);
306 jlink_tap_init();
307 jlink_speed(jtag_get_speed());
308
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);
313 }
314 jlink_tap_execute();
315
316 return ERROR_OK;
317 }
318
319 static int jlink_quit(void)
320 {
321 jlink_usb_close(jlink_handle);
322 return ERROR_OK;
323 }
324
325 /***************************************************************************/
326 /* Queue command implementations */
327
328 static void jlink_end_state(tap_state_t state)
329 {
330 if (tap_is_state_stable(state))
331 {
332 tap_set_end_state(state);
333 }
334 else
335 {
336 LOG_ERROR("BUG: %i is not a valid end state", state);
337 exit(-1);
338 }
339 }
340
341 /* Goes to the end state. */
342 static void jlink_state_move(void)
343 {
344 int i;
345 int tms = 0;
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());
348
349 for (i = 0; i < tms_scan_bits; i++)
350 {
351 tms = (tms_scan >> i) & 1;
352 jlink_tap_append_step(tms, 0);
353 }
354
355 tap_set_state(tap_get_end_state());
356 }
357
358 static void jlink_path_move(int num_states, tap_state_t *path)
359 {
360 int i;
361
362 for (i = 0; i < num_states; i++)
363 {
364 if (path[i] == tap_state_transition(tap_get_state(), false))
365 {
366 jlink_tap_append_step(0, 0);
367 }
368 else if (path[i] == tap_state_transition(tap_get_state(), true))
369 {
370 jlink_tap_append_step(1, 0);
371 }
372 else
373 {
374 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
375 exit(-1);
376 }
377
378 tap_set_state(path[i]);
379 }
380
381 tap_set_end_state(tap_get_state());
382 }
383
384 static void jlink_runtest(int num_cycles)
385 {
386 int i;
387
388 tap_state_t saved_end_state = tap_get_end_state();
389
390 jlink_tap_ensure_space(1,num_cycles + 16);
391
392 /* only do a state_move when we're not already in IDLE */
393 if (tap_get_state() != TAP_IDLE)
394 {
395 jlink_end_state(TAP_IDLE);
396 jlink_state_move();
397 // num_cycles--;
398 }
399
400 /* execute num_cycles */
401 for (i = 0; i < num_cycles; i++)
402 {
403 jlink_tap_append_step(0, 0);
404 }
405
406 /* finish in end_state */
407 jlink_end_state(saved_end_state);
408 if (tap_get_state() != tap_get_end_state())
409 {
410 jlink_state_move();
411 }
412 }
413
414 static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
415 {
416 tap_state_t saved_end_state;
417
418 jlink_tap_ensure_space(1, scan_size + 16);
419
420 saved_end_state = tap_get_end_state();
421
422 /* Move to appropriate scan state */
423 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
424
425 /* Only move if we're not already there */
426 if (tap_get_state() != tap_get_end_state())
427 jlink_state_move();
428
429 jlink_end_state(saved_end_state);
430
431 /* Scan */
432 jlink_tap_append_scan(scan_size, buffer, command);
433
434 /* We are in Exit1, go to Pause */
435 jlink_tap_append_step(0, 0);
436
437 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
438
439 if (tap_get_state() != tap_get_end_state())
440 {
441 jlink_state_move();
442 }
443 }
444
445 static void jlink_reset(int trst, int srst)
446 {
447 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
448
449 /* Signals are active low */
450 if (srst == 0)
451 {
452 jlink_simple_command(EMU_CMD_HW_RESET1);
453 }
454 if (srst == 1)
455 {
456 jlink_simple_command(EMU_CMD_HW_RESET0);
457 }
458
459 if (trst == 1)
460 {
461 jlink_simple_command(EMU_CMD_HW_TRST0);
462 }
463
464 if (trst == 0)
465 {
466 jlink_simple_command(EMU_CMD_HW_TRST1);
467 }
468 }
469
470 static void jlink_simple_command(uint8_t command)
471 {
472 int result;
473
474 DEBUG_JTAG_IO("0x%02x", command);
475
476 usb_out_buffer[0] = command;
477 result = jlink_usb_write(jlink_handle, 1);
478
479 if (result != 1)
480 {
481 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
482 }
483 }
484
485 static int jlink_get_status(void)
486 {
487 int result;
488
489 jlink_simple_command(EMU_CMD_GET_STATE);
490
491 result = jlink_usb_read(jlink_handle, 8);
492 if (result != 8)
493 {
494 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
495 return ERROR_JTAG_DEVICE_ERROR;
496 }
497
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]);
503
504 if (vref < 1500)
505 LOG_ERROR("Vref too low. Check Target Power\n");
506
507 return ERROR_OK;
508 }
509
510 static int jlink_get_version_info(void)
511 {
512 int result;
513 int len;
514 uint32_t jlink_caps, jlink_max_size;
515
516 /* query hardware version */
517 jlink_simple_command(EMU_CMD_VERSION);
518
519 result = jlink_usb_read(jlink_handle, 2);
520 if (2 != result)
521 {
522 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
523 return ERROR_JTAG_DEVICE_ERROR;
524 }
525
526 len = buf_get_u32(usb_in_buffer, 0, 16);
527 if (len > JLINK_IN_BUFFER_SIZE)
528 {
529 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
530 len = JLINK_IN_BUFFER_SIZE;
531 }
532
533 result = jlink_usb_read(jlink_handle, len);
534 if (result != len)
535 {
536 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
537 return ERROR_JTAG_DEVICE_ERROR;
538 }
539
540 usb_in_buffer[result] = 0;
541 LOG_INFO("%s", (char *)usb_in_buffer);
542
543 /* query hardware capabilities */
544 jlink_simple_command(EMU_CMD_GET_CAPS);
545
546 result = jlink_usb_read(jlink_handle, 4);
547 if (4 != result)
548 {
549 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)\n", result);
550 return ERROR_JTAG_DEVICE_ERROR;
551 }
552
553 jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
554 LOG_INFO("JLink caps 0x%x", (unsigned)jlink_caps);
555
556 if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION))
557 {
558 /* query hardware version */
559 jlink_simple_command(EMU_CMD_GET_HW_VERSION);
560
561 result = jlink_usb_read(jlink_handle, 4);
562 if (4 != result)
563 {
564 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)\n", result);
565 return ERROR_JTAG_DEVICE_ERROR;
566 }
567
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;
572
573 LOG_INFO("JLink hw version %i", (int)jlink_hw_version);
574 }
575
576 if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE))
577 {
578 /* query hardware maximum memory block */
579 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
580
581 result = jlink_usb_read(jlink_handle, 4);
582 if (4 != result)
583 {
584 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)\n", result);
585 return ERROR_JTAG_DEVICE_ERROR;
586 }
587
588 jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
589 LOG_INFO("JLink max mem block %i", (int)jlink_max_size);
590 }
591
592 return ERROR_OK;
593 }
594
595 COMMAND_HANDLER(jlink_handle_jlink_info_command)
596 {
597 if (jlink_get_version_info() == ERROR_OK)
598 {
599 /* attempt to get status */
600 jlink_get_status();
601 }
602
603 return ERROR_OK;
604 }
605
606 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command)
607 {
608 switch (CMD_ARGC) {
609 case 0:
610 command_print(CMD_CTX, "jlink hw jtag %i", jlink_hw_jtag_version);
611 break;
612 case 1: {
613 int request_version = atoi(CMD_ARGV[0]);
614 switch (request_version) {
615 case 2: case 3:
616 jlink_hw_jtag_version = request_version;
617 break;
618 default:
619 return ERROR_COMMAND_SYNTAX_ERROR;
620 }
621 break;
622 }
623 default:
624 return ERROR_COMMAND_SYNTAX_ERROR;
625 }
626
627 return ERROR_OK;
628 }
629
630 static int jlink_register_commands(struct command_context *cmd_ctx)
631 {
632
633 register_command(cmd_ctx, NULL, "jlink_info",
634 &jlink_handle_jlink_info_command, COMMAND_EXEC,
635 "query jlink info");
636 register_command(cmd_ctx, NULL, "jlink_hw_jtag",
637 &jlink_handle_jlink_hw_jtag_command, COMMAND_EXEC,
638 "set/get jlink hw jtag command version [2 | 3]");
639 return ERROR_OK;
640 }
641
642 struct jtag_interface jlink_interface = {
643 .name = "jlink",
644 .execute_queue = &jlink_execute_queue,
645 .speed = &jlink_speed,
646 .speed_div = &jlink_speed_div,
647 .khz = &jlink_khz,
648 .register_commands = &jlink_register_commands,
649 .init = &jlink_init,
650 .quit = &jlink_quit,
651 };
652
653 /***************************************************************************/
654 /* J-Link tap functions */
655
656
657 static unsigned tap_length = 0;
658 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
659 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
660 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
661
662 struct pending_scan_result {
663 int first; /* First bit position in tdo_buffer to read */
664 int length; /* Number of bits to read */
665 struct scan_command *command; /* Corresponding scan command */
666 uint8_t *buffer;
667 };
668
669 #define MAX_PENDING_SCAN_RESULTS 256
670
671 static int pending_scan_results_length;
672 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
673
674 static void jlink_tap_init(void)
675 {
676 tap_length = 0;
677 pending_scan_results_length = 0;
678 }
679
680 static void jlink_tap_ensure_space(int scans, int bits)
681 {
682 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
683 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
684
685 if (scans > available_scans || bits > available_bits)
686 {
687 jlink_tap_execute();
688 }
689 }
690
691 static void jlink_tap_append_step(int tms, int tdi)
692 {
693 int index = tap_length / 8;
694
695 if (index >= JLINK_TAP_BUFFER_SIZE)
696 {
697 LOG_ERROR("jlink_tap_append_step: overflow");
698 *(uint32_t *)0xFFFFFFFF = 0;
699 exit(-1);
700 }
701
702 int bit_index = tap_length % 8;
703 uint8_t bit = 1 << bit_index;
704
705 // we do not pad TMS, so be sure to initialize all bits
706 if (0 == bit_index)
707 {
708 tms_buffer[index] = tdi_buffer[index] = 0;
709 }
710
711 if (tms)
712 tms_buffer[index] |= bit;
713 else
714 tms_buffer[index] &= ~bit;
715
716 if (tdi)
717 tdi_buffer[index] |= bit;
718 else
719 tdi_buffer[index] &= ~bit;
720
721 tap_length++;
722 }
723
724 static void jlink_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
725 {
726 struct pending_scan_result *pending_scan_result =
727 &pending_scan_results_buffer[pending_scan_results_length];
728 int i;
729
730 pending_scan_result->first = tap_length;
731 pending_scan_result->length = length;
732 pending_scan_result->command = command;
733 pending_scan_result->buffer = buffer;
734
735 for (i = 0; i < length; i++)
736 {
737 int tms = (i < (length - 1)) ? 0 : 1;
738 int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0;
739 jlink_tap_append_step(tms, tdi);
740 }
741 pending_scan_results_length++;
742 }
743
744 /* Pad and send a tap sequence to the device, and receive the answer.
745 * For the purpose of padding we assume that we are in idle or pause state. */
746 static int jlink_tap_execute(void)
747 {
748 int byte_length;
749 int i;
750 int result;
751
752 if (!tap_length)
753 return ERROR_OK;
754
755 /* JLink returns an extra NULL in packet when size of incoming
756 * message is a multiple of 64, creates problems with USB comms.
757 * WARNING: This will interfere with tap state counting. */
758 while ((DIV_ROUND_UP(tap_length, 8) % 64) == 0)
759 {
760 jlink_tap_append_step((tap_get_state() == TAP_RESET)?1:0, 0);
761 }
762
763 // number of full bytes (plus one if some would be left over)
764 byte_length = DIV_ROUND_UP(tap_length, 8);
765
766 bool use_jtag3 = jlink_hw_jtag_version >= 3;
767 usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
768 usb_out_buffer[1] = 0;
769 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
770 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
771 memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
772 memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
773
774 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
775 tap_length, jlink_last_state);
776
777 result = jlink_usb_message(jlink_handle, 4 + 2 * byte_length, byte_length);
778 if (result != byte_length)
779 {
780 LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result, byte_length);
781 jlink_tap_init();
782 return ERROR_JTAG_QUEUE_FAILED;
783 }
784
785 memcpy(tdo_buffer, usb_in_buffer, byte_length);
786
787 for (i = 0; i < pending_scan_results_length; i++)
788 {
789 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
790 uint8_t *buffer = pending_scan_result->buffer;
791 int length = pending_scan_result->length;
792 int first = pending_scan_result->first;
793 struct scan_command *command = pending_scan_result->command;
794
795 /* Copy to buffer */
796 buf_set_buf(tdo_buffer, first, buffer, 0, length);
797
798 DEBUG_JTAG_IO("pending scan result, length = %d", length);
799
800 #ifdef _DEBUG_USB_COMMS_
801 jlink_debug_buffer(buffer, DIV_ROUND_UP(length, 8));
802 #endif
803
804 if (jtag_read_buffer(buffer, command) != ERROR_OK)
805 {
806 jlink_tap_init();
807 return ERROR_JTAG_QUEUE_FAILED;
808 }
809
810 if (pending_scan_result->buffer != NULL)
811 {
812 free(pending_scan_result->buffer);
813 }
814 }
815
816 jlink_tap_init();
817 return ERROR_OK;
818 }
819
820 /*****************************************************************************/
821 /* JLink USB low-level functions */
822
823 static struct jlink* jlink_usb_open()
824 {
825 usb_init();
826
827 const uint16_t vids[] = { VID, 0 };
828 const uint16_t pids[] = { PID, 0 };
829 struct usb_dev_handle *dev;
830 if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
831 return NULL;
832
833 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
834 * AREA!!!!!!!!!!! The behavior of libusb is not completely
835 * consistent across Windows, Linux, and Mac OS X platforms.
836 * The actions taken in the following compiler conditionals may
837 * not agree with published documentation for libusb, but were
838 * found to be necessary through trials and tribulations. Even
839 * little tweaks can break one or more platforms, so if you do
840 * make changes test them carefully on all platforms before
841 * committing them!
842 */
843
844 #if IS_WIN32 == 0
845
846 usb_reset(dev);
847
848 #if IS_DARWIN == 0
849
850 int timeout = 5;
851 /* reopen jlink after usb_reset
852 * on win32 this may take a second or two to re-enumerate */
853 int retval;
854 while ((retval = jtag_usb_open(vids, pids, &dev)) != ERROR_OK)
855 {
856 usleep(1000);
857 timeout--;
858 if (!timeout) {
859 break;
860 }
861 }
862 if (ERROR_OK != retval)
863 return NULL;
864 #endif
865
866 #endif
867
868 /* usb_set_configuration required under win32 */
869 struct usb_device *udev = usb_device(dev);
870 usb_set_configuration(dev, udev->config[0].bConfigurationValue);
871 usb_claim_interface(dev, 0);
872
873 #if 0
874 /*
875 * This makes problems under Mac OS X. And is not needed
876 * under Windows. Hopefully this will not break a linux build
877 */
878 usb_set_altinterface(result->usb_handle, 0);
879 #endif
880 struct usb_interface *iface = udev->config->interface;
881 struct usb_interface_descriptor *desc = iface->altsetting;
882 for (int i = 0; i < desc->bNumEndpoints; i++)
883 {
884 uint8_t epnum = desc->endpoint[i].bEndpointAddress;
885 bool is_input = epnum & 0x80;
886 LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
887 if (is_input)
888 jlink_read_ep = epnum;
889 else
890 jlink_write_ep = epnum;
891 }
892
893 struct jlink *result = malloc(sizeof(struct jlink));
894 result->usb_handle = dev;
895 return result;
896 }
897
898 static void jlink_usb_close(struct jlink *jlink)
899 {
900 usb_close(jlink->usb_handle);
901 free(jlink);
902 }
903
904 /* Send a message and receive the reply. */
905 static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length)
906 {
907 int result;
908
909 result = jlink_usb_write(jlink, out_length);
910 if (result != out_length)
911 {
912 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
913 out_length, result);
914 return ERROR_JTAG_DEVICE_ERROR;
915 }
916
917 result = jlink_usb_read(jlink, in_length);
918 if ((result != in_length) && (result != (in_length + 1)))
919 {
920 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
921 in_length, result);
922 return ERROR_JTAG_DEVICE_ERROR;
923 }
924
925 if (jlink_hw_jtag_version < 3)
926 return result;
927
928 int result2 = ERROR_OK;
929 if (result == in_length)
930 {
931 /* Must read the result from the EMU too */
932 result2 = jlink_usb_read_emu_result(jlink);
933 if (1 != result2)
934 {
935 LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, result=%d, in_length=%i", result2,in_length);
936 /* Try again once, should only happen if (in_length%64 == 0) */
937 result2 = jlink_usb_read_emu_result(jlink);
938 if (1 != result2)
939 {
940 LOG_ERROR("jlink_usb_read_emu_result failed "
941 "(requested = 1, result=%d)", result2);
942 return ERROR_JTAG_DEVICE_ERROR;
943 }
944 }
945
946 /* Check the result itself */
947 result2 = usb_emu_result_buffer[0];
948 }
949 else
950 {
951 /* Save the result, then remove it from return value */
952 result2 = usb_in_buffer[result--];
953 }
954
955 if (result2)
956 {
957 LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
958 return ERROR_JTAG_DEVICE_ERROR;
959 }
960
961 return result;
962 }
963
964 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
965 static int usb_bulk_with_retries(
966 int (*f)(usb_dev_handle *, int, char *, int, int),
967 usb_dev_handle *dev, int ep,
968 char *bytes, int size, int timeout)
969 {
970 int tries = 3, count = 0;
971
972 while (tries && (count < size))
973 {
974 int result = f(dev, ep, bytes + count, size - count, timeout);
975 if (result > 0)
976 count += result;
977 else if ((-ETIMEDOUT != result) || !--tries)
978 return result;
979 }
980 return count;
981 }
982
983 static int wrap_usb_bulk_write(usb_dev_handle *dev, int ep,
984 char *buff, int size, int timeout)
985 {
986 /* usb_bulk_write() takes const char *buff */
987 return usb_bulk_write(dev, ep, buff, size, timeout);
988 }
989
990 static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
991 char *bytes, int size, int timeout)
992 {
993 return usb_bulk_with_retries(&wrap_usb_bulk_write,
994 dev, ep, bytes, size, timeout);
995 }
996
997 static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
998 char *bytes, int size, int timeout)
999 {
1000 return usb_bulk_with_retries(&usb_bulk_read,
1001 dev, ep, bytes, size, timeout);
1002 }
1003
1004 /* Write data from out_buffer to USB. */
1005 static int jlink_usb_write(struct jlink *jlink, int out_length)
1006 {
1007 int result;
1008
1009 if (out_length > JLINK_OUT_BUFFER_SIZE)
1010 {
1011 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
1012 return -1;
1013 }
1014
1015 result = usb_bulk_write_ex(jlink->usb_handle, jlink_write_ep,
1016 (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
1017
1018 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
1019
1020 #ifdef _DEBUG_USB_COMMS_
1021 jlink_debug_buffer(usb_out_buffer, out_length);
1022 #endif
1023 return result;
1024 }
1025
1026 /* Read data from USB into in_buffer. */
1027 static int jlink_usb_read(struct jlink *jlink, int expected_size)
1028 {
1029 int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
1030 (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
1031
1032 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
1033
1034 #ifdef _DEBUG_USB_COMMS_
1035 jlink_debug_buffer(usb_in_buffer, result);
1036 #endif
1037 return result;
1038 }
1039
1040 /* Read the result from the previous EMU cmd into result_buffer. */
1041 static int jlink_usb_read_emu_result(struct jlink *jlink)
1042 {
1043 int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
1044 (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
1045 JLINK_USB_TIMEOUT);
1046
1047 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
1048
1049 #ifdef _DEBUG_USB_COMMS_
1050 jlink_debug_buffer(usb_emu_result_buffer, result);
1051 #endif
1052 return result;
1053 }
1054
1055 #ifdef _DEBUG_USB_COMMS_
1056 #define BYTES_PER_LINE 16
1057
1058 static void jlink_debug_buffer(uint8_t *buffer, int length)
1059 {
1060 char line[81];
1061 char s[4];
1062 int i;
1063 int j;
1064
1065 for (i = 0; i < length; i += BYTES_PER_LINE)
1066 {
1067 snprintf(line, 5, "%04x", i);
1068 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1069 {
1070 snprintf(s, 4, " %02x", buffer[j]);
1071 strcat(line, s);
1072 }
1073 LOG_DEBUG("%s", line);
1074 }
1075 }
1076 #endif
1077

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)