16dac2a01b7f4f908bafb394963c27b7e8d194b3
[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 * 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 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "replacements.h"
26
27 #include "jtag.h"
28
29 #include <usb.h>
30 #include <string.h>
31
32 #include "log.h"
33
34 /* enable this to debug communication
35 */
36 #if 0
37 #define _DEBUG_USB_COMMS_
38 #endif
39
40 #ifdef _DEBUG_JTAG_IO_
41 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
42 #else
43 #define DEBUG_JTAG_IO(expr ...)
44 #endif
45
46 #define VID 0x1366
47 #define PID 0x0101
48
49 #define JLINK_WRITE_ENDPOINT 0x02
50 #define JLINK_READ_ENDPOINT 0x81
51
52 #define JLINK_USB_TIMEOUT 100
53
54 #define JLINK_IN_BUFFER_SIZE 2064
55 #define JLINK_OUT_BUFFER_SIZE 2064
56
57 /* Global USB buffers */
58 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
59 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
60
61 /* Constants for JLink command */
62
63 /* The JLINK_TAP_SEQUENCE_COMMAND is obsolete
64 * and we should use EMU_CMD_HW_JTAG instead */
65 #define JLINK_TAP_SEQUENCE_COMMAND 0xcd
66
67 #define EMU_CMD_VERSION 0x01
68 #define EMU_CMD_SET_SPEED 0x05
69 #define EMU_CMD_GET_STATE 0x07
70 #define EMU_CMD_HW_JTAG 0xcf
71 #define EMU_CMD_HW_RESET0 0xdc
72 #define EMU_CMD_HW_RESET1 0xdd
73 #define EMU_CMD_HW_TRST0 0xde
74 #define EMU_CMD_HW_TRST1 0xdf
75
76 /* max speed 12MHz v5.0 jlink */
77 #define JLINK_MAX_SPEED 12000
78
79 /* External interface functions */
80 int jlink_execute_queue(void);
81 int jlink_speed(int speed);
82 int jlink_khz(int khz, int *jtag_speed);
83 int jlink_register_commands(struct command_context_s *cmd_ctx);
84 int jlink_init(void);
85 int jlink_quit(void);
86
87 /* CLI command handler functions */
88 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
89
90 /* Queue command functions */
91 void jlink_end_state(enum tap_state state);
92 void jlink_state_move(void);
93 void jlink_path_move(int num_states, enum tap_state *path);
94 void jlink_runtest(int num_cycles);
95 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
96 void jlink_reset(int trst, int srst);
97 void jlink_simple_command(u8 command);
98 int jlink_get_status(void);
99
100 /* J-Link tap buffer functions */
101 void jlink_tap_init();
102 int jlink_tap_execute();
103 void jlink_tap_ensure_space(int scans, int bits);
104 void jlink_tap_append_step(int tms, int tdi);
105 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
106
107 /* Jlink lowlevel functions */
108 typedef struct jlink_jtag
109 {
110 struct usb_dev_handle* usb_handle;
111 } jlink_jtag_t;
112
113 jlink_jtag_t *jlink_usb_open(void);
114 void jlink_usb_close(jlink_jtag_t *jlink_jtag);
115 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
116 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
117 int jlink_usb_read(jlink_jtag_t *jlink_jtag);
118
119 /* helper functions */
120 int jlink_get_version_info(void);
121
122 #ifdef _DEBUG_USB_COMMS_
123 void jlink_debug_buffer(u8 *buffer, int length);
124 #endif
125
126 jlink_jtag_t* jlink_jtag_handle;
127
128 /***************************************************************************/
129 /* External interface implementation */
130
131 jtag_interface_t jlink_interface =
132 {
133 .name = "jlink",
134 .execute_queue = jlink_execute_queue,
135 .speed = jlink_speed,
136 .khz = jlink_khz,
137 .register_commands = jlink_register_commands,
138 .init = jlink_init,
139 .quit = jlink_quit
140 };
141
142 int jlink_execute_queue(void)
143 {
144 jtag_command_t *cmd = jtag_command_queue;
145 int scan_size;
146 enum scan_type type;
147 u8 *buffer;
148
149 while (cmd != NULL)
150 {
151 switch (cmd->type)
152 {
153 case JTAG_END_STATE:
154 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
155
156 if (cmd->cmd.end_state->end_state != -1)
157 {
158 jlink_end_state(cmd->cmd.end_state->end_state);
159 }
160 break;
161
162 case JTAG_RUNTEST:
163 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
164 cmd->cmd.runtest->end_state);
165
166 if (cmd->cmd.runtest->end_state != -1)
167 {
168 jlink_end_state(cmd->cmd.runtest->end_state);
169 }
170 jlink_runtest(cmd->cmd.runtest->num_cycles);
171 break;
172
173 case JTAG_STATEMOVE:
174 DEBUG_JTAG_IO("statemove end in %i",
175 cmd->cmd.statemove->end_state);
176
177 if (cmd->cmd.statemove->end_state != -1)
178 {
179 jlink_end_state(cmd->cmd.statemove->end_state);
180 }
181 jlink_state_move();
182 break;
183
184 case JTAG_PATHMOVE:
185 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
186 cmd->cmd.pathmove->num_states,
187 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
188
189 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
190 break;
191
192 case JTAG_SCAN:
193 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
194
195 if (cmd->cmd.scan->end_state != -1)
196 {
197 jlink_end_state(cmd->cmd.scan->end_state);
198 }
199
200 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
201 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
202
203 #ifdef _DEBUG_USB_COMMS_
204 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
205 #endif
206 type = jtag_scan_type(cmd->cmd.scan);
207 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
208 break;
209
210 case JTAG_RESET:
211 DEBUG_JTAG_IO("reset trst: %i srst %i",
212 cmd->cmd.reset->trst,
213 cmd->cmd.reset->srst);
214
215 jlink_tap_execute();
216
217 if (cmd->cmd.reset->trst == 1)
218 {
219 cur_state = TAP_TLR;
220 }
221 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
222 break;
223
224 case JTAG_SLEEP:
225 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
226 jlink_tap_execute();
227 jtag_sleep(cmd->cmd.sleep->us);
228 break;
229
230 default:
231 LOG_ERROR("BUG: unknown JTAG command type encountered");
232 exit(-1);
233 }
234 cmd = cmd->next;
235 }
236
237 return jlink_tap_execute();
238 }
239
240 /* Sets speed in kHz. */
241 int jlink_speed(int speed)
242 {
243 int result;
244
245 if (speed <= JLINK_MAX_SPEED)
246 {
247 /* check for RTCK setting */
248 if (speed == 0)
249 speed = -1;
250
251 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
252 usb_out_buffer[1] = (speed >> 0) & 0xff;
253 usb_out_buffer[2] = (speed >> 8) & 0xff;
254
255 result = jlink_usb_write(jlink_jtag_handle, 3);
256
257 if (result == 3)
258 {
259 return ERROR_OK;
260 }
261 else
262 {
263 LOG_ERROR("J-Link setting speed failed (%d)", result);
264 return ERROR_JTAG_DEVICE_ERROR;
265 }
266 }
267 else
268 {
269 LOG_INFO("Requested speed %dkHz exceeds maximum of %dkHz, ignored", speed, JLINK_MAX_SPEED);
270 }
271
272 return ERROR_OK;
273 }
274
275 int jlink_khz(int khz, int *jtag_speed)
276 {
277 *jtag_speed = khz;
278
279 return ERROR_OK;
280 }
281
282 int jlink_register_commands(struct command_context_s *cmd_ctx)
283 {
284 register_command(cmd_ctx, NULL, "jlink_info", jlink_handle_jlink_info_command, COMMAND_EXEC,
285 "query jlink info");
286 return ERROR_OK;
287 }
288
289 int jlink_init(void)
290 {
291 int check_cnt;
292
293 jlink_jtag_handle = jlink_usb_open();
294
295 if (jlink_jtag_handle == 0)
296 {
297 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
298 return ERROR_JTAG_INIT_FAILED;
299 }
300
301 check_cnt = 0;
302 while (check_cnt < 3)
303 {
304 if (jlink_get_version_info() == ERROR_OK)
305 {
306 /* attempt to get status */
307 jlink_get_status();
308 break;
309 }
310
311 check_cnt++;
312 }
313
314 if (check_cnt == 3)
315 {
316 LOG_INFO("J-Link initial read failed, don't worry");
317 }
318
319 LOG_INFO("J-Link JTAG Interface ready");
320
321 jlink_reset(0, 0);
322 jlink_tap_init();
323
324 return ERROR_OK;
325 }
326
327 int jlink_quit(void)
328 {
329 jlink_usb_close(jlink_jtag_handle);
330 return ERROR_OK;
331 }
332
333 /***************************************************************************/
334 /* Queue command implementations */
335
336 void jlink_end_state(enum tap_state state)
337 {
338 if (tap_move_map[state] != -1)
339 {
340 end_state = state;
341 }
342 else
343 {
344 LOG_ERROR("BUG: %i is not a valid end state", state);
345 exit(-1);
346 }
347 }
348
349 /* Goes to the end state. */
350 void jlink_state_move(void)
351 {
352 int i;
353 int tms = 0;
354 u8 tms_scan = TAP_MOVE(cur_state, end_state);
355
356 for (i = 0; i < 7; i++)
357 {
358 tms = (tms_scan >> i) & 1;
359 jlink_tap_append_step(tms, 0);
360 }
361
362 cur_state = end_state;
363 }
364
365 void jlink_path_move(int num_states, enum tap_state *path)
366 {
367 int i;
368
369 for (i = 0; i < num_states; i++)
370 {
371 if (path[i] == tap_transitions[cur_state].low)
372 {
373 jlink_tap_append_step(0, 0);
374 }
375 else if (path[i] == tap_transitions[cur_state].high)
376 {
377 jlink_tap_append_step(1, 0);
378 }
379 else
380 {
381 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
382 exit(-1);
383 }
384
385 cur_state = path[i];
386 }
387
388 end_state = cur_state;
389 }
390
391 void jlink_runtest(int num_cycles)
392 {
393 int i;
394
395 enum tap_state saved_end_state = end_state;
396
397 /* only do a state_move when we're not already in RTI */
398 if (cur_state != TAP_RTI)
399 {
400 jlink_end_state(TAP_RTI);
401 jlink_state_move();
402 }
403
404 /* execute num_cycles */
405 for (i = 0; i < num_cycles; i++)
406 {
407 jlink_tap_append_step(0, 0);
408 }
409
410 /* finish in end_state */
411 jlink_end_state(saved_end_state);
412 if (cur_state != end_state)
413 {
414 jlink_state_move();
415 }
416 }
417
418 void jlink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
419 {
420 enum tap_state saved_end_state;
421
422 jlink_tap_ensure_space(1, scan_size + 8);
423
424 saved_end_state = end_state;
425
426 /* Move to appropriate scan state */
427 jlink_end_state(ir_scan ? TAP_SI : TAP_SD);
428
429 jlink_state_move();
430 jlink_end_state(saved_end_state);
431
432 /* Scan */
433 jlink_tap_append_scan(scan_size, buffer, command);
434
435 /* We are in Exit1, go to Pause */
436 jlink_tap_append_step(0, 0);
437
438 cur_state = ir_scan ? TAP_PI : TAP_PD;
439
440 if (cur_state != end_state)
441 {
442 jlink_state_move();
443 }
444 }
445
446 void jlink_reset(int trst, int srst)
447 {
448 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
449
450 /* Signals are active low */
451 if (trst == 0)
452 {
453 jlink_simple_command(EMU_CMD_HW_TRST1);
454 }
455 else if (trst == 1)
456 {
457 jlink_simple_command(EMU_CMD_HW_TRST0);
458 }
459
460 if (srst == 0)
461 {
462 jlink_simple_command(EMU_CMD_HW_RESET1);
463 }
464 else if (srst == 1)
465 {
466 jlink_simple_command(EMU_CMD_HW_RESET0);
467 }
468 }
469
470 void jlink_simple_command(u8 command)
471 {
472 int result;
473
474 DEBUG_JTAG_IO("0x%02x", command);
475
476 usb_out_buffer[0] = command;
477 result = jlink_usb_write(jlink_jtag_handle, 1);
478
479 if (result != 1)
480 {
481 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
482 }
483 }
484
485 int jlink_get_status(void)
486 {
487 int result;
488
489 jlink_simple_command(EMU_CMD_GET_STATE);
490 result = jlink_usb_read(jlink_jtag_handle);
491
492 if(result == 8)
493 {
494 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
495 LOG_INFO("Vref = %d.%d TCK=%d TDI=%d TDO=%d TMS=%d SRST=%d TRST=%d\n", \
496 vref / 1000, vref % 1000, \
497 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
498 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
499
500 if(vref < 1500)
501 {
502 LOG_ERROR("Vref too low. Eventually the target isn't powered or disconnected?\n");
503 }
504 }
505 else
506 {
507 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
508 }
509
510 return ERROR_OK;
511 }
512
513 int jlink_get_version_info(void)
514 {
515 int result;
516 int len = 0;
517
518 /* query hardware version */
519 jlink_simple_command(EMU_CMD_VERSION);
520 result = jlink_usb_read(jlink_jtag_handle);
521
522 if (result == 2)
523 {
524 len = buf_get_u32(usb_in_buffer, 0, 16);
525 result = jlink_usb_read(jlink_jtag_handle);
526
527 if(result == len)
528 {
529 usb_in_buffer[result] = 0;
530 LOG_INFO(usb_in_buffer);
531 return ERROR_OK;
532 }
533 }
534
535 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
536 return ERROR_JTAG_DEVICE_ERROR;
537 }
538
539 int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
540 {
541 if (jlink_get_version_info() == ERROR_OK)
542 {
543 /* attempt to get status */
544 jlink_get_status();
545 }
546
547 return ERROR_OK;
548 }
549
550 /***************************************************************************/
551 /* J-Link tap functions */
552
553 /* We use the maximal value observed */
554 #define JLINK_TAP_BUFFER_SIZE 390
555
556 static int tap_length;
557 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
558 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
559 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
560
561 typedef struct
562 {
563 int first; /* First bit position in tdo_buffer to read */
564 int length; /* Number of bits to read */
565 scan_command_t *command; /* Corresponding scan command */
566 u8 *buffer;
567 } pending_scan_result_t;
568
569 #define MAX_PENDING_SCAN_RESULTS 16
570
571 static int pending_scan_results_length;
572 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
573
574 static int last_tms;
575
576 void jlink_tap_init()
577 {
578 tap_length = 0;
579 pending_scan_results_length = 0;
580 }
581
582 void jlink_tap_ensure_space(int scans, int bits)
583 {
584 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
585 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length;
586
587 if (scans > available_scans || bits > available_bits)
588 {
589 jlink_tap_execute();
590 }
591 }
592
593 void jlink_tap_append_step(int tms, int tdi)
594 {
595 last_tms = tms;
596 int index = tap_length / 8;
597
598 if (index < JLINK_TAP_BUFFER_SIZE)
599 {
600 int bit_index = tap_length % 8;
601 u8 bit = 1 << bit_index;
602
603 if (tms)
604 {
605 tms_buffer[index] |= bit;
606 }
607 else
608 {
609 tms_buffer[index] &= ~bit;
610 }
611
612 if (tdi)
613 {
614 tdi_buffer[index] |= bit;
615 }
616 else
617 {
618 tdi_buffer[index] &= ~bit;
619 }
620
621 tap_length++;
622 }
623 else
624 {
625 LOG_ERROR("jlink_tap_append_step, overflow");
626 }
627 }
628
629 void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
630 {
631 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
632 int i;
633
634 pending_scan_result->first = tap_length;
635 pending_scan_result->length = length;
636 pending_scan_result->command = command;
637 pending_scan_result->buffer = buffer;
638
639 for (i = 0; i < length; i++)
640 {
641 jlink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
642 }
643 pending_scan_results_length++;
644 }
645
646 /* Pad and send a tap sequence to the device, and receive the answer.
647 * For the purpose of padding we assume that we are in idle or pause state. */
648 int jlink_tap_execute()
649 {
650 int byte_length;
651 int tms_offset;
652 int tdi_offset;
653 int i;
654 int result;
655
656 if (tap_length > 0)
657 {
658 /* Pad last byte so that tap_length is divisible by 8 */
659 while (tap_length % 8 != 0)
660 {
661 /* More of the last TMS value keeps us in the same state,
662 * analogous to free-running JTAG interfaces. */
663 jlink_tap_append_step(last_tms, 0);
664 }
665
666 byte_length = tap_length / 8;
667
668 usb_out_buffer[0] = JLINK_TAP_SEQUENCE_COMMAND;
669 usb_out_buffer[1] = (tap_length >> 0) & 0xff;
670 usb_out_buffer[2] = (tap_length >> 8) & 0xff;
671
672 tms_offset = 3;
673 for (i = 0; i < byte_length; i++)
674 {
675 usb_out_buffer[tms_offset + i] = tms_buffer[i];
676 }
677
678 tdi_offset = tms_offset + byte_length;
679 for (i = 0; i < byte_length; i++)
680 {
681 usb_out_buffer[tdi_offset + i] = tdi_buffer[i];
682 }
683
684 result = jlink_usb_message(jlink_jtag_handle, 3 + 2 * byte_length, byte_length);
685
686 if (result == byte_length)
687 {
688 for (i = 0; i < byte_length; i++)
689 {
690 tdo_buffer[i] = usb_in_buffer[i];
691 }
692
693 for (i = 0; i < pending_scan_results_length; i++)
694 {
695 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
696 u8 *buffer = pending_scan_result->buffer;
697 int length = pending_scan_result->length;
698 int first = pending_scan_result->first;
699 scan_command_t *command = pending_scan_result->command;
700
701 /* Copy to buffer */
702 buf_set_buf(tdo_buffer, first, buffer, 0, length);
703
704 DEBUG_JTAG_IO("pending scan result, length = %d", length);
705
706 #ifdef _DEBUG_USB_COMMS_
707 jlink_debug_buffer(buffer, byte_length);
708 #endif
709
710 if (jtag_read_buffer(buffer, command) != ERROR_OK)
711 {
712 jlink_tap_init();
713 return ERROR_JTAG_QUEUE_FAILED;
714 }
715
716 if (pending_scan_result->buffer != NULL)
717 {
718 free(pending_scan_result->buffer);
719 }
720 }
721 }
722 else
723 {
724 LOG_ERROR("jlink_tap_execute, wrong result %d, expected %d", result, byte_length);
725 return ERROR_JTAG_QUEUE_FAILED;
726 }
727
728 jlink_tap_init();
729 }
730
731 return ERROR_OK;
732 }
733
734 /*****************************************************************************/
735 /* JLink USB low-level functions */
736
737 jlink_jtag_t* jlink_usb_open()
738 {
739 struct usb_bus *busses;
740 struct usb_bus *bus;
741 struct usb_device *dev;
742
743 jlink_jtag_t *result;
744
745 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
746
747 usb_init();
748 usb_find_busses();
749 usb_find_devices();
750
751 busses = usb_get_busses();
752
753 /* find jlink_jtag device in usb bus */
754
755 for (bus = busses; bus; bus = bus->next)
756 {
757 for (dev = bus->devices; dev; dev = dev->next)
758 {
759 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
760 {
761 result->usb_handle = usb_open(dev);
762
763 /* usb_set_configuration required under win32 */
764 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
765 usb_claim_interface(result->usb_handle, 0);
766 usb_set_altinterface(result->usb_handle, 0);
767 return result;
768 }
769 }
770 }
771
772 free(result);
773 return NULL;
774 }
775
776 void jlink_usb_close(jlink_jtag_t *jlink_jtag)
777 {
778 usb_close(jlink_jtag->usb_handle);
779 free(jlink_jtag);
780 }
781
782 /* Send a message and receive the reply. */
783 int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
784 {
785 int result;
786
787 result = jlink_usb_write(jlink_jtag, out_length);
788 if (result == out_length)
789 {
790 result = jlink_usb_read(jlink_jtag);
791 if (result == in_length)
792 {
793 return result;
794 }
795 else
796 {
797 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
798 return -1;
799 }
800 }
801 else
802 {
803 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
804 return -1;
805 }
806 }
807
808 /* Write data from out_buffer to USB. */
809 int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
810 {
811 int result;
812
813 if (out_length > JLINK_OUT_BUFFER_SIZE)
814 {
815 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
816 return -1;
817 }
818
819 result = usb_bulk_write(jlink_jtag->usb_handle, JLINK_WRITE_ENDPOINT, \
820 usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
821
822 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
823
824 #ifdef _DEBUG_USB_COMMS_
825 jlink_debug_buffer(usb_out_buffer, out_length);
826 #endif
827 return result;
828 }
829
830 /* Read data from USB into in_buffer. */
831 int jlink_usb_read(jlink_jtag_t *jlink_jtag)
832 {
833 int result = usb_bulk_read(jlink_jtag->usb_handle, JLINK_READ_ENDPOINT, \
834 usb_in_buffer, JLINK_IN_BUFFER_SIZE, JLINK_USB_TIMEOUT);
835
836 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
837
838 #ifdef _DEBUG_USB_COMMS_
839 jlink_debug_buffer(usb_in_buffer, result);
840 #endif
841 return result;
842 }
843
844 #ifdef _DEBUG_USB_COMMS_
845 #define BYTES_PER_LINE 16
846
847 void jlink_debug_buffer(u8 *buffer, int length)
848 {
849 char line[81];
850 char s[4];
851 int i;
852 int j;
853
854 for (i = 0; i < length; i += BYTES_PER_LINE)
855 {
856 snprintf(line, 5, "%04x", i);
857 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
858 {
859 snprintf(s, 4, " %02x", buffer[j]);
860 strcat(line, s);
861 }
862 LOG_DEBUG(line);
863 }
864 }
865 #endif