1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 /* Versaloon is a programming tool for multiple MCUs.
21 * It's distributed under GPLv3.
22 * You can find it at http://www.SimonQian.com/en/Versaloon.
29 #include <jtag/interface.h>
30 #include <jtag/commands.h>
31 #include "usb_common.h"
33 //#define _VSLLINK_IN_DEBUG_MODE_
35 static uint16_t vsllink_usb_vid
;
36 static uint16_t vsllink_usb_pid
;
37 static uint8_t vsllink_usb_bulkout
;
38 static uint8_t vsllink_usb_bulkin
;
39 static uint8_t vsllink_usb_interface
;
40 static int VSLLINK_USB_TIMEOUT
= 1000;
42 static int vsllink_tms_offset
;
44 /* Global USB buffers */
45 static uint8_t *vsllink_usb_in_buffer
;
46 static uint8_t *vsllink_usb_out_buffer
;
47 static int vsllink_buffer_size
= 128;
49 /* Constants for Versaloon command */
50 #define VERSALOON_GET_INFO 0x00
51 #define VERSALOON_GET_TVCC 0x01
53 /* Constants for VSLLink command */
54 #define VSLLINK_CMD_CONN 0x80
55 #define VSLLINK_CMD_DISCONN 0x81
56 #define VSLLINK_CMD_SET_SPEED 0x82
57 #define VSLLINK_CMD_SET_PORT 0x90
58 #define VSLLINK_CMD_GET_PORT 0x91
59 #define VSLLINK_CMD_SET_PORTDIR 0x92
60 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
61 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
62 #define VSLLINK_CMD_HW_SWDCMD 0xA2
63 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
65 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
66 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
67 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
69 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
70 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
72 #define JTAG_PINMSK_SRST (1 << 0)
73 #define JTAG_PINMSK_TRST (1 << 1)
74 #define JTAG_PINMSK_USR1 (1 << 2)
75 #define JTAG_PINMSK_USR2 (1 << 3)
76 #define JTAG_PINMSK_TCK (1 << 4)
77 #define JTAG_PINMSK_TMS (1 << 5)
78 #define JTAG_PINMSK_TDI (1 << 6)
79 #define JTAG_PINMSK_TDO (1 << 7)
81 struct pending_scan_result
{
84 int length
; /* Number of bits to read */
85 struct scan_command
*command
; /* Corresponding scan command */
87 bool last
; /* indicate the last scan pending */
90 #define MAX_PENDING_SCAN_RESULTS 256
92 static int pending_scan_results_length
;
93 static struct pending_scan_result
94 pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
96 /* Queue command functions */
97 static void vsllink_end_state(tap_state_t state
);
98 static void vsllink_state_move(void);
99 static void vsllink_path_move(int num_states
, tap_state_t
*path
);
100 static void vsllink_runtest(int num_cycles
);
101 static void vsllink_stableclocks(int num_cycles
, int tms
);
102 static void vsllink_scan(bool ir_scan
, enum scan_type type
,
103 uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
104 static void vsllink_reset(int trst
, int srst
);
105 static void vsllink_simple_command(uint8_t command
);
107 /* VSLLink tap buffer functions */
108 static void vsllink_tap_append_step(int tms
, int tdi
);
109 static void vsllink_tap_init(void);
110 static int vsllink_tap_execute(void);
111 static void vsllink_tap_ensure_pending(int scans
);
112 static void vsllink_tap_append_scan(int length
, uint8_t *buffer
,
113 struct scan_command
*command
);
115 /* VSLLink lowlevel functions */
117 struct usb_dev_handle
*usb_handle
;
120 static struct vsllink
*vsllink_usb_open(void);
121 static void vsllink_usb_close(struct vsllink
*vsllink
);
122 static int vsllink_usb_message(struct vsllink
*vsllink
, int out_length
,
124 static int vsllink_usb_write(struct vsllink
*vsllink
, int out_length
);
125 static int vsllink_usb_read(struct vsllink
*vsllink
);
127 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
128 static void vsllink_debug_buffer(uint8_t *buffer
, int length
);
131 static int tap_length
;
132 static int tap_buffer_size
;
133 static uint8_t *tms_buffer
;
134 static uint8_t *tdi_buffer
;
135 static uint8_t *tdo_buffer
;
137 static struct vsllink
*vsllink_handle
;
139 static void reset_command_pointer(void)
144 static int vsllink_execute_queue(void)
146 struct jtag_command
*cmd
= jtag_command_queue
;
151 DEBUG_JTAG_IO("-------------------------------------"
153 "-------------------------------------");
155 reset_command_pointer();
156 while (cmd
!= NULL
) {
159 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
160 cmd
->cmd
.runtest
->num_cycles
,
161 tap_state_name(cmd
->cmd
.runtest
164 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
165 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
169 DEBUG_JTAG_IO("statemove end in %s",
170 tap_state_name(cmd
->cmd
.statemove
173 vsllink_end_state(cmd
->cmd
.statemove
175 vsllink_state_move();
179 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
180 cmd
->cmd
.pathmove
->num_states
,
181 tap_state_name(cmd
->cmd
.pathmove
182 ->path
[cmd
->cmd
.pathmove
186 cmd
->cmd
.pathmove
->num_states
,
187 cmd
->cmd
.pathmove
->path
);
191 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
193 scan_size
= jtag_build_buffer(
194 cmd
->cmd
.scan
, &buffer
);
196 if (cmd
->cmd
.scan
->ir_scan
)
198 "JTAG Scan write IR(%d bits), "
201 tap_state_name(cmd
->cmd
.scan
206 "JTAG Scan write DR(%d bits), "
209 tap_state_name(cmd
->cmd
.scan
212 #ifdef _DEBUG_JTAG_IO_
213 vsllink_debug_buffer(buffer
,
214 DIV_ROUND_UP(scan_size
, 8));
217 type
= jtag_scan_type(cmd
->cmd
.scan
);
219 vsllink_scan(cmd
->cmd
.scan
->ir_scan
,
220 type
, buffer
, scan_size
,
225 DEBUG_JTAG_IO("reset trst: %i srst %i",
226 cmd
->cmd
.reset
->trst
,
227 cmd
->cmd
.reset
->srst
);
229 vsllink_tap_execute();
231 if (cmd
->cmd
.reset
->trst
== 1)
232 tap_set_state(TAP_RESET
);
234 vsllink_reset(cmd
->cmd
.reset
->trst
,
235 cmd
->cmd
.reset
->srst
);
239 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
240 vsllink_tap_execute();
241 jtag_sleep(cmd
->cmd
.sleep
->us
);
244 case JTAG_STABLECLOCKS
:
245 DEBUG_JTAG_IO("add %d clocks",
246 cmd
->cmd
.stableclocks
->num_cycles
);
247 switch (tap_get_state()) {
249 /* tms must be '1' to stay
259 /* else, tms should be '0' */
262 /* above stable states are OK */
264 LOG_ERROR("jtag_add_clocks() "
265 "in non-stable state \"%s\"",
266 tap_state_name(tap_get_state())
270 vsllink_stableclocks(cmd
->cmd
.stableclocks
271 ->num_cycles
, scan_size
);
275 LOG_ERROR("BUG: unknown JTAG command type "
276 "encountered: %d", cmd
->type
);
282 return vsllink_tap_execute();
285 static int vsllink_speed(int speed
)
289 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_SPEED
;
290 vsllink_usb_out_buffer
[1] = (speed
>> 0) & 0xff;
291 vsllink_usb_out_buffer
[2] = (speed
>> 8) & 0xFF;
293 result
= vsllink_usb_write(vsllink_handle
, 3);
298 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
299 return ERROR_JTAG_DEVICE_ERROR
;
305 static int vsllink_khz(int khz
, int *jtag_speed
)
312 static int vsllink_speed_div(int jtag_speed
, int *khz
)
319 static int vsllink_init(void)
321 int check_cnt
, to_tmp
;
323 char version_str
[100];
325 vsllink_usb_in_buffer
= malloc(vsllink_buffer_size
);
326 vsllink_usb_out_buffer
= malloc(vsllink_buffer_size
);
327 if ((vsllink_usb_in_buffer
== NULL
)
328 || (vsllink_usb_out_buffer
== NULL
)) {
329 LOG_ERROR("Not enough memory");
333 vsllink_handle
= vsllink_usb_open();
334 if (vsllink_handle
== 0) {
335 LOG_ERROR("Can't find USB JTAG Interface!"\
336 "Please check connection and permissions.");
337 return ERROR_JTAG_INIT_FAILED
;
339 LOG_DEBUG("vsllink found on %04X:%04X",
340 vsllink_usb_vid
, vsllink_usb_pid
);
342 to_tmp
= VSLLINK_USB_TIMEOUT
;
343 VSLLINK_USB_TIMEOUT
= 100;
345 while (check_cnt
< 5) {
346 vsllink_simple_command(VERSALOON_GET_INFO
);
347 result
= vsllink_usb_read(vsllink_handle
);
350 vsllink_usb_in_buffer
[result
] = 0;
351 vsllink_buffer_size
= vsllink_usb_in_buffer
[0]
352 + (vsllink_usb_in_buffer
[1] << 8);
353 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2,
354 sizeof(version_str
));
355 LOG_INFO("%s", version_str
);
357 /* free the pre-allocated memory */
358 free(vsllink_usb_in_buffer
);
359 free(vsllink_usb_out_buffer
);
360 vsllink_usb_in_buffer
= NULL
;
361 vsllink_usb_out_buffer
= NULL
;
363 /* alloc new memory */
364 vsllink_usb_in_buffer
= malloc(vsllink_buffer_size
);
365 vsllink_usb_out_buffer
= malloc(vsllink_buffer_size
);
366 if ((vsllink_usb_in_buffer
== NULL
) ||
367 (vsllink_usb_out_buffer
== NULL
)) {
368 LOG_ERROR("Not enough memory");
371 LOG_INFO("buffer size for USB is %d bytes",
372 vsllink_buffer_size
);
374 /* alloc tms/tdi/tdo buffer */
375 tap_buffer_size
= (vsllink_buffer_size
- 3) / 2;
376 tms_buffer
= (uint8_t *)malloc(tap_buffer_size
);
377 tdi_buffer
= (uint8_t *)malloc(tap_buffer_size
);
378 tdo_buffer
= (uint8_t *)malloc(tap_buffer_size
);
379 if ((tms_buffer
== NULL
) || (tdi_buffer
== NULL
) ||
380 (tdo_buffer
== NULL
)) {
381 LOG_ERROR("Not enough memory");
386 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
389 if (check_cnt
== 3) {
390 /* Fail to access Versaloon */
391 LOG_ERROR("VSLLink initial failed");
394 VSLLINK_USB_TIMEOUT
= to_tmp
;
396 /* Some older firmware versions sometimes fail if the
397 * voltage isn't read first.
399 vsllink_simple_command(VERSALOON_GET_TVCC
);
400 result
= vsllink_usb_read(vsllink_handle
);
402 LOG_WARNING("Fail to get target voltage");
404 LOG_INFO("Target runs at %d mV", vsllink_usb_in_buffer
[0] +
405 (vsllink_usb_in_buffer
[1] << 8));
407 /* connect to vsllink */
408 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_CONN
;
409 vsllink_usb_out_buffer
[1] = 1;
410 vsllink_usb_message(vsllink_handle
, 2, 0);
411 if (vsllink_usb_read(vsllink_handle
) > 2) {
412 strncpy(version_str
, (char *)vsllink_usb_in_buffer
+ 2,
413 sizeof(version_str
));
414 LOG_INFO("%s", version_str
);
417 /* Set SRST and TRST to output, Set USR1 and USR2 to input */
418 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
419 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
420 | JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
421 vsllink_usb_out_buffer
[2] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
422 if (vsllink_usb_write(vsllink_handle
, 3) != 3) {
423 LOG_ERROR("VSLLink USB send data error");
429 LOG_INFO("VSLLink Interface ready");
436 static int vsllink_quit(void)
438 if ((vsllink_usb_in_buffer
!= NULL
)
439 && (vsllink_usb_out_buffer
!= NULL
)) {
440 // Set all pins to input
441 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORTDIR
;
442 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
443 | JTAG_PINMSK_USR1
| JTAG_PINMSK_USR2
;
444 vsllink_usb_out_buffer
[2] = 0;
445 if (vsllink_usb_write(vsllink_handle
, 3) != 3) {
446 LOG_ERROR("VSLLink USB send data error");
451 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
452 vsllink_usb_close(vsllink_handle
);
453 vsllink_handle
= NULL
;
456 if (vsllink_usb_in_buffer
!= NULL
) {
457 free(vsllink_usb_in_buffer
);
458 vsllink_usb_in_buffer
= NULL
;
460 if (vsllink_usb_out_buffer
!= NULL
) {
461 free(vsllink_usb_out_buffer
);
462 vsllink_usb_out_buffer
= NULL
;
468 /***************************************************************************/
469 /* Queue command implementations */
471 static void vsllink_end_state(tap_state_t state
)
473 if (tap_is_state_stable(state
))
474 tap_set_end_state(state
);
476 LOG_ERROR("BUG: %i is not a valid end state", state
);
481 /* Goes to the end state. */
482 static void vsllink_state_move(void)
485 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(),
486 tap_get_end_state());
487 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(),
488 tap_get_end_state());
490 for (i
= 0; i
< tms_scan_bits
; i
++)
491 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
493 tap_set_state(tap_get_end_state());
496 static void vsllink_path_move(int num_states
, tap_state_t
*path
)
498 for (int i
= 0; i
< num_states
; i
++) {
499 if (path
[i
] == tap_state_transition(tap_get_state(), false))
500 vsllink_tap_append_step(0, 0);
501 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
502 vsllink_tap_append_step(1, 0);
504 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
505 tap_state_name(tap_get_state()),
506 tap_state_name(path
[i
]));
510 tap_set_state(path
[i
]);
513 tap_set_end_state(tap_get_state());
516 static void vsllink_stableclocks(int num_cycles
, int tms
)
518 while (num_cycles
> 0) {
519 vsllink_tap_append_step(tms
, 0);
524 static void vsllink_runtest(int num_cycles
)
526 tap_state_t saved_end_state
= tap_get_end_state();
528 if (tap_get_state() != TAP_IDLE
) {
529 /* enter IDLE state */
530 vsllink_end_state(TAP_IDLE
);
531 vsllink_state_move();
534 vsllink_stableclocks(num_cycles
, 0);
538 vsllink_end_state(saved_end_state
);
539 if (tap_get_end_state() != tap_get_end_state())
540 vsllink_state_move();
543 static void vsllink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
544 int scan_size
, struct scan_command
*command
)
546 tap_state_t saved_end_state
;
548 saved_end_state
= tap_get_end_state();
550 /* Move to appropriate scan state */
551 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
553 if (tap_get_state() != tap_get_end_state())
554 vsllink_state_move();
555 vsllink_end_state(saved_end_state
);
558 vsllink_tap_append_scan(scan_size
, buffer
, command
);
560 /* Goto Pause and record position to insert tms:0 */
561 vsllink_tap_append_step(0, 0);
562 vsllink_tms_offset
= tap_length
;
564 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
566 if (tap_get_state() != tap_get_end_state())
567 vsllink_state_move();
570 static void vsllink_reset(int trst
, int srst
)
574 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
576 /* Signals are active low */
577 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_SET_PORT
;
578 vsllink_usb_out_buffer
[1] = JTAG_PINMSK_SRST
| JTAG_PINMSK_TRST
;
579 vsllink_usb_out_buffer
[2] = 0;
581 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
583 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
585 result
= vsllink_usb_write(vsllink_handle
, 3);
587 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)",
591 static void vsllink_simple_command(uint8_t command
)
595 DEBUG_JTAG_IO("0x%02x", command
);
597 vsllink_usb_out_buffer
[0] = command
;
598 result
= vsllink_usb_write(vsllink_handle
, 1);
601 LOG_ERROR("VSLLink command 0x%02x failed (%d)",
605 COMMAND_HANDLER(vsllink_handle_mode_command
)
608 LOG_ERROR("parameter error, "
609 "should be one parameter for mode");
616 COMMAND_HANDLER(vsllink_handle_usb_vid_command
)
619 LOG_ERROR("parameter error, "
620 "should be one parameter for VID");
624 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], vsllink_usb_vid
);
628 COMMAND_HANDLER(vsllink_handle_usb_pid_command
)
631 LOG_ERROR("parameter error, "
632 "should be one parameter for PID");
635 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], vsllink_usb_pid
);
639 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command
)
642 LOG_ERROR("parameter error, "
643 "should be one parameter for BULKIN endpoint");
647 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], vsllink_usb_bulkin
);
649 vsllink_usb_bulkin
|= 0x80;
654 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command
)
657 LOG_ERROR("parameter error, "
658 "should be one parameter for BULKOUT endpoint");
662 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], vsllink_usb_bulkout
);
664 vsllink_usb_bulkout
&= ~0x80;
669 COMMAND_HANDLER(vsllink_handle_usb_interface_command
)
672 LOG_ERROR("parameter error, "
673 "should be one parameter for interface number");
677 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0], vsllink_usb_interface
);
681 /***************************************************************************/
682 /* VSLLink tap functions */
684 static void vsllink_tap_init(void)
687 pending_scan_results_length
= 0;
688 vsllink_tms_offset
= 0;
691 static void vsllink_tap_ensure_pending(int scans
)
693 int available_scans
=
694 MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
696 if (scans
> available_scans
)
697 vsllink_tap_execute();
700 static void vsllink_tap_append_step(int tms
, int tdi
)
702 int index_var
= tap_length
/ 8;
704 int bit_index
= tap_length
% 8;
705 uint8_t bit
= 1 << bit_index
;
708 tms_buffer
[index_var
] |= bit
;
710 tms_buffer
[index_var
] &= ~bit
;
713 tdi_buffer
[index_var
] |= bit
;
715 tdi_buffer
[index_var
] &= ~bit
;
719 if (tap_buffer_size
* 8 <= tap_length
)
720 vsllink_tap_execute();
723 static void vsllink_tap_append_scan(int length
, uint8_t *buffer
,
724 struct scan_command
*command
)
726 struct pending_scan_result
*pending_scan_result
;
727 int len_tmp
, len_all
, i
;
730 while (len_all
< length
) {
731 vsllink_tap_ensure_pending(1);
732 pending_scan_result
=
733 &pending_scan_results_buffer
[
734 pending_scan_results_length
];
736 if ((length
- len_all
) > (tap_buffer_size
* 8 - tap_length
)) {
737 /* Use all memory available
738 vsllink_tap_append_step will commit automatically */
739 len_tmp
= tap_buffer_size
* 8 - tap_length
;
740 pending_scan_result
->last
= false;
742 len_tmp
= length
- len_all
;
743 pending_scan_result
->last
= true;
745 pending_scan_result
->src_offset
= tap_length
;
746 pending_scan_result
->dest_offset
= len_all
;
747 pending_scan_result
->length
= len_tmp
;
748 pending_scan_result
->command
= command
;
749 pending_scan_result
->buffer
= buffer
;
750 pending_scan_results_length
++;
752 for (i
= 0; i
< len_tmp
; i
++) {
753 vsllink_tap_append_step(((len_all
+ i
) < length
-1
755 (buffer
[(len_all
+ i
)/8]
756 >> ((len_all
+ i
)%8)) & 1);
763 static int vsllink_tap_execute(void)
772 /* Pad data so that tap_length is divisible by 8 */
773 if ((tap_length
% 8) != 0) {
774 if (vsllink_tms_offset
> 0) {
775 /* append tms:0 at vsllink_tms_offset,
778 int start_pos
= DIV_ROUND_UP(tap_length
, 8) - 1;
779 int end_pos
= DIV_ROUND_UP(vsllink_tms_offset
, 8) - 1;
780 int shift_cnt
= (start_pos
+ 1) * 8 - tap_length
;
781 uint8_t last_mask
= ~(
782 (1 << (vsllink_tms_offset
% 8)) - 1);
785 if (start_pos
== end_pos
) {
786 tms_buffer
[start_pos
] =
787 (tms_buffer
[start_pos
]
789 | ((tms_buffer
[start_pos
]
792 tdi_buffer
[start_pos
] =
793 (tdi_buffer
[start_pos
]
796 ((tdi_buffer
[start_pos
]
800 } else if (start_pos
== (end_pos
+ 1)) {
801 tms_buffer
[start_pos
] =
802 (tms_buffer
[start_pos
]
804 ((tms_buffer
[start_pos
- 1]
807 tdi_buffer
[start_pos
] =
808 (tdi_buffer
[start_pos
]
810 ((tdi_buffer
[start_pos
- 1]
814 tms_buffer
[start_pos
] =
815 (tms_buffer
[start_pos
]
817 (tms_buffer
[start_pos
- 1]
819 tdi_buffer
[start_pos
] =
820 (tdi_buffer
[start_pos
]
822 (tdi_buffer
[start_pos
- 1]
827 tap_length
= DIV_ROUND_UP(tap_length
, 8) * 8;
829 /* append data at last */
830 while ((tap_length
% 8) != 0) {
831 vsllink_tap_append_step(
832 (tap_get_state() == TAP_RESET
)
837 byte_length
= tap_length
/ 8;
839 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGRAWCMD
;
840 vsllink_usb_out_buffer
[1] = ((byte_length
* 2 + 3) >> 0) & 0xff;
841 vsllink_usb_out_buffer
[2] = ((byte_length
* 2 + 3) >> 8) & 0xff;
843 memcpy(&vsllink_usb_out_buffer
[3], tdi_buffer
, byte_length
);
844 memcpy(&vsllink_usb_out_buffer
[3 + byte_length
], tms_buffer
,
847 result
= vsllink_usb_message(vsllink_handle
, 3 + 2 * byte_length
,
850 if (result
== byte_length
) {
851 for (i
= 0; i
< pending_scan_results_length
; i
++) {
852 struct pending_scan_result
*pending_scan_result
=
853 &pending_scan_results_buffer
[i
];
854 uint8_t *buffer
= pending_scan_result
->buffer
;
855 int length
= pending_scan_result
->length
;
856 int src_first
= pending_scan_result
->src_offset
;
857 int dest_first
= pending_scan_result
->dest_offset
;
858 bool last
= pending_scan_result
->last
;
860 struct scan_command
*command
;
862 command
= pending_scan_result
->command
;
863 buf_set_buf(vsllink_usb_in_buffer
, src_first
, buffer
,
866 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):",
868 #ifdef _DEBUG_JTAG_IO_
869 vsllink_debug_buffer(buffer
+ dest_first
/ 8,
870 DIV_ROUND_UP(length
, 7));
874 if (jtag_read_buffer(buffer
, command
)
877 return ERROR_JTAG_QUEUE_FAILED
;
880 if (pending_scan_result
->buffer
!= NULL
)
881 free(pending_scan_result
->buffer
);
885 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d",
886 result
, byte_length
);
887 return ERROR_JTAG_QUEUE_FAILED
;
895 /*****************************************************************************/
896 /* VSLLink USB low-level functions */
898 static struct vsllink
*vsllink_usb_open(void)
902 const uint16_t vids
[] = { vsllink_usb_vid
, 0 };
903 const uint16_t pids
[] = { vsllink_usb_pid
, 0 };
904 struct usb_dev_handle
*dev
;
905 if (jtag_usb_open(vids
, pids
, &dev
) != ERROR_OK
)
908 /* usb_set_configuration required under win32 */
909 struct usb_device
*udev
= usb_device(dev
);
910 int ret
= usb_set_configuration(dev
,
911 udev
->config
[0].bConfigurationValue
);
913 LOG_ERROR("fail to set configuration to %d (error %d)."
914 "Not enough permissions for the device?",
915 udev
->config
[0].bConfigurationValue
, ret
);
918 ret
= usb_claim_interface(dev
, vsllink_usb_interface
);
920 LOG_ERROR("fail to claim interface %d, %d returned",
921 vsllink_usb_interface
, ret
);
926 * This makes problems under Mac OS X. And is not needed
927 * under Windows. Hopefully this will not break a linux build
929 usb_set_altinterface(dev
, 0);
932 struct vsllink
*result
= malloc(sizeof(struct vsllink
));
933 result
->usb_handle
= dev
;
937 static void vsllink_usb_close(struct vsllink
*vsllink
)
941 ret
= usb_release_interface(vsllink
->usb_handle
,
942 vsllink_usb_interface
);
944 LOG_ERROR("fail to release interface %d, %d returned",
945 vsllink_usb_interface
, ret
);
949 ret
= usb_close(vsllink
->usb_handle
);
951 LOG_ERROR("fail to close usb, %d returned", ret
);
958 /* Send a message and receive the reply. */
959 static int vsllink_usb_message(struct vsllink
*vsllink
, int out_length
,
964 result
= vsllink_usb_write(vsllink
, out_length
);
965 if (result
== out_length
) {
967 result
= vsllink_usb_read(vsllink
);
968 if (result
== in_length
)
971 LOG_ERROR("usb_bulk_read failed "
972 "(requested=%d, result=%d)",
979 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
985 /* Write data from out_buffer to USB. */
986 static int vsllink_usb_write(struct vsllink
*vsllink
, int out_length
)
990 if (out_length
> vsllink_buffer_size
) {
991 LOG_ERROR("vsllink_write illegal out_length=%d (max=%d)",
992 out_length
, vsllink_buffer_size
);
996 result
= usb_bulk_write(vsllink
->usb_handle
, vsllink_usb_bulkout
,
997 (char *)vsllink_usb_out_buffer
, out_length
,
998 VSLLINK_USB_TIMEOUT
);
1000 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d",
1001 out_length
, result
);
1003 #ifdef _DEBUG_USB_COMMS_
1004 LOG_DEBUG("USB out:");
1005 vsllink_debug_buffer(vsllink_usb_out_buffer
, out_length
);
1008 #ifdef _VSLLINK_IN_DEBUG_MODE_
1015 /* Read data from USB into in_buffer. */
1016 static int vsllink_usb_read(struct vsllink
*vsllink
)
1018 int result
= usb_bulk_read(vsllink
->usb_handle
, vsllink_usb_bulkin
,
1019 (char *)vsllink_usb_in_buffer
, vsllink_buffer_size
,
1020 VSLLINK_USB_TIMEOUT
);
1022 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result
);
1024 #ifdef _DEBUG_USB_COMMS_
1025 LOG_DEBUG("USB in:");
1026 vsllink_debug_buffer(vsllink_usb_in_buffer
, result
);
1031 #define BYTES_PER_LINE 16
1033 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1034 static void vsllink_debug_buffer(uint8_t *buffer
, int length
)
1041 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
1042 snprintf(line
, 5, "%04x", i
);
1043 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
1044 snprintf(s
, 4, " %02x", buffer
[j
]);
1047 LOG_DEBUG("%s", line
);
1050 #endif /* _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_ */
1052 static const struct command_registration vsllink_command_handlers
[] = {
1054 .name
= "vsllink_usb_vid",
1055 .handler
= &vsllink_handle_usb_vid_command
,
1056 .mode
= COMMAND_CONFIG
,
1059 .name
= "vsllink_usb_pid",
1060 .handler
= &vsllink_handle_usb_pid_command
,
1061 .mode
= COMMAND_CONFIG
,
1064 .name
= "vsllink_usb_bulkin",
1065 .handler
= &vsllink_handle_usb_bulkin_command
,
1066 .mode
= COMMAND_CONFIG
,
1069 .name
= "vsllink_usb_bulkout",
1070 .handler
= &vsllink_handle_usb_bulkout_command
,
1071 .mode
= COMMAND_CONFIG
,
1074 .name
= "vsllink_usb_interface",
1075 .handler
= &vsllink_handle_usb_interface_command
,
1076 .mode
= COMMAND_CONFIG
,
1079 .name
= "vsllink_mode",
1080 .handler
= &vsllink_handle_mode_command
,
1081 .mode
= COMMAND_CONFIG
,
1083 COMMAND_REGISTRATION_DONE
1086 struct jtag_interface vsllink_interface
= {
1088 .commands
= vsllink_command_handlers
,
1090 .init
= vsllink_init
,
1091 .quit
= vsllink_quit
,
1093 .speed
= vsllink_speed
,
1094 .speed_div
= vsllink_speed_div
,
1095 .execute_queue
= vsllink_execute_queue
,