1 /***************************************************************************
2 * Copyright (C) 2013 by mike brown *
3 * mike@theshedworks.org.uk *
5 * Copyright (C) 2013 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
28 #include <transport/transport.h>
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
36 #ifdef _DEBUG_JTAG_IO_
37 #define DEBUG_IO(expr...) LOG_DEBUG(expr)
39 #define DEBUG_IO(expr...) do {} while (0)
43 * See CMSIS-DAP documentation:
44 * Version 0.01 - Beta.
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
55 * VID 0x0d28: mbed Software
56 * PID 0x0204: MBED CMSIS-DAP
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 };
64 #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
65 #define USB_TIMEOUT 1000
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
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 */
87 #define INFO_CAPS_SWD 0x01
88 #define INFO_CAPS_JTAG 0x02
91 #define LED_ID_CONNECT 0x00
92 #define LED_ID_RUN 0x01
98 #define CONNECT_DEFAULT 0x00
99 #define CONNECT_SWD 0x01
100 #define CONNECT_JTAG 0x02
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
118 /* CMSIS-DAP SWD Commands */
119 #define CMD_DAP_SWD_CONFIGURE 0x13
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
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
132 /* DAP Status Code */
134 #define DAP_ERROR 0xFF
136 /* CMSIS-DAP Vendor Commands
139 static char *info_caps_str
[] = {
144 /* max clock speed (kHz) */
145 #define DAP_MAX_CLOCK 5000
148 hid_device
*dev_handle
;
149 uint16_t packet_size
;
150 uint16_t packet_count
;
151 uint8_t *packet_buffer
;
156 static struct cmsis_dap
*cmsis_dap_handle
;
158 static int cmsis_dap_usb_open(void)
160 hid_device
*dev
= NULL
;
162 struct hid_device_info
*devs
, *cur_dev
;
163 unsigned short target_vid
, target_pid
;
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."
173 devs
= hid_enumerate(0x0, 0x0);
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
);
181 if (wcsstr(cur_dev
->product_string
, L
"CMSIS-DAP"))
183 if the user hasn't specified VID:PID *and*
184 product string contains "CMSIS-DAP", pick it
190 otherwise, exhaustively compare against all VID:PID in list
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
))
198 cur_dev
= cur_dev
->next
;
201 if (NULL
!= cur_dev
) {
202 target_vid
= cur_dev
->vendor_id
;
203 target_pid
= cur_dev
->product_id
;
206 hid_free_enumeration(devs
);
208 if (hid_init() != 0) {
209 LOG_ERROR("unable to open HIDAPI");
213 dev
= hid_open(target_vid
, target_pid
, NULL
);
216 LOG_ERROR("unable to open CMSIS-DAP device");
220 struct cmsis_dap
*dap
= malloc(sizeof(struct cmsis_dap
));
222 LOG_ERROR("unable to allocate memory");
226 dap
->dev_handle
= dev
;
230 cmsis_dap_handle
= dap
;
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 */
237 int packet_size
= PACKET_SIZE
;
239 /* atmel cmsis-dap uses 512 byte reports */
240 if (target_vid
== 0x03eb)
241 packet_size
= 512 + 1;
243 cmsis_dap_handle
->packet_buffer
= malloc(packet_size
);
244 cmsis_dap_handle
->packet_size
= packet_size
;
246 if (cmsis_dap_handle
->packet_buffer
== NULL
) {
247 LOG_ERROR("unable to allocate memory");
254 static void cmsis_dap_usb_close(struct cmsis_dap
*dap
)
256 hid_close(dap
->dev_handle
);
259 if (cmsis_dap_handle
->packet_buffer
)
260 free(cmsis_dap_handle
->packet_buffer
);
262 if (cmsis_dap_handle
) {
263 free(cmsis_dap_handle
);
264 cmsis_dap_handle
= NULL
;
270 /* Send a message and receive the reply */
271 static int cmsis_dap_usb_xfer(struct cmsis_dap
*dap
, int txlen
)
273 /* Pad the rest of the TX buffer with 0's */
274 memset(dap
->packet_buffer
+ txlen
, 0, dap
->packet_size
- 1 - txlen
);
276 /* write data to device */
277 int retval
= hid_write(dap
->dev_handle
, dap
->packet_buffer
, dap
->packet_size
);
279 LOG_ERROR("error writing data: %ls", hid_error(dap
->dev_handle
));
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
));
293 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins
, uint8_t mask
, uint32_t delay
, uint8_t *input
)
296 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
298 buffer
[0] = 0; /* report number */
299 buffer
[1] = CMD_DAP_SWJ_PINS
;
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);
308 if (retval
!= ERROR_OK
) {
309 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
310 return ERROR_JTAG_DEVICE_ERROR
;
319 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock
)
322 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
324 /* set clock in Hz */
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);
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
;
342 static int cmsis_dap_cmd_DAP_Info(uint8_t info
, uint8_t **data
)
345 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
347 buffer
[0] = 0; /* report number */
348 buffer
[1] = CMD_DAP_INFO
;
350 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 3);
352 if (retval
!= ERROR_OK
) {
353 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
354 return ERROR_JTAG_DEVICE_ERROR
;
357 *data
= &(buffer
[1]);
362 static int cmsis_dap_cmd_DAP_LED(uint8_t leds
)
365 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
367 buffer
[0] = 0; /* report number */
368 buffer
[1] = CMD_DAP_LED
;
371 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 4);
373 if (retval
!= ERROR_OK
|| buffer
[1] != 0x00) {
374 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
375 return ERROR_JTAG_DEVICE_ERROR
;
381 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode
)
384 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
386 buffer
[0] = 0; /* report number */
387 buffer
[1] = CMD_DAP_CONNECT
;
389 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 3);
391 if (retval
!= ERROR_OK
) {
392 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
393 return ERROR_JTAG_DEVICE_ERROR
;
396 if (buffer
[1] != mode
) {
397 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode
);
398 return ERROR_JTAG_DEVICE_ERROR
;
404 static int cmsis_dap_cmd_DAP_Disconnect(void)
407 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
409 buffer
[0] = 0; /* report number */
410 buffer
[1] = CMD_DAP_DISCONNECT
;
411 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 2);
413 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
414 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
415 return ERROR_JTAG_DEVICE_ERROR
;
421 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle
, uint16_t delay
, uint16_t retry
)
424 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
426 buffer
[0] = 0; /* report number */
427 buffer
[1] = CMD_DAP_TFER_CONFIGURE
;
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);
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
;
443 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg
)
446 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
448 buffer
[0] = 0; /* report number */
449 buffer
[1] = CMD_DAP_SWD_CONFIGURE
;
451 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 3);
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
;
462 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us
)
465 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
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);
473 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
) {
474 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
475 return ERROR_JTAG_DEVICE_ERROR
;
482 static int cmsis_dap_swd_read_reg(uint8_t cmd
, uint32_t *value
)
484 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
488 DEBUG_IO("CMSIS-DAP: Read Reg 0x%02" PRIx8
, cmd
);
490 buffer
[0] = 0; /* report number */
491 buffer
[1] = CMD_DAP_TFER
;
495 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 5);
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]);
503 val
= le_to_h_u32(&buffer
[3]);
504 DEBUG_IO("0x%08" PRIx32
, val
);
512 static int cmsis_dap_swd_write_reg(uint8_t cmd
, uint32_t value
)
514 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
516 DEBUG_IO("CMSIS-DAP: Write Reg 0x%02" PRIx8
" 0x%08" PRIx32
, cmd
, value
);
518 buffer
[0] = 0; /* report number */
519 buffer
[1] = CMD_DAP_TFER
;
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);
529 if (buffer
[1] != 0x01) {
530 LOG_ERROR("CMSIS-DAP: Write Error (0x%02" PRIx8
")", buffer
[2]);
537 static int cmsis_dap_get_version_info(void)
541 /* INFO_ID_FW_VER - string */
542 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER
, &data
);
543 if (retval
!= ERROR_OK
)
546 if (data
[0]) /* strlen */
547 LOG_INFO("CMSIS-DAP: FW Version = %s", &data
[1]);
552 static int cmsis_dap_get_caps_info(void)
556 /* INFO_ID_CAPS - byte */
557 int retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS
, &data
);
558 if (retval
!= ERROR_OK
)
562 uint8_t caps
= data
[1];
564 cmsis_dap_handle
->caps
= caps
;
566 if (caps
& INFO_CAPS_SWD
)
567 LOG_INFO("CMSIS-DAP: %s", info_caps_str
[0]);
568 if (caps
& INFO_CAPS_JTAG
)
569 LOG_INFO("CMSIS-DAP: %s", info_caps_str
[1]);
575 static int cmsis_dap_get_status(void)
579 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d
);
581 if (retval
== ERROR_OK
) {
582 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
583 (d
& (0x01 << 0)) ? 1 : 0, /* Bit 0: SWCLK/TCK */
584 (d
& (0x01 << 1)) ? 1 : 0, /* Bit 1: SWDIO/TMS */
585 (d
& (0x01 << 2)) ? 1 : 0, /* Bit 2: TDI */
586 (d
& (0x01 << 3)) ? 1 : 0, /* Bit 3: TDO */
587 (d
& (0x01 << 5)) ? 1 : 0, /* Bit 5: nTRST */
588 (d
& (0x01 << 7)) ? 1 : 0); /* Bit 7: nRESET */
594 static int cmsis_dap_reset_link(void)
596 uint8_t *buffer
= cmsis_dap_handle
->packet_buffer
;
598 LOG_DEBUG("CMSIS-DAP: cmsis_dap_reset_link");
599 LOG_INFO("DAP_SWJ Sequence (reset: 50+ '1' followed by 0)");
601 /* reset line with SWDIO high for >50 cycles */
602 buffer
[0] = 0; /* report number */
603 buffer
[1] = CMD_DAP_SWJ_SEQ
;
612 int retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 10);
614 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
)
617 /* 16bit JTAG-SWD sequence */
618 buffer
[0] = 0; /* report number */
619 buffer
[1] = CMD_DAP_SWJ_SEQ
;
623 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 5);
625 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
)
628 /* another reset just incase */
629 buffer
[0] = 0; /* report number */
630 buffer
[1] = CMD_DAP_SWJ_SEQ
;
639 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 10);
641 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
)
644 /* 16 cycle idle period */
645 buffer
[0] = 0; /* report number */
646 buffer
[1] = CMD_DAP_SWJ_SEQ
;
650 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 5);
652 if (retval
!= ERROR_OK
|| buffer
[1] != DAP_OK
)
655 DEBUG_IO("DAP Read IDCODE");
657 /* read the id code is always the next sequence */
658 buffer
[0] = 0; /* report number */
659 buffer
[1] = CMD_DAP_TFER
;
663 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 5);
665 if (retval
!= ERROR_OK
)
668 if (buffer
[1] == 0) {
669 LOG_DEBUG("Result 0x%02" PRIx8
" 0x%02" PRIx8
, buffer
[1], buffer
[2]);
671 LOG_DEBUG("DAP Reset Target");
672 buffer
[0] = 0; /* report number */
673 buffer
[1] = CMD_DAP_RESET_TARGET
;
674 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 2);
675 LOG_DEBUG("Result 0x%02" PRIx8
" 0x%02" PRIx8
, buffer
[1], buffer
[2]);
677 LOG_DEBUG("DAP Write Abort");
678 buffer
[0] = 0; /* report number */
679 buffer
[1] = CMD_DAP_WRITE_ABORT
;
681 buffer
[3] = 0x1e/*0x1f*/;
685 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 7);
686 LOG_DEBUG("Result 0x%02" PRIx8
, buffer
[1]);
688 return 0x80 + buffer
[1];
691 LOG_DEBUG("DAP Write Abort");
692 buffer
[0] = 0; /* report number */
693 buffer
[1] = CMD_DAP_WRITE_ABORT
;
699 retval
= cmsis_dap_usb_xfer(cmsis_dap_handle
, 7);
700 LOG_DEBUG("Result 0x%02" PRIx8
, buffer
[1]);
705 static int cmsis_dap_init(void)
710 if (cmsis_dap_handle
== NULL
) {
713 retval
= cmsis_dap_usb_open();
714 if (retval
!= ERROR_OK
)
717 retval
= cmsis_dap_get_caps_info();
718 if (retval
!= ERROR_OK
)
721 /* Connect in JTAG mode */
722 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_JTAG
)) {
723 LOG_ERROR("CMSIS-DAP: JTAG not supported");
724 return ERROR_JTAG_DEVICE_ERROR
;
727 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG
);
728 if (retval
!= ERROR_OK
)
731 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
734 retval
= cmsis_dap_get_version_info();
735 if (retval
!= ERROR_OK
)
738 /* INFO_ID_PKT_SZ - short */
739 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ
, &data
);
740 if (retval
!= ERROR_OK
)
743 if (data
[0] == 2) { /* short */
744 uint16_t pkt_sz
= data
[1] + (data
[2] << 8);
746 if (cmsis_dap_handle
->packet_size
!= pkt_sz
+ 1) {
747 /* reallocate buffer */
748 cmsis_dap_handle
->packet_size
= pkt_sz
+ 1;
749 cmsis_dap_handle
->packet_buffer
= realloc(cmsis_dap_handle
->packet_buffer
,
750 cmsis_dap_handle
->packet_size
);
751 if (cmsis_dap_handle
->packet_buffer
== NULL
) {
752 LOG_ERROR("unable to reallocate memory");
757 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16
, pkt_sz
);
760 /* INFO_ID_PKT_CNT - byte */
761 retval
= cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT
, &data
);
762 if (retval
!= ERROR_OK
)
765 if (data
[0] == 1) { /* byte */
766 uint16_t pkt_cnt
= data
[1];
767 cmsis_dap_handle
->packet_count
= pkt_cnt
;
768 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16
, pkt_cnt
);
771 retval
= cmsis_dap_get_status();
772 if (retval
!= ERROR_OK
)
775 /* Now try to connect to the target
776 * TODO: This is all SWD only @ present */
777 retval
= cmsis_dap_cmd_DAP_SWJ_Clock(100); /* 100kHz */
778 if (retval
!= ERROR_OK
)
781 retval
= cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
782 if (retval
!= ERROR_OK
)
784 retval
= cmsis_dap_cmd_DAP_SWD_Configure(0x00);
785 if (retval
!= ERROR_OK
)
788 retval
= cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
789 if (retval
!= ERROR_OK
)
792 /* support connecting with srst asserted */
793 enum reset_types jtag_reset_config
= jtag_get_reset_config();
795 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
796 if (jtag_reset_config
& RESET_SRST_NO_GATING
) {
797 retval
= cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL
);
798 if (retval
!= ERROR_OK
)
800 LOG_INFO("Connecting under reset");
804 retval
= cmsis_dap_reset_link();
805 if (retval
!= ERROR_OK
)
808 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
810 LOG_INFO("CMSIS-DAP: Interface ready");
815 static int cmsis_dap_swd_init(uint8_t trn
)
819 DEBUG_IO("CMSIS-DAP: cmsis_dap_swd_init");
821 if (cmsis_dap_handle
== NULL
) {
824 retval
= cmsis_dap_usb_open();
825 if (retval
!= ERROR_OK
)
828 retval
= cmsis_dap_get_caps_info();
829 if (retval
!= ERROR_OK
)
833 if (!(cmsis_dap_handle
->caps
& INFO_CAPS_SWD
)) {
834 LOG_ERROR("CMSIS-DAP: SWD not supported");
835 return ERROR_JTAG_DEVICE_ERROR
;
838 retval
= cmsis_dap_cmd_DAP_Connect(CONNECT_SWD
);
839 if (retval
!= ERROR_OK
)
842 /* Add more setup here.??... */
844 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
848 static int cmsis_dap_quit(void)
850 cmsis_dap_cmd_DAP_Disconnect();
851 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
853 cmsis_dap_usb_close(cmsis_dap_handle
);
858 static void cmsis_dap_execute_reset(struct jtag_command
*cmd
)
860 int retval
= cmsis_dap_cmd_DAP_SWJ_Pins(cmd
->cmd
.reset
->srst
? 0 : (1 << 7), \
862 if (retval
!= ERROR_OK
)
863 LOG_ERROR("CMSIS-DAP: Interface reset failed");
866 static void cmsis_dap_execute_sleep(struct jtag_command
*cmd
)
869 int retval
= cmsis_dap_cmd_DAP_Delay(cmd
->cmd
.sleep
->us
);
870 if (retval
!= ERROR_OK
)
872 jtag_sleep(cmd
->cmd
.sleep
->us
);
875 static void cmsis_dap_execute_command(struct jtag_command
*cmd
)
879 cmsis_dap_execute_reset(cmd
);
882 cmsis_dap_execute_sleep(cmd
);
885 LOG_ERROR("BUG: unknown JTAG command type encountered");
890 static int cmsis_dap_execute_queue(void)
892 struct jtag_command
*cmd
= jtag_command_queue
;
894 while (cmd
!= NULL
) {
895 cmsis_dap_execute_command(cmd
);
902 static int cmsis_dap_speed(int speed
)
904 if (speed
> DAP_MAX_CLOCK
) {
905 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed
, DAP_MAX_CLOCK
);
906 speed
= DAP_MAX_CLOCK
;
910 LOG_INFO("RTCK not supported");
911 return ERROR_JTAG_NOT_IMPLEMENTED
;
914 return cmsis_dap_cmd_DAP_SWJ_Clock(speed
);
917 static int cmsis_dap_speed_div(int speed
, int *khz
)
923 static int cmsis_dap_khz(int khz
, int *jtag_speed
)
929 COMMAND_HANDLER(cmsis_dap_handle_info_command
)
931 if (cmsis_dap_get_version_info() == ERROR_OK
)
932 cmsis_dap_get_status();
937 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command
)
939 if (CMD_ARGC
> MAX_USB_IDS
* 2) {
940 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
941 "(maximum is %d pairs)", MAX_USB_IDS
);
942 CMD_ARGC
= MAX_USB_IDS
* 2;
944 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
945 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
947 return ERROR_COMMAND_SYNTAX_ERROR
;
948 /* remove the incomplete trailing id */
953 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
954 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], cmsis_dap_vid
[i
>> 1]);
955 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], cmsis_dap_pid
[i
>> 1]);
959 * Explicitly terminate, in case there are multiples instances of
962 cmsis_dap_vid
[i
>> 1] = cmsis_dap_pid
[i
>> 1] = 0;
967 static const struct command_registration cmsis_dap_subcommand_handlers
[] = {
970 .handler
= &cmsis_dap_handle_info_command
,
971 .mode
= COMMAND_EXEC
,
973 .help
= "show cmsis-dap info",
975 COMMAND_REGISTRATION_DONE
978 COMMAND_HANDLER(cmsis_dap_reset_command
)
980 LOG_DEBUG("cmsis_dap_reset_command");
984 COMMAND_HANDLER(cmsis_dap_jtag_command
)
986 LOG_DEBUG("cmsis_dap_jtag_command");
990 static const struct command_registration cmsis_dap_jtag_subcommand_handlers
[] = {
994 .handler
= cmsis_dap_jtag_command
,
1000 .handler
= cmsis_dap_jtag_command
,
1004 .name
= "arp_init-reset",
1005 .mode
= COMMAND_ANY
,
1006 .handler
= cmsis_dap_reset_command
,
1010 .name
= "tapisenabled",
1011 .mode
= COMMAND_EXEC
,
1012 .jim_handler
= jim_jtag_tap_enabler
,
1015 .name
= "tapenable",
1016 .mode
= COMMAND_EXEC
,
1017 .jim_handler
= jim_jtag_tap_enabler
,
1020 .name
= "tapdisable",
1021 .mode
= COMMAND_EXEC
,
1022 .handler
= cmsis_dap_jtag_command
,
1026 .name
= "configure",
1027 .mode
= COMMAND_EXEC
,
1028 .handler
= cmsis_dap_jtag_command
,
1033 .mode
= COMMAND_EXEC
,
1034 .jim_handler
= jim_jtag_configure
,
1038 .mode
= COMMAND_ANY
,
1039 .handler
= cmsis_dap_jtag_command
,
1042 COMMAND_REGISTRATION_DONE
1045 static const struct command_registration cmsis_dap_command_handlers
[] = {
1047 .name
= "cmsis-dap",
1048 .mode
= COMMAND_ANY
,
1049 .help
= "perform CMSIS-DAP management",
1051 .chain
= cmsis_dap_subcommand_handlers
,
1054 .name
= "cmsis_dap_vid_pid",
1055 .handler
= &cmsis_dap_handle_vid_pid_command
,
1056 .mode
= COMMAND_CONFIG
,
1057 .help
= "the vendor ID and product ID of the CMSIS-DAP device",
1058 .usage
= "(vid pid)* ",
1061 /* this is currently a nasty hack so we get
1062 * reset working with non jtag interfaces */
1064 .mode
= COMMAND_ANY
,
1066 .chain
= cmsis_dap_jtag_subcommand_handlers
,
1068 COMMAND_REGISTRATION_DONE
1071 static const struct swd_driver cmsis_dap_swd_driver
= {
1072 .init
= cmsis_dap_swd_init
,
1073 .read_reg
= cmsis_dap_swd_read_reg
,
1074 .write_reg
= cmsis_dap_swd_write_reg
,
1077 const char *cmsis_dap_transport
[] = {"cmsis-dap", NULL
};
1079 struct jtag_interface cmsis_dap_interface
= {
1080 .name
= "cmsis-dap",
1081 .commands
= cmsis_dap_command_handlers
,
1082 .swd
= &cmsis_dap_swd_driver
,
1083 .transports
= cmsis_dap_transport
,
1085 .execute_queue
= cmsis_dap_execute_queue
,
1086 .speed
= cmsis_dap_speed
,
1087 .speed_div
= cmsis_dap_speed_div
,
1088 .khz
= cmsis_dap_khz
,
1089 .init
= cmsis_dap_init
,
1090 .quit
= cmsis_dap_quit
,
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)