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