CMSIS-DAP: print a debug message when the USB product string cannot be read
[openocd.git] / src / jtag / drivers / cmsis_dap_usb.c
1 /***************************************************************************
2 * Copyright (C) 2013 by mike brown *
3 * mike@theshedworks.org.uk *
4 * *
5 * Copyright (C) 2013 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <transport/transport.h>
29 #include <jtag/swd.h>
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
32 #include <jtag/tcl.h>
33
34 #include <hidapi.h>
35
36 #ifdef _DEBUG_JTAG_IO_
37 #define DEBUG_IO(expr...) LOG_DEBUG(expr)
38 #else
39 #define DEBUG_IO(expr...) do {} while (0)
40 #endif
41
42 /*
43 * See CMSIS-DAP documentation:
44 * Version 0.01 - Beta.
45 */
46
47 /* USB Config */
48
49 /* Known vid/pid pairs:
50 * VID 0xc251: Keil Software
51 * PID 0xf001: LPC-Link-II CMSIS_DAP
52 * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
53 * PID 0x2722: Keil ULINK2 CMSIS-DAP
54 *
55 * VID 0x0d28: mbed Software
56 * PID 0x0204: MBED CMSIS-DAP
57 */
58
59 #define MAX_USB_IDS 8
60 /* vid = pid = 0 marks the end of the list */
61 static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
62 static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
63
64 #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
65 #define USB_TIMEOUT 1000
66
67 /* CMSIS-DAP General Commands */
68 #define CMD_DAP_INFO 0x00
69 #define CMD_DAP_LED 0x01
70 #define CMD_DAP_CONNECT 0x02
71 #define CMD_DAP_DISCONNECT 0x03
72 #define CMD_DAP_WRITE_ABORT 0x08
73 #define CMD_DAP_DELAY 0x09
74 #define CMD_DAP_RESET_TARGET 0x0A
75
76 /* CMD_INFO */
77 #define INFO_ID_VID 0x00 /* string */
78 #define INFO_ID_PID 0x02 /* string */
79 #define INFO_ID_SERNUM 0x03 /* string */
80 #define INFO_ID_FW_VER 0x04 /* string */
81 #define INFO_ID_TD_VEND 0x05 /* string */
82 #define INFO_ID_TD_NAME 0x06 /* string */
83 #define INFO_ID_CAPS 0xf0 /* byte */
84 #define INFO_ID_PKT_CNT 0xfe /* byte */
85 #define INFO_ID_PKT_SZ 0xff /* short */
86
87 #define INFO_CAPS_SWD 0x01
88 #define INFO_CAPS_JTAG 0x02
89
90 /* CMD_LED */
91 #define LED_ID_CONNECT 0x00
92 #define LED_ID_RUN 0x01
93
94 #define LED_OFF 0x00
95 #define LED_ON 0x01
96
97 /* CMD_CONNECT */
98 #define CONNECT_DEFAULT 0x00
99 #define CONNECT_SWD 0x01
100 #define CONNECT_JTAG 0x02
101
102 /* CMSIS-DAP Common SWD/JTAG Commands */
103 #define CMD_DAP_DELAY 0x09
104 #define CMD_DAP_SWJ_PINS 0x10
105 #define CMD_DAP_SWJ_CLOCK 0x11
106 #define CMD_DAP_SWJ_SEQ 0x12
107
108 /*
109 * PINS
110 * Bit 0: SWCLK/TCK
111 * Bit 1: SWDIO/TMS
112 * Bit 2: TDI
113 * Bit 3: TDO
114 * Bit 5: nTRST
115 * Bit 7: nRESET
116 */
117
118 /* CMSIS-DAP SWD Commands */
119 #define CMD_DAP_SWD_CONFIGURE 0x13
120
121 /* CMSIS-DAP JTAG Commands */
122 #define CMD_DAP_JTAG_SEQ 0x14
123 #define CMD_DAP_JTAG_CONFIGURE 0x15
124 #define CMD_DAP_JTAG_IDCODE 0x16
125
126 /* CMSIS-DAP Transfer Commands */
127 #define CMD_DAP_TFER_CONFIGURE 0x04
128 #define CMD_DAP_TFER 0x05
129 #define CMD_DAP_TFER_BLOCK 0x06
130 #define CMD_DAP_TFER_ABORT 0x07
131
132 /* DAP Status Code */
133 #define DAP_OK 0
134 #define DAP_ERROR 0xFF
135
136 /* CMSIS-DAP Vendor Commands
137 * None as yet... */
138
139 static char *info_caps_str[] = {
140 "SWD Supported",
141 "JTAG Supported"
142 };
143
144 /* max clock speed (kHz) */
145 #define DAP_MAX_CLOCK 5000
146
147 struct cmsis_dap {
148 hid_device *dev_handle;
149 uint16_t packet_size;
150 uint16_t packet_count;
151 uint8_t *packet_buffer;
152 uint8_t caps;
153 uint8_t mode;
154 };
155
156 static struct cmsis_dap *cmsis_dap_handle;
157
158 static int cmsis_dap_usb_open(void)
159 {
160 hid_device *dev = NULL;
161 int i;
162 struct hid_device_info *devs, *cur_dev;
163 unsigned short target_vid, target_pid;
164
165 target_vid = 0;
166 target_pid = 0;
167
168 /*
169 The CMSIS-DAP specification stipulates:
170 "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
171 debuggers to idenify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
172 */
173 devs = hid_enumerate(0x0, 0x0);
174 cur_dev = devs;
175 while (NULL != cur_dev) {
176 if (0 == cmsis_dap_vid[0]) {
177 if (NULL == cur_dev->product_string) {
178 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
179 cur_dev->vendor_id, cur_dev->product_id);
180 } else {
181 if (wcsstr(cur_dev->product_string, L"CMSIS-DAP"))
182 /*
183 if the user hasn't specified VID:PID *and*
184 product string contains "CMSIS-DAP", pick it
185 */
186 break;
187 }
188 } else {
189 /*
190 otherwise, exhaustively compare against all VID:PID in list
191 */
192 for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
193 if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
194 break;
195 }
196 }
197
198 cur_dev = cur_dev->next;
199 }
200
201 if (NULL != cur_dev) {
202 target_vid = cur_dev->vendor_id;
203 target_pid = cur_dev->product_id;
204 }
205
206 hid_free_enumeration(devs);
207
208 if (hid_init() != 0) {
209 LOG_ERROR("unable to open HIDAPI");
210 return ERROR_FAIL;
211 }
212
213 dev = hid_open(target_vid, target_pid, NULL);
214
215 if (dev == NULL) {
216 LOG_ERROR("unable to open CMSIS-DAP device");
217 return ERROR_FAIL;
218 }
219
220 struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
221 if (dap == NULL) {
222 LOG_ERROR("unable to allocate memory");
223 return ERROR_FAIL;
224 }
225
226 dap->dev_handle = dev;
227 dap->caps = 0;
228 dap->mode = 0;
229
230 cmsis_dap_handle = dap;
231
232 /* allocate default packet buffer, may be changed later.
233 * currently with HIDAPI we have no way of getting the output report length
234 * without this info we cannot communicate with the adapter.
235 * For the moment we ahve to hard code the packet size */
236
237 int packet_size = PACKET_SIZE;
238
239 /* atmel cmsis-dap uses 512 byte reports */
240 if (target_vid == 0x03eb)
241 packet_size = 512 + 1;
242
243 cmsis_dap_handle->packet_buffer = malloc(packet_size);
244 cmsis_dap_handle->packet_size = packet_size;
245
246 if (cmsis_dap_handle->packet_buffer == NULL) {
247 LOG_ERROR("unable to allocate memory");
248 return ERROR_FAIL;
249 }
250
251 return ERROR_OK;
252 }
253
254 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
255 {
256 hid_close(dap->dev_handle);
257 hid_exit();
258
259 if (cmsis_dap_handle->packet_buffer)
260 free(cmsis_dap_handle->packet_buffer);
261
262 if (cmsis_dap_handle) {
263 free(cmsis_dap_handle);
264 cmsis_dap_handle = NULL;
265 }
266
267 return;
268 }
269
270 /* Send a message and receive the reply */
271 static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
272 {
273 /* Pad the rest of the TX buffer with 0's */
274 memset(dap->packet_buffer + txlen, 0, dap->packet_size - 1 - txlen);
275
276 /* write data to device */
277 int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
278 if (retval == -1) {
279 LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
280 return ERROR_FAIL;
281 }
282
283 /* get reply */
284 retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
285 if (retval == -1 || retval == 0) {
286 LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
287 return ERROR_FAIL;
288 }
289
290 return ERROR_OK;
291 }
292
293 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
294 {
295 int retval;
296 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
297
298 buffer[0] = 0; /* report number */
299 buffer[1] = CMD_DAP_SWJ_PINS;
300 buffer[2] = pins;
301 buffer[3] = mask;
302 buffer[4] = delay & 0xff;
303 buffer[5] = (delay >> 8) & 0xff;
304 buffer[6] = (delay >> 16) & 0xff;
305 buffer[7] = (delay >> 24) & 0xff;
306 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
307
308 if (retval != ERROR_OK) {
309 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
310 return ERROR_JTAG_DEVICE_ERROR;
311 }
312
313 if (input)
314 *input = buffer[1];
315
316 return ERROR_OK;
317 }
318
319 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
320 {
321 int retval;
322 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
323
324 /* set clock in Hz */
325 swj_clock *= 1000;
326 buffer[0] = 0; /* report number */
327 buffer[1] = CMD_DAP_SWJ_CLOCK;
328 buffer[2] = swj_clock & 0xff;
329 buffer[3] = (swj_clock >> 8) & 0xff;
330 buffer[4] = (swj_clock >> 16) & 0xff;
331 buffer[5] = (swj_clock >> 24) & 0xff;
332 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
333
334 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
335 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
336 return ERROR_JTAG_DEVICE_ERROR;
337 }
338
339 return ERROR_OK;
340 }
341
342 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
343 {
344 int retval;
345 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
346
347 buffer[0] = 0; /* report number */
348 buffer[1] = CMD_DAP_INFO;
349 buffer[2] = info;
350 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
351
352 if (retval != ERROR_OK) {
353 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
354 return ERROR_JTAG_DEVICE_ERROR;
355 }
356
357 *data = &(buffer[1]);
358
359 return ERROR_OK;
360 }
361
362 static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
363 {
364 int retval;
365 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
366
367 buffer[0] = 0; /* report number */
368 buffer[1] = CMD_DAP_LED;
369 buffer[2] = 0x00;
370 buffer[3] = leds;
371 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
372
373 if (retval != ERROR_OK || buffer[1] != 0x00) {
374 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
375 return ERROR_JTAG_DEVICE_ERROR;
376 }
377
378 return ERROR_OK;
379 }
380
381 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
382 {
383 int retval;
384 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
385
386 buffer[0] = 0; /* report number */
387 buffer[1] = CMD_DAP_CONNECT;
388 buffer[2] = mode;
389 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
390
391 if (retval != ERROR_OK) {
392 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
393 return ERROR_JTAG_DEVICE_ERROR;
394 }
395
396 if (buffer[1] != mode) {
397 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
398 return ERROR_JTAG_DEVICE_ERROR;
399 }
400
401 return ERROR_OK;
402 }
403
404 static int cmsis_dap_cmd_DAP_Disconnect(void)
405 {
406 int retval;
407 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
408
409 buffer[0] = 0; /* report number */
410 buffer[1] = CMD_DAP_DISCONNECT;
411 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
412
413 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
414 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
415 return ERROR_JTAG_DEVICE_ERROR;
416 }
417
418 return ERROR_OK;
419 }
420
421 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t delay, uint16_t retry)
422 {
423 int retval;
424 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
425
426 buffer[0] = 0; /* report number */
427 buffer[1] = CMD_DAP_TFER_CONFIGURE;
428 buffer[2] = idle;
429 buffer[3] = delay & 0xff;
430 buffer[4] = (delay >> 8) & 0xff;
431 buffer[5] = retry & 0xff;
432 buffer[6] = (retry >> 8) & 0xff;
433 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
434
435 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
436 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
437 return ERROR_JTAG_DEVICE_ERROR;
438 }
439
440 return ERROR_OK;
441 }
442
443 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
444 {
445 int retval;
446 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
447
448 buffer[0] = 0; /* report number */
449 buffer[1] = CMD_DAP_SWD_CONFIGURE;
450 buffer[2] = cfg;
451 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
452
453 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
454 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
455 return ERROR_JTAG_DEVICE_ERROR;
456 }
457
458 return ERROR_OK;
459 }
460
461 #if 0
462 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
463 {
464 int retval;
465 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
466
467 buffer[0] = 0; /* report number */
468 buffer[1] = CMD_DAP_DELAY;
469 buffer[2] = delay_us & 0xff;
470 buffer[3] = (delay_us >> 8) & 0xff;
471 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
472
473 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
474 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
475 return ERROR_JTAG_DEVICE_ERROR;
476 }
477
478 return ERROR_OK;
479 }
480 #endif
481
482 static int cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value)
483 {
484 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
485 int retval;
486 uint32_t val;
487
488 DEBUG_IO("CMSIS-DAP: Read Reg 0x%02" PRIx8, cmd);
489
490 buffer[0] = 0; /* report number */
491 buffer[1] = CMD_DAP_TFER;
492 buffer[2] = 0x00;
493 buffer[3] = 0x01;
494 buffer[4] = cmd;
495 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
496
497 /* TODO - need better response checking */
498 if (retval != ERROR_OK || buffer[1] != 0x01) {
499 LOG_ERROR("CMSIS-DAP: Read Error (0x%02" PRIx8 ")", buffer[2]);
500 return buffer[2];
501 }
502
503 val = le_to_h_u32(&buffer[3]);
504 DEBUG_IO("0x%08" PRIx32, val);
505
506 if (value)
507 *value = val;
508
509 return retval;
510 }
511
512 static int cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value)
513 {
514 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
515
516 DEBUG_IO("CMSIS-DAP: Write Reg 0x%02" PRIx8 " 0x%08" PRIx32, cmd, value);
517
518 buffer[0] = 0; /* report number */
519 buffer[1] = CMD_DAP_TFER;
520 buffer[2] = 0x00;
521 buffer[3] = 0x01;
522 buffer[4] = cmd;
523 buffer[5] = (value) & 0xff;
524 buffer[6] = (value >> 8) & 0xff;
525 buffer[7] = (value >> 16) & 0xff;
526 buffer[8] = (value >> 24) & 0xff;
527 int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 9);
528
529 if (buffer[1] != 0x01) {
530 LOG_ERROR("CMSIS-DAP: Write Error (0x%02" PRIx8 ")", buffer[2]);
531 retval = buffer[2];
532 }
533
534 return retval;
535 }
536
537 static int cmsis_dap_swd_read_block(uint8_t cmd, uint32_t blocksize, uint8_t *dest_buf)
538 {
539 uint8_t *buffer;
540 int tfer_sz;
541 int retval = ERROR_OK;
542 uint16_t read_count;
543
544 DEBUG_IO("CMSIS-DAP: Read Block 0x%02" PRIx8 " %" PRIu32, cmd, blocksize);
545
546 while (blocksize) {
547
548 buffer = cmsis_dap_handle->packet_buffer;
549 tfer_sz = blocksize;
550 if (tfer_sz > 15)
551 tfer_sz = 8;
552
553 buffer[0] = 0; /* report number */
554 buffer[1] = CMD_DAP_TFER_BLOCK;
555 buffer[2] = 0x00;
556 buffer[3] = tfer_sz;
557 buffer[4] = 0x00;
558 buffer[5] = cmd;
559 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
560
561 read_count = le_to_h_u16(&buffer[1]);
562 if (read_count != tfer_sz) {
563 LOG_ERROR("CMSIS-DAP: Block Read Error (0x%02" PRIx8 ")", buffer[3]);
564 retval = buffer[3];
565 }
566
567 read_count *= 4;
568 memcpy(dest_buf, &buffer[4], read_count);
569
570 dest_buf += read_count;
571 blocksize -= tfer_sz;
572 }
573
574 return retval;
575 }
576
577 static int cmsis_dap_get_version_info(void)
578 {
579 uint8_t *data;
580
581 /* INFO_ID_FW_VER - string */
582 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
583 if (retval != ERROR_OK)
584 return retval;
585
586 if (data[0]) /* strlen */
587 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
588
589 return ERROR_OK;
590 }
591
592 static int cmsis_dap_get_caps_info(void)
593 {
594 uint8_t *data;
595
596 /* INFO_ID_CAPS - byte */
597 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
598 if (retval != ERROR_OK)
599 return retval;
600
601 if (data[0] == 1) {
602 uint8_t caps = data[1];
603
604 cmsis_dap_handle->caps = caps;
605
606 if (caps & INFO_CAPS_SWD)
607 LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
608 if (caps & INFO_CAPS_JTAG)
609 LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
610 }
611
612 return ERROR_OK;
613 }
614
615 static int cmsis_dap_get_status(void)
616 {
617 uint8_t d;
618
619 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
620
621 if (retval == ERROR_OK) {
622 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
623 (d & (0x01 << 0)) ? 1 : 0, /* Bit 0: SWCLK/TCK */
624 (d & (0x01 << 1)) ? 1 : 0, /* Bit 1: SWDIO/TMS */
625 (d & (0x01 << 2)) ? 1 : 0, /* Bit 2: TDI */
626 (d & (0x01 << 3)) ? 1 : 0, /* Bit 3: TDO */
627 (d & (0x01 << 5)) ? 1 : 0, /* Bit 5: nTRST */
628 (d & (0x01 << 7)) ? 1 : 0); /* Bit 7: nRESET */
629 }
630
631 return retval;
632 }
633
634 static int cmsis_dap_reset_link(void)
635 {
636 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
637
638 LOG_DEBUG("CMSIS-DAP: cmsis_dap_reset_link");
639 LOG_INFO("DAP_SWJ Sequence (reset: 50+ '1' followed by 0)");
640
641 /* reset line with SWDIO high for >50 cycles */
642 buffer[0] = 0; /* report number */
643 buffer[1] = CMD_DAP_SWJ_SEQ;
644 buffer[2] = 7 * 8;
645 buffer[3] = 0xff;
646 buffer[4] = 0xff;
647 buffer[5] = 0xff;
648 buffer[6] = 0xff;
649 buffer[7] = 0xff;
650 buffer[8] = 0xff;
651 buffer[9] = 0xff;
652 int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
653
654 if (retval != ERROR_OK || buffer[1] != DAP_OK)
655 return ERROR_FAIL;
656
657 /* 16bit JTAG-SWD sequence */
658 buffer[0] = 0; /* report number */
659 buffer[1] = CMD_DAP_SWJ_SEQ;
660 buffer[2] = 2 * 8;
661 buffer[3] = 0x9e;
662 buffer[4] = 0xe7;
663 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
664
665 if (retval != ERROR_OK || buffer[1] != DAP_OK)
666 return ERROR_FAIL;
667
668 /* another reset just incase */
669 buffer[0] = 0; /* report number */
670 buffer[1] = CMD_DAP_SWJ_SEQ;
671 buffer[2] = 7 * 8;
672 buffer[3] = 0xff;
673 buffer[4] = 0xff;
674 buffer[5] = 0xff;
675 buffer[6] = 0xff;
676 buffer[7] = 0xff;
677 buffer[8] = 0xff;
678 buffer[9] = 0xff;
679 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
680
681 if (retval != ERROR_OK || buffer[1] != DAP_OK)
682 return ERROR_FAIL;
683
684 /* 16 cycle idle period */
685 buffer[0] = 0; /* report number */
686 buffer[1] = CMD_DAP_SWJ_SEQ;
687 buffer[2] = 2 * 8;
688 buffer[3] = 0x00;
689 buffer[4] = 0x00;
690 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
691
692 if (retval != ERROR_OK || buffer[1] != DAP_OK)
693 return ERROR_FAIL;
694
695 DEBUG_IO("DAP Read IDCODE");
696
697 /* read the id code is always the next sequence */
698 buffer[0] = 0; /* report number */
699 buffer[1] = CMD_DAP_TFER;
700 buffer[2] = 0x00;
701 buffer[3] = 0x01;
702 buffer[4] = 0x02;
703 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
704
705 if (retval != ERROR_OK)
706 return retval;
707
708 if (buffer[1] == 0) {
709 LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
710
711 LOG_DEBUG("DAP Reset Target");
712 buffer[0] = 0; /* report number */
713 buffer[1] = CMD_DAP_RESET_TARGET;
714 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
715 LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
716
717 LOG_DEBUG("DAP Write Abort");
718 buffer[0] = 0; /* report number */
719 buffer[1] = CMD_DAP_WRITE_ABORT;
720 buffer[2] = 0x00;
721 buffer[3] = 0x1e/*0x1f*/;
722 buffer[4] = 0x00;
723 buffer[5] = 0x00;
724 buffer[6] = 0x00;
725 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
726 LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
727
728 return 0x80 + buffer[1];
729 }
730
731 LOG_DEBUG("DAP Write Abort");
732 buffer[0] = 0; /* report number */
733 buffer[1] = CMD_DAP_WRITE_ABORT;
734 buffer[2] = 0x00;
735 buffer[3] = 0x1e;
736 buffer[4] = 0x00;
737 buffer[5] = 0x00;
738 buffer[6] = 0x00;
739 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
740 LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
741
742 return retval;
743 }
744
745 static int cmsis_dap_init(void)
746 {
747 int retval;
748 uint8_t *data;
749
750 if (cmsis_dap_handle == NULL) {
751
752 /* JTAG init */
753 retval = cmsis_dap_usb_open();
754 if (retval != ERROR_OK)
755 return retval;
756
757 retval = cmsis_dap_get_caps_info();
758 if (retval != ERROR_OK)
759 return retval;
760
761 /* Connect in JTAG mode */
762 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
763 LOG_ERROR("CMSIS-DAP: JTAG not supported");
764 return ERROR_JTAG_DEVICE_ERROR;
765 }
766
767 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
768 if (retval != ERROR_OK)
769 return retval;
770
771 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
772 }
773
774 retval = cmsis_dap_get_version_info();
775 if (retval != ERROR_OK)
776 return retval;
777
778 /* INFO_ID_PKT_SZ - short */
779 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
780 if (retval != ERROR_OK)
781 return retval;
782
783 if (data[0] == 2) { /* short */
784 uint16_t pkt_sz = data[1] + (data[2] << 8);
785
786 if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
787 /* reallocate buffer */
788 cmsis_dap_handle->packet_size = pkt_sz + 1;
789 cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
790 cmsis_dap_handle->packet_size);
791 if (cmsis_dap_handle->packet_buffer == NULL) {
792 LOG_ERROR("unable to reallocate memory");
793 return ERROR_FAIL;
794 }
795 }
796
797 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
798 }
799
800 /* INFO_ID_PKT_CNT - byte */
801 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
802 if (retval != ERROR_OK)
803 return retval;
804
805 if (data[0] == 1) { /* byte */
806 uint16_t pkt_cnt = data[1];
807 cmsis_dap_handle->packet_count = pkt_cnt;
808 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
809 }
810
811 retval = cmsis_dap_get_status();
812 if (retval != ERROR_OK)
813 return ERROR_FAIL;
814
815 /* Now try to connect to the target
816 * TODO: This is all SWD only @ present */
817 retval = cmsis_dap_cmd_DAP_SWJ_Clock(100); /* 100kHz */
818 if (retval != ERROR_OK)
819 return ERROR_FAIL;
820
821 retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
822 if (retval != ERROR_OK)
823 return ERROR_FAIL;
824 retval = cmsis_dap_cmd_DAP_SWD_Configure(0x00);
825 if (retval != ERROR_OK)
826 return ERROR_FAIL;
827
828 retval = cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
829 if (retval != ERROR_OK)
830 return ERROR_FAIL;
831
832 /* support connecting with srst asserted */
833 enum reset_types jtag_reset_config = jtag_get_reset_config();
834
835 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
836 if (jtag_reset_config & RESET_SRST_NO_GATING) {
837 retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL);
838 if (retval != ERROR_OK)
839 return ERROR_FAIL;
840 LOG_INFO("Connecting under reset");
841 }
842 }
843
844 retval = cmsis_dap_reset_link();
845 if (retval != ERROR_OK)
846 return ERROR_FAIL;
847
848 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
849
850 LOG_INFO("CMSIS-DAP: Interface ready");
851
852 return ERROR_OK;
853 }
854
855 static int cmsis_dap_swd_init(uint8_t trn)
856 {
857 int retval;
858
859 DEBUG_IO("CMSIS-DAP: cmsis_dap_swd_init");
860
861 if (cmsis_dap_handle == NULL) {
862
863 /* SWD init */
864 retval = cmsis_dap_usb_open();
865 if (retval != ERROR_OK)
866 return retval;
867
868 retval = cmsis_dap_get_caps_info();
869 if (retval != ERROR_OK)
870 return retval;
871 }
872
873 if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
874 LOG_ERROR("CMSIS-DAP: SWD not supported");
875 return ERROR_JTAG_DEVICE_ERROR;
876 }
877
878 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
879 if (retval != ERROR_OK)
880 return retval;
881
882 /* Add more setup here.??... */
883
884 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
885 return ERROR_OK;
886 }
887
888 static int cmsis_dap_quit(void)
889 {
890 cmsis_dap_cmd_DAP_Disconnect();
891 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
892
893 cmsis_dap_usb_close(cmsis_dap_handle);
894
895 return ERROR_OK;
896 }
897
898 static void cmsis_dap_execute_reset(struct jtag_command *cmd)
899 {
900 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(cmd->cmd.reset->srst ? 0 : (1 << 7), \
901 (1 << 7), 0, NULL);
902 if (retval != ERROR_OK)
903 LOG_ERROR("CMSIS-DAP: Interface reset failed");
904 }
905
906 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
907 {
908 #if 0
909 int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
910 if (retval != ERROR_OK)
911 #endif
912 jtag_sleep(cmd->cmd.sleep->us);
913 }
914
915 static void cmsis_dap_execute_command(struct jtag_command *cmd)
916 {
917 switch (cmd->type) {
918 case JTAG_RESET:
919 cmsis_dap_execute_reset(cmd);
920 break;
921 case JTAG_SLEEP:
922 cmsis_dap_execute_sleep(cmd);
923 break;
924 default:
925 LOG_ERROR("BUG: unknown JTAG command type encountered");
926 exit(-1);
927 }
928 }
929
930 static int cmsis_dap_execute_queue(void)
931 {
932 struct jtag_command *cmd = jtag_command_queue;
933
934 while (cmd != NULL) {
935 cmsis_dap_execute_command(cmd);
936 cmd = cmd->next;
937 }
938
939 return ERROR_OK;
940 }
941
942 static int cmsis_dap_speed(int speed)
943 {
944 if (speed > DAP_MAX_CLOCK) {
945 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, DAP_MAX_CLOCK);
946 speed = DAP_MAX_CLOCK;
947 }
948
949 if (speed == 0) {
950 LOG_INFO("RTCK not supported");
951 return ERROR_JTAG_NOT_IMPLEMENTED;
952 }
953
954 return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
955 }
956
957 static int cmsis_dap_speed_div(int speed, int *khz)
958 {
959 *khz = speed;
960 return ERROR_OK;
961 }
962
963 static int cmsis_dap_khz(int khz, int *jtag_speed)
964 {
965 *jtag_speed = khz;
966 return ERROR_OK;
967 }
968
969 COMMAND_HANDLER(cmsis_dap_handle_info_command)
970 {
971 if (cmsis_dap_get_version_info() == ERROR_OK)
972 cmsis_dap_get_status();
973
974 return ERROR_OK;
975 }
976
977 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
978 {
979 if (CMD_ARGC > MAX_USB_IDS * 2) {
980 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
981 "(maximum is %d pairs)", MAX_USB_IDS);
982 CMD_ARGC = MAX_USB_IDS * 2;
983 }
984 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
985 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
986 if (CMD_ARGC < 2)
987 return ERROR_COMMAND_SYNTAX_ERROR;
988 /* remove the incomplete trailing id */
989 CMD_ARGC -= 1;
990 }
991
992 unsigned i;
993 for (i = 0; i < CMD_ARGC; i += 2) {
994 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
995 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
996 }
997
998 /*
999 * Explicitly terminate, in case there are multiples instances of
1000 * cmsis_dap_vid_pid.
1001 */
1002 cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
1003
1004 return ERROR_OK;
1005 }
1006
1007 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
1008 {
1009 .name = "info",
1010 .handler = &cmsis_dap_handle_info_command,
1011 .mode = COMMAND_EXEC,
1012 .usage = "",
1013 .help = "show cmsis-dap info",
1014 },
1015 COMMAND_REGISTRATION_DONE
1016 };
1017
1018 COMMAND_HANDLER(cmsis_dap_reset_command)
1019 {
1020 LOG_DEBUG("cmsis_dap_reset_command");
1021 return ERROR_OK;
1022 }
1023
1024 COMMAND_HANDLER(cmsis_dap_jtag_command)
1025 {
1026 LOG_DEBUG("cmsis_dap_jtag_command");
1027 return ERROR_OK;
1028 }
1029
1030 static const struct command_registration cmsis_dap_jtag_subcommand_handlers[] = {
1031 {
1032 .name = "init",
1033 .mode = COMMAND_ANY,
1034 .handler = cmsis_dap_jtag_command,
1035 .usage = ""
1036 },
1037 {
1038 .name = "arp_init",
1039 .mode = COMMAND_ANY,
1040 .handler = cmsis_dap_jtag_command,
1041 .usage = ""
1042 },
1043 {
1044 .name = "arp_init-reset",
1045 .mode = COMMAND_ANY,
1046 .handler = cmsis_dap_reset_command,
1047 .usage = ""
1048 },
1049 {
1050 .name = "tapisenabled",
1051 .mode = COMMAND_EXEC,
1052 .jim_handler = jim_jtag_tap_enabler,
1053 },
1054 {
1055 .name = "tapenable",
1056 .mode = COMMAND_EXEC,
1057 .jim_handler = jim_jtag_tap_enabler,
1058 },
1059 {
1060 .name = "tapdisable",
1061 .mode = COMMAND_EXEC,
1062 .handler = cmsis_dap_jtag_command,
1063 .usage = "",
1064 },
1065 {
1066 .name = "configure",
1067 .mode = COMMAND_EXEC,
1068 .handler = cmsis_dap_jtag_command,
1069 .usage = "",
1070 },
1071 {
1072 .name = "cget",
1073 .mode = COMMAND_EXEC,
1074 .jim_handler = jim_jtag_configure,
1075 },
1076 {
1077 .name = "names",
1078 .mode = COMMAND_ANY,
1079 .handler = cmsis_dap_jtag_command,
1080 .usage = "",
1081 },
1082 COMMAND_REGISTRATION_DONE
1083 };
1084
1085 static const struct command_registration cmsis_dap_command_handlers[] = {
1086 {
1087 .name = "cmsis-dap",
1088 .mode = COMMAND_ANY,
1089 .help = "perform CMSIS-DAP management",
1090 .usage = "<cmd>",
1091 .chain = cmsis_dap_subcommand_handlers,
1092 },
1093 {
1094 .name = "cmsis_dap_vid_pid",
1095 .handler = &cmsis_dap_handle_vid_pid_command,
1096 .mode = COMMAND_CONFIG,
1097 .help = "the vendor ID and product ID of the CMSIS-DAP device",
1098 .usage = "(vid pid)* ",
1099 },
1100 {
1101 /* this is currently a nasty hack so we get
1102 * reset working with non jtag interfaces */
1103 .name = "jtag",
1104 .mode = COMMAND_ANY,
1105 .usage = "",
1106 .chain = cmsis_dap_jtag_subcommand_handlers,
1107 },
1108 COMMAND_REGISTRATION_DONE
1109 };
1110
1111 static const struct swd_driver cmsis_dap_swd_driver = {
1112 .init = cmsis_dap_swd_init,
1113 .read_reg = cmsis_dap_swd_read_reg,
1114 .write_reg = cmsis_dap_swd_write_reg,
1115 .read_block = cmsis_dap_swd_read_block
1116 };
1117
1118 const char *cmsis_dap_transport[] = {"cmsis-dap", NULL};
1119
1120 struct jtag_interface cmsis_dap_interface = {
1121 .name = "cmsis-dap",
1122 .commands = cmsis_dap_command_handlers,
1123 .swd = &cmsis_dap_swd_driver,
1124 .transports = cmsis_dap_transport,
1125
1126 .execute_queue = cmsis_dap_execute_queue,
1127 .speed = cmsis_dap_speed,
1128 .speed_div = cmsis_dap_speed_div,
1129 .khz = cmsis_dap_khz,
1130 .init = cmsis_dap_init,
1131 .quit = cmsis_dap_quit,
1132 };

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)