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

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)