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

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)