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");
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 /* When we are reconnecting, DAP_Connect needs to be rerun, at
718 * least on Keil ULINK-ME */
719 retval
= cmsis_dap_cmd_DAP_Connect(seq
== LINE_RESET
|| seq
== JTAG_TO_SWD
?
720 CONNECT_SWD
: CONNECT_JTAG
);
721 if (retval
!= ERROR_OK
)
726 LOG_DEBUG("SWD line reset");
727 s
= swd_seq_line_reset
;
728 s_len
= swd_seq_line_reset_len
;
731 LOG_DEBUG("JTAG-to-SWD");
732 s
= swd_seq_jtag_to_swd
;
733 s_len
= swd_seq_jtag_to_swd_len
;
736 LOG_DEBUG("SWD-to-JTAG");
737 s
= swd_seq_swd_to_jtag
;
738 s_len
= swd_seq_swd_to_jtag_len
;
741 LOG_ERROR("Sequence %d not supported", seq
);
745 buffer
[0] = 0; /* report number */
746 buffer
[1] = CMD_DAP_SWJ_SEQ
;
748 bit_copy(&buffer
[3], 0, s
, 0, s_len
);
750 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, DIV_ROUND_UP(s_len
, 8) + 3);
752 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
)
758 static int cmsis_dap_swd_open(void)
762 if (cmsis_dap_handle
== NULL
) {
764 retval
= cmsis_dap_usb_open();
765 if (retval
!= ERROR_OK
)
768 retval
= cmsis_dap_get_caps_info();
769 if (retval
!= ERROR_OK
)
773 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_SWD
)) {
774 LOG_ERROR("CMSIS-DAP: SWD not supported");
775 return ERROR_JTAG_DEVICE_ERROR
;
778 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_SWD
);
779 if (retval
!= ERROR_OK
)
782 /* Add more setup here.??... */
784 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
788 static int cmsis_dap_init(void)
794 retval
= cmsis_dap_swd_open();
795 if (retval
!= ERROR_OK
)
799 if (cmsis_dap_handle
== NULL
) {
802 retval
= cmsis_dap_usb_open();
803 if (retval
!= ERROR_OK
)
806 retval
= cmsis_dap_get_caps_info();
807 if (retval
!= ERROR_OK
)
810 /* Connect in JTAG mode */
811 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_JTAG
)) {
812 LOG_ERROR("CMSIS-DAP: JTAG not supported");
813 return ERROR_JTAG_DEVICE_ERROR
;
816 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG
);
817 if (retval
!= ERROR_OK
)
820 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
823 retval
= cmsis_dap_get_version_info();
824 if (retval
!= ERROR_OK
)
827 /* INFO_ID_PKT_SZ - short */
828 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ
, &data
);
829 if (retval
!= ERROR_OK
)
832 if (data
[0] == 2) { /* short */
833 uint16_t pkt_sz
= data
[1] + (data
[2] << 8);
835 /* 4 bytes of command header + 5 bytes per register
836 * write. For bulk read sequences just 4 bytes are
837 * needed per transfer, so this is suboptimal. */
838 pending_queue_len
= (pkt_sz
- 4) / 5;
839 pending_transfers
= malloc(pending_queue_len
* sizeof(*pending_transfers
));
840 if (!pending_transfers
) {
841 LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
845 if (cmsis_dap_handle
->packet_size
!= pkt_sz
+ 1) {
846 /* reallocate buffer */
847 cmsis_dap_handle
->packet_size
= pkt_sz
+ 1;
848 cmsis_dap_handle
->packet_buffer
= realloc(cmsis_dap_handle
->packet_buffer
,
849 cmsis_dap_handle
->packet_size
);
850 if (cmsis_dap_handle
->packet_buffer
== NULL
) {
851 LOG_ERROR("unable to reallocate memory");
856 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16
, pkt_sz
);
859 /* INFO_ID_PKT_CNT - byte */
860 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT
, &data
);
861 if (retval
!= ERROR_OK
)
864 if (data
[0] == 1) { /* byte */
865 uint16_t pkt_cnt
= data
[1];
866 cmsis_dap_handle
->packet_count
= pkt_cnt
;
867 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16
, pkt_cnt
);
870 retval
= cmsis_dap_get_status();
871 if (retval
!= ERROR_OK
)
874 /* Now try to connect to the target
875 * TODO: This is all SWD only @ present */
876 retval
= cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
877 if (retval
!= ERROR_OK
)
880 /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
881 * up to 64 times. This must be changed to 0 if sticky
882 * overrun detection is enabled. */
883 retval
= cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
884 if (retval
!= ERROR_OK
)
886 /* Data Phase (bit 2) must be set to 1 if sticky overrun
887 * detection is enabled */
888 retval
= cmsis_dap_cmd_DAP_SWD_Configure(0); /* 1 TRN, no Data Phase */
889 if (retval
!= ERROR_OK
)
892 retval
= cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
893 if (retval
!= ERROR_OK
)
896 /* support connecting with srst asserted */
897 enum reset_types jtag_reset_config
= jtag_get_reset_config();
899 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
900 if (jtag_reset_config
& RESET_SRST_NO_GATING
) {
901 retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL
);
902 if (retval
!= ERROR_OK
)
904 LOG_INFO("Connecting under reset");
908 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
910 LOG_INFO("CMSIS-DAP: Interface ready");
915 static int cmsis_dap_swd_init(void)
921 static int cmsis_dap_quit(void)
923 cmsis_dap_cmd_DAP_Disconnect();
924 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
926 cmsis_dap_usb_close(cmsis_dap_handle
);
931 static void cmsis_dap_execute_reset(struct jtag_command
*cmd
)
933 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(cmd
->cmd
.reset
->srst
? 0 : (1 << 7), \
935 if (retval
!= ERROR_OK
)
936 LOG_ERROR("CMSIS-DAP: Interface reset failed");
939 static void cmsis_dap_execute_sleep(struct jtag_command
*cmd
)
942 int retval
= cmsis_dap_cmd_DAP_Delay(cmd
->cmd
.sleep
->us
);
943 if (retval
!= ERROR_OK
)
945 jtag_sleep(cmd
->cmd
.sleep
->us
);
948 static void cmsis_dap_execute_command(struct jtag_command
*cmd
)
952 cmsis_dap_execute_reset(cmd
);
955 cmsis_dap_execute_sleep(cmd
);
958 LOG_ERROR("BUG: unknown JTAG command type encountered");
963 static int cmsis_dap_execute_queue(void)
965 struct jtag_command
*cmd
= jtag_command_queue
;
967 while (cmd
!= NULL
) {
968 cmsis_dap_execute_command(cmd
);
975 static int cmsis_dap_speed(int speed
)
977 if (speed
> DAP_MAX_CLOCK
) {
978 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed
, DAP_MAX_CLOCK
);
979 speed
= DAP_MAX_CLOCK
;
983 LOG_INFO("RTCK not supported");
984 return ERROR_JTAG_NOT_IMPLEMENTED
;
987 return cmsis_dap_cmd_DAP_SWJ_Clock(speed
);
990 static int cmsis_dap_speed_div(int speed
, int *khz
)
996 static int cmsis_dap_khz(int khz
, int *jtag_speed
)
1002 static int_least32_t cmsis_dap_swd_frequency(struct adiv5_dap
*dap
, int_least32_t hz
)
1005 cmsis_dap_speed(hz
/ 1000);
1010 COMMAND_HANDLER(cmsis_dap_handle_info_command
)
1012 if (cmsis_dap_get_version_info() == ERROR_OK
)
1013 cmsis_dap_get_status();
1018 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command
)
1020 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
1021 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1022 "(maximum is %d pairs)", MAX_USB_IDS
);
1023 CMD_ARGC
= MAX_USB_IDS
* 2;
1025 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
1026 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1028 return ERROR_COMMAND_SYNTAX_ERROR
;
1029 /* remove the incomplete trailing id */
1034 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
1035 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], cmsis_dap_vid
[i
>> 1]);
1036 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], cmsis_dap_pid
[i
>> 1]);
1040 * Explicitly terminate, in case there are multiples instances of
1041 * cmsis_dap_vid_pid.
1043 cmsis_dap_vid
[i
>> 1] = cmsis_dap_pid
[i
>> 1] = 0;
1048 COMMAND_HANDLER(cmsis_dap_handle_serial_command
)
1050 if (CMD_ARGC
== 1) {
1051 size_t len
= mbstowcs(NULL
, CMD_ARGV
[0], 0);
1052 cmsis_dap_serial
= calloc(len
+ 1, sizeof(wchar_t));
1053 if (cmsis_dap_serial
== NULL
) {
1054 LOG_ERROR("unable to allocate memory");
1057 if (mbstowcs(cmsis_dap_serial
, CMD_ARGV
[0], len
+ 1) == (size_t)-1) {
1058 free(cmsis_dap_serial
);
1059 cmsis_dap_serial
= NULL
;
1060 LOG_ERROR("unable to convert serial");
1063 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1069 static const struct command_registration cmsis_dap_subcommand_handlers
[] = {
1072 .handler
= &cmsis_dap_handle_info_command
,
1073 .mode
= COMMAND_EXEC
,
1075 .help
= "show cmsis-dap info",
1077 COMMAND_REGISTRATION_DONE
1080 static const struct command_registration cmsis_dap_command_handlers
[] = {
1082 .name
= "cmsis-dap",
1083 .mode
= COMMAND_ANY
,
1084 .help
= "perform CMSIS-DAP management",
1086 .chain
= cmsis_dap_subcommand_handlers
,
1089 .name
= "cmsis_dap_vid_pid",
1090 .handler
= &cmsis_dap_handle_vid_pid_command
,
1091 .mode
= COMMAND_CONFIG
,
1092 .help
= "the vendor ID and product ID of the CMSIS-DAP device",
1093 .usage
= "(vid pid)* ",
1096 .name
= "cmsis_dap_serial",
1097 .handler
= &cmsis_dap_handle_serial_command
,
1098 .mode
= COMMAND_CONFIG
,
1099 .help
= "set the serial number of the adapter",
1100 .usage
= "serial_string",
1102 COMMAND_REGISTRATION_DONE
1105 static const struct swd_driver cmsis_dap_swd_driver
= {
1106 .init
= cmsis_dap_swd_init
,
1107 .frequency
= cmsis_dap_swd_frequency
,
1108 .switch_seq
= cmsis_dap_swd_switch_seq
,
1109 .read_reg
= cmsis_dap_swd_read_reg
,
1110 .write_reg
= cmsis_dap_swd_write_reg
,
1111 .run
= cmsis_dap_swd_run_queue
,
1114 static const char * const cmsis_dap_transport
[] = { "swd", NULL
};
1116 struct jtag_interface cmsis_dap_interface
= {
1117 .name
= "cmsis-dap",
1118 .commands
= cmsis_dap_command_handlers
,
1119 .swd
= &cmsis_dap_swd_driver
,
1120 .transports
= cmsis_dap_transport
,
1122 .execute_queue
= cmsis_dap_execute_queue
,
1123 .speed
= cmsis_dap_speed
,
1124 .speed_div
= cmsis_dap_speed_div
,
1125 .khz
= cmsis_dap_khz
,
1126 .init
= cmsis_dap_init
,
1127 .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)