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