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, 10, CMSIS_DAP_BLOCKING);
340  if (retval == ERROR_TIMEOUT_REACHED)
341  break;
342  }
343  if (i)
344  LOG_DEBUG("Flushed %u packets", i);
345 }
346 
347 /* Send a message and receive the reply */
348 static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
349 {
350  if (dap->write_count + dap->read_count) {
351  LOG_ERROR("internal: queue not empty before xfer");
352  }
353  if (dap->pending_fifo_block_count) {
354  LOG_ERROR("pending %u blocks, flushing", dap->pending_fifo_block_count);
355  while (dap->pending_fifo_block_count) {
356  dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
358  }
359  dap->pending_fifo_put_idx = 0;
360  dap->pending_fifo_get_idx = 0;
361  }
362 
363  uint8_t current_cmd = dap->command[0];
364  int retval = dap->backend->write(dap, txlen, TIMEOUT_MS);
365  if (retval < 0)
366  return retval;
367 
368  /* get reply */
369  retval = dap->backend->read(dap, TIMEOUT_MS, CMSIS_DAP_BLOCKING);
370  if (retval < 0)
371  return retval;
372 
373  uint8_t *resp = dap->response;
374  if (resp[0] == DAP_ERROR) {
375  LOG_ERROR("CMSIS-DAP command 0x%" PRIx8 " not implemented", current_cmd);
376  return ERROR_NOT_IMPLEMENTED;
377  }
378 
379  if (resp[0] != current_cmd) {
380  LOG_ERROR("CMSIS-DAP command mismatch. Sent 0x%" PRIx8
381  " received 0x%" PRIx8, current_cmd, resp[0]);
382 
383  dap->backend->cancel_all(dap);
385  return ERROR_FAIL;
386  }
387 
388  return ERROR_OK;
389 }
390 
391 static int cmsis_dap_cmd_dap_swj_pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
392 {
393  uint8_t *command = cmsis_dap_handle->command;
394 
396  command[1] = pins;
397  command[2] = mask;
398  h_u32_to_le(&command[3], delay);
399 
400  int retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
401  if (retval != ERROR_OK) {
402  LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
404  }
405 
406  if (input)
407  *input = cmsis_dap_handle->response[1];
408 
409  return ERROR_OK;
410 }
411 
412 static int cmsis_dap_cmd_dap_swj_clock(uint32_t swj_clock)
413 {
414  uint8_t *command = cmsis_dap_handle->command;
415 
416  /* set clock in Hz */
417  swj_clock *= 1000;
418 
420  h_u32_to_le(&command[1], swj_clock);
421 
422  int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
423  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
424  LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
426  }
427 
428  return ERROR_OK;
429 }
430 
431 /* clock a sequence of bits out on TMS, to change JTAG states */
432 static int cmsis_dap_cmd_dap_swj_sequence(uint8_t s_len, const uint8_t *sequence)
433 {
434  uint8_t *command = cmsis_dap_handle->command;
435 
436 #ifdef CMSIS_DAP_JTAG_DEBUG
437  LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
438  for (unsigned int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
439  printf("%02X ", sequence[i]);
440 
441  printf("\n");
442 #endif
443 
445  command[1] = s_len;
446  bit_copy(&command[2], 0, sequence, 0, s_len);
447 
448  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2 + DIV_ROUND_UP(s_len, 8));
449  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK)
450  return ERROR_FAIL;
451 
452  return ERROR_OK;
453 }
454 
455 static int cmsis_dap_cmd_dap_info(uint8_t info, uint8_t **data)
456 {
457  uint8_t *command = cmsis_dap_handle->command;
458 
459  command[0] = CMD_DAP_INFO;
460  command[1] = info;
461 
462  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
463  if (retval != ERROR_OK) {
464  LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
466  }
467 
468  *data = &cmsis_dap_handle->response[1];
469 
470  return ERROR_OK;
471 }
472 
473 static int cmsis_dap_cmd_dap_led(uint8_t led, uint8_t state)
474 {
475  uint8_t *command = cmsis_dap_handle->command;
476 
477  command[0] = CMD_DAP_LED;
478  command[1] = led;
479  command[2] = state;
480 
481  int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
482  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
483  LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
485  }
486 
487  return ERROR_OK;
488 }
489 
490 static int cmsis_dap_cmd_dap_connect(uint8_t mode)
491 {
492  uint8_t *command = cmsis_dap_handle->command;
493 
495  command[1] = mode;
496 
497  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
498  if (retval != ERROR_OK) {
499  LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
501  }
502 
503  if (cmsis_dap_handle->response[1] != mode) {
504  LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
506  }
507 
508  return ERROR_OK;
509 }
510 
512 {
513  uint8_t *command = cmsis_dap_handle->command;
514 
516 
517  int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
518  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
519  LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
521  }
522 
523  return ERROR_OK;
524 }
525 
526 static int cmsis_dap_cmd_dap_tfer_configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
527 {
528  uint8_t *command = cmsis_dap_handle->command;
529 
531  command[1] = idle;
532  h_u16_to_le(&command[2], retry_count);
533  h_u16_to_le(&command[4], match_retry);
534 
535  int retval = cmsis_dap_xfer(cmsis_dap_handle, 6);
536  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
537  LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
539  }
540 
541  return ERROR_OK;
542 }
543 
544 static int cmsis_dap_cmd_dap_swd_configure(uint8_t cfg)
545 {
546  uint8_t *command = cmsis_dap_handle->command;
547 
549  command[1] = cfg;
550 
551  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
552  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
553  LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
555  }
556 
557  return ERROR_OK;
558 }
559 
560 #if 0
561 static int cmsis_dap_cmd_dap_delay(uint16_t delay_us)
562 {
563  uint8_t *command = cmsis_dap_handle->command;
564 
565  command[0] = CMD_DAP_DELAY;
567 
568  int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
569  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
570  LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
572  }
573 
574  return ERROR_OK;
575 }
576 #endif
577 
578 static int cmsis_dap_metacmd_targetsel(uint32_t instance_id)
579 {
580  uint8_t *command = cmsis_dap_handle->command;
581  const uint32_t seq_rd = 0x80, seq_wr = 0x00;
582 
583  /* SWD multi-drop requires a transfer ala CMD_DAP_TFER,
584  but with no expectation of an SWD ACK response. In
585  CMSIS-DAP v1.20 and v2.00, CMD_DAP_SWD_SEQUENCE was
586  added to allow this special sequence to be generated.
587  The purpose of this operation is to select the target
588  corresponding to the instance_id that is written */
589 
590  LOG_DEBUG_IO("DP write reg TARGETSEL %" PRIx32, instance_id);
591 
592  size_t idx = 0;
593  command[idx++] = CMD_DAP_SWD_SEQUENCE;
594  command[idx++] = 3; /* sequence count */
595 
596  /* sequence 0: packet request for TARGETSEL */
597  command[idx++] = seq_wr | 8;
598  command[idx++] = SWD_CMD_START | swd_cmd(false, false, DP_TARGETSEL) | SWD_CMD_STOP | SWD_CMD_PARK;
599 
600  /* sequence 1: read Trn ACK Trn, no expectation for target to ACK */
601  command[idx++] = seq_rd | 5;
602 
603  /* sequence 2: WDATA plus parity */
604  command[idx++] = seq_wr | (32 + 1);
605  h_u32_to_le(command + idx, instance_id);
606  idx += 4;
607  command[idx++] = parity_u32(instance_id);
608 
609  int retval = cmsis_dap_xfer(cmsis_dap_handle, idx);
610  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
611  LOG_ERROR("CMSIS-DAP command SWD_Sequence failed.");
613  }
614 
615  return ERROR_OK;
616 }
617 
624 {
625  uint8_t *command = cmsis_dap_handle->command;
626 
628  command[1] = transport;
629 
630  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
631  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
632  LOG_ERROR("CMSIS-DAP: command CMD_SWO_Transport(%d) failed.", transport);
634  }
635 
636  return ERROR_OK;
637 }
638 
643 static int cmsis_dap_cmd_dap_swo_mode(uint8_t mode)
644 {
645  uint8_t *command = cmsis_dap_handle->command;
646 
648  command[1] = mode;
649 
650  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
651  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
652  LOG_ERROR("CMSIS-DAP: command CMD_SWO_Mode(%d) failed.", mode);
654  }
655 
656  return ERROR_OK;
657 }
658 
669  uint32_t in_baudrate,
670  uint32_t *dev_baudrate)
671 {
672  uint8_t *command = cmsis_dap_handle->command;
673 
675  h_u32_to_le(&command[1], in_baudrate);
676 
677  int retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
678  uint32_t rvbr = le_to_h_u32(&cmsis_dap_handle->response[1]);
679  if (retval != ERROR_OK || rvbr == 0) {
680  LOG_ERROR("CMSIS-DAP: command CMD_SWO_Baudrate(%u) -> %u failed.", in_baudrate, rvbr);
681  if (dev_baudrate)
682  *dev_baudrate = 0;
684  }
685 
686  if (dev_baudrate)
687  *dev_baudrate = rvbr;
688 
689  return ERROR_OK;
690 }
691 
698 static int cmsis_dap_cmd_dap_swo_control(uint8_t control)
699 {
700  uint8_t *command = cmsis_dap_handle->command;
701 
703  command[1] = control;
704 
705  int retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
706  if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
707  LOG_ERROR("CMSIS-DAP: command CMD_SWO_Control(%d) failed.", control);
709  }
710 
711  return ERROR_OK;
712 }
713 
723  uint8_t *trace_status,
724  size_t *trace_count)
725 {
726  uint8_t *command = cmsis_dap_handle->command;
727 
729 
730  int retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
731  if (retval != ERROR_OK) {
732  LOG_ERROR("CMSIS-DAP: command CMD_SWO_Status failed.");
734  }
735 
736  if (trace_status)
738  if (trace_count)
739  *trace_count = le_to_h_u32(&cmsis_dap_handle->response[2]);
740 
741  return ERROR_OK;
742 }
743 
752  size_t max_trace_count,
753  uint8_t *trace_status,
754  size_t *trace_count,
755  uint8_t *data)
756 {
757  uint8_t *command = cmsis_dap_handle->command;
758 
760  h_u16_to_le(&command[1], max_trace_count);
761 
762  int retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
763  if (retval != ERROR_OK) {
764  LOG_ERROR("CMSIS-DAP: command CMD_SWO_Data failed.");
766  }
767 
769  *trace_count = le_to_h_u16(&cmsis_dap_handle->response[2]);
770 
771  if (*trace_count > 0)
772  memcpy(data, &cmsis_dap_handle->response[4], *trace_count);
773 
774  return ERROR_OK;
775 }
776 
778 {
779  for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++)
780  dap->pending_fifo[i].transfer_count = 0;
781 
782  dap->pending_fifo_put_idx = 0;
783  dap->pending_fifo_get_idx = 0;
784  dap->pending_fifo_block_count = 0;
785 }
786 
788 {
789  dap->backend->cancel_all(dap);
792 }
793 
795 {
796  uint8_t *command = dap->command;
797  struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_put_idx];
798 
799  assert(dap->write_count + dap->read_count == block->transfer_count);
800 
801  /* Reset packet size check counters for the next packet */
802  dap->write_count = 0;
803  dap->read_count = 0;
804 
805  LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %u%s",
807  cmsis_dap_handle->swd_cmds_differ ? "" : ", same swd ops");
808 
809  if (queued_retval != ERROR_OK) {
810  LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
811  goto skip;
812  }
813 
814  if (block->transfer_count == 0) {
815  LOG_ERROR("internal: write an empty queue?!");
816  goto skip;
817  }
818 
819  bool block_cmd = !cmsis_dap_handle->swd_cmds_differ
821  block->command = block_cmd ? CMD_DAP_TFER_BLOCK : CMD_DAP_TFER;
822 
823  command[0] = block->command;
824  command[1] = 0x00; /* DAP Index */
825 
826  unsigned int idx;
827  if (block_cmd) {
828  h_u16_to_le(&command[2], block->transfer_count);
829  idx = 4; /* The first transfer will store the common DAP register */
830  } else {
831  command[2] = block->transfer_count;
832  idx = 3;
833  }
834 
835  for (unsigned int i = 0; i < block->transfer_count; i++) {
836  struct pending_transfer_result *transfer = &(block->transfers[i]);
837  uint8_t cmd = transfer->cmd;
838  uint32_t data = transfer->data;
839 
840  LOG_DEBUG_IO("%s %s reg %x %" PRIx32,
841  cmd & SWD_CMD_APNDP ? "AP" : "DP",
842  cmd & SWD_CMD_RNW ? "read" : "write",
843  (cmd & SWD_CMD_A32) >> 1, data);
844 
845  /* When proper WAIT handling is implemented in the
846  * common SWD framework, this kludge can be
847  * removed. However, this might lead to minor
848  * performance degradation as the adapter wouldn't be
849  * able to automatically retry anything (because ARM
850  * has forgotten to implement sticky error flags
851  * clearing). See also comments regarding
852  * cmsis_dap_cmd_dap_tfer_configure() and
853  * cmsis_dap_cmd_dap_swd_configure() in
854  * cmsis_dap_init().
855  */
856  if (!(cmd & SWD_CMD_RNW) &&
857  !(cmd & SWD_CMD_APNDP) &&
858  (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
859  (data & CORUNDETECT)) {
860  LOG_DEBUG("refusing to enable sticky overrun detection");
861  data &= ~CORUNDETECT;
862  }
863 
864  if (!block_cmd || i == 0)
865  command[idx++] = (cmd >> 1) & 0x0f;
866 
867  if (!(cmd & SWD_CMD_RNW)) {
868  h_u32_to_le(&command[idx], data);
869  idx += 4;
870  }
871  }
872 
873  int retval = dap->backend->write(dap, idx, TIMEOUT_MS);
874  if (retval < 0) {
875  queued_retval = retval;
876  goto skip;
877  }
878 
879  unsigned int packet_count = cmsis_dap_quirk_mode ? 1 : dap->packet_count;
880  dap->pending_fifo_put_idx = (dap->pending_fifo_put_idx + 1) % packet_count;
882  if (dap->pending_fifo_block_count > packet_count)
883  LOG_ERROR("internal: too much pending writes %u", dap->pending_fifo_block_count);
884 
885  return;
886 
887 skip:
888  block->transfer_count = 0;
889 }
890 
891 static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, enum cmsis_dap_blocking blocking)
892 {
893  int retval;
894  struct pending_request_block *block = &dap->pending_fifo[dap->pending_fifo_get_idx];
895 
896  if (dap->pending_fifo_block_count == 0) {
897  LOG_ERROR("internal: no pending write when reading?!");
898  return;
899  }
900 
901  if (queued_retval != ERROR_OK) {
902  /* keep reading blocks until the pipeline is empty */
903  retval = dap->backend->read(dap, 10, CMSIS_DAP_BLOCKING);
904  if (retval == ERROR_TIMEOUT_REACHED || retval == 0) {
905  /* timeout means that we flushed the pipeline,
906  * we can safely discard remaining pending requests */
908  return;
909  }
910  goto skip;
911  }
912 
913  /* get reply */
914  retval = dap->backend->read(dap, TIMEOUT_MS, blocking);
915  bool timeout = (retval == ERROR_TIMEOUT_REACHED || retval == 0);
916  if (timeout && blocking == CMSIS_DAP_NON_BLOCKING)
917  return;
918 
919  if (retval <= 0) {
920  LOG_DEBUG("error reading adapter response");
922  if (timeout) {
923  /* timeout means that we flushed the pipeline,
924  * we can safely discard remaining pending requests */
926  return;
927  }
928  goto skip;
929  }
930 
931  uint8_t *resp = dap->response;
932  if (resp[0] != block->command) {
933  LOG_ERROR("CMSIS-DAP command mismatch. Expected 0x%x received 0x%" PRIx8,
934  block->command, resp[0]);
937  return;
938  }
939 
940  unsigned int transfer_count;
941  unsigned int idx;
942  if (block->command == CMD_DAP_TFER_BLOCK) {
943  transfer_count = le_to_h_u16(&resp[1]);
944  idx = 3;
945  } else {
946  transfer_count = resp[1];
947  idx = 2;
948  }
949  if (resp[idx] & 0x08) {
950  LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count);
952  goto skip;
953  }
954  uint8_t ack = resp[idx++] & 0x07;
955  if (ack != SWD_ACK_OK) {
956  LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count,
957  ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
959  /* TODO: use results of transfers completed before the error occurred? */
960  goto skip;
961  }
962 
963  if (block->transfer_count != transfer_count) {
964  LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
968  return;
969  }
970 
971  LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %u, %s mode",
973  blocking ? "blocking" : "nonblocking");
974 
975  for (unsigned int i = 0; i < transfer_count; i++) {
976  struct pending_transfer_result *transfer = &(block->transfers[i]);
977  if (transfer->cmd & SWD_CMD_RNW) {
978  static uint32_t last_read;
979  uint32_t data = le_to_h_u32(&resp[idx]);
980  uint32_t tmp = data;
981  idx += 4;
982 
983  LOG_DEBUG_IO("Read result: %" PRIx32, data);
984 
985  /* Imitate posted AP reads */
986  if ((transfer->cmd & SWD_CMD_APNDP) ||
987  ((transfer->cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
988  tmp = last_read;
989  last_read = data;
990  }
991 
992  if (transfer->buffer)
993  *(uint32_t *)(transfer->buffer) = tmp;
994  }
995  }
996 
997 skip:
998  block->transfer_count = 0;
999  if (!cmsis_dap_quirk_mode && dap->packet_count > 1)
1000  dap->pending_fifo_get_idx = (dap->pending_fifo_get_idx + 1) % dap->packet_count;
1001  dap->pending_fifo_block_count--;
1002 }
1003 
1004 static int cmsis_dap_swd_run_queue(void)
1005 {
1009 
1011  }
1012 
1015 
1018 
1019  int retval = queued_retval;
1021 
1022  return retval;
1023 }
1024 
1025 static unsigned int cmsis_dap_tfer_cmd_size(unsigned int write_count,
1026  unsigned int read_count, bool block_tfer)
1027 {
1028  unsigned int size;
1029  if (block_tfer) {
1030  size = 5; /* DAP_TransferBlock header */
1031  size += write_count * 4; /* data */
1032  } else {
1033  size = 3; /* DAP_Transfer header */
1034  size += write_count * (1 + 4); /* DAP register + data */
1035  size += read_count; /* DAP register */
1036  }
1037  return size;
1038 }
1039 
1040 static unsigned int cmsis_dap_tfer_resp_size(unsigned int write_count,
1041  unsigned int read_count, bool block_tfer)
1042 {
1043  unsigned int size;
1044  if (block_tfer)
1045  size = 4; /* DAP_TransferBlock response header */
1046  else
1047  size = 3; /* DAP_Transfer response header */
1048 
1049  size += read_count * 4; /* data */
1050  return size;
1051 }
1052 
1053 static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
1054 {
1055  /* TARGETSEL register write cannot be queued */
1056  if (swd_cmd(false, false, DP_TARGETSEL) == cmd) {
1058 
1060  return;
1061  }
1062 
1063  /* Compute sizes of the DAP Transfer command and the expected response
1064  * for all queued and this operation */
1065  unsigned int write_count = cmsis_dap_handle->write_count;
1066  unsigned int read_count = cmsis_dap_handle->read_count;
1067  bool block_cmd;
1068  if (write_count + read_count < CMD_DAP_TFER_BLOCK_MIN_OPS)
1069  block_cmd = false;
1070  else
1071  block_cmd = !cmsis_dap_handle->swd_cmds_differ
1073 
1074  if (cmd & SWD_CMD_RNW)
1075  read_count++;
1076  else
1077  write_count++;
1078 
1079  unsigned int cmd_size = cmsis_dap_tfer_cmd_size(write_count, read_count,
1080  block_cmd);
1081  unsigned int resp_size = cmsis_dap_tfer_resp_size(write_count, read_count,
1082  block_cmd);
1083  unsigned int max_transfer_count = block_cmd ? 65535 : 255;
1084 
1085  /* Does the DAP Transfer command and also its expected response fit into one packet? */
1086  if (cmd_size > tfer_max_command_size
1087  || resp_size > tfer_max_response_size
1088  || write_count + read_count > max_transfer_count) {
1091 
1092  /* Not enough room in the queue. Run the queue. */
1094 
1095  unsigned int packet_count = cmsis_dap_quirk_mode ? 1 : cmsis_dap_handle->packet_count;
1096  if (cmsis_dap_handle->pending_fifo_block_count >= packet_count)
1098  }
1099 
1101 
1102  if (queued_retval != ERROR_OK)
1103  return;
1104 
1106  struct pending_transfer_result *transfer = &(block->transfers[block->transfer_count]);
1107  transfer->data = data;
1108  transfer->cmd = cmd;
1109  if (block->transfer_count == 0) {
1112  } else if (cmd != cmsis_dap_handle->common_swd_cmd) {
1114  }
1115 
1116  if (cmd & SWD_CMD_RNW) {
1117  /* Queue a read transaction */
1118  transfer->buffer = dst;
1120  } else {
1122  }
1123  block->transfer_count++;
1124 }
1125 
1126 static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1127 {
1128  assert(!(cmd & SWD_CMD_RNW));
1129  cmsis_dap_swd_queue_cmd(cmd, NULL, value);
1130 }
1131 
1132 static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1133 {
1134  assert(cmd & SWD_CMD_RNW);
1135  cmsis_dap_swd_queue_cmd(cmd, value, 0);
1136 }
1137 
1139 {
1140  uint8_t *data;
1141 
1142  int retval = cmsis_dap_cmd_dap_info(INFO_ID_SERNUM, &data);
1143  if (retval != ERROR_OK)
1144  return retval;
1145 
1146  if (data[0]) /* strlen */
1147  LOG_INFO("CMSIS-DAP: Serial# = %s", &data[1]);
1148 
1149  return ERROR_OK;
1150 }
1151 
1153 {
1154  uint8_t *data;
1155 
1156  /* INFO_ID_FW_VER - string */
1157  int retval = cmsis_dap_cmd_dap_info(INFO_ID_FW_VER, &data);
1158  if (retval != ERROR_OK)
1159  return retval;
1160 
1161  if (data[0]) /* strlen */
1162  LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
1163 
1164  return ERROR_OK;
1165 }
1166 
1167 static int cmsis_dap_get_caps_info(void)
1168 {
1169  uint8_t *data;
1170 
1171  /* INFO_ID_CAPS - byte */
1172  int retval = cmsis_dap_cmd_dap_info(INFO_ID_CAPS, &data);
1173  if (retval != ERROR_OK)
1174  return retval;
1175 
1176  if (data[0] == 1 || data[0] == 2) {
1177  uint16_t caps = data[1];
1178  if (data[0] == 2)
1179  caps |= (uint16_t)data[2] << 8;
1180 
1182 
1183  for (unsigned int i = 0; i < INFO_CAPS__NUM_CAPS; ++i) {
1184  if (caps & BIT(i))
1185  LOG_INFO("CMSIS-DAP: %s", info_caps_str[i]);
1186  }
1187  }
1188 
1189  return ERROR_OK;
1190 }
1191 
1192 static int cmsis_dap_get_swo_buf_sz(uint32_t *swo_buf_sz)
1193 {
1194  uint8_t *data;
1195 
1196  /* INFO_ID_SWO_BUF_SZ - word */
1198  if (retval != ERROR_OK)
1199  return retval;
1200 
1201  if (data[0] != 4)
1202  return ERROR_FAIL;
1203 
1204  *swo_buf_sz = le_to_h_u32(&data[1]);
1205 
1206  LOG_INFO("CMSIS-DAP: SWO Trace Buffer Size = %u bytes", *swo_buf_sz);
1207 
1208  return ERROR_OK;
1209 }
1210 
1211 static int cmsis_dap_get_status(void)
1212 {
1213  uint8_t d;
1214 
1215  int retval = cmsis_dap_cmd_dap_swj_pins(0, 0, 0, &d);
1216 
1217  if (retval == ERROR_OK) {
1218  LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
1219  (d & SWJ_PIN_TCK) ? 1 : 0,
1220  (d & SWJ_PIN_TMS) ? 1 : 0,
1221  (d & SWJ_PIN_TDI) ? 1 : 0,
1222  (d & SWJ_PIN_TDO) ? 1 : 0,
1223  (d & SWJ_PIN_TRST) ? 1 : 0,
1224  (d & SWJ_PIN_SRST) ? 1 : 0);
1225  }
1226 
1227  return retval;
1228 }
1229 
1231 {
1232  const uint8_t *s;
1233  unsigned int s_len;
1234  int retval;
1235 
1236  if (swd_mode)
1238 
1239  if (cmsis_dap_quirk_mode && seq != LINE_RESET &&
1241  == (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
1242  /* Following workaround deasserts reset on most adapters.
1243  * Do not reconnect if a reset line is active!
1244  * Reconnecting would break connecting under reset. */
1245 
1246  /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
1248 
1249  /* When we are reconnecting, DAP_Connect needs to be rerun, at
1250  * least on Keil ULINK-ME */
1252  if (retval != ERROR_OK)
1253  return retval;
1254  }
1255 
1256  switch (seq) {
1257  case LINE_RESET:
1258  LOG_DEBUG_IO("SWD line reset");
1259  s = swd_seq_line_reset;
1260  s_len = swd_seq_line_reset_len;
1261  break;
1262  case JTAG_TO_SWD:
1263  LOG_DEBUG("JTAG-to-SWD");
1264  s = swd_seq_jtag_to_swd;
1265  s_len = swd_seq_jtag_to_swd_len;
1266  break;
1267  case JTAG_TO_DORMANT:
1268  LOG_DEBUG("JTAG-to-DORMANT");
1271  break;
1272  case SWD_TO_JTAG:
1273  LOG_DEBUG("SWD-to-JTAG");
1274  s = swd_seq_swd_to_jtag;
1275  s_len = swd_seq_swd_to_jtag_len;
1276  break;
1277  case SWD_TO_DORMANT:
1278  LOG_DEBUG("SWD-to-DORMANT");
1281  break;
1282  case DORMANT_TO_SWD:
1283  LOG_DEBUG("DORMANT-to-SWD");
1286  break;
1287  case DORMANT_TO_JTAG:
1288  LOG_DEBUG("DORMANT-to-JTAG");
1291  break;
1292  default:
1293  LOG_ERROR("Sequence %d not supported", seq);
1294  return ERROR_FAIL;
1295  }
1296 
1297  retval = cmsis_dap_cmd_dap_swj_sequence(s_len, s);
1298  if (retval != ERROR_OK)
1299  return retval;
1300 
1301  /* Atmel EDBG needs renew clock setting after SWJ_Sequence
1302  * otherwise default frequency is used */
1304 }
1305 
1306 static int cmsis_dap_swd_open(void)
1307 {
1308  if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
1309  LOG_ERROR("CMSIS-DAP: SWD not supported");
1310  return ERROR_JTAG_DEVICE_ERROR;
1311  }
1312 
1313  int retval = cmsis_dap_cmd_dap_connect(CONNECT_SWD);
1314  if (retval != ERROR_OK)
1315  return retval;
1316 
1317  /* Add more setup here.??... */
1318 
1319  LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
1320  return ERROR_OK;
1321 }
1322 
1323 static int cmsis_dap_init(void)
1324 {
1325  uint8_t *data;
1326 
1327  int retval = cmsis_dap_open();
1328  if (retval != ERROR_OK)
1329  return retval;
1330 
1332 
1333  retval = cmsis_dap_get_caps_info();
1334  if (retval != ERROR_OK)
1335  return retval;
1336 
1337  retval = cmsis_dap_get_version_info();
1338  if (retval != ERROR_OK)
1339  return retval;
1340 
1341  retval = cmsis_dap_get_serial_info();
1342  if (retval != ERROR_OK)
1343  return retval;
1344 
1345  if (swd_mode) {
1346  retval = cmsis_dap_swd_open();
1347  if (retval != ERROR_OK)
1348  return retval;
1349  } else {
1350  /* Connect in JTAG mode */
1351  if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
1352  LOG_ERROR("CMSIS-DAP: JTAG not supported");
1353  return ERROR_JTAG_DEVICE_ERROR;
1354  }
1355 
1357  if (retval != ERROR_OK)
1358  return retval;
1359 
1360  LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
1361  }
1362 
1363  /* Be conservative and suppress submitting multiple HID requests
1364  * until we get packet count info from the adaptor */
1366 
1367  /* INFO_ID_PKT_SZ - short */
1369  if (retval != ERROR_OK)
1370  goto init_err;
1371 
1372  if (data[0] == 2) { /* short */
1373  uint16_t pkt_sz = data[1] + (data[2] << 8);
1374  if (pkt_sz != cmsis_dap_handle->packet_size) {
1377  if (retval != ERROR_OK)
1378  goto init_err;
1379 
1380  LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRIu16, pkt_sz);
1381  }
1382  }
1383 
1384  /* Maximal number of transfers which fit to one packet:
1385  * Limited by response size: 3 bytes of response header + 4 per read
1386  * Plus writes to full command size: 3 bytes cmd header + 1 per read + 5 per write */
1389  unsigned int max_reads = tfer_max_response_size / 4;
1390  pending_queue_len = max_reads + (tfer_max_command_size - max_reads) / 5;
1393 
1394  /* INFO_ID_PKT_CNT - byte */
1396  if (retval != ERROR_OK)
1397  goto init_err;
1398 
1399  if (data[0] == 1) { /* byte */
1400  unsigned int pkt_cnt = data[1];
1401  if (pkt_cnt > 1)
1403 
1404  LOG_DEBUG("CMSIS-DAP: Packet Count = %u", pkt_cnt);
1405  }
1406 
1407  LOG_DEBUG("Allocating FIFO for %u pending packets", cmsis_dap_handle->packet_count);
1408  for (unsigned int i = 0; i < cmsis_dap_handle->packet_count; i++) {
1410  * sizeof(struct pending_transfer_result));
1412  LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
1413  retval = ERROR_FAIL;
1414  goto init_err;
1415  }
1416  }
1417 
1418  /* Intentionally not checked for error, just logs an info message
1419  * not vital for further debugging */
1420  (void)cmsis_dap_get_status();
1421 
1422  /* Now try to connect to the target
1423  * TODO: This is all SWD only @ present */
1425  if (retval != ERROR_OK)
1426  goto init_err;
1427 
1428  /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
1429  * up to 64 times. This must be changed to 0 if sticky
1430  * overrun detection is enabled. */
1431  retval = cmsis_dap_cmd_dap_tfer_configure(0, 64, 0);
1432  if (retval != ERROR_OK)
1433  goto init_err;
1434 
1435  if (swd_mode) {
1436  /* Data Phase (bit 2) must be set to 1 if sticky overrun
1437  * detection is enabled */
1438  retval = cmsis_dap_cmd_dap_swd_configure(0); /* 1 TRN, no Data Phase */
1439  if (retval != ERROR_OK)
1440  goto init_err;
1441  }
1442  /* Both LEDs on */
1443  /* Intentionally not checked for error, debugging will work
1444  * without LEDs */
1447 
1448  /* support connecting with srst asserted */
1450 
1453  retval = cmsis_dap_cmd_dap_swj_pins(0, SWJ_PIN_SRST, 0, NULL);
1454  if (retval != ERROR_OK)
1455  goto init_err;
1456  LOG_INFO("Connecting under reset");
1457  }
1458  }
1459  LOG_INFO("CMSIS-DAP: Interface ready");
1460  return ERROR_OK;
1461 
1462 init_err:
1463  cmsis_dap_quit();
1464  return retval;
1465 }
1466 
1467 static int cmsis_dap_swd_init(void)
1468 {
1469  swd_mode = true;
1470  return ERROR_OK;
1471 }
1472 
1473 static int cmsis_dap_quit(void)
1474 {
1476 
1477  /* Both LEDs off */
1480 
1482 
1483  return ERROR_OK;
1484 }
1485 
1486 static int cmsis_dap_reset(int trst, int srst)
1487 {
1488  /* Set both TRST and SRST even if they're not enabled as
1489  * there's no way to tristate them */
1490 
1491  output_pins = 0;
1492  if (!srst)
1494  if (!trst)
1496 
1499  if (retval != ERROR_OK)
1500  LOG_ERROR("CMSIS-DAP: Interface reset failed");
1501  return retval;
1502 }
1503 
1505 {
1506 #if 0
1507  int retval = cmsis_dap_cmd_dap_delay(cmd->cmd.sleep->us);
1508  if (retval != ERROR_OK)
1509 #endif
1510  jtag_sleep(cmd->cmd.sleep->us);
1511 }
1512 
1513 /* Set TMS high for five TCK clocks, to move the TAP to the Test-Logic-Reset state */
1515 {
1516  LOG_INFO("cmsis-dap JTAG TLR_RESET");
1517  uint8_t seq = 0xff;
1518 
1519  int retval = cmsis_dap_cmd_dap_swj_sequence(8, &seq);
1520  if (retval == ERROR_OK)
1522  return retval;
1523 }
1524 
1525 /* Set new end state */
1527 {
1530  else {
1531  LOG_ERROR("BUG: %i is not a valid end state", state);
1532  exit(-1);
1533  }
1534 }
1535 
1536 #ifdef SPRINT_BINARY
1537 static void sprint_binary(char *s, const uint8_t *buf, unsigned int offset, unsigned int len)
1538 {
1539  if (!len)
1540  return;
1541 
1542  /*
1543  buf = { 0x18 } len=5 should result in: 11000
1544  buf = { 0xff 0x18 } len=13 should result in: 11111111 11000
1545  buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
1546  i=3 there means i/8 = 0 so c = 0xFF, and
1547  */
1548  for (unsigned int i = offset; i < offset + len; ++i) {
1549  uint8_t c = buf[i / 8], mask = 1 << (i % 8);
1550  if ((i != offset) && !(i % 8))
1551  putchar(' ');
1552  *s++ = (c & mask) ? '1' : '0';
1553  }
1554  *s = 0;
1555 }
1556 #endif
1557 
1558 #ifdef CMSIS_DAP_JTAG_DEBUG
1559 static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
1560 {
1561  /* cmd is a usb packet to go to the cmsis-dap interface */
1562  printf("cmsis-dap buffer (%d b): ", cmdlen);
1563  for (int i = 0; i < cmdlen; ++i)
1564  printf(" %02x", cmd[i]);
1565  printf("\n");
1566  switch (cmd[0]) {
1567  case CMD_DAP_JTAG_SEQ: {
1568  printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[0], cmd[1]);
1569  /*
1570  * #1 = number of sequences
1571  * #2 = sequence info 1
1572  * #3...4+n_bytes-1 = sequence 1
1573  * #4+n_bytes = sequence info 2
1574  * #5+n_bytes = sequence 2 (single bit)
1575  */
1576  int pos = 2;
1577  for (int seq = 0; seq < cmd[1]; ++seq) {
1578  uint8_t info = cmd[pos++];
1579  int len = info & DAP_JTAG_SEQ_TCK;
1580  if (len == 0)
1581  len = 64;
1582  printf(" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
1583  seq, pos, info, len, info & DAP_JTAG_SEQ_TMS, info & DAP_JTAG_SEQ_TDO);
1584  for (int i = 0; i < DIV_ROUND_UP(len, 8); ++i)
1585  printf(" %02x", cmd[pos + i]);
1586  pos += DIV_ROUND_UP(len, 8);
1587  printf("\n");
1588  }
1589  if (pos != cmdlen) {
1590  printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
1591  exit(-1);
1592  }
1593 
1594  break;
1595  }
1596  default:
1597  LOG_DEBUG("unknown cmsis-dap command %02x", cmd[1]);
1598  break;
1599  }
1600 }
1601 #endif
1602 
1603 static void cmsis_dap_flush(void)
1604 {
1605  if (!queued_seq_count)
1606  return;
1607 
1608  LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
1610 
1611  /* prepare CMSIS-DAP packet */
1612  uint8_t *command = cmsis_dap_handle->command;
1616 
1617 #ifdef CMSIS_DAP_JTAG_DEBUG
1618  debug_parse_cmsis_buf(command, queued_seq_buf_end + 2);
1619 #endif
1620 
1621  /* send command to USB device */
1623 
1624  uint8_t *resp = cmsis_dap_handle->response;
1625  if (retval != ERROR_OK || resp[1] != DAP_OK) {
1626  LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
1627  exit(-1);
1628  }
1629 
1630 #ifdef CMSIS_DAP_JTAG_DEBUG
1631  LOG_DEBUG_IO("USB response buf:");
1632  for (int c = 0; c < queued_seq_buf_end + 3; ++c)
1633  printf("%02X ", resp[c]);
1634  printf("\n");
1635 #endif
1636 
1637  /* copy scan results into client buffers */
1638  for (int i = 0; i < pending_scan_result_count; ++i) {
1640  LOG_DEBUG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
1641  i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
1642 #ifdef CMSIS_DAP_JTAG_DEBUG
1643  for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
1644  printf("%02X ", resp[2+scan->first+b]);
1645  printf("\n");
1646 #endif
1647  bit_copy(scan->buffer, scan->buffer_offset, &resp[2 + scan->first], 0, scan->length);
1648  }
1649 
1650  /* reset */
1651  queued_seq_count = 0;
1652  queued_seq_buf_end = 0;
1653  queued_seq_tdo_ptr = 0;
1655 }
1656 
1657 /* queue a sequence of bits to clock out TDI / in TDO, executing if the buffer is full.
1658  *
1659  * sequence=NULL means clock out zeros on TDI
1660  * tdo_buffer=NULL means don't capture TDO
1661  */
1662 static void cmsis_dap_add_jtag_sequence(unsigned int s_len, const uint8_t *sequence,
1663  unsigned int s_offset, bool tms,
1664  uint8_t *tdo_buffer, unsigned int tdo_buffer_offset)
1665 {
1666  LOG_DEBUG_IO("[at %d] %u bits, tms %s, seq offset %u, tdo buf %p, tdo offset %u",
1668  s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
1669 
1670  if (s_len == 0)
1671  return;
1672 
1673  if (s_len > 64) {
1674  LOG_DEBUG_IO("START JTAG SEQ SPLIT");
1675  for (unsigned int offset = 0; offset < s_len; offset += 64) {
1676  unsigned int len = s_len - offset;
1677  if (len > 64)
1678  len = 64;
1679  LOG_DEBUG_IO("Splitting long jtag sequence: %u-bit chunk starting at offset %u", len, offset);
1681  len,
1682  sequence,
1683  s_offset + offset,
1684  tms,
1685  tdo_buffer,
1686  !tdo_buffer ? 0 : (tdo_buffer_offset + offset)
1687  );
1688  }
1689  LOG_DEBUG_IO("END JTAG SEQ SPLIT");
1690  return;
1691  }
1692 
1693  unsigned int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
1694  if (queued_seq_count >= 255 || queued_seq_buf_end + cmd_len > QUEUED_SEQ_BUF_LEN)
1695  /* empty out the buffer */
1696  cmsis_dap_flush();
1697 
1698  ++queued_seq_count;
1699 
1700  /* control byte */
1702  (tms ? DAP_JTAG_SEQ_TMS : 0) |
1703  (tdo_buffer ? DAP_JTAG_SEQ_TDO : 0) |
1704  (s_len == 64 ? 0 : s_len);
1705 
1706  if (sequence)
1707  bit_copy(&queued_seq_buf[queued_seq_buf_end + 1], 0, sequence, s_offset, s_len);
1708  else
1709  memset(&queued_seq_buf[queued_seq_buf_end + 1], 0, DIV_ROUND_UP(s_len, 8));
1710 
1711  queued_seq_buf_end += cmd_len;
1712 
1713  if (tdo_buffer) {
1715  scan->first = queued_seq_tdo_ptr;
1716  queued_seq_tdo_ptr += DIV_ROUND_UP(s_len, 8);
1717  scan->length = s_len;
1718  scan->buffer = tdo_buffer;
1719  scan->buffer_offset = tdo_buffer_offset;
1720  }
1721 }
1722 
1723 /* queue a sequence of bits to clock out TMS, executing if the buffer is full */
1724 static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
1725 {
1726  LOG_DEBUG_IO("%d bits: %02X", s_len, *sequence);
1727  /* we use a series of CMD_DAP_JTAG_SEQ commands to toggle TMS,
1728  because even though it seems ridiculously inefficient, it
1729  allows us to combine TMS and scan sequences into the same
1730  USB packet. */
1731  /* TODO: combine runs of the same tms value */
1732  for (int i = 0; i < s_len; ++i) {
1733  bool bit = (sequence[i / 8] & (1 << (i % 8))) != 0;
1735  }
1736 }
1737 
1738 /* Move to the end state by queuing a sequence to clock into TMS */
1739 static void cmsis_dap_state_move(void)
1740 {
1741  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1742  uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1743 
1744  LOG_DEBUG_IO("state move from %s to %s: %d clocks, %02X on tms",
1746  tms_scan_bits, tms_scan);
1747  cmsis_dap_add_tms_sequence(&tms_scan, tms_scan_bits);
1748 
1750 }
1751 
1752 
1753 /* Execute a JTAG scan operation by queueing TMS and TDI/TDO sequences */
1755 {
1756  LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
1757  jtag_scan_type(cmd->cmd.scan));
1758 
1759  /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
1760  while (cmd->cmd.scan->num_fields > 0
1761  && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
1762  cmd->cmd.scan->num_fields--;
1763  LOG_DEBUG("discarding trailing empty field");
1764  }
1765 
1766  if (!cmd->cmd.scan->num_fields) {
1767  LOG_DEBUG("empty scan, doing nothing");
1768  return;
1769  }
1770 
1771  if (cmd->cmd.scan->ir_scan) {
1772  if (tap_get_state() != TAP_IRSHIFT) {
1775  }
1776  } else {
1777  if (tap_get_state() != TAP_DRSHIFT) {
1780  }
1781  }
1782 
1783  cmsis_dap_end_state(cmd->cmd.scan->end_state);
1784 
1785  struct scan_field *field = cmd->cmd.scan->fields;
1786  unsigned int scan_size = 0;
1787 
1788  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
1789  scan_size += field->num_bits;
1790  LOG_DEBUG_IO("%s%s field %u/%u %u bits",
1791  field->in_value ? "in" : "",
1792  field->out_value ? "out" : "",
1793  i,
1794  cmd->cmd.scan->num_fields,
1795  field->num_bits);
1796 
1797  if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
1798  LOG_DEBUG_IO("Last field and have to move out of SHIFT state");
1799  /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
1800  * movement. This last field can't have length zero, it was checked above. */
1802  field->num_bits - 1, /* number of bits to clock */
1803  field->out_value, /* output sequence */
1804  0, /* output offset */
1805  false, /* TMS low */
1806  field->in_value,
1807  0);
1808 
1809  /* Clock the last bit out, with TMS high */
1810  uint8_t last_bit = 0;
1811  if (field->out_value)
1812  bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
1814  1,
1815  &last_bit,
1816  0,
1817  true,
1818  field->in_value,
1819  field->num_bits - 1);
1821 
1822  /* Now clock one more cycle, with TMS low, to get us into a PAUSE state */
1824  1,
1825  &last_bit,
1826  0,
1827  false,
1828  NULL,
1829  0);
1831  } else {
1832  LOG_DEBUG_IO("Internal field, staying in SHIFT state afterwards");
1833  /* Clocking part of a sequence into DR or IR with TMS=0,
1834  leaving TMS=0 at the end so we can continue later */
1836  field->num_bits,
1837  field->out_value,
1838  0,
1839  false,
1840  field->in_value,
1841  0);
1842  }
1843  }
1844 
1845  if (tap_get_state() != tap_get_end_state()) {
1848  }
1849 
1850  LOG_DEBUG_IO("%s scan, %i bits, end in %s",
1851  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1853 }
1854 
1855 static void cmsis_dap_pathmove(int num_states, enum tap_state *path)
1856 {
1857  uint8_t tms0 = 0x00;
1858  uint8_t tms1 = 0xff;
1859 
1860  for (int i = 0; i < num_states; i++) {
1861  if (path[i] == tap_state_transition(tap_get_state(), false))
1862  cmsis_dap_add_tms_sequence(&tms0, 1);
1863  else if (path[i] == tap_state_transition(tap_get_state(), true))
1864  cmsis_dap_add_tms_sequence(&tms1, 1);
1865  else {
1866  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
1868  exit(-1);
1869  }
1870 
1871  tap_set_state(path[i]);
1872  }
1873 
1875 }
1876 
1878 {
1879  LOG_DEBUG_IO("pathmove: %i states, end in %i",
1880  cmd->cmd.pathmove->num_states,
1881  cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1882 
1883  cmsis_dap_pathmove(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
1884 }
1885 
1886 static void cmsis_dap_stableclocks(unsigned int num_cycles)
1887 {
1888  uint8_t tms = tap_get_state() == TAP_RESET;
1889  /* TODO: Perform optimizations? */
1890  /* Execute num_cycles. */
1891  for (unsigned int i = 0; i < num_cycles; i++)
1892  cmsis_dap_add_tms_sequence(&tms, 1);
1893 }
1894 
1895 static void cmsis_dap_runtest(unsigned int num_cycles)
1896 {
1897  enum tap_state saved_end_state = tap_get_end_state();
1898 
1899  /* Only do a state_move when we're not already in IDLE. */
1900  if (tap_get_state() != TAP_IDLE) {
1903  }
1904  cmsis_dap_stableclocks(num_cycles);
1905 
1906  /* Finish in end_state. */
1907  cmsis_dap_end_state(saved_end_state);
1908 
1909  if (tap_get_state() != tap_get_end_state())
1911 }
1912 
1914 {
1915  LOG_DEBUG_IO("runtest %u cycles, end in %i", cmd->cmd.runtest->num_cycles,
1916  cmd->cmd.runtest->end_state);
1917 
1918  cmsis_dap_end_state(cmd->cmd.runtest->end_state);
1919  cmsis_dap_runtest(cmd->cmd.runtest->num_cycles);
1920 }
1921 
1923 {
1924  LOG_DEBUG_IO("stableclocks %u cycles", cmd->cmd.runtest->num_cycles);
1925  cmsis_dap_stableclocks(cmd->cmd.runtest->num_cycles);
1926 }
1927 
1929 {
1930  LOG_DEBUG_IO("TMS: %u bits", cmd->cmd.tms->num_bits);
1931  cmsis_dap_cmd_dap_swj_sequence(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
1932 }
1933 
1934 /* TODO: Is there need to call cmsis_dap_flush() for the JTAG_PATHMOVE,
1935  * JTAG_RUNTEST, JTAG_STABLECLOCKS? */
1937 {
1938  switch (cmd->type) {
1939  case JTAG_SLEEP:
1940  cmsis_dap_flush();
1942  break;
1943  case JTAG_TLR_RESET:
1944  cmsis_dap_flush();
1946  break;
1947  case JTAG_SCAN:
1949  break;
1950  case JTAG_PATHMOVE:
1952  break;
1953  case JTAG_RUNTEST:
1955  break;
1956  case JTAG_STABLECLOCKS:
1958  break;
1959  case JTAG_TMS:
1961  break;
1962  default:
1963  LOG_ERROR("BUG: unknown JTAG command type 0x%X encountered", cmd->type);
1964  exit(-1);
1965  }
1966 }
1967 
1968 static int cmsis_dap_execute_queue(struct jtag_command *cmd_queue)
1969 {
1970  struct jtag_command *cmd = cmd_queue;
1971 
1972  while (cmd) {
1974  cmd = cmd->next;
1975  }
1976 
1977  cmsis_dap_flush();
1978 
1979  return ERROR_OK;
1980 }
1981 
1982 static int cmsis_dap_speed(int speed)
1983 {
1984  if (speed == 0) {
1985  LOG_ERROR("RTCK not supported. Set nonzero \"adapter speed\".");
1987  }
1988 
1989  return cmsis_dap_cmd_dap_swj_clock(speed);
1990 }
1991 
1992 static int cmsis_dap_speed_div(int speed, int *khz)
1993 {
1994  *khz = speed;
1995  return ERROR_OK;
1996 }
1997 
1998 static int cmsis_dap_khz(int khz, int *jtag_speed)
1999 {
2000  *jtag_speed = khz;
2001  return ERROR_OK;
2002 }
2003 
2004 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
2005  uint32_t trace_freq, uint16_t *prescaler)
2006 {
2007  unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
2008  if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
2009  return false;
2010 
2011  /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
2012  unsigned int max_deviation = (traceclkin_freq * 3) / 100;
2013  if (presc * trace_freq < traceclkin_freq - max_deviation ||
2014  presc * trace_freq > traceclkin_freq + max_deviation)
2015  return false;
2016 
2017  *prescaler = presc;
2018 
2019  return true;
2020 }
2021 
2026  bool trace_enabled,
2027  enum tpiu_pin_protocol pin_protocol,
2028  uint32_t port_size,
2029  unsigned int *swo_freq,
2030  unsigned int traceclkin_hz,
2031  uint16_t *swo_prescaler)
2032 {
2033  int retval;
2034 
2035  if (!trace_enabled) {
2038  if (retval != ERROR_OK) {
2039  LOG_ERROR("Failed to disable the SWO-trace.");
2040  return retval;
2041  }
2042  }
2044  LOG_INFO("SWO-trace disabled.");
2045  return ERROR_OK;
2046  }
2047 
2050  LOG_ERROR("SWO-trace is not supported by the device.");
2051  return ERROR_FAIL;
2052  }
2053 
2054  uint8_t swo_mode;
2055  if (pin_protocol == TPIU_PIN_PROTOCOL_ASYNC_UART &&
2057  swo_mode = DAP_SWO_MODE_UART;
2058  } else if (pin_protocol == TPIU_PIN_PROTOCOL_ASYNC_MANCHESTER &&
2060  swo_mode = DAP_SWO_MODE_MANCHESTER;
2061  } else {
2062  LOG_ERROR("Selected pin protocol is not supported.");
2063  return ERROR_FAIL;
2064  }
2065 
2066  if (*swo_freq == 0) {
2067  LOG_INFO("SWO-trace frequency autodetection not implemented.");
2068  return ERROR_FAIL;
2069  }
2070 
2072  if (retval != ERROR_OK)
2073  return retval;
2074 
2076 
2078  if (retval != ERROR_OK)
2079  return retval;
2080 
2082  if (retval != ERROR_OK)
2083  return retval;
2084 
2085  retval = cmsis_dap_cmd_dap_swo_mode(swo_mode);
2086  if (retval != ERROR_OK)
2087  return retval;
2088 
2089  retval = cmsis_dap_cmd_dap_swo_baudrate(*swo_freq, swo_freq);
2090  if (retval != ERROR_OK)
2091  return retval;
2092 
2093  if (!calculate_swo_prescaler(traceclkin_hz, *swo_freq,
2094  swo_prescaler)) {
2095  LOG_ERROR("SWO frequency is not suitable. Please choose a "
2096  "different frequency or use auto-detection.");
2097  return ERROR_FAIL;
2098  }
2099 
2100  LOG_INFO("SWO frequency: %u Hz.", *swo_freq);
2101  LOG_INFO("SWO prescaler: %u.", *swo_prescaler);
2102 
2104  if (retval != ERROR_OK)
2105  return retval;
2106 
2108 
2109  return ERROR_OK;
2110 }
2111 
2115 static int cmsis_dap_poll_trace(uint8_t *buf, size_t *size)
2116 {
2117  uint8_t trace_status;
2118  size_t trace_count;
2119 
2121  *size = 0;
2122  return ERROR_OK;
2123  }
2124 
2125  int retval = cmsis_dap_cmd_dap_swo_status(&trace_status, &trace_count);
2126  if (retval != ERROR_OK)
2127  return retval;
2129  return ERROR_FAIL;
2130 
2131  *size = trace_count < *size ? trace_count : *size;
2132  size_t read_so_far = 0;
2133  do {
2134  size_t rb = 0;
2135  uint32_t packet_size = cmsis_dap_handle->packet_size - 4 /*data-reply*/;
2136  uint32_t remaining = *size - read_so_far;
2137  if (remaining < packet_size)
2138  packet_size = remaining;
2139  retval = cmsis_dap_cmd_dap_swo_data(
2140  packet_size,
2141  &trace_status,
2142  &rb,
2143  &buf[read_so_far]);
2144  if (retval != ERROR_OK)
2145  return retval;
2147  return ERROR_FAIL;
2148 
2149  read_so_far += rb;
2150  } while (read_so_far < *size);
2151 
2152  return ERROR_OK;
2153 }
2154 
2155 COMMAND_HANDLER(cmsis_dap_handle_info_command)
2156 {
2159 
2160  return ERROR_OK;
2161 }
2162 
2163 COMMAND_HANDLER(cmsis_dap_handle_cmd_command)
2164 {
2165  uint8_t *command = cmsis_dap_handle->command;
2166 
2167  for (unsigned int i = 0; i < CMD_ARGC; i++)
2169 
2170  int retval = cmsis_dap_xfer(cmsis_dap_handle, CMD_ARGC);
2171 
2172  if (retval != ERROR_OK) {
2173  LOG_ERROR("CMSIS-DAP command failed.");
2174  return ERROR_JTAG_DEVICE_ERROR;
2175  }
2176 
2177  uint8_t *resp = cmsis_dap_handle->response;
2178  LOG_INFO("Returned data %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8,
2179  resp[1], resp[2], resp[3], resp[4]);
2180 
2181  return ERROR_OK;
2182 }
2183 
2184 COMMAND_HANDLER(cmsis_dap_handle_backend_command)
2185 {
2186  if (CMD_ARGC != 1)
2188 
2189  if (strcmp(CMD_ARGV[0], "auto") == 0) {
2190  cmsis_dap_backend = -1; /* autoselect */
2191  } else {
2192  for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
2193  if (strcasecmp(cmsis_dap_backends[i]->name, CMD_ARGV[0]) == 0) {
2194  if (cmsis_dap_backends[i]->open) {
2195  cmsis_dap_backend = i;
2196  return ERROR_OK;
2197  }
2198 
2199  command_print(CMD, "Requested cmsis-dap backend %s is disabled by configure",
2200  cmsis_dap_backends[i]->name);
2201  return ERROR_NOT_IMPLEMENTED;
2202  }
2203  }
2204 
2205  command_print(CMD, "invalid argument %s to cmsis-dap backend", CMD_ARGV[0]);
2207  }
2208 
2209  return ERROR_OK;
2210 }
2211 
2212 COMMAND_HANDLER(cmsis_dap_handle_quirk_command)
2213 {
2214  if (CMD_ARGC > 1)
2216 
2217  if (CMD_ARGC == 1) {
2219  return ERROR_OK;
2220  }
2221 
2222  command_print(CMD, "%s", cmsis_dap_quirk_mode ? "enabled" : "disabled");
2223 
2224  return ERROR_OK;
2225 }
2226 
2227 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
2228  {
2229  .name = "info",
2230  .handler = &cmsis_dap_handle_info_command,
2231  .mode = COMMAND_EXEC,
2232  .usage = "",
2233  .help = "show cmsis-dap info",
2234  },
2235  {
2236  .name = "cmd",
2237  .handler = &cmsis_dap_handle_cmd_command,
2238  .mode = COMMAND_EXEC,
2239  .usage = "",
2240  .help = "issue cmsis-dap command",
2241  },
2242  {
2243  .name = "backend",
2244  .handler = &cmsis_dap_handle_backend_command,
2245  .mode = COMMAND_CONFIG,
2246  .help = "set the communication backend to use (USB bulk or HID, or TCP).",
2247  .usage = "(auto | usb_bulk | hid | tcp)",
2248  },
2249  {
2250  .name = "quirk",
2251  .handler = &cmsis_dap_handle_quirk_command,
2252  .mode = COMMAND_ANY,
2253  .help = "allow expensive workarounds of known adapter quirks.",
2254  .usage = "[enable | disable]",
2255  },
2256 #if BUILD_CMSIS_DAP_USB
2257  {
2258  .name = "usb",
2260  .mode = COMMAND_ANY,
2261  .help = "USB bulk backend-specific commands",
2262  .usage = "<cmd>",
2263  },
2264 #endif
2265 #if BUILD_CMSIS_DAP_TCP
2266  {
2267  .name = "tcp",
2269  .mode = COMMAND_ANY,
2270  .help = "TCP backend-specific commands",
2271  .usage = "<cmd>",
2272  },
2273 #endif
2275 };
2276 
2277 
2278 static const struct command_registration cmsis_dap_command_handlers[] = {
2279  {
2280  .name = "cmsis-dap",
2281  .mode = COMMAND_ANY,
2282  .help = "perform CMSIS-DAP management",
2283  .usage = "<cmd>",
2285  },
2287 };
2288 
2289 static const struct swd_driver cmsis_dap_swd_driver = {
2291  .switch_seq = cmsis_dap_swd_switch_seq,
2292  .read_reg = cmsis_dap_swd_read_reg,
2293  .write_reg = cmsis_dap_swd_write_reg,
2294  .run = cmsis_dap_swd_run_queue,
2295 };
2296 
2297 static struct jtag_interface cmsis_dap_interface = {
2299  .execute_queue = cmsis_dap_execute_queue,
2300 };
2301 
2303  .name = "cmsis-dap",
2304  .transport_ids = TRANSPORT_SWD | TRANSPORT_JTAG,
2305  .transport_preferred_id = TRANSPORT_SWD,
2306  .commands = cmsis_dap_command_handlers,
2307 
2308  .init = cmsis_dap_init,
2309  .quit = cmsis_dap_quit,
2310  .reset = cmsis_dap_reset,
2311  .speed = cmsis_dap_speed,
2312  .khz = cmsis_dap_khz,
2313  .speed_div = cmsis_dap_speed_div,
2314  .config_trace = cmsis_dap_config_trace,
2315  .poll_trace = cmsis_dap_poll_trace,
2316 
2317  .jtag_ops = &cmsis_dap_interface,
2318  .swd_ops = &cmsis_dap_swd_driver,
2319 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:340
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:217
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:335
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: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:1323
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:722
static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, enum cmsis_dap_blocking blocking)
Definition: cmsis_dap.c:891
static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
Definition: cmsis_dap.c:794
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:751
#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:1211
static int cmsis_dap_reset(int trst, int srst)
Definition: cmsis_dap.c:1486
static int cmsis_dap_get_caps_info(void)
Definition: cmsis_dap.c:1167
static void cmsis_dap_stableclocks(unsigned int num_cycles)
Definition: cmsis_dap.c:1886
#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:1230
#define INFO_ID_CAPS
Definition: cmsis_dap.c:101
static void cmsis_dap_execute_runtest(struct jtag_command *cmd)
Definition: cmsis_dap.c:1913
static struct jtag_interface cmsis_dap_interface
Definition: cmsis_dap.c:2297
static int cmsis_dap_swd_open(void)
Definition: cmsis_dap.c:1306
#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:698
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:455
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:391
static unsigned int cmsis_dap_tfer_cmd_size(unsigned int write_count, unsigned int read_count, bool block_tfer)
Definition: cmsis_dap.c:1025
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:668
const struct cmsis_dap_backend cmsis_dap_usb_backend
Definition: cmsis_dap.c:45
#define CMD_DAP_LED
Definition: cmsis_dap.c:87
static unsigned int tfer_max_response_size
Definition: cmsis_dap.c:244
static void cmsis_dap_end_state(enum tap_state state)
Definition: cmsis_dap.c:1526
static int cmsis_dap_speed_div(int speed, int *khz)
Definition: cmsis_dap.c:1992
#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:544
static bool calculate_swo_prescaler(unsigned int traceclkin_freq, uint32_t trace_freq, uint16_t *prescaler)
Definition: cmsis_dap.c:2004
#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 void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: cmsis_dap.c:1126
struct adapter_driver cmsis_dap_adapter_driver
Definition: cmsis_dap.c:2302
static int cmsis_dap_cmd_dap_swj_clock(uint32_t swj_clock)
Definition: cmsis_dap.c:412
static void cmsis_dap_swd_discard_all_pending(struct cmsis_dap *dap)
Definition: cmsis_dap.c:777
static void cmsis_dap_flush(void)
Definition: cmsis_dap.c:1603
static int cmsis_dap_execute_queue(struct jtag_command *cmd_queue)
Definition: cmsis_dap.c:1968
#define CMD_DAP_JTAG_SEQ
Definition: cmsis_dap.c:157
#define CMD_DAP_TFER_CONFIGURE
Definition: cmsis_dap.c:171
#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:1473
static bool swd_mode
Definition: cmsis_dap.c:82
static const struct swd_driver cmsis_dap_swd_driver
Definition: cmsis_dap.c:2289
#define DAP_SWO_TRANSPORT_DATA
Definition: cmsis_dap.c:196
#define CMD_DAP_DELAY
Definition: cmsis_dap.c:130
#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:787
static int cmsis_dap_cmd_dap_disconnect(void)
Definition: cmsis_dap.c:511
#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:526
static void cmsis_dap_execute_stableclocks(struct jtag_command *cmd)
Definition: cmsis_dap.c:1922
static uint8_t queued_seq_buf[1024]
Definition: cmsis_dap.c:256
#define DAP_JTAG_SEQ_TMS
Definition: cmsis_dap.c:165
static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
Definition: cmsis_dap.c:348
#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:1138
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
#define CMD_DAP_CONNECT
Definition: cmsis_dap.c:88
#define CMD_DAP_TFER_BLOCK
Definition: cmsis_dap.c:173
static void cmsis_dap_execute_scan(struct jtag_command *cmd)
Definition: cmsis_dap.c:1754
static void cmsis_dap_execute_command(struct jtag_command *cmd)
Definition: cmsis_dap.c:1936
#define CMD_DAP_SWO_CONTROL
Definition: cmsis_dap.c:189
static int cmsis_dap_swd_run_queue(void)
Definition: cmsis_dap.c:1004
#define INFO_ID_SERNUM
Definition: cmsis_dap.c:97
static int cmsis_dap_cmd_dap_connect(uint8_t mode)
Definition: cmsis_dap.c:490
#define CMD_DAP_TFER
Definition: cmsis_dap.c:172
static unsigned int tfer_max_command_size
Definition: cmsis_dap.c:243
#define CMD_DAP_SWO_TRANSPORT
Definition: cmsis_dap.c:186
static void cmsis_dap_runtest(unsigned int num_cycles)
Definition: cmsis_dap.c:1895
#define CMD_DAP_TFER_BLOCK_MIN_OPS
Definition: cmsis_dap.c:179
static void cmsis_dap_execute_tms(struct jtag_command *cmd)
Definition: cmsis_dap.c:1928
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:2227
static void cmsis_dap_state_move(void)
Definition: cmsis_dap.c:1739
static int cmsis_dap_cmd_dap_swo_mode(uint8_t mode)
Sets the SWO trace capture mode.
Definition: cmsis_dap.c:643
static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
Definition: cmsis_dap.c:1053
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:432
static int cmsis_dap_khz(int khz, int *jtag_speed)
Definition: cmsis_dap.c:1998
static int cmsis_dap_metacmd_targetsel(uint32_t instance_id)
Definition: cmsis_dap.c:578
const struct cmsis_dap_backend cmsis_dap_hid_backend
Definition: cmsis_dap.c:51
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:2115
const struct cmsis_dap_backend cmsis_dap_tcp_backend
Definition: cmsis_dap.c:57
#define CMD_DAP_SWO_BAUDRATE
Definition: cmsis_dap.c:188
static int cmsis_dap_swd_init(void)
Definition: cmsis_dap.c:1467
static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
Definition: cmsis_dap.c:1504
#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:2155
static int cmsis_dap_speed(int speed)
Definition: cmsis_dap.c:1982
#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:473
static void cmsis_dap_pathmove(int num_states, enum tap_state *path)
Definition: cmsis_dap.c:1855
static unsigned int cmsis_dap_tfer_resp_size(unsigned int write_count, unsigned int read_count, bool block_tfer)
Definition: cmsis_dap.c:1040
static int cmsis_dap_cmd_dap_swo_transport(uint8_t transport)
Sets the SWO transport mode.
Definition: cmsis_dap.c:623
#define DAP_SWO_CONTROL_STOP
Definition: cmsis_dap.c:205
static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: cmsis_dap.c:1132
#define SWJ_PIN_TRST
Definition: cmsis_dap.c:149
#define SWJ_PIN_TDI
Definition: cmsis_dap.c:147
static void cmsis_dap_execute_pathmove(struct jtag_command *cmd)
Definition: cmsis_dap.c:1877
static const struct command_registration cmsis_dap_command_handlers[]
Definition: cmsis_dap.c:2278
#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:1514
static int cmsis_dap_get_swo_buf_sz(uint32_t *swo_buf_sz)
Definition: cmsis_dap.c:1192
#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:2025
#define DAP_SWO_MODE_UART
Definition: cmsis_dap.c:201
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:1662
static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
Definition: cmsis_dap.c:1724
#define SWJ_PIN_TDO
Definition: cmsis_dap.c:148
static int cmsis_dap_get_version_info(void)
Definition: cmsis_dap.c:1152
#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: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: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: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:1070
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1742
#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 ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#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
static uint32_t bit(uint32_t value, unsigned int b)
Definition: opcodes.h:39
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: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
int(* open)(struct cmsis_dap *dap, const uint16_t vids[], const uint16_t pids[], const char *serial)
Definition: cmsis_dap.h:68
void(* packet_buffer_free)(struct cmsis_dap *dap)
Definition: cmsis_dap.h:74
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:239
enum command_mode mode
Definition: command.h:241
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: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