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

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)