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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.Versaloon.com/.
29 #include <jtag/interface.h>
30 #include <jtag/commands.h>
32 #include "usb_common.h"
34 #include "versaloon/versaloon_include.h"
35 #include "versaloon/versaloon.h"
37 static int vsllink_tms_offset
;
39 struct pending_scan_result
{
42 int length
; /* Number of bits to read */
43 struct scan_command
*command
; /* Corresponding scan command */
46 bool last
; /* indicate the last scan pending */
49 #define MAX_PENDING_SCAN_RESULTS 256
51 static int pending_scan_results_length
;
52 static struct pending_scan_result
53 pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
55 /* Queue command functions */
56 static void vsllink_end_state(tap_state_t state
);
57 static void vsllink_state_move(void);
58 static void vsllink_path_move(int num_states
, tap_state_t
*path
);
59 static void vsllink_tms(int num_bits
, const uint8_t *bits
);
60 static void vsllink_runtest(int num_cycles
);
61 static void vsllink_stableclocks(int num_cycles
, int tms
);
62 static void vsllink_scan(bool ir_scan
, enum scan_type type
,
63 uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
64 static void vsllink_reset(int trst
, int srst
);
66 /* VSLLink tap buffer functions */
67 static void vsllink_tap_append_step(int tms
, int tdi
);
68 static void vsllink_tap_init(void);
69 static int vsllink_tap_execute(void);
70 static void vsllink_tap_ensure_pending(int scans
);
71 static void vsllink_tap_append_scan(int length
, uint8_t *buffer
,
72 struct scan_command
*command
);
74 /* VSLLink SWD functions */
75 static int_least32_t vsllink_swd_frequency(struct adiv5_dap
*dap
,
77 static int vsllink_swd_switch_seq(struct adiv5_dap
*dap
,
78 enum swd_special_seq seq
);
80 /* VSLLink lowlevel functions */
82 struct usb_dev_handle
*usb_handle
;
85 static struct vsllink
*vsllink_usb_open(void);
86 static void vsllink_usb_close(struct vsllink
*vsllink
);
88 #if defined _DEBUG_JTAG_IO_
89 static void vsllink_debug_buffer(uint8_t *buffer
, int length
);
92 static int tap_length
;
93 static int tap_buffer_size
;
94 static uint8_t *tms_buffer
;
95 static uint8_t *tdi_buffer
;
96 static uint8_t *tdo_buffer
;
99 static int queued_retval
;
101 struct vsllink
*vsllink_handle
;
103 static int vsllink_execute_queue(void)
105 struct jtag_command
*cmd
= jtag_command_queue
;
110 DEBUG_JTAG_IO("-------------------------------------"
112 "-------------------------------------");
114 while (cmd
!= NULL
) {
117 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
118 cmd
->cmd
.runtest
->num_cycles
,
119 tap_state_name(cmd
->cmd
.runtest
->end_state
));
121 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
122 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
126 DEBUG_JTAG_IO("statemove end in %s",
127 tap_state_name(cmd
->cmd
.statemove
->end_state
));
129 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
130 vsllink_state_move();
134 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
135 cmd
->cmd
.pathmove
->num_states
,
136 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
138 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
142 DEBUG_JTAG_IO("JTAG Scan...");
144 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
146 scan_size
= jtag_build_buffer(
147 cmd
->cmd
.scan
, &buffer
);
149 if (cmd
->cmd
.scan
->ir_scan
)
151 "JTAG Scan write IR(%d bits), "
154 tap_state_name(cmd
->cmd
.scan
->end_state
));
158 "JTAG Scan write DR(%d bits), "
161 tap_state_name(cmd
->cmd
.scan
->end_state
));
163 #ifdef _DEBUG_JTAG_IO_
164 vsllink_debug_buffer(buffer
,
165 DIV_ROUND_UP(scan_size
, 8));
168 type
= jtag_scan_type(cmd
->cmd
.scan
);
170 vsllink_scan(cmd
->cmd
.scan
->ir_scan
,
171 type
, buffer
, scan_size
,
176 DEBUG_JTAG_IO("reset trst: %i srst %i",
177 cmd
->cmd
.reset
->trst
,
178 cmd
->cmd
.reset
->srst
);
180 vsllink_tap_execute();
182 if (cmd
->cmd
.reset
->trst
== 1)
183 tap_set_state(TAP_RESET
);
185 vsllink_reset(cmd
->cmd
.reset
->trst
,
186 cmd
->cmd
.reset
->srst
);
190 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
191 vsllink_tap_execute();
192 jtag_sleep(cmd
->cmd
.sleep
->us
);
195 case JTAG_STABLECLOCKS
:
196 DEBUG_JTAG_IO("add %d clocks",
197 cmd
->cmd
.stableclocks
->num_cycles
);
199 switch (tap_get_state()) {
201 /* tms must be '1' to stay
211 /* else, tms should be '0' */
214 /* above stable states are OK */
216 LOG_ERROR("jtag_add_clocks() "
217 "in non-stable state \"%s\"",
218 tap_state_name(tap_get_state())
222 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
226 DEBUG_JTAG_IO("add %d jtag tms",
227 cmd
->cmd
.tms
->num_bits
);
229 vsllink_tms(cmd
->cmd
.tms
->num_bits
, cmd
->cmd
.tms
->bits
);
233 LOG_ERROR("BUG: unknown JTAG command type "
234 "encountered: %d", cmd
->type
);
240 return vsllink_tap_execute();
243 static int vsllink_speed(int speed
)
248 versaloon_interface
.adaptors
.jtag_raw
.config(0, (uint16_t)speed
);
249 return versaloon_interface
.adaptors
.peripheral_commit();
252 static int vsllink_khz(int khz
, int *jtag_speed
)
259 static int vsllink_speed_div(int jtag_speed
, int *khz
)
266 static void vsllink_free_buffer(void)
268 if (tdi_buffer
!= NULL
) {
272 if (tdo_buffer
!= NULL
) {
276 if (tms_buffer
!= NULL
) {
282 static int vsllink_quit(void)
284 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
| GPIO_TRST
,
285 0, 0, GPIO_SRST
| GPIO_TRST
);
286 versaloon_interface
.adaptors
.gpio
.fini(0);
289 versaloon_interface
.adaptors
.swd
.fini(0);
291 versaloon_interface
.adaptors
.jtag_raw
.fini(0);
293 versaloon_interface
.adaptors
.peripheral_commit();
294 versaloon_interface
.fini();
296 vsllink_free_buffer();
297 vsllink_usb_close(vsllink_handle
);
302 static int vsllink_interface_init(void)
304 vsllink_handle
= vsllink_usb_open();
305 if (vsllink_handle
== 0) {
306 LOG_ERROR("Can't find USB JTAG Interface!" \
307 "Please check connection and permissions.");
308 return ERROR_JTAG_INIT_FAILED
;
310 LOG_DEBUG("vsllink found on %04X:%04X",
311 versaloon_interface
.usb_setting
.vid
,
312 versaloon_interface
.usb_setting
.pid
);
313 versaloon_usb_device_handle
= vsllink_handle
->usb_handle
;
315 if (ERROR_OK
!= versaloon_interface
.init())
317 if (versaloon_interface
.usb_setting
.buf_size
< 32) {
318 versaloon_interface
.fini();
325 static int vsllink_init(void)
327 int retval
= vsllink_interface_init();
328 if (ERROR_OK
!= retval
)
331 versaloon_interface
.adaptors
.gpio
.init(0);
332 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
, 0, GPIO_SRST
,
334 versaloon_interface
.adaptors
.delay
.delayms(100);
335 versaloon_interface
.adaptors
.peripheral_commit();
338 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_TRST
, 0,
339 GPIO_TRST
, GPIO_TRST
);
340 versaloon_interface
.adaptors
.swd
.init(0);
341 vsllink_swd_frequency(NULL
, jtag_get_speed_khz() * 1000);
342 vsllink_swd_switch_seq(NULL
, JTAG_TO_SWD
);
345 /* malloc buffer size for tap */
346 tap_buffer_size
= versaloon_interface
.usb_setting
.buf_size
/ 2 - 32;
347 vsllink_free_buffer();
348 tdi_buffer
= malloc(tap_buffer_size
);
349 tdo_buffer
= malloc(tap_buffer_size
);
350 tms_buffer
= malloc(tap_buffer_size
);
351 if ((NULL
== tdi_buffer
) || (NULL
== tdo_buffer
) || (NULL
== tms_buffer
)) {
356 versaloon_interface
.adaptors
.jtag_raw
.init(0);
357 versaloon_interface
.adaptors
.jtag_raw
.config(0, jtag_get_speed_khz());
358 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
| GPIO_TRST
,
359 GPIO_TRST
, GPIO_SRST
, GPIO_SRST
);
362 if (ERROR_OK
!= versaloon_interface
.adaptors
.peripheral_commit())
370 /**************************************************************************
371 * Queue command implementations */
373 static void vsllink_end_state(tap_state_t state
)
375 if (tap_is_state_stable(state
))
376 tap_set_end_state(state
);
378 LOG_ERROR("BUG: %i is not a valid end state", state
);
383 /* Goes to the end state. */
384 static void vsllink_state_move(void)
387 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(),
388 tap_get_end_state());
389 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(),
390 tap_get_end_state());
392 for (i
= 0; i
< tms_scan_bits
; i
++)
393 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
395 tap_set_state(tap_get_end_state());
398 static void vsllink_path_move(int num_states
, tap_state_t
*path
)
400 for (int i
= 0; i
< num_states
; i
++) {
401 if (path
[i
] == tap_state_transition(tap_get_state(), false))
402 vsllink_tap_append_step(0, 0);
403 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
404 vsllink_tap_append_step(1, 0);
406 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
407 tap_state_name(tap_get_state()),
408 tap_state_name(path
[i
]));
412 tap_set_state(path
[i
]);
415 tap_set_end_state(tap_get_state());
418 static void vsllink_tms(int num_bits
, const uint8_t *bits
)
420 for (int i
= 0; i
< num_bits
; i
++)
421 vsllink_tap_append_step((bits
[i
/ 8] >> (i
% 8)) & 1, 0);
424 static void vsllink_stableclocks(int num_cycles
, int tms
)
426 while (num_cycles
> 0) {
427 vsllink_tap_append_step(tms
, 0);
432 static void vsllink_runtest(int num_cycles
)
434 tap_state_t saved_end_state
= tap_get_end_state();
436 if (tap_get_state() != TAP_IDLE
) {
437 /* enter IDLE state */
438 vsllink_end_state(TAP_IDLE
);
439 vsllink_state_move();
442 vsllink_stableclocks(num_cycles
, 0);
446 vsllink_end_state(saved_end_state
);
447 if (tap_get_end_state() != tap_get_end_state())
448 vsllink_state_move();
451 static void vsllink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
452 int scan_size
, struct scan_command
*command
)
454 tap_state_t saved_end_state
;
456 saved_end_state
= tap_get_end_state();
458 /* Move to appropriate scan state */
459 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
461 if (tap_get_state() != tap_get_end_state())
462 vsllink_state_move();
463 vsllink_end_state(saved_end_state
);
466 vsllink_tap_append_scan(scan_size
, buffer
, command
);
468 /* Goto Pause and record position to insert tms:0 */
469 vsllink_tap_append_step(0, 0);
470 vsllink_tms_offset
= tap_length
;
472 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
474 if (tap_get_state() != tap_get_end_state())
475 vsllink_state_move();
478 static void vsllink_reset(int trst
, int srst
)
480 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
483 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
, 0, GPIO_SRST
, GPIO_SRST
);
485 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
, GPIO_SRST
, 0, 0);
489 versaloon_interface
.adaptors
.gpio
.out(0, GPIO_TRST
, GPIO_TRST
);
491 versaloon_interface
.adaptors
.gpio
.out(0, GPIO_TRST
, 0);
494 versaloon_interface
.adaptors
.peripheral_commit();
497 COMMAND_HANDLER(vsllink_handle_usb_vid_command
)
500 return ERROR_COMMAND_SYNTAX_ERROR
;
502 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0],
503 versaloon_interface
.usb_setting
.vid
);
507 COMMAND_HANDLER(vsllink_handle_usb_pid_command
)
510 return ERROR_COMMAND_SYNTAX_ERROR
;
511 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0],
512 versaloon_interface
.usb_setting
.pid
);
516 COMMAND_HANDLER(vsllink_handle_usb_serial_command
)
519 return ERROR_COMMAND_SYNTAX_ERROR
;
521 free(versaloon_interface
.usb_setting
.serialstring
);
524 versaloon_interface
.usb_setting
.serialstring
= strdup(CMD_ARGV
[0]);
526 versaloon_interface
.usb_setting
.serialstring
= NULL
;
531 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command
)
534 return ERROR_COMMAND_SYNTAX_ERROR
;
536 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0],
537 versaloon_interface
.usb_setting
.ep_in
);
539 versaloon_interface
.usb_setting
.ep_in
|= 0x80;
544 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command
)
547 return ERROR_COMMAND_SYNTAX_ERROR
;
549 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0],
550 versaloon_interface
.usb_setting
.ep_out
);
552 versaloon_interface
.usb_setting
.ep_out
&= ~0x80;
557 COMMAND_HANDLER(vsllink_handle_usb_interface_command
)
560 return ERROR_COMMAND_SYNTAX_ERROR
;
562 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0],
563 versaloon_interface
.usb_setting
.interface
);
567 /**************************************************************************
568 * VSLLink tap functions */
570 static void vsllink_tap_init(void)
573 pending_scan_results_length
= 0;
574 vsllink_tms_offset
= 0;
577 static void vsllink_tap_ensure_pending(int scans
)
579 int available_scans
=
580 MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
582 if (scans
> available_scans
)
583 vsllink_tap_execute();
586 static void vsllink_tap_append_step(int tms
, int tdi
)
588 int index_var
= tap_length
/ 8;
590 int bit_index
= tap_length
% 8;
591 uint8_t bit
= 1 << bit_index
;
594 tms_buffer
[index_var
] |= bit
;
596 tms_buffer
[index_var
] &= ~bit
;
599 tdi_buffer
[index_var
] |= bit
;
601 tdi_buffer
[index_var
] &= ~bit
;
605 if (tap_buffer_size
* 8 <= tap_length
)
606 vsllink_tap_execute();
609 static void vsllink_tap_append_scan(int length
, uint8_t *buffer
,
610 struct scan_command
*command
)
612 struct pending_scan_result
*pending_scan_result
;
613 int len_tmp
, len_all
, i
;
616 while (len_all
< length
) {
617 vsllink_tap_ensure_pending(1);
618 pending_scan_result
=
619 &pending_scan_results_buffer
[
620 pending_scan_results_length
];
622 if ((length
- len_all
) > (tap_buffer_size
* 8 - tap_length
)) {
623 /* Use all memory available
624 vsllink_tap_append_step will commit automatically */
625 len_tmp
= tap_buffer_size
* 8 - tap_length
;
626 pending_scan_result
->last
= false;
628 len_tmp
= length
- len_all
;
629 pending_scan_result
->last
= true;
631 pending_scan_result
->src_offset
= tap_length
;
632 pending_scan_result
->dest_offset
= len_all
;
633 pending_scan_result
->length
= len_tmp
;
634 pending_scan_result
->command
= command
;
635 pending_scan_result
->buffer
= buffer
;
636 pending_scan_results_length
++;
638 for (i
= 0; i
< len_tmp
; i
++) {
639 vsllink_tap_append_step(((len_all
+ i
) < length
-1
641 (buffer
[(len_all
+ i
)/8]
642 >> ((len_all
+ i
)%8)) & 1);
649 static int vsllink_jtag_execute(void)
657 versaloon_interface
.adaptors
.jtag_raw
.execute(0, tdi_buffer
, tms_buffer
,
658 tdo_buffer
, tap_length
);
660 result
= versaloon_interface
.adaptors
.peripheral_commit();
662 if (result
== ERROR_OK
) {
663 for (i
= 0; i
< pending_scan_results_length
; i
++) {
664 struct pending_scan_result
*pending_scan_result
=
665 &pending_scan_results_buffer
[i
];
666 uint8_t *buffer
= pending_scan_result
->buffer
;
667 int length
= pending_scan_result
->length
;
668 int src_first
= pending_scan_result
->src_offset
;
669 int dest_first
= pending_scan_result
->dest_offset
;
670 bool last
= pending_scan_result
->last
;
672 struct scan_command
*command
;
674 command
= pending_scan_result
->command
;
675 buf_set_buf(tdo_buffer
, src_first
, buffer
, dest_first
, length
);
677 #ifdef _DEBUG_JTAG_IO_
679 "JTAG scan read(%d bits, from src %d bits to dest %d bits):",
680 length
, src_first
, dest_first
);
681 vsllink_debug_buffer(buffer
+ dest_first
/ 8,
682 DIV_ROUND_UP(length
, 7));
686 if (jtag_read_buffer(buffer
, command
)
689 return ERROR_JTAG_QUEUE_FAILED
;
692 if (pending_scan_result
->buffer
!= NULL
)
693 free(pending_scan_result
->buffer
);
697 LOG_ERROR("vsllink_jtag_execute failure");
698 return ERROR_JTAG_QUEUE_FAILED
;
706 static int vsllink_tap_execute(void)
711 return vsllink_jtag_execute();
714 static int vsllink_swd_init(void)
716 LOG_INFO("VSLLink SWD mode enabled");
722 static int_least32_t vsllink_swd_frequency(struct adiv5_dap
*dap
,
725 const int_least32_t delay2hz
[] = {
726 1850000, 235000, 130000, 102000, 85000, 72000
730 uint16_t delay
= UINT16_MAX
;
732 for (uint16_t i
= 0; i
< ARRAY_SIZE(delay2hz
); i
++) {
733 if (hz
>= delay2hz
[i
]) {
740 if (delay
== UINT16_MAX
)
741 delay
= (500000 / hz
) - 1;
743 /* Calculate retry count after a WAIT response. This will give
744 * a retry timeout at about ~250 ms. 54 is the number of bits
745 * found in a transaction. */
746 uint16_t retry_count
= 250 * hz
/ 1000 / 54;
748 LOG_DEBUG("SWD delay: %d, retry count: %d", delay
, retry_count
);
750 versaloon_interface
.adaptors
.swd
.config(0, 2, retry_count
, delay
);
751 queued_retval
= versaloon_interface
.adaptors
.peripheral_commit();
757 static int vsllink_swd_switch_seq(struct adiv5_dap
*dap
,
758 enum swd_special_seq seq
)
762 LOG_DEBUG("SWD line reset");
763 versaloon_interface
.adaptors
.swd
.seqout(0, swd_seq_line_reset
,
764 swd_seq_line_reset_len
);
767 LOG_DEBUG("JTAG-to-SWD");
768 versaloon_interface
.adaptors
.swd
.seqout(0, swd_seq_jtag_to_swd
,
769 swd_seq_jtag_to_swd_len
);
772 LOG_DEBUG("SWD-to-JTAG");
773 versaloon_interface
.adaptors
.swd
.seqout(0, swd_seq_swd_to_jtag
,
774 swd_seq_swd_to_jtag_len
);
777 LOG_ERROR("Sequence %d not supported", seq
);
781 return versaloon_interface
.adaptors
.peripheral_commit();
784 static void vsllink_swd_read_reg(struct adiv5_dap
*dap
, uint8_t cmd
,
787 if (queued_retval
!= ERROR_OK
)
794 versaloon_interface
.adaptors
.swd
.transact(0, cmd
, &val
, &ack
);
795 retval
= versaloon_interface
.adaptors
.peripheral_commit();
797 if (retval
!= ERROR_OK
) {
798 queued_retval
= ERROR_FAIL
;
810 queued_retval
= retval
;
813 static void vsllink_swd_write_reg(struct adiv5_dap
*dap
, uint8_t cmd
,
816 if (queued_retval
!= ERROR_OK
)
822 versaloon_interface
.adaptors
.swd
.transact(0, cmd
, &value
, &ack
);
823 retval
= versaloon_interface
.adaptors
.peripheral_commit();
825 if (retval
!= ERROR_OK
) {
826 queued_retval
= ERROR_FAIL
;
835 queued_retval
= retval
;
838 static int vsllink_swd_run_queue(struct adiv5_dap
*dap
)
840 int retval
= queued_retval
;
841 queued_retval
= ERROR_OK
;
845 /****************************************************************************
846 * VSLLink USB low-level functions */
848 static uint8_t usb_check_string(usb_dev_handle
*usb
, uint8_t stringidx
,
849 char *string
, char *buff
, uint16_t buf_size
)
857 buff
= malloc(buf_size
);
860 goto free_and_return
;
866 len
= usb_get_string_simple(usb
, stringidx
, buff
, buf_size
);
867 if ((len
< 0) || ((size_t)len
!= strlen(buff
))) {
869 goto free_and_return
;
873 if ((string
!= NULL
) && strcmp(buff
, string
)) {
875 goto free_and_return
;
879 if (alloced
&& (buff
!= NULL
)) {
886 static usb_dev_handle
*find_usb_device(uint16_t VID
, uint16_t PID
, uint8_t interface
,
887 char *serialstring
, char *productstring
)
889 usb_dev_handle
*dev_handle
= NULL
;
890 struct usb_bus
*busses
;
892 struct usb_device
*dev
;
897 busses
= usb_get_busses();
899 for (bus
= busses
; bus
; bus
= bus
->next
) {
900 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
901 if ((dev
->descriptor
.idVendor
== VID
)
902 && (dev
->descriptor
.idProduct
== PID
)) {
903 dev_handle
= usb_open(dev
);
904 if (NULL
== dev_handle
) {
905 LOG_ERROR("failed to open %04X:%04X, %s", VID
, PID
,
910 /* check description string */
911 if ((productstring
!= NULL
&& !usb_check_string(dev_handle
,
912 dev
->descriptor
.iProduct
, productstring
, NULL
, 0))
913 || (serialstring
!= NULL
&& !usb_check_string(dev_handle
,
914 dev
->descriptor
.iSerialNumber
, serialstring
, NULL
, 0))) {
915 usb_close(dev_handle
);
920 if (usb_claim_interface(dev_handle
, interface
) != 0) {
921 LOG_ERROR(ERRMSG_FAILURE_OPERATION_MESSAGE
,
922 "claim interface", usb_strerror());
923 usb_close(dev_handle
);
928 if (dev_handle
!= NULL
)
937 static struct vsllink
*vsllink_usb_open(void)
941 struct usb_dev_handle
*dev
;
943 dev
= find_usb_device(versaloon_interface
.usb_setting
.vid
,
944 versaloon_interface
.usb_setting
.pid
,
945 versaloon_interface
.usb_setting
.interface
,
946 versaloon_interface
.usb_setting
.serialstring
, "Versaloon");
950 struct vsllink
*result
= malloc(sizeof(struct vsllink
));
951 result
->usb_handle
= dev
;
955 static void vsllink_usb_close(struct vsllink
*vsllink
)
959 ret
= usb_release_interface(vsllink
->usb_handle
,
960 versaloon_interface
.usb_setting
.interface
);
962 LOG_ERROR("fail to release interface %d, %d returned",
963 versaloon_interface
.usb_setting
.interface
, ret
);
967 ret
= usb_close(vsllink
->usb_handle
);
969 LOG_ERROR("fail to close usb, %d returned", ret
);
976 #define BYTES_PER_LINE 16
978 #if defined _DEBUG_JTAG_IO_
979 static void vsllink_debug_buffer(uint8_t *buffer
, int length
)
986 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
987 snprintf(line
, 5, "%04x", i
);
988 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
989 snprintf(s
, 4, " %02x", buffer
[j
]);
992 LOG_DEBUG("%s", line
);
995 #endif /* _DEBUG_JTAG_IO_ */
997 static const struct command_registration vsllink_command_handlers
[] = {
999 .name
= "vsllink_usb_vid",
1000 .handler
= &vsllink_handle_usb_vid_command
,
1001 .mode
= COMMAND_CONFIG
,
1004 .name
= "vsllink_usb_pid",
1005 .handler
= &vsllink_handle_usb_pid_command
,
1006 .mode
= COMMAND_CONFIG
,
1009 .name
= "vsllink_usb_serial",
1010 .handler
= &vsllink_handle_usb_serial_command
,
1011 .mode
= COMMAND_CONFIG
,
1014 .name
= "vsllink_usb_bulkin",
1015 .handler
= &vsllink_handle_usb_bulkin_command
,
1016 .mode
= COMMAND_CONFIG
,
1019 .name
= "vsllink_usb_bulkout",
1020 .handler
= &vsllink_handle_usb_bulkout_command
,
1021 .mode
= COMMAND_CONFIG
,
1024 .name
= "vsllink_usb_interface",
1025 .handler
= &vsllink_handle_usb_interface_command
,
1026 .mode
= COMMAND_CONFIG
,
1028 COMMAND_REGISTRATION_DONE
1031 static const char * const vsllink_transports
[] = {"jtag", "swd", NULL
};
1033 static const struct swd_driver vsllink_swd_driver
= {
1034 .init
= vsllink_swd_init
,
1035 .frequency
= vsllink_swd_frequency
,
1036 .switch_seq
= vsllink_swd_switch_seq
,
1037 .read_reg
= vsllink_swd_read_reg
,
1038 .write_reg
= vsllink_swd_write_reg
,
1039 .run
= vsllink_swd_run_queue
,
1042 struct jtag_interface vsllink_interface
= {
1044 .supported
= DEBUG_CAP_TMS_SEQ
,
1045 .commands
= vsllink_command_handlers
,
1046 .transports
= vsllink_transports
,
1047 .swd
= &vsllink_swd_driver
,
1049 .init
= vsllink_init
,
1050 .quit
= vsllink_quit
,
1052 .speed
= vsllink_speed
,
1053 .speed_div
= vsllink_speed_div
,
1054 .execute_queue
= vsllink_execute_queue
,