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