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