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

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)