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