1 /***************************************************************************
2 * Copyright (C) 2014 by Paul Fertser *
3 * fercerpav@gmail.com *
5 * Copyright (C) 2013 by mike brown *
6 * mike@theshedworks.org.uk *
8 * Copyright (C) 2013 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
31 #include <transport/transport.h>
33 #include <jtag/interface.h>
34 #include <jtag/commands.h>
40 * See CMSIS-DAP documentation:
41 * Version 0.01 - Beta.
46 /* Known vid/pid pairs:
47 * VID 0xc251: Keil Software
48 * PID 0xf001: LPC-Link-II CMSIS_DAP
49 * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
50 * PID 0x2722: Keil ULINK2 CMSIS-DAP
52 * VID 0x0d28: mbed Software
53 * PID 0x0204: MBED CMSIS-DAP
57 /* vid = pid = 0 marks the end of the list */
58 static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
59 static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
60 static wchar_t *cmsis_dap_serial;
63 #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
64 #define USB_TIMEOUT 1000
66 /* CMSIS-DAP General Commands */
67 #define CMD_DAP_INFO 0x00
68 #define CMD_DAP_LED 0x01
69 #define CMD_DAP_CONNECT 0x02
70 #define CMD_DAP_DISCONNECT 0x03
71 #define CMD_DAP_WRITE_ABORT 0x08
72 #define CMD_DAP_DELAY 0x09
73 #define CMD_DAP_RESET_TARGET 0x0A
76 #define INFO_ID_VID 0x00 /* string */
77 #define INFO_ID_PID 0x02 /* string */
78 #define INFO_ID_SERNUM 0x03 /* string */
79 #define INFO_ID_FW_VER 0x04 /* string */
80 #define INFO_ID_TD_VEND 0x05 /* string */
81 #define INFO_ID_TD_NAME 0x06 /* string */
82 #define INFO_ID_CAPS 0xf0 /* byte */
83 #define INFO_ID_PKT_CNT 0xfe /* byte */
84 #define INFO_ID_PKT_SZ 0xff /* short */
86 #define INFO_CAPS_SWD 0x01
87 #define INFO_CAPS_JTAG 0x02
90 #define LED_ID_CONNECT 0x00
91 #define LED_ID_RUN 0x01
97 #define CONNECT_DEFAULT 0x00
98 #define CONNECT_SWD 0x01
99 #define CONNECT_JTAG 0x02
101 /* CMSIS-DAP Common SWD/JTAG Commands */
102 #define CMD_DAP_DELAY 0x09
103 #define CMD_DAP_SWJ_PINS 0x10
104 #define CMD_DAP_SWJ_CLOCK 0x11
105 #define CMD_DAP_SWJ_SEQ 0x12
117 /* CMSIS-DAP SWD Commands */
118 #define CMD_DAP_SWD_CONFIGURE 0x13
120 /* CMSIS-DAP JTAG Commands */
121 #define CMD_DAP_JTAG_SEQ 0x14
122 #define CMD_DAP_JTAG_CONFIGURE 0x15
123 #define CMD_DAP_JTAG_IDCODE 0x16
125 /* CMSIS-DAP Transfer Commands */
126 #define CMD_DAP_TFER_CONFIGURE 0x04
127 #define CMD_DAP_TFER 0x05
128 #define CMD_DAP_TFER_BLOCK 0x06
129 #define CMD_DAP_TFER_ABORT 0x07
131 /* DAP Status Code */
133 #define DAP_ERROR 0xFF
135 /* CMSIS-DAP Vendor Commands
138 static const char * const info_caps_str[] = {
143 /* max clock speed (kHz) */
144 #define DAP_MAX_CLOCK 5000
147 hid_device *dev_handle;
148 uint16_t packet_size;
149 uint16_t packet_count;
150 uint8_t *packet_buffer;
155 struct pending_transfer_result {
161 static int pending_transfer_count, pending_queue_len;
162 static struct pending_transfer_result *pending_transfers;
164 static int queued_retval;
166 static struct cmsis_dap *cmsis_dap_handle;
168 static int cmsis_dap_usb_open(void)
170 hid_device *dev = NULL;
172 struct hid_device_info *devs, *cur_dev;
173 unsigned short target_vid, target_pid;
174 wchar_t *target_serial = NULL;
177 bool serial_found = false;
183 * The CMSIS-DAP specification stipulates:
184 * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
185 * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
187 devs = hid_enumerate(0x0, 0x0);
189 while (NULL != cur_dev) {
190 if (0 == cmsis_dap_vid[0]) {
191 if (NULL == cur_dev->product_string) {
192 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
193 cur_dev->vendor_id, cur_dev->product_id);
195 if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
196 /* if the user hasn't specified VID:PID *and*
197 * product string contains "CMSIS-DAP", pick it
203 /* otherwise, exhaustively compare against all VID:PID in list */
204 for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
205 if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
209 if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
214 /* we have found an adapter, so exit further checks */
215 /* check serial number matches if given */
216 if (cmsis_dap_serial != NULL) {
217 if (wcscmp(cmsis_dap_serial, cur_dev->serial_number) == 0) {
225 cur_dev = cur_dev->next;
228 if (NULL != cur_dev) {
229 target_vid = cur_dev->vendor_id;
230 target_pid = cur_dev->product_id;
232 target_serial = cmsis_dap_serial;
235 hid_free_enumeration(devs);
237 if (target_vid == 0 && target_pid == 0) {
238 LOG_ERROR("unable to find CMSIS-DAP device");
242 if (hid_init() != 0) {
243 LOG_ERROR("unable to open HIDAPI");
247 dev = hid_open(target_vid, target_pid, target_serial);
250 LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
254 struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
256 LOG_ERROR("unable to allocate memory");
260 dap->dev_handle = dev;
264 cmsis_dap_handle = dap;
266 /* allocate default packet buffer, may be changed later.
267 * currently with HIDAPI we have no way of getting the output report length
268 * without this info we cannot communicate with the adapter.
269 * For the moment we ahve to hard code the packet size */
271 int packet_size = PACKET_SIZE;
273 /* atmel cmsis-dap uses 512 byte reports */
274 /* TODO: HID report descriptor should be parsed instead of
275 * hardcoding a match by VID */
276 if (target_vid == 0x03eb)
277 packet_size = 512 + 1;
279 cmsis_dap_handle->packet_buffer = malloc(packet_size);
280 cmsis_dap_handle->packet_size = packet_size;
282 if (cmsis_dap_handle->packet_buffer == NULL) {
283 LOG_ERROR("unable to allocate memory");
290 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
292 hid_close(dap->dev_handle);
295 free(cmsis_dap_handle->packet_buffer);
296 free(cmsis_dap_handle);
297 cmsis_dap_handle = NULL;
298 free(cmsis_dap_serial);
299 cmsis_dap_serial = NULL;
300 free(pending_transfers);
301 pending_transfers = NULL;
306 /* Send a message and receive the reply */
307 static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
309 /* Pad the rest of the TX buffer with 0's */
310 memset(dap->packet_buffer + txlen, 0, dap->packet_size - txlen);
312 /* write data to device */
313 int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
315 LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
320 retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
321 if (retval == -1 || retval == 0) {
322 LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
329 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
332 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
334 buffer[0] = 0; /* report number */
335 buffer[1] = CMD_DAP_SWJ_PINS;
338 buffer[4] = delay & 0xff;
339 buffer[5] = (delay >> 8) & 0xff;
340 buffer[6] = (delay >> 16) & 0xff;
341 buffer[7] = (delay >> 24) & 0xff;
342 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
344 if (retval != ERROR_OK) {
345 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
346 return ERROR_JTAG_DEVICE_ERROR;
355 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
358 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
360 /* set clock in Hz */
362 buffer[0] = 0; /* report number */
363 buffer[1] = CMD_DAP_SWJ_CLOCK;
364 buffer[2] = swj_clock & 0xff;
365 buffer[3] = (swj_clock >> 8) & 0xff;
366 buffer[4] = (swj_clock >> 16) & 0xff;
367 buffer[5] = (swj_clock >> 24) & 0xff;
368 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
370 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
371 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
372 return ERROR_JTAG_DEVICE_ERROR;
378 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
381 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
383 buffer[0] = 0; /* report number */
384 buffer[1] = CMD_DAP_INFO;
386 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
388 if (retval != ERROR_OK) {
389 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
390 return ERROR_JTAG_DEVICE_ERROR;
393 *data = &(buffer[1]);
398 static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
401 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
403 buffer[0] = 0; /* report number */
404 buffer[1] = CMD_DAP_LED;
407 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
409 if (retval != ERROR_OK || buffer[1] != 0x00) {
410 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
411 return ERROR_JTAG_DEVICE_ERROR;
417 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
420 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
422 buffer[0] = 0; /* report number */
423 buffer[1] = CMD_DAP_CONNECT;
425 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
427 if (retval != ERROR_OK) {
428 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
429 return ERROR_JTAG_DEVICE_ERROR;
432 if (buffer[1] != mode) {
433 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
434 return ERROR_JTAG_DEVICE_ERROR;
440 static int cmsis_dap_cmd_DAP_Disconnect(void)
443 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
445 buffer[0] = 0; /* report number */
446 buffer[1] = CMD_DAP_DISCONNECT;
447 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
449 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
450 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
451 return ERROR_JTAG_DEVICE_ERROR;
457 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
460 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
462 buffer[0] = 0; /* report number */
463 buffer[1] = CMD_DAP_TFER_CONFIGURE;
465 buffer[3] = retry_count & 0xff;
466 buffer[4] = (retry_count >> 8) & 0xff;
467 buffer[5] = match_retry & 0xff;
468 buffer[6] = (match_retry >> 8) & 0xff;
469 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
471 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
472 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
473 return ERROR_JTAG_DEVICE_ERROR;
479 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
482 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
484 buffer[0] = 0; /* report number */
485 buffer[1] = CMD_DAP_SWD_CONFIGURE;
487 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
489 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
490 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
491 return ERROR_JTAG_DEVICE_ERROR;
498 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
501 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
503 buffer[0] = 0; /* report number */
504 buffer[1] = CMD_DAP_DELAY;
505 buffer[2] = delay_us & 0xff;
506 buffer[3] = (delay_us >> 8) & 0xff;
507 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
509 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
510 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
511 return ERROR_JTAG_DEVICE_ERROR;
518 static int cmsis_dap_swd_run_queue(struct adiv5_dap *dap)
520 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
522 LOG_DEBUG("Executing %d queued transactions", pending_transfer_count);
524 if (queued_retval != ERROR_OK) {
525 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
529 if (!pending_transfer_count)
533 buffer[idx++] = 0; /* report number */
534 buffer[idx++] = CMD_DAP_TFER;
535 buffer[idx++] = 0x00; /* DAP Index */
536 buffer[idx++] = pending_transfer_count;
538 for (int i = 0; i < pending_transfer_count; i++) {
539 uint8_t cmd = pending_transfers[i].cmd;
540 uint32_t data = pending_transfers[i].data;
542 LOG_DEBUG("%s %s reg %x %"PRIx32,
543 cmd & SWD_CMD_APnDP ? "AP" : "DP",
544 cmd & SWD_CMD_RnW ? "read" : "write",
545 (cmd & SWD_CMD_A32) >> 1, data);
547 /* When proper WAIT handling is implemented in the
548 * common SWD framework, this kludge can be
549 * removed. However, this might lead to minor
550 * performance degradation as the adapter wouldn't be
551 * able to automatically retry anything (because ARM
552 * has forgotten to implement sticky error flags
553 * clearing). See also comments regarding
554 * cmsis_dap_cmd_DAP_TFER_Configure() and
555 * cmsis_dap_cmd_DAP_SWD_Configure() in
558 if (!(cmd & SWD_CMD_RnW) &&
559 !(cmd & SWD_CMD_APnDP) &&
560 (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
561 (data & CORUNDETECT)) {
562 LOG_DEBUG("refusing to enable sticky overrun detection");
563 data &= ~CORUNDETECT;
566 buffer[idx++] = (cmd >> 1) & 0x0f;
567 if (!(cmd & SWD_CMD_RnW)) {
568 buffer[idx++] = (data) & 0xff;
569 buffer[idx++] = (data >> 8) & 0xff;
570 buffer[idx++] = (data >> 16) & 0xff;
571 buffer[idx++] = (data >> 24) & 0xff;
575 queued_retval = cmsis_dap_usb_xfer(cmsis_dap_handle, idx);
576 if (queued_retval != ERROR_OK)
580 uint8_t ack = buffer[idx] & 0x07;
581 if (ack != SWD_ACK_OK || (buffer[idx] & 0x08)) {
582 LOG_DEBUG("SWD ack not OK: %d %s", buffer[idx-1],
583 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
584 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
589 if (pending_transfer_count != buffer[1])
590 LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
591 pending_transfer_count, buffer[1]);
593 for (int i = 0; i < buffer[1]; i++) {
594 if (pending_transfers[i].cmd & SWD_CMD_RnW) {
595 static uint32_t last_read;
596 uint32_t data = le_to_h_u32(&buffer[idx]);
600 LOG_DEBUG("Read result: %"PRIx32, data);
602 /* Imitate posted AP reads */
603 if ((pending_transfers[i].cmd & SWD_CMD_APnDP) ||
604 ((pending_transfers[i].cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
609 if (pending_transfers[i].buffer)
610 *(uint32_t *)pending_transfers[i].buffer = tmp;
615 pending_transfer_count = 0;
616 int retval = queued_retval;
617 queued_retval = ERROR_OK;
622 static void cmsis_dap_swd_queue_cmd(struct adiv5_dap *dap, uint8_t cmd, uint32_t *dst, uint32_t data)
624 if (pending_transfer_count == pending_queue_len) {
625 /* Not enough room in the queue. Run the queue. */
626 queued_retval = cmsis_dap_swd_run_queue(dap);
629 if (queued_retval != ERROR_OK)
632 pending_transfers[pending_transfer_count].data = data;
633 pending_transfers[pending_transfer_count].cmd = cmd;
634 if (cmd & SWD_CMD_RnW) {
635 /* Queue a read transaction */
636 pending_transfers[pending_transfer_count].buffer = dst;
638 pending_transfer_count++;
641 static void cmsis_dap_swd_write_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t value)
643 assert(!(cmd & SWD_CMD_RnW));
644 cmsis_dap_swd_queue_cmd(dap, cmd, NULL, value);
647 static void cmsis_dap_swd_read_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t *value)
649 assert(cmd & SWD_CMD_RnW);
650 cmsis_dap_swd_queue_cmd(dap, cmd, value, 0);
653 static int cmsis_dap_get_version_info(void)
657 /* INFO_ID_FW_VER - string */
658 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
659 if (retval != ERROR_OK)
662 if (data[0]) /* strlen */
663 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
668 static int cmsis_dap_get_caps_info(void)
672 /* INFO_ID_CAPS - byte */
673 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
674 if (retval != ERROR_OK)
678 uint8_t caps = data[1];
680 cmsis_dap_handle->caps = caps;
682 if (caps & INFO_CAPS_SWD)
683 LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
684 if (caps & INFO_CAPS_JTAG)
685 LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
691 static int cmsis_dap_get_status(void)
695 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
697 if (retval == ERROR_OK) {
698 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
699 (d & (0x01 << 0)) ? 1 : 0, /* Bit 0: SWCLK/TCK */
700 (d & (0x01 << 1)) ? 1 : 0, /* Bit 1: SWDIO/TMS */
701 (d & (0x01 << 2)) ? 1 : 0, /* Bit 2: TDI */
702 (d & (0x01 << 3)) ? 1 : 0, /* Bit 3: TDO */
703 (d & (0x01 << 5)) ? 1 : 0, /* Bit 5: nTRST */
704 (d & (0x01 << 7)) ? 1 : 0); /* Bit 7: nRESET */
710 static int cmsis_dap_swd_switch_seq(struct adiv5_dap *dap, enum swd_special_seq seq)
712 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
717 /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
718 cmsis_dap_cmd_DAP_Disconnect();
720 /* When we are reconnecting, DAP_Connect needs to be rerun, at
721 * least on Keil ULINK-ME */
722 retval = cmsis_dap_cmd_DAP_Connect(seq == LINE_RESET || seq == JTAG_TO_SWD ?
723 CONNECT_SWD : CONNECT_JTAG);
724 if (retval != ERROR_OK)
729 LOG_DEBUG("SWD line reset");
730 s = swd_seq_line_reset;
731 s_len = swd_seq_line_reset_len;
734 LOG_DEBUG("JTAG-to-SWD");
735 s = swd_seq_jtag_to_swd;
736 s_len = swd_seq_jtag_to_swd_len;
739 LOG_DEBUG("SWD-to-JTAG");
740 s = swd_seq_swd_to_jtag;
741 s_len = swd_seq_swd_to_jtag_len;
744 LOG_ERROR("Sequence %d not supported", seq);
748 buffer[0] = 0; /* report number */
749 buffer[1] = CMD_DAP_SWJ_SEQ;
751 bit_copy(&buffer[3], 0, s, 0, s_len);
753 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, DIV_ROUND_UP(s_len, 8) + 3);
755 if (retval != ERROR_OK || buffer[1] != DAP_OK)
761 static int cmsis_dap_swd_open(void)
765 if (cmsis_dap_handle == NULL) {
767 retval = cmsis_dap_usb_open();
768 if (retval != ERROR_OK)
771 retval = cmsis_dap_get_caps_info();
772 if (retval != ERROR_OK)
776 if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
777 LOG_ERROR("CMSIS-DAP: SWD not supported");
778 return ERROR_JTAG_DEVICE_ERROR;
781 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
782 if (retval != ERROR_OK)
785 /* Add more setup here.??... */
787 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
791 static int cmsis_dap_init(void)
797 retval = cmsis_dap_swd_open();
798 if (retval != ERROR_OK)
802 if (cmsis_dap_handle == NULL) {
805 retval = cmsis_dap_usb_open();
806 if (retval != ERROR_OK)
809 retval = cmsis_dap_get_caps_info();
810 if (retval != ERROR_OK)
813 /* Connect in JTAG mode */
814 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
815 LOG_ERROR("CMSIS-DAP: JTAG not supported");
816 return ERROR_JTAG_DEVICE_ERROR;
819 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
820 if (retval != ERROR_OK)
823 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
826 retval = cmsis_dap_get_version_info();
827 if (retval != ERROR_OK)
830 /* INFO_ID_PKT_SZ - short */
831 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
832 if (retval != ERROR_OK)
835 if (data[0] == 2) { /* short */
836 uint16_t pkt_sz = data[1] + (data[2] << 8);
838 /* 4 bytes of command header + 5 bytes per register
839 * write. For bulk read sequences just 4 bytes are
840 * needed per transfer, so this is suboptimal. */
841 pending_queue_len = (pkt_sz - 4) / 5;
842 pending_transfers = malloc(pending_queue_len * sizeof(*pending_transfers));
843 if (!pending_transfers) {
844 LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
848 if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
849 /* reallocate buffer */
850 cmsis_dap_handle->packet_size = pkt_sz + 1;
851 cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
852 cmsis_dap_handle->packet_size);
853 if (cmsis_dap_handle->packet_buffer == NULL) {
854 LOG_ERROR("unable to reallocate memory");
859 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
862 /* INFO_ID_PKT_CNT - byte */
863 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
864 if (retval != ERROR_OK)
867 if (data[0] == 1) { /* byte */
868 uint16_t pkt_cnt = data[1];
869 cmsis_dap_handle->packet_count = pkt_cnt;
870 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
873 retval = cmsis_dap_get_status();
874 if (retval != ERROR_OK)
877 /* Now try to connect to the target
878 * TODO: This is all SWD only @ present */
879 retval = cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
880 if (retval != ERROR_OK)
883 /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
884 * up to 64 times. This must be changed to 0 if sticky
885 * overrun detection is enabled. */
886 retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
887 if (retval != ERROR_OK)
889 /* Data Phase (bit 2) must be set to 1 if sticky overrun
890 * detection is enabled */
891 retval = cmsis_dap_cmd_DAP_SWD_Configure(0); /* 1 TRN, no Data Phase */
892 if (retval != ERROR_OK)
895 retval = cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
896 if (retval != ERROR_OK)
899 /* support connecting with srst asserted */
900 enum reset_types jtag_reset_config = jtag_get_reset_config();
902 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
903 if (jtag_reset_config & RESET_SRST_NO_GATING) {
904 retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL);
905 if (retval != ERROR_OK)
907 LOG_INFO("Connecting under reset");
911 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
913 LOG_INFO("CMSIS-DAP: Interface ready");
918 static int cmsis_dap_swd_init(void)
924 static int cmsis_dap_quit(void)
926 cmsis_dap_cmd_DAP_Disconnect();
927 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
929 cmsis_dap_usb_close(cmsis_dap_handle);
934 static void cmsis_dap_execute_reset(struct jtag_command *cmd)
936 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(cmd->cmd.reset->srst ? 0 : (1 << 7), \
938 if (retval != ERROR_OK)
939 LOG_ERROR("CMSIS-DAP: Interface reset failed");
942 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
945 int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
946 if (retval != ERROR_OK)
948 jtag_sleep(cmd->cmd.sleep->us);
951 static void cmsis_dap_execute_command(struct jtag_command *cmd)
955 cmsis_dap_execute_reset(cmd);
958 cmsis_dap_execute_sleep(cmd);
961 LOG_ERROR("BUG: unknown JTAG command type encountered");
966 static int cmsis_dap_execute_queue(void)
968 struct jtag_command *cmd = jtag_command_queue;
970 while (cmd != NULL) {
971 cmsis_dap_execute_command(cmd);
978 static int cmsis_dap_speed(int speed)
980 if (speed > DAP_MAX_CLOCK) {
981 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, DAP_MAX_CLOCK);
982 speed = DAP_MAX_CLOCK;
986 LOG_INFO("RTCK not supported");
987 return ERROR_JTAG_NOT_IMPLEMENTED;
990 return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
993 static int cmsis_dap_speed_div(int speed, int *khz)
999 static int cmsis_dap_khz(int khz, int *jtag_speed)
1005 static int_least32_t cmsis_dap_swd_frequency(struct adiv5_dap *dap, int_least32_t hz)
1008 cmsis_dap_speed(hz / 1000);
1013 COMMAND_HANDLER(cmsis_dap_handle_info_command)
1015 if (cmsis_dap_get_version_info() == ERROR_OK)
1016 cmsis_dap_get_status();
1021 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
1023 if (CMD_ARGC > MAX_USB_IDS * 2) {
1024 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1025 "(maximum is %d pairs)", MAX_USB_IDS);
1026 CMD_ARGC = MAX_USB_IDS * 2;
1028 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
1029 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1031 return ERROR_COMMAND_SYNTAX_ERROR;
1032 /* remove the incomplete trailing id */
1037 for (i = 0; i < CMD_ARGC; i += 2) {
1038 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
1039 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
1043 * Explicitly terminate, in case there are multiples instances of
1044 * cmsis_dap_vid_pid.
1046 cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
1051 COMMAND_HANDLER(cmsis_dap_handle_serial_command)
1053 if (CMD_ARGC == 1) {
1054 size_t len = mbstowcs(NULL, CMD_ARGV[0], 0);
1055 cmsis_dap_serial = calloc(len + 1, sizeof(wchar_t));
1056 if (cmsis_dap_serial == NULL) {
1057 LOG_ERROR("unable to allocate memory");
1060 if (mbstowcs(cmsis_dap_serial, CMD_ARGV[0], len + 1) == (size_t)-1) {
1061 free(cmsis_dap_serial);
1062 cmsis_dap_serial = NULL;
1063 LOG_ERROR("unable to convert serial");
1066 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1072 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
1075 .handler = &cmsis_dap_handle_info_command,
1076 .mode = COMMAND_EXEC,
1078 .help = "show cmsis-dap info",
1080 COMMAND_REGISTRATION_DONE
1083 static const struct command_registration cmsis_dap_command_handlers[] = {
1085 .name = "cmsis-dap",
1086 .mode = COMMAND_ANY,
1087 .help = "perform CMSIS-DAP management",
1089 .chain = cmsis_dap_subcommand_handlers,
1092 .name = "cmsis_dap_vid_pid",
1093 .handler = &cmsis_dap_handle_vid_pid_command,
1094 .mode = COMMAND_CONFIG,
1095 .help = "the vendor ID and product ID of the CMSIS-DAP device",
1096 .usage = "(vid pid)* ",
1099 .name = "cmsis_dap_serial",
1100 .handler = &cmsis_dap_handle_serial_command,
1101 .mode = COMMAND_CONFIG,
1102 .help = "set the serial number of the adapter",
1103 .usage = "serial_string",
1105 COMMAND_REGISTRATION_DONE
1108 static const struct swd_driver cmsis_dap_swd_driver = {
1109 .init = cmsis_dap_swd_init,
1110 .frequency = cmsis_dap_swd_frequency,
1111 .switch_seq = cmsis_dap_swd_switch_seq,
1112 .read_reg = cmsis_dap_swd_read_reg,
1113 .write_reg = cmsis_dap_swd_write_reg,
1114 .run = cmsis_dap_swd_run_queue,
1117 static const char * const cmsis_dap_transport[] = { "swd", NULL };
1119 struct jtag_interface cmsis_dap_interface = {
1120 .name = "cmsis-dap",
1121 .commands = cmsis_dap_command_handlers,
1122 .swd = &cmsis_dap_swd_driver,
1123 .transports = cmsis_dap_transport,
1125 .execute_queue = cmsis_dap_execute_queue,
1126 .speed = cmsis_dap_speed,
1127 .speed_div = cmsis_dap_speed_div,
1128 .khz = cmsis_dap_khz,
1129 .init = cmsis_dap_init,
1130 .quit = cmsis_dap_quit,