1 /***************************************************************************
2 * Copyright (C) 2018 by Mickaƫl Thomas *
5 * Copyright (C) 2016 by Maksym Hilliaka *
6 * oter@frozen-team.com *
8 * Copyright (C) 2016 by Phillip Pearson *
11 * Copyright (C) 2014 by Paul Fertser *
12 * fercerpav@gmail.com *
14 * Copyright (C) 2013 by mike brown *
15 * mike@theshedworks.org.uk *
17 * Copyright (C) 2013 by Spencer Oliver *
18 * spen@spen-soft.co.uk *
20 * This program is free software; you can redistribute it and/or modify *
21 * it under the terms of the GNU General Public License as published by *
22 * the Free Software Foundation; either version 2 of the License, or *
23 * (at your option) any later version. *
25 * This program is distributed in the hope that it will be useful, *
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
28 * GNU General Public License for more details. *
30 * You should have received a copy of the GNU General Public License *
31 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
32 ***************************************************************************/
38 #include <transport/transport.h>
40 #include <jtag/interface.h>
41 #include <jtag/commands.h>
44 #include "cmsis_dap.h"
46 static const struct cmsis_dap_backend
*const cmsis_dap_backends
[] = {
47 #if BUILD_CMSIS_DAP_USB == 1
48 &cmsis_dap_usb_backend
,
51 #if BUILD_CMSIS_DAP_HID == 1
52 &cmsis_dap_hid_backend
,
58 /* Known vid/pid pairs:
59 * VID 0xc251: Keil Software
60 * PID 0xf001: LPC-Link-II CMSIS_DAP
61 * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
62 * PID 0x2722: Keil ULINK2 CMSIS-DAP
63 * PID 0x2750: Keil ULINKplus CMSIS-DAP
65 * VID 0x0d28: mbed Software
66 * PID 0x0204: MBED CMSIS-DAP
70 /* vid = pid = 0 marks the end of the list */
71 static uint16_t cmsis_dap_vid
[MAX_USB_IDS
+ 1] = { 0 };
72 static uint16_t cmsis_dap_pid
[MAX_USB_IDS
+ 1] = { 0 };
73 static char *cmsis_dap_serial
;
74 static int cmsis_dap_backend
= -1;
77 #define USB_TIMEOUT 1000
79 /* CMSIS-DAP General Commands */
80 #define CMD_DAP_INFO 0x00
81 #define CMD_DAP_LED 0x01
82 #define CMD_DAP_CONNECT 0x02
83 #define CMD_DAP_DISCONNECT 0x03
84 #define CMD_DAP_WRITE_ABORT 0x08
85 #define CMD_DAP_DELAY 0x09
86 #define CMD_DAP_RESET_TARGET 0x0A
89 #define INFO_ID_VENDOR 0x01 /* string */
90 #define INFO_ID_PRODUCT 0x02 /* string */
91 #define INFO_ID_SERNUM 0x03 /* string */
92 #define INFO_ID_FW_VER 0x04 /* string */
93 #define INFO_ID_TD_VEND 0x05 /* string */
94 #define INFO_ID_TD_NAME 0x06 /* string */
95 #define INFO_ID_CAPS 0xf0 /* byte */
96 #define INFO_ID_PKT_CNT 0xfe /* byte */
97 #define INFO_ID_PKT_SZ 0xff /* short */
99 #define INFO_CAPS_SWD 0x01
100 #define INFO_CAPS_JTAG 0x02
103 #define LED_ID_CONNECT 0x00
104 #define LED_ID_RUN 0x01
110 #define CONNECT_DEFAULT 0x00
111 #define CONNECT_SWD 0x01
112 #define CONNECT_JTAG 0x02
114 /* CMSIS-DAP Common SWD/JTAG Commands */
115 #define CMD_DAP_DELAY 0x09
116 #define CMD_DAP_SWJ_PINS 0x10
117 #define CMD_DAP_SWJ_CLOCK 0x11
118 #define CMD_DAP_SWJ_SEQ 0x12
130 #define SWJ_PIN_TCK (1<<0)
131 #define SWJ_PIN_TMS (1<<1)
132 #define SWJ_PIN_TDI (1<<2)
133 #define SWJ_PIN_TDO (1<<3)
134 #define SWJ_PIN_TRST (1<<5)
135 #define SWJ_PIN_SRST (1<<7)
137 /* CMSIS-DAP SWD Commands */
138 #define CMD_DAP_SWD_CONFIGURE 0x13
139 #define CMD_DAP_SWD_SEQUENCE 0x1D
141 /* CMSIS-DAP JTAG Commands */
142 #define CMD_DAP_JTAG_SEQ 0x14
143 #define CMD_DAP_JTAG_CONFIGURE 0x15
144 #define CMD_DAP_JTAG_IDCODE 0x16
146 /* CMSIS-DAP JTAG sequence info masks */
147 /* Number of bits to clock through (0 means 64) */
148 #define DAP_JTAG_SEQ_TCK 0x3F
149 /* TMS will be set during the sequence if this bit is set */
150 #define DAP_JTAG_SEQ_TMS 0x40
151 /* TDO output will be captured if this bit is set */
152 #define DAP_JTAG_SEQ_TDO 0x80
155 /* CMSIS-DAP Transfer Commands */
156 #define CMD_DAP_TFER_CONFIGURE 0x04
157 #define CMD_DAP_TFER 0x05
158 #define CMD_DAP_TFER_BLOCK 0x06
159 #define CMD_DAP_TFER_ABORT 0x07
161 /* DAP Status Code */
163 #define DAP_ERROR 0xFF
165 /* CMSIS-DAP Vendor Commands
168 static const char * const info_caps_str
[] = {
173 struct pending_transfer_result
{
179 struct pending_request_block
{
180 struct pending_transfer_result
*transfers
;
184 struct pending_scan_result
{
185 /** Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer. */
187 /** Number of bits to read. */
189 /** Location to store the result */
191 /** Offset in the destination buffer */
192 unsigned buffer_offset
;
195 /* Up to MIN(packet_count, MAX_PENDING_REQUESTS) requests may be issued
196 * until the first response arrives */
197 #define MAX_PENDING_REQUESTS 3
199 /* Pending requests are organized as a FIFO - circular buffer */
200 /* Each block in FIFO can contain up to pending_queue_len transfers */
201 static int pending_queue_len
;
202 static struct pending_request_block pending_fifo
[MAX_PENDING_REQUESTS
];
203 static int pending_fifo_put_idx
, pending_fifo_get_idx
;
204 static int pending_fifo_block_count
;
206 /* pointers to buffers that will receive jtag scan results on the next flush */
207 #define MAX_PENDING_SCAN_RESULTS 256
208 static int pending_scan_result_count
;
209 static struct pending_scan_result pending_scan_results
[MAX_PENDING_SCAN_RESULTS
];
211 /* queued JTAG sequences that will be executed on the next flush */
212 #define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_size - 3)
213 static int queued_seq_count
;
214 static int queued_seq_buf_end
;
215 static int queued_seq_tdo_ptr
;
216 static uint8_t queued_seq_buf
[1024]; /* TODO: make dynamic / move into cmsis object */
218 static int queued_retval
;
220 static uint8_t output_pins
= SWJ_PIN_SRST
| SWJ_PIN_TRST
;
222 static struct cmsis_dap
*cmsis_dap_handle
;
225 static int cmsis_dap_quit(void);
227 static int cmsis_dap_open(void)
229 const struct cmsis_dap_backend
*backend
= NULL
;
231 struct cmsis_dap
*dap
= calloc(1, sizeof(struct cmsis_dap
));
233 LOG_ERROR("unable to allocate memory");
237 if (cmsis_dap_backend
>= 0) {
238 /* Use forced backend */
239 backend
= cmsis_dap_backends
[cmsis_dap_backend
];
240 if (backend
->open(dap
, cmsis_dap_vid
, cmsis_dap_pid
, cmsis_dap_serial
) != ERROR_OK
)
243 /* Try all backends */
244 for (unsigned int i
= 0; i
< ARRAY_SIZE(cmsis_dap_backends
); i
++) {
245 backend
= cmsis_dap_backends
[i
];
246 if (backend
->open(dap
, cmsis_dap_vid
, cmsis_dap_pid
, cmsis_dap_serial
) == ERROR_OK
)
253 if (backend
== NULL
) {
254 LOG_ERROR("unable to find a matching CMSIS-DAP device");
259 dap
->backend
= backend
;
261 cmsis_dap_handle
= dap
;
266 static void cmsis_dap_close(struct cmsis_dap
*dap
)
269 dap
->backend
->close(dap
);
273 free(cmsis_dap_handle
->packet_buffer
);
274 free(cmsis_dap_handle
);
275 cmsis_dap_handle
= NULL
;
276 free(cmsis_dap_serial
);
277 cmsis_dap_serial
= NULL
;
279 for (int i
= 0; i
< MAX_PENDING_REQUESTS
; i
++) {
280 free(pending_fifo
[i
].transfers
);
281 pending_fifo
[i
].transfers
= NULL
;
285 static void cmsis_dap_flush_read(struct cmsis_dap
*dap
)
288 /* Some CMSIS-DAP adapters keep buffered packets over
289 * USB close/open so we need to flush up to 64 old packets
290 * to be sure all buffers are empty */
291 for (i
= 0; i
< 64; i
++) {
292 int retval
= dap
->backend
->read(dap
, 10);
293 if (retval
== ERROR_TIMEOUT_REACHED
)
297 LOG_DEBUG("Flushed %u packets", i
);
300 /* Send a message and receive the reply */
301 static int cmsis_dap_xfer(struct cmsis_dap
*dap
, int txlen
)
303 if (pending_fifo_block_count
) {
304 LOG_ERROR("pending %d blocks, flushing", pending_fifo_block_count
);
305 while (pending_fifo_block_count
) {
306 dap
->backend
->read(dap
, 10);
307 pending_fifo_block_count
--;
309 pending_fifo_put_idx
= 0;
310 pending_fifo_get_idx
= 0;
313 uint8_t current_cmd
= cmsis_dap_handle
->command
[0];
314 int retval
= dap
->backend
->write(dap
, txlen
, USB_TIMEOUT
);
319 retval
= dap
->backend
->read(dap
, USB_TIMEOUT
);
323 uint8_t *resp
= cmsis_dap_handle
->response
;
324 if (resp
[0] == DAP_ERROR
) {
325 LOG_ERROR("CMSIS-DAP command 0x%" PRIx8
" not implemented", current_cmd
);
326 return ERROR_NOT_IMPLEMENTED
;
329 if (resp
[0] != current_cmd
) {
330 LOG_ERROR("CMSIS-DAP command mismatch. Sent 0x%" PRIx8
331 " received 0x%" PRIx8
, current_cmd
, resp
[0]);
333 cmsis_dap_flush_read(dap
);
340 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins
, uint8_t mask
, uint32_t delay
, uint8_t *input
)
343 uint8_t *command
= cmsis_dap_handle
->command
;
345 command
[0] = CMD_DAP_SWJ_PINS
;
348 h_u32_to_le(&command
[3], delay
);
350 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 7);
352 if (retval
!= ERROR_OK
) {
353 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
354 return ERROR_JTAG_DEVICE_ERROR
;
358 *input
= cmsis_dap_handle
->response
[1];
363 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock
)
366 uint8_t *command
= cmsis_dap_handle
->command
;
368 /* set clock in Hz */
371 command
[0] = CMD_DAP_SWJ_CLOCK
;
372 h_u32_to_le(&command
[1], swj_clock
);
374 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 5);
376 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
377 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
378 return ERROR_JTAG_DEVICE_ERROR
;
384 /* clock a sequence of bits out on TMS, to change JTAG states */
385 static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len
, const uint8_t *sequence
)
388 uint8_t *command
= cmsis_dap_handle
->command
;
390 #ifdef CMSIS_DAP_JTAG_DEBUG
391 LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len
);
392 for (int i
= 0; i
< DIV_ROUND_UP(s_len
, 8); ++i
)
393 printf("%02X ", sequence
[i
]);
398 command
[0] = CMD_DAP_SWJ_SEQ
;
400 bit_copy(&command
[2], 0, sequence
, 0, s_len
);
402 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 2 + DIV_ROUND_UP(s_len
, 8));
403 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
)
409 static int cmsis_dap_cmd_DAP_Info(uint8_t info
, uint8_t **data
)
412 uint8_t *command
= cmsis_dap_handle
->command
;
414 command
[0] = CMD_DAP_INFO
;
417 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 2);
419 if (retval
!= ERROR_OK
) {
420 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
421 return ERROR_JTAG_DEVICE_ERROR
;
424 *data
= &cmsis_dap_handle
->response
[1];
429 static int cmsis_dap_cmd_DAP_LED(uint8_t led
, uint8_t state
)
432 uint8_t *command
= cmsis_dap_handle
->command
;
434 command
[0] = CMD_DAP_LED
;
438 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 3);
440 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
441 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
442 return ERROR_JTAG_DEVICE_ERROR
;
448 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode
)
451 uint8_t *command
= cmsis_dap_handle
->command
;
453 command
[0] = CMD_DAP_CONNECT
;
456 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 2);
458 if (retval
!= ERROR_OK
) {
459 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
460 return ERROR_JTAG_DEVICE_ERROR
;
463 if (cmsis_dap_handle
->response
[1] != mode
) {
464 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode
);
465 return ERROR_JTAG_DEVICE_ERROR
;
471 static int cmsis_dap_cmd_DAP_Disconnect(void)
474 uint8_t *command
= cmsis_dap_handle
->command
;
476 command
[0] = CMD_DAP_DISCONNECT
;
478 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 1);
480 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
481 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
482 return ERROR_JTAG_DEVICE_ERROR
;
488 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle
, uint16_t retry_count
, uint16_t match_retry
)
491 uint8_t *command
= cmsis_dap_handle
->command
;
493 command
[0] = CMD_DAP_TFER_CONFIGURE
;
495 h_u16_to_le(&command
[2], retry_count
);
496 h_u16_to_le(&command
[4], match_retry
);
498 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 6);
500 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
501 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
502 return ERROR_JTAG_DEVICE_ERROR
;
508 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg
)
511 uint8_t *command
= cmsis_dap_handle
->command
;
513 command
[0] = CMD_DAP_SWD_CONFIGURE
;
516 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 2);
518 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
519 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
520 return ERROR_JTAG_DEVICE_ERROR
;
527 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us
)
530 uint8_t *command
= cmsis_dap_handle
->command
;
532 command
[0] = CMD_DAP_DELAY
;
533 h_u16_to_le(&command
[1], delay_us
);
535 retval
= cmsis_dap_xfer(cmsis_dap_handle
, 3);
537 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
538 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
539 return ERROR_JTAG_DEVICE_ERROR
;
546 static int cmsis_dap_metacmd_targetsel(uint32_t instance_id
)
549 uint8_t *command
= cmsis_dap_handle
->command
;
550 const uint32_t SEQ_RD
= 0x80, SEQ_WR
= 0x00;
552 /* SWD multi-drop requires a transfer ala CMD_DAP_TFER,
553 but with no expectation of an SWD ACK response. In
554 CMSIS-DAP v1.20 and v2.00, CMD_DAP_SWD_SEQUENCE was
555 added to allow this special sequence to be generated.
556 The purpose of this operation is to select the target
557 corresponding to the instance_id that is written */
560 command
[idx
++] = CMD_DAP_SWD_SEQUENCE
;
561 command
[idx
++] = 3; /* sequence count */
563 /* sequence 0: packet request for TARGETSEL */
564 command
[idx
++] = SEQ_WR
| 8;
565 command
[idx
++] = SWD_CMD_START
| swd_cmd(false, false, DP_TARGETSEL
) | SWD_CMD_STOP
| SWD_CMD_PARK
;
567 /* sequence 1: read Trn ACK Trn, no expectation for target to ACK */
568 command
[idx
++] = SEQ_RD
| 5;
570 /* sequence 2: WDATA plus parity */
571 command
[idx
++] = SEQ_WR
| (32 + 1);
572 h_u32_to_le(command
+ idx
, instance_id
);
574 command
[idx
++] = parity_u32(instance_id
);
576 retval
= cmsis_dap_xfer(cmsis_dap_handle
, idx
);
578 if (retval
!= ERROR_OK
|| cmsis_dap_handle
->response
[1] != DAP_OK
) {
579 LOG_ERROR("CMSIS-DAP command SWD_Sequence failed.");
580 return ERROR_JTAG_DEVICE_ERROR
;
585 static void cmsis_dap_swd_write_from_queue(struct cmsis_dap
*dap
)
587 uint8_t *command
= cmsis_dap_handle
->command
;
588 struct pending_request_block
*block
= &pending_fifo
[pending_fifo_put_idx
];
590 LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %d", block
->transfer_count
, pending_fifo_put_idx
);
592 if (queued_retval
!= ERROR_OK
) {
593 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
597 if (block
->transfer_count
== 0)
600 command
[0] = CMD_DAP_TFER
;
601 command
[1] = 0x00; /* DAP Index */
602 command
[2] = block
->transfer_count
;
605 for (int i
= 0; i
< block
->transfer_count
; i
++) {
606 struct pending_transfer_result
*transfer
= &(block
->transfers
[i
]);
607 uint8_t cmd
= transfer
->cmd
;
608 uint32_t data
= transfer
->data
;
610 LOG_DEBUG_IO("%s %s reg %x %"PRIx32
,
611 cmd
& SWD_CMD_APnDP
? "AP" : "DP",
612 cmd
& SWD_CMD_RnW
? "read" : "write",
613 (cmd
& SWD_CMD_A32
) >> 1, data
);
615 /* When proper WAIT handling is implemented in the
616 * common SWD framework, this kludge can be
617 * removed. However, this might lead to minor
618 * performance degradation as the adapter wouldn't be
619 * able to automatically retry anything (because ARM
620 * has forgotten to implement sticky error flags
621 * clearing). See also comments regarding
622 * cmsis_dap_cmd_DAP_TFER_Configure() and
623 * cmsis_dap_cmd_DAP_SWD_Configure() in
626 if (!(cmd
& SWD_CMD_RnW
) &&
627 !(cmd
& SWD_CMD_APnDP
) &&
628 (cmd
& SWD_CMD_A32
) >> 1 == DP_CTRL_STAT
&&
629 (data
& CORUNDETECT
)) {
630 LOG_DEBUG("refusing to enable sticky overrun detection");
631 data
&= ~CORUNDETECT
;
634 command
[idx
++] = (cmd
>> 1) & 0x0f;
635 if (!(cmd
& SWD_CMD_RnW
)) {
636 h_u32_to_le(&command
[idx
], data
);
641 int retval
= dap
->backend
->write(dap
, idx
, USB_TIMEOUT
);
644 queued_retval
= retval
;
647 queued_retval
= ERROR_OK
;
650 pending_fifo_put_idx
= (pending_fifo_put_idx
+ 1) % dap
->packet_count
;
651 pending_fifo_block_count
++;
652 if (pending_fifo_block_count
> dap
->packet_count
)
653 LOG_ERROR("too much pending writes %d", pending_fifo_block_count
);
658 block
->transfer_count
= 0;
661 static void cmsis_dap_swd_read_process(struct cmsis_dap
*dap
, int timeout_ms
)
663 struct pending_request_block
*block
= &pending_fifo
[pending_fifo_get_idx
];
665 if (pending_fifo_block_count
== 0)
666 LOG_ERROR("no pending write");
669 int retval
= dap
->backend
->read(dap
, timeout_ms
);
670 if (retval
== ERROR_TIMEOUT_REACHED
&& timeout_ms
< USB_TIMEOUT
)
674 LOG_DEBUG("error reading data");
675 queued_retval
= ERROR_FAIL
;
679 uint8_t *resp
= dap
->response
;
680 if (resp
[0] != CMD_DAP_TFER
) {
681 LOG_ERROR("CMSIS-DAP command mismatch. Expected 0x%" PRIx8
682 " received 0x%" PRIx8
, CMD_DAP_TFER
, resp
[0]);
683 queued_retval
= ERROR_FAIL
;
687 uint8_t transfer_count
= resp
[1];
688 uint8_t ack
= resp
[2] & 0x07;
689 if (resp
[2] & 0x08) {
690 LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count
);
691 queued_retval
= ERROR_FAIL
;
694 if (ack
!= SWD_ACK_OK
) {
695 LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count
,
696 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
697 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
698 /* TODO: use results of transfers completed before the error occurred? */
702 if (block
->transfer_count
!= transfer_count
)
703 LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
704 block
->transfer_count
, transfer_count
);
706 LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %d",
707 transfer_count
, pending_fifo_get_idx
);
709 for (int i
= 0; i
< transfer_count
; i
++) {
710 struct pending_transfer_result
*transfer
= &(block
->transfers
[i
]);
711 if (transfer
->cmd
& SWD_CMD_RnW
) {
712 static uint32_t last_read
;
713 uint32_t data
= le_to_h_u32(&resp
[idx
]);
717 LOG_DEBUG_IO("Read result: %"PRIx32
, data
);
719 /* Imitate posted AP reads */
720 if ((transfer
->cmd
& SWD_CMD_APnDP
) ||
721 ((transfer
->cmd
& SWD_CMD_A32
) >> 1 == DP_RDBUFF
)) {
726 if (transfer
->buffer
)
727 *(uint32_t *)(transfer
->buffer
) = tmp
;
732 block
->transfer_count
= 0;
733 pending_fifo_get_idx
= (pending_fifo_get_idx
+ 1) % dap
->packet_count
;
734 pending_fifo_block_count
--;
737 static int cmsis_dap_swd_run_queue(void)
739 if (pending_fifo_block_count
)
740 cmsis_dap_swd_read_process(cmsis_dap_handle
, 0);
742 cmsis_dap_swd_write_from_queue(cmsis_dap_handle
);
744 while (pending_fifo_block_count
)
745 cmsis_dap_swd_read_process(cmsis_dap_handle
, USB_TIMEOUT
);
747 pending_fifo_put_idx
= 0;
748 pending_fifo_get_idx
= 0;
750 int retval
= queued_retval
;
751 queued_retval
= ERROR_OK
;
756 static void cmsis_dap_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
)
758 bool targetsel_cmd
= swd_cmd(false, false, DP_TARGETSEL
) == cmd
;
760 if (pending_fifo
[pending_fifo_put_idx
].transfer_count
== pending_queue_len
762 if (pending_fifo_block_count
)
763 cmsis_dap_swd_read_process(cmsis_dap_handle
, 0);
765 /* Not enough room in the queue. Run the queue. */
766 cmsis_dap_swd_write_from_queue(cmsis_dap_handle
);
768 if (pending_fifo_block_count
>= cmsis_dap_handle
->packet_count
)
769 cmsis_dap_swd_read_process(cmsis_dap_handle
, USB_TIMEOUT
);
772 if (queued_retval
!= ERROR_OK
)
776 cmsis_dap_metacmd_targetsel(data
);
780 struct pending_request_block
*block
= &pending_fifo
[pending_fifo_put_idx
];
781 struct pending_transfer_result
*transfer
= &(block
->transfers
[block
->transfer_count
]);
782 transfer
->data
= data
;
784 if (cmd
& SWD_CMD_RnW
) {
785 /* Queue a read transaction */
786 transfer
->buffer
= dst
;
788 block
->transfer_count
++;
791 static void cmsis_dap_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
793 assert(!(cmd
& SWD_CMD_RnW
));
794 cmsis_dap_swd_queue_cmd(cmd
, NULL
, value
);
797 static void cmsis_dap_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
799 assert(cmd
& SWD_CMD_RnW
);
800 cmsis_dap_swd_queue_cmd(cmd
, value
, 0);
803 static int cmsis_dap_get_serial_info(void)
807 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_SERNUM
, &data
);
808 if (retval
!= ERROR_OK
)
811 if (data
[0]) /* strlen */
812 LOG_INFO("CMSIS-DAP: Serial# = %s", &data
[1]);
817 static int cmsis_dap_get_version_info(void)
821 /* INFO_ID_FW_VER - string */
822 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER
, &data
);
823 if (retval
!= ERROR_OK
)
826 if (data
[0]) /* strlen */
827 LOG_INFO("CMSIS-DAP: FW Version = %s", &data
[1]);
832 static int cmsis_dap_get_caps_info(void)
836 /* INFO_ID_CAPS - byte */
837 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS
, &data
);
838 if (retval
!= ERROR_OK
)
842 uint8_t caps
= data
[1];
844 cmsis_dap_handle
->caps
= caps
;
846 if (caps
& INFO_CAPS_SWD
)
847 LOG_INFO("CMSIS-DAP: %s", info_caps_str
[0]);
848 if (caps
& INFO_CAPS_JTAG
)
849 LOG_INFO("CMSIS-DAP: %s", info_caps_str
[1]);
855 static int cmsis_dap_get_status(void)
859 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d
);
861 if (retval
== ERROR_OK
) {
862 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
863 (d
& SWJ_PIN_TCK
) ? 1 : 0,
864 (d
& SWJ_PIN_TMS
) ? 1 : 0,
865 (d
& SWJ_PIN_TDI
) ? 1 : 0,
866 (d
& SWJ_PIN_TDO
) ? 1 : 0,
867 (d
& SWJ_PIN_TRST
) ? 1 : 0,
868 (d
& SWJ_PIN_SRST
) ? 1 : 0);
874 static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq
)
880 if ((output_pins
& (SWJ_PIN_SRST
| SWJ_PIN_TRST
)) == (SWJ_PIN_SRST
| SWJ_PIN_TRST
)) {
881 /* Following workaround deasserts reset on most adapters.
882 * Do not reconnect if a reset line is active!
883 * Reconnecting would break connecting under reset. */
885 /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
886 cmsis_dap_cmd_DAP_Disconnect();
888 /* When we are reconnecting, DAP_Connect needs to be rerun, at
889 * least on Keil ULINK-ME */
890 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_SWD
);
891 if (retval
!= ERROR_OK
)
897 LOG_DEBUG_IO("SWD line reset");
898 s
= swd_seq_line_reset
;
899 s_len
= swd_seq_line_reset_len
;
902 LOG_DEBUG("JTAG-to-SWD");
903 s
= swd_seq_jtag_to_swd
;
904 s_len
= swd_seq_jtag_to_swd_len
;
906 case JTAG_TO_DORMANT
:
907 LOG_DEBUG("JTAG-to-DORMANT");
908 s
= swd_seq_jtag_to_dormant
;
909 s_len
= swd_seq_jtag_to_dormant_len
;
912 LOG_DEBUG("SWD-to-JTAG");
913 s
= swd_seq_swd_to_jtag
;
914 s_len
= swd_seq_swd_to_jtag_len
;
917 LOG_DEBUG("SWD-to-DORMANT");
918 s
= swd_seq_swd_to_dormant
;
919 s_len
= swd_seq_swd_to_dormant_len
;
922 LOG_DEBUG("DORMANT-to-SWD");
923 s
= swd_seq_dormant_to_swd
;
924 s_len
= swd_seq_dormant_to_swd_len
;
927 LOG_ERROR("Sequence %d not supported", seq
);
931 retval
= cmsis_dap_cmd_DAP_SWJ_Sequence(s_len
, s
);
932 if (retval
!= ERROR_OK
)
935 /* Atmel EDBG needs renew clock setting after SWJ_Sequence
936 * otherwise default frequency is used */
937 return cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
940 static int cmsis_dap_swd_open(void)
944 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_SWD
)) {
945 LOG_ERROR("CMSIS-DAP: SWD not supported");
946 return ERROR_JTAG_DEVICE_ERROR
;
949 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_SWD
);
950 if (retval
!= ERROR_OK
)
953 /* Add more setup here.??... */
955 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
959 static int cmsis_dap_init(void)
964 retval
= cmsis_dap_open();
965 if (retval
!= ERROR_OK
)
968 cmsis_dap_flush_read(cmsis_dap_handle
);
970 retval
= cmsis_dap_get_caps_info();
971 if (retval
!= ERROR_OK
)
974 retval
= cmsis_dap_get_version_info();
975 if (retval
!= ERROR_OK
)
978 retval
= cmsis_dap_get_serial_info();
979 if (retval
!= ERROR_OK
)
983 retval
= cmsis_dap_swd_open();
984 if (retval
!= ERROR_OK
)
987 /* Connect in JTAG mode */
988 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_JTAG
)) {
989 LOG_ERROR("CMSIS-DAP: JTAG not supported");
990 return ERROR_JTAG_DEVICE_ERROR
;
993 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG
);
994 if (retval
!= ERROR_OK
)
997 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
1000 /* Be conservative and suppress submitting multiple HID requests
1001 * until we get packet count info from the adaptor */
1002 cmsis_dap_handle
->packet_count
= 1;
1003 pending_queue_len
= 12;
1005 /* INFO_ID_PKT_SZ - short */
1006 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ
, &data
);
1007 if (retval
!= ERROR_OK
)
1010 if (data
[0] == 2) { /* short */
1011 uint16_t pkt_sz
= data
[1] + (data
[2] << 8);
1012 if (pkt_sz
!= cmsis_dap_handle
->packet_size
) {
1014 /* 4 bytes of command header + 5 bytes per register
1015 * write. For bulk read sequences just 4 bytes are
1016 * needed per transfer, so this is suboptimal. */
1017 pending_queue_len
= (pkt_sz
- 4) / 5;
1019 free(cmsis_dap_handle
->packet_buffer
);
1020 retval
= cmsis_dap_handle
->backend
->packet_buffer_alloc(cmsis_dap_handle
, pkt_sz
);
1021 if (retval
!= ERROR_OK
)
1024 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRIu16
, pkt_sz
);
1028 /* INFO_ID_PKT_CNT - byte */
1029 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT
, &data
);
1030 if (retval
!= ERROR_OK
)
1033 if (data
[0] == 1) { /* byte */
1034 int pkt_cnt
= data
[1];
1036 cmsis_dap_handle
->packet_count
= MIN(MAX_PENDING_REQUESTS
, pkt_cnt
);
1038 LOG_DEBUG("CMSIS-DAP: Packet Count = %d", pkt_cnt
);
1041 LOG_DEBUG("Allocating FIFO for %d pending packets", cmsis_dap_handle
->packet_count
);
1042 for (int i
= 0; i
< cmsis_dap_handle
->packet_count
; i
++) {
1043 pending_fifo
[i
].transfers
= malloc(pending_queue_len
* sizeof(struct pending_transfer_result
));
1044 if (!pending_fifo
[i
].transfers
) {
1045 LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
1046 retval
= ERROR_FAIL
;
1051 /* Intentionally not checked for error, just logs an info message
1052 * not vital for further debugging */
1053 (void)cmsis_dap_get_status();
1055 /* Now try to connect to the target
1056 * TODO: This is all SWD only @ present */
1057 retval
= cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
1058 if (retval
!= ERROR_OK
)
1061 /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
1062 * up to 64 times. This must be changed to 0 if sticky
1063 * overrun detection is enabled. */
1064 retval
= cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
1065 if (retval
!= ERROR_OK
)
1069 /* Data Phase (bit 2) must be set to 1 if sticky overrun
1070 * detection is enabled */
1071 retval
= cmsis_dap_cmd_DAP_SWD_Configure(0); /* 1 TRN, no Data Phase */
1072 if (retval
!= ERROR_OK
)
1076 /* Intentionally not checked for error, debugging will work
1078 (void)cmsis_dap_cmd_DAP_LED(LED_ID_CONNECT
, LED_ON
);
1079 (void)cmsis_dap_cmd_DAP_LED(LED_ID_RUN
, LED_ON
);
1081 /* support connecting with srst asserted */
1082 enum reset_types jtag_reset_config
= jtag_get_reset_config();
1084 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
1085 if (jtag_reset_config
& RESET_SRST_NO_GATING
) {
1086 retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, SWJ_PIN_SRST
, 0, NULL
);
1087 if (retval
!= ERROR_OK
)
1089 LOG_INFO("Connecting under reset");
1092 LOG_INFO("CMSIS-DAP: Interface ready");
1100 static int cmsis_dap_swd_init(void)
1106 static int cmsis_dap_quit(void)
1108 cmsis_dap_cmd_DAP_Disconnect();
1110 cmsis_dap_cmd_DAP_LED(LED_ID_RUN
, LED_OFF
);
1111 cmsis_dap_cmd_DAP_LED(LED_ID_CONNECT
, LED_OFF
);
1113 cmsis_dap_close(cmsis_dap_handle
);
1118 static int cmsis_dap_reset(int trst
, int srst
)
1120 /* Set both TRST and SRST even if they're not enabled as
1121 * there's no way to tristate them */
1125 output_pins
|= SWJ_PIN_SRST
;
1127 output_pins
|= SWJ_PIN_TRST
;
1129 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(output_pins
,
1130 SWJ_PIN_TRST
| SWJ_PIN_SRST
, 0, NULL
);
1131 if (retval
!= ERROR_OK
)
1132 LOG_ERROR("CMSIS-DAP: Interface reset failed");
1136 static void cmsis_dap_execute_sleep(struct jtag_command
*cmd
)
1139 int retval
= cmsis_dap_cmd_DAP_Delay(cmd
->cmd
.sleep
->us
);
1140 if (retval
!= ERROR_OK
)
1142 jtag_sleep(cmd
->cmd
.sleep
->us
);
1145 /* Set TMS high for five TCK clocks, to move the TAP to the Test-Logic-Reset state */
1146 static int cmsis_dap_execute_tlr_reset(struct jtag_command
*cmd
)
1148 LOG_INFO("cmsis-dap JTAG TLR_RESET");
1150 int ret
= cmsis_dap_cmd_DAP_SWJ_Sequence(8, &seq
);
1151 if (ret
== ERROR_OK
)
1152 tap_set_state(TAP_RESET
);
1156 /* Set new end state */
1157 static void cmsis_dap_end_state(tap_state_t state
)
1159 if (tap_is_state_stable(state
))
1160 tap_set_end_state(state
);
1162 LOG_ERROR("BUG: %i is not a valid end state", state
);
1167 #ifdef SPRINT_BINARY
1168 static void sprint_binary(char *s
, const uint8_t *buf
, int offset
, int len
)
1174 buf = { 0x18 } len=5 should result in: 11000
1175 buf = { 0xff 0x18 } len=13 should result in: 11111111 11000
1176 buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
1177 i=3 there means i/8 = 0 so c = 0xFF, and
1179 for (int i
= offset
; i
< offset
+ len
; ++i
) {
1180 uint8_t c
= buf
[i
/ 8], mask
= 1 << (i
% 8);
1181 if ((i
!= offset
) && !(i
% 8))
1183 *s
++ = (c
& mask
) ? '1' : '0';
1189 #ifdef CMSIS_DAP_JTAG_DEBUG
1190 static void debug_parse_cmsis_buf(const uint8_t *cmd
, int cmdlen
)
1192 /* cmd is a usb packet to go to the cmsis-dap interface */
1193 printf("cmsis-dap buffer (%d b): ", cmdlen
);
1194 for (int i
= 0; i
< cmdlen
; ++i
)
1195 printf(" %02x", cmd
[i
]);
1198 case CMD_DAP_JTAG_SEQ
: {
1199 printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd
[1], cmd
[2]);
1201 * #2 = number of sequences
1202 * #3 = sequence info 1
1203 * #4...4+n_bytes-1 = sequence 1
1204 * #4+n_bytes = sequence info 2
1205 * #5+n_bytes = sequence 2 (single bit)
1208 for (int seq
= 0; seq
< cmd
[2]; ++seq
) {
1209 uint8_t info
= cmd
[pos
++];
1210 int len
= info
& DAP_JTAG_SEQ_TCK
;
1213 printf(" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
1214 seq
, pos
, info
, len
, info
& DAP_JTAG_SEQ_TMS
, info
& DAP_JTAG_SEQ_TDO
);
1215 for (int i
= 0; i
< DIV_ROUND_UP(len
, 8); ++i
)
1216 printf(" %02x", cmd
[pos
+i
]);
1217 pos
+= DIV_ROUND_UP(len
, 8);
1220 if (pos
!= cmdlen
) {
1221 printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos
, cmdlen
);
1228 LOG_DEBUG("unknown cmsis-dap command %02x", cmd
[1]);
1234 static void cmsis_dap_flush(void)
1236 if (!queued_seq_count
)
1239 LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
1240 queued_seq_count
, queued_seq_buf_end
, pending_scan_result_count
);
1242 /* prepare CMSIS-DAP packet */
1243 uint8_t *command
= cmsis_dap_handle
->command
;
1244 command
[0] = CMD_DAP_JTAG_SEQ
;
1245 command
[1] = queued_seq_count
;
1246 memcpy(&command
[2], queued_seq_buf
, queued_seq_buf_end
);
1248 #ifdef CMSIS_DAP_JTAG_DEBUG
1249 debug_parse_cmsis_buf(command
, queued_seq_buf_end
+ 2);
1252 /* send command to USB device */
1253 int retval
= cmsis_dap_xfer(cmsis_dap_handle
, queued_seq_buf_end
+ 2);
1255 uint8_t *resp
= cmsis_dap_handle
->response
;
1256 if (retval
!= ERROR_OK
|| resp
[1] != DAP_OK
) {
1257 LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
1261 #ifdef CMSIS_DAP_JTAG_DEBUG
1262 LOG_DEBUG_IO("USB response buf:");
1263 for (int c
= 0; c
< queued_seq_buf_end
+ 3; ++c
)
1264 printf("%02X ", resp
[c
]);
1268 /* copy scan results into client buffers */
1269 for (int i
= 0; i
< pending_scan_result_count
; ++i
) {
1270 struct pending_scan_result
*scan
= &pending_scan_results
[i
];
1271 LOG_DEBUG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
1272 i
, pending_scan_result_count
, scan
->length
, scan
->first
+ 2, scan
->buffer_offset
);
1273 #ifdef CMSIS_DAP_JTAG_DEBUG
1274 for (uint32_t b
= 0; b
< DIV_ROUND_UP(scan
->length
, 8); ++b
)
1275 printf("%02X ", resp
[2+scan
->first
+b
]);
1278 bit_copy(scan
->buffer
, scan
->buffer_offset
, &resp
[2 + scan
->first
], 0, scan
->length
);
1282 queued_seq_count
= 0;
1283 queued_seq_buf_end
= 0;
1284 queued_seq_tdo_ptr
= 0;
1285 pending_scan_result_count
= 0;
1288 /* queue a sequence of bits to clock out TDI / in TDO, executing if the buffer is full.
1290 * sequence=NULL means clock out zeros on TDI
1291 * tdo_buffer=NULL means don't capture TDO
1293 static void cmsis_dap_add_jtag_sequence(int s_len
, const uint8_t *sequence
, int s_offset
,
1294 bool tms
, uint8_t *tdo_buffer
, int tdo_buffer_offset
)
1296 LOG_DEBUG_IO("[at %d] %d bits, tms %s, seq offset %d, tdo buf %p, tdo offset %d",
1298 s_len
, tms
? "HIGH" : "LOW", s_offset
, tdo_buffer
, tdo_buffer_offset
);
1304 LOG_DEBUG_IO("START JTAG SEQ SPLIT");
1305 for (int offset
= 0; offset
< s_len
; offset
+= 64) {
1306 int len
= s_len
- offset
;
1309 LOG_DEBUG_IO("Splitting long jtag sequence: %d-bit chunk starting at offset %d", len
, offset
);
1310 cmsis_dap_add_jtag_sequence(
1316 tdo_buffer
== NULL
? 0 : (tdo_buffer_offset
+ offset
)
1319 LOG_DEBUG_IO("END JTAG SEQ SPLIT");
1323 int cmd_len
= 1 + DIV_ROUND_UP(s_len
, 8);
1324 if (queued_seq_count
>= 255 || queued_seq_buf_end
+ cmd_len
> QUEUED_SEQ_BUF_LEN
)
1325 /* empty out the buffer */
1331 queued_seq_buf
[queued_seq_buf_end
] =
1332 (tms
? DAP_JTAG_SEQ_TMS
: 0) |
1333 (tdo_buffer
!= NULL
? DAP_JTAG_SEQ_TDO
: 0) |
1334 (s_len
== 64 ? 0 : s_len
);
1336 if (sequence
!= NULL
)
1337 bit_copy(&queued_seq_buf
[queued_seq_buf_end
+ 1], 0, sequence
, s_offset
, s_len
);
1339 memset(&queued_seq_buf
[queued_seq_buf_end
+ 1], 0, DIV_ROUND_UP(s_len
, 8));
1341 queued_seq_buf_end
+= cmd_len
;
1343 if (tdo_buffer
!= NULL
) {
1344 struct pending_scan_result
*scan
= &pending_scan_results
[pending_scan_result_count
++];
1345 scan
->first
= queued_seq_tdo_ptr
;
1346 queued_seq_tdo_ptr
+= DIV_ROUND_UP(s_len
, 8);
1347 scan
->length
= s_len
;
1348 scan
->buffer
= tdo_buffer
;
1349 scan
->buffer_offset
= tdo_buffer_offset
;
1353 /* queue a sequence of bits to clock out TMS, executing if the buffer is full */
1354 static void cmsis_dap_add_tms_sequence(const uint8_t *sequence
, int s_len
)
1356 LOG_DEBUG_IO("%d bits: %02X", s_len
, *sequence
);
1357 /* we use a series of CMD_DAP_JTAG_SEQ commands to toggle TMS,
1358 because even though it seems ridiculously inefficient, it
1359 allows us to combine TMS and scan sequences into the same
1361 /* TODO: combine runs of the same tms value */
1362 for (int i
= 0; i
< s_len
; ++i
) {
1363 bool bit
= (sequence
[i
/ 8] & (1 << (i
% 8))) != 0;
1364 cmsis_dap_add_jtag_sequence(1, NULL
, 0, bit
, NULL
, 0);
1368 /* Move to the end state by queuing a sequence to clock into TMS */
1369 static void cmsis_dap_state_move(void)
1372 uint8_t tms_scan_bits
;
1374 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1375 tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1377 LOG_DEBUG_IO("state move from %s to %s: %d clocks, %02X on tms",
1378 tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()),
1379 tms_scan_bits
, tms_scan
);
1380 cmsis_dap_add_tms_sequence(&tms_scan
, tms_scan_bits
);
1382 tap_set_state(tap_get_end_state());
1386 /* Execute a JTAG scan operation by queueing TMS and TDI/TDO sequences */
1387 static void cmsis_dap_execute_scan(struct jtag_command
*cmd
)
1389 LOG_DEBUG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN",
1390 jtag_scan_type(cmd
->cmd
.scan
));
1392 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
1393 while (cmd
->cmd
.scan
->num_fields
> 0
1394 && cmd
->cmd
.scan
->fields
[cmd
->cmd
.scan
->num_fields
- 1].num_bits
== 0) {
1395 cmd
->cmd
.scan
->num_fields
--;
1396 LOG_DEBUG("discarding trailing empty field");
1399 if (cmd
->cmd
.scan
->num_fields
== 0) {
1400 LOG_DEBUG("empty scan, doing nothing");
1404 if (cmd
->cmd
.scan
->ir_scan
) {
1405 if (tap_get_state() != TAP_IRSHIFT
) {
1406 cmsis_dap_end_state(TAP_IRSHIFT
);
1407 cmsis_dap_state_move();
1410 if (tap_get_state() != TAP_DRSHIFT
) {
1411 cmsis_dap_end_state(TAP_DRSHIFT
);
1412 cmsis_dap_state_move();
1416 cmsis_dap_end_state(cmd
->cmd
.scan
->end_state
);
1418 struct scan_field
*field
= cmd
->cmd
.scan
->fields
;
1419 unsigned scan_size
= 0;
1421 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++, field
++) {
1422 scan_size
+= field
->num_bits
;
1423 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
1424 field
->in_value
? "in" : "",
1425 field
->out_value
? "out" : "",
1427 cmd
->cmd
.scan
->num_fields
,
1430 if (i
== cmd
->cmd
.scan
->num_fields
- 1 && tap_get_state() != tap_get_end_state()) {
1431 LOG_DEBUG_IO("Last field and have to move out of SHIFT state");
1432 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
1433 * movement. This last field can't have length zero, it was checked above. */
1434 cmsis_dap_add_jtag_sequence(
1435 field
->num_bits
- 1, /* number of bits to clock */
1436 field
->out_value
, /* output sequence */
1437 0, /* output offset */
1438 false, /* TMS low */
1442 /* Clock the last bit out, with TMS high */
1443 uint8_t last_bit
= 0;
1444 if (field
->out_value
)
1445 bit_copy(&last_bit
, 0, field
->out_value
, field
->num_bits
- 1, 1);
1446 cmsis_dap_add_jtag_sequence(
1452 field
->num_bits
- 1);
1453 tap_set_state(tap_state_transition(tap_get_state(), 1));
1455 /* Now clock one more cycle, with TMS low, to get us into a PAUSE state */
1456 cmsis_dap_add_jtag_sequence(
1463 tap_set_state(tap_state_transition(tap_get_state(), 0));
1465 LOG_DEBUG_IO("Internal field, staying in SHIFT state afterwards");
1466 /* Clocking part of a sequence into DR or IR with TMS=0,
1467 leaving TMS=0 at the end so we can continue later */
1468 cmsis_dap_add_jtag_sequence(
1478 if (tap_get_state() != tap_get_end_state()) {
1479 cmsis_dap_end_state(tap_get_end_state());
1480 cmsis_dap_state_move();
1483 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
1484 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
1485 tap_state_name(tap_get_end_state()));
1488 static void cmsis_dap_pathmove(int num_states
, tap_state_t
*path
)
1491 uint8_t tms0
= 0x00;
1492 uint8_t tms1
= 0xff;
1494 for (i
= 0; i
< num_states
; i
++) {
1495 if (path
[i
] == tap_state_transition(tap_get_state(), false))
1496 cmsis_dap_add_tms_sequence(&tms0
, 1);
1497 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
1498 cmsis_dap_add_tms_sequence(&tms1
, 1);
1500 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
1501 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
1505 tap_set_state(path
[i
]);
1508 cmsis_dap_end_state(tap_get_state());
1511 static void cmsis_dap_execute_pathmove(struct jtag_command
*cmd
)
1513 LOG_DEBUG_IO("pathmove: %i states, end in %i",
1514 cmd
->cmd
.pathmove
->num_states
,
1515 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1517 cmsis_dap_pathmove(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
1520 static void cmsis_dap_stableclocks(int num_cycles
)
1524 uint8_t tms
= tap_get_state() == TAP_RESET
;
1525 /* TODO: Perform optimizations? */
1526 /* Execute num_cycles. */
1527 for (i
= 0; i
< num_cycles
; i
++)
1528 cmsis_dap_add_tms_sequence(&tms
, 1);
1531 static void cmsis_dap_runtest(int num_cycles
)
1533 tap_state_t saved_end_state
= tap_get_end_state();
1535 /* Only do a state_move when we're not already in IDLE. */
1536 if (tap_get_state() != TAP_IDLE
) {
1537 cmsis_dap_end_state(TAP_IDLE
);
1538 cmsis_dap_state_move();
1540 cmsis_dap_stableclocks(num_cycles
);
1542 /* Finish in end_state. */
1543 cmsis_dap_end_state(saved_end_state
);
1545 if (tap_get_state() != tap_get_end_state())
1546 cmsis_dap_state_move();
1549 static void cmsis_dap_execute_runtest(struct jtag_command
*cmd
)
1551 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
,
1552 cmd
->cmd
.runtest
->end_state
);
1554 cmsis_dap_end_state(cmd
->cmd
.runtest
->end_state
);
1555 cmsis_dap_runtest(cmd
->cmd
.runtest
->num_cycles
);
1558 static void cmsis_dap_execute_stableclocks(struct jtag_command
*cmd
)
1560 LOG_DEBUG_IO("stableclocks %i cycles", cmd
->cmd
.runtest
->num_cycles
);
1561 cmsis_dap_stableclocks(cmd
->cmd
.runtest
->num_cycles
);
1564 static void cmsis_dap_execute_tms(struct jtag_command
*cmd
)
1566 LOG_DEBUG_IO("TMS: %d bits", cmd
->cmd
.tms
->num_bits
);
1567 cmsis_dap_cmd_DAP_SWJ_Sequence(cmd
->cmd
.tms
->num_bits
, cmd
->cmd
.tms
->bits
);
1570 /* TODO: Is there need to call cmsis_dap_flush() for the JTAG_PATHMOVE,
1571 * JTAG_RUNTEST, JTAG_STABLECLOCKS? */
1572 static void cmsis_dap_execute_command(struct jtag_command
*cmd
)
1574 switch (cmd
->type
) {
1577 cmsis_dap_execute_sleep(cmd
);
1579 case JTAG_TLR_RESET
:
1581 cmsis_dap_execute_tlr_reset(cmd
);
1584 cmsis_dap_execute_scan(cmd
);
1587 cmsis_dap_execute_pathmove(cmd
);
1590 cmsis_dap_execute_runtest(cmd
);
1592 case JTAG_STABLECLOCKS
:
1593 cmsis_dap_execute_stableclocks(cmd
);
1596 cmsis_dap_execute_tms(cmd
);
1599 LOG_ERROR("BUG: unknown JTAG command type 0x%X encountered", cmd
->type
);
1604 static int cmsis_dap_execute_queue(void)
1606 struct jtag_command
*cmd
= jtag_command_queue
;
1608 while (cmd
!= NULL
) {
1609 cmsis_dap_execute_command(cmd
);
1618 static int cmsis_dap_speed(int speed
)
1621 LOG_ERROR("RTCK not supported. Set nonzero \"adapter speed\".");
1622 return ERROR_JTAG_NOT_IMPLEMENTED
;
1625 return cmsis_dap_cmd_DAP_SWJ_Clock(speed
);
1628 static int cmsis_dap_speed_div(int speed
, int *khz
)
1634 static int cmsis_dap_khz(int khz
, int *jtag_speed
)
1640 COMMAND_HANDLER(cmsis_dap_handle_info_command
)
1642 if (cmsis_dap_get_version_info() == ERROR_OK
)
1643 cmsis_dap_get_status();
1648 COMMAND_HANDLER(cmsis_dap_handle_cmd_command
)
1652 uint8_t *command
= cmsis_dap_handle
->command
;
1654 for (i
= 0; i
< CMD_ARGC
; i
++)
1655 command
[i
] = strtoul(CMD_ARGV
[i
], NULL
, 16);
1657 retval
= cmsis_dap_xfer(cmsis_dap_handle
, CMD_ARGC
);
1659 if (retval
!= ERROR_OK
) {
1660 LOG_ERROR("CMSIS-DAP command failed.");
1661 return ERROR_JTAG_DEVICE_ERROR
;
1664 uint8_t *resp
= cmsis_dap_handle
->response
;
1665 LOG_INFO("Returned data %02" PRIx8
" %02" PRIx8
" %02" PRIx8
" %02" PRIx8
,
1666 resp
[1], resp
[2], resp
[3], resp
[4]);
1671 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command
)
1673 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
1674 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1675 "(maximum is %d pairs)", MAX_USB_IDS
);
1676 CMD_ARGC
= MAX_USB_IDS
* 2;
1678 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
1679 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1681 return ERROR_COMMAND_SYNTAX_ERROR
;
1682 /* remove the incomplete trailing id */
1687 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
1688 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], cmsis_dap_vid
[i
>> 1]);
1689 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], cmsis_dap_pid
[i
>> 1]);
1693 * Explicitly terminate, in case there are multiples instances of
1694 * cmsis_dap_vid_pid.
1696 cmsis_dap_vid
[i
>> 1] = cmsis_dap_pid
[i
>> 1] = 0;
1701 COMMAND_HANDLER(cmsis_dap_handle_serial_command
)
1704 cmsis_dap_serial
= strdup(CMD_ARGV
[0]);
1706 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1711 COMMAND_HANDLER(cmsis_dap_handle_backend_command
)
1713 if (CMD_ARGC
== 1) {
1714 if (strcmp(CMD_ARGV
[0], "auto") == 0) {
1715 cmsis_dap_backend
= -1; /* autoselect */
1717 for (unsigned int i
= 0; i
< ARRAY_SIZE(cmsis_dap_backends
); i
++) {
1718 if (strcasecmp(cmsis_dap_backends
[i
]->name
, CMD_ARGV
[0]) == 0) {
1719 cmsis_dap_backend
= i
;
1724 LOG_ERROR("invalid backend argument to cmsis_dap_backend <backend>");
1727 LOG_ERROR("expected exactly one argument to cmsis_dap_backend <backend>");
1733 static const struct command_registration cmsis_dap_subcommand_handlers
[] = {
1736 .handler
= &cmsis_dap_handle_info_command
,
1737 .mode
= COMMAND_EXEC
,
1739 .help
= "show cmsis-dap info",
1743 .handler
= &cmsis_dap_handle_cmd_command
,
1744 .mode
= COMMAND_EXEC
,
1746 .help
= "issue cmsis-dap command",
1748 COMMAND_REGISTRATION_DONE
1752 static const struct command_registration cmsis_dap_command_handlers
[] = {
1754 .name
= "cmsis-dap",
1755 .mode
= COMMAND_ANY
,
1756 .help
= "perform CMSIS-DAP management",
1758 .chain
= cmsis_dap_subcommand_handlers
,
1761 .name
= "cmsis_dap_vid_pid",
1762 .handler
= &cmsis_dap_handle_vid_pid_command
,
1763 .mode
= COMMAND_CONFIG
,
1764 .help
= "the vendor ID and product ID of the CMSIS-DAP device",
1765 .usage
= "(vid pid)* ",
1768 .name
= "cmsis_dap_serial",
1769 .handler
= &cmsis_dap_handle_serial_command
,
1770 .mode
= COMMAND_CONFIG
,
1771 .help
= "set the serial number of the adapter",
1772 .usage
= "serial_string",
1775 .name
= "cmsis_dap_backend",
1776 .handler
= &cmsis_dap_handle_backend_command
,
1777 .mode
= COMMAND_CONFIG
,
1778 .help
= "set the communication backend to use (USB bulk or HID).",
1779 .usage
= "(auto | usb_bulk | hid)",
1781 #if BUILD_CMSIS_DAP_USB
1783 .name
= "cmsis_dap_usb",
1784 .chain
= cmsis_dap_usb_subcommand_handlers
,
1785 .mode
= COMMAND_ANY
,
1786 .help
= "USB bulk backend-specific commands",
1790 COMMAND_REGISTRATION_DONE
1793 static const struct swd_driver cmsis_dap_swd_driver
= {
1794 .init
= cmsis_dap_swd_init
,
1795 .switch_seq
= cmsis_dap_swd_switch_seq
,
1796 .read_reg
= cmsis_dap_swd_read_reg
,
1797 .write_reg
= cmsis_dap_swd_write_reg
,
1798 .run
= cmsis_dap_swd_run_queue
,
1801 static const char * const cmsis_dap_transport
[] = { "swd", "jtag", NULL
};
1803 static struct jtag_interface cmsis_dap_interface
= {
1804 .supported
= DEBUG_CAP_TMS_SEQ
,
1805 .execute_queue
= cmsis_dap_execute_queue
,
1808 struct adapter_driver cmsis_dap_adapter_driver
= {
1809 .name
= "cmsis-dap",
1810 .transports
= cmsis_dap_transport
,
1811 .commands
= cmsis_dap_command_handlers
,
1813 .init
= cmsis_dap_init
,
1814 .quit
= cmsis_dap_quit
,
1815 .reset
= cmsis_dap_reset
,
1816 .speed
= cmsis_dap_speed
,
1817 .khz
= cmsis_dap_khz
,
1818 .speed_div
= cmsis_dap_speed_div
,
1820 .jtag_ops
= &cmsis_dap_interface
,
1821 .swd_ops
= &cmsis_dap_swd_driver
,
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)