libjaylink: Update to latest master branch
[openocd.git] / src / jtag / drivers / vsllink.c
1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
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/.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <jtag/interface.h>
30 #include <jtag/commands.h>
31 #include <jtag/swd.h>
32 #include <libusb.h>
33
34 #include "versaloon/versaloon_include.h"
35 #include "versaloon/versaloon.h"
36
37 static int vsllink_tms_offset;
38
39 struct pending_scan_result {
40 int src_offset;
41 int dest_offset;
42 int length; /* Number of bits to read */
43 struct scan_command *command; /* Corresponding scan command */
44 uint8_t *ack;
45 uint8_t *buffer;
46 bool last; /* indicate the last scan pending */
47 };
48
49 #define MAX_PENDING_SCAN_RESULTS 256
50
51 static int pending_scan_results_length;
52 static struct pending_scan_result
53 pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
54
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);
65
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);
73
74 /* VSLLink SWD functions */
75 static int_least32_t vsllink_swd_frequency(int_least32_t hz);
76 static int vsllink_swd_switch_seq(enum swd_special_seq seq);
77
78 /* VSLLink lowlevel functions */
79 struct vsllink {
80 struct libusb_context *libusb_ctx;
81 struct libusb_device_handle *usb_device_handle;
82 };
83
84 static int vsllink_usb_open(struct vsllink *vsllink);
85 static void vsllink_usb_close(struct vsllink *vsllink);
86
87 #if defined _DEBUG_JTAG_IO_
88 static void vsllink_debug_buffer(uint8_t *buffer, int length);
89 #endif
90
91 static int tap_length;
92 static int tap_buffer_size;
93 static uint8_t *tms_buffer;
94 static uint8_t *tdi_buffer;
95 static uint8_t *tdo_buffer;
96
97 static bool swd_mode;
98
99 static struct vsllink *vsllink_handle;
100
101 static int vsllink_execute_queue(void)
102 {
103 struct jtag_command *cmd = jtag_command_queue;
104 int scan_size;
105 enum scan_type type;
106 uint8_t *buffer;
107
108 DEBUG_JTAG_IO("-------------------------------------"
109 " vsllink "
110 "-------------------------------------");
111
112 while (cmd != NULL) {
113 switch (cmd->type) {
114 case JTAG_RUNTEST:
115 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
116 cmd->cmd.runtest->num_cycles,
117 tap_state_name(cmd->cmd.runtest->end_state));
118
119 vsllink_end_state(cmd->cmd.runtest->end_state);
120 vsllink_runtest(cmd->cmd.runtest->num_cycles);
121 break;
122
123 case JTAG_TLR_RESET:
124 DEBUG_JTAG_IO("statemove end in %s",
125 tap_state_name(cmd->cmd.statemove->end_state));
126
127 vsllink_end_state(cmd->cmd.statemove->end_state);
128 vsllink_state_move();
129 break;
130
131 case JTAG_PATHMOVE:
132 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
133 cmd->cmd.pathmove->num_states,
134 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
135
136 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
137 break;
138
139 case JTAG_SCAN:
140 DEBUG_JTAG_IO("JTAG Scan...");
141
142 vsllink_end_state(cmd->cmd.scan->end_state);
143
144 scan_size = jtag_build_buffer(
145 cmd->cmd.scan, &buffer);
146
147 if (cmd->cmd.scan->ir_scan)
148 DEBUG_JTAG_IO(
149 "JTAG Scan write IR(%d bits), "
150 "end in %s:",
151 scan_size,
152 tap_state_name(cmd->cmd.scan->end_state));
153
154 else
155 DEBUG_JTAG_IO(
156 "JTAG Scan write DR(%d bits), "
157 "end in %s:",
158 scan_size,
159 tap_state_name(cmd->cmd.scan->end_state));
160
161 #ifdef _DEBUG_JTAG_IO_
162 vsllink_debug_buffer(buffer,
163 DIV_ROUND_UP(scan_size, 8));
164 #endif
165
166 type = jtag_scan_type(cmd->cmd.scan);
167
168 vsllink_scan(cmd->cmd.scan->ir_scan,
169 type, buffer, scan_size,
170 cmd->cmd.scan);
171 break;
172
173 case JTAG_RESET:
174 DEBUG_JTAG_IO("reset trst: %i srst %i",
175 cmd->cmd.reset->trst,
176 cmd->cmd.reset->srst);
177
178 vsllink_tap_execute();
179
180 if (cmd->cmd.reset->trst == 1)
181 tap_set_state(TAP_RESET);
182
183 vsllink_reset(cmd->cmd.reset->trst,
184 cmd->cmd.reset->srst);
185 break;
186
187 case JTAG_SLEEP:
188 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
189 vsllink_tap_execute();
190 jtag_sleep(cmd->cmd.sleep->us);
191 break;
192
193 case JTAG_STABLECLOCKS:
194 DEBUG_JTAG_IO("add %d clocks",
195 cmd->cmd.stableclocks->num_cycles);
196
197 switch (tap_get_state()) {
198 case TAP_RESET:
199 /* tms must be '1' to stay
200 * n TAP_RESET mode
201 */
202 scan_size = 1;
203 break;
204 case TAP_DRSHIFT:
205 case TAP_IDLE:
206 case TAP_DRPAUSE:
207 case TAP_IRSHIFT:
208 case TAP_IRPAUSE:
209 /* else, tms should be '0' */
210 scan_size = 0;
211 break;
212 /* above stable states are OK */
213 default:
214 LOG_ERROR("jtag_add_clocks() "
215 "in non-stable state \"%s\"",
216 tap_state_name(tap_get_state())
217 );
218 exit(-1);
219 }
220 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
221 break;
222
223 case JTAG_TMS:
224 DEBUG_JTAG_IO("add %d jtag tms",
225 cmd->cmd.tms->num_bits);
226
227 vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
228 break;
229
230 default:
231 LOG_ERROR("BUG: unknown JTAG command type "
232 "encountered: %d", cmd->type);
233 exit(-1);
234 }
235 cmd = cmd->next;
236 }
237
238 return vsllink_tap_execute();
239 }
240
241 static int vsllink_speed(int speed)
242 {
243 if (swd_mode) {
244 vsllink_swd_frequency(speed * 1000);
245 return ERROR_OK;
246 }
247
248 versaloon_interface.adaptors.jtag_raw.config(0, (uint16_t)speed);
249 return versaloon_interface.adaptors.peripheral_commit();
250 }
251
252 static int vsllink_khz(int khz, int *jtag_speed)
253 {
254 *jtag_speed = khz;
255
256 return ERROR_OK;
257 }
258
259 static int vsllink_speed_div(int jtag_speed, int *khz)
260 {
261 *khz = jtag_speed;
262
263 return ERROR_OK;
264 }
265
266 static void vsllink_free_buffer(void)
267 {
268 if (tdi_buffer != NULL) {
269 free(tdi_buffer);
270 tdi_buffer = NULL;
271 }
272 if (tdo_buffer != NULL) {
273 free(tdo_buffer);
274 tdo_buffer = NULL;
275 }
276 if (tms_buffer != NULL) {
277 free(tms_buffer);
278 tms_buffer = NULL;
279 }
280 }
281
282 static int vsllink_quit(void)
283 {
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);
287
288 if (swd_mode)
289 versaloon_interface.adaptors.swd.fini(0);
290 else
291 versaloon_interface.adaptors.jtag_raw.fini(0);
292
293 versaloon_interface.adaptors.peripheral_commit();
294 versaloon_interface.fini();
295
296 vsllink_free_buffer();
297 vsllink_usb_close(vsllink_handle);
298
299 free(vsllink_handle);
300
301 return ERROR_OK;
302 }
303
304 static int vsllink_interface_init(void)
305 {
306 vsllink_handle = malloc(sizeof(struct vsllink));
307 if (NULL == vsllink_handle) {
308 LOG_ERROR("unable to allocate memory");
309 return ERROR_FAIL;
310 }
311
312 libusb_init(&vsllink_handle->libusb_ctx);
313
314 if (ERROR_OK != vsllink_usb_open(vsllink_handle)) {
315 LOG_ERROR("Can't find USB JTAG Interface!" \
316 "Please check connection and permissions.");
317 return ERROR_JTAG_INIT_FAILED;
318 }
319 LOG_DEBUG("vsllink found on %04X:%04X",
320 versaloon_interface.usb_setting.vid,
321 versaloon_interface.usb_setting.pid);
322 versaloon_usb_device_handle = vsllink_handle->usb_device_handle;
323
324 if (ERROR_OK != versaloon_interface.init())
325 return ERROR_FAIL;
326 if (versaloon_interface.usb_setting.buf_size < 32) {
327 versaloon_interface.fini();
328 return ERROR_FAIL;
329 }
330
331 return ERROR_OK;
332 }
333
334 static int vsllink_init(void)
335 {
336 int retval = vsllink_interface_init();
337 if (ERROR_OK != retval)
338 return retval;
339
340 versaloon_interface.adaptors.gpio.init(0);
341 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST,
342 GPIO_SRST);
343 versaloon_interface.adaptors.delay.delayms(100);
344 versaloon_interface.adaptors.peripheral_commit();
345
346 if (swd_mode) {
347 versaloon_interface.adaptors.gpio.config(0, GPIO_TRST, 0,
348 GPIO_TRST, GPIO_TRST);
349 versaloon_interface.adaptors.swd.init(0);
350 vsllink_swd_frequency(jtag_get_speed_khz() * 1000);
351 vsllink_swd_switch_seq(JTAG_TO_SWD);
352
353 } else {
354 /* malloc buffer size for tap */
355 tap_buffer_size = versaloon_interface.usb_setting.buf_size / 2 - 32;
356 vsllink_free_buffer();
357 tdi_buffer = malloc(tap_buffer_size);
358 tdo_buffer = malloc(tap_buffer_size);
359 tms_buffer = malloc(tap_buffer_size);
360 if ((NULL == tdi_buffer) || (NULL == tdo_buffer) || (NULL == tms_buffer)) {
361 vsllink_quit();
362 return ERROR_FAIL;
363 }
364
365 versaloon_interface.adaptors.jtag_raw.init(0);
366 versaloon_interface.adaptors.jtag_raw.config(0, jtag_get_speed_khz());
367 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
368 GPIO_TRST, GPIO_SRST, GPIO_SRST);
369 }
370
371 if (ERROR_OK != versaloon_interface.adaptors.peripheral_commit())
372 return ERROR_FAIL;
373
374 vsllink_reset(0, 0);
375 vsllink_tap_init();
376 return ERROR_OK;
377 }
378
379 /**************************************************************************
380 * Queue command implementations */
381
382 static void vsllink_end_state(tap_state_t state)
383 {
384 if (tap_is_state_stable(state))
385 tap_set_end_state(state);
386 else {
387 LOG_ERROR("BUG: %i is not a valid end state", state);
388 exit(-1);
389 }
390 }
391
392 /* Goes to the end state. */
393 static void vsllink_state_move(void)
394 {
395 int i;
396 uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
397 tap_get_end_state());
398 uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(),
399 tap_get_end_state());
400
401 for (i = 0; i < tms_scan_bits; i++)
402 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
403
404 tap_set_state(tap_get_end_state());
405 }
406
407 static void vsllink_path_move(int num_states, tap_state_t *path)
408 {
409 for (int i = 0; i < num_states; i++) {
410 if (path[i] == tap_state_transition(tap_get_state(), false))
411 vsllink_tap_append_step(0, 0);
412 else if (path[i] == tap_state_transition(tap_get_state(), true))
413 vsllink_tap_append_step(1, 0);
414 else {
415 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
416 tap_state_name(tap_get_state()),
417 tap_state_name(path[i]));
418 exit(-1);
419 }
420
421 tap_set_state(path[i]);
422 }
423
424 tap_set_end_state(tap_get_state());
425 }
426
427 static void vsllink_tms(int num_bits, const uint8_t *bits)
428 {
429 for (int i = 0; i < num_bits; i++)
430 vsllink_tap_append_step((bits[i / 8] >> (i % 8)) & 1, 0);
431 }
432
433 static void vsllink_stableclocks(int num_cycles, int tms)
434 {
435 while (num_cycles > 0) {
436 vsllink_tap_append_step(tms, 0);
437 num_cycles--;
438 }
439 }
440
441 static void vsllink_runtest(int num_cycles)
442 {
443 tap_state_t saved_end_state = tap_get_end_state();
444
445 if (tap_get_state() != TAP_IDLE) {
446 /* enter IDLE state */
447 vsllink_end_state(TAP_IDLE);
448 vsllink_state_move();
449 }
450
451 vsllink_stableclocks(num_cycles, 0);
452
453 /* post-process */
454 /* set end_state */
455 vsllink_end_state(saved_end_state);
456 if (tap_get_end_state() != tap_get_end_state())
457 vsllink_state_move();
458 }
459
460 static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
461 int scan_size, struct scan_command *command)
462 {
463 tap_state_t saved_end_state;
464
465 saved_end_state = tap_get_end_state();
466
467 /* Move to appropriate scan state */
468 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
469
470 if (tap_get_state() != tap_get_end_state())
471 vsllink_state_move();
472 vsllink_end_state(saved_end_state);
473
474 /* Scan */
475 vsllink_tap_append_scan(scan_size, buffer, command);
476
477 /* Goto Pause and record position to insert tms:0 */
478 vsllink_tap_append_step(0, 0);
479 vsllink_tms_offset = tap_length;
480
481 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
482
483 if (tap_get_state() != tap_get_end_state())
484 vsllink_state_move();
485 }
486
487 static void vsllink_reset(int trst, int srst)
488 {
489 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
490
491 if (!srst)
492 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST, GPIO_SRST);
493 else
494 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, GPIO_SRST, 0, 0);
495
496 if (!swd_mode) {
497 if (!trst)
498 versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, GPIO_TRST);
499 else
500 versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, 0);
501 }
502
503 versaloon_interface.adaptors.peripheral_commit();
504 }
505
506 COMMAND_HANDLER(vsllink_handle_usb_vid_command)
507 {
508 if (CMD_ARGC != 1)
509 return ERROR_COMMAND_SYNTAX_ERROR;
510
511 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
512 versaloon_interface.usb_setting.vid);
513 return ERROR_OK;
514 }
515
516 COMMAND_HANDLER(vsllink_handle_usb_pid_command)
517 {
518 if (CMD_ARGC != 1)
519 return ERROR_COMMAND_SYNTAX_ERROR;
520 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
521 versaloon_interface.usb_setting.pid);
522 return ERROR_OK;
523 }
524
525 COMMAND_HANDLER(vsllink_handle_usb_serial_command)
526 {
527 if (CMD_ARGC > 1)
528 return ERROR_COMMAND_SYNTAX_ERROR;
529
530 free(versaloon_interface.usb_setting.serialstring);
531
532 if (CMD_ARGC == 1)
533 versaloon_interface.usb_setting.serialstring = strdup(CMD_ARGV[0]);
534 else
535 versaloon_interface.usb_setting.serialstring = NULL;
536
537 return ERROR_OK;
538 }
539
540 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
541 {
542 if (CMD_ARGC != 1)
543 return ERROR_COMMAND_SYNTAX_ERROR;
544
545 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
546 versaloon_interface.usb_setting.ep_in);
547
548 versaloon_interface.usb_setting.ep_in |= 0x80;
549
550 return ERROR_OK;
551 }
552
553 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
554 {
555 if (CMD_ARGC != 1)
556 return ERROR_COMMAND_SYNTAX_ERROR;
557
558 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
559 versaloon_interface.usb_setting.ep_out);
560
561 versaloon_interface.usb_setting.ep_out &= ~0x80;
562
563 return ERROR_OK;
564 }
565
566 COMMAND_HANDLER(vsllink_handle_usb_interface_command)
567 {
568 if (CMD_ARGC != 1)
569 return ERROR_COMMAND_SYNTAX_ERROR;
570
571 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
572 versaloon_interface.usb_setting.interface);
573 return ERROR_OK;
574 }
575
576 /**************************************************************************
577 * VSLLink tap functions */
578
579 static void vsllink_tap_init(void)
580 {
581 tap_length = 0;
582 pending_scan_results_length = 0;
583 vsllink_tms_offset = 0;
584 }
585
586 static void vsllink_tap_ensure_pending(int scans)
587 {
588 int available_scans =
589 MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
590
591 if (scans > available_scans)
592 vsllink_tap_execute();
593 }
594
595 static void vsllink_tap_append_step(int tms, int tdi)
596 {
597 int index_var = tap_length / 8;
598
599 int bit_index = tap_length % 8;
600 uint8_t bit = 1 << bit_index;
601
602 if (tms)
603 tms_buffer[index_var] |= bit;
604 else
605 tms_buffer[index_var] &= ~bit;
606
607 if (tdi)
608 tdi_buffer[index_var] |= bit;
609 else
610 tdi_buffer[index_var] &= ~bit;
611
612 tap_length++;
613
614 if (tap_buffer_size * 8 <= tap_length)
615 vsllink_tap_execute();
616 }
617
618 static void vsllink_tap_append_scan(int length, uint8_t *buffer,
619 struct scan_command *command)
620 {
621 struct pending_scan_result *pending_scan_result;
622 int len_tmp, len_all, i;
623
624 len_all = 0;
625 while (len_all < length) {
626 vsllink_tap_ensure_pending(1);
627 pending_scan_result =
628 &pending_scan_results_buffer[
629 pending_scan_results_length];
630
631 if ((length - len_all) > (tap_buffer_size * 8 - tap_length)) {
632 /* Use all memory available
633 vsllink_tap_append_step will commit automatically */
634 len_tmp = tap_buffer_size * 8 - tap_length;
635 pending_scan_result->last = false;
636 } else {
637 len_tmp = length - len_all;
638 pending_scan_result->last = true;
639 }
640 pending_scan_result->src_offset = tap_length;
641 pending_scan_result->dest_offset = len_all;
642 pending_scan_result->length = len_tmp;
643 pending_scan_result->command = command;
644 pending_scan_result->buffer = buffer;
645 pending_scan_results_length++;
646
647 for (i = 0; i < len_tmp; i++) {
648 vsllink_tap_append_step(((len_all + i) < length-1
649 ? 0 : 1),
650 (buffer[(len_all + i)/8]
651 >> ((len_all + i)%8)) & 1);
652 }
653
654 len_all += len_tmp;
655 }
656 }
657
658 static int vsllink_jtag_execute(void)
659 {
660 int i;
661 int result;
662
663 if (tap_length <= 0)
664 return ERROR_OK;
665
666 versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer,
667 tdo_buffer, tap_length);
668
669 result = versaloon_interface.adaptors.peripheral_commit();
670
671 if (result == ERROR_OK) {
672 for (i = 0; i < pending_scan_results_length; i++) {
673 struct pending_scan_result *pending_scan_result =
674 &pending_scan_results_buffer[i];
675 uint8_t *buffer = pending_scan_result->buffer;
676 int length = pending_scan_result->length;
677 int src_first = pending_scan_result->src_offset;
678 int dest_first = pending_scan_result->dest_offset;
679 bool last = pending_scan_result->last;
680
681 struct scan_command *command;
682
683 command = pending_scan_result->command;
684 buf_set_buf(tdo_buffer, src_first, buffer, dest_first, length);
685
686 #ifdef _DEBUG_JTAG_IO_
687 DEBUG_JTAG_IO(
688 "JTAG scan read(%d bits, from src %d bits to dest %d bits):",
689 length, src_first, dest_first);
690 vsllink_debug_buffer(buffer + dest_first / 8,
691 DIV_ROUND_UP(length, 7));
692 #endif
693
694 if (last) {
695 if (jtag_read_buffer(buffer, command)
696 != ERROR_OK) {
697 vsllink_tap_init();
698 return ERROR_JTAG_QUEUE_FAILED;
699 }
700
701 if (pending_scan_result->buffer != NULL)
702 free(pending_scan_result->buffer);
703 }
704 }
705 } else {
706 LOG_ERROR("vsllink_jtag_execute failure");
707 return ERROR_JTAG_QUEUE_FAILED;
708 }
709
710 vsllink_tap_init();
711
712 return ERROR_OK;
713 }
714
715 static int vsllink_tap_execute(void)
716 {
717 if (swd_mode)
718 return ERROR_OK;
719
720 return vsllink_jtag_execute();
721 }
722
723 static int vsllink_swd_init(void)
724 {
725 LOG_INFO("VSLLink SWD mode enabled");
726 swd_mode = true;
727
728 return ERROR_OK;
729 }
730
731 static int_least32_t vsllink_swd_frequency(int_least32_t hz)
732 {
733 const int_least32_t delay2hz[] = {
734 1850000, 235000, 130000, 102000, 85000, 72000
735 };
736
737 if (hz > 0) {
738 uint16_t delay = UINT16_MAX;
739
740 for (uint16_t i = 0; i < ARRAY_SIZE(delay2hz); i++) {
741 if (hz >= delay2hz[i]) {
742 hz = delay2hz[i];
743 delay = i;
744 break;
745 }
746 }
747
748 if (delay == UINT16_MAX)
749 delay = (500000 / hz) - 1;
750
751 /* Calculate retry count after a WAIT response. This will give
752 * a retry timeout at about ~250 ms. 54 is the number of bits
753 * found in a transaction. */
754 uint16_t retry_count = 250 * hz / 1000 / 54;
755
756 LOG_DEBUG("SWD delay: %d, retry count: %d", delay, retry_count);
757
758 versaloon_interface.adaptors.swd.config(0, 2, retry_count, delay);
759 }
760
761 return hz;
762 }
763
764 static int vsllink_swd_switch_seq(enum swd_special_seq seq)
765 {
766 switch (seq) {
767 case LINE_RESET:
768 LOG_DEBUG("SWD line reset");
769 versaloon_interface.adaptors.swd.seqout(0, swd_seq_line_reset,
770 swd_seq_line_reset_len);
771 break;
772 case JTAG_TO_SWD:
773 LOG_DEBUG("JTAG-to-SWD");
774 versaloon_interface.adaptors.swd.seqout(0, swd_seq_jtag_to_swd,
775 swd_seq_jtag_to_swd_len);
776 break;
777 case SWD_TO_JTAG:
778 LOG_DEBUG("SWD-to-JTAG");
779 versaloon_interface.adaptors.swd.seqout(0, swd_seq_swd_to_jtag,
780 swd_seq_swd_to_jtag_len);
781 break;
782 default:
783 LOG_ERROR("Sequence %d not supported", seq);
784 return ERROR_FAIL;
785 }
786
787 return ERROR_OK;
788 }
789
790 static void vsllink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
791 {
792 versaloon_interface.adaptors.swd.transact(0, cmd, value, NULL);
793 }
794
795 static void vsllink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
796 {
797 versaloon_interface.adaptors.swd.transact(0, cmd, &value, NULL);
798 }
799
800 static int vsllink_swd_run_queue(void)
801 {
802 return versaloon_interface.adaptors.peripheral_commit();
803 }
804
805 /****************************************************************************
806 * VSLLink USB low-level functions */
807
808 static int vsllink_check_usb_strings(
809 struct libusb_device_handle *usb_device_handle,
810 struct libusb_device_descriptor *usb_desc)
811 {
812 char desc_string[256];
813 int retval;
814
815 if (NULL != versaloon_interface.usb_setting.serialstring) {
816 retval = libusb_get_string_descriptor_ascii(usb_device_handle,
817 usb_desc->iSerialNumber, (unsigned char *)desc_string,
818 sizeof(desc_string));
819 if (retval < 0)
820 return ERROR_FAIL;
821
822 if (strncmp(desc_string, versaloon_interface.usb_setting.serialstring,
823 sizeof(desc_string)))
824 return ERROR_FAIL;
825 }
826
827 retval = libusb_get_string_descriptor_ascii(usb_device_handle,
828 usb_desc->iProduct, (unsigned char *)desc_string,
829 sizeof(desc_string));
830 if (retval < 0)
831 return ERROR_FAIL;
832
833 if (strstr(desc_string, "Versaloon") == NULL)
834 return ERROR_FAIL;
835
836 return ERROR_OK;
837 }
838
839 static int vsllink_usb_open(struct vsllink *vsllink)
840 {
841 ssize_t num_devices, i;
842 libusb_device **usb_devices;
843 struct libusb_device_descriptor usb_desc;
844 struct libusb_device_handle *usb_device_handle;
845 int retval;
846
847 num_devices = libusb_get_device_list(vsllink->libusb_ctx, &usb_devices);
848
849 if (num_devices <= 0)
850 return ERROR_FAIL;
851
852 for (i = 0; i < num_devices; i++) {
853 libusb_device *device = usb_devices[i];
854
855 retval = libusb_get_device_descriptor(device, &usb_desc);
856 if (retval != 0)
857 continue;
858
859 if (usb_desc.idVendor != versaloon_interface.usb_setting.vid ||
860 usb_desc.idProduct != versaloon_interface.usb_setting.pid)
861 continue;
862
863 retval = libusb_open(device, &usb_device_handle);
864 if (retval != 0)
865 continue;
866
867 retval = vsllink_check_usb_strings(usb_device_handle, &usb_desc);
868 if (ERROR_OK == retval)
869 break;
870
871 libusb_close(usb_device_handle);
872 }
873
874 libusb_free_device_list(usb_devices, 1);
875
876 if (i == num_devices)
877 return ERROR_FAIL;
878
879 retval = libusb_claim_interface(usb_device_handle,
880 versaloon_interface.usb_setting.interface);
881 if (retval != 0) {
882 LOG_ERROR("unable to claim interface");
883 libusb_close(usb_device_handle);
884 return ERROR_FAIL;
885 }
886
887 vsllink->usb_device_handle = usb_device_handle;
888 return ERROR_OK;
889 }
890
891 static void vsllink_usb_close(struct vsllink *vsllink)
892 {
893 libusb_release_interface(vsllink->usb_device_handle,
894 versaloon_interface.usb_setting.interface);
895 libusb_close(vsllink->usb_device_handle);
896 }
897
898 #define BYTES_PER_LINE 16
899
900 #if defined _DEBUG_JTAG_IO_
901 static void vsllink_debug_buffer(uint8_t *buffer, int length)
902 {
903 char line[81];
904 char s[4];
905 int i;
906 int j;
907
908 for (i = 0; i < length; i += BYTES_PER_LINE) {
909 snprintf(line, 5, "%04x", i);
910 for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
911 snprintf(s, 4, " %02x", buffer[j]);
912 strcat(line, s);
913 }
914 LOG_DEBUG("%s", line);
915 }
916 }
917 #endif /* _DEBUG_JTAG_IO_ */
918
919 static const struct command_registration vsllink_command_handlers[] = {
920 {
921 .name = "vsllink_usb_vid",
922 .handler = &vsllink_handle_usb_vid_command,
923 .mode = COMMAND_CONFIG,
924 },
925 {
926 .name = "vsllink_usb_pid",
927 .handler = &vsllink_handle_usb_pid_command,
928 .mode = COMMAND_CONFIG,
929 },
930 {
931 .name = "vsllink_usb_serial",
932 .handler = &vsllink_handle_usb_serial_command,
933 .mode = COMMAND_CONFIG,
934 },
935 {
936 .name = "vsllink_usb_bulkin",
937 .handler = &vsllink_handle_usb_bulkin_command,
938 .mode = COMMAND_CONFIG,
939 },
940 {
941 .name = "vsllink_usb_bulkout",
942 .handler = &vsllink_handle_usb_bulkout_command,
943 .mode = COMMAND_CONFIG,
944 },
945 {
946 .name = "vsllink_usb_interface",
947 .handler = &vsllink_handle_usb_interface_command,
948 .mode = COMMAND_CONFIG,
949 },
950 COMMAND_REGISTRATION_DONE
951 };
952
953 static const char * const vsllink_transports[] = {"jtag", "swd", NULL};
954
955 static const struct swd_driver vsllink_swd_driver = {
956 .init = vsllink_swd_init,
957 .frequency = vsllink_swd_frequency,
958 .switch_seq = vsllink_swd_switch_seq,
959 .read_reg = vsllink_swd_read_reg,
960 .write_reg = vsllink_swd_write_reg,
961 .run = vsllink_swd_run_queue,
962 };
963
964 struct jtag_interface vsllink_interface = {
965 .name = "vsllink",
966 .supported = DEBUG_CAP_TMS_SEQ,
967 .commands = vsllink_command_handlers,
968 .transports = vsllink_transports,
969 .swd = &vsllink_swd_driver,
970
971 .init = vsllink_init,
972 .quit = vsllink_quit,
973 .khz = vsllink_khz,
974 .speed = vsllink_speed,
975 .speed_div = vsllink_speed_div,
976 .execute_queue = vsllink_execute_queue,
977 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)