Zach Welch <zw@superlucidity.net> fix jlink reset
[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 "replacements.h"
29
30 #include "jtag.h"
31
32 #include <usb.h>
33 #include <string.h>
34
35 #include "log.h"
36
37 #define VID 0x1366
38 #define PID 0x0101
39
40 #define JLINK_WRITE_ENDPOINT 0x02
41 #define JLINK_READ_ENDPOINT 0x81
42
43 #define JLINK_USB_TIMEOUT 1000
44
45 // See Section 1.3.2 of the Segger JLink USB protocol manual
46 #define JLINK_IN_BUFFER_SIZE 2048
47 #define JLINK_OUT_BUFFER_SIZE 2048
48 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
49
50 /* Global USB buffers */
51 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
52 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
53 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
54
55 /* Constants for JLink command */
56 #define EMU_CMD_VERSION 0x01
57 #define EMU_CMD_SET_SPEED 0x05
58 #define EMU_CMD_GET_STATE 0x07
59 #define EMU_CMD_HW_JTAG3 0xcf
60 #define EMU_CMD_HW_RESET0 0xdc
61 #define EMU_CMD_HW_RESET1 0xdd
62 #define EMU_CMD_HW_TRST0 0xde
63 #define EMU_CMD_HW_TRST1 0xdf
64
65 /* max speed 12MHz v5.0 jlink */
66 #define JLINK_MAX_SPEED 12000
67
68 /* External interface functions */
69 static int jlink_execute_queue(void);
70 static int jlink_speed(int speed);
71 static int jlink_speed_div(int speed, int* khz);
72 static int jlink_khz(int khz, int *jtag_speed);
73 static int jlink_register_commands(struct command_context_s *cmd_ctx);
74 static int jlink_init(void);
75 static int jlink_quit(void);
76
77 /* CLI command handler functions */
78 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79
80 /* Queue command functions */
81 static void jlink_end_state(tap_state_t state);
82 static void jlink_state_move(void);
83 static void jlink_path_move(int num_states, tap_state_t *path);
84 static void jlink_runtest(int num_cycles);
85 static void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
86 static void jlink_reset(int trst, int srst);
87 static void jlink_simple_command(u8 command);
88 static int jlink_get_status(void);
89
90 /* J-Link tap buffer functions */
91 static void jlink_tap_init(void);
92 static int jlink_tap_execute(void);
93 static void jlink_tap_ensure_space(int scans, int bits);
94 static void jlink_tap_append_step(int tms, int tdi);
95 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
96
97 /* Jlink lowlevel functions */
98 typedef struct jlink_jtag
99 {
100 struct usb_dev_handle* usb_handle;
101 } jlink_jtag_t;
102
103 static jlink_jtag_t *jlink_usb_open(void);
104 static void jlink_usb_close(jlink_jtag_t *jlink_jtag);
105 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
106 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
107 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size);
108 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
109
110 /* helper functions */
111 static int jlink_get_version_info(void);
112
113 #ifdef _DEBUG_USB_COMMS_
114 static void jlink_debug_buffer(u8 *buffer, int length);
115 #endif
116
117 static jlink_jtag_t* jlink_jtag_handle;
118
119 /***************************************************************************/
120 /* External interface implementation */
121
122 jtag_interface_t jlink_interface =
123 {
124 .name = "jlink",
125 .execute_queue = jlink_execute_queue,
126 .speed = jlink_speed,
127 .speed_div = jlink_speed_div,
128 .khz = jlink_khz,
129 .register_commands = jlink_register_commands,
130 .init = jlink_init,
131 .quit = jlink_quit
132 };
133
134 static void jlink_execute_end_state(jtag_command_t *cmd)
135 {
136 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
137
138 if (cmd->cmd.end_state->end_state != TAP_INVALID)
139 jlink_end_state(cmd->cmd.end_state->end_state);
140 }
141
142 static void jlink_execute_runtest(jtag_command_t *cmd)
143 {
144 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
145 cmd->cmd.runtest->num_cycles,
146 cmd->cmd.runtest->end_state);
147
148 if (cmd->cmd.runtest->end_state != TAP_INVALID)
149 jlink_end_state(cmd->cmd.runtest->end_state);
150
151 jlink_runtest(cmd->cmd.runtest->num_cycles);
152 }
153
154 static void jlink_execute_statemove(jtag_command_t *cmd)
155 {
156 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
157
158 if (cmd->cmd.statemove->end_state != TAP_INVALID)
159 {
160 jlink_end_state(cmd->cmd.statemove->end_state);
161 }
162 jlink_state_move();
163 }
164
165 static void jlink_execute_pathmove(jtag_command_t *cmd)
166 {
167 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
168 cmd->cmd.pathmove->num_states,
169 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
170
171 jlink_path_move(cmd->cmd.pathmove->num_states,
172 cmd->cmd.pathmove->path);
173 }
174
175 static void jlink_execute_scan(jtag_command_t *cmd)
176 {
177 int scan_size;
178 enum scan_type type;
179 u8 *buffer;
180
181 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
182
183 if (cmd->cmd.scan->end_state != TAP_INVALID)
184 jlink_end_state(cmd->cmd.scan->end_state);
185
186 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
187 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
188
189 #ifdef _DEBUG_USB_COMMS_
190 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
191 #endif
192 type = jtag_scan_type(cmd->cmd.scan);
193 jlink_scan(cmd->cmd.scan->ir_scan,
194 type, buffer, scan_size, cmd->cmd.scan);
195 }
196
197 static void jlink_execute_reset(jtag_command_t *cmd)
198 {
199 DEBUG_JTAG_IO("reset trst: %i srst %i",
200 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
201
202 jlink_tap_execute();
203
204 if (cmd->cmd.reset->trst == 1)
205 tap_set_state(TAP_RESET);
206
207 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
208 }
209
210 static void jlink_execute_sleep(jtag_command_t *cmd)
211 {
212 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
213 jlink_tap_execute();
214 jtag_sleep(cmd->cmd.sleep->us);
215 }
216
217 static void jlink_execute_command(jtag_command_t *cmd)
218 {
219 switch (cmd->type)
220 {
221 case JTAG_END_STATE: jlink_execute_end_state(cmd); break;
222 case JTAG_RUNTEST: jlink_execute_runtest(cmd); break;
223 case JTAG_STATEMOVE: jlink_execute_statemove(cmd); break;
224 case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break;
225 case JTAG_SCAN: jlink_execute_scan(cmd); break;
226 case JTAG_RESET: jlink_execute_reset(cmd); break;
227 case JTAG_SLEEP: jlink_execute_sleep(cmd); break;
228 default:
229 LOG_ERROR("BUG: unknown JTAG command type encountered");
230 exit(-1);
231 }
232 }
233
234 static int jlink_execute_queue(void)
235 {
236 jtag_command_t *cmd = jtag_command_queue;
237
238 while (cmd != NULL)
239 {
240 jlink_execute_command(cmd);
241 cmd = cmd->next;
242 }
243
244 return jlink_tap_execute();
245 }
246
247 /* Sets speed in kHz. */
248 static int jlink_speed(int speed)
249 {
250 int result;
251
252 if (speed > JLINK_MAX_SPEED)
253 {
254 LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
255 speed, JLINK_MAX_SPEED);
256 return ERROR_OK;
257 }
258
259 /* check for RTCK setting */
260 if (speed == 0)
261 speed = -1;
262
263 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
264 usb_out_buffer[1] = (speed >> 0) & 0xff;
265 usb_out_buffer[2] = (speed >> 8) & 0xff;
266
267 result = jlink_usb_write(jlink_jtag_handle, 3);
268 if (result != 3)
269 {
270 LOG_ERROR("J-Link setting speed failed (%d)", result);
271 return ERROR_JTAG_DEVICE_ERROR;
272 }
273
274 return ERROR_OK;
275 }
276
277 static int jlink_speed_div(int speed, int* khz)
278 {
279 *khz = speed;
280
281 return ERROR_OK;
282 }
283
284 static int jlink_khz(int khz, int *jtag_speed)
285 {
286 *jtag_speed = khz;
287
288 return ERROR_OK;
289 }
290
291 static int jlink_register_commands(struct command_context_s *cmd_ctx)
292 {
293 register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
294 "query jlink info");
295 return ERROR_OK;
296 }
297
298 static int jlink_init(void)
299 {
300 int check_cnt;
301
302 jlink_jtag_handle = jlink_usb_open();
303
304 if (jlink_jtag_handle == 0)
305 {
306 LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
307 return ERROR_JTAG_INIT_FAILED;
308 }
309
310 check_cnt = 0;
311 while (check_cnt < 3)
312 {
313 if (jlink_get_version_info() == ERROR_OK)
314 {
315 /* attempt to get status */
316 jlink_get_status();
317 break;
318 }
319
320 check_cnt++;
321 }
322
323 if (check_cnt == 3)
324 {
325 LOG_INFO("J-Link initial read failed, don't worry");
326 }
327
328 LOG_INFO("J-Link JTAG Interface ready");
329
330 jlink_reset(0, 0);
331 jlink_tap_init();
332
333 return ERROR_OK;
334 }
335
336 static int jlink_quit(void)
337 {
338 jlink_usb_close(jlink_jtag_handle);
339 return ERROR_OK;
340 }
341
342 /***************************************************************************/
343 /* Queue command implementations */
344
345 static void jlink_end_state(tap_state_t state)
346 {
347 if (tap_is_state_stable(state))
348 {
349 tap_set_end_state(state);
350 }
351 else
352 {
353 LOG_ERROR("BUG: %i is not a valid end state", state);
354 exit(-1);
355 }
356 }
357
358 /* Goes to the end state. */
359 static void jlink_state_move(void)
360 {
361 int i;
362 int tms = 0;
363 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
364
365 for (i = 0; i < 7; i++)
366 {
367 tms = (tms_scan >> i) & 1;
368 jlink_tap_append_step(tms, 0);
369 }
370
371 tap_set_state(tap_get_end_state());
372 }
373
374 static void jlink_path_move(int num_states, tap_state_t *path)
375 {
376 int i;
377
378 for (i = 0; i < num_states; i++)
379 {
380 if (path[i] == tap_state_transition(tap_get_state(), false))
381 {
382 jlink_tap_append_step(0, 0);
383 }
384 else if (path[i] == tap_state_transition(tap_get_state(), true))
385 {
386 jlink_tap_append_step(1, 0);
387 }
388 else
389 {
390 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
391 exit(-1);
392 }
393
394 tap_set_state(path[i]);
395 }
396
397 tap_set_end_state(tap_get_state());
398 }
399
400 static void jlink_runtest(int num_cycles)
401 {
402 int i;
403
404 tap_state_t saved_end_state = tap_get_end_state();
405
406 /* only do a state_move when we're not already in IDLE */
407 if (tap_get_state() != TAP_IDLE)
408 {
409 jlink_end_state(TAP_IDLE);
410 jlink_state_move();
411 }
412
413 /* execute num_cycles */
414 for (i = 0; i < num_cycles; i++)
415 {
416 jlink_tap_append_step(0, 0);
417 }
418
419 /* finish in end_state */
420 jlink_end_state(saved_end_state);
421 if (tap_get_state() != tap_get_end_state())
422 {
423 jlink_state_move();
424 }
425 }
426
427 static void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
428 {
429 tap_state_t saved_end_state;
430
431 jlink_tap_ensure_space(1, scan_size + 8);
432
433 saved_end_state = tap_get_end_state();
434
435 /* Move to appropriate scan state */
436 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
437
438 /* Only move if we're not already there */
439 if (tap_get_state() != tap_get_end_state())
440 jlink_state_move();
441
442 jlink_end_state(saved_end_state);
443
444 /* Scan */
445 jlink_tap_append_scan(scan_size, buffer, command);
446
447 /* We are in Exit1, go to Pause */
448 jlink_tap_append_step(0, 0);
449
450 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
451
452 if (tap_get_state() != tap_get_end_state())
453 {
454 jlink_state_move();
455 }
456 }
457
458 static void jlink_reset(int trst, int srst)
459 {
460 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
461
462 /* Signals are active low */
463 if (srst == 0)
464 {
465 jlink_simple_command(EMU_CMD_HW_RESET1);
466 jlink_end_state(TAP_RESET);
467 jlink_state_move();
468 }
469 else if (srst == 1)
470 {
471 jlink_simple_command(EMU_CMD_HW_RESET0);
472 }
473
474 if (trst == 0)
475 {
476 jlink_simple_command(EMU_CMD_HW_TRST1);
477 jlink_end_state(TAP_RESET);
478 jlink_state_move();
479 }
480 else if (trst == 1)
481 {
482 jlink_simple_command(EMU_CMD_HW_TRST0);
483 }
484 }
485
486 static void jlink_simple_command(u8 command)
487 {
488 int result;
489
490 DEBUG_JTAG_IO("0x%02x", command);
491
492 usb_out_buffer[0] = command;
493 result = jlink_usb_write(jlink_jtag_handle, 1);
494
495 if (result != 1)
496 {
497 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
498 }
499 }
500
501 static int jlink_get_status(void)
502 {
503 int result;
504
505 jlink_simple_command(EMU_CMD_GET_STATE);
506
507 result = jlink_usb_read(jlink_jtag_handle, 8);
508 if (result != 8)
509 {
510 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
511 return ERROR_JTAG_DEVICE_ERROR;
512 }
513
514 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
515 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
516 vref / 1000, vref % 1000, \
517 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
518 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
519
520 if (vref < 1500)
521 LOG_ERROR("Vref too low. Check Target Power\n");
522
523 return ERROR_OK;
524 }
525
526 static int jlink_get_version_info(void)
527 {
528 int result;
529 int len;
530
531 /* query hardware version */
532 jlink_simple_command(EMU_CMD_VERSION);
533
534 result = jlink_usb_read(jlink_jtag_handle, 2);
535 if (2 != result)
536 {
537 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
538 result);
539 return ERROR_JTAG_DEVICE_ERROR;
540 }
541
542 len = buf_get_u32(usb_in_buffer, 0, 16);
543 result = jlink_usb_read(jlink_jtag_handle, len);
544 if (result != len)
545 {
546 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
547 result);
548 return ERROR_JTAG_DEVICE_ERROR;
549 }
550
551 usb_in_buffer[result] = 0;
552 LOG_INFO("%s", (char *)usb_in_buffer);
553
554 return ERROR_OK;
555 }
556
557 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
558 {
559 if (jlink_get_version_info() == ERROR_OK)
560 {
561 /* attempt to get status */
562 jlink_get_status();
563 }
564
565 return ERROR_OK;
566 }
567
568 /***************************************************************************/
569 /* J-Link tap functions */
570
571 /* 2048 is the max value we can use here */
572 #define JLINK_TAP_BUFFER_SIZE 2048
573
574 static unsigned tap_length;
575 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
576 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
577 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
578
579 typedef struct
580 {
581 int first; /* First bit position in tdo_buffer to read */
582 int length; /* Number of bits to read */
583 scan_command_t *command; /* Corresponding scan command */
584 u8 *buffer;
585 } pending_scan_result_t;
586
587 #define MAX_PENDING_SCAN_RESULTS 256
588
589 static int pending_scan_results_length;
590 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
591
592 static int last_tms;
593
594 static void jlink_tap_init(void)
595 {
596 tap_length = 0;
597 pending_scan_results_length = 0;
598 }
599
600 static void jlink_tap_ensure_space(int scans, int bits)
601 {
602 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
603 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
604
605 if (scans > available_scans || bits > available_bits)
606 {
607 jlink_tap_execute();
608 }
609 }
610
611 static void jlink_tap_append_step(int tms, int tdi)
612 {
613 last_tms = tms;
614 int index = tap_length / 8;
615
616 if (index >= JLINK_TAP_BUFFER_SIZE)
617 {
618 LOG_ERROR("jlink_tap_append_step: overflow");
619 exit(-1);
620 }
621
622 int bit_index = tap_length % 8;
623 u8 bit = 1 << bit_index;
624
625 if (tms)
626 tms_buffer[index] |= bit;
627 else
628 tms_buffer[index] &= ~bit;
629
630 if (tdi)
631 tdi_buffer[index] |= bit;
632 else
633 tdi_buffer[index] &= ~bit;
634
635 tap_length++;
636 }
637
638 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
639 {
640 pending_scan_result_t *pending_scan_result =
641 &pending_scan_results_buffer[pending_scan_results_length];
642 int i;
643
644 pending_scan_result->first = tap_length;
645 pending_scan_result->length = length;
646 pending_scan_result->command = command;
647 pending_scan_result->buffer = buffer;
648
649 for (i = 0; i < length; i++)
650 {
651 int tms = i < length - 1 ? 0 : 1;
652 int tdi = buffer[i / 8] & (1 << (i % 8));
653 jlink_tap_append_step(tms, tdi);
654 }
655 pending_scan_results_length++;
656 }
657
658 /* Pad and send a tap sequence to the device, and receive the answer.
659 * For the purpose of padding we assume that we are in idle or pause state. */
660 static int jlink_tap_execute(void)
661 {
662 int byte_length;
663 int tms_offset;
664 int tdi_offset;
665 int i;
666 int result;
667
668 if (!tap_length)
669 return ERROR_OK;
670
671 /* Pad last byte so that tap_length is divisible by 8 */
672 while (tap_length % 8 != 0)
673 {
674 /* More of the last TMS value keeps us in the same state,
675 * analogous to free-running JTAG interfaces. */
676 jlink_tap_append_step(last_tms, 0);
677 }
678
679 byte_length = tap_length / 8;
680
681 usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
682 usb_out_buffer[1] = 0;
683 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
684 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
685
686 tms_offset = 4;
687 for (i = 0; i < byte_length; i++)
688 {
689 usb_out_buffer[tms_offset + i] = tms_buffer[i];
690 }
691
692 tdi_offset = tms_offset + byte_length;
693 for (i = 0; i < byte_length; i++)
694 {
695 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
696 }
697
698 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
699
700 if (result != byte_length)
701 {
702 LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
703 result, byte_length);
704 return ERROR_JTAG_QUEUE_FAILED;
705 }
706
707 for (i = 0; i < byte_length; i++)
708 tdo_buffer[i] = usb_in_buffer[i];
709
710 for (i = 0; i < pending_scan_results_length; i++)
711 {
712 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
713 u8 *buffer = pending_scan_result->buffer;
714 int length = pending_scan_result->length;
715 int first = pending_scan_result->first;
716 scan_command_t *command = pending_scan_result->command;
717
718 /* Copy to buffer */
719 buf_set_buf(tdo_buffer, first, buffer, 0, length);
720
721 DEBUG_JTAG_IO("pending scan result, length = %d", length);
722
723 #ifdef _DEBUG_USB_COMMS_
724 jlink_debug_buffer(buffer, byte_length);
725 #endif
726
727 if (jtag_read_buffer(buffer, command) != ERROR_OK)
728 {
729 jlink_tap_init();
730 return ERROR_JTAG_QUEUE_FAILED;
731 }
732
733 if (pending_scan_result->buffer != NULL)
734 {
735 free(pending_scan_result->buffer);
736 }
737 }
738
739 jlink_tap_init();
740
741 return ERROR_OK;
742 }
743
744 /*****************************************************************************/
745 /* JLink USB low-level functions */
746
747 static jlink_jtag_t* jlink_usb_open()
748 {
749 struct usb_bus *busses;
750 struct usb_bus *bus;
751 struct usb_device *dev;
752
753 jlink_jtag_t *result;
754
755 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
756
757 usb_init();
758 usb_find_busses();
759 usb_find_devices();
760
761 busses = usb_get_busses();
762
763 /* find jlink_jtag device in usb bus */
764
765 for (bus = busses; bus; bus = bus->next)
766 {
767 for (dev = bus->devices; dev; dev = dev->next)
768 {
769 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
770 {
771 result->usb_handle = usb_open(dev);
772
773 /* usb_set_configuration required under win32 */
774 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
775 usb_claim_interface(result->usb_handle, 0);
776
777 #if 0
778 /*
779 * This makes problems under Mac OS X. And is not needed
780 * under Windows. Hopefully this will not break a linux build
781 */
782 usb_set_altinterface(result->usb_handle, 0);
783 #endif
784 return result;
785 }
786 }
787 }
788
789 free(result);
790 return NULL;
791 }
792
793 static void jlink_usb_close(jlink_jtag_t *jlink_jtag)
794 {
795 usb_close(jlink_jtag->usb_handle);
796 free(jlink_jtag);
797 }
798
799 /* Send a message and receive the reply. */
800 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
801 {
802 int result;
803 int result2;
804
805 result = jlink_usb_write(jlink_jtag, out_length);
806 if (result != out_length)
807 {
808 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
809 out_length, result);
810 return ERROR_JTAG_DEVICE_ERROR;
811 }
812
813 result = jlink_usb_read(jlink_jtag, in_length);
814 if ((result != in_length) && (result != in_length + 1))
815 {
816 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
817 in_length, result);
818 return ERROR_JTAG_DEVICE_ERROR;
819 }
820
821 if (result == in_length)
822 {
823 /* Must read the result from the EMU too */
824 result2 = jlink_usb_read_emu_result(jlink_jtag);
825 if (1 != result2)
826 {
827 LOG_ERROR("jlink_usb_read_emu_result failed "
828 "(requested=1, result=%d)", result2);
829 return ERROR_JTAG_DEVICE_ERROR;
830 }
831
832 /* Check the result itself */
833 result2 = usb_emu_result_buffer[0];
834 }
835 else
836 {
837 /* Save the result, then remove it from return value */
838 result2 = usb_in_buffer[result--];
839 }
840
841 if (result2)
842 {
843 LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
844 return ERROR_JTAG_DEVICE_ERROR;
845 }
846
847 return result;
848 }
849
850 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
851 static int usb_bulk_with_retries(
852 int (*f)(usb_dev_handle *, int, char *, int, int),
853 usb_dev_handle *dev, int ep,
854 char *bytes, int size, int timeout)
855 {
856 int rc = 0, tries = 3, this_size;
857
858 while (tries && size) {
859
860 this_size = f(dev, ep, bytes, size, timeout);
861 if (this_size > 0) {
862
863 size -= this_size;
864 rc += this_size;
865 bytes += this_size;
866
867 } else
868 tries --;
869 }
870 return rc;
871 }
872 static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
873 char *bytes, int size, int timeout)
874 {
875 return usb_bulk_with_retries(&usb_bulk_write,
876 dev, ep, bytes, size, timeout);
877 }
878 static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
879 char *bytes, int size, int timeout)
880 {
881 return usb_bulk_with_retries(&usb_bulk_read,
882 dev, ep, bytes, size, timeout);
883 }
884
885 /* Write data from out_buffer to USB. */
886 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
887 {
888 int result;
889
890 if (out_length > JLINK_OUT_BUFFER_SIZE)
891 {
892 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
893 return -1;
894 }
895
896 result = usb_bulk_write_ex(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT,
897 (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
898
899 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
900
901 #ifdef _DEBUG_USB_COMMS_
902 jlink_debug_buffer(usb_out_buffer, out_length);
903 #endif
904 return result;
905 }
906
907 /* Read data from USB into in_buffer. */
908 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size)
909 {
910 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT,
911 (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
912
913 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
914
915 #ifdef _DEBUG_USB_COMMS_
916 jlink_debug_buffer(usb_in_buffer, result);
917 #endif
918 return result;
919 }
920
921 /* Read the result from the previous EMU cmd into result_buffer. */
922 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
923 {
924 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT,
925 (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
926 JLINK_USB_TIMEOUT);
927
928 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
929
930 #ifdef _DEBUG_USB_COMMS_
931 jlink_debug_buffer(usb_emu_result_buffer, result);
932 #endif
933 return result;
934 }
935
936 #ifdef _DEBUG_USB_COMMS_
937 #define BYTES_PER_LINE 16
938
939 static void jlink_debug_buffer(u8 *buffer, int length)
940 {
941 char line[81];
942 char s[4];
943 int i;
944 int j;
945
946 for (i = 0; i < length; i += BYTES_PER_LINE)
947 {
948 snprintf(line, 5, "%04x", i);
949 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
950 {
951 snprintf(s, 4, " %02x", buffer[j]);
952 strcat(line, s);
953 }
954 LOG_DEBUG("%s", line);
955 }
956 }
957 #endif

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)