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

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)