a997956cacfd368fd82c60b29c53e19f6685adec
[openocd.git] / src / jtag / arm-jtag-ew.c
1 // vim:ts=4 sw=4:
2
3 /***************************************************************************
4 * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
5 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #define INCLUDE_JTAG_INTERFACE_H
28 #include "jtag.h"
29 #include <usb.h>
30
31
32 #define USB_VID 0x15ba
33 #define USB_PID 0x001e
34
35 #define ARMJTAGEW_EPT_BULK_OUT 0x01u
36 #define ARMJTAGEW_EPT_BULK_IN 0x82u
37
38 #define ARMJTAGEW_USB_TIMEOUT 2000
39
40 #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
41 #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
42
43
44 /* USB command request codes. */
45 #define CMD_GET_VERSION 0x00
46 #define CMD_SELECT_DPIMPL 0x10
47 #define CMD_SET_TCK_FREQUENCY 0x11
48 #define CMD_GET_TCK_FREQUENCY 0x12
49 #define CMD_MEASURE_MAX_TCK_FREQ 0x15
50 #define CMD_MEASURE_RTCK_RESPONSE 0x16
51 #define CMD_TAP_SHIFT 0x17
52 #define CMD_SET_TAPHW_STATE 0x20
53 #define CMD_GET_TAPHW_STATE 0x21
54 #define CMD_TGPWR_SETUP 0x22
55
56 /* Global USB buffers */
57 static u8 usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
58 static u8 usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
59
60 /* External interface functions */
61 static int armjtagew_execute_queue(void);
62 static int armjtagew_speed(int speed);
63 static int armjtagew_khz(int khz, int *jtag_speed);
64 static int armjtagew_register_commands(struct command_context_s *cmd_ctx);
65 static int armjtagew_init(void);
66 static int armjtagew_quit(void);
67
68 /* CLI command handler functions */
69 static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70
71 /* Queue command functions */
72 static void armjtagew_end_state(tap_state_t state);
73 static void armjtagew_state_move(void);
74 static void armjtagew_path_move(int num_states, tap_state_t *path);
75 static void armjtagew_runtest(int num_cycles);
76 static void armjtagew_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
77 static void armjtagew_reset(int trst, int srst);
78 //static void armjtagew_simple_command(u8 command);
79 static int armjtagew_get_status(void);
80
81 /* tap buffer functions */
82 static void armjtagew_tap_init(void);
83 static int armjtagew_tap_execute(void);
84 static void armjtagew_tap_ensure_space(int scans, int bits);
85 static void armjtagew_tap_append_step(int tms, int tdi);
86 static void armjtagew_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
87
88 /* ARM-JTAG-EW lowlevel functions */
89 typedef struct armjtagew_jtag
90 {
91 struct usb_dev_handle* usb_handle;
92 } armjtagew_jtag_t;
93
94 static armjtagew_jtag_t *armjtagew_usb_open(void);
95 static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag);
96 static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length);
97 static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length);
98 static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length);
99
100 /* helper functions */
101 static int armjtagew_get_version_info(void);
102
103 #ifdef _DEBUG_USB_COMMS_
104 static void armjtagew_debug_buffer(u8 *buffer, int length);
105 #endif
106
107 static armjtagew_jtag_t* armjtagew_jtag_handle;
108
109
110
111 /***************************************************************************/
112 /* External interface implementation */
113
114 jtag_interface_t armjtagew_interface =
115 {
116 .name = "arm-jtag-ew",
117 .execute_queue = armjtagew_execute_queue,
118 .speed = armjtagew_speed,
119 .khz = armjtagew_khz,
120 .register_commands = armjtagew_register_commands,
121 .init = armjtagew_init,
122 .quit = armjtagew_quit
123 };
124
125
126 static int armjtagew_execute_queue(void)
127 {
128 jtag_command_t *cmd = jtag_command_queue;
129 int scan_size;
130 enum scan_type type;
131 u8 *buffer;
132
133 while (cmd != NULL)
134 {
135 switch (cmd->type)
136 {
137 case JTAG_RUNTEST:
138 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
139 cmd->cmd.runtest->end_state);
140
141 if (cmd->cmd.runtest->end_state != TAP_INVALID)
142 {
143 armjtagew_end_state(cmd->cmd.runtest->end_state);
144 }
145 armjtagew_runtest(cmd->cmd.runtest->num_cycles);
146 break;
147
148 case JTAG_STATEMOVE:
149 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
150
151 if (cmd->cmd.statemove->end_state != TAP_INVALID)
152 {
153 armjtagew_end_state(cmd->cmd.statemove->end_state);
154 }
155 armjtagew_state_move();
156 break;
157
158 case JTAG_PATHMOVE:
159 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
160 cmd->cmd.pathmove->num_states, \
161 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
162
163 armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
164 break;
165
166 case JTAG_SCAN:
167 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
168
169 if (cmd->cmd.scan->end_state != TAP_INVALID)
170 {
171 armjtagew_end_state(cmd->cmd.scan->end_state);
172 }
173
174 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
175 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
176
177 #ifdef _DEBUG_USB_COMMS_
178 armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
179 #endif
180 type = jtag_scan_type(cmd->cmd.scan);
181 armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
182 break;
183
184 case JTAG_RESET:
185 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
186
187 armjtagew_tap_execute();
188
189 if (cmd->cmd.reset->trst == 1)
190 {
191 tap_set_state(TAP_RESET);
192 }
193 armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
194 break;
195
196 case JTAG_SLEEP:
197 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
198 armjtagew_tap_execute();
199 jtag_sleep(cmd->cmd.sleep->us);
200 break;
201
202 default:
203 LOG_ERROR("BUG: unknown JTAG command type encountered");
204 exit(-1);
205 }
206 cmd = cmd->next;
207 }
208
209 return armjtagew_tap_execute();
210 }
211
212
213 /* Sets speed in kHz. */
214 static int armjtagew_speed(int speed)
215 {
216 int result;
217 int speed_real;
218
219
220 usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
221 buf_set_u32(usb_out_buffer+1, 0, 32, speed);
222
223 result = armjtagew_usb_message(armjtagew_jtag_handle, 4, 4);
224
225 if (result < 0)
226 {
227 LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
228 return ERROR_JTAG_DEVICE_ERROR;
229 }
230
231 usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
232 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4);
233 speed_real = (int)buf_get_u32(usb_in_buffer,0,32);
234 if(result < 0)
235 {
236 LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
237 return ERROR_JTAG_DEVICE_ERROR;
238 }
239 else
240 {
241 LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
242 }
243
244 return ERROR_OK;
245 }
246
247
248 static int armjtagew_khz(int khz, int *jtag_speed)
249 {
250 *jtag_speed = khz;
251
252 return ERROR_OK;
253 }
254
255 static int armjtagew_register_commands(struct command_context_s *cmd_ctx)
256 {
257 register_command(cmd_ctx, NULL, "armjtagew_info", armjtagew_handle_armjtagew_info_command, COMMAND_EXEC,
258 "query armjtagew info");
259 return ERROR_OK;
260 }
261
262 static int armjtagew_init(void)
263 {
264 int check_cnt;
265
266 armjtagew_jtag_handle = armjtagew_usb_open();
267
268 if (armjtagew_jtag_handle == 0)
269 {
270 LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
271 return ERROR_JTAG_INIT_FAILED;
272 }
273
274 check_cnt = 0;
275 while (check_cnt < 3)
276 {
277 if (armjtagew_get_version_info() == ERROR_OK)
278 {
279 /* attempt to get status */
280 armjtagew_get_status();
281 break;
282 }
283
284 check_cnt++;
285 }
286
287 if (check_cnt == 3)
288 {
289 LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
290 }
291
292 LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
293
294 armjtagew_reset(0, 0);
295 armjtagew_tap_init();
296
297 return ERROR_OK;
298 }
299
300 static int armjtagew_quit(void)
301 {
302 armjtagew_usb_close(armjtagew_jtag_handle);
303 return ERROR_OK;
304 }
305
306 /***************************************************************************/
307 /* Queue command implementations */
308
309 static void armjtagew_end_state(tap_state_t state)
310 {
311 if (tap_is_state_stable(state))
312 {
313 tap_set_end_state(state);
314 }
315 else
316 {
317 LOG_ERROR("BUG: %i is not a valid end state", state);
318 exit(-1);
319 }
320 }
321
322 /* Goes to the end state. */
323 static void armjtagew_state_move(void)
324 {
325 int i;
326 int tms = 0;
327 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
328 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
329
330 for (i = 0; i < tms_count; i++)
331 {
332 tms = (tms_scan >> i) & 1;
333 armjtagew_tap_append_step(tms, 0);
334 }
335
336 tap_set_state(tap_get_end_state());
337 }
338
339 static void armjtagew_path_move(int num_states, tap_state_t *path)
340 {
341 int i;
342
343 for (i = 0; i < num_states; i++)
344 {
345 /*
346 * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
347 * Either handle that here, or update the documentation with examples
348 * how to fix that in the configuration files.
349 */
350 if (path[i] == tap_state_transition(tap_get_state(), false))
351 {
352 armjtagew_tap_append_step(0, 0);
353 }
354 else if (path[i] == tap_state_transition(tap_get_state(), true))
355 {
356 armjtagew_tap_append_step(1, 0);
357 }
358 else
359 {
360 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
361 exit(-1);
362 }
363
364 tap_set_state(path[i]);
365 }
366
367 tap_set_end_state(tap_get_state());
368 }
369
370 static void armjtagew_runtest(int num_cycles)
371 {
372 int i;
373
374 tap_state_t saved_end_state = tap_get_end_state();
375
376 /* only do a state_move when we're not already in IDLE */
377 if (tap_get_state() != TAP_IDLE)
378 {
379 armjtagew_end_state(TAP_IDLE);
380 armjtagew_state_move();
381 }
382
383 /* execute num_cycles */
384 for (i = 0; i < num_cycles; i++)
385 {
386 armjtagew_tap_append_step(0, 0);
387 }
388
389 /* finish in end_state */
390 armjtagew_end_state(saved_end_state);
391 if (tap_get_state() != tap_get_end_state())
392 {
393 armjtagew_state_move();
394 }
395 }
396
397 static void armjtagew_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
398 {
399 tap_state_t saved_end_state;
400
401 armjtagew_tap_ensure_space(1, scan_size + 8);
402
403 saved_end_state = tap_get_end_state();
404
405 /* Move to appropriate scan state */
406 armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
407
408 armjtagew_state_move();
409 armjtagew_end_state(saved_end_state);
410
411 /* Scan */
412 armjtagew_tap_append_scan(scan_size, buffer, command);
413
414 /* We are in Exit1, go to Pause */
415 armjtagew_tap_append_step(0, 0);
416
417 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
418
419 if (tap_get_state() != tap_get_end_state())
420 {
421 armjtagew_state_move();
422 }
423 }
424
425 static void armjtagew_reset(int trst, int srst)
426 {
427 const u8 trst_mask = (1u<<5);
428 const u8 srst_mask = (1u<<6);
429 u8 val = 0;
430 u8 outp_en = 0;
431 u8 change_mask = 0;
432 int result;
433
434 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
435
436 if (srst == 0)
437 {
438 val |= srst_mask;
439 outp_en &= ~srst_mask; /* tristate */
440 change_mask |= srst_mask;
441 }
442 else if (srst == 1)
443 {
444 val &= ~srst_mask;
445 outp_en |= srst_mask;
446 change_mask |= srst_mask;
447 }
448
449 if (trst == 0)
450 {
451 val |= trst_mask;
452 outp_en &= ~trst_mask; /* tristate */
453 change_mask |= trst_mask;
454 }
455 else if (trst == 1)
456 {
457 val &= ~trst_mask;
458 outp_en |= trst_mask;
459 change_mask |= trst_mask;
460 }
461
462 usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
463 usb_out_buffer[1] = val;
464 usb_out_buffer[2] = outp_en;
465 usb_out_buffer[3] = change_mask;
466 result = armjtagew_usb_write(armjtagew_jtag_handle, 4);
467 if (result != 4)
468 {
469 LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
470 }
471 }
472
473
474 static int armjtagew_get_status(void)
475 {
476 int result;
477
478 usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
479 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 12);
480
481 if (result == 0)
482 {
483 unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
484 LOG_INFO("U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s\n", \
485 buf_get_u32(usb_in_buffer + 0, 0, 16), \
486 buf_get_u32(usb_in_buffer + 2, 0, 16), \
487 buf_get_u32(usb_in_buffer + 4, 0, 16), \
488 buf_get_u32(usb_in_buffer + 6, 0, 16), \
489 usb_in_buffer[9], \
490 usb_in_buffer[11] ? "OVERCURRENT" : "OK", \
491 usb_in_buffer[10] ? "enabled" : "disabled");
492
493 if (u_tg < 1500)
494 {
495 LOG_ERROR("Vref too low. Check Target Power\n");
496 }
497 }
498 else
499 {
500 LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)\n", result);
501 }
502
503 return ERROR_OK;
504 }
505
506 static int armjtagew_get_version_info(void)
507 {
508 int result;
509 char sn[16];
510 char auxinfo[257];
511
512 /* query hardware version */
513 usb_out_buffer[0] = CMD_GET_VERSION;
514 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4+15+256);
515
516 if (result != 0)
517 {
518 LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)\n", result);
519 return ERROR_JTAG_DEVICE_ERROR;
520 }
521
522
523 memcpy(sn, usb_in_buffer+4, 15);
524 sn[15] = '\0';
525 memcpy(auxinfo, usb_in_buffer+4+15, 256);
526 auxinfo[256] = '\0';
527
528 LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
529 usb_in_buffer[1], usb_in_buffer[0], \
530 isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
531 sn, auxinfo);
532 return ERROR_OK;
533 }
534
535 static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
536 {
537 if (armjtagew_get_version_info() == ERROR_OK)
538 {
539 /* attempt to get status */
540 armjtagew_get_status();
541 }
542
543 return ERROR_OK;
544 }
545
546 /***************************************************************************/
547 /* ARM-JTAG-EW tap functions */
548
549 /* 2048 is the max value we can use here */
550 #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
551
552 static int tap_length;
553 static u8 tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
554 static u8 tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
555 static u8 tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
556
557 typedef struct
558 {
559 int first; /* First bit position in tdo_buffer to read */
560 int length; /* Number of bits to read */
561 scan_command_t *command; /* Corresponding scan command */
562 u8 *buffer;
563 } pending_scan_result_t;
564
565 #define MAX_PENDING_SCAN_RESULTS 256
566
567 static int pending_scan_results_length;
568 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
569
570 static int last_tms;
571
572 static void armjtagew_tap_init(void)
573 {
574 tap_length = 0;
575 pending_scan_results_length = 0;
576 }
577
578 static void armjtagew_tap_ensure_space(int scans, int bits)
579 {
580 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
581 int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
582
583 if (scans > available_scans || bits > available_bits)
584 {
585 armjtagew_tap_execute();
586 }
587 }
588
589 static void armjtagew_tap_append_step(int tms, int tdi)
590 {
591 last_tms = tms;
592 int index = tap_length / 8;
593
594 if (index < ARMJTAGEW_TAP_BUFFER_SIZE)
595 {
596 int bit_index = tap_length % 8;
597 u8 bit = 1 << bit_index;
598
599 if (tms)
600 {
601 tms_buffer[index] |= bit;
602 }
603 else
604 {
605 tms_buffer[index] &= ~bit;
606 }
607
608 if (tdi)
609 {
610 tdi_buffer[index] |= bit;
611 }
612 else
613 {
614 tdi_buffer[index] &= ~bit;
615 }
616
617 tap_length++;
618 }
619 else
620 {
621 LOG_ERROR("armjtagew_tap_append_step, overflow");
622 }
623 }
624
625 void armjtagew_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
626 {
627 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
628 int i;
629
630 pending_scan_result->first = tap_length;
631 pending_scan_result->length = length;
632 pending_scan_result->command = command;
633 pending_scan_result->buffer = buffer;
634
635 for (i = 0; i < length; i++)
636 {
637 armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
638 }
639 pending_scan_results_length++;
640 }
641
642 /* Pad and send a tap sequence to the device, and receive the answer.
643 * For the purpose of padding we assume that we are in idle or pause state. */
644 static int armjtagew_tap_execute(void)
645 {
646 int byte_length;
647 int tms_offset;
648 int tdi_offset;
649 int i;
650 int result;
651
652 if (tap_length > 0)
653 {
654 /* Pad last byte so that tap_length is divisible by 8 */
655 while (tap_length % 8 != 0)
656 {
657 /* More of the last TMS value keeps us in the same state,
658 * analogous to free-running JTAG interfaces. */
659 armjtagew_tap_append_step(last_tms, 0);
660 }
661
662 byte_length = tap_length / 8;
663
664 usb_out_buffer[0] = CMD_TAP_SHIFT;
665 buf_set_u32(usb_out_buffer+1, 0, 16, byte_length);
666
667 tms_offset = 3;
668 for (i = 0; i < byte_length; i++)
669 {
670 usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8);
671 }
672
673 tdi_offset = tms_offset + byte_length;
674 for (i = 0; i < byte_length; i++)
675 {
676 usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8);
677 }
678
679 result = armjtagew_usb_message(armjtagew_jtag_handle, 3 + 2 * byte_length, byte_length + 4);
680
681 if (result == 0)
682 {
683 int stat;
684
685 stat = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
686 if(stat) {
687 LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat);
688 return ERROR_JTAG_QUEUE_FAILED;
689 }
690
691 for (i = 0; i < byte_length; i++)
692 {
693 tdo_buffer[i] = flip_u32(usb_in_buffer[i],8);
694 }
695
696 for (i = 0; i < pending_scan_results_length; i++)
697 {
698 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
699 u8 *buffer = pending_scan_result->buffer;
700 int length = pending_scan_result->length;
701 int first = pending_scan_result->first;
702 scan_command_t *command = pending_scan_result->command;
703
704 /* Copy to buffer */
705 buf_set_buf(tdo_buffer, first, buffer, 0, length);
706
707 DEBUG_JTAG_IO("pending scan result, length = %d", length);
708
709 #ifdef _DEBUG_USB_COMMS_
710 armjtagew_debug_buffer(buffer, byte_length);
711 #endif
712
713 if (jtag_read_buffer(buffer, command) != ERROR_OK)
714 {
715 armjtagew_tap_init();
716 return ERROR_JTAG_QUEUE_FAILED;
717 }
718
719 if (pending_scan_result->buffer != NULL)
720 {
721 free(pending_scan_result->buffer);
722 }
723 }
724 }
725 else
726 {
727 LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length);
728 return ERROR_JTAG_QUEUE_FAILED;
729 }
730
731 armjtagew_tap_init();
732 }
733
734 return ERROR_OK;
735 }
736
737 /*****************************************************************************/
738 /* JLink USB low-level functions */
739
740 static armjtagew_jtag_t* armjtagew_usb_open()
741 {
742 struct usb_bus *busses;
743 struct usb_bus *bus;
744 struct usb_device *dev;
745
746 armjtagew_jtag_t *result;
747
748 result = (armjtagew_jtag_t*) malloc(sizeof(armjtagew_jtag_t));
749
750 usb_init();
751 usb_find_busses();
752 usb_find_devices();
753
754 busses = usb_get_busses();
755
756 /* find armjtagew_jtag device in usb bus */
757
758 for (bus = busses; bus; bus = bus->next)
759 {
760 for (dev = bus->devices; dev; dev = dev->next)
761 {
762 if ((dev->descriptor.idVendor == USB_VID) && (dev->descriptor.idProduct == USB_PID))
763 {
764 result->usb_handle = usb_open(dev);
765
766 #if 0
767 /* usb_set_configuration required under win32 */
768 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
769 #endif
770 usb_claim_interface(result->usb_handle, 0);
771
772 #if 0
773 /*
774 * This makes problems under Mac OS X. And is not needed
775 * under Windows. Hopefully this will not break a linux build
776 */
777 usb_set_altinterface(result->usb_handle, 0);
778 #endif
779 return result;
780 }
781 }
782 }
783
784 free(result);
785 return NULL;
786 }
787
788 static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag)
789 {
790 usb_close(armjtagew_jtag->usb_handle);
791 free(armjtagew_jtag);
792 }
793
794 /* Send a message and receive the reply. */
795 static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length)
796 {
797 int result;
798
799 result = armjtagew_usb_write(armjtagew_jtag, out_length);
800 if (result == out_length)
801 {
802 result = armjtagew_usb_read(armjtagew_jtag, in_length);
803 if (result != in_length)
804 {
805 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
806 return -1;
807 }
808 }
809 else
810 {
811 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
812 return -1;
813 }
814 return 0;
815 }
816
817 /* Write data from out_buffer to USB. */
818 static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length)
819 {
820 int result;
821
822 if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE)
823 {
824 LOG_ERROR("armjtagew_jtag_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE);
825 return -1;
826 }
827
828 result = usb_bulk_write(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
829 (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
830
831 DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
832
833 #ifdef _DEBUG_USB_COMMS_
834 armjtagew_debug_buffer(usb_out_buffer, out_length);
835 #endif
836 return result;
837 }
838
839 /* Read data from USB into in_buffer. */
840 static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length)
841 {
842 int result = usb_bulk_read(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
843 (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
844
845 DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
846
847 #ifdef _DEBUG_USB_COMMS_
848 armjtagew_debug_buffer(usb_in_buffer, result);
849 #endif
850 return result;
851 }
852
853
854 #ifdef _DEBUG_USB_COMMS_
855 #define BYTES_PER_LINE 16
856
857 static void armjtagew_debug_buffer(u8 *buffer, int length)
858 {
859 char line[81];
860 char s[4];
861 int i;
862 int j;
863
864 for (i = 0; i < length; i += BYTES_PER_LINE)
865 {
866 snprintf(line, 5, "%04x", i);
867 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
868 {
869 snprintf(s, 4, " %02x", buffer[j]);
870 strcat(line, s);
871 }
872 LOG_DEBUG("%s", line);
873 }
874 }
875 #endif
876

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)