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

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)