1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
28 #include "replacements.h"
40 #define JLINK_WRITE_ENDPOINT 0x02
41 #define JLINK_READ_ENDPOINT 0x81
43 #define JLINK_USB_TIMEOUT 1000
45 // See Section 1.3.2 of the Segger JLink USB protocol manual
46 #define JLINK_IN_BUFFER_SIZE 2048
47 #define JLINK_OUT_BUFFER_SIZE 2048
48 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
50 /* Global USB buffers */
51 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
52 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
53 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
55 /* Constants for JLink command */
56 #define EMU_CMD_VERSION 0x01
57 #define EMU_CMD_SET_SPEED 0x05
58 #define EMU_CMD_GET_STATE 0x07
59 #define EMU_CMD_HW_JTAG3 0xcf
60 #define EMU_CMD_HW_RESET0 0xdc
61 #define EMU_CMD_HW_RESET1 0xdd
62 #define EMU_CMD_HW_TRST0 0xde
63 #define EMU_CMD_HW_TRST1 0xdf
65 /* max speed 12MHz v5.0 jlink */
66 #define JLINK_MAX_SPEED 12000
68 /* External interface functions */
69 static int jlink_execute_queue(void);
70 static int jlink_speed(int speed);
71 static int jlink_speed_div(int speed, int* khz);
72 static int jlink_khz(int khz, int *jtag_speed);
73 static int jlink_register_commands(struct command_context_s *cmd_ctx);
74 static int jlink_init(void);
75 static int jlink_quit(void);
77 /* CLI command handler functions */
78 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
80 /* Queue command functions */
81 static void jlink_end_state(tap_state_t state);
82 static void jlink_state_move(void);
83 static void jlink_path_move(int num_states, tap_state_t *path);
84 static void jlink_runtest(int num_cycles);
85 static void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
86 static void jlink_reset(int trst, int srst);
87 static void jlink_simple_command(u8 command);
88 static int jlink_get_status(void);
90 /* J-Link tap buffer functions */
91 static void jlink_tap_init(void);
92 static int jlink_tap_execute(void);
93 static void jlink_tap_ensure_space(int scans, int bits);
94 static void jlink_tap_append_step(int tms, int tdi);
95 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
97 /* Jlink lowlevel functions */
98 typedef struct jlink_jtag
100 struct usb_dev_handle* usb_handle;
103 static jlink_jtag_t *jlink_usb_open(void);
104 static void jlink_usb_close(jlink_jtag_t *jlink_jtag);
105 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
106 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
107 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size);
108 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
110 /* helper functions */
111 static int jlink_get_version_info(void);
113 #ifdef _DEBUG_USB_COMMS_
114 static void jlink_debug_buffer(u8 *buffer, int length);
117 static jlink_jtag_t* jlink_jtag_handle;
119 /***************************************************************************/
120 /* External interface implementation */
122 jtag_interface_t jlink_interface =
125 .execute_queue = jlink_execute_queue,
126 .speed = jlink_speed,
127 .speed_div = jlink_speed_div,
129 .register_commands = jlink_register_commands,
134 static void jlink_execute_end_state(jtag_command_t *cmd)
136 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
138 if (cmd->cmd.end_state->end_state != TAP_INVALID)
139 jlink_end_state(cmd->cmd.end_state->end_state);
142 static void jlink_execute_runtest(jtag_command_t *cmd)
144 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
145 cmd->cmd.runtest->num_cycles,
146 cmd->cmd.runtest->end_state);
148 if (cmd->cmd.runtest->end_state != TAP_INVALID)
149 jlink_end_state(cmd->cmd.runtest->end_state);
151 jlink_runtest(cmd->cmd.runtest->num_cycles);
154 static void jlink_execute_statemove(jtag_command_t *cmd)
156 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
158 if (cmd->cmd.statemove->end_state != TAP_INVALID)
160 jlink_end_state(cmd->cmd.statemove->end_state);
165 static void jlink_execute_pathmove(jtag_command_t *cmd)
167 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
168 cmd->cmd.pathmove->num_states,
169 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
171 jlink_path_move(cmd->cmd.pathmove->num_states,
172 cmd->cmd.pathmove->path);
175 static void jlink_execute_scan(jtag_command_t *cmd)
181 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
183 if (cmd->cmd.scan->end_state != TAP_INVALID)
184 jlink_end_state(cmd->cmd.scan->end_state);
186 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
187 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
189 #ifdef _DEBUG_USB_COMMS_
190 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
192 type = jtag_scan_type(cmd->cmd.scan);
193 jlink_scan(cmd->cmd.scan->ir_scan,
194 type, buffer, scan_size, cmd->cmd.scan);
197 static void jlink_execute_reset(jtag_command_t *cmd)
199 DEBUG_JTAG_IO("reset trst: %i srst %i",
200 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
204 if (cmd->cmd.reset->trst == 1)
205 tap_set_state(TAP_RESET);
207 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
210 static void jlink_execute_sleep(jtag_command_t *cmd)
212 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
214 jtag_sleep(cmd->cmd.sleep->us);
217 static void jlink_execute_command(jtag_command_t *cmd)
221 case JTAG_END_STATE: jlink_execute_end_state(cmd); break;
222 case JTAG_RUNTEST: jlink_execute_runtest(cmd); break;
223 case JTAG_STATEMOVE: jlink_execute_statemove(cmd); break;
224 case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break;
225 case JTAG_SCAN: jlink_execute_scan(cmd); break;
226 case JTAG_RESET: jlink_execute_reset(cmd); break;
227 case JTAG_SLEEP: jlink_execute_sleep(cmd); break;
229 LOG_ERROR("BUG: unknown JTAG command type encountered");
234 static int jlink_execute_queue(void)
236 jtag_command_t *cmd = jtag_command_queue;
240 jlink_execute_command(cmd);
244 return jlink_tap_execute();
247 /* Sets speed in kHz. */
248 static int jlink_speed(int speed)
252 if (speed > JLINK_MAX_SPEED)
254 LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
255 speed, JLINK_MAX_SPEED);
259 /* check for RTCK setting */
263 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
264 usb_out_buffer[1] = (speed >> 0) & 0xff;
265 usb_out_buffer[2] = (speed >> 8) & 0xff;
267 result = jlink_usb_write(jlink_jtag_handle, 3);
270 LOG_ERROR("J-Link setting speed failed (%d)", result);
271 return ERROR_JTAG_DEVICE_ERROR;
277 static int jlink_speed_div(int speed, int* khz)
284 static int jlink_khz(int khz, int *jtag_speed)
291 static int jlink_register_commands(struct command_context_s *cmd_ctx)
293 register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
298 static int jlink_init(void)
302 jlink_jtag_handle = jlink_usb_open();
304 if (jlink_jtag_handle == 0)
306 LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
307 return ERROR_JTAG_INIT_FAILED;
311 while (check_cnt < 3)
313 if (jlink_get_version_info() == ERROR_OK)
315 /* attempt to get status */
325 LOG_INFO("J-Link initial read failed, don't worry");
328 LOG_INFO("J-Link JTAG Interface ready");
336 static int jlink_quit(void)
338 jlink_usb_close(jlink_jtag_handle);
342 /***************************************************************************/
343 /* Queue command implementations */
345 static void jlink_end_state(tap_state_t state)
347 if (tap_is_state_stable(state))
349 tap_set_end_state(state);
353 LOG_ERROR("BUG: %i is not a valid end state", state);
358 /* Goes to the end state. */
359 static void jlink_state_move(void)
363 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
365 for (i = 0; i < 7; i++)
367 tms = (tms_scan >> i) & 1;
368 jlink_tap_append_step(tms, 0);
371 tap_set_state(tap_get_end_state());
374 static void jlink_path_move(int num_states, tap_state_t *path)
378 for (i = 0; i < num_states; i++)
380 if (path[i] == tap_state_transition(tap_get_state(), false))
382 jlink_tap_append_step(0, 0);
384 else if (path[i] == tap_state_transition(tap_get_state(), true))
386 jlink_tap_append_step(1, 0);
390 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
394 tap_set_state(path[i]);
397 tap_set_end_state(tap_get_state());
400 static void jlink_runtest(int num_cycles)
404 tap_state_t saved_end_state = tap_get_end_state();
406 /* only do a state_move when we're not already in IDLE */
407 if (tap_get_state() != TAP_IDLE)
409 jlink_end_state(TAP_IDLE);
413 /* execute num_cycles */
414 for (i = 0; i < num_cycles; i++)
416 jlink_tap_append_step(0, 0);
419 /* finish in end_state */
420 jlink_end_state(saved_end_state);
421 if (tap_get_state() != tap_get_end_state())
427 static void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
429 tap_state_t saved_end_state;
431 jlink_tap_ensure_space(1, scan_size + 8);
433 saved_end_state = tap_get_end_state();
435 /* Move to appropriate scan state */
436 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
438 /* Only move if we're not already there */
439 if (tap_get_state() != tap_get_end_state())
442 jlink_end_state(saved_end_state);
445 jlink_tap_append_scan(scan_size, buffer, command);
447 /* We are in Exit1, go to Pause */
448 jlink_tap_append_step(0, 0);
450 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
452 if (tap_get_state() != tap_get_end_state())
458 static void jlink_reset(int trst, int srst)
460 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
462 /* Signals are active low */
465 jlink_simple_command(EMU_CMD_HW_RESET1);
469 jlink_simple_command(EMU_CMD_HW_RESET0);
474 jlink_simple_command(EMU_CMD_HW_TRST1);
478 jlink_simple_command(EMU_CMD_HW_TRST0);
482 static void jlink_simple_command(u8 command)
486 DEBUG_JTAG_IO("0x%02x", command);
488 usb_out_buffer[0] = command;
489 result = jlink_usb_write(jlink_jtag_handle, 1);
493 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
497 static int jlink_get_status(void)
501 jlink_simple_command(EMU_CMD_GET_STATE);
503 result = jlink_usb_read(jlink_jtag_handle, 8);
506 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
507 return ERROR_JTAG_DEVICE_ERROR;
510 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
511 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
512 vref / 1000, vref % 1000, \
513 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
514 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
517 LOG_ERROR("Vref too low. Check Target Power\n");
522 static int jlink_get_version_info(void)
527 /* query hardware version */
528 jlink_simple_command(EMU_CMD_VERSION);
530 result = jlink_usb_read(jlink_jtag_handle, 2);
533 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
535 return ERROR_JTAG_DEVICE_ERROR;
538 len = buf_get_u32(usb_in_buffer, 0, 16);
539 result = jlink_usb_read(jlink_jtag_handle, len);
542 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n",
544 return ERROR_JTAG_DEVICE_ERROR;
547 usb_in_buffer[result] = 0;
548 LOG_INFO("%s", (char *)usb_in_buffer);
553 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
555 if (jlink_get_version_info() == ERROR_OK)
557 /* attempt to get status */
564 /***************************************************************************/
565 /* J-Link tap functions */
567 /* 2048 is the max value we can use here */
568 #define JLINK_TAP_BUFFER_SIZE 2048
570 static unsigned tap_length;
571 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
572 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
573 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
577 int first; /* First bit position in tdo_buffer to read */
578 int length; /* Number of bits to read */
579 scan_command_t *command; /* Corresponding scan command */
581 } pending_scan_result_t;
583 #define MAX_PENDING_SCAN_RESULTS 256
585 static int pending_scan_results_length;
586 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
590 static void jlink_tap_init(void)
593 pending_scan_results_length = 0;
596 static void jlink_tap_ensure_space(int scans, int bits)
598 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
599 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
601 if (scans > available_scans || bits > available_bits)
607 static void jlink_tap_append_step(int tms, int tdi)
610 int index = tap_length / 8;
612 if (index >= JLINK_TAP_BUFFER_SIZE)
614 LOG_ERROR("jlink_tap_append_step: overflow");
618 int bit_index = tap_length % 8;
619 u8 bit = 1 << bit_index;
622 tms_buffer[index] |= bit;
624 tms_buffer[index] &= ~bit;
627 tdi_buffer[index] |= bit;
629 tdi_buffer[index] &= ~bit;
634 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
636 pending_scan_result_t *pending_scan_result =
637 &pending_scan_results_buffer[pending_scan_results_length];
640 pending_scan_result->first = tap_length;
641 pending_scan_result->length = length;
642 pending_scan_result->command = command;
643 pending_scan_result->buffer = buffer;
645 for (i = 0; i < length; i++)
647 int tms = i < length - 1 ? 0 : 1;
648 int tdi = buffer[i / 8] & (1 << (i % 8));
649 jlink_tap_append_step(tms, tdi);
651 pending_scan_results_length++;
654 /* Pad and send a tap sequence to the device, and receive the answer.
655 * For the purpose of padding we assume that we are in idle or pause state. */
656 static int jlink_tap_execute(void)
667 /* Pad last byte so that tap_length is divisible by 8 */
668 while (tap_length % 8 != 0)
670 /* More of the last TMS value keeps us in the same state,
671 * analogous to free-running JTAG interfaces. */
672 jlink_tap_append_step(last_tms, 0);
675 byte_length = tap_length / 8;
677 usb_out_buffer[0] = EMU_CMD_HW_JTAG3;
678 usb_out_buffer[1] = 0;
679 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
680 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
683 for (i = 0; i < byte_length; i++)
685 usb_out_buffer[tms_offset + i] = tms_buffer[i];
688 tdi_offset = tms_offset + byte_length;
689 for (i = 0; i < byte_length; i++)
691 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
694 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
696 if (result != byte_length)
698 LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
699 result, byte_length);
700 return ERROR_JTAG_QUEUE_FAILED;
703 for (i = 0; i < byte_length; i++)
704 tdo_buffer[i] = usb_in_buffer[i];
706 for (i = 0; i < pending_scan_results_length; i++)
708 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
709 u8 *buffer = pending_scan_result->buffer;
710 int length = pending_scan_result->length;
711 int first = pending_scan_result->first;
712 scan_command_t *command = pending_scan_result->command;
715 buf_set_buf(tdo_buffer, first, buffer, 0, length);
717 DEBUG_JTAG_IO("pending scan result, length = %d", length);
719 #ifdef _DEBUG_USB_COMMS_
720 jlink_debug_buffer(buffer, byte_length);
723 if (jtag_read_buffer(buffer, command) != ERROR_OK)
726 return ERROR_JTAG_QUEUE_FAILED;
729 if (pending_scan_result->buffer != NULL)
731 free(pending_scan_result->buffer);
740 /*****************************************************************************/
741 /* JLink USB low-level functions */
743 static jlink_jtag_t* jlink_usb_open()
745 struct usb_bus *busses;
747 struct usb_device *dev;
749 jlink_jtag_t *result;
751 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
757 busses = usb_get_busses();
759 /* find jlink_jtag device in usb bus */
761 for (bus = busses; bus; bus = bus->next)
763 for (dev = bus->devices; dev; dev = dev->next)
765 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
767 result->usb_handle = usb_open(dev);
769 /* usb_set_configuration required under win32 */
770 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
771 usb_claim_interface(result->usb_handle, 0);
775 * This makes problems under Mac OS X. And is not needed
776 * under Windows. Hopefully this will not break a linux build
778 usb_set_altinterface(result->usb_handle, 0);
789 static void jlink_usb_close(jlink_jtag_t *jlink_jtag)
791 usb_close(jlink_jtag->usb_handle);
795 /* Send a message and receive the reply. */
796 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
801 result = jlink_usb_write(jlink_jtag, out_length);
802 if (result != out_length)
804 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
806 return ERROR_JTAG_DEVICE_ERROR;
809 result = jlink_usb_read(jlink_jtag, in_length);
810 if ((result != in_length) && (result != in_length + 1))
812 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
814 return ERROR_JTAG_DEVICE_ERROR;
817 if (result == in_length)
819 /* Must read the result from the EMU too */
820 result2 = jlink_usb_read_emu_result(jlink_jtag);
823 LOG_ERROR("jlink_usb_read_emu_result failed "
824 "(requested=1, result=%d)", result2);
825 return ERROR_JTAG_DEVICE_ERROR;
828 /* Check the result itself */
829 result2 = usb_emu_result_buffer[0];
833 /* Save the result, then remove it from return value */
834 result2 = usb_in_buffer[result--];
839 LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
840 return ERROR_JTAG_DEVICE_ERROR;
846 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
847 static int usb_bulk_with_retries(
848 int (*f)(usb_dev_handle *, int, char *, int, int),
849 usb_dev_handle *dev, int ep,
850 char *bytes, int size, int timeout)
852 int rc = 0, tries = 3, this_size;
854 while (tries && size) {
856 this_size = f(dev, ep, bytes, size, timeout);
868 static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
869 char *bytes, int size, int timeout)
871 return usb_bulk_with_retries(&usb_bulk_write,
872 dev, ep, bytes, size, timeout);
874 static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
875 char *bytes, int size, int timeout)
877 return usb_bulk_with_retries(&usb_bulk_read,
878 dev, ep, bytes, size, timeout);
881 /* Write data from out_buffer to USB. */
882 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
886 if (out_length > JLINK_OUT_BUFFER_SIZE)
888 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
892 result = usb_bulk_write_ex(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT,
893 (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
895 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
897 #ifdef _DEBUG_USB_COMMS_
898 jlink_debug_buffer(usb_out_buffer, out_length);
903 /* Read data from USB into in_buffer. */
904 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size)
906 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT,
907 (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
909 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
911 #ifdef _DEBUG_USB_COMMS_
912 jlink_debug_buffer(usb_in_buffer, result);
917 /* Read the result from the previous EMU cmd into result_buffer. */
918 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
920 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT,
921 (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
924 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
926 #ifdef _DEBUG_USB_COMMS_
927 jlink_debug_buffer(usb_emu_result_buffer, result);
932 #ifdef _DEBUG_USB_COMMS_
933 #define BYTES_PER_LINE 16
935 static void jlink_debug_buffer(u8 *buffer, int length)
942 for (i = 0; i < length; i += BYTES_PER_LINE)
944 snprintf(line, 5, "%04x", i);
945 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
947 snprintf(s, 4, " %02x", buffer[j]);
950 LOG_DEBUG("%s", line);