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

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)