arm-jtag-ew,jlink: switch to COMMAND_HANDLER
[openocd/ztw.git] / src / jtag / arm-jtag-ew.c
blob18b353acf34a0a35a4f6efa20d2d9346bd6e8536
1 /***************************************************************************
2 * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include "interface.h"
26 #include "commands.h"
27 #include <usb.h>
30 #define USB_VID 0x15ba
31 #define USB_PID 0x001e
33 #define ARMJTAGEW_EPT_BULK_OUT 0x01u
34 #define ARMJTAGEW_EPT_BULK_IN 0x82u
36 #define ARMJTAGEW_USB_TIMEOUT 2000
38 #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
39 #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
42 /* USB command request codes. */
43 #define CMD_GET_VERSION 0x00
44 #define CMD_SELECT_DPIMPL 0x10
45 #define CMD_SET_TCK_FREQUENCY 0x11
46 #define CMD_GET_TCK_FREQUENCY 0x12
47 #define CMD_MEASURE_MAX_TCK_FREQ 0x15
48 #define CMD_MEASURE_RTCK_RESPONSE 0x16
49 #define CMD_TAP_SHIFT 0x17
50 #define CMD_SET_TAPHW_STATE 0x20
51 #define CMD_GET_TAPHW_STATE 0x21
52 #define CMD_TGPWR_SETUP 0x22
54 /* Global USB buffers */
55 static uint8_t usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
56 static uint8_t usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
58 /* External interface functions */
59 static int armjtagew_execute_queue(void);
60 static int armjtagew_speed(int speed);
61 static int armjtagew_khz(int khz, int *jtag_speed);
62 static int armjtagew_register_commands(struct command_context *cmd_ctx);
63 static int armjtagew_init(void);
64 static int armjtagew_quit(void);
66 /* Queue command functions */
67 static void armjtagew_end_state(tap_state_t state);
68 static void armjtagew_state_move(void);
69 static void armjtagew_path_move(int num_states, tap_state_t *path);
70 static void armjtagew_runtest(int num_cycles);
71 static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command);
72 static void armjtagew_reset(int trst, int srst);
73 //static void armjtagew_simple_command(uint8_t command);
74 static int armjtagew_get_status(void);
76 /* tap buffer functions */
77 static void armjtagew_tap_init(void);
78 static int armjtagew_tap_execute(void);
79 static void armjtagew_tap_ensure_space(int scans, int bits);
80 static void armjtagew_tap_append_step(int tms, int tdi);
81 static void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
83 /* ARM-JTAG-EW lowlevel functions */
84 struct armjtagew {
85 struct usb_dev_handle* usb_handle;
88 static struct armjtagew *armjtagew_usb_open(void);
89 static void armjtagew_usb_close(struct armjtagew *armjtagew);
90 static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length);
91 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length);
92 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length);
94 /* helper functions */
95 static int armjtagew_get_version_info(void);
97 #ifdef _DEBUG_USB_COMMS_
98 static void armjtagew_debug_buffer(uint8_t *buffer, int length);
99 #endif
101 static struct armjtagew* armjtagew_handle;
105 /***************************************************************************/
106 /* External interface implementation */
108 struct jtag_interface armjtagew_interface =
110 .name = "arm-jtag-ew",
111 .execute_queue = armjtagew_execute_queue,
112 .speed = armjtagew_speed,
113 .khz = armjtagew_khz,
114 .register_commands = armjtagew_register_commands,
115 .init = armjtagew_init,
116 .quit = armjtagew_quit
120 static int armjtagew_execute_queue(void)
122 struct jtag_command *cmd = jtag_command_queue;
123 int scan_size;
124 enum scan_type type;
125 uint8_t *buffer;
127 while (cmd != NULL)
129 switch (cmd->type)
131 case JTAG_RUNTEST:
132 DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
133 cmd->cmd.runtest->end_state);
135 armjtagew_end_state(cmd->cmd.runtest->end_state);
136 armjtagew_runtest(cmd->cmd.runtest->num_cycles);
137 break;
139 case JTAG_STATEMOVE:
140 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
142 armjtagew_end_state(cmd->cmd.statemove->end_state);
143 armjtagew_state_move();
144 break;
146 case JTAG_PATHMOVE:
147 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
148 cmd->cmd.pathmove->num_states, \
149 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
151 armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
152 break;
154 case JTAG_SCAN:
155 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
157 armjtagew_end_state(cmd->cmd.scan->end_state);
159 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
160 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
162 #ifdef _DEBUG_USB_COMMS_
163 armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
164 #endif
165 type = jtag_scan_type(cmd->cmd.scan);
166 armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
167 break;
169 case JTAG_RESET:
170 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
172 armjtagew_tap_execute();
174 if (cmd->cmd.reset->trst == 1)
176 tap_set_state(TAP_RESET);
178 armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
179 break;
181 case JTAG_SLEEP:
182 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
183 armjtagew_tap_execute();
184 jtag_sleep(cmd->cmd.sleep->us);
185 break;
187 default:
188 LOG_ERROR("BUG: unknown JTAG command type encountered");
189 exit(-1);
191 cmd = cmd->next;
194 return armjtagew_tap_execute();
198 /* Sets speed in kHz. */
199 static int armjtagew_speed(int speed)
201 int result;
202 int speed_real;
205 usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
206 buf_set_u32(usb_out_buffer + 1, 0, 32, speed);
208 result = armjtagew_usb_message(armjtagew_handle, 4, 4);
210 if (result < 0)
212 LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
213 return ERROR_JTAG_DEVICE_ERROR;
216 usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
217 result = armjtagew_usb_message(armjtagew_handle, 1, 4);
218 speed_real = (int)buf_get_u32(usb_in_buffer,0,32);
219 if (result < 0)
221 LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
222 return ERROR_JTAG_DEVICE_ERROR;
224 else
226 LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
229 return ERROR_OK;
233 static int armjtagew_khz(int khz, int *jtag_speed)
235 *jtag_speed = khz;
237 return ERROR_OK;
240 static int armjtagew_init(void)
242 int check_cnt;
244 armjtagew_handle = armjtagew_usb_open();
246 if (armjtagew_handle == 0)
248 LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
249 return ERROR_JTAG_INIT_FAILED;
252 check_cnt = 0;
253 while (check_cnt < 3)
255 if (armjtagew_get_version_info() == ERROR_OK)
257 /* attempt to get status */
258 armjtagew_get_status();
259 break;
262 check_cnt++;
265 if (check_cnt == 3)
267 LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
270 LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
272 armjtagew_reset(0, 0);
273 armjtagew_tap_init();
275 return ERROR_OK;
278 static int armjtagew_quit(void)
280 armjtagew_usb_close(armjtagew_handle);
281 return ERROR_OK;
284 /***************************************************************************/
285 /* Queue command implementations */
287 static void armjtagew_end_state(tap_state_t state)
289 if (tap_is_state_stable(state))
291 tap_set_end_state(state);
293 else
295 LOG_ERROR("BUG: %i is not a valid end state", state);
296 exit(-1);
300 /* Goes to the end state. */
301 static void armjtagew_state_move(void)
303 int i;
304 int tms = 0;
305 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
306 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
308 for (i = 0; i < tms_count; i++)
310 tms = (tms_scan >> i) & 1;
311 armjtagew_tap_append_step(tms, 0);
314 tap_set_state(tap_get_end_state());
317 static void armjtagew_path_move(int num_states, tap_state_t *path)
319 int i;
321 for (i = 0; i < num_states; i++)
324 * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
325 * Either handle that here, or update the documentation with examples
326 * how to fix that in the configuration files.
328 if (path[i] == tap_state_transition(tap_get_state(), false))
330 armjtagew_tap_append_step(0, 0);
332 else if (path[i] == tap_state_transition(tap_get_state(), true))
334 armjtagew_tap_append_step(1, 0);
336 else
338 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
339 exit(-1);
342 tap_set_state(path[i]);
345 tap_set_end_state(tap_get_state());
348 static void armjtagew_runtest(int num_cycles)
350 int i;
352 tap_state_t saved_end_state = tap_get_end_state();
354 /* only do a state_move when we're not already in IDLE */
355 if (tap_get_state() != TAP_IDLE)
357 armjtagew_end_state(TAP_IDLE);
358 armjtagew_state_move();
361 /* execute num_cycles */
362 for (i = 0; i < num_cycles; i++)
364 armjtagew_tap_append_step(0, 0);
367 /* finish in end_state */
368 armjtagew_end_state(saved_end_state);
369 if (tap_get_state() != tap_get_end_state())
371 armjtagew_state_move();
375 static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
377 tap_state_t saved_end_state;
379 armjtagew_tap_ensure_space(1, scan_size + 8);
381 saved_end_state = tap_get_end_state();
383 /* Move to appropriate scan state */
384 armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
386 armjtagew_state_move();
387 armjtagew_end_state(saved_end_state);
389 /* Scan */
390 armjtagew_tap_append_scan(scan_size, buffer, command);
392 /* We are in Exit1, go to Pause */
393 armjtagew_tap_append_step(0, 0);
395 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
397 if (tap_get_state() != tap_get_end_state())
399 armjtagew_state_move();
403 static void armjtagew_reset(int trst, int srst)
405 const uint8_t trst_mask = (1u << 5);
406 const uint8_t srst_mask = (1u << 6);
407 uint8_t val = 0;
408 uint8_t outp_en = 0;
409 uint8_t change_mask = 0;
410 int result;
412 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
414 if (srst == 0)
416 val |= srst_mask;
417 outp_en &= ~srst_mask; /* tristate */
418 change_mask |= srst_mask;
420 else if (srst == 1)
422 val &= ~srst_mask;
423 outp_en |= srst_mask;
424 change_mask |= srst_mask;
427 if (trst == 0)
429 val |= trst_mask;
430 outp_en &= ~trst_mask; /* tristate */
431 change_mask |= trst_mask;
433 else if (trst == 1)
435 val &= ~trst_mask;
436 outp_en |= trst_mask;
437 change_mask |= trst_mask;
440 usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
441 usb_out_buffer[1] = val;
442 usb_out_buffer[2] = outp_en;
443 usb_out_buffer[3] = change_mask;
444 result = armjtagew_usb_write(armjtagew_handle, 4);
445 if (result != 4)
447 LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
452 static int armjtagew_get_status(void)
454 int result;
456 usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
457 result = armjtagew_usb_message(armjtagew_handle, 1, 12);
459 if (result == 0)
461 unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
462 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",
463 (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)),
464 (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)),
465 (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)),
466 (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)),
467 usb_in_buffer[9],
468 usb_in_buffer[11] ? "OVERCURRENT" : "OK",
469 usb_in_buffer[10] ? "enabled" : "disabled");
471 if (u_tg < 1500)
473 LOG_ERROR("Vref too low. Check Target Power\n");
476 else
478 LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)\n", result);
481 return ERROR_OK;
484 static int armjtagew_get_version_info(void)
486 int result;
487 char sn[16];
488 char auxinfo[257];
490 /* query hardware version */
491 usb_out_buffer[0] = CMD_GET_VERSION;
492 result = armjtagew_usb_message(armjtagew_handle, 1, 4 + 15 + 256);
494 if (result != 0)
496 LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)\n", result);
497 return ERROR_JTAG_DEVICE_ERROR;
501 memcpy(sn, usb_in_buffer + 4, 15);
502 sn[15] = '\0';
503 memcpy(auxinfo, usb_in_buffer + 4+15, 256);
504 auxinfo[256] = '\0';
506 LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
507 usb_in_buffer[1], usb_in_buffer[0], \
508 isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
509 sn, auxinfo);
510 return ERROR_OK;
513 COMMAND_HANDLER(armjtagew_handle_armjtagew_info_command)
515 if (armjtagew_get_version_info() == ERROR_OK)
517 /* attempt to get status */
518 armjtagew_get_status();
521 return ERROR_OK;
524 static int armjtagew_register_commands(struct command_context *cmd_ctx)
526 register_command(cmd_ctx, NULL, "armjtagew_info",
527 &armjtagew_handle_armjtagew_info_command, COMMAND_EXEC,
528 "query armjtagew info");
529 return ERROR_OK;
532 /***************************************************************************/
533 /* ARM-JTAG-EW tap functions */
535 /* 2048 is the max value we can use here */
536 #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
538 static int tap_length;
539 static uint8_t tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
540 static uint8_t tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
541 static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
543 struct pending_scan_result {
544 int first; /* First bit position in tdo_buffer to read */
545 int length; /* Number of bits to read */
546 struct scan_command *command; /* Corresponding scan command */
547 uint8_t *buffer;
550 #define MAX_PENDING_SCAN_RESULTS 256
552 static int pending_scan_results_length;
553 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
555 static int last_tms;
557 static void armjtagew_tap_init(void)
559 tap_length = 0;
560 pending_scan_results_length = 0;
563 static void armjtagew_tap_ensure_space(int scans, int bits)
565 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
566 int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
568 if (scans > available_scans || bits > available_bits)
570 armjtagew_tap_execute();
574 static void armjtagew_tap_append_step(int tms, int tdi)
576 last_tms = tms;
577 int index = tap_length / 8;
579 if (index < ARMJTAGEW_TAP_BUFFER_SIZE)
581 int bit_index = tap_length % 8;
582 uint8_t bit = 1 << bit_index;
584 if (tms)
586 tms_buffer[index] |= bit;
588 else
590 tms_buffer[index] &= ~bit;
593 if (tdi)
595 tdi_buffer[index] |= bit;
597 else
599 tdi_buffer[index] &= ~bit;
602 tap_length++;
604 else
606 LOG_ERROR("armjtagew_tap_append_step, overflow");
610 void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
612 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
613 int i;
615 pending_scan_result->first = tap_length;
616 pending_scan_result->length = length;
617 pending_scan_result->command = command;
618 pending_scan_result->buffer = buffer;
620 for (i = 0; i < length; i++)
622 armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
624 pending_scan_results_length++;
627 /* Pad and send a tap sequence to the device, and receive the answer.
628 * For the purpose of padding we assume that we are in idle or pause state. */
629 static int armjtagew_tap_execute(void)
631 int byte_length;
632 int tms_offset;
633 int tdi_offset;
634 int i;
635 int result;
637 if (tap_length > 0)
639 /* Pad last byte so that tap_length is divisible by 8 */
640 while (tap_length % 8 != 0)
642 /* More of the last TMS value keeps us in the same state,
643 * analogous to free-running JTAG interfaces. */
644 armjtagew_tap_append_step(last_tms, 0);
647 byte_length = tap_length / 8;
649 usb_out_buffer[0] = CMD_TAP_SHIFT;
650 buf_set_u32(usb_out_buffer + 1, 0, 16, byte_length);
652 tms_offset = 3;
653 for (i = 0; i < byte_length; i++)
655 usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8);
658 tdi_offset = tms_offset + byte_length;
659 for (i = 0; i < byte_length; i++)
661 usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8);
664 result = armjtagew_usb_message(armjtagew_handle, 3 + 2 * byte_length, byte_length + 4);
666 if (result == 0)
668 int stat;
670 stat = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
671 if (stat) {
672 LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat);
673 return ERROR_JTAG_QUEUE_FAILED;
676 for (i = 0; i < byte_length; i++)
678 tdo_buffer[i] = flip_u32(usb_in_buffer[i],8);
681 for (i = 0; i < pending_scan_results_length; i++)
683 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
684 uint8_t *buffer = pending_scan_result->buffer;
685 int length = pending_scan_result->length;
686 int first = pending_scan_result->first;
687 struct scan_command *command = pending_scan_result->command;
689 /* Copy to buffer */
690 buf_set_buf(tdo_buffer, first, buffer, 0, length);
692 DEBUG_JTAG_IO("pending scan result, length = %d", length);
694 #ifdef _DEBUG_USB_COMMS_
695 armjtagew_debug_buffer(buffer, byte_length);
696 #endif
698 if (jtag_read_buffer(buffer, command) != ERROR_OK)
700 armjtagew_tap_init();
701 return ERROR_JTAG_QUEUE_FAILED;
704 if (pending_scan_result->buffer != NULL)
706 free(pending_scan_result->buffer);
710 else
712 LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length);
713 return ERROR_JTAG_QUEUE_FAILED;
716 armjtagew_tap_init();
719 return ERROR_OK;
722 /*****************************************************************************/
723 /* JLink USB low-level functions */
725 static struct armjtagew* armjtagew_usb_open()
727 struct usb_bus *busses;
728 struct usb_bus *bus;
729 struct usb_device *dev;
731 struct armjtagew *result;
733 result = (struct armjtagew*) malloc(sizeof(struct armjtagew));
735 usb_init();
736 usb_find_busses();
737 usb_find_devices();
739 busses = usb_get_busses();
741 /* find armjtagew device in usb bus */
743 for (bus = busses; bus; bus = bus->next)
745 for (dev = bus->devices; dev; dev = dev->next)
747 if ((dev->descriptor.idVendor == USB_VID) && (dev->descriptor.idProduct == USB_PID))
749 result->usb_handle = usb_open(dev);
751 #if 0
752 /* usb_set_configuration required under win32 */
753 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
754 #endif
755 usb_claim_interface(result->usb_handle, 0);
757 #if 0
759 * This makes problems under Mac OS X. And is not needed
760 * under Windows. Hopefully this will not break a linux build
762 usb_set_altinterface(result->usb_handle, 0);
763 #endif
764 return result;
769 free(result);
770 return NULL;
773 static void armjtagew_usb_close(struct armjtagew *armjtagew)
775 usb_close(armjtagew->usb_handle);
776 free(armjtagew);
779 /* Send a message and receive the reply. */
780 static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length)
782 int result;
784 result = armjtagew_usb_write(armjtagew, out_length);
785 if (result == out_length)
787 result = armjtagew_usb_read(armjtagew, in_length);
788 if (result != in_length)
790 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
791 return -1;
794 else
796 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
797 return -1;
799 return 0;
802 /* Write data from out_buffer to USB. */
803 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length)
805 int result;
807 if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE)
809 LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE);
810 return -1;
813 result = usb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
814 (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
816 DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
818 #ifdef _DEBUG_USB_COMMS_
819 armjtagew_debug_buffer(usb_out_buffer, out_length);
820 #endif
821 return result;
824 /* Read data from USB into in_buffer. */
825 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
827 int result = usb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
828 (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
830 DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
832 #ifdef _DEBUG_USB_COMMS_
833 armjtagew_debug_buffer(usb_in_buffer, result);
834 #endif
835 return result;
839 #ifdef _DEBUG_USB_COMMS_
840 #define BYTES_PER_LINE 16
842 static void armjtagew_debug_buffer(uint8_t *buffer, int length)
844 char line[81];
845 char s[4];
846 int i;
847 int j;
849 for (i = 0; i < length; i += BYTES_PER_LINE)
851 snprintf(line, 5, "%04x", i);
852 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
854 snprintf(s, 4, " %02x", buffer[j]);
855 strcat(line, s);
857 LOG_DEBUG("%s", line);
860 #endif