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