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, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
18 /* Versaloon is a programming tool for multiple MCUs.
19 * It's distributed under GPLv3.
20 * You can find it at http://www.Versaloon.com/.
27 #include <jtag/interface.h>
28 #include <jtag/commands.h>
32 #include "versaloon/versaloon_include.h"
33 #include "versaloon/versaloon.h"
35 static int vsllink_tms_offset
;
37 struct pending_scan_result
{
40 int length
; /* Number of bits to read */
41 struct scan_command
*command
; /* Corresponding scan command */
44 bool last
; /* indicate the last scan pending */
47 #define MAX_PENDING_SCAN_RESULTS 256
49 static int pending_scan_results_length
;
50 static struct pending_scan_result
51 pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
53 /* Queue command functions */
54 static void vsllink_end_state(tap_state_t state
);
55 static void vsllink_state_move(void);
56 static void vsllink_path_move(int num_states
, tap_state_t
*path
);
57 static void vsllink_tms(int num_bits
, const uint8_t *bits
);
58 static void vsllink_runtest(int num_cycles
);
59 static void vsllink_stableclocks(int num_cycles
, int tms
);
60 static void vsllink_scan(bool ir_scan
, enum scan_type type
,
61 uint8_t *buffer
, int scan_size
, struct scan_command
*command
);
62 static void vsllink_reset(int trst
, int srst
);
64 /* VSLLink tap buffer functions */
65 static void vsllink_tap_append_step(int tms
, int tdi
);
66 static void vsllink_tap_init(void);
67 static int vsllink_tap_execute(void);
68 static void vsllink_tap_ensure_pending(int scans
);
69 static void vsllink_tap_append_scan(int length
, uint8_t *buffer
,
70 struct scan_command
*command
);
72 /* VSLLink SWD functions */
73 static int_least32_t vsllink_swd_frequency(int_least32_t hz
);
74 static int vsllink_swd_switch_seq(enum swd_special_seq seq
);
76 /* VSLLink lowlevel functions */
78 struct libusb_context
*libusb_ctx
;
79 struct libusb_device_handle
*usb_device_handle
;
82 static int vsllink_usb_open(struct vsllink
*vsllink
);
83 static void vsllink_usb_close(struct vsllink
*vsllink
);
85 #if defined _DEBUG_JTAG_IO_
86 static void vsllink_debug_buffer(uint8_t *buffer
, int length
);
89 static int tap_length
;
90 static int tap_buffer_size
;
91 static uint8_t *tms_buffer
;
92 static uint8_t *tdi_buffer
;
93 static uint8_t *tdo_buffer
;
97 static struct vsllink
*vsllink_handle
;
99 static int vsllink_execute_queue(void)
101 struct jtag_command
*cmd
= jtag_command_queue
;
106 DEBUG_JTAG_IO("-------------------------------------"
108 "-------------------------------------");
110 while (cmd
!= NULL
) {
113 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
114 cmd
->cmd
.runtest
->num_cycles
,
115 tap_state_name(cmd
->cmd
.runtest
->end_state
));
117 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
118 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
122 DEBUG_JTAG_IO("statemove end in %s",
123 tap_state_name(cmd
->cmd
.statemove
->end_state
));
125 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
126 vsllink_state_move();
130 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
131 cmd
->cmd
.pathmove
->num_states
,
132 tap_state_name(cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]));
134 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
138 DEBUG_JTAG_IO("JTAG Scan...");
140 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
142 scan_size
= jtag_build_buffer(
143 cmd
->cmd
.scan
, &buffer
);
145 if (cmd
->cmd
.scan
->ir_scan
)
147 "JTAG Scan write IR(%d bits), "
150 tap_state_name(cmd
->cmd
.scan
->end_state
));
154 "JTAG Scan write DR(%d bits), "
157 tap_state_name(cmd
->cmd
.scan
->end_state
));
159 #ifdef _DEBUG_JTAG_IO_
160 vsllink_debug_buffer(buffer
,
161 DIV_ROUND_UP(scan_size
, 8));
164 type
= jtag_scan_type(cmd
->cmd
.scan
);
166 vsllink_scan(cmd
->cmd
.scan
->ir_scan
,
167 type
, buffer
, scan_size
,
172 DEBUG_JTAG_IO("reset trst: %i srst %i",
173 cmd
->cmd
.reset
->trst
,
174 cmd
->cmd
.reset
->srst
);
176 vsllink_tap_execute();
178 if (cmd
->cmd
.reset
->trst
== 1)
179 tap_set_state(TAP_RESET
);
181 vsllink_reset(cmd
->cmd
.reset
->trst
,
182 cmd
->cmd
.reset
->srst
);
186 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
187 vsllink_tap_execute();
188 jtag_sleep(cmd
->cmd
.sleep
->us
);
191 case JTAG_STABLECLOCKS
:
192 DEBUG_JTAG_IO("add %d clocks",
193 cmd
->cmd
.stableclocks
->num_cycles
);
195 switch (tap_get_state()) {
197 /* tms must be '1' to stay
207 /* else, tms should be '0' */
210 /* above stable states are OK */
212 LOG_ERROR("jtag_add_clocks() "
213 "in non-stable state \"%s\"",
214 tap_state_name(tap_get_state())
218 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
222 DEBUG_JTAG_IO("add %d jtag tms",
223 cmd
->cmd
.tms
->num_bits
);
225 vsllink_tms(cmd
->cmd
.tms
->num_bits
, cmd
->cmd
.tms
->bits
);
229 LOG_ERROR("BUG: unknown JTAG command type "
230 "encountered: %d", cmd
->type
);
236 return vsllink_tap_execute();
239 static int vsllink_speed(int speed
)
242 vsllink_swd_frequency(speed
* 1000);
246 versaloon_interface
.adaptors
.jtag_raw
.config(0, (uint16_t)speed
);
247 return versaloon_interface
.adaptors
.peripheral_commit();
250 static int vsllink_khz(int khz
, int *jtag_speed
)
257 static int vsllink_speed_div(int jtag_speed
, int *khz
)
264 static void vsllink_free_buffer(void)
266 if (tdi_buffer
!= NULL
) {
270 if (tdo_buffer
!= NULL
) {
274 if (tms_buffer
!= NULL
) {
280 static int vsllink_quit(void)
282 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
| GPIO_TRST
,
283 0, 0, GPIO_SRST
| GPIO_TRST
);
284 versaloon_interface
.adaptors
.gpio
.fini(0);
287 versaloon_interface
.adaptors
.swd
.fini(0);
289 versaloon_interface
.adaptors
.jtag_raw
.fini(0);
291 versaloon_interface
.adaptors
.peripheral_commit();
292 versaloon_interface
.fini();
294 vsllink_free_buffer();
295 vsllink_usb_close(vsllink_handle
);
297 free(vsllink_handle
);
302 static int vsllink_interface_init(void)
304 vsllink_handle
= malloc(sizeof(struct vsllink
));
305 if (NULL
== vsllink_handle
) {
306 LOG_ERROR("unable to allocate memory");
310 libusb_init(&vsllink_handle
->libusb_ctx
);
312 if (ERROR_OK
!= vsllink_usb_open(vsllink_handle
)) {
313 LOG_ERROR("Can't find USB JTAG Interface!" \
314 "Please check connection and permissions.");
315 return ERROR_JTAG_INIT_FAILED
;
317 LOG_DEBUG("vsllink found on %04X:%04X",
318 versaloon_interface
.usb_setting
.vid
,
319 versaloon_interface
.usb_setting
.pid
);
320 versaloon_usb_device_handle
= vsllink_handle
->usb_device_handle
;
322 if (ERROR_OK
!= versaloon_interface
.init())
324 if (versaloon_interface
.usb_setting
.buf_size
< 32) {
325 versaloon_interface
.fini();
332 static int vsllink_init(void)
334 int retval
= vsllink_interface_init();
335 if (ERROR_OK
!= retval
)
338 versaloon_interface
.adaptors
.gpio
.init(0);
339 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
, 0, GPIO_SRST
,
341 versaloon_interface
.adaptors
.delay
.delayms(100);
342 versaloon_interface
.adaptors
.peripheral_commit();
345 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_TRST
, 0,
346 GPIO_TRST
, GPIO_TRST
);
347 versaloon_interface
.adaptors
.swd
.init(0);
348 vsllink_swd_frequency(jtag_get_speed_khz() * 1000);
349 vsllink_swd_switch_seq(JTAG_TO_SWD
);
352 /* malloc buffer size for tap */
353 tap_buffer_size
= versaloon_interface
.usb_setting
.buf_size
/ 2 - 32;
354 vsllink_free_buffer();
355 tdi_buffer
= malloc(tap_buffer_size
);
356 tdo_buffer
= malloc(tap_buffer_size
);
357 tms_buffer
= malloc(tap_buffer_size
);
358 if ((NULL
== tdi_buffer
) || (NULL
== tdo_buffer
) || (NULL
== tms_buffer
)) {
363 versaloon_interface
.adaptors
.jtag_raw
.init(0);
364 versaloon_interface
.adaptors
.jtag_raw
.config(0, jtag_get_speed_khz());
365 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
| GPIO_TRST
,
366 GPIO_TRST
, GPIO_SRST
, GPIO_SRST
);
369 if (ERROR_OK
!= versaloon_interface
.adaptors
.peripheral_commit())
377 /**************************************************************************
378 * Queue command implementations */
380 static void vsllink_end_state(tap_state_t state
)
382 if (tap_is_state_stable(state
))
383 tap_set_end_state(state
);
385 LOG_ERROR("BUG: %i is not a valid end state", state
);
390 /* Goes to the end state. */
391 static void vsllink_state_move(void)
394 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(),
395 tap_get_end_state());
396 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(),
397 tap_get_end_state());
399 for (i
= 0; i
< tms_scan_bits
; i
++)
400 vsllink_tap_append_step((tms_scan
>> i
) & 1, 0);
402 tap_set_state(tap_get_end_state());
405 static void vsllink_path_move(int num_states
, tap_state_t
*path
)
407 for (int i
= 0; i
< num_states
; i
++) {
408 if (path
[i
] == tap_state_transition(tap_get_state(), false))
409 vsllink_tap_append_step(0, 0);
410 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
411 vsllink_tap_append_step(1, 0);
413 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
414 tap_state_name(tap_get_state()),
415 tap_state_name(path
[i
]));
419 tap_set_state(path
[i
]);
422 tap_set_end_state(tap_get_state());
425 static void vsllink_tms(int num_bits
, const uint8_t *bits
)
427 for (int i
= 0; i
< num_bits
; i
++)
428 vsllink_tap_append_step((bits
[i
/ 8] >> (i
% 8)) & 1, 0);
431 static void vsllink_stableclocks(int num_cycles
, int tms
)
433 while (num_cycles
> 0) {
434 vsllink_tap_append_step(tms
, 0);
439 static void vsllink_runtest(int num_cycles
)
441 tap_state_t saved_end_state
= tap_get_end_state();
443 if (tap_get_state() != TAP_IDLE
) {
444 /* enter IDLE state */
445 vsllink_end_state(TAP_IDLE
);
446 vsllink_state_move();
449 vsllink_stableclocks(num_cycles
, 0);
453 vsllink_end_state(saved_end_state
);
454 if (tap_get_end_state() != tap_get_end_state())
455 vsllink_state_move();
458 static void vsllink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
459 int scan_size
, struct scan_command
*command
)
461 tap_state_t saved_end_state
;
463 saved_end_state
= tap_get_end_state();
465 /* Move to appropriate scan state */
466 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
468 if (tap_get_state() != tap_get_end_state())
469 vsllink_state_move();
470 vsllink_end_state(saved_end_state
);
473 vsllink_tap_append_scan(scan_size
, buffer
, command
);
475 /* Goto Pause and record position to insert tms:0 */
476 vsllink_tap_append_step(0, 0);
477 vsllink_tms_offset
= tap_length
;
479 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
481 if (tap_get_state() != tap_get_end_state())
482 vsllink_state_move();
485 static void vsllink_reset(int trst
, int srst
)
487 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
490 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
, 0, GPIO_SRST
, GPIO_SRST
);
492 versaloon_interface
.adaptors
.gpio
.config(0, GPIO_SRST
, GPIO_SRST
, 0, 0);
496 versaloon_interface
.adaptors
.gpio
.out(0, GPIO_TRST
, GPIO_TRST
);
498 versaloon_interface
.adaptors
.gpio
.out(0, GPIO_TRST
, 0);
501 versaloon_interface
.adaptors
.peripheral_commit();
504 COMMAND_HANDLER(vsllink_handle_usb_vid_command
)
507 return ERROR_COMMAND_SYNTAX_ERROR
;
509 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0],
510 versaloon_interface
.usb_setting
.vid
);
514 COMMAND_HANDLER(vsllink_handle_usb_pid_command
)
517 return ERROR_COMMAND_SYNTAX_ERROR
;
518 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0],
519 versaloon_interface
.usb_setting
.pid
);
523 COMMAND_HANDLER(vsllink_handle_usb_serial_command
)
526 return ERROR_COMMAND_SYNTAX_ERROR
;
528 free(versaloon_interface
.usb_setting
.serialstring
);
531 versaloon_interface
.usb_setting
.serialstring
= strdup(CMD_ARGV
[0]);
533 versaloon_interface
.usb_setting
.serialstring
= NULL
;
538 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command
)
541 return ERROR_COMMAND_SYNTAX_ERROR
;
543 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0],
544 versaloon_interface
.usb_setting
.ep_in
);
546 versaloon_interface
.usb_setting
.ep_in
|= 0x80;
551 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command
)
554 return ERROR_COMMAND_SYNTAX_ERROR
;
556 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0],
557 versaloon_interface
.usb_setting
.ep_out
);
559 versaloon_interface
.usb_setting
.ep_out
&= ~0x80;
564 COMMAND_HANDLER(vsllink_handle_usb_interface_command
)
567 return ERROR_COMMAND_SYNTAX_ERROR
;
569 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0],
570 versaloon_interface
.usb_setting
.interface
);
574 /**************************************************************************
575 * VSLLink tap functions */
577 static void vsllink_tap_init(void)
580 pending_scan_results_length
= 0;
581 vsllink_tms_offset
= 0;
584 static void vsllink_tap_ensure_pending(int scans
)
586 int available_scans
=
587 MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
589 if (scans
> available_scans
)
590 vsllink_tap_execute();
593 static void vsllink_tap_append_step(int tms
, int tdi
)
595 int index_var
= tap_length
/ 8;
597 int bit_index
= tap_length
% 8;
598 uint8_t bit
= 1 << bit_index
;
601 tms_buffer
[index_var
] |= bit
;
603 tms_buffer
[index_var
] &= ~bit
;
606 tdi_buffer
[index_var
] |= bit
;
608 tdi_buffer
[index_var
] &= ~bit
;
612 if (tap_buffer_size
* 8 <= tap_length
)
613 vsllink_tap_execute();
616 static void vsllink_tap_append_scan(int length
, uint8_t *buffer
,
617 struct scan_command
*command
)
619 struct pending_scan_result
*pending_scan_result
;
620 int len_tmp
, len_all
, i
;
623 while (len_all
< length
) {
624 vsllink_tap_ensure_pending(1);
625 pending_scan_result
=
626 &pending_scan_results_buffer
[
627 pending_scan_results_length
];
629 if ((length
- len_all
) > (tap_buffer_size
* 8 - tap_length
)) {
630 /* Use all memory available
631 vsllink_tap_append_step will commit automatically */
632 len_tmp
= tap_buffer_size
* 8 - tap_length
;
633 pending_scan_result
->last
= false;
635 len_tmp
= length
- len_all
;
636 pending_scan_result
->last
= true;
638 pending_scan_result
->src_offset
= tap_length
;
639 pending_scan_result
->dest_offset
= len_all
;
640 pending_scan_result
->length
= len_tmp
;
641 pending_scan_result
->command
= command
;
642 pending_scan_result
->buffer
= buffer
;
643 pending_scan_results_length
++;
645 for (i
= 0; i
< len_tmp
; i
++) {
646 vsllink_tap_append_step(((len_all
+ i
) < length
-1
648 (buffer
[(len_all
+ i
)/8]
649 >> ((len_all
+ i
)%8)) & 1);
656 static int vsllink_jtag_execute(void)
664 versaloon_interface
.adaptors
.jtag_raw
.execute(0, tdi_buffer
, tms_buffer
,
665 tdo_buffer
, tap_length
);
667 result
= versaloon_interface
.adaptors
.peripheral_commit();
669 if (result
== ERROR_OK
) {
670 for (i
= 0; i
< pending_scan_results_length
; i
++) {
671 struct pending_scan_result
*pending_scan_result
=
672 &pending_scan_results_buffer
[i
];
673 uint8_t *buffer
= pending_scan_result
->buffer
;
674 int length
= pending_scan_result
->length
;
675 int src_first
= pending_scan_result
->src_offset
;
676 int dest_first
= pending_scan_result
->dest_offset
;
677 bool last
= pending_scan_result
->last
;
679 struct scan_command
*command
;
681 command
= pending_scan_result
->command
;
682 buf_set_buf(tdo_buffer
, src_first
, buffer
, dest_first
, length
);
684 #ifdef _DEBUG_JTAG_IO_
686 "JTAG scan read(%d bits, from src %d bits to dest %d bits):",
687 length
, src_first
, dest_first
);
688 vsllink_debug_buffer(buffer
+ dest_first
/ 8,
689 DIV_ROUND_UP(length
, 7));
693 if (jtag_read_buffer(buffer
, command
)
696 return ERROR_JTAG_QUEUE_FAILED
;
699 if (pending_scan_result
->buffer
!= NULL
)
700 free(pending_scan_result
->buffer
);
704 LOG_ERROR("vsllink_jtag_execute failure");
705 return ERROR_JTAG_QUEUE_FAILED
;
713 static int vsllink_tap_execute(void)
718 return vsllink_jtag_execute();
721 static int vsllink_swd_init(void)
723 LOG_INFO("VSLLink SWD mode enabled");
729 static int_least32_t vsllink_swd_frequency(int_least32_t hz
)
731 const int_least32_t delay2hz
[] = {
732 1850000, 235000, 130000, 102000, 85000, 72000
736 uint16_t delay
= UINT16_MAX
;
738 for (uint16_t i
= 0; i
< ARRAY_SIZE(delay2hz
); i
++) {
739 if (hz
>= delay2hz
[i
]) {
746 if (delay
== UINT16_MAX
)
747 delay
= (500000 / hz
) - 1;
749 /* Calculate retry count after a WAIT response. This will give
750 * a retry timeout at about ~250 ms. 54 is the number of bits
751 * found in a transaction. */
752 uint16_t retry_count
= 250 * hz
/ 1000 / 54;
754 LOG_DEBUG("SWD delay: %d, retry count: %d", delay
, retry_count
);
756 versaloon_interface
.adaptors
.swd
.config(0, 2, retry_count
, delay
);
762 static int vsllink_swd_switch_seq(enum swd_special_seq seq
)
766 LOG_DEBUG("SWD line reset");
767 versaloon_interface
.adaptors
.swd
.seqout(0, swd_seq_line_reset
,
768 swd_seq_line_reset_len
);
771 LOG_DEBUG("JTAG-to-SWD");
772 versaloon_interface
.adaptors
.swd
.seqout(0, swd_seq_jtag_to_swd
,
773 swd_seq_jtag_to_swd_len
);
776 LOG_DEBUG("SWD-to-JTAG");
777 versaloon_interface
.adaptors
.swd
.seqout(0, swd_seq_swd_to_jtag
,
778 swd_seq_swd_to_jtag_len
);
781 LOG_ERROR("Sequence %d not supported", seq
);
788 static void vsllink_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
790 versaloon_interface
.adaptors
.swd
.transact(0, cmd
, value
, NULL
);
793 static void vsllink_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
795 versaloon_interface
.adaptors
.swd
.transact(0, cmd
, &value
, NULL
);
798 static int vsllink_swd_run_queue(void)
800 return versaloon_interface
.adaptors
.peripheral_commit();
803 /****************************************************************************
804 * VSLLink USB low-level functions */
806 static int vsllink_check_usb_strings(
807 struct libusb_device_handle
*usb_device_handle
,
808 struct libusb_device_descriptor
*usb_desc
)
810 char desc_string
[256];
813 if (NULL
!= versaloon_interface
.usb_setting
.serialstring
) {
814 retval
= libusb_get_string_descriptor_ascii(usb_device_handle
,
815 usb_desc
->iSerialNumber
, (unsigned char *)desc_string
,
816 sizeof(desc_string
));
820 if (strncmp(desc_string
, versaloon_interface
.usb_setting
.serialstring
,
821 sizeof(desc_string
)))
825 retval
= libusb_get_string_descriptor_ascii(usb_device_handle
,
826 usb_desc
->iProduct
, (unsigned char *)desc_string
,
827 sizeof(desc_string
));
831 if (strstr(desc_string
, "Versaloon") == NULL
)
837 static int vsllink_usb_open(struct vsllink
*vsllink
)
839 ssize_t num_devices
, i
;
840 libusb_device
**usb_devices
;
841 struct libusb_device_descriptor usb_desc
;
842 struct libusb_device_handle
*usb_device_handle
;
845 num_devices
= libusb_get_device_list(vsllink
->libusb_ctx
, &usb_devices
);
847 if (num_devices
<= 0)
850 for (i
= 0; i
< num_devices
; i
++) {
851 libusb_device
*device
= usb_devices
[i
];
853 retval
= libusb_get_device_descriptor(device
, &usb_desc
);
857 if (usb_desc
.idVendor
!= versaloon_interface
.usb_setting
.vid
||
858 usb_desc
.idProduct
!= versaloon_interface
.usb_setting
.pid
)
861 retval
= libusb_open(device
, &usb_device_handle
);
865 retval
= vsllink_check_usb_strings(usb_device_handle
, &usb_desc
);
866 if (ERROR_OK
== retval
)
869 libusb_close(usb_device_handle
);
872 libusb_free_device_list(usb_devices
, 1);
874 if (i
== num_devices
)
877 retval
= libusb_claim_interface(usb_device_handle
,
878 versaloon_interface
.usb_setting
.interface
);
880 LOG_ERROR("unable to claim interface");
881 libusb_close(usb_device_handle
);
885 vsllink
->usb_device_handle
= usb_device_handle
;
889 static void vsllink_usb_close(struct vsllink
*vsllink
)
891 libusb_release_interface(vsllink
->usb_device_handle
,
892 versaloon_interface
.usb_setting
.interface
);
893 libusb_close(vsllink
->usb_device_handle
);
896 #define BYTES_PER_LINE 16
898 #if defined _DEBUG_JTAG_IO_
899 static void vsllink_debug_buffer(uint8_t *buffer
, int length
)
906 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
907 snprintf(line
, 5, "%04x", i
);
908 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
909 snprintf(s
, 4, " %02x", buffer
[j
]);
912 LOG_DEBUG("%s", line
);
915 #endif /* _DEBUG_JTAG_IO_ */
917 static const struct command_registration vsllink_command_handlers
[] = {
919 .name
= "vsllink_usb_vid",
920 .handler
= &vsllink_handle_usb_vid_command
,
921 .mode
= COMMAND_CONFIG
,
924 .name
= "vsllink_usb_pid",
925 .handler
= &vsllink_handle_usb_pid_command
,
926 .mode
= COMMAND_CONFIG
,
929 .name
= "vsllink_usb_serial",
930 .handler
= &vsllink_handle_usb_serial_command
,
931 .mode
= COMMAND_CONFIG
,
934 .name
= "vsllink_usb_bulkin",
935 .handler
= &vsllink_handle_usb_bulkin_command
,
936 .mode
= COMMAND_CONFIG
,
939 .name
= "vsllink_usb_bulkout",
940 .handler
= &vsllink_handle_usb_bulkout_command
,
941 .mode
= COMMAND_CONFIG
,
944 .name
= "vsllink_usb_interface",
945 .handler
= &vsllink_handle_usb_interface_command
,
946 .mode
= COMMAND_CONFIG
,
948 COMMAND_REGISTRATION_DONE
951 static const char * const vsllink_transports
[] = {"jtag", "swd", NULL
};
953 static const struct swd_driver vsllink_swd_driver
= {
954 .init
= vsllink_swd_init
,
955 .frequency
= vsllink_swd_frequency
,
956 .switch_seq
= vsllink_swd_switch_seq
,
957 .read_reg
= vsllink_swd_read_reg
,
958 .write_reg
= vsllink_swd_write_reg
,
959 .run
= vsllink_swd_run_queue
,
962 struct jtag_interface vsllink_interface
= {
964 .supported
= DEBUG_CAP_TMS_SEQ
,
965 .commands
= vsllink_command_handlers
,
966 .transports
= vsllink_transports
,
967 .swd
= &vsllink_swd_driver
,
969 .init
= vsllink_init
,
970 .quit
= vsllink_quit
,
972 .speed
= vsllink_speed
,
973 .speed_div
= vsllink_speed_div
,
974 .execute_queue
= vsllink_execute_queue
,