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 ((cur_dev
->serial_number
!= NULL
) && wcscmp(cmsis_dap_serial
, cur_dev
->serial_number
) == 0) {
227 cur_dev
= cur_dev
->next
;
230 if (NULL
!= cur_dev
) {
231 target_vid
= cur_dev
->vendor_id
;
232 target_pid
= cur_dev
->product_id
;
234 target_serial
= cmsis_dap_serial
;
237 hid_free_enumeration(devs
);
239 if (target_vid
== 0 && target_pid
== 0) {
240 LOG_ERROR("unable to find CMSIS-DAP device");
244 if (hid_init() != 0) {
245 LOG_ERROR("unable to open HIDAPI");
249 dev
= hid_open(target_vid
, target_pid
, target_serial
);
252 LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid
, target_pid
);
256 struct cmsis_dap
*dap
= malloc(sizeof(struct cmsis_dap
));
258 LOG_ERROR("unable to allocate memory");
262 dap
->dev_handle
= dev
;
266 cmsis_dap_handle
= dap
;
268 /* allocate default packet buffer, may be changed later.
269 * currently with HIDAPI we have no way of getting the output report length
270 * without this info we cannot communicate with the adapter.
271 * For the moment we ahve to hard code the packet size */
273 int packet_size
= PACKET_SIZE
;
275 /* atmel cmsis-dap uses 512 byte reports */
276 /* TODO: HID report descriptor should be parsed instead of
277 * hardcoding a match by VID */
278 if (target_vid
== 0x03eb)
279 packet_size
= 512 + 1;
281 cmsis_dap_handle
->packet_buffer
= malloc(packet_size
);
282 cmsis_dap_handle
->packet_size
= packet_size
;
284 if (cmsis_dap_handle
->packet_buffer
== NULL
) {
285 LOG_ERROR("unable to allocate memory");
292 static void cmsis_dap_usb_close(struct cmsis_dap
*dap
)
294 hid_close(dap
->dev_handle
);
297 free(cmsis_dap_handle
->packet_buffer
);
298 free(cmsis_dap_handle
);
299 cmsis_dap_handle
= NULL
;
300 free(cmsis_dap_serial
);
301 cmsis_dap_serial
= NULL
;
302 free(pending_transfers
);
303 pending_transfers
= NULL
;
308 /* Send a message and receive the reply */
309 static int cmsis_dap_usb_xfer(struct cmsis_dap
*dap
, int txlen
)
311 /* Pad the rest of the TX buffer with 0's */
312 memset(dap
->packet_buffer
+ txlen
, 0, dap
->packet_size
- txlen
);
314 /* write data to device */
315 int retval
= hid_write(dap
->dev_handle
, dap
->packet_buffer
, dap
->packet_size
);
317 LOG_ERROR("error writing data: %ls", hid_error(dap
->dev_handle
));
322 retval
= hid_read_timeout(dap
->dev_handle
, dap
->packet_buffer
, dap
->packet_size
, USB_TIMEOUT
);
323 if (retval
== -1 || retval
== 0) {
324 LOG_DEBUG("error reading data: %ls", hid_error(dap
->dev_handle
));
331 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins
, uint8_t mask
, uint32_t delay
, uint8_t *input
)
334 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
336 buffer
[0] = 0; /* report number */
337 buffer
[1] = CMD_DAP_SWJ_PINS
;
340 buffer
[4] = delay
& 0xff;
341 buffer
[5] = (delay
>> 8) & 0xff;
342 buffer
[6] = (delay
>> 16) & 0xff;
343 buffer
[7] = (delay
>> 24) & 0xff;
344 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 8);
346 if (retval
!= ERROR_OK
) {
347 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
348 return ERROR_JTAG_DEVICE_ERROR
;
357 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock
)
360 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
362 /* set clock in Hz */
364 buffer
[0] = 0; /* report number */
365 buffer
[1] = CMD_DAP_SWJ_CLOCK
;
366 buffer
[2] = swj_clock
& 0xff;
367 buffer
[3] = (swj_clock
>> 8) & 0xff;
368 buffer
[4] = (swj_clock
>> 16) & 0xff;
369 buffer
[5] = (swj_clock
>> 24) & 0xff;
370 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 6);
372 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
373 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
374 return ERROR_JTAG_DEVICE_ERROR
;
380 static int cmsis_dap_cmd_DAP_Info(uint8_t info
, uint8_t **data
)
383 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
385 buffer
[0] = 0; /* report number */
386 buffer
[1] = CMD_DAP_INFO
;
388 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 3);
390 if (retval
!= ERROR_OK
) {
391 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
392 return ERROR_JTAG_DEVICE_ERROR
;
395 *data
= &(buffer
[1]);
400 static int cmsis_dap_cmd_DAP_LED(uint8_t leds
)
403 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
405 buffer
[0] = 0; /* report number */
406 buffer
[1] = CMD_DAP_LED
;
409 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 4);
411 if (retval
!= ERROR_OK
|| buffer
[1] != 0x00) {
412 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
413 return ERROR_JTAG_DEVICE_ERROR
;
419 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode
)
422 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
424 buffer
[0] = 0; /* report number */
425 buffer
[1] = CMD_DAP_CONNECT
;
427 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 3);
429 if (retval
!= ERROR_OK
) {
430 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
431 return ERROR_JTAG_DEVICE_ERROR
;
434 if (buffer
[1] != mode
) {
435 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode
);
436 return ERROR_JTAG_DEVICE_ERROR
;
442 static int cmsis_dap_cmd_DAP_Disconnect(void)
445 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
447 buffer
[0] = 0; /* report number */
448 buffer
[1] = CMD_DAP_DISCONNECT
;
449 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 2);
451 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
452 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
453 return ERROR_JTAG_DEVICE_ERROR
;
459 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle
, uint16_t retry_count
, uint16_t match_retry
)
462 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
464 buffer
[0] = 0; /* report number */
465 buffer
[1] = CMD_DAP_TFER_CONFIGURE
;
467 buffer
[3] = retry_count
& 0xff;
468 buffer
[4] = (retry_count
>> 8) & 0xff;
469 buffer
[5] = match_retry
& 0xff;
470 buffer
[6] = (match_retry
>> 8) & 0xff;
471 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 7);
473 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
474 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
475 return ERROR_JTAG_DEVICE_ERROR
;
481 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg
)
484 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
486 buffer
[0] = 0; /* report number */
487 buffer
[1] = CMD_DAP_SWD_CONFIGURE
;
489 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 3);
491 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
492 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
493 return ERROR_JTAG_DEVICE_ERROR
;
500 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us
)
503 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
505 buffer
[0] = 0; /* report number */
506 buffer
[1] = CMD_DAP_DELAY
;
507 buffer
[2] = delay_us
& 0xff;
508 buffer
[3] = (delay_us
>> 8) & 0xff;
509 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 4);
511 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
512 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
513 return ERROR_JTAG_DEVICE_ERROR
;
520 static int cmsis_dap_swd_run_queue(void)
522 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
524 LOG_DEBUG("Executing %d queued transactions", pending_transfer_count
);
526 if (queued_retval
!= ERROR_OK
) {
527 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
531 if (!pending_transfer_count
)
535 buffer
[idx
++] = 0; /* report number */
536 buffer
[idx
++] = CMD_DAP_TFER
;
537 buffer
[idx
++] = 0x00; /* DAP Index */
538 buffer
[idx
++] = pending_transfer_count
;
540 for (int i
= 0; i
< pending_transfer_count
; i
++) {
541 uint8_t cmd
= pending_transfers
[i
].cmd
;
542 uint32_t data
= pending_transfers
[i
].data
;
544 LOG_DEBUG("%s %s reg %x %"PRIx32
,
545 cmd
& SWD_CMD_APnDP
? "AP" : "DP",
546 cmd
& SWD_CMD_RnW
? "read" : "write",
547 (cmd
& SWD_CMD_A32
) >> 1, data
);
549 /* When proper WAIT handling is implemented in the
550 * common SWD framework, this kludge can be
551 * removed. However, this might lead to minor
552 * performance degradation as the adapter wouldn't be
553 * able to automatically retry anything (because ARM
554 * has forgotten to implement sticky error flags
555 * clearing). See also comments regarding
556 * cmsis_dap_cmd_DAP_TFER_Configure() and
557 * cmsis_dap_cmd_DAP_SWD_Configure() in
560 if (!(cmd
& SWD_CMD_RnW
) &&
561 !(cmd
& SWD_CMD_APnDP
) &&
562 (cmd
& SWD_CMD_A32
) >> 1 == DP_CTRL_STAT
&&
563 (data
& CORUNDETECT
)) {
564 LOG_DEBUG("refusing to enable sticky overrun detection");
565 data
&= ~CORUNDETECT
;
568 buffer
[idx
++] = (cmd
>> 1) & 0x0f;
569 if (!(cmd
& SWD_CMD_RnW
)) {
570 buffer
[idx
++] = (data
) & 0xff;
571 buffer
[idx
++] = (data
>> 8) & 0xff;
572 buffer
[idx
++] = (data
>> 16) & 0xff;
573 buffer
[idx
++] = (data
>> 24) & 0xff;
577 queued_retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, idx
);
578 if (queued_retval
!= ERROR_OK
)
582 uint8_t ack
= buffer
[idx
] & 0x07;
583 if (ack
!= SWD_ACK_OK
|| (buffer
[idx
] & 0x08)) {
584 LOG_DEBUG("SWD ack not OK: %d %s", buffer
[idx
-1],
585 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
586 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
591 if (pending_transfer_count
!= buffer
[1])
592 LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
593 pending_transfer_count
, buffer
[1]);
595 for (int i
= 0; i
< buffer
[1]; i
++) {
596 if (pending_transfers
[i
].cmd
& SWD_CMD_RnW
) {
597 static uint32_t last_read
;
598 uint32_t data
= le_to_h_u32(&buffer
[idx
]);
602 LOG_DEBUG("Read result: %"PRIx32
, data
);
604 /* Imitate posted AP reads */
605 if ((pending_transfers
[i
].cmd
& SWD_CMD_APnDP
) ||
606 ((pending_transfers
[i
].cmd
& SWD_CMD_A32
) >> 1 == DP_RDBUFF
)) {
611 if (pending_transfers
[i
].buffer
)
612 *(uint32_t *)pending_transfers
[i
].buffer
= tmp
;
617 pending_transfer_count
= 0;
618 int retval
= queued_retval
;
619 queued_retval
= ERROR_OK
;
624 static void cmsis_dap_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
)
626 if (pending_transfer_count
== pending_queue_len
) {
627 /* Not enough room in the queue. Run the queue. */
628 queued_retval
= cmsis_dap_swd_run_queue();
631 if (queued_retval
!= ERROR_OK
)
634 pending_transfers
[pending_transfer_count
].data
= data
;
635 pending_transfers
[pending_transfer_count
].cmd
= cmd
;
636 if (cmd
& SWD_CMD_RnW
) {
637 /* Queue a read transaction */
638 pending_transfers
[pending_transfer_count
].buffer
= dst
;
640 pending_transfer_count
++;
643 static void cmsis_dap_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
645 assert(!(cmd
& SWD_CMD_RnW
));
646 cmsis_dap_swd_queue_cmd(cmd
, NULL
, value
);
649 static void cmsis_dap_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
651 assert(cmd
& SWD_CMD_RnW
);
652 cmsis_dap_swd_queue_cmd(cmd
, value
, 0);
655 static int cmsis_dap_get_version_info(void)
659 /* INFO_ID_FW_VER - string */
660 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER
, &data
);
661 if (retval
!= ERROR_OK
)
664 if (data
[0]) /* strlen */
665 LOG_INFO("CMSIS-DAP: FW Version = %s", &data
[1]);
670 static int cmsis_dap_get_caps_info(void)
674 /* INFO_ID_CAPS - byte */
675 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS
, &data
);
676 if (retval
!= ERROR_OK
)
680 uint8_t caps
= data
[1];
682 cmsis_dap_handle
->caps
= caps
;
684 if (caps
& INFO_CAPS_SWD
)
685 LOG_INFO("CMSIS-DAP: %s", info_caps_str
[0]);
686 if (caps
& INFO_CAPS_JTAG
)
687 LOG_INFO("CMSIS-DAP: %s", info_caps_str
[1]);
693 static int cmsis_dap_get_status(void)
697 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d
);
699 if (retval
== ERROR_OK
) {
700 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
701 (d
& (0x01 << 0)) ? 1 : 0, /* Bit 0: SWCLK/TCK */
702 (d
& (0x01 << 1)) ? 1 : 0, /* Bit 1: SWDIO/TMS */
703 (d
& (0x01 << 2)) ? 1 : 0, /* Bit 2: TDI */
704 (d
& (0x01 << 3)) ? 1 : 0, /* Bit 3: TDO */
705 (d
& (0x01 << 5)) ? 1 : 0, /* Bit 5: nTRST */
706 (d
& (0x01 << 7)) ? 1 : 0); /* Bit 7: nRESET */
712 static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq
)
714 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
719 /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
720 cmsis_dap_cmd_DAP_Disconnect();
722 /* When we are reconnecting, DAP_Connect needs to be rerun, at
723 * least on Keil ULINK-ME */
724 retval
= cmsis_dap_cmd_DAP_Connect(seq
== LINE_RESET
|| seq
== JTAG_TO_SWD
?
725 CONNECT_SWD
: CONNECT_JTAG
);
726 if (retval
!= ERROR_OK
)
731 LOG_DEBUG("SWD line reset");
732 s
= swd_seq_line_reset
;
733 s_len
= swd_seq_line_reset_len
;
736 LOG_DEBUG("JTAG-to-SWD");
737 s
= swd_seq_jtag_to_swd
;
738 s_len
= swd_seq_jtag_to_swd_len
;
741 LOG_DEBUG("SWD-to-JTAG");
742 s
= swd_seq_swd_to_jtag
;
743 s_len
= swd_seq_swd_to_jtag_len
;
746 LOG_ERROR("Sequence %d not supported", seq
);
750 buffer
[0] = 0; /* report number */
751 buffer
[1] = CMD_DAP_SWJ_SEQ
;
753 bit_copy(&buffer
[3], 0, s
, 0, s_len
);
755 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, DIV_ROUND_UP(s_len
, 8) + 3);
757 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
)
763 static int cmsis_dap_swd_open(void)
767 if (cmsis_dap_handle
== NULL
) {
769 retval
= cmsis_dap_usb_open();
770 if (retval
!= ERROR_OK
)
773 retval
= cmsis_dap_get_caps_info();
774 if (retval
!= ERROR_OK
)
778 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_SWD
)) {
779 LOG_ERROR("CMSIS-DAP: SWD not supported");
780 return ERROR_JTAG_DEVICE_ERROR
;
783 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_SWD
);
784 if (retval
!= ERROR_OK
)
787 /* Add more setup here.??... */
789 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
793 static int cmsis_dap_init(void)
799 retval
= cmsis_dap_swd_open();
800 if (retval
!= ERROR_OK
)
804 if (cmsis_dap_handle
== NULL
) {
807 retval
= cmsis_dap_usb_open();
808 if (retval
!= ERROR_OK
)
811 retval
= cmsis_dap_get_caps_info();
812 if (retval
!= ERROR_OK
)
815 /* Connect in JTAG mode */
816 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_JTAG
)) {
817 LOG_ERROR("CMSIS-DAP: JTAG not supported");
818 return ERROR_JTAG_DEVICE_ERROR
;
821 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG
);
822 if (retval
!= ERROR_OK
)
825 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
828 retval
= cmsis_dap_get_version_info();
829 if (retval
!= ERROR_OK
)
832 /* INFO_ID_PKT_SZ - short */
833 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ
, &data
);
834 if (retval
!= ERROR_OK
)
837 if (data
[0] == 2) { /* short */
838 uint16_t pkt_sz
= data
[1] + (data
[2] << 8);
840 /* 4 bytes of command header + 5 bytes per register
841 * write. For bulk read sequences just 4 bytes are
842 * needed per transfer, so this is suboptimal. */
843 pending_queue_len
= (pkt_sz
- 4) / 5;
844 pending_transfers
= malloc(pending_queue_len
* sizeof(*pending_transfers
));
845 if (!pending_transfers
) {
846 LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
850 if (cmsis_dap_handle
->packet_size
!= pkt_sz
+ 1) {
851 /* reallocate buffer */
852 cmsis_dap_handle
->packet_size
= pkt_sz
+ 1;
853 cmsis_dap_handle
->packet_buffer
= realloc(cmsis_dap_handle
->packet_buffer
,
854 cmsis_dap_handle
->packet_size
);
855 if (cmsis_dap_handle
->packet_buffer
== NULL
) {
856 LOG_ERROR("unable to reallocate memory");
861 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16
, pkt_sz
);
864 /* INFO_ID_PKT_CNT - byte */
865 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT
, &data
);
866 if (retval
!= ERROR_OK
)
869 if (data
[0] == 1) { /* byte */
870 uint16_t pkt_cnt
= data
[1];
871 cmsis_dap_handle
->packet_count
= pkt_cnt
;
872 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16
, pkt_cnt
);
875 retval
= cmsis_dap_get_status();
876 if (retval
!= ERROR_OK
)
879 /* Now try to connect to the target
880 * TODO: This is all SWD only @ present */
881 retval
= cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
882 if (retval
!= ERROR_OK
)
885 /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
886 * up to 64 times. This must be changed to 0 if sticky
887 * overrun detection is enabled. */
888 retval
= cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
889 if (retval
!= ERROR_OK
)
891 /* Data Phase (bit 2) must be set to 1 if sticky overrun
892 * detection is enabled */
893 retval
= cmsis_dap_cmd_DAP_SWD_Configure(0); /* 1 TRN, no Data Phase */
894 if (retval
!= ERROR_OK
)
897 retval
= cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
898 if (retval
!= ERROR_OK
)
901 /* support connecting with srst asserted */
902 enum reset_types jtag_reset_config
= jtag_get_reset_config();
904 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
905 if (jtag_reset_config
& RESET_SRST_NO_GATING
) {
906 retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL
);
907 if (retval
!= ERROR_OK
)
909 LOG_INFO("Connecting under reset");
913 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
915 LOG_INFO("CMSIS-DAP: Interface ready");
920 static int cmsis_dap_swd_init(void)
926 static int cmsis_dap_quit(void)
928 cmsis_dap_cmd_DAP_Disconnect();
929 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
931 cmsis_dap_usb_close(cmsis_dap_handle
);
936 static void cmsis_dap_execute_reset(struct jtag_command
*cmd
)
938 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(cmd
->cmd
.reset
->srst
? 0 : (1 << 7), \
940 if (retval
!= ERROR_OK
)
941 LOG_ERROR("CMSIS-DAP: Interface reset failed");
944 static void cmsis_dap_execute_sleep(struct jtag_command
*cmd
)
947 int retval
= cmsis_dap_cmd_DAP_Delay(cmd
->cmd
.sleep
->us
);
948 if (retval
!= ERROR_OK
)
950 jtag_sleep(cmd
->cmd
.sleep
->us
);
953 static void cmsis_dap_execute_command(struct jtag_command
*cmd
)
957 cmsis_dap_execute_reset(cmd
);
960 cmsis_dap_execute_sleep(cmd
);
963 LOG_ERROR("BUG: unknown JTAG command type encountered");
968 static int cmsis_dap_execute_queue(void)
970 struct jtag_command
*cmd
= jtag_command_queue
;
972 while (cmd
!= NULL
) {
973 cmsis_dap_execute_command(cmd
);
980 static int cmsis_dap_speed(int speed
)
982 if (speed
> DAP_MAX_CLOCK
) {
983 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed
, DAP_MAX_CLOCK
);
984 speed
= DAP_MAX_CLOCK
;
988 LOG_INFO("RTCK not supported");
989 return ERROR_JTAG_NOT_IMPLEMENTED
;
992 return cmsis_dap_cmd_DAP_SWJ_Clock(speed
);
995 static int cmsis_dap_speed_div(int speed
, int *khz
)
1001 static int cmsis_dap_khz(int khz
, int *jtag_speed
)
1007 static int_least32_t cmsis_dap_swd_frequency(int_least32_t hz
)
1010 cmsis_dap_speed(hz
/ 1000);
1015 COMMAND_HANDLER(cmsis_dap_handle_info_command
)
1017 if (cmsis_dap_get_version_info() == ERROR_OK
)
1018 cmsis_dap_get_status();
1023 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command
)
1025 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
1026 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1027 "(maximum is %d pairs)", MAX_USB_IDS
);
1028 CMD_ARGC
= MAX_USB_IDS
* 2;
1030 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
1031 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1033 return ERROR_COMMAND_SYNTAX_ERROR
;
1034 /* remove the incomplete trailing id */
1039 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
1040 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], cmsis_dap_vid
[i
>> 1]);
1041 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], cmsis_dap_pid
[i
>> 1]);
1045 * Explicitly terminate, in case there are multiples instances of
1046 * cmsis_dap_vid_pid.
1048 cmsis_dap_vid
[i
>> 1] = cmsis_dap_pid
[i
>> 1] = 0;
1053 COMMAND_HANDLER(cmsis_dap_handle_serial_command
)
1055 if (CMD_ARGC
== 1) {
1056 size_t len
= mbstowcs(NULL
, CMD_ARGV
[0], 0);
1057 cmsis_dap_serial
= calloc(len
+ 1, sizeof(wchar_t));
1058 if (cmsis_dap_serial
== NULL
) {
1059 LOG_ERROR("unable to allocate memory");
1062 if (mbstowcs(cmsis_dap_serial
, CMD_ARGV
[0], len
+ 1) == (size_t)-1) {
1063 free(cmsis_dap_serial
);
1064 cmsis_dap_serial
= NULL
;
1065 LOG_ERROR("unable to convert serial");
1068 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1074 static const struct command_registration cmsis_dap_subcommand_handlers
[] = {
1077 .handler
= &cmsis_dap_handle_info_command
,
1078 .mode
= COMMAND_EXEC
,
1080 .help
= "show cmsis-dap info",
1082 COMMAND_REGISTRATION_DONE
1085 static const struct command_registration cmsis_dap_command_handlers
[] = {
1087 .name
= "cmsis-dap",
1088 .mode
= COMMAND_ANY
,
1089 .help
= "perform CMSIS-DAP management",
1091 .chain
= cmsis_dap_subcommand_handlers
,
1094 .name
= "cmsis_dap_vid_pid",
1095 .handler
= &cmsis_dap_handle_vid_pid_command
,
1096 .mode
= COMMAND_CONFIG
,
1097 .help
= "the vendor ID and product ID of the CMSIS-DAP device",
1098 .usage
= "(vid pid)* ",
1101 .name
= "cmsis_dap_serial",
1102 .handler
= &cmsis_dap_handle_serial_command
,
1103 .mode
= COMMAND_CONFIG
,
1104 .help
= "set the serial number of the adapter",
1105 .usage
= "serial_string",
1107 COMMAND_REGISTRATION_DONE
1110 static const struct swd_driver cmsis_dap_swd_driver
= {
1111 .init
= cmsis_dap_swd_init
,
1112 .frequency
= cmsis_dap_swd_frequency
,
1113 .switch_seq
= cmsis_dap_swd_switch_seq
,
1114 .read_reg
= cmsis_dap_swd_read_reg
,
1115 .write_reg
= cmsis_dap_swd_write_reg
,
1116 .run
= cmsis_dap_swd_run_queue
,
1119 static const char * const cmsis_dap_transport
[] = { "swd", NULL
};
1121 struct jtag_interface cmsis_dap_interface
= {
1122 .name
= "cmsis-dap",
1123 .commands
= cmsis_dap_command_handlers
,
1124 .swd
= &cmsis_dap_swd_driver
,
1125 .transports
= cmsis_dap_transport
,
1127 .execute_queue
= cmsis_dap_execute_queue
,
1128 .speed
= cmsis_dap_speed
,
1129 .speed_div
= cmsis_dap_speed_div
,
1130 .khz
= cmsis_dap_khz
,
1131 .init
= cmsis_dap_init
,
1132 .quit
= cmsis_dap_quit
,
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)