target: Do not use LOG_USER() for error messages
[openocd.git] / src / jtag / drivers / arm-jtag-ew.c
blob4c50c54c9bd4aa01b59f5197965ff2a3449e8254
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
5 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
6 ***************************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
12 #include <jtag/interface.h>
13 #include <jtag/commands.h>
14 #include "helper/system.h"
15 #include "libusb_helper.h"
17 #define USB_VID 0x15ba
18 #define USB_PID 0x001e
20 #define ARMJTAGEW_EPT_BULK_OUT 0x01u
21 #define ARMJTAGEW_EPT_BULK_IN 0x82u
23 #define ARMJTAGEW_USB_TIMEOUT 2000
25 #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
26 #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
28 /* USB command request codes. */
29 #define CMD_GET_VERSION 0x00
30 #define CMD_SELECT_DPIMPL 0x10
31 #define CMD_SET_TCK_FREQUENCY 0x11
32 #define CMD_GET_TCK_FREQUENCY 0x12
33 #define CMD_MEASURE_MAX_TCK_FREQ 0x15
34 #define CMD_MEASURE_RTCK_RESPONSE 0x16
35 #define CMD_TAP_SHIFT 0x17
36 #define CMD_SET_TAPHW_STATE 0x20
37 #define CMD_GET_TAPHW_STATE 0x21
38 #define CMD_TGPWR_SETUP 0x22
40 /* Global USB buffers */
41 static uint8_t usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
42 static uint8_t usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
44 /* Queue command functions */
45 static void armjtagew_end_state(tap_state_t state);
46 static void armjtagew_state_move(void);
47 static void armjtagew_path_move(int num_states, tap_state_t *path);
48 static void armjtagew_runtest(int num_cycles);
49 static void armjtagew_scan(bool ir_scan,
50 enum scan_type type,
51 uint8_t *buffer,
52 int scan_size,
53 struct scan_command *command);
54 static void armjtagew_reset(int trst, int srst);
55 /* static void armjtagew_simple_command(uint8_t command); */
56 static int armjtagew_get_status(void);
58 /* tap buffer functions */
59 static void armjtagew_tap_init(void);
60 static int armjtagew_tap_execute(void);
61 static void armjtagew_tap_ensure_space(int scans, int bits);
62 static void armjtagew_tap_append_step(int tms, int tdi);
63 static void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
65 /* ARM-JTAG-EW lowlevel functions */
66 struct armjtagew {
67 struct libusb_device_handle *usb_handle;
70 static struct armjtagew *armjtagew_usb_open(void);
71 static void armjtagew_usb_close(struct armjtagew *armjtagew);
72 static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length);
73 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length);
74 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length);
76 /* helper functions */
77 static int armjtagew_get_version_info(void);
79 #ifdef _DEBUG_USB_COMMS_
80 static void armjtagew_debug_buffer(uint8_t *buffer, int length);
81 #endif
83 static struct armjtagew *armjtagew_handle;
85 /**************************************************************************
86 * External interface implementation */
88 static int armjtagew_execute_queue(struct jtag_command *cmd_queue)
90 struct jtag_command *cmd = cmd_queue;
91 int scan_size;
92 enum scan_type type;
93 uint8_t *buffer;
95 while (cmd) {
96 switch (cmd->type) {
97 case JTAG_RUNTEST:
98 LOG_DEBUG_IO("runtest %i cycles, end in %i",
99 cmd->cmd.runtest->num_cycles,
100 cmd->cmd.runtest->end_state);
102 armjtagew_end_state(cmd->cmd.runtest->end_state);
103 armjtagew_runtest(cmd->cmd.runtest->num_cycles);
104 break;
106 case JTAG_TLR_RESET:
107 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
109 armjtagew_end_state(cmd->cmd.statemove->end_state);
110 armjtagew_state_move();
111 break;
113 case JTAG_PATHMOVE:
114 LOG_DEBUG_IO("pathmove: %i states, end in %i",
115 cmd->cmd.pathmove->num_states,
116 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
118 armjtagew_path_move(cmd->cmd.pathmove->num_states,
119 cmd->cmd.pathmove->path);
120 break;
122 case JTAG_SCAN:
123 LOG_DEBUG_IO("scan end in %i", cmd->cmd.scan->end_state);
125 armjtagew_end_state(cmd->cmd.scan->end_state);
127 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
128 LOG_DEBUG_IO("scan input, length = %d", scan_size);
130 #ifdef _DEBUG_USB_COMMS_
131 armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
132 #endif
133 type = jtag_scan_type(cmd->cmd.scan);
134 armjtagew_scan(cmd->cmd.scan->ir_scan,
135 type, buffer,
136 scan_size, cmd->cmd.scan);
137 break;
139 case JTAG_RESET:
140 LOG_DEBUG_IO("reset trst: %i srst %i",
141 cmd->cmd.reset->trst,
142 cmd->cmd.reset->srst);
144 armjtagew_tap_execute();
146 if (cmd->cmd.reset->trst == 1)
147 tap_set_state(TAP_RESET);
148 armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
149 break;
151 case JTAG_SLEEP:
152 LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
153 armjtagew_tap_execute();
154 jtag_sleep(cmd->cmd.sleep->us);
155 break;
157 default:
158 LOG_ERROR("BUG: unknown JTAG command type encountered");
159 exit(-1);
161 cmd = cmd->next;
164 return armjtagew_tap_execute();
167 /* Sets speed in kHz. */
168 static int armjtagew_speed(int speed)
170 int result;
171 int speed_real;
174 usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
175 buf_set_u32(usb_out_buffer + 1, 0, 32, speed*1000);
177 result = armjtagew_usb_message(armjtagew_handle, 5, 4);
179 if (result < 0) {
180 LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
181 return ERROR_JTAG_DEVICE_ERROR;
184 usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
185 result = armjtagew_usb_message(armjtagew_handle, 1, 4);
186 speed_real = (int)buf_get_u32(usb_in_buffer, 0, 32) / 1000;
187 if (result < 0) {
188 LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
189 return ERROR_JTAG_DEVICE_ERROR;
190 } else
191 LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
193 return ERROR_OK;
196 static int armjtagew_khz(int khz, int *jtag_speed)
198 *jtag_speed = khz;
200 return ERROR_OK;
203 static int armjtagew_speed_div(int speed, int *khz)
205 *khz = speed;
207 return ERROR_OK;
210 static int armjtagew_init(void)
212 int check_cnt;
214 armjtagew_handle = armjtagew_usb_open();
216 if (!armjtagew_handle) {
217 LOG_ERROR(
218 "Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
219 return ERROR_JTAG_INIT_FAILED;
222 check_cnt = 0;
223 while (check_cnt < 3) {
224 if (armjtagew_get_version_info() == ERROR_OK) {
225 /* attempt to get status */
226 armjtagew_get_status();
227 break;
230 check_cnt++;
233 if (check_cnt == 3)
234 LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
236 /* Initial JTAG speed (for reset and initialization): 32 kHz */
237 armjtagew_speed(32);
239 LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
241 armjtagew_reset(0, 0);
242 armjtagew_tap_init();
244 return ERROR_OK;
247 static int armjtagew_quit(void)
249 armjtagew_usb_close(armjtagew_handle);
250 return ERROR_OK;
253 /**************************************************************************
254 * Queue command implementations */
256 static void armjtagew_end_state(tap_state_t state)
258 if (tap_is_state_stable(state))
259 tap_set_end_state(state);
260 else {
261 LOG_ERROR("BUG: %i is not a valid end state", state);
262 exit(-1);
266 /* Goes to the end state. */
267 static void armjtagew_state_move(void)
269 int i;
270 int tms = 0;
271 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
272 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
274 for (i = 0; i < tms_count; i++) {
275 tms = (tms_scan >> i) & 1;
276 armjtagew_tap_append_step(tms, 0);
279 tap_set_state(tap_get_end_state());
282 static void armjtagew_path_move(int num_states, tap_state_t *path)
284 int i;
286 for (i = 0; i < num_states; i++) {
288 * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
289 * Either handle that here, or update the documentation with examples
290 * how to fix that in the configuration files.
292 if (path[i] == tap_state_transition(tap_get_state(), false))
293 armjtagew_tap_append_step(0, 0);
294 else if (path[i] == tap_state_transition(tap_get_state(), true))
295 armjtagew_tap_append_step(1, 0);
296 else {
297 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
298 tap_state_name(tap_get_state()), tap_state_name(path[i]));
299 exit(-1);
302 tap_set_state(path[i]);
305 tap_set_end_state(tap_get_state());
308 static void armjtagew_runtest(int num_cycles)
310 int i;
312 tap_state_t saved_end_state = tap_get_end_state();
314 /* only do a state_move when we're not already in IDLE */
315 if (tap_get_state() != TAP_IDLE) {
316 armjtagew_end_state(TAP_IDLE);
317 armjtagew_state_move();
320 /* execute num_cycles */
321 for (i = 0; i < num_cycles; i++)
322 armjtagew_tap_append_step(0, 0);
324 /* finish in end_state */
325 armjtagew_end_state(saved_end_state);
326 if (tap_get_state() != tap_get_end_state())
327 armjtagew_state_move();
330 static void armjtagew_scan(bool ir_scan,
331 enum scan_type type,
332 uint8_t *buffer,
333 int scan_size,
334 struct scan_command *command)
336 tap_state_t saved_end_state;
338 armjtagew_tap_ensure_space(1, scan_size + 8);
340 saved_end_state = tap_get_end_state();
342 /* Move to appropriate scan state */
343 armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
345 /* Only move if we're not already there */
346 if (tap_get_state() != tap_get_end_state())
347 armjtagew_state_move();
349 armjtagew_end_state(saved_end_state);
351 /* Scan */
352 armjtagew_tap_append_scan(scan_size, buffer, command);
354 /* We are in Exit1, go to Pause */
355 armjtagew_tap_append_step(0, 0);
357 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
359 if (tap_get_state() != tap_get_end_state())
360 armjtagew_state_move();
363 static void armjtagew_reset(int trst, int srst)
365 const uint8_t trst_mask = (1u << 5);
366 const uint8_t srst_mask = (1u << 6);
367 uint8_t val = 0;
368 uint8_t outp_en = 0;
369 uint8_t change_mask = 0;
370 int result;
372 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
374 if (srst == 0) {
375 val |= srst_mask;
376 outp_en &= ~srst_mask; /* tristate */
377 change_mask |= srst_mask;
378 } else if (srst == 1) {
379 val &= ~srst_mask;
380 outp_en |= srst_mask;
381 change_mask |= srst_mask;
384 if (trst == 0) {
385 val |= trst_mask;
386 outp_en &= ~trst_mask; /* tristate */
387 change_mask |= trst_mask;
388 } else if (trst == 1) {
389 val &= ~trst_mask;
390 outp_en |= trst_mask;
391 change_mask |= trst_mask;
394 usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
395 usb_out_buffer[1] = val;
396 usb_out_buffer[2] = outp_en;
397 usb_out_buffer[3] = change_mask;
398 result = armjtagew_usb_write(armjtagew_handle, 4);
399 if (result != 4)
400 LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
403 static int armjtagew_get_status(void)
405 int result;
407 usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
408 result = armjtagew_usb_message(armjtagew_handle, 1, 12);
410 if (result == 0) {
411 unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
412 LOG_INFO(
413 "U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s",
414 (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)),
415 (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)),
416 (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)),
417 (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)),
418 usb_in_buffer[9],
419 usb_in_buffer[11] ? "OVERCURRENT" : "OK",
420 usb_in_buffer[10] ? "enabled" : "disabled");
422 if (u_tg < 1500)
423 LOG_ERROR("Vref too low. Check Target Power");
424 } else
425 LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)", result);
427 return ERROR_OK;
430 static int armjtagew_get_version_info(void)
432 int result;
433 char sn[16];
434 char auxinfo[257];
436 /* query hardware version */
437 usb_out_buffer[0] = CMD_GET_VERSION;
438 result = armjtagew_usb_message(armjtagew_handle, 1, 4 + 15 + 256);
440 if (result != 0) {
441 LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)", result);
442 return ERROR_JTAG_DEVICE_ERROR;
445 memcpy(sn, usb_in_buffer + 4, 15);
446 sn[15] = '\0';
447 memcpy(auxinfo, usb_in_buffer + 4+15, 256);
448 auxinfo[256] = '\0';
450 LOG_INFO(
451 "ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s",
452 usb_in_buffer[1],
453 usb_in_buffer[0],
454 isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X',
456 auxinfo);
458 if (1 != usb_in_buffer[1] || 6 != usb_in_buffer[0])
459 LOG_WARNING(
460 "ARM-JTAG-EW firmware version %d.%d is untested with this version of OpenOCD. You might experience unexpected behavior.",
461 usb_in_buffer[1],
462 usb_in_buffer[0]);
463 return ERROR_OK;
466 COMMAND_HANDLER(armjtagew_handle_armjtagew_info_command)
468 if (armjtagew_get_version_info() == ERROR_OK) {
469 /* attempt to get status */
470 armjtagew_get_status();
473 return ERROR_OK;
476 static const struct command_registration armjtagew_command_handlers[] = {
478 .name = "armjtagew_info",
479 .handler = &armjtagew_handle_armjtagew_info_command,
480 .mode = COMMAND_EXEC,
481 .help = "query armjtagew info",
482 .usage = "",
484 COMMAND_REGISTRATION_DONE
487 static struct jtag_interface armjtagew_interface = {
488 .execute_queue = armjtagew_execute_queue,
491 struct adapter_driver armjtagew_adapter_driver = {
492 .name = "arm-jtag-ew",
493 .transports = jtag_only,
494 .commands = armjtagew_command_handlers,
496 .init = armjtagew_init,
497 .quit = armjtagew_quit,
498 .speed = armjtagew_speed,
499 .khz = armjtagew_khz,
500 .speed_div = armjtagew_speed_div,
502 .jtag_ops = &armjtagew_interface,
505 /**************************************************************************
506 * ARM-JTAG-EW tap functions */
508 /* 2048 is the max value we can use here */
509 #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
511 static int tap_length;
512 static uint8_t tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
513 static uint8_t tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
514 static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
516 struct pending_scan_result {
517 int first; /* First bit position in tdo_buffer to read */
518 int length; /* Number of bits to read */
519 struct scan_command *command; /* Corresponding scan command */
520 uint8_t *buffer;
523 #define MAX_PENDING_SCAN_RESULTS 256
525 static int pending_scan_results_length;
526 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
528 static int last_tms;
530 static void armjtagew_tap_init(void)
532 tap_length = 0;
533 pending_scan_results_length = 0;
536 static void armjtagew_tap_ensure_space(int scans, int bits)
538 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
539 int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
541 if (scans > available_scans || bits > available_bits)
542 armjtagew_tap_execute();
545 static void armjtagew_tap_append_step(int tms, int tdi)
547 last_tms = tms;
548 int index_local = tap_length / 8;
550 if (index_local < ARMJTAGEW_TAP_BUFFER_SIZE) {
551 int bit_index = tap_length % 8;
552 uint8_t bit = 1 << bit_index;
554 if (tms)
555 tms_buffer[index_local] |= bit;
556 else
557 tms_buffer[index_local] &= ~bit;
559 if (tdi)
560 tdi_buffer[index_local] |= bit;
561 else
562 tdi_buffer[index_local] &= ~bit;
564 tap_length++;
565 } else
566 LOG_ERROR("armjtagew_tap_append_step, overflow");
569 void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
571 struct pending_scan_result *pending_scan_result =
572 &pending_scan_results_buffer[pending_scan_results_length];
573 int i;
575 pending_scan_result->first = tap_length;
576 pending_scan_result->length = length;
577 pending_scan_result->command = command;
578 pending_scan_result->buffer = buffer;
580 for (i = 0; i < length; i++)
581 armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
582 pending_scan_results_length++;
585 /* Pad and send a tap sequence to the device, and receive the answer.
586 * For the purpose of padding we assume that we are in idle or pause state. */
587 static int armjtagew_tap_execute(void)
589 int byte_length;
590 int tms_offset;
591 int tdi_offset;
592 int i;
593 int result;
595 if (tap_length > 0) {
596 /* Pad last byte so that tap_length is divisible by 8 */
597 while (tap_length % 8 != 0) {
598 /* More of the last TMS value keeps us in the same state,
599 * analogous to free-running JTAG interfaces. */
600 armjtagew_tap_append_step(last_tms, 0);
603 byte_length = tap_length / 8;
605 usb_out_buffer[0] = CMD_TAP_SHIFT;
606 buf_set_u32(usb_out_buffer + 1, 0, 16, byte_length);
608 tms_offset = 3;
609 for (i = 0; i < byte_length; i++)
610 usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i], 8);
612 tdi_offset = tms_offset + byte_length;
613 for (i = 0; i < byte_length; i++)
614 usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i], 8);
616 result = armjtagew_usb_message(armjtagew_handle,
617 3 + 2 * byte_length,
618 byte_length + 4);
620 if (result == 0) {
621 int stat_local;
623 stat_local = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
624 if (stat_local) {
625 LOG_ERROR(
626 "armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command",
627 stat_local);
628 return ERROR_JTAG_QUEUE_FAILED;
631 for (i = 0; i < byte_length; i++)
632 tdo_buffer[i] = flip_u32(usb_in_buffer[i], 8);
634 for (i = 0; i < pending_scan_results_length; i++) {
635 struct pending_scan_result *pending_scan_result =
636 &pending_scan_results_buffer[i];
637 uint8_t *buffer = pending_scan_result->buffer;
638 int length = pending_scan_result->length;
639 int first = pending_scan_result->first;
640 struct scan_command *command = pending_scan_result->command;
642 /* Copy to buffer */
643 buf_set_buf(tdo_buffer, first, buffer, 0, length);
645 LOG_DEBUG_IO("pending scan result, length = %d", length);
647 #ifdef _DEBUG_USB_COMMS_
648 armjtagew_debug_buffer(buffer, byte_length);
649 #endif
651 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
652 armjtagew_tap_init();
653 return ERROR_JTAG_QUEUE_FAILED;
656 free(pending_scan_result->buffer);
658 } else {
659 LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d",
660 result,
661 byte_length);
662 return ERROR_JTAG_QUEUE_FAILED;
665 armjtagew_tap_init();
668 return ERROR_OK;
671 /****************************************************************************
672 * JLink USB low-level functions */
674 static struct armjtagew *armjtagew_usb_open(void)
676 const uint16_t vids[] = { USB_VID, 0 };
677 const uint16_t pids[] = { USB_PID, 0 };
678 struct libusb_device_handle *dev;
680 if (jtag_libusb_open(vids, pids, NULL, &dev, NULL) != ERROR_OK)
681 return NULL;
683 struct armjtagew *result = malloc(sizeof(struct armjtagew));
684 result->usb_handle = dev;
686 #if 0
687 /* libusb_set_configuration required under win32 */
688 struct libusb_config_descriptor *config;
689 struct libusb_device *usb_dev = libusb_get_device(dev);
690 libusb_get_config_descriptor(usb_dev, 0, &config);
691 libusb_set_configuration(dev, config->bConfigurationValue);
692 #endif
693 libusb_claim_interface(dev, 0);
694 #if 0
696 * This makes problems under Mac OS X. And is not needed
697 * under Windows. Hopefully this will not break a linux build
699 libusb_set_interface_alt_setting(dev, 0, 0);
700 #endif
701 return result;
704 static void armjtagew_usb_close(struct armjtagew *armjtagew)
706 libusb_close(armjtagew->usb_handle);
707 free(armjtagew);
710 /* Send a message and receive the reply. */
711 static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length)
713 int result;
715 result = armjtagew_usb_write(armjtagew, out_length);
716 if (result == out_length) {
717 result = armjtagew_usb_read(armjtagew, in_length);
718 if (result != in_length) {
719 LOG_ERROR("jtag_libusb_bulk_read failed (requested=%d, result=%d)",
720 in_length,
721 result);
722 return -1;
724 } else {
725 LOG_ERROR("jtag_libusb_bulk_write failed (requested=%d, result=%d)", out_length, result);
726 return -1;
728 return 0;
731 /* Write data from out_buffer to USB. */
732 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length)
734 int result;
735 int transferred;
737 if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE) {
738 LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)",
739 out_length,
740 ARMJTAGEW_OUT_BUFFER_SIZE);
741 return -1;
744 result = jtag_libusb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT,
745 (char *)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT, &transferred);
747 LOG_DEBUG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
749 #ifdef _DEBUG_USB_COMMS_
750 armjtagew_debug_buffer(usb_out_buffer, out_length);
751 #endif
752 if (result != ERROR_OK)
753 return -1;
754 return transferred;
757 /* Read data from USB into in_buffer. */
758 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
760 int transferred;
761 int result = jtag_libusb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN,
762 (char *)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT, &transferred);
764 LOG_DEBUG_IO("armjtagew_usb_read, result = %d", result);
766 #ifdef _DEBUG_USB_COMMS_
767 armjtagew_debug_buffer(usb_in_buffer, result);
768 #endif
769 if (result != ERROR_OK)
770 return -1;
771 return transferred;
774 #ifdef _DEBUG_USB_COMMS_
775 #define BYTES_PER_LINE 16
777 static void armjtagew_debug_buffer(uint8_t *buffer, int length)
779 char line[8 + 3 * BYTES_PER_LINE + 1];
781 for (int i = 0; i < length; i += BYTES_PER_LINE) {
782 int n = snprintf(line, 9, "%04x", i);
783 for (int j = i; j < i + BYTES_PER_LINE && j < length; j++)
784 n += snprintf(line + n, 4, " %02x", buffer[j]);
785 LOG_DEBUG("%s", line);
787 /* Prevent GDB timeout (writing to log might take some time) */
788 keep_alive();
791 #endif