adapter: switch from struct jtag_interface to adapter_driver
[openocd.git] / src / jtag / drivers / cmsis_dap_usb.c
1 /***************************************************************************
2 * Copyright (C) 2016 by Maksym Hilliaka *
3 * oter@frozen-team.com *
4 * *
5 * Copyright (C) 2016 by Phillip Pearson *
6 * pp@myelin.co.nz *
7 * *
8 * Copyright (C) 2014 by Paul Fertser *
9 * fercerpav@gmail.com *
10 * *
11 * Copyright (C) 2013 by mike brown *
12 * mike@theshedworks.org.uk *
13 * *
14 * Copyright (C) 2013 by Spencer Oliver *
15 * spen@spen-soft.co.uk *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
29 ***************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <transport/transport.h>
36 #include <jtag/swd.h>
37 #include <jtag/interface.h>
38 #include <jtag/commands.h>
39 #include <jtag/tcl.h>
40
41 #include <hidapi.h>
42
43 /*
44 * See CMSIS-DAP documentation:
45 * Version 0.01 - Beta.
46 */
47
48 /* USB Config */
49
50 /* Known vid/pid pairs:
51 * VID 0xc251: Keil Software
52 * PID 0xf001: LPC-Link-II CMSIS_DAP
53 * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
54 * PID 0x2722: Keil ULINK2 CMSIS-DAP
55 *
56 * VID 0x0d28: mbed Software
57 * PID 0x0204: MBED CMSIS-DAP
58 */
59
60 #define MAX_USB_IDS 8
61 /* vid = pid = 0 marks the end of the list */
62 static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
63 static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
64 static wchar_t *cmsis_dap_serial;
65 static bool swd_mode;
66
67 #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
68 #define USB_TIMEOUT 1000
69
70 /* CMSIS-DAP General Commands */
71 #define CMD_DAP_INFO 0x00
72 #define CMD_DAP_LED 0x01
73 #define CMD_DAP_CONNECT 0x02
74 #define CMD_DAP_DISCONNECT 0x03
75 #define CMD_DAP_WRITE_ABORT 0x08
76 #define CMD_DAP_DELAY 0x09
77 #define CMD_DAP_RESET_TARGET 0x0A
78
79 /* CMD_INFO */
80 #define INFO_ID_VENDOR 0x01 /* string */
81 #define INFO_ID_PRODUCT 0x02 /* string */
82 #define INFO_ID_SERNUM 0x03 /* string */
83 #define INFO_ID_FW_VER 0x04 /* string */
84 #define INFO_ID_TD_VEND 0x05 /* string */
85 #define INFO_ID_TD_NAME 0x06 /* string */
86 #define INFO_ID_CAPS 0xf0 /* byte */
87 #define INFO_ID_PKT_CNT 0xfe /* byte */
88 #define INFO_ID_PKT_SZ 0xff /* short */
89
90 #define INFO_CAPS_SWD 0x01
91 #define INFO_CAPS_JTAG 0x02
92
93 /* CMD_LED */
94 #define LED_ID_CONNECT 0x00
95 #define LED_ID_RUN 0x01
96
97 #define LED_OFF 0x00
98 #define LED_ON 0x01
99
100 /* CMD_CONNECT */
101 #define CONNECT_DEFAULT 0x00
102 #define CONNECT_SWD 0x01
103 #define CONNECT_JTAG 0x02
104
105 /* CMSIS-DAP Common SWD/JTAG Commands */
106 #define CMD_DAP_DELAY 0x09
107 #define CMD_DAP_SWJ_PINS 0x10
108 #define CMD_DAP_SWJ_CLOCK 0x11
109 #define CMD_DAP_SWJ_SEQ 0x12
110
111 /*
112 * PINS
113 * Bit 0: SWCLK/TCK
114 * Bit 1: SWDIO/TMS
115 * Bit 2: TDI
116 * Bit 3: TDO
117 * Bit 5: nTRST
118 * Bit 7: nRESET
119 */
120
121 #define SWJ_PIN_TCK (1<<0)
122 #define SWJ_PIN_TMS (1<<1)
123 #define SWJ_PIN_TDI (1<<2)
124 #define SWJ_PIN_TDO (1<<3)
125 #define SWJ_PIN_TRST (1<<5)
126 #define SWJ_PIN_SRST (1<<7)
127
128 /* CMSIS-DAP SWD Commands */
129 #define CMD_DAP_SWD_CONFIGURE 0x13
130
131 /* CMSIS-DAP JTAG Commands */
132 #define CMD_DAP_JTAG_SEQ 0x14
133 #define CMD_DAP_JTAG_CONFIGURE 0x15
134 #define CMD_DAP_JTAG_IDCODE 0x16
135
136 /* CMSIS-DAP JTAG sequence info masks */
137 /* Number of bits to clock through (0 means 64) */
138 #define DAP_JTAG_SEQ_TCK 0x3F
139 /* TMS will be set during the sequence if this bit is set */
140 #define DAP_JTAG_SEQ_TMS 0x40
141 /* TDO output will be captured if this bit is set */
142 #define DAP_JTAG_SEQ_TDO 0x80
143
144
145 /* CMSIS-DAP Transfer Commands */
146 #define CMD_DAP_TFER_CONFIGURE 0x04
147 #define CMD_DAP_TFER 0x05
148 #define CMD_DAP_TFER_BLOCK 0x06
149 #define CMD_DAP_TFER_ABORT 0x07
150
151 /* DAP Status Code */
152 #define DAP_OK 0
153 #define DAP_ERROR 0xFF
154
155 /* CMSIS-DAP Vendor Commands
156 * None as yet... */
157
158 static const char * const info_caps_str[] = {
159 "SWD Supported",
160 "JTAG Supported"
161 };
162
163 /* max clock speed (kHz) */
164 #define DAP_MAX_CLOCK 5000
165
166 struct cmsis_dap {
167 hid_device *dev_handle;
168 uint16_t packet_size;
169 int packet_count;
170 uint8_t *packet_buffer;
171 uint8_t caps;
172 uint8_t mode;
173 };
174
175 struct pending_transfer_result {
176 uint8_t cmd;
177 uint32_t data;
178 void *buffer;
179 };
180
181 struct pending_request_block {
182 struct pending_transfer_result *transfers;
183 int transfer_count;
184 };
185
186 struct pending_scan_result {
187 /** Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer. */
188 unsigned first;
189 /** Number of bits to read. */
190 unsigned length;
191 /** Location to store the result */
192 uint8_t *buffer;
193 /** Offset in the destination buffer */
194 unsigned buffer_offset;
195 };
196
197 /* Up to MIN(packet_count, MAX_PENDING_REQUESTS) requests may be issued
198 * until the first response arrives */
199 #define MAX_PENDING_REQUESTS 3
200
201 /* Pending requests are organized as a FIFO - circular buffer */
202 /* Each block in FIFO can contain up to pending_queue_len transfers */
203 static int pending_queue_len;
204 static struct pending_request_block pending_fifo[MAX_PENDING_REQUESTS];
205 static int pending_fifo_put_idx, pending_fifo_get_idx;
206 static int pending_fifo_block_count;
207
208 /* pointers to buffers that will receive jtag scan results on the next flush */
209 #define MAX_PENDING_SCAN_RESULTS 256
210 static int pending_scan_result_count;
211 static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS];
212
213 /* queued JTAG sequences that will be executed on the next flush */
214 #define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_size - 3)
215 static int queued_seq_count;
216 static int queued_seq_buf_end;
217 static int queued_seq_tdo_ptr;
218 static uint8_t queued_seq_buf[1024]; /* TODO: make dynamic / move into cmsis object */
219
220 static int queued_retval;
221
222 static uint8_t output_pins = SWJ_PIN_SRST | SWJ_PIN_TRST;
223
224 static struct cmsis_dap *cmsis_dap_handle;
225
226 static int cmsis_dap_usb_open(void)
227 {
228 hid_device *dev = NULL;
229 int i;
230 struct hid_device_info *devs, *cur_dev;
231 unsigned short target_vid, target_pid;
232 wchar_t *target_serial = NULL;
233
234 bool found = false;
235 bool serial_found = false;
236
237 target_vid = 0;
238 target_pid = 0;
239
240 /*
241 * The CMSIS-DAP specification stipulates:
242 * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
243 * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
244 */
245 devs = hid_enumerate(0x0, 0x0);
246 cur_dev = devs;
247 while (NULL != cur_dev) {
248 if (0 == cmsis_dap_vid[0]) {
249 if (NULL == cur_dev->product_string) {
250 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
251 cur_dev->vendor_id, cur_dev->product_id);
252 } else {
253 if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
254 /* if the user hasn't specified VID:PID *and*
255 * product string contains "CMSIS-DAP", pick it
256 */
257 found = true;
258 }
259 }
260 } else {
261 /* otherwise, exhaustively compare against all VID:PID in list */
262 for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
263 if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
264 found = true;
265 }
266
267 if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
268 found = true;
269 }
270
271 if (found) {
272 /* we have found an adapter, so exit further checks */
273 /* check serial number matches if given */
274 if (cmsis_dap_serial != NULL) {
275 if ((cur_dev->serial_number != NULL) && wcscmp(cmsis_dap_serial, cur_dev->serial_number) == 0) {
276 serial_found = true;
277 break;
278 }
279 } else
280 break;
281
282 found = false;
283 }
284
285 cur_dev = cur_dev->next;
286 }
287
288 if (NULL != cur_dev) {
289 target_vid = cur_dev->vendor_id;
290 target_pid = cur_dev->product_id;
291 if (serial_found)
292 target_serial = cmsis_dap_serial;
293 }
294
295 hid_free_enumeration(devs);
296
297 if (target_vid == 0 && target_pid == 0) {
298 LOG_ERROR("unable to find CMSIS-DAP device");
299 return ERROR_FAIL;
300 }
301
302 if (hid_init() != 0) {
303 LOG_ERROR("unable to open HIDAPI");
304 return ERROR_FAIL;
305 }
306
307 dev = hid_open(target_vid, target_pid, target_serial);
308
309 if (dev == NULL) {
310 LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
311 return ERROR_FAIL;
312 }
313
314 struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
315 if (dap == NULL) {
316 LOG_ERROR("unable to allocate memory");
317 return ERROR_FAIL;
318 }
319
320 dap->dev_handle = dev;
321 dap->caps = 0;
322 dap->mode = 0;
323
324 cmsis_dap_handle = dap;
325
326 /* allocate default packet buffer, may be changed later.
327 * currently with HIDAPI we have no way of getting the output report length
328 * without this info we cannot communicate with the adapter.
329 * For the moment we ahve to hard code the packet size */
330
331 int packet_size = PACKET_SIZE;
332
333 /* atmel cmsis-dap uses 512 byte reports */
334 /* except when it doesn't e.g. with mEDBG on SAMD10 Xplained
335 * board */
336 /* TODO: HID report descriptor should be parsed instead of
337 * hardcoding a match by VID */
338 if (target_vid == 0x03eb && target_pid != 0x2145)
339 packet_size = 512 + 1;
340
341 cmsis_dap_handle->packet_buffer = malloc(packet_size);
342 cmsis_dap_handle->packet_size = packet_size;
343
344 if (cmsis_dap_handle->packet_buffer == NULL) {
345 LOG_ERROR("unable to allocate memory");
346 return ERROR_FAIL;
347 }
348
349 return ERROR_OK;
350 }
351
352 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
353 {
354 hid_close(dap->dev_handle);
355 hid_exit();
356
357 free(cmsis_dap_handle->packet_buffer);
358 free(cmsis_dap_handle);
359 cmsis_dap_handle = NULL;
360 free(cmsis_dap_serial);
361 cmsis_dap_serial = NULL;
362
363 for (int i = 0; i < MAX_PENDING_REQUESTS; i++) {
364 free(pending_fifo[i].transfers);
365 pending_fifo[i].transfers = NULL;
366 }
367
368 return;
369 }
370
371 static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen)
372 {
373 #ifdef CMSIS_DAP_JTAG_DEBUG
374 LOG_DEBUG("cmsis-dap usb xfer cmd=%02X", dap->packet_buffer[1]);
375 #endif
376 /* Pad the rest of the TX buffer with 0's */
377 memset(dap->packet_buffer + txlen, 0, dap->packet_size - txlen);
378
379 /* write data to device */
380 int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
381 if (retval == -1) {
382 LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
383 return ERROR_FAIL;
384 }
385
386 return ERROR_OK;
387 }
388
389 /* Send a message and receive the reply */
390 static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
391 {
392 if (pending_fifo_block_count) {
393 LOG_ERROR("pending %d blocks, flushing", pending_fifo_block_count);
394 while (pending_fifo_block_count) {
395 hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, 10);
396 pending_fifo_block_count--;
397 }
398 pending_fifo_put_idx = 0;
399 pending_fifo_get_idx = 0;
400 }
401
402 int retval = cmsis_dap_usb_write(dap, txlen);
403 if (retval != ERROR_OK)
404 return retval;
405
406 /* get reply */
407 retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
408 if (retval == -1 || retval == 0) {
409 LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
410 return ERROR_FAIL;
411 }
412
413 return ERROR_OK;
414 }
415
416 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
417 {
418 int retval;
419 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
420
421 buffer[0] = 0; /* report number */
422 buffer[1] = CMD_DAP_SWJ_PINS;
423 buffer[2] = pins;
424 buffer[3] = mask;
425 buffer[4] = delay & 0xff;
426 buffer[5] = (delay >> 8) & 0xff;
427 buffer[6] = (delay >> 16) & 0xff;
428 buffer[7] = (delay >> 24) & 0xff;
429 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
430
431 if (retval != ERROR_OK) {
432 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
433 return ERROR_JTAG_DEVICE_ERROR;
434 }
435
436 if (input)
437 *input = buffer[1];
438
439 return ERROR_OK;
440 }
441
442 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
443 {
444 int retval;
445 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
446
447 /* set clock in Hz */
448 swj_clock *= 1000;
449 buffer[0] = 0; /* report number */
450 buffer[1] = CMD_DAP_SWJ_CLOCK;
451 buffer[2] = swj_clock & 0xff;
452 buffer[3] = (swj_clock >> 8) & 0xff;
453 buffer[4] = (swj_clock >> 16) & 0xff;
454 buffer[5] = (swj_clock >> 24) & 0xff;
455 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
456
457 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
458 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
459 return ERROR_JTAG_DEVICE_ERROR;
460 }
461
462 return ERROR_OK;
463 }
464
465 /* clock a sequence of bits out on TMS, to change JTAG states */
466 static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence)
467 {
468 int retval;
469 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
470
471 #ifdef CMSIS_DAP_JTAG_DEBUG
472 LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
473 for (int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
474 printf("%02X ", sequence[i]);
475
476 printf("\n");
477 #endif
478
479 buffer[0] = 0; /* report number */
480 buffer[1] = CMD_DAP_SWJ_SEQ;
481 buffer[2] = s_len;
482 bit_copy(&buffer[3], 0, sequence, 0, s_len);
483
484 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, DIV_ROUND_UP(s_len, 8) + 3);
485
486 if (retval != ERROR_OK || buffer[1] != DAP_OK)
487 return ERROR_FAIL;
488
489 return ERROR_OK;
490 }
491
492 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
493 {
494 int retval;
495 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
496
497 buffer[0] = 0; /* report number */
498 buffer[1] = CMD_DAP_INFO;
499 buffer[2] = info;
500 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
501
502 if (retval != ERROR_OK) {
503 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
504 return ERROR_JTAG_DEVICE_ERROR;
505 }
506
507 *data = &(buffer[1]);
508
509 return ERROR_OK;
510 }
511
512 static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
513 {
514 int retval;
515 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
516
517 buffer[0] = 0; /* report number */
518 buffer[1] = CMD_DAP_LED;
519 buffer[2] = 0x00;
520 buffer[3] = leds;
521 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
522
523 if (retval != ERROR_OK || buffer[1] != 0x00) {
524 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
525 return ERROR_JTAG_DEVICE_ERROR;
526 }
527
528 return ERROR_OK;
529 }
530
531 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
532 {
533 int retval;
534 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
535
536 buffer[0] = 0; /* report number */
537 buffer[1] = CMD_DAP_CONNECT;
538 buffer[2] = mode;
539 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
540
541 if (retval != ERROR_OK) {
542 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
543 return ERROR_JTAG_DEVICE_ERROR;
544 }
545
546 if (buffer[1] != mode) {
547 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
548 return ERROR_JTAG_DEVICE_ERROR;
549 }
550
551 return ERROR_OK;
552 }
553
554 static int cmsis_dap_cmd_DAP_Disconnect(void)
555 {
556 int retval;
557 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
558
559 buffer[0] = 0; /* report number */
560 buffer[1] = CMD_DAP_DISCONNECT;
561 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
562
563 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
564 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
565 return ERROR_JTAG_DEVICE_ERROR;
566 }
567
568 return ERROR_OK;
569 }
570
571 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
572 {
573 int retval;
574 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
575
576 buffer[0] = 0; /* report number */
577 buffer[1] = CMD_DAP_TFER_CONFIGURE;
578 buffer[2] = idle;
579 buffer[3] = retry_count & 0xff;
580 buffer[4] = (retry_count >> 8) & 0xff;
581 buffer[5] = match_retry & 0xff;
582 buffer[6] = (match_retry >> 8) & 0xff;
583 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
584
585 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
586 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
587 return ERROR_JTAG_DEVICE_ERROR;
588 }
589
590 return ERROR_OK;
591 }
592
593 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
594 {
595 int retval;
596 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
597
598 buffer[0] = 0; /* report number */
599 buffer[1] = CMD_DAP_SWD_CONFIGURE;
600 buffer[2] = cfg;
601 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
602
603 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
604 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
605 return ERROR_JTAG_DEVICE_ERROR;
606 }
607
608 return ERROR_OK;
609 }
610
611 #if 0
612 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
613 {
614 int retval;
615 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
616
617 buffer[0] = 0; /* report number */
618 buffer[1] = CMD_DAP_DELAY;
619 buffer[2] = delay_us & 0xff;
620 buffer[3] = (delay_us >> 8) & 0xff;
621 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
622
623 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
624 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
625 return ERROR_JTAG_DEVICE_ERROR;
626 }
627
628 return ERROR_OK;
629 }
630 #endif
631
632 static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
633 {
634 uint8_t *buffer = dap->packet_buffer;
635 struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
636
637 LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %d", block->transfer_count, pending_fifo_put_idx);
638
639 if (queued_retval != ERROR_OK) {
640 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
641 goto skip;
642 }
643
644 if (block->transfer_count == 0)
645 goto skip;
646
647 size_t idx = 0;
648 buffer[idx++] = 0; /* report number */
649 buffer[idx++] = CMD_DAP_TFER;
650 buffer[idx++] = 0x00; /* DAP Index */
651 buffer[idx++] = block->transfer_count;
652
653 for (int i = 0; i < block->transfer_count; i++) {
654 struct pending_transfer_result *transfer = &(block->transfers[i]);
655 uint8_t cmd = transfer->cmd;
656 uint32_t data = transfer->data;
657
658 LOG_DEBUG_IO("%s %s reg %x %"PRIx32,
659 cmd & SWD_CMD_APnDP ? "AP" : "DP",
660 cmd & SWD_CMD_RnW ? "read" : "write",
661 (cmd & SWD_CMD_A32) >> 1, data);
662
663 /* When proper WAIT handling is implemented in the
664 * common SWD framework, this kludge can be
665 * removed. However, this might lead to minor
666 * performance degradation as the adapter wouldn't be
667 * able to automatically retry anything (because ARM
668 * has forgotten to implement sticky error flags
669 * clearing). See also comments regarding
670 * cmsis_dap_cmd_DAP_TFER_Configure() and
671 * cmsis_dap_cmd_DAP_SWD_Configure() in
672 * cmsis_dap_init().
673 */
674 if (!(cmd & SWD_CMD_RnW) &&
675 !(cmd & SWD_CMD_APnDP) &&
676 (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
677 (data & CORUNDETECT)) {
678 LOG_DEBUG("refusing to enable sticky overrun detection");
679 data &= ~CORUNDETECT;
680 }
681
682 buffer[idx++] = (cmd >> 1) & 0x0f;
683 if (!(cmd & SWD_CMD_RnW)) {
684 buffer[idx++] = (data) & 0xff;
685 buffer[idx++] = (data >> 8) & 0xff;
686 buffer[idx++] = (data >> 16) & 0xff;
687 buffer[idx++] = (data >> 24) & 0xff;
688 }
689 }
690
691 queued_retval = cmsis_dap_usb_write(dap, idx);
692 if (queued_retval != ERROR_OK)
693 goto skip;
694
695 pending_fifo_put_idx = (pending_fifo_put_idx + 1) % dap->packet_count;
696 pending_fifo_block_count++;
697 if (pending_fifo_block_count > dap->packet_count)
698 LOG_ERROR("too much pending writes %d", pending_fifo_block_count);
699
700 return;
701
702 skip:
703 block->transfer_count = 0;
704 }
705
706 static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
707 {
708 uint8_t *buffer = dap->packet_buffer;
709 struct pending_request_block *block = &pending_fifo[pending_fifo_get_idx];
710
711 if (pending_fifo_block_count == 0)
712 LOG_ERROR("no pending write");
713
714 /* get reply */
715 int retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, timeout_ms);
716 if (retval == 0 && timeout_ms < USB_TIMEOUT)
717 return;
718
719 if (retval == -1 || retval == 0) {
720 LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
721 queued_retval = ERROR_FAIL;
722 goto skip;
723 }
724
725 if (buffer[2] & 0x08) {
726 LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", buffer[1]);
727 queued_retval = ERROR_FAIL;
728 goto skip;
729 }
730 uint8_t ack = buffer[2] & 0x07;
731 if (ack != SWD_ACK_OK) {
732 LOG_DEBUG("SWD ack not OK @ %d %s", buffer[1],
733 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
734 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
735 goto skip;
736 }
737
738 if (block->transfer_count != buffer[1])
739 LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
740 block->transfer_count, buffer[1]);
741
742 LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %d", buffer[1], pending_fifo_get_idx);
743 size_t idx = 3;
744 for (int i = 0; i < buffer[1]; i++) {
745 struct pending_transfer_result *transfer = &(block->transfers[i]);
746 if (transfer->cmd & SWD_CMD_RnW) {
747 static uint32_t last_read;
748 uint32_t data = le_to_h_u32(&buffer[idx]);
749 uint32_t tmp = data;
750 idx += 4;
751
752 LOG_DEBUG_IO("Read result: %"PRIx32, data);
753
754 /* Imitate posted AP reads */
755 if ((transfer->cmd & SWD_CMD_APnDP) ||
756 ((transfer->cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
757 tmp = last_read;
758 last_read = data;
759 }
760
761 if (transfer->buffer)
762 *(uint32_t *)(transfer->buffer) = tmp;
763 }
764 }
765
766 skip:
767 block->transfer_count = 0;
768 pending_fifo_get_idx = (pending_fifo_get_idx + 1) % dap->packet_count;
769 pending_fifo_block_count--;
770 }
771
772 static int cmsis_dap_swd_run_queue(void)
773 {
774 if (pending_fifo_block_count)
775 cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
776
777 cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
778
779 while (pending_fifo_block_count)
780 cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
781
782 pending_fifo_put_idx = 0;
783 pending_fifo_get_idx = 0;
784
785 int retval = queued_retval;
786 queued_retval = ERROR_OK;
787
788 return retval;
789 }
790
791 static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
792 {
793 if (pending_fifo[pending_fifo_put_idx].transfer_count == pending_queue_len) {
794 if (pending_fifo_block_count)
795 cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
796
797 /* Not enough room in the queue. Run the queue. */
798 cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
799
800 if (pending_fifo_block_count >= cmsis_dap_handle->packet_count)
801 cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
802 }
803
804 if (queued_retval != ERROR_OK)
805 return;
806
807 struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
808 struct pending_transfer_result *transfer = &(block->transfers[block->transfer_count]);
809 transfer->data = data;
810 transfer->cmd = cmd;
811 if (cmd & SWD_CMD_RnW) {
812 /* Queue a read transaction */
813 transfer->buffer = dst;
814 }
815 block->transfer_count++;
816 }
817
818 static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
819 {
820 assert(!(cmd & SWD_CMD_RnW));
821 cmsis_dap_swd_queue_cmd(cmd, NULL, value);
822 }
823
824 static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
825 {
826 assert(cmd & SWD_CMD_RnW);
827 cmsis_dap_swd_queue_cmd(cmd, value, 0);
828 }
829
830 static int cmsis_dap_get_serial_info(void)
831 {
832 uint8_t *data;
833
834 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_SERNUM, &data);
835 if (retval != ERROR_OK)
836 return retval;
837
838 if (data[0]) /* strlen */
839 LOG_INFO("CMSIS-DAP: Serial# = %s", &data[1]);
840
841 return ERROR_OK;
842 }
843
844 static int cmsis_dap_get_version_info(void)
845 {
846 uint8_t *data;
847
848 /* INFO_ID_FW_VER - string */
849 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
850 if (retval != ERROR_OK)
851 return retval;
852
853 if (data[0]) /* strlen */
854 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
855
856 return ERROR_OK;
857 }
858
859 static int cmsis_dap_get_caps_info(void)
860 {
861 uint8_t *data;
862
863 /* INFO_ID_CAPS - byte */
864 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
865 if (retval != ERROR_OK)
866 return retval;
867
868 if (data[0] == 1) {
869 uint8_t caps = data[1];
870
871 cmsis_dap_handle->caps = caps;
872
873 if (caps & INFO_CAPS_SWD)
874 LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
875 if (caps & INFO_CAPS_JTAG)
876 LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
877 }
878
879 return ERROR_OK;
880 }
881
882 static int cmsis_dap_get_status(void)
883 {
884 uint8_t d;
885
886 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
887
888 if (retval == ERROR_OK) {
889 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
890 (d & SWJ_PIN_TCK) ? 1 : 0,
891 (d & SWJ_PIN_TMS) ? 1 : 0,
892 (d & SWJ_PIN_TDI) ? 1 : 0,
893 (d & SWJ_PIN_TDO) ? 1 : 0,
894 (d & SWJ_PIN_TRST) ? 1 : 0,
895 (d & SWJ_PIN_SRST) ? 1 : 0);
896 }
897
898 return retval;
899 }
900
901 static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
902 {
903 const uint8_t *s;
904 unsigned int s_len;
905 int retval;
906
907 if ((output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST)) == (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
908 /* Following workaround deasserts reset on most adapters.
909 * Do not reconnect if a reset line is active!
910 * Reconnecting would break connecting under reset. */
911
912 /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
913 cmsis_dap_cmd_DAP_Disconnect();
914
915 /* When we are reconnecting, DAP_Connect needs to be rerun, at
916 * least on Keil ULINK-ME */
917 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
918 if (retval != ERROR_OK)
919 return retval;
920 }
921
922 switch (seq) {
923 case LINE_RESET:
924 LOG_DEBUG("SWD line reset");
925 s = swd_seq_line_reset;
926 s_len = swd_seq_line_reset_len;
927 break;
928 case JTAG_TO_SWD:
929 LOG_DEBUG("JTAG-to-SWD");
930 s = swd_seq_jtag_to_swd;
931 s_len = swd_seq_jtag_to_swd_len;
932 break;
933 case SWD_TO_JTAG:
934 LOG_DEBUG("SWD-to-JTAG");
935 s = swd_seq_swd_to_jtag;
936 s_len = swd_seq_swd_to_jtag_len;
937 break;
938 default:
939 LOG_ERROR("Sequence %d not supported", seq);
940 return ERROR_FAIL;
941 }
942
943 retval = cmsis_dap_cmd_DAP_SWJ_Sequence(s_len, s);
944 if (retval != ERROR_OK)
945 return retval;
946
947 /* Atmel EDBG needs renew clock setting after SWJ_Sequence
948 * otherwise default frequency is used */
949 return cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
950 }
951
952 static int cmsis_dap_swd_open(void)
953 {
954 int retval;
955
956 if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
957 LOG_ERROR("CMSIS-DAP: SWD not supported");
958 return ERROR_JTAG_DEVICE_ERROR;
959 }
960
961 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
962 if (retval != ERROR_OK)
963 return retval;
964
965 /* Add more setup here.??... */
966
967 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
968 return ERROR_OK;
969 }
970
971 static int cmsis_dap_init(void)
972 {
973 int retval;
974 uint8_t *data;
975
976 retval = cmsis_dap_usb_open();
977 if (retval != ERROR_OK)
978 return retval;
979
980 retval = cmsis_dap_get_caps_info();
981 if (retval != ERROR_OK)
982 return retval;
983
984 retval = cmsis_dap_get_version_info();
985 if (retval != ERROR_OK)
986 return retval;
987
988 retval = cmsis_dap_get_serial_info();
989 if (retval != ERROR_OK)
990 return retval;
991
992 if (swd_mode) {
993 retval = cmsis_dap_swd_open();
994 if (retval != ERROR_OK)
995 return retval;
996 } else {
997 /* Connect in JTAG mode */
998 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
999 LOG_ERROR("CMSIS-DAP: JTAG not supported");
1000 return ERROR_JTAG_DEVICE_ERROR;
1001 }
1002
1003 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
1004 if (retval != ERROR_OK)
1005 return retval;
1006
1007 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
1008 }
1009
1010 /* Be conservative and supress submiting multiple HID requests
1011 * until we get packet count info from the adaptor */
1012 cmsis_dap_handle->packet_count = 1;
1013 pending_queue_len = 12;
1014
1015 /* INFO_ID_PKT_SZ - short */
1016 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
1017 if (retval != ERROR_OK)
1018 return retval;
1019
1020 if (data[0] == 2) { /* short */
1021 uint16_t pkt_sz = data[1] + (data[2] << 8);
1022
1023 /* 4 bytes of command header + 5 bytes per register
1024 * write. For bulk read sequences just 4 bytes are
1025 * needed per transfer, so this is suboptimal. */
1026 pending_queue_len = (pkt_sz - 4) / 5;
1027
1028 if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
1029 /* reallocate buffer */
1030 cmsis_dap_handle->packet_size = pkt_sz + 1;
1031 cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
1032 cmsis_dap_handle->packet_size);
1033 if (cmsis_dap_handle->packet_buffer == NULL) {
1034 LOG_ERROR("unable to reallocate memory");
1035 return ERROR_FAIL;
1036 }
1037 }
1038
1039 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
1040 }
1041
1042 /* INFO_ID_PKT_CNT - byte */
1043 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
1044 if (retval != ERROR_OK)
1045 return retval;
1046
1047 if (data[0] == 1) { /* byte */
1048 int pkt_cnt = data[1];
1049 if (pkt_cnt > 1)
1050 cmsis_dap_handle->packet_count = MIN(MAX_PENDING_REQUESTS, pkt_cnt);
1051
1052 LOG_DEBUG("CMSIS-DAP: Packet Count = %d", pkt_cnt);
1053 }
1054
1055 LOG_DEBUG("Allocating FIFO for %d pending HID requests", cmsis_dap_handle->packet_count);
1056 for (int i = 0; i < cmsis_dap_handle->packet_count; i++) {
1057 pending_fifo[i].transfers = malloc(pending_queue_len * sizeof(struct pending_transfer_result));
1058 if (!pending_fifo[i].transfers) {
1059 LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
1060 return ERROR_FAIL;
1061 }
1062 }
1063
1064
1065 retval = cmsis_dap_get_status();
1066 if (retval != ERROR_OK)
1067 return ERROR_FAIL;
1068
1069 /* Now try to connect to the target
1070 * TODO: This is all SWD only @ present */
1071 retval = cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
1072 if (retval != ERROR_OK)
1073 return ERROR_FAIL;
1074
1075 /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
1076 * up to 64 times. This must be changed to 0 if sticky
1077 * overrun detection is enabled. */
1078 retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
1079 if (retval != ERROR_OK)
1080 return ERROR_FAIL;
1081
1082 if (swd_mode) {
1083 /* Data Phase (bit 2) must be set to 1 if sticky overrun
1084 * detection is enabled */
1085 retval = cmsis_dap_cmd_DAP_SWD_Configure(0); /* 1 TRN, no Data Phase */
1086 if (retval != ERROR_OK)
1087 return ERROR_FAIL;
1088 }
1089
1090 retval = cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
1091 if (retval != ERROR_OK)
1092 return ERROR_FAIL;
1093
1094 /* support connecting with srst asserted */
1095 enum reset_types jtag_reset_config = jtag_get_reset_config();
1096
1097 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1098 if (jtag_reset_config & RESET_SRST_NO_GATING) {
1099 retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, SWJ_PIN_SRST, 0, NULL);
1100 if (retval != ERROR_OK)
1101 return ERROR_FAIL;
1102 LOG_INFO("Connecting under reset");
1103 }
1104 }
1105
1106 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
1107
1108 LOG_INFO("CMSIS-DAP: Interface ready");
1109
1110 return ERROR_OK;
1111 }
1112
1113 static int cmsis_dap_swd_init(void)
1114 {
1115 swd_mode = true;
1116 return ERROR_OK;
1117 }
1118
1119 static int cmsis_dap_quit(void)
1120 {
1121 cmsis_dap_cmd_DAP_Disconnect();
1122 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
1123
1124 cmsis_dap_usb_close(cmsis_dap_handle);
1125
1126 return ERROR_OK;
1127 }
1128
1129 static int cmsis_dap_reset(int trst, int srst)
1130 {
1131 /* Set both TRST and SRST even if they're not enabled as
1132 * there's no way to tristate them */
1133
1134 output_pins = 0;
1135 if (!srst)
1136 output_pins |= SWJ_PIN_SRST;
1137 if (!trst)
1138 output_pins |= SWJ_PIN_TRST;
1139
1140 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(output_pins,
1141 SWJ_PIN_TRST | SWJ_PIN_SRST, 0, NULL);
1142 if (retval != ERROR_OK)
1143 LOG_ERROR("CMSIS-DAP: Interface reset failed");
1144 return retval;
1145 }
1146
1147 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
1148 {
1149 #if 0
1150 int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
1151 if (retval != ERROR_OK)
1152 #endif
1153 jtag_sleep(cmd->cmd.sleep->us);
1154 }
1155
1156 /* Set TMS high for five TCK clocks, to move the TAP to the Test-Logic-Reset state */
1157 static int cmsis_dap_execute_tlr_reset(struct jtag_command *cmd)
1158 {
1159 LOG_INFO("cmsis-dap JTAG TLR_RESET");
1160 uint8_t seq = 0xff;
1161 int ret = cmsis_dap_cmd_DAP_SWJ_Sequence(8, &seq);
1162 if (ret == ERROR_OK)
1163 tap_set_state(TAP_RESET);
1164 return ret;
1165 }
1166
1167 /* Set new end state */
1168 static void cmsis_dap_end_state(tap_state_t state)
1169 {
1170 if (tap_is_state_stable(state))
1171 tap_set_end_state(state);
1172 else {
1173 LOG_ERROR("BUG: %i is not a valid end state", state);
1174 exit(-1);
1175 }
1176 }
1177
1178 #ifdef SPRINT_BINARY
1179 static void sprint_binary(char *s, const uint8_t *buf, int offset, int len)
1180 {
1181 if (!len)
1182 return;
1183
1184 /*
1185 buf = { 0x18 } len=5 should result in: 11000
1186 buf = { 0xff 0x18 } len=13 should result in: 11111111 11000
1187 buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
1188 i=3 there means i/8 = 0 so c = 0xFF, and
1189 */
1190 for (int i = offset; i < offset + len; ++i) {
1191 uint8_t c = buf[i / 8], mask = 1 << (i % 8);
1192 if ((i != offset) && !(i % 8))
1193 putchar(' ');
1194 *s++ = (c & mask) ? '1' : '0';
1195 }
1196 *s = 0;
1197 }
1198 #endif
1199
1200 #ifdef CMSIS_DAP_JTAG_DEBUG
1201 static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
1202 {
1203 /* cmd is a usb packet to go to the cmsis-dap interface */
1204 printf("cmsis-dap buffer (%d b): ", cmdlen);
1205 for (int i = 0; i < cmdlen; ++i)
1206 printf(" %02x", cmd[i]);
1207 printf("\n");
1208 switch (cmd[1]) {
1209 case CMD_DAP_JTAG_SEQ: {
1210 printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[1], cmd[2]);
1211 /*
1212 * #2 = number of sequences
1213 * #3 = sequence info 1
1214 * #4...4+n_bytes-1 = sequence 1
1215 * #4+n_bytes = sequence info 2
1216 * #5+n_bytes = sequence 2 (single bit)
1217 */
1218 int pos = 3;
1219 for (int seq = 0; seq < cmd[2]; ++seq) {
1220 uint8_t info = cmd[pos++];
1221 int len = info & DAP_JTAG_SEQ_TCK;
1222 if (len == 0)
1223 len = 64;
1224 printf(" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
1225 seq, pos, info, len, info & DAP_JTAG_SEQ_TMS, info & DAP_JTAG_SEQ_TDO);
1226 for (int i = 0; i < DIV_ROUND_UP(len, 8); ++i)
1227 printf(" %02x", cmd[pos+i]);
1228 pos += DIV_ROUND_UP(len, 8);
1229 printf("\n");
1230 }
1231 if (pos != cmdlen) {
1232 printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
1233 exit(-1);
1234 }
1235
1236 break;
1237 }
1238 default:
1239 LOG_DEBUG("unknown cmsis-dap command %02x", cmd[1]);
1240 break;
1241 }
1242 }
1243 #endif
1244
1245 static void cmsis_dap_flush(void)
1246 {
1247 if (!queued_seq_count)
1248 return;
1249
1250 LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
1251 queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
1252
1253 /* prep CMSIS-DAP packet */
1254 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
1255 buffer[0] = 0; /* report number */
1256 buffer[1] = CMD_DAP_JTAG_SEQ;
1257 buffer[2] = queued_seq_count;
1258 memcpy(buffer + 3, queued_seq_buf, queued_seq_buf_end);
1259
1260 #ifdef CMSIS_DAP_JTAG_DEBUG
1261 debug_parse_cmsis_buf(buffer, queued_seq_buf_end + 3);
1262 #endif
1263
1264 /* send command to USB device */
1265 int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, queued_seq_buf_end + 3);
1266 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
1267 LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
1268 exit(-1);
1269 }
1270
1271 #ifdef CMSIS_DAP_JTAG_DEBUG
1272 LOG_DEBUG_IO("USB response buf:");
1273 for (int c = 0; c < queued_seq_buf_end + 3; ++c)
1274 printf("%02X ", buffer[c]);
1275 printf("\n");
1276 #endif
1277
1278 /* copy scan results into client buffers */
1279 for (int i = 0; i < pending_scan_result_count; ++i) {
1280 struct pending_scan_result *scan = &pending_scan_results[i];
1281 LOG_DEBUG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
1282 i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
1283 #ifdef CMSIS_DAP_JTAG_DEBUG
1284 for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
1285 printf("%02X ", buffer[2+scan->first+b]);
1286 printf("\n");
1287 #endif
1288 bit_copy(scan->buffer, scan->buffer_offset, buffer + 2 + scan->first, 0, scan->length);
1289 }
1290
1291 /* reset */
1292 queued_seq_count = 0;
1293 queued_seq_buf_end = 0;
1294 queued_seq_tdo_ptr = 0;
1295 pending_scan_result_count = 0;
1296 }
1297
1298 /* queue a sequence of bits to clock out TDI / in TDO, executing if the buffer is full.
1299 *
1300 * sequence=NULL means clock out zeros on TDI
1301 * tdo_buffer=NULL means don't capture TDO
1302 */
1303 static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int s_offset,
1304 bool tms, uint8_t *tdo_buffer, int tdo_buffer_offset)
1305 {
1306 LOG_DEBUG_IO("[at %d] %d bits, tms %s, seq offset %d, tdo buf %p, tdo offset %d",
1307 queued_seq_buf_end,
1308 s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
1309
1310 if (s_len == 0)
1311 return;
1312
1313 if (s_len > 64) {
1314 LOG_DEBUG_IO("START JTAG SEQ SPLIT");
1315 for (int offset = 0; offset < s_len; offset += 64) {
1316 int len = s_len - offset;
1317 if (len > 64)
1318 len = 64;
1319 LOG_DEBUG_IO("Splitting long jtag sequence: %d-bit chunk starting at offset %d", len, offset);
1320 cmsis_dap_add_jtag_sequence(
1321 len,
1322 sequence,
1323 s_offset + offset,
1324 tms,
1325 tdo_buffer,
1326 tdo_buffer == NULL ? 0 : (tdo_buffer_offset + offset)
1327 );
1328 }
1329 LOG_DEBUG_IO("END JTAG SEQ SPLIT");
1330 return;
1331 }
1332
1333 int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
1334 if (queued_seq_count >= 255 || queued_seq_buf_end + cmd_len > QUEUED_SEQ_BUF_LEN)
1335 /* empty out the buffer */
1336 cmsis_dap_flush();
1337
1338 ++queued_seq_count;
1339
1340 /* control byte */
1341 queued_seq_buf[queued_seq_buf_end] =
1342 (tms ? DAP_JTAG_SEQ_TMS : 0) |
1343 (tdo_buffer != NULL ? DAP_JTAG_SEQ_TDO : 0) |
1344 (s_len == 64 ? 0 : s_len);
1345
1346 if (sequence != NULL)
1347 bit_copy(&queued_seq_buf[queued_seq_buf_end + 1], 0, sequence, s_offset, s_len);
1348 else
1349 memset(&queued_seq_buf[queued_seq_buf_end + 1], 0, DIV_ROUND_UP(s_len, 8));
1350
1351 queued_seq_buf_end += cmd_len;
1352
1353 if (tdo_buffer != NULL) {
1354 struct pending_scan_result *scan = &pending_scan_results[pending_scan_result_count++];
1355 scan->first = queued_seq_tdo_ptr;
1356 queued_seq_tdo_ptr += DIV_ROUND_UP(s_len, 8);
1357 scan->length = s_len;
1358 scan->buffer = tdo_buffer;
1359 scan->buffer_offset = tdo_buffer_offset;
1360 }
1361 }
1362
1363 /* queue a sequence of bits to clock out TMS, executing if the buffer is full */
1364 static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
1365 {
1366 LOG_DEBUG_IO("%d bits: %02X", s_len, *sequence);
1367 /* we use a series of CMD_DAP_JTAG_SEQ commands to toggle TMS,
1368 because even though it seems ridiculously inefficient, it
1369 allows us to combine TMS and scan sequences into the same
1370 USB packet. */
1371 /* TODO: combine runs of the same tms value */
1372 for (int i = 0; i < s_len; ++i) {
1373 bool bit = (sequence[i / 8] & (1 << (i % 8))) != 0;
1374 cmsis_dap_add_jtag_sequence(1, NULL, 0, bit, NULL, 0);
1375 }
1376 }
1377
1378 /* Move to the end state by queuing a sequence to clock into TMS */
1379 static void cmsis_dap_state_move(void)
1380 {
1381 uint8_t tms_scan;
1382 uint8_t tms_scan_bits;
1383
1384 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1385 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1386
1387 LOG_DEBUG_IO("state move from %s to %s: %d clocks, %02X on tms",
1388 tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()),
1389 tms_scan_bits, tms_scan);
1390 cmsis_dap_add_tms_sequence(&tms_scan, tms_scan_bits);
1391
1392 tap_set_state(tap_get_end_state());
1393 }
1394
1395
1396 /* Execute a JTAG scan operation by queueing TMS and TDI/TDO sequences */
1397 static void cmsis_dap_execute_scan(struct jtag_command *cmd)
1398 {
1399 LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
1400 jtag_scan_type(cmd->cmd.scan));
1401
1402 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
1403 while (cmd->cmd.scan->num_fields > 0
1404 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
1405 cmd->cmd.scan->num_fields--;
1406 LOG_DEBUG("discarding trailing empty field");
1407 }
1408
1409 if (cmd->cmd.scan->num_fields == 0) {
1410 LOG_DEBUG("empty scan, doing nothing");
1411 return;
1412 }
1413
1414 if (cmd->cmd.scan->ir_scan) {
1415 if (tap_get_state() != TAP_IRSHIFT) {
1416 cmsis_dap_end_state(TAP_IRSHIFT);
1417 cmsis_dap_state_move();
1418 }
1419 } else {
1420 if (tap_get_state() != TAP_DRSHIFT) {
1421 cmsis_dap_end_state(TAP_DRSHIFT);
1422 cmsis_dap_state_move();
1423 }
1424 }
1425
1426 cmsis_dap_end_state(cmd->cmd.scan->end_state);
1427
1428 struct scan_field *field = cmd->cmd.scan->fields;
1429 unsigned scan_size = 0;
1430
1431 for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
1432 scan_size += field->num_bits;
1433 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
1434 field->in_value ? "in" : "",
1435 field->out_value ? "out" : "",
1436 i,
1437 cmd->cmd.scan->num_fields,
1438 field->num_bits);
1439
1440 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
1441 LOG_DEBUG_IO("Last field and have to move out of SHIFT state");
1442 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
1443 * movement. This last field can't have length zero, it was checked above. */
1444 cmsis_dap_add_jtag_sequence(
1445 field->num_bits - 1, /* number of bits to clock */
1446 field->out_value, /* output sequence */
1447 0, /* output offset */
1448 false, /* TMS low */
1449 field->in_value,
1450 0);
1451
1452 /* Clock the last bit out, with TMS high */
1453 uint8_t last_bit = 0;
1454 if (field->out_value)
1455 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
1456 cmsis_dap_add_jtag_sequence(
1457 1,
1458 &last_bit,
1459 0,
1460 true,
1461 field->in_value,
1462 field->num_bits - 1);
1463 tap_set_state(tap_state_transition(tap_get_state(), 1));
1464
1465 /* Now clock one more cycle, with TMS low, to get us into a PAUSE state */
1466 cmsis_dap_add_jtag_sequence(
1467 1,
1468 &last_bit,
1469 0,
1470 false,
1471 NULL,
1472 0);
1473 tap_set_state(tap_state_transition(tap_get_state(), 0));
1474 } else {
1475 LOG_DEBUG_IO("Internal field, staying in SHIFT state afterwards");
1476 /* Clocking part of a sequence into DR or IR with TMS=0,
1477 leaving TMS=0 at the end so we can continue later */
1478 cmsis_dap_add_jtag_sequence(
1479 field->num_bits,
1480 field->out_value,
1481 0,
1482 false,
1483 field->in_value,
1484 0);
1485 }
1486 }
1487
1488 if (tap_get_state() != tap_get_end_state()) {
1489 cmsis_dap_end_state(tap_get_end_state());
1490 cmsis_dap_state_move();
1491 }
1492
1493 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
1494 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1495 tap_state_name(tap_get_end_state()));
1496 }
1497
1498 static void cmsis_dap_pathmove(int num_states, tap_state_t *path)
1499 {
1500 int i;
1501 uint8_t tms0 = 0x00;
1502 uint8_t tms1 = 0xff;
1503
1504 for (i = 0; i < num_states; i++) {
1505 if (path[i] == tap_state_transition(tap_get_state(), false))
1506 cmsis_dap_add_tms_sequence(&tms0, 1);
1507 else if (path[i] == tap_state_transition(tap_get_state(), true))
1508 cmsis_dap_add_tms_sequence(&tms1, 1);
1509 else {
1510 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
1511 tap_state_name(tap_get_state()), tap_state_name(path[i]));
1512 exit(-1);
1513 }
1514
1515 tap_set_state(path[i]);
1516 }
1517
1518 cmsis_dap_end_state(tap_get_state());
1519 }
1520
1521 static void cmsis_dap_execute_pathmove(struct jtag_command *cmd)
1522 {
1523 LOG_DEBUG_IO("pathmove: %i states, end in %i",
1524 cmd->cmd.pathmove->num_states,
1525 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1526
1527 cmsis_dap_pathmove(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
1528 }
1529
1530 static void cmsis_dap_stableclocks(int num_cycles)
1531 {
1532 int i;
1533
1534 uint8_t tms = tap_get_state() == TAP_RESET;
1535 /* TODO: Perform optimizations? */
1536 /* Execute num_cycles. */
1537 for (i = 0; i < num_cycles; i++)
1538 cmsis_dap_add_tms_sequence(&tms, 1);
1539 }
1540
1541 static void cmsis_dap_runtest(int num_cycles)
1542 {
1543 tap_state_t saved_end_state = tap_get_end_state();
1544
1545 /* Only do a state_move when we're not already in IDLE. */
1546 if (tap_get_state() != TAP_IDLE) {
1547 cmsis_dap_end_state(TAP_IDLE);
1548 cmsis_dap_state_move();
1549 }
1550 cmsis_dap_stableclocks(num_cycles);
1551
1552 /* Finish in end_state. */
1553 cmsis_dap_end_state(saved_end_state);
1554
1555 if (tap_get_state() != tap_get_end_state())
1556 cmsis_dap_state_move();
1557 }
1558
1559 static void cmsis_dap_execute_runtest(struct jtag_command *cmd)
1560 {
1561 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
1562 cmd->cmd.runtest->end_state);
1563
1564 cmsis_dap_end_state(cmd->cmd.runtest->end_state);
1565 cmsis_dap_runtest(cmd->cmd.runtest->num_cycles);
1566 }
1567
1568 static void cmsis_dap_execute_stableclocks(struct jtag_command *cmd)
1569 {
1570 LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
1571 cmsis_dap_stableclocks(cmd->cmd.runtest->num_cycles);
1572 }
1573
1574 static void cmsis_dap_execute_tms(struct jtag_command *cmd)
1575 {
1576 LOG_DEBUG_IO("TMS: %d bits", cmd->cmd.tms->num_bits);
1577 cmsis_dap_cmd_DAP_SWJ_Sequence(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
1578 }
1579
1580 /* TODO: Is there need to call cmsis_dap_flush() for the JTAG_PATHMOVE,
1581 * JTAG_RUNTEST, JTAG_STABLECLOCKS? */
1582 static void cmsis_dap_execute_command(struct jtag_command *cmd)
1583 {
1584 switch (cmd->type) {
1585 case JTAG_SLEEP:
1586 cmsis_dap_flush();
1587 cmsis_dap_execute_sleep(cmd);
1588 break;
1589 case JTAG_TLR_RESET:
1590 cmsis_dap_flush();
1591 cmsis_dap_execute_tlr_reset(cmd);
1592 break;
1593 case JTAG_SCAN:
1594 cmsis_dap_execute_scan(cmd);
1595 break;
1596 case JTAG_PATHMOVE:
1597 cmsis_dap_execute_pathmove(cmd);
1598 break;
1599 case JTAG_RUNTEST:
1600 cmsis_dap_execute_runtest(cmd);
1601 break;
1602 case JTAG_STABLECLOCKS:
1603 cmsis_dap_execute_stableclocks(cmd);
1604 break;
1605 case JTAG_TMS:
1606 cmsis_dap_execute_tms(cmd);
1607 break;
1608 default:
1609 LOG_ERROR("BUG: unknown JTAG command type 0x%X encountered", cmd->type);
1610 exit(-1);
1611 }
1612 }
1613
1614 static int cmsis_dap_execute_queue(void)
1615 {
1616 struct jtag_command *cmd = jtag_command_queue;
1617
1618 while (cmd != NULL) {
1619 cmsis_dap_execute_command(cmd);
1620 cmd = cmd->next;
1621 }
1622
1623 cmsis_dap_flush();
1624
1625 return ERROR_OK;
1626 }
1627
1628 static int cmsis_dap_speed(int speed)
1629 {
1630 if (speed > DAP_MAX_CLOCK)
1631 LOG_INFO("High speed (adapter_khz %d) may be limited by adapter firmware.", speed);
1632
1633 if (speed == 0) {
1634 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
1635 return ERROR_JTAG_NOT_IMPLEMENTED;
1636 }
1637
1638 return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
1639 }
1640
1641 static int cmsis_dap_speed_div(int speed, int *khz)
1642 {
1643 *khz = speed;
1644 return ERROR_OK;
1645 }
1646
1647 static int cmsis_dap_khz(int khz, int *jtag_speed)
1648 {
1649 *jtag_speed = khz;
1650 return ERROR_OK;
1651 }
1652
1653 COMMAND_HANDLER(cmsis_dap_handle_info_command)
1654 {
1655 if (cmsis_dap_get_version_info() == ERROR_OK)
1656 cmsis_dap_get_status();
1657
1658 return ERROR_OK;
1659 }
1660
1661 COMMAND_HANDLER(cmsis_dap_handle_cmd_command)
1662 {
1663 int retval;
1664 unsigned i;
1665 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
1666
1667 buffer[0] = 0; /* report number */
1668
1669 for (i = 0; i < CMD_ARGC; i++)
1670 buffer[i + 1] = strtoul(CMD_ARGV[i], NULL, 16);
1671
1672 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, CMD_ARGC + 1);
1673
1674 if (retval != ERROR_OK) {
1675 LOG_ERROR("CMSIS-DAP command failed.");
1676 return ERROR_JTAG_DEVICE_ERROR;
1677 }
1678
1679 LOG_INFO("Returned data %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8,
1680 buffer[1], buffer[2], buffer[3], buffer[4]);
1681
1682 return ERROR_OK;
1683 }
1684
1685 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
1686 {
1687 if (CMD_ARGC > MAX_USB_IDS * 2) {
1688 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1689 "(maximum is %d pairs)", MAX_USB_IDS);
1690 CMD_ARGC = MAX_USB_IDS * 2;
1691 }
1692 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
1693 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1694 if (CMD_ARGC < 2)
1695 return ERROR_COMMAND_SYNTAX_ERROR;
1696 /* remove the incomplete trailing id */
1697 CMD_ARGC -= 1;
1698 }
1699
1700 unsigned i;
1701 for (i = 0; i < CMD_ARGC; i += 2) {
1702 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
1703 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
1704 }
1705
1706 /*
1707 * Explicitly terminate, in case there are multiples instances of
1708 * cmsis_dap_vid_pid.
1709 */
1710 cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
1711
1712 return ERROR_OK;
1713 }
1714
1715 COMMAND_HANDLER(cmsis_dap_handle_serial_command)
1716 {
1717 if (CMD_ARGC == 1) {
1718 size_t len = mbstowcs(NULL, CMD_ARGV[0], 0);
1719 cmsis_dap_serial = calloc(len + 1, sizeof(wchar_t));
1720 if (cmsis_dap_serial == NULL) {
1721 LOG_ERROR("unable to allocate memory");
1722 return ERROR_OK;
1723 }
1724 if (mbstowcs(cmsis_dap_serial, CMD_ARGV[0], len + 1) == (size_t)-1) {
1725 free(cmsis_dap_serial);
1726 cmsis_dap_serial = NULL;
1727 LOG_ERROR("unable to convert serial");
1728 }
1729 } else {
1730 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1731 }
1732
1733 return ERROR_OK;
1734 }
1735
1736 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
1737 {
1738 .name = "info",
1739 .handler = &cmsis_dap_handle_info_command,
1740 .mode = COMMAND_EXEC,
1741 .usage = "",
1742 .help = "show cmsis-dap info",
1743 },
1744 {
1745 .name = "cmd",
1746 .handler = &cmsis_dap_handle_cmd_command,
1747 .mode = COMMAND_EXEC,
1748 .usage = "",
1749 .help = "issue cmsis-dap command",
1750 },
1751 COMMAND_REGISTRATION_DONE
1752 };
1753
1754 static const struct command_registration cmsis_dap_command_handlers[] = {
1755 {
1756 .name = "cmsis-dap",
1757 .mode = COMMAND_ANY,
1758 .help = "perform CMSIS-DAP management",
1759 .usage = "<cmd>",
1760 .chain = cmsis_dap_subcommand_handlers,
1761 },
1762 {
1763 .name = "cmsis_dap_vid_pid",
1764 .handler = &cmsis_dap_handle_vid_pid_command,
1765 .mode = COMMAND_CONFIG,
1766 .help = "the vendor ID and product ID of the CMSIS-DAP device",
1767 .usage = "(vid pid)* ",
1768 },
1769 {
1770 .name = "cmsis_dap_serial",
1771 .handler = &cmsis_dap_handle_serial_command,
1772 .mode = COMMAND_CONFIG,
1773 .help = "set the serial number of the adapter",
1774 .usage = "serial_string",
1775 },
1776 COMMAND_REGISTRATION_DONE
1777 };
1778
1779 static const struct swd_driver cmsis_dap_swd_driver = {
1780 .init = cmsis_dap_swd_init,
1781 .switch_seq = cmsis_dap_swd_switch_seq,
1782 .read_reg = cmsis_dap_swd_read_reg,
1783 .write_reg = cmsis_dap_swd_write_reg,
1784 .run = cmsis_dap_swd_run_queue,
1785 };
1786
1787 static const char * const cmsis_dap_transport[] = { "swd", "jtag", NULL };
1788
1789 static struct jtag_interface cmsis_dap_interface = {
1790 .supported = DEBUG_CAP_TMS_SEQ,
1791 .execute_queue = cmsis_dap_execute_queue,
1792 };
1793
1794 struct adapter_driver cmsis_dap_adapter_driver = {
1795 .name = "cmsis-dap",
1796 .transports = cmsis_dap_transport,
1797 .commands = cmsis_dap_command_handlers,
1798
1799 .init = cmsis_dap_init,
1800 .quit = cmsis_dap_quit,
1801 .reset = cmsis_dap_reset,
1802 .speed = cmsis_dap_speed,
1803 .khz = cmsis_dap_khz,
1804 .speed_div = cmsis_dap_speed_div,
1805
1806 .jtag_ops = &cmsis_dap_interface,
1807 .swd_ops = &cmsis_dap_swd_driver,
1808 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)