10e6638627def00d9f9050fe9a8ca56c9bd30bf1
[openocd.git] / src / jtag / drivers / cmsis_dap.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /***************************************************************************
4 * Copyright (C) 2021 by Adrian Negreanu *
5 * groleo@gmail.com *
6 * *
7 * Copyright (C) 2018 by Mickaƫl Thomas *
8 * mickael9@gmail.com *
9 * *
10 * Copyright (C) 2016 by Maksym Hilliaka *
11 * oter@frozen-team.com *
12 * *
13 * Copyright (C) 2016 by Phillip Pearson *
14 * pp@myelin.co.nz *
15 * *
16 * Copyright (C) 2014 by Paul Fertser *
17 * fercerpav@gmail.com *
18 * *
19 * Copyright (C) 2013 by mike brown *
20 * mike@theshedworks.org.uk *
21 * *
22 * Copyright (C) 2013 by Spencer Oliver *
23 * spen@spen-soft.co.uk *
24 ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <transport/transport.h>
31 #include "helper/replacements.h"
32 #include <jtag/adapter.h>
33 #include <jtag/swd.h>
34 #include <jtag/interface.h>
35 #include <jtag/commands.h>
36 #include <jtag/tcl.h>
37 #include <target/cortex_m.h>
38
39 #include "cmsis_dap.h"
40 #include "libusb_helper.h"
41
42 static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
43 #if BUILD_CMSIS_DAP_USB == 1
44 &cmsis_dap_usb_backend,
45 #endif
46
47 #if BUILD_CMSIS_DAP_HID == 1
48 &cmsis_dap_hid_backend,
49 #endif
50 };
51
52 /* USB Config */
53
54 /* Known vid/pid pairs:
55 * VID 0xc251: Keil Software
56 * PID 0xf001: LPC-Link-II CMSIS_DAP
57 * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
58 * PID 0x2722: Keil ULINK2 CMSIS-DAP
59 * PID 0x2750: Keil ULINKplus CMSIS-DAP
60 *
61 * VID 0x0d28: mbed Software
62 * PID 0x0204: MBED CMSIS-DAP
63 */
64
65 #define MAX_USB_IDS 8
66 /* vid = pid = 0 marks the end of the list */
67 static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
68 static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
69 static int cmsis_dap_backend = -1;
70 static bool swd_mode;
71
72 /* CMSIS-DAP General Commands */
73 #define CMD_DAP_INFO 0x00
74 #define CMD_DAP_LED 0x01
75 #define CMD_DAP_CONNECT 0x02
76 #define CMD_DAP_DISCONNECT 0x03
77 #define CMD_DAP_WRITE_ABORT 0x08
78 #define CMD_DAP_DELAY 0x09
79 #define CMD_DAP_RESET_TARGET 0x0A
80
81 /* CMD_INFO */
82 #define INFO_ID_VENDOR 0x01 /* string */
83 #define INFO_ID_PRODUCT 0x02 /* string */
84 #define INFO_ID_SERNUM 0x03 /* string */
85 #define INFO_ID_FW_VER 0x04 /* string */
86 #define INFO_ID_TD_VEND 0x05 /* string */
87 #define INFO_ID_TD_NAME 0x06 /* string */
88 #define INFO_ID_CAPS 0xf0 /* byte */
89 #define INFO_ID_PKT_CNT 0xfe /* byte */
90 #define INFO_ID_PKT_SZ 0xff /* short */
91 #define INFO_ID_SWO_BUF_SZ 0xfd /* word */
92
93 #define INFO_CAPS_SWD BIT(0)
94 #define INFO_CAPS_JTAG BIT(1)
95 #define INFO_CAPS_SWO_UART BIT(2)
96 #define INFO_CAPS_SWO_MANCHESTER BIT(3)
97 #define INFO_CAPS_ATOMIC_CMDS BIT(4)
98 #define INFO_CAPS_TEST_DOMAIN_TIMER BIT(5)
99 #define INFO_CAPS_SWO_STREAMING_TRACE BIT(6)
100 #define INFO_CAPS_UART_PORT BIT(7)
101 #define INFO_CAPS_USB_COM_PORT BIT(8)
102 #define INFO_CAPS__NUM_CAPS 9
103
104 /* CMD_LED */
105 #define LED_ID_CONNECT 0x00
106 #define LED_ID_RUN 0x01
107
108 #define LED_OFF 0x00
109 #define LED_ON 0x01
110
111 /* CMD_CONNECT */
112 #define CONNECT_DEFAULT 0x00
113 #define CONNECT_SWD 0x01
114 #define CONNECT_JTAG 0x02
115
116 /* CMSIS-DAP Common SWD/JTAG Commands */
117 #define CMD_DAP_DELAY 0x09
118 #define CMD_DAP_SWJ_PINS 0x10
119 #define CMD_DAP_SWJ_CLOCK 0x11
120 #define CMD_DAP_SWJ_SEQ 0x12
121
122 /*
123 * PINS
124 * Bit 0: SWCLK/TCK
125 * Bit 1: SWDIO/TMS
126 * Bit 2: TDI
127 * Bit 3: TDO
128 * Bit 5: nTRST
129 * Bit 7: nRESET
130 */
131
132 #define SWJ_PIN_TCK (1<<0)
133 #define SWJ_PIN_TMS (1<<1)
134 #define SWJ_PIN_TDI (1<<2)
135 #define SWJ_PIN_TDO (1<<3)
136 #define SWJ_PIN_TRST (1<<5)
137 #define SWJ_PIN_SRST (1<<7)
138
139 /* CMSIS-DAP SWD Commands */
140 #define CMD_DAP_SWD_CONFIGURE 0x13
141 #define CMD_DAP_SWD_SEQUENCE 0x1D
142
143 /* CMSIS-DAP JTAG Commands */
144 #define CMD_DAP_JTAG_SEQ 0x14
145 #define CMD_DAP_JTAG_CONFIGURE 0x15
146 #define CMD_DAP_JTAG_IDCODE 0x16
147
148 /* CMSIS-DAP JTAG sequence info masks */
149 /* Number of bits to clock through (0 means 64) */
150 #define DAP_JTAG_SEQ_TCK 0x3F
151 /* TMS will be set during the sequence if this bit is set */
152 #define DAP_JTAG_SEQ_TMS 0x40
153 /* TDO output will be captured if this bit is set */
154 #define DAP_JTAG_SEQ_TDO 0x80
155
156
157 /* CMSIS-DAP Transfer Commands */
158 #define CMD_DAP_TFER_CONFIGURE 0x04
159 #define CMD_DAP_TFER 0x05
160 #define CMD_DAP_TFER_BLOCK 0x06
161 #define CMD_DAP_TFER_ABORT 0x07
162
163 /* DAP Status Code */
164 #define DAP_OK 0
165 #define DAP_ERROR 0xFF
166
167 /* CMSIS-DAP SWO Commands */
168 #define CMD_DAP_SWO_TRANSPORT 0x17
169 #define CMD_DAP_SWO_MODE 0x18
170 #define CMD_DAP_SWO_BAUDRATE 0x19
171 #define CMD_DAP_SWO_CONTROL 0x1A
172 #define CMD_DAP_SWO_STATUS 0x1B
173 #define CMD_DAP_SWO_DATA 0x1C
174 #define CMD_DAP_SWO_EX_STATUS 0x1E
175
176 /* SWO transport mode for reading trace data */
177 #define DAP_SWO_TRANSPORT_NONE 0
178 #define DAP_SWO_TRANSPORT_DATA 1
179 #define DAP_SWO_TRANSPORT_WINUSB 2
180
181 /* SWO trace capture mode */
182 #define DAP_SWO_MODE_OFF 0
183 #define DAP_SWO_MODE_UART 1
184 #define DAP_SWO_MODE_MANCHESTER 2
185
186 /* SWO trace data capture */
187 #define DAP_SWO_CONTROL_STOP 0
188 #define DAP_SWO_CONTROL_START 1
189
190 /* SWO trace status */
191 #define DAP_SWO_STATUS_CAPTURE_INACTIVE 0
192 #define DAP_SWO_STATUS_CAPTURE_ACTIVE 1
193 #define DAP_SWO_STATUS_CAPTURE_MASK BIT(0)
194 #define DAP_SWO_STATUS_STREAM_ERROR_MASK BIT(6)
195 #define DAP_SWO_STATUS_BUFFER_OVERRUN_MASK BIT(7)
196
197 /* CMSIS-DAP Vendor Commands
198 * None as yet... */
199
200 static const char * const info_caps_str[INFO_CAPS__NUM_CAPS] = {
201 "SWD supported",
202 "JTAG supported",
203 "SWO-UART supported",
204 "SWO-MANCHESTER supported",
205 "Atomic commands supported",
206 "Test domain timer supported",
207 "SWO streaming trace supported",
208 "UART communication port supported",
209 "UART via USB COM port supported",
210 };
211
212 struct pending_scan_result {
213 /** Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer. */
214 unsigned int first;
215 /** Number of bits to read. */
216 unsigned int length;
217 /** Location to store the result */
218 uint8_t *buffer;
219 /** Offset in the destination buffer */
220 unsigned int buffer_offset;
221 };
222
223 /* Each block in FIFO can contain up to pending_queue_len transfers */
224 static unsigned int pending_queue_len;
225 static unsigned int tfer_max_command_size;
226 static unsigned int tfer_max_response_size;
227
228 /* pointers to buffers that will receive jtag scan results on the next flush */
229 #define MAX_PENDING_SCAN_RESULTS 256
230 static int pending_scan_result_count;
231 static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS];
232
233 /* queued JTAG sequences that will be executed on the next flush */
234 #define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_usable_size - 3)
235 static int queued_seq_count;
236 static int queued_seq_buf_end;
237 static int queued_seq_tdo_ptr;
238 static uint8_t queued_seq_buf[1024]; /* TODO: make dynamic / move into cmsis object */
239
240 static int queued_retval;
241
242 static uint8_t output_pins = SWJ_PIN_SRST | SWJ_PIN_TRST;
243
244 static struct cmsis_dap *cmsis_dap_handle;
245
246
247 static int cmsis_dap_quit(void);
248
249 static int cmsis_dap_open(void)
250 {
251 const struct cmsis_dap_backend *backend = NULL;
252
253 struct cmsis_dap *dap = calloc(1, sizeof(struct cmsis_dap));
254 if (!dap) {
255 LOG_ERROR("unable to allocate memory");
256 return ERROR_FAIL;
257 }
258
259 if (cmsis_dap_backend >= 0) {
260 /* Use forced backend */
261 backend = cmsis_dap_backends[cmsis_dap_backend];
262 if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) != ERROR_OK)
263 backend = NULL;
264 } else {
265 /* Try all backends */
266 for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
267 backend = cmsis_dap_backends[i];
268 if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, adapter_get_required_serial()) == ERROR_OK)
269 break;
270 else
271 backend = NULL;
272 }
273 }
274
275 if (!backend) {
276 LOG_ERROR("unable to find a matching CMSIS-DAP device");
277 free(dap);
278 return ERROR_FAIL;
279 }
280
281 dap->backend = backend;
282
283 cmsis_dap_handle = dap;
284
285 return ERROR_OK;
286 }
287
288 static void cmsis_dap_close(struct cmsis_dap *dap)
289 {
290 if (dap->backend) {
291 dap->backend->close(dap);
292 dap->backend = NULL;
293 }
294
295 free(dap->packet_buffer);
296
297 for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
298 free(dap->pending_fifo[i].transfers);
299 dap->pending_fifo[i].transfers = NULL;
300 }
301
302 free(cmsis_dap_handle);
303 cmsis_dap_handle = NULL;
304 }
305
306 static void cmsis_dap_flush_read(struct cmsis_dap *dap)
307 {
308 unsigned int i;
309 /* Some CMSIS-DAP adapters keep buffered packets over
310 * USB close/open so we need to flush up to 64 old packets
311 * to be sure all buffers are empty */
312 for (i = 0; i < 64; i++) {
313 int retval = dap->backend->read(dap, 10);
314 if (retval == ERROR_TIMEOUT_REACHED)
315 break;
316 }
317 if (i)
318 LOG_DEBUG("Flushed %u packets", i);
319 }
320
321 /* Send a message and receive the reply */
322 static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
323 {
324 if (dap->pending_fifo_block_count) {
325 LOG_ERROR("pending %u blocks, flushing", dap->pending_fifo_block_count);
326 while (dap->pending_fifo_block_count) {
327 dap->backend->read(dap, 10);
328 dap->pending_fifo_block_count--;
329 }
330 dap->pending_fifo_put_idx = 0;
331 dap->pending_fifo_get_idx = 0;
332 }
333
334 uint8_t current_cmd = dap->command[0];
335 int retval = dap->backend->write(dap, txlen, LIBUSB_TIMEOUT_MS);
336 if (retval < 0)
337 return retval;
338
339 /* get reply */
340 retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS);
341 if (retval < 0)
342 return retval;
343
344 uint8_t *resp = dap->response;
345 if (resp[0] == DAP_ERROR) {
346 LOG_ERROR("CMSIS-DAP command 0x%" PRIx8 " not implemented", current_cmd);
347 return ERROR_NOT_IMPLEMENTED;
348 }
349
350 if (resp[0] != current_cmd) {
351 LOG_ERROR("CMSIS-DAP command mismatch. Sent 0x%" PRIx8
352 " received 0x%" PRIx8, current_cmd, resp[0]);
353
354 cmsis_dap_flush_read(dap);
355 return ERROR_FAIL;
356 }
357
358 return ERROR_OK;
359 }
360
361 static int cmsis_dap_cmd_dap_swj_pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
362 {
363 uint8_t *command = cmsis_dap_handle->command;
364
365 command[0] = CMD_DAP_SWJ_PINS;
366 command[1] = pins;
367 command[2] = mask;
368 h_u32_to_le(&command[3], delay);
369
370 int retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
371 if (retval != ERROR_OK) {
372 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
373 return ERROR_JTAG_DEVICE_ERROR;
374 }
375
376 if (input)
377 *input = cmsis_dap_handle->response[1];
378
379 return ERROR_OK;
380 }
381
382 static int cmsis_dap_cmd_dap_swj_clock(uint32_t swj_clock)
383 {
384 uint8_t *command = cmsis_dap_handle->command;
385
386 /* set clock in Hz */
387 swj_clock *= 1000;
388
389 command[0] = CMD_DAP_SWJ_CLOCK;
390 h_u32_to_le(&command[1], swj_clock);
391
392 int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
393 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
394 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
395 return ERROR_JTAG_DEVICE_ERROR;
396 }
397
398 return ERROR_OK;
399 }
400
401 /* clock a sequence of bits out on TMS, to change JTAG states */
402 static int cmsis_dap_cmd_dap_swj_sequence(uint8_t s_len, const uint8_t *sequence)
403 {
404 uint8_t *command = cmsis_dap_handle->command;
405
406 #ifdef CMSIS_DAP_JTAG_DEBUG
407 LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
408 for (unsigned int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
409 printf("%02X ", sequence[i]);
410
411 printf("\n");
412 #endif
413
414 command[0] = CMD_DAP_SWJ_SEQ;
415 command[1] = s_len;
416 bit_copy(&command[2], 0, sequence, 0, s_len);
417
418 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2 + DIV_ROUND_UP(s_len, 8));
419 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK)
420 return ERROR_FAIL;
421
422 return ERROR_OK;
423 }
424
425 static int cmsis_dap_cmd_dap_info(uint8_t info, uint8_t **data)
426 {
427 uint8_t *command = cmsis_dap_handle->command;
428
429 command[0] = CMD_DAP_INFO;
430 command[1] = info;
431
432 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
433 if (retval != ERROR_OK) {
434 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
435 return ERROR_JTAG_DEVICE_ERROR;
436 }
437
438 *data = &cmsis_dap_handle->response[1];
439
440 return ERROR_OK;
441 }
442
443 static int cmsis_dap_cmd_dap_led(uint8_t led, uint8_t state)
444 {
445 uint8_t *command = cmsis_dap_handle->command;
446
447 command[0] = CMD_DAP_LED;
448 command[1] = led;
449 command[2] = state;
450
451 int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
452 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
453 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
454 return ERROR_JTAG_DEVICE_ERROR;
455 }
456
457 return ERROR_OK;
458 }
459
460 static int cmsis_dap_cmd_dap_connect(uint8_t mode)
461 {
462 uint8_t *command = cmsis_dap_handle->command;
463
464 command[0] = CMD_DAP_CONNECT;
465 command[1] = mode;
466
467 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
468 if (retval != ERROR_OK) {
469 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
470 return ERROR_JTAG_DEVICE_ERROR;
471 }
472
473 if (cmsis_dap_handle->response[1] != mode) {
474 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
475 return ERROR_JTAG_DEVICE_ERROR;
476 }
477
478 return ERROR_OK;
479 }
480
481 static int cmsis_dap_cmd_dap_disconnect(void)
482 {
483 uint8_t *command = cmsis_dap_handle->command;
484
485 command[0] = CMD_DAP_DISCONNECT;
486
487 int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
488 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
489 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
490 return ERROR_JTAG_DEVICE_ERROR;
491 }
492
493 return ERROR_OK;
494 }
495
496 static int cmsis_dap_cmd_dap_tfer_configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
497 {
498 uint8_t *command = cmsis_dap_handle->command;
499
500 command[0] = CMD_DAP_TFER_CONFIGURE;
501 command[1] = idle;
502 h_u16_to_le(&command[2], retry_count);
503 h_u16_to_le(&command[4], match_retry);
504
505 int retval = cmsis_dap_xfer(cmsis_dap_handle, 6);
506 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
507 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
508 return ERROR_JTAG_DEVICE_ERROR;
509 }
510
511 return ERROR_OK;
512 }
513
514 static int cmsis_dap_cmd_dap_swd_configure(uint8_t cfg)
515 {
516 uint8_t *command = cmsis_dap_handle->command;
517
518 command[0] = CMD_DAP_SWD_CONFIGURE;
519 command[1] = cfg;
520
521 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
522 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
523 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
524 return ERROR_JTAG_DEVICE_ERROR;
525 }
526
527 return ERROR_OK;
528 }
529
530 #if 0
531 static int cmsis_dap_cmd_dap_delay(uint16_t delay_us)
532 {
533 uint8_t *command = cmsis_dap_handle->command;
534
535 command[0] = CMD_DAP_DELAY;
536 h_u16_to_le(&command[1], delay_us);
537
538 int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
539 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
540 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
541 return ERROR_JTAG_DEVICE_ERROR;
542 }
543
544 return ERROR_OK;
545 }
546 #endif
547
548 static int cmsis_dap_metacmd_targetsel(uint32_t instance_id)
549 {
550 uint8_t *command = cmsis_dap_handle->command;
551 const uint32_t SEQ_RD = 0x80, SEQ_WR = 0x00;
552
553 /* SWD multi-drop requires a transfer ala CMD_DAP_TFER,
554 but with no expectation of an SWD ACK response. In
555 CMSIS-DAP v1.20 and v2.00, CMD_DAP_SWD_SEQUENCE was
556 added to allow this special sequence to be generated.
557 The purpose of this operation is to select the target
558 corresponding to the instance_id that is written */
559
560 LOG_DEBUG_IO("DP write reg TARGETSEL %" PRIx32, instance_id);
561
562 size_t idx = 0;
563 command[idx++] = CMD_DAP_SWD_SEQUENCE;
564 command[idx++] = 3; /* sequence count */
565
566 /* sequence 0: packet request for TARGETSEL */
567 command[idx++] = SEQ_WR | 8;
568 command[idx++] = SWD_CMD_START | swd_cmd(false, false, DP_TARGETSEL) | SWD_CMD_STOP | SWD_CMD_PARK;
569
570 /* sequence 1: read Trn ACK Trn, no expectation for target to ACK */
571 command[idx++] = SEQ_RD | 5;
572
573 /* sequence 2: WDATA plus parity */
574 command[idx++] = SEQ_WR | (32 + 1);
575 h_u32_to_le(command + idx, instance_id);
576 idx += 4;
577 command[idx++] = parity_u32(instance_id);
578
579 int retval = cmsis_dap_xfer(cmsis_dap_handle, idx);
580 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
581 LOG_ERROR("CMSIS-DAP command SWD_Sequence failed.");
582 return ERROR_JTAG_DEVICE_ERROR;
583 }
584
585 return ERROR_OK;
586 }
587
588 /**
589 * Sets the SWO transport mode.
590 * @param[in] transport The transport mode. Can be None, SWO_Data or
591 * WinUSB (requires CMSIS-DAP v2).
592 */
593 static int cmsis_dap_cmd_dap_swo_transport(uint8_t transport)
594 {
595 uint8_t *command = cmsis_dap_handle->command;
596
597 command[0] = CMD_DAP_SWO_TRANSPORT;
598 command[1] = transport;
599
600 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
601 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
602 LOG_ERROR("CMSIS-DAP: command CMD_SWO_Transport(%d) failed.", transport);
603 return ERROR_JTAG_DEVICE_ERROR;
604 }
605
606 return ERROR_OK;
607 }
608
609 /**
610 * Sets the SWO trace capture mode.
611 * @param[in] mode Trace capture mode. Can be UART or MANCHESTER.
612 */
613 static int cmsis_dap_cmd_dap_swo_mode(uint8_t mode)
614 {
615 uint8_t *command = cmsis_dap_handle->command;
616
617 command[0] = CMD_DAP_SWO_MODE;
618 command[1] = mode;
619
620 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
621 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
622 LOG_ERROR("CMSIS-DAP: command CMD_SWO_Mode(%d) failed.", mode);
623 return ERROR_JTAG_DEVICE_ERROR;
624 }
625
626 return ERROR_OK;
627 }
628
629 /**
630 * Sets the baudrate for capturing SWO trace data.
631 * Can be called iteratively to determine supported baudrates.
632 * @param[in] in_baudrate Requested baudrate.
633 * @param[out] dev_baudrate Actual baudrate or 0 (baudrate not configured).
634 * When requested baudrate is not achievable the
635 * closest configured baudrate can be returned or
636 * 0 which indicates that baudrate was not configured.
637 */
638 static int cmsis_dap_cmd_dap_swo_baudrate(
639 uint32_t in_baudrate,
640 uint32_t *dev_baudrate)
641 {
642 uint8_t *command = cmsis_dap_handle->command;
643
644 command[0] = CMD_DAP_SWO_BAUDRATE;
645 h_u32_to_le(&command[1], in_baudrate);
646
647 int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
648 uint32_t rvbr = le_to_h_u32(&cmsis_dap_handle->response[1]);
649 if (retval != ERROR_OK || rvbr == 0) {
650 LOG_ERROR("CMSIS-DAP: command CMD_SWO_Baudrate(%u) -> %u failed.", in_baudrate, rvbr);
651 if (dev_baudrate)
652 *dev_baudrate = 0;
653 return ERROR_JTAG_DEVICE_ERROR;
654 }
655
656 if (dev_baudrate)
657 *dev_baudrate = rvbr;
658
659 return ERROR_OK;
660 }
661
662 /**
663 * Controls the SWO trace data capture.
664 * @param[in] control Start or stop a trace. Starting capture automatically
665 * flushes any existing trace data in buffers which has
666 * not yet been read.
667 */
668 static int cmsis_dap_cmd_dap_swo_control(uint8_t control)
669 {
670 uint8_t *command = cmsis_dap_handle->command;
671
672 command[0] = CMD_DAP_SWO_CONTROL;
673 command[1] = control;
674
675 int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
676 if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
677 LOG_ERROR("CMSIS-DAP: command CMD_SWO_Control(%d) failed.", control);
678 return ERROR_JTAG_DEVICE_ERROR;
679 }
680
681 return ERROR_OK;
682 }
683
684 /**
685 * Reads the SWO trace status.
686 * @param[out] trace_status The trace's status.
687 * Bit0: Trace Capture (1 - active, 0 - inactive).
688 * Bit6: Trace Stream Error.
689 * Bit7: Trace Buffer Overrun.
690 * @param[out] trace_count Number of bytes in Trace Buffer (not yet read).
691 */
692 static int cmsis_dap_cmd_dap_swo_status(
693 uint8_t *trace_status,
694 size_t *trace_count)
695 {
696 uint8_t *command = cmsis_dap_handle->command;
697
698 command[0] = CMD_DAP_SWO_STATUS;
699
700 int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
701 if (retval != ERROR_OK) {
702 LOG_ERROR("CMSIS-DAP: command CMD_SWO_Status failed.");
703 return ERROR_JTAG_DEVICE_ERROR;
704 }
705
706 if (trace_status)
707 *trace_status = cmsis_dap_handle->response[1];
708 if (trace_count)
709 *trace_count = le_to_h_u32(&cmsis_dap_handle->response[2]);
710
711 return ERROR_OK;
712 }
713
714 /**
715 * Reads the captured SWO trace data from Trace Buffer.
716 * @param[in] max_trace_count Maximum number of Trace Data bytes to read.
717 * @param[out] trace_status The trace's status.
718 * @param[out] trace_count Number of Trace Data bytes read.
719 * @param[out] data Trace Data bytes read.
720 */
721 static int cmsis_dap_cmd_dap_swo_data(
722 size_t max_trace_count,
723 uint8_t *trace_status,
724 size_t *trace_count,
725 uint8_t *data)
726 {
727 uint8_t *command = cmsis_dap_handle->command;
728
729 command[0] = CMD_DAP_SWO_DATA;
730 h_u16_to_le(&command[1], max_trace_count);
731
732 int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
733 if (retval != ERROR_OK) {
734 LOG_ERROR("CMSIS-DAP: command CMD_SWO_Data failed.");
735 return ERROR_JTAG_DEVICE_ERROR;
736 }
737
738 *trace_status = cmsis_dap_handle->response[1];
739 *trace_count = le_to_h_u16(&cmsis_dap_handle->response[2]);
740
741 if (*trace_count > 0)
742 memcpy(data, &cmsis_dap_handle->response[4], *trace_count);
743
744 return ERROR_OK;
745 }
746
747
748 static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
749 {
750 uint8_t *command = dap->command;
751 struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_put_idx];
752
753 assert(dap->write_count + dap->read_count == block->transfer_count);
754
755 /* Reset packet size check counters for the next packet */
756 dap->write_count = 0;
757 dap->read_count = 0;
758
759 LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %u",
760 block->transfer_count, dap->pending_fifo_put_idx);
761
762 if (queued_retval != ERROR_OK) {
763 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
764 goto skip;
765 }
766
767 if (block->transfer_count == 0)
768 goto skip;
769
770 command[0] = CMD_DAP_TFER;
771 command[1] = 0x00; /* DAP Index */
772 command[2] = block->transfer_count;
773 size_t idx = 3;
774
775 for (unsigned int i = 0; i < block->transfer_count; i++) {
776 struct pending_transfer_result *transfer = &(block->transfers[i]);
777 uint8_t cmd = transfer->cmd;
778 uint32_t data = transfer->data;
779
780 LOG_DEBUG_IO("%s %s reg %x %" PRIx32,
781 cmd & SWD_CMD_APNDP ? "AP" : "DP",
782 cmd & SWD_CMD_RNW ? "read" : "write",
783 (cmd & SWD_CMD_A32) >> 1, data);
784
785 /* When proper WAIT handling is implemented in the
786 * common SWD framework, this kludge can be
787 * removed. However, this might lead to minor
788 * performance degradation as the adapter wouldn't be
789 * able to automatically retry anything (because ARM
790 * has forgotten to implement sticky error flags
791 * clearing). See also comments regarding
792 * cmsis_dap_cmd_dap_tfer_configure() and
793 * cmsis_dap_cmd_dap_swd_configure() in
794 * cmsis_dap_init().
795 */
796 if (!(cmd & SWD_CMD_RNW) &&
797 !(cmd & SWD_CMD_APNDP) &&
798 (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
799 (data & CORUNDETECT)) {
800 LOG_DEBUG("refusing to enable sticky overrun detection");
801 data &= ~CORUNDETECT;
802 }
803
804 command[idx++] = (cmd >> 1) & 0x0f;
805 if (!(cmd & SWD_CMD_RNW)) {
806 h_u32_to_le(&command[idx], data);
807 idx += 4;
808 }
809 }
810
811 int retval = dap->backend->write(dap, idx, LIBUSB_TIMEOUT_MS);
812 if (retval < 0) {
813 queued_retval = retval;
814 goto skip;
815 } else {
816 queued_retval = ERROR_OK;
817 }
818
819 dap->pending_fifo_put_idx = (dap->pending_fifo_put_idx + 1) % dap->packet_count;
820 dap->pending_fifo_block_count++;
821 if (dap->pending_fifo_block_count > dap->packet_count)
822 LOG_ERROR("too much pending writes %u", dap->pending_fifo_block_count);
823
824 return;
825
826 skip:
827 block->transfer_count = 0;
828 }
829
830 static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
831 {
832 struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_get_idx];
833
834 if (dap->pending_fifo_block_count == 0)
835 LOG_ERROR("no pending write");
836
837 /* get reply */
838 int retval = dap->backend->read(dap, timeout_ms);
839 if (retval == ERROR_TIMEOUT_REACHED && timeout_ms < LIBUSB_TIMEOUT_MS)
840 return;
841
842 if (retval <= 0) {
843 LOG_DEBUG("error reading data");
844 queued_retval = ERROR_FAIL;
845 goto skip;
846 }
847
848 uint8_t *resp = dap->response;
849 if (resp[0] != CMD_DAP_TFER) {
850 LOG_ERROR("CMSIS-DAP command mismatch. Expected 0x%x received 0x%" PRIx8,
851 CMD_DAP_TFER, resp[0]);
852 queued_retval = ERROR_FAIL;
853 goto skip;
854 }
855
856 unsigned int transfer_count = resp[1];
857 uint8_t ack = resp[2] & 0x07;
858 if (resp[2] & 0x08) {
859 LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count);
860 queued_retval = ERROR_FAIL;
861 goto skip;
862 }
863 if (ack != SWD_ACK_OK) {
864 LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count,
865 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
866 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
867 /* TODO: use results of transfers completed before the error occurred? */
868 goto skip;
869 }
870
871 if (block->transfer_count != transfer_count)
872 LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
873 block->transfer_count, transfer_count);
874
875 LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %u timeout %i",
876 transfer_count, dap->pending_fifo_get_idx, timeout_ms);
877 unsigned int idx = 3;
878 for (unsigned int i = 0; i < transfer_count; i++) {
879 struct pending_transfer_result *transfer = &(block->transfers[i]);
880 if (transfer->cmd & SWD_CMD_RNW) {
881 static uint32_t last_read;
882 uint32_t data = le_to_h_u32(&resp[idx]);
883 uint32_t tmp = data;
884 idx += 4;
885
886 LOG_DEBUG_IO("Read result: %" PRIx32, data);
887
888 /* Imitate posted AP reads */
889 if ((transfer->cmd & SWD_CMD_APNDP) ||
890 ((transfer->cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
891 tmp = last_read;
892 last_read = data;
893 }
894
895 if (transfer->buffer)
896 *(uint32_t *)(transfer->buffer) = tmp;
897 }
898 }
899
900 skip:
901 block->transfer_count = 0;
902 dap->pending_fifo_get_idx = (dap->pending_fifo_get_idx + 1) % dap->packet_count;
903 dap->pending_fifo_block_count--;
904 }
905
906 static int cmsis_dap_swd_run_queue(void)
907 {
908 if (cmsis_dap_handle->pending_fifo_block_count)
909 cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
910
911 cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
912
913 while (cmsis_dap_handle->pending_fifo_block_count)
914 cmsis_dap_swd_read_process(cmsis_dap_handle, LIBUSB_TIMEOUT_MS);
915
916 cmsis_dap_handle->pending_fifo_put_idx = 0;
917 cmsis_dap_handle->pending_fifo_get_idx = 0;
918
919 int retval = queued_retval;
920 queued_retval = ERROR_OK;
921
922 return retval;
923 }
924
925 static unsigned int cmsis_dap_tfer_cmd_size(unsigned int write_count,
926 unsigned int read_count)
927 {
928 unsigned int size = 3; /* header */
929 size += write_count * (1 + 4); /* DAP register + data */
930 size += read_count; /* DAP register */
931 return size;
932 }
933
934 static unsigned int cmsis_dap_tfer_resp_size(unsigned int write_count,
935 unsigned int read_count)
936 {
937 unsigned int size = 3; /* header */
938 size += read_count * 4; /* data */
939 return size;
940 }
941
942 static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
943 {
944 /* Compute sizes of the DAP Transfer command and the expected response
945 * for all queued and this operation */
946 bool targetsel_cmd = swd_cmd(false, false, DP_TARGETSEL) == cmd;
947
948 unsigned int write_count = cmsis_dap_handle->write_count;
949 unsigned int read_count = cmsis_dap_handle->read_count;
950 if (cmd & SWD_CMD_RNW)
951 read_count++;
952 else
953 write_count++;
954
955 unsigned int cmd_size = cmsis_dap_tfer_cmd_size(write_count, read_count);
956 unsigned int resp_size = cmsis_dap_tfer_resp_size(write_count, read_count);
957
958 /* Does the DAP Transfer command and the expected response fit into one packet?
959 * Run the queue also before a targetsel - it cannot be queued */
960 if (cmd_size > tfer_max_command_size
961 || resp_size > tfer_max_response_size
962 || targetsel_cmd) {
963 if (cmsis_dap_handle->pending_fifo_block_count)
964 cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
965
966 /* Not enough room in the queue. Run the queue. */
967 cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
968
969 if (cmsis_dap_handle->pending_fifo_block_count >= cmsis_dap_handle->packet_count)
970 cmsis_dap_swd_read_process(cmsis_dap_handle, LIBUSB_TIMEOUT_MS);
971 }
972
973 assert(cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx].transfer_count < pending_queue_len);
974
975 if (queued_retval != ERROR_OK)
976 return;
977
978 if (targetsel_cmd) {
979 cmsis_dap_metacmd_targetsel(data);
980 return;
981 }
982
983 struct pending_request_block *block = &cmsis_dap_handle->pending_fifo[cmsis_dap_handle->pending_fifo_put_idx];
984 struct pending_transfer_result *transfer = &(block->transfers[block->transfer_count]);
985 transfer->data = data;
986 transfer->cmd = cmd;
987 if (cmd & SWD_CMD_RNW) {
988 /* Queue a read transaction */
989 transfer->buffer = dst;
990 cmsis_dap_handle->read_count++;
991 } else {
992 cmsis_dap_handle->write_count++;
993 }
994 block->transfer_count++;
995 }
996
997 static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
998 {
999 assert(!(cmd & SWD_CMD_RNW));
1000 cmsis_dap_swd_queue_cmd(cmd, NULL, value);
1001 }
1002
1003 static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1004 {
1005 assert(cmd & SWD_CMD_RNW);
1006 cmsis_dap_swd_queue_cmd(cmd, value, 0);
1007 }
1008
1009 static int cmsis_dap_get_serial_info(void)
1010 {
1011 uint8_t *data;
1012
1013 int retval = cmsis_dap_cmd_dap_info(INFO_ID_SERNUM, &data);
1014 if (retval != ERROR_OK)
1015 return retval;
1016
1017 if (data[0]) /* strlen */
1018 LOG_INFO("CMSIS-DAP: Serial# = %s", &data[1]);
1019
1020 return ERROR_OK;
1021 }
1022
1023 static int cmsis_dap_get_version_info(void)
1024 {
1025 uint8_t *data;
1026
1027 /* INFO_ID_FW_VER - string */
1028 int retval = cmsis_dap_cmd_dap_info(INFO_ID_FW_VER, &data);
1029 if (retval != ERROR_OK)
1030 return retval;
1031
1032 if (data[0]) /* strlen */
1033 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
1034
1035 return ERROR_OK;
1036 }
1037
1038 static int cmsis_dap_get_caps_info(void)
1039 {
1040 uint8_t *data;
1041
1042 /* INFO_ID_CAPS - byte */
1043 int retval = cmsis_dap_cmd_dap_info(INFO_ID_CAPS, &data);
1044 if (retval != ERROR_OK)
1045 return retval;
1046
1047 if (data[0] == 1 || data[0] == 2) {
1048 uint16_t caps = data[1];
1049 if (data[0] == 2)
1050 caps |= (uint16_t)data[2] << 8;
1051
1052 cmsis_dap_handle->caps = caps;
1053
1054 for (unsigned int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) {
1055 if (caps & BIT(i))
1056 LOG_INFO("CMSIS-DAP: %s", info_caps_str[i]);
1057 }
1058 }
1059
1060 return ERROR_OK;
1061 }
1062
1063 static int cmsis_dap_get_swo_buf_sz(uint32_t *swo_buf_sz)
1064 {
1065 uint8_t *data;
1066
1067 /* INFO_ID_SWO_BUF_SZ - word */
1068 int retval = cmsis_dap_cmd_dap_info(INFO_ID_SWO_BUF_SZ, &data);
1069 if (retval != ERROR_OK)
1070 return retval;
1071
1072 if (data[0] != 4)
1073 return ERROR_FAIL;
1074
1075 *swo_buf_sz = le_to_h_u32(&data[1]);
1076
1077 LOG_INFO("CMSIS-DAP: SWO Trace Buffer Size = %u bytes", *swo_buf_sz);
1078
1079 return ERROR_OK;
1080 }
1081
1082 static int cmsis_dap_get_status(void)
1083 {
1084 uint8_t d;
1085
1086 int retval = cmsis_dap_cmd_dap_swj_pins(0, 0, 0, &d);
1087
1088 if (retval == ERROR_OK) {
1089 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
1090 (d & SWJ_PIN_TCK) ? 1 : 0,
1091 (d & SWJ_PIN_TMS) ? 1 : 0,
1092 (d & SWJ_PIN_TDI) ? 1 : 0,
1093 (d & SWJ_PIN_TDO) ? 1 : 0,
1094 (d & SWJ_PIN_TRST) ? 1 : 0,
1095 (d & SWJ_PIN_SRST) ? 1 : 0);
1096 }
1097
1098 return retval;
1099 }
1100
1101 static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
1102 {
1103 const uint8_t *s;
1104 unsigned int s_len;
1105 int retval;
1106
1107 if ((output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST)) == (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
1108 /* Following workaround deasserts reset on most adapters.
1109 * Do not reconnect if a reset line is active!
1110 * Reconnecting would break connecting under reset. */
1111
1112 /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
1113 cmsis_dap_cmd_dap_disconnect();
1114
1115 /* When we are reconnecting, DAP_Connect needs to be rerun, at
1116 * least on Keil ULINK-ME */
1117 retval = cmsis_dap_cmd_dap_connect(CONNECT_SWD);
1118 if (retval != ERROR_OK)
1119 return retval;
1120 }
1121
1122 switch (seq) {
1123 case LINE_RESET:
1124 LOG_DEBUG_IO("SWD line reset");
1125 s = swd_seq_line_reset;
1126 s_len = swd_seq_line_reset_len;
1127 break;
1128 case JTAG_TO_SWD:
1129 LOG_DEBUG("JTAG-to-SWD");
1130 s = swd_seq_jtag_to_swd;
1131 s_len = swd_seq_jtag_to_swd_len;
1132 break;
1133 case JTAG_TO_DORMANT:
1134 LOG_DEBUG("JTAG-to-DORMANT");
1135 s = swd_seq_jtag_to_dormant;
1136 s_len = swd_seq_jtag_to_dormant_len;
1137 break;
1138 case SWD_TO_JTAG:
1139 LOG_DEBUG("SWD-to-JTAG");
1140 s = swd_seq_swd_to_jtag;
1141 s_len = swd_seq_swd_to_jtag_len;
1142 break;
1143 case SWD_TO_DORMANT:
1144 LOG_DEBUG("SWD-to-DORMANT");
1145 s = swd_seq_swd_to_dormant;
1146 s_len = swd_seq_swd_to_dormant_len;
1147 break;
1148 case DORMANT_TO_SWD:
1149 LOG_DEBUG("DORMANT-to-SWD");
1150 s = swd_seq_dormant_to_swd;
1151 s_len = swd_seq_dormant_to_swd_len;
1152 break;
1153 case DORMANT_TO_JTAG:
1154 LOG_DEBUG("DORMANT-to-JTAG");
1155 s = swd_seq_dormant_to_jtag;
1156 s_len = swd_seq_dormant_to_jtag_len;
1157 break;
1158 default:
1159 LOG_ERROR("Sequence %d not supported", seq);
1160 return ERROR_FAIL;
1161 }
1162
1163 retval = cmsis_dap_cmd_dap_swj_sequence(s_len, s);
1164 if (retval != ERROR_OK)
1165 return retval;
1166
1167 /* Atmel EDBG needs renew clock setting after SWJ_Sequence
1168 * otherwise default frequency is used */
1169 return cmsis_dap_cmd_dap_swj_clock(adapter_get_speed_khz());
1170 }
1171
1172 static int cmsis_dap_swd_open(void)
1173 {
1174 if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
1175 LOG_ERROR("CMSIS-DAP: SWD not supported");
1176 return ERROR_JTAG_DEVICE_ERROR;
1177 }
1178
1179 int retval = cmsis_dap_cmd_dap_connect(CONNECT_SWD);
1180 if (retval != ERROR_OK)
1181 return retval;
1182
1183 /* Add more setup here.??... */
1184
1185 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
1186 return ERROR_OK;
1187 }
1188
1189 static int cmsis_dap_init(void)
1190 {
1191 uint8_t *data;
1192
1193 int retval = cmsis_dap_open();
1194 if (retval != ERROR_OK)
1195 return retval;
1196
1197 cmsis_dap_flush_read(cmsis_dap_handle);
1198
1199 retval = cmsis_dap_get_caps_info();
1200 if (retval != ERROR_OK)
1201 return retval;
1202
1203 retval = cmsis_dap_get_version_info();
1204 if (retval != ERROR_OK)
1205 return retval;
1206
1207 retval = cmsis_dap_get_serial_info();
1208 if (retval != ERROR_OK)
1209 return retval;
1210
1211 if (swd_mode) {
1212 retval = cmsis_dap_swd_open();
1213 if (retval != ERROR_OK)
1214 return retval;
1215 } else {
1216 /* Connect in JTAG mode */
1217 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
1218 LOG_ERROR("CMSIS-DAP: JTAG not supported");
1219 return ERROR_JTAG_DEVICE_ERROR;
1220 }
1221
1222 retval = cmsis_dap_cmd_dap_connect(CONNECT_JTAG);
1223 if (retval != ERROR_OK)
1224 return retval;
1225
1226 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
1227 }
1228
1229 /* Be conservative and suppress submitting multiple HID requests
1230 * until we get packet count info from the adaptor */
1231 cmsis_dap_handle->packet_count = 1;
1232
1233 /* INFO_ID_PKT_SZ - short */
1234 retval = cmsis_dap_cmd_dap_info(INFO_ID_PKT_SZ, &data);
1235 if (retval != ERROR_OK)
1236 goto init_err;
1237
1238 if (data[0] == 2) { /* short */
1239 uint16_t pkt_sz = data[1] + (data[2] << 8);
1240 if (pkt_sz != cmsis_dap_handle->packet_size) {
1241 free(cmsis_dap_handle->packet_buffer);
1242 retval = cmsis_dap_handle->backend->packet_buffer_alloc(cmsis_dap_handle, pkt_sz);
1243 if (retval != ERROR_OK)
1244 goto init_err;
1245
1246 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRIu16, pkt_sz);
1247 }
1248 }
1249
1250 /* Maximal number of transfers which fit to one packet:
1251 * Limited by response size: 3 bytes of response header + 4 per read
1252 * Plus writes to full command size: 3 bytes cmd header + 1 per read + 5 per write */
1253 tfer_max_command_size = cmsis_dap_handle->packet_usable_size;
1254 tfer_max_response_size = cmsis_dap_handle->packet_usable_size;
1255 unsigned int max_reads = tfer_max_response_size / 4;
1256 pending_queue_len = max_reads + (tfer_max_command_size - max_reads) / 5;
1257 cmsis_dap_handle->write_count = 0;
1258 cmsis_dap_handle->read_count = 0;
1259
1260 /* INFO_ID_PKT_CNT - byte */
1261 retval = cmsis_dap_cmd_dap_info(INFO_ID_PKT_CNT, &data);
1262 if (retval != ERROR_OK)
1263 goto init_err;
1264
1265 if (data[0] == 1) { /* byte */
1266 unsigned int pkt_cnt = data[1];
1267 if (pkt_cnt > 1)
1268 cmsis_dap_handle->packet_count = MIN(MAX_PENDING_REQUESTS, pkt_cnt);
1269
1270 LOG_DEBUG("CMSIS-DAP: Packet Count = %u", pkt_cnt);
1271 }
1272
1273 LOG_DEBUG("Allocating FIFO for %u pending packets", cmsis_dap_handle->packet_count);
1274 for (unsigned int i = 0; i < cmsis_dap_handle->packet_count; i++) {
1275 cmsis_dap_handle->pending_fifo[i].transfers = malloc(pending_queue_len
1276 * sizeof(struct pending_transfer_result));
1277 if (!cmsis_dap_handle->pending_fifo[i].transfers) {
1278 LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
1279 retval = ERROR_FAIL;
1280 goto init_err;
1281 }
1282 }
1283
1284 /* Intentionally not checked for error, just logs an info message
1285 * not vital for further debugging */
1286 (void)cmsis_dap_get_status();
1287
1288 /* Now try to connect to the target
1289 * TODO: This is all SWD only @ present */
1290 retval = cmsis_dap_cmd_dap_swj_clock(adapter_get_speed_khz());
1291 if (retval != ERROR_OK)
1292 goto init_err;
1293
1294 /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
1295 * up to 64 times. This must be changed to 0 if sticky
1296 * overrun detection is enabled. */
1297 retval = cmsis_dap_cmd_dap_tfer_configure(0, 64, 0);
1298 if (retval != ERROR_OK)
1299 goto init_err;
1300
1301 if (swd_mode) {
1302 /* Data Phase (bit 2) must be set to 1 if sticky overrun
1303 * detection is enabled */
1304 retval = cmsis_dap_cmd_dap_swd_configure(0); /* 1 TRN, no Data Phase */
1305 if (retval != ERROR_OK)
1306 goto init_err;
1307 }
1308 /* Both LEDs on */
1309 /* Intentionally not checked for error, debugging will work
1310 * without LEDs */
1311 (void)cmsis_dap_cmd_dap_led(LED_ID_CONNECT, LED_ON);
1312 (void)cmsis_dap_cmd_dap_led(LED_ID_RUN, LED_ON);
1313
1314 /* support connecting with srst asserted */
1315 enum reset_types jtag_reset_config = jtag_get_reset_config();
1316
1317 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1318 if (jtag_reset_config & RESET_SRST_NO_GATING) {
1319 retval = cmsis_dap_cmd_dap_swj_pins(0, SWJ_PIN_SRST, 0, NULL);
1320 if (retval != ERROR_OK)
1321 goto init_err;
1322 LOG_INFO("Connecting under reset");
1323 }
1324 }
1325 LOG_INFO("CMSIS-DAP: Interface ready");
1326 return ERROR_OK;
1327
1328 init_err:
1329 cmsis_dap_quit();
1330 return retval;
1331 }
1332
1333 static int cmsis_dap_swd_init(void)
1334 {
1335 swd_mode = true;
1336 return ERROR_OK;
1337 }
1338
1339 static int cmsis_dap_quit(void)
1340 {
1341 cmsis_dap_cmd_dap_disconnect();
1342
1343 /* Both LEDs off */
1344 cmsis_dap_cmd_dap_led(LED_ID_RUN, LED_OFF);
1345 cmsis_dap_cmd_dap_led(LED_ID_CONNECT, LED_OFF);
1346
1347 cmsis_dap_close(cmsis_dap_handle);
1348
1349 return ERROR_OK;
1350 }
1351
1352 static int cmsis_dap_reset(int trst, int srst)
1353 {
1354 /* Set both TRST and SRST even if they're not enabled as
1355 * there's no way to tristate them */
1356
1357 output_pins = 0;
1358 if (!srst)
1359 output_pins |= SWJ_PIN_SRST;
1360 if (!trst)
1361 output_pins |= SWJ_PIN_TRST;
1362
1363 int retval = cmsis_dap_cmd_dap_swj_pins(output_pins,
1364 SWJ_PIN_TRST | SWJ_PIN_SRST, 0, NULL);
1365 if (retval != ERROR_OK)
1366 LOG_ERROR("CMSIS-DAP: Interface reset failed");
1367 return retval;
1368 }
1369
1370 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
1371 {
1372 #if 0
1373 int retval = cmsis_dap_cmd_dap_delay(cmd->cmd.sleep->us);
1374 if (retval != ERROR_OK)
1375 #endif
1376 jtag_sleep(cmd->cmd.sleep->us);
1377 }
1378
1379 /* Set TMS high for five TCK clocks, to move the TAP to the Test-Logic-Reset state */
1380 static int cmsis_dap_execute_tlr_reset(struct jtag_command *cmd)
1381 {
1382 LOG_INFO("cmsis-dap JTAG TLR_RESET");
1383 uint8_t seq = 0xff;
1384
1385 int retval = cmsis_dap_cmd_dap_swj_sequence(8, &seq);
1386 if (retval == ERROR_OK)
1387 tap_set_state(TAP_RESET);
1388 return retval;
1389 }
1390
1391 /* Set new end state */
1392 static void cmsis_dap_end_state(tap_state_t state)
1393 {
1394 if (tap_is_state_stable(state))
1395 tap_set_end_state(state);
1396 else {
1397 LOG_ERROR("BUG: %i is not a valid end state", state);
1398 exit(-1);
1399 }
1400 }
1401
1402 #ifdef SPRINT_BINARY
1403 static void sprint_binary(char *s, const uint8_t *buf, unsigned int offset, unsigned int len)
1404 {
1405 if (!len)
1406 return;
1407
1408 /*
1409 buf = { 0x18 } len=5 should result in: 11000
1410 buf = { 0xff 0x18 } len=13 should result in: 11111111 11000
1411 buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
1412 i=3 there means i/8 = 0 so c = 0xFF, and
1413 */
1414 for (unsigned int i = offset; i < offset + len; ++i) {
1415 uint8_t c = buf[i / 8], mask = 1 << (i % 8);
1416 if ((i != offset) && !(i % 8))
1417 putchar(' ');
1418 *s++ = (c & mask) ? '1' : '0';
1419 }
1420 *s = 0;
1421 }
1422 #endif
1423
1424 #ifdef CMSIS_DAP_JTAG_DEBUG
1425 static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
1426 {
1427 /* cmd is a usb packet to go to the cmsis-dap interface */
1428 printf("cmsis-dap buffer (%d b): ", cmdlen);
1429 for (int i = 0; i < cmdlen; ++i)
1430 printf(" %02x", cmd[i]);
1431 printf("\n");
1432 switch (cmd[0]) {
1433 case CMD_DAP_JTAG_SEQ: {
1434 printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[0], cmd[1]);
1435 /*
1436 * #1 = number of sequences
1437 * #2 = sequence info 1
1438 * #3...4+n_bytes-1 = sequence 1
1439 * #4+n_bytes = sequence info 2
1440 * #5+n_bytes = sequence 2 (single bit)
1441 */
1442 int pos = 2;
1443 for (int seq = 0; seq < cmd[1]; ++seq) {
1444 uint8_t info = cmd[pos++];
1445 int len = info & DAP_JTAG_SEQ_TCK;
1446 if (len == 0)
1447 len = 64;
1448 printf(" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
1449 seq, pos, info, len, info & DAP_JTAG_SEQ_TMS, info & DAP_JTAG_SEQ_TDO);
1450 for (int i = 0; i < DIV_ROUND_UP(len, 8); ++i)
1451 printf(" %02x", cmd[pos+i]);
1452 pos += DIV_ROUND_UP(len, 8);
1453 printf("\n");
1454 }
1455 if (pos != cmdlen) {
1456 printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
1457 exit(-1);
1458 }
1459
1460 break;
1461 }
1462 default:
1463 LOG_DEBUG("unknown cmsis-dap command %02x", cmd[1]);
1464 break;
1465 }
1466 }
1467 #endif
1468
1469 static void cmsis_dap_flush(void)
1470 {
1471 if (!queued_seq_count)
1472 return;
1473
1474 LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
1475 queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
1476
1477 /* prepare CMSIS-DAP packet */
1478 uint8_t *command = cmsis_dap_handle->command;
1479 command[0] = CMD_DAP_JTAG_SEQ;
1480 command[1] = queued_seq_count;
1481 memcpy(&command[2], queued_seq_buf, queued_seq_buf_end);
1482
1483 #ifdef CMSIS_DAP_JTAG_DEBUG
1484 debug_parse_cmsis_buf(command, queued_seq_buf_end + 2);
1485 #endif
1486
1487 /* send command to USB device */
1488 int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 2);
1489
1490 uint8_t *resp = cmsis_dap_handle->response;
1491 if (retval != ERROR_OK || resp[1] != DAP_OK) {
1492 LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
1493 exit(-1);
1494 }
1495
1496 #ifdef CMSIS_DAP_JTAG_DEBUG
1497 LOG_DEBUG_IO("USB response buf:");
1498 for (int c = 0; c < queued_seq_buf_end + 3; ++c)
1499 printf("%02X ", resp[c]);
1500 printf("\n");
1501 #endif
1502
1503 /* copy scan results into client buffers */
1504 for (int i = 0; i < pending_scan_result_count; ++i) {
1505 struct pending_scan_result *scan = &pending_scan_results[i];
1506 LOG_DEBUG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
1507 i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
1508 #ifdef CMSIS_DAP_JTAG_DEBUG
1509 for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
1510 printf("%02X ", resp[2+scan->first+b]);
1511 printf("\n");
1512 #endif
1513 bit_copy(scan->buffer, scan->buffer_offset, &resp[2 + scan->first], 0, scan->length);
1514 }
1515
1516 /* reset */
1517 queued_seq_count = 0;
1518 queued_seq_buf_end = 0;
1519 queued_seq_tdo_ptr = 0;
1520 pending_scan_result_count = 0;
1521 }
1522
1523 /* queue a sequence of bits to clock out TDI / in TDO, executing if the buffer is full.
1524 *
1525 * sequence=NULL means clock out zeros on TDI
1526 * tdo_buffer=NULL means don't capture TDO
1527 */
1528 static void cmsis_dap_add_jtag_sequence(unsigned int s_len, const uint8_t *sequence,
1529 unsigned int s_offset, bool tms,
1530 uint8_t *tdo_buffer, unsigned int tdo_buffer_offset)
1531 {
1532 LOG_DEBUG_IO("[at %d] %u bits, tms %s, seq offset %u, tdo buf %p, tdo offset %u",
1533 queued_seq_buf_end,
1534 s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
1535
1536 if (s_len == 0)
1537 return;
1538
1539 if (s_len > 64) {
1540 LOG_DEBUG_IO("START JTAG SEQ SPLIT");
1541 for (unsigned int offset = 0; offset < s_len; offset += 64) {
1542 unsigned int len = s_len - offset;
1543 if (len > 64)
1544 len = 64;
1545 LOG_DEBUG_IO("Splitting long jtag sequence: %u-bit chunk starting at offset %u", len, offset);
1546 cmsis_dap_add_jtag_sequence(
1547 len,
1548 sequence,
1549 s_offset + offset,
1550 tms,
1551 tdo_buffer,
1552 !tdo_buffer ? 0 : (tdo_buffer_offset + offset)
1553 );
1554 }
1555 LOG_DEBUG_IO("END JTAG SEQ SPLIT");
1556 return;
1557 }
1558
1559 unsigned int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
1560 if (queued_seq_count >= 255 || queued_seq_buf_end + cmd_len > QUEUED_SEQ_BUF_LEN)
1561 /* empty out the buffer */
1562 cmsis_dap_flush();
1563
1564 ++queued_seq_count;
1565
1566 /* control byte */
1567 queued_seq_buf[queued_seq_buf_end] =
1568 (tms ? DAP_JTAG_SEQ_TMS : 0) |
1569 (tdo_buffer ? DAP_JTAG_SEQ_TDO : 0) |
1570 (s_len == 64 ? 0 : s_len);
1571
1572 if (sequence)
1573 bit_copy(&queued_seq_buf[queued_seq_buf_end + 1], 0, sequence, s_offset, s_len);
1574 else
1575 memset(&queued_seq_buf[queued_seq_buf_end + 1], 0, DIV_ROUND_UP(s_len, 8));
1576
1577 queued_seq_buf_end += cmd_len;
1578
1579 if (tdo_buffer) {
1580 struct pending_scan_result *scan = &pending_scan_results[pending_scan_result_count++];
1581 scan->first = queued_seq_tdo_ptr;
1582 queued_seq_tdo_ptr += DIV_ROUND_UP(s_len, 8);
1583 scan->length = s_len;
1584 scan->buffer = tdo_buffer;
1585 scan->buffer_offset = tdo_buffer_offset;
1586 }
1587 }
1588
1589 /* queue a sequence of bits to clock out TMS, executing if the buffer is full */
1590 static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
1591 {
1592 LOG_DEBUG_IO("%d bits: %02X", s_len, *sequence);
1593 /* we use a series of CMD_DAP_JTAG_SEQ commands to toggle TMS,
1594 because even though it seems ridiculously inefficient, it
1595 allows us to combine TMS and scan sequences into the same
1596 USB packet. */
1597 /* TODO: combine runs of the same tms value */
1598 for (int i = 0; i < s_len; ++i) {
1599 bool bit = (sequence[i / 8] & (1 << (i % 8))) != 0;
1600 cmsis_dap_add_jtag_sequence(1, NULL, 0, bit, NULL, 0);
1601 }
1602 }
1603
1604 /* Move to the end state by queuing a sequence to clock into TMS */
1605 static void cmsis_dap_state_move(void)
1606 {
1607 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1608 uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1609
1610 LOG_DEBUG_IO("state move from %s to %s: %d clocks, %02X on tms",
1611 tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()),
1612 tms_scan_bits, tms_scan);
1613 cmsis_dap_add_tms_sequence(&tms_scan, tms_scan_bits);
1614
1615 tap_set_state(tap_get_end_state());
1616 }
1617
1618
1619 /* Execute a JTAG scan operation by queueing TMS and TDI/TDO sequences */
1620 static void cmsis_dap_execute_scan(struct jtag_command *cmd)
1621 {
1622 LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
1623 jtag_scan_type(cmd->cmd.scan));
1624
1625 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
1626 while (cmd->cmd.scan->num_fields > 0
1627 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
1628 cmd->cmd.scan->num_fields--;
1629 LOG_DEBUG("discarding trailing empty field");
1630 }
1631
1632 if (cmd->cmd.scan->num_fields == 0) {
1633 LOG_DEBUG("empty scan, doing nothing");
1634 return;
1635 }
1636
1637 if (cmd->cmd.scan->ir_scan) {
1638 if (tap_get_state() != TAP_IRSHIFT) {
1639 cmsis_dap_end_state(TAP_IRSHIFT);
1640 cmsis_dap_state_move();
1641 }
1642 } else {
1643 if (tap_get_state() != TAP_DRSHIFT) {
1644 cmsis_dap_end_state(TAP_DRSHIFT);
1645 cmsis_dap_state_move();
1646 }
1647 }
1648
1649 cmsis_dap_end_state(cmd->cmd.scan->end_state);
1650
1651 struct scan_field *field = cmd->cmd.scan->fields;
1652 unsigned scan_size = 0;
1653
1654 for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
1655 scan_size += field->num_bits;
1656 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
1657 field->in_value ? "in" : "",
1658 field->out_value ? "out" : "",
1659 i,
1660 cmd->cmd.scan->num_fields,
1661 field->num_bits);
1662
1663 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
1664 LOG_DEBUG_IO("Last field and have to move out of SHIFT state");
1665 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
1666 * movement. This last field can't have length zero, it was checked above. */
1667 cmsis_dap_add_jtag_sequence(
1668 field->num_bits - 1, /* number of bits to clock */
1669 field->out_value, /* output sequence */
1670 0, /* output offset */
1671 false, /* TMS low */
1672 field->in_value,
1673 0);
1674
1675 /* Clock the last bit out, with TMS high */
1676 uint8_t last_bit = 0;
1677 if (field->out_value)
1678 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
1679 cmsis_dap_add_jtag_sequence(
1680 1,
1681 &last_bit,
1682 0,
1683 true,
1684 field->in_value,
1685 field->num_bits - 1);
1686 tap_set_state(tap_state_transition(tap_get_state(), 1));
1687
1688 /* Now clock one more cycle, with TMS low, to get us into a PAUSE state */
1689 cmsis_dap_add_jtag_sequence(
1690 1,
1691 &last_bit,
1692 0,
1693 false,
1694 NULL,
1695 0);
1696 tap_set_state(tap_state_transition(tap_get_state(), 0));
1697 } else {
1698 LOG_DEBUG_IO("Internal field, staying in SHIFT state afterwards");
1699 /* Clocking part of a sequence into DR or IR with TMS=0,
1700 leaving TMS=0 at the end so we can continue later */
1701 cmsis_dap_add_jtag_sequence(
1702 field->num_bits,
1703 field->out_value,
1704 0,
1705 false,
1706 field->in_value,
1707 0);
1708 }
1709 }
1710
1711 if (tap_get_state() != tap_get_end_state()) {
1712 cmsis_dap_end_state(tap_get_end_state());
1713 cmsis_dap_state_move();
1714 }
1715
1716 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
1717 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1718 tap_state_name(tap_get_end_state()));
1719 }
1720
1721 static void cmsis_dap_pathmove(int num_states, tap_state_t *path)
1722 {
1723 uint8_t tms0 = 0x00;
1724 uint8_t tms1 = 0xff;
1725
1726 for (int i = 0; i < num_states; i++) {
1727 if (path[i] == tap_state_transition(tap_get_state(), false))
1728 cmsis_dap_add_tms_sequence(&tms0, 1);
1729 else if (path[i] == tap_state_transition(tap_get_state(), true))
1730 cmsis_dap_add_tms_sequence(&tms1, 1);
1731 else {
1732 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
1733 tap_state_name(tap_get_state()), tap_state_name(path[i]));
1734 exit(-1);
1735 }
1736
1737 tap_set_state(path[i]);
1738 }
1739
1740 cmsis_dap_end_state(tap_get_state());
1741 }
1742
1743 static void cmsis_dap_execute_pathmove(struct jtag_command *cmd)
1744 {
1745 LOG_DEBUG_IO("pathmove: %i states, end in %i",
1746 cmd->cmd.pathmove->num_states,
1747 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1748
1749 cmsis_dap_pathmove(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
1750 }
1751
1752 static void cmsis_dap_stableclocks(int num_cycles)
1753 {
1754 uint8_t tms = tap_get_state() == TAP_RESET;
1755 /* TODO: Perform optimizations? */
1756 /* Execute num_cycles. */
1757 for (int i = 0; i < num_cycles; i++)
1758 cmsis_dap_add_tms_sequence(&tms, 1);
1759 }
1760
1761 static void cmsis_dap_runtest(int num_cycles)
1762 {
1763 tap_state_t saved_end_state = tap_get_end_state();
1764
1765 /* Only do a state_move when we're not already in IDLE. */
1766 if (tap_get_state() != TAP_IDLE) {
1767 cmsis_dap_end_state(TAP_IDLE);
1768 cmsis_dap_state_move();
1769 }
1770 cmsis_dap_stableclocks(num_cycles);
1771
1772 /* Finish in end_state. */
1773 cmsis_dap_end_state(saved_end_state);
1774
1775 if (tap_get_state() != tap_get_end_state())
1776 cmsis_dap_state_move();
1777 }
1778
1779 static void cmsis_dap_execute_runtest(struct jtag_command *cmd)
1780 {
1781 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
1782 cmd->cmd.runtest->end_state);
1783
1784 cmsis_dap_end_state(cmd->cmd.runtest->end_state);
1785 cmsis_dap_runtest(cmd->cmd.runtest->num_cycles);
1786 }
1787
1788 static void cmsis_dap_execute_stableclocks(struct jtag_command *cmd)
1789 {
1790 LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
1791 cmsis_dap_stableclocks(cmd->cmd.runtest->num_cycles);
1792 }
1793
1794 static void cmsis_dap_execute_tms(struct jtag_command *cmd)
1795 {
1796 LOG_DEBUG_IO("TMS: %d bits", cmd->cmd.tms->num_bits);
1797 cmsis_dap_cmd_dap_swj_sequence(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
1798 }
1799
1800 /* TODO: Is there need to call cmsis_dap_flush() for the JTAG_PATHMOVE,
1801 * JTAG_RUNTEST, JTAG_STABLECLOCKS? */
1802 static void cmsis_dap_execute_command(struct jtag_command *cmd)
1803 {
1804 switch (cmd->type) {
1805 case JTAG_SLEEP:
1806 cmsis_dap_flush();
1807 cmsis_dap_execute_sleep(cmd);
1808 break;
1809 case JTAG_TLR_RESET:
1810 cmsis_dap_flush();
1811 cmsis_dap_execute_tlr_reset(cmd);
1812 break;
1813 case JTAG_SCAN:
1814 cmsis_dap_execute_scan(cmd);
1815 break;
1816 case JTAG_PATHMOVE:
1817 cmsis_dap_execute_pathmove(cmd);
1818 break;
1819 case JTAG_RUNTEST:
1820 cmsis_dap_execute_runtest(cmd);
1821 break;
1822 case JTAG_STABLECLOCKS:
1823 cmsis_dap_execute_stableclocks(cmd);
1824 break;
1825 case JTAG_TMS:
1826 cmsis_dap_execute_tms(cmd);
1827 break;
1828 default:
1829 LOG_ERROR("BUG: unknown JTAG command type 0x%X encountered", cmd->type);
1830 exit(-1);
1831 }
1832 }
1833
1834 static int cmsis_dap_execute_queue(void)
1835 {
1836 struct jtag_command *cmd = jtag_command_queue;
1837
1838 while (cmd) {
1839 cmsis_dap_execute_command(cmd);
1840 cmd = cmd->next;
1841 }
1842
1843 cmsis_dap_flush();
1844
1845 return ERROR_OK;
1846 }
1847
1848 static int cmsis_dap_speed(int speed)
1849 {
1850 if (speed == 0) {
1851 LOG_ERROR("RTCK not supported. Set nonzero \"adapter speed\".");
1852 return ERROR_JTAG_NOT_IMPLEMENTED;
1853 }
1854
1855 return cmsis_dap_cmd_dap_swj_clock(speed);
1856 }
1857
1858 static int cmsis_dap_speed_div(int speed, int *khz)
1859 {
1860 *khz = speed;
1861 return ERROR_OK;
1862 }
1863
1864 static int cmsis_dap_khz(int khz, int *jtag_speed)
1865 {
1866 *jtag_speed = khz;
1867 return ERROR_OK;
1868 }
1869
1870 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1871 uint32_t trace_freq, uint16_t *prescaler)
1872 {
1873 unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1874 if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1875 return false;
1876
1877 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1878 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1879 if (presc * trace_freq < traceclkin_freq - max_deviation ||
1880 presc * trace_freq > traceclkin_freq + max_deviation)
1881 return false;
1882
1883 *prescaler = presc;
1884
1885 return true;
1886 }
1887
1888 /**
1889 * @see adapter_driver::config_trace
1890 */
1891 static int cmsis_dap_config_trace(
1892 bool trace_enabled,
1893 enum tpiu_pin_protocol pin_protocol,
1894 uint32_t port_size,
1895 unsigned int *swo_freq,
1896 unsigned int traceclkin_hz,
1897 uint16_t *swo_prescaler)
1898 {
1899 int retval;
1900
1901 if (!trace_enabled) {
1902 if (cmsis_dap_handle->trace_enabled) {
1903 retval = cmsis_dap_cmd_dap_swo_control(DAP_SWO_CONTROL_STOP);
1904 if (retval != ERROR_OK) {
1905 LOG_ERROR("Failed to disable the SWO-trace.");
1906 return retval;
1907 }
1908 }
1909 cmsis_dap_handle->trace_enabled = false;
1910 LOG_INFO("SWO-trace disabled.");
1911 return ERROR_OK;
1912 }
1913
1914 if (!(cmsis_dap_handle->caps & INFO_CAPS_SWO_UART) &&
1915 !(cmsis_dap_handle->caps & INFO_CAPS_SWO_MANCHESTER)) {
1916 LOG_ERROR("SWO-trace is not supported by the device.");
1917 return ERROR_FAIL;
1918 }
1919
1920 uint8_t swo_mode;
1921 if (pin_protocol == TPIU_PIN_PROTOCOL_ASYNC_UART &&
1922 (cmsis_dap_handle->caps & INFO_CAPS_SWO_UART)) {
1923 swo_mode = DAP_SWO_MODE_UART;
1924 } else if (pin_protocol == TPIU_PIN_PROTOCOL_ASYNC_MANCHESTER &&
1925 (cmsis_dap_handle->caps & INFO_CAPS_SWO_MANCHESTER)) {
1926 swo_mode = DAP_SWO_MODE_MANCHESTER;
1927 } else {
1928 LOG_ERROR("Selected pin protocol is not supported.");
1929 return ERROR_FAIL;
1930 }
1931
1932 if (*swo_freq == 0) {
1933 LOG_INFO("SWO-trace frequency autodetection not implemented.");
1934 return ERROR_FAIL;
1935 }
1936
1937 retval = cmsis_dap_cmd_dap_swo_control(DAP_SWO_CONTROL_STOP);
1938 if (retval != ERROR_OK)
1939 return retval;
1940
1941 cmsis_dap_handle->trace_enabled = false;
1942
1943 retval = cmsis_dap_get_swo_buf_sz(&cmsis_dap_handle->swo_buf_sz);
1944 if (retval != ERROR_OK)
1945 return retval;
1946
1947 retval = cmsis_dap_cmd_dap_swo_transport(DAP_SWO_TRANSPORT_DATA);
1948 if (retval != ERROR_OK)
1949 return retval;
1950
1951 retval = cmsis_dap_cmd_dap_swo_mode(swo_mode);
1952 if (retval != ERROR_OK)
1953 return retval;
1954
1955 retval = cmsis_dap_cmd_dap_swo_baudrate(*swo_freq, swo_freq);
1956 if (retval != ERROR_OK)
1957 return retval;
1958
1959 if (!calculate_swo_prescaler(traceclkin_hz, *swo_freq,
1960 swo_prescaler)) {
1961 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1962 "different frequency or use auto-detection.");
1963 return ERROR_FAIL;
1964 }
1965
1966 LOG_INFO("SWO frequency: %u Hz.", *swo_freq);
1967 LOG_INFO("SWO prescaler: %u.", *swo_prescaler);
1968
1969 retval = cmsis_dap_cmd_dap_swo_control(DAP_SWO_CONTROL_START);
1970 if (retval != ERROR_OK)
1971 return retval;
1972
1973 cmsis_dap_handle->trace_enabled = true;
1974
1975 return ERROR_OK;
1976 }
1977
1978 /**
1979 * @see adapter_driver::poll_trace
1980 */
1981 static int cmsis_dap_poll_trace(uint8_t *buf, size_t *size)
1982 {
1983 uint8_t trace_status;
1984 size_t trace_count;
1985
1986 if (!cmsis_dap_handle->trace_enabled) {
1987 *size = 0;
1988 return ERROR_OK;
1989 }
1990
1991 int retval = cmsis_dap_cmd_dap_swo_status(&trace_status, &trace_count);
1992 if (retval != ERROR_OK)
1993 return retval;
1994 if ((trace_status & DAP_SWO_STATUS_CAPTURE_MASK) != DAP_SWO_STATUS_CAPTURE_ACTIVE)
1995 return ERROR_FAIL;
1996
1997 *size = trace_count < *size ? trace_count : *size;
1998 size_t read_so_far = 0;
1999 do {
2000 size_t rb = 0;
2001 uint32_t packet_size = cmsis_dap_handle->packet_size - 4 /*data-reply*/;
2002 uint32_t remaining = *size - read_so_far;
2003 if (remaining < packet_size)
2004 packet_size = remaining;
2005 retval = cmsis_dap_cmd_dap_swo_data(
2006 packet_size,
2007 &trace_status,
2008 &rb,
2009 &buf[read_so_far]);
2010 if (retval != ERROR_OK)
2011 return retval;
2012 if ((trace_status & DAP_SWO_STATUS_CAPTURE_MASK) != DAP_SWO_STATUS_CAPTURE_ACTIVE)
2013 return ERROR_FAIL;
2014
2015 read_so_far += rb;
2016 } while (read_so_far < *size);
2017
2018 return ERROR_OK;
2019 }
2020
2021 COMMAND_HANDLER(cmsis_dap_handle_info_command)
2022 {
2023 if (cmsis_dap_get_version_info() == ERROR_OK)
2024 cmsis_dap_get_status();
2025
2026 return ERROR_OK;
2027 }
2028
2029 COMMAND_HANDLER(cmsis_dap_handle_cmd_command)
2030 {
2031 uint8_t *command = cmsis_dap_handle->command;
2032
2033 for (unsigned i = 0; i < CMD_ARGC; i++)
2034 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[i], command[i]);
2035
2036 int retval = cmsis_dap_xfer(cmsis_dap_handle, CMD_ARGC);
2037
2038 if (retval != ERROR_OK) {
2039 LOG_ERROR("CMSIS-DAP command failed.");
2040 return ERROR_JTAG_DEVICE_ERROR;
2041 }
2042
2043 uint8_t *resp = cmsis_dap_handle->response;
2044 LOG_INFO("Returned data %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8,
2045 resp[1], resp[2], resp[3], resp[4]);
2046
2047 return ERROR_OK;
2048 }
2049
2050 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
2051 {
2052 if (CMD_ARGC > MAX_USB_IDS * 2) {
2053 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
2054 "(maximum is %d pairs)", MAX_USB_IDS);
2055 CMD_ARGC = MAX_USB_IDS * 2;
2056 }
2057 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
2058 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
2059 if (CMD_ARGC < 2)
2060 return ERROR_COMMAND_SYNTAX_ERROR;
2061 /* remove the incomplete trailing id */
2062 CMD_ARGC -= 1;
2063 }
2064
2065 unsigned i;
2066 for (i = 0; i < CMD_ARGC; i += 2) {
2067 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
2068 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
2069 }
2070
2071 /*
2072 * Explicitly terminate, in case there are multiples instances of
2073 * cmsis_dap_vid_pid.
2074 */
2075 cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
2076
2077 return ERROR_OK;
2078 }
2079
2080 COMMAND_HANDLER(cmsis_dap_handle_backend_command)
2081 {
2082 if (CMD_ARGC == 1) {
2083 if (strcmp(CMD_ARGV[0], "auto") == 0) {
2084 cmsis_dap_backend = -1; /* autoselect */
2085 } else {
2086 for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
2087 if (strcasecmp(cmsis_dap_backends[i]->name, CMD_ARGV[0]) == 0) {
2088 cmsis_dap_backend = i;
2089 return ERROR_OK;
2090 }
2091 }
2092
2093 LOG_ERROR("invalid backend argument to cmsis_dap_backend <backend>");
2094 }
2095 } else {
2096 LOG_ERROR("expected exactly one argument to cmsis_dap_backend <backend>");
2097 }
2098
2099 return ERROR_OK;
2100 }
2101
2102 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
2103 {
2104 .name = "info",
2105 .handler = &cmsis_dap_handle_info_command,
2106 .mode = COMMAND_EXEC,
2107 .usage = "",
2108 .help = "show cmsis-dap info",
2109 },
2110 {
2111 .name = "cmd",
2112 .handler = &cmsis_dap_handle_cmd_command,
2113 .mode = COMMAND_EXEC,
2114 .usage = "",
2115 .help = "issue cmsis-dap command",
2116 },
2117 COMMAND_REGISTRATION_DONE
2118 };
2119
2120
2121 static const struct command_registration cmsis_dap_command_handlers[] = {
2122 {
2123 .name = "cmsis-dap",
2124 .mode = COMMAND_ANY,
2125 .help = "perform CMSIS-DAP management",
2126 .usage = "<cmd>",
2127 .chain = cmsis_dap_subcommand_handlers,
2128 },
2129 {
2130 .name = "cmsis_dap_vid_pid",
2131 .handler = &cmsis_dap_handle_vid_pid_command,
2132 .mode = COMMAND_CONFIG,
2133 .help = "the vendor ID and product ID of the CMSIS-DAP device",
2134 .usage = "(vid pid)*",
2135 },
2136 {
2137 .name = "cmsis_dap_backend",
2138 .handler = &cmsis_dap_handle_backend_command,
2139 .mode = COMMAND_CONFIG,
2140 .help = "set the communication backend to use (USB bulk or HID).",
2141 .usage = "(auto | usb_bulk | hid)",
2142 },
2143 #if BUILD_CMSIS_DAP_USB
2144 {
2145 .name = "cmsis_dap_usb",
2146 .chain = cmsis_dap_usb_subcommand_handlers,
2147 .mode = COMMAND_ANY,
2148 .help = "USB bulk backend-specific commands",
2149 .usage = "<cmd>",
2150 },
2151 #endif
2152 COMMAND_REGISTRATION_DONE
2153 };
2154
2155 static const struct swd_driver cmsis_dap_swd_driver = {
2156 .init = cmsis_dap_swd_init,
2157 .switch_seq = cmsis_dap_swd_switch_seq,
2158 .read_reg = cmsis_dap_swd_read_reg,
2159 .write_reg = cmsis_dap_swd_write_reg,
2160 .run = cmsis_dap_swd_run_queue,
2161 };
2162
2163 static const char * const cmsis_dap_transport[] = { "swd", "jtag", NULL };
2164
2165 static struct jtag_interface cmsis_dap_interface = {
2166 .supported = DEBUG_CAP_TMS_SEQ,
2167 .execute_queue = cmsis_dap_execute_queue,
2168 };
2169
2170 struct adapter_driver cmsis_dap_adapter_driver = {
2171 .name = "cmsis-dap",
2172 .transports = cmsis_dap_transport,
2173 .commands = cmsis_dap_command_handlers,
2174
2175 .init = cmsis_dap_init,
2176 .quit = cmsis_dap_quit,
2177 .reset = cmsis_dap_reset,
2178 .speed = cmsis_dap_speed,
2179 .khz = cmsis_dap_khz,
2180 .speed_div = cmsis_dap_speed_div,
2181 .config_trace = cmsis_dap_config_trace,
2182 .poll_trace = cmsis_dap_poll_trace,
2183
2184 .jtag_ops = &cmsis_dap_interface,
2185 .swd_ops = &cmsis_dap_swd_driver,
2186 };

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)