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