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

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)