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 };
63 static wchar_t *cmsis_dap_serial;
66 #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
67 #define USB_TIMEOUT 1000
69 /* CMSIS-DAP General Commands */
70 #define CMD_DAP_INFO 0x00
71 #define CMD_DAP_LED 0x01
72 #define CMD_DAP_CONNECT 0x02
73 #define CMD_DAP_DISCONNECT 0x03
74 #define CMD_DAP_WRITE_ABORT 0x08
75 #define CMD_DAP_DELAY 0x09
76 #define CMD_DAP_RESET_TARGET 0x0A
79 #define INFO_ID_VID 0x00 /* string */
80 #define INFO_ID_PID 0x02 /* string */
81 #define INFO_ID_SERNUM 0x03 /* string */
82 #define INFO_ID_FW_VER 0x04 /* string */
83 #define INFO_ID_TD_VEND 0x05 /* string */
84 #define INFO_ID_TD_NAME 0x06 /* string */
85 #define INFO_ID_CAPS 0xf0 /* byte */
86 #define INFO_ID_PKT_CNT 0xfe /* byte */
87 #define INFO_ID_PKT_SZ 0xff /* short */
89 #define INFO_CAPS_SWD 0x01
90 #define INFO_CAPS_JTAG 0x02
93 #define LED_ID_CONNECT 0x00
94 #define LED_ID_RUN 0x01
100 #define CONNECT_DEFAULT 0x00
101 #define CONNECT_SWD 0x01
102 #define CONNECT_JTAG 0x02
104 /* CMSIS-DAP Common SWD/JTAG Commands */
105 #define CMD_DAP_DELAY 0x09
106 #define CMD_DAP_SWJ_PINS 0x10
107 #define CMD_DAP_SWJ_CLOCK 0x11
108 #define CMD_DAP_SWJ_SEQ 0x12
120 /* CMSIS-DAP SWD Commands */
121 #define CMD_DAP_SWD_CONFIGURE 0x13
123 /* CMSIS-DAP JTAG Commands */
124 #define CMD_DAP_JTAG_SEQ 0x14
125 #define CMD_DAP_JTAG_CONFIGURE 0x15
126 #define CMD_DAP_JTAG_IDCODE 0x16
128 /* CMSIS-DAP Transfer Commands */
129 #define CMD_DAP_TFER_CONFIGURE 0x04
130 #define CMD_DAP_TFER 0x05
131 #define CMD_DAP_TFER_BLOCK 0x06
132 #define CMD_DAP_TFER_ABORT 0x07
134 /* DAP Status Code */
136 #define DAP_ERROR 0xFF
138 /* CMSIS-DAP Vendor Commands
141 static const char * const info_caps_str[] = {
146 /* max clock speed (kHz) */
147 #define DAP_MAX_CLOCK 5000
150 hid_device *dev_handle;
151 uint16_t packet_size;
152 uint16_t packet_count;
153 uint8_t *packet_buffer;
158 static struct cmsis_dap *cmsis_dap_handle;
160 static int cmsis_dap_usb_open(void)
162 hid_device *dev = NULL;
164 struct hid_device_info *devs, *cur_dev;
165 unsigned short target_vid, target_pid;
166 wchar_t *target_serial = NULL;
169 bool serial_found = false;
175 * The CMSIS-DAP specification stipulates:
176 * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
177 * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
179 devs = hid_enumerate(0x0, 0x0);
181 while (NULL != cur_dev) {
182 if (0 == cmsis_dap_vid[0]) {
183 if (NULL == cur_dev->product_string) {
184 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
185 cur_dev->vendor_id, cur_dev->product_id);
187 if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
188 /* if the user hasn't specified VID:PID *and*
189 * product string contains "CMSIS-DAP", pick it
195 /* otherwise, exhaustively compare against all VID:PID in list */
196 for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
197 if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
201 if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
206 /* we have found an adapter, so exit further checks */
207 /* check serial number matches if given */
208 if (cmsis_dap_serial != NULL) {
209 if (wcscmp(cmsis_dap_serial, cur_dev->serial_number) == 0) {
217 cur_dev = cur_dev->next;
220 if (NULL != cur_dev) {
221 target_vid = cur_dev->vendor_id;
222 target_pid = cur_dev->product_id;
224 target_serial = cmsis_dap_serial;
227 hid_free_enumeration(devs);
229 if (target_vid == 0 && target_pid == 0) {
230 LOG_ERROR("unable to find CMSIS-DAP device");
234 if (hid_init() != 0) {
235 LOG_ERROR("unable to open HIDAPI");
239 dev = hid_open(target_vid, target_pid, target_serial);
242 LOG_ERROR("unable to open CMSIS-DAP device");
246 struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
248 LOG_ERROR("unable to allocate memory");
252 dap->dev_handle = dev;
256 cmsis_dap_handle = dap;
258 /* allocate default packet buffer, may be changed later.
259 * currently with HIDAPI we have no way of getting the output report length
260 * without this info we cannot communicate with the adapter.
261 * For the moment we ahve to hard code the packet size */
263 int packet_size = PACKET_SIZE;
265 /* atmel cmsis-dap uses 512 byte reports */
266 if (target_vid == 0x03eb)
267 packet_size = 512 + 1;
269 cmsis_dap_handle->packet_buffer = malloc(packet_size);
270 cmsis_dap_handle->packet_size = packet_size;
272 if (cmsis_dap_handle->packet_buffer == NULL) {
273 LOG_ERROR("unable to allocate memory");
280 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
282 hid_close(dap->dev_handle);
285 if (cmsis_dap_handle->packet_buffer)
286 free(cmsis_dap_handle->packet_buffer);
288 if (cmsis_dap_handle) {
289 free(cmsis_dap_handle);
290 cmsis_dap_handle = NULL;
293 if (cmsis_dap_serial) {
294 free(cmsis_dap_serial);
295 cmsis_dap_serial = NULL;
301 /* Send a message and receive the reply */
302 static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
304 /* Pad the rest of the TX buffer with 0's */
305 memset(dap->packet_buffer + txlen, 0, dap->packet_size - 1 - txlen);
307 /* write data to device */
308 int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
310 LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
315 retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
316 if (retval == -1 || retval == 0) {
317 LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
324 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
327 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
329 buffer[0] = 0; /* report number */
330 buffer[1] = CMD_DAP_SWJ_PINS;
333 buffer[4] = delay & 0xff;
334 buffer[5] = (delay >> 8) & 0xff;
335 buffer[6] = (delay >> 16) & 0xff;
336 buffer[7] = (delay >> 24) & 0xff;
337 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
339 if (retval != ERROR_OK) {
340 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
341 return ERROR_JTAG_DEVICE_ERROR;
350 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
353 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
355 /* set clock in Hz */
357 buffer[0] = 0; /* report number */
358 buffer[1] = CMD_DAP_SWJ_CLOCK;
359 buffer[2] = swj_clock & 0xff;
360 buffer[3] = (swj_clock >> 8) & 0xff;
361 buffer[4] = (swj_clock >> 16) & 0xff;
362 buffer[5] = (swj_clock >> 24) & 0xff;
363 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
365 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
366 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
367 return ERROR_JTAG_DEVICE_ERROR;
373 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
376 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
378 buffer[0] = 0; /* report number */
379 buffer[1] = CMD_DAP_INFO;
381 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
383 if (retval != ERROR_OK) {
384 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
385 return ERROR_JTAG_DEVICE_ERROR;
388 *data = &(buffer[1]);
393 static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
396 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
398 buffer[0] = 0; /* report number */
399 buffer[1] = CMD_DAP_LED;
402 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
404 if (retval != ERROR_OK || buffer[1] != 0x00) {
405 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
406 return ERROR_JTAG_DEVICE_ERROR;
412 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
415 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
417 buffer[0] = 0; /* report number */
418 buffer[1] = CMD_DAP_CONNECT;
420 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
422 if (retval != ERROR_OK) {
423 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
424 return ERROR_JTAG_DEVICE_ERROR;
427 if (buffer[1] != mode) {
428 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
429 return ERROR_JTAG_DEVICE_ERROR;
435 static int cmsis_dap_cmd_DAP_Disconnect(void)
438 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
440 buffer[0] = 0; /* report number */
441 buffer[1] = CMD_DAP_DISCONNECT;
442 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
444 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
445 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
446 return ERROR_JTAG_DEVICE_ERROR;
452 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t delay, uint16_t retry)
455 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
457 buffer[0] = 0; /* report number */
458 buffer[1] = CMD_DAP_TFER_CONFIGURE;
460 buffer[3] = delay & 0xff;
461 buffer[4] = (delay >> 8) & 0xff;
462 buffer[5] = retry & 0xff;
463 buffer[6] = (retry >> 8) & 0xff;
464 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
466 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
467 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
468 return ERROR_JTAG_DEVICE_ERROR;
474 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
477 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
479 buffer[0] = 0; /* report number */
480 buffer[1] = CMD_DAP_SWD_CONFIGURE;
482 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
484 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
485 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
486 return ERROR_JTAG_DEVICE_ERROR;
493 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
496 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
498 buffer[0] = 0; /* report number */
499 buffer[1] = CMD_DAP_DELAY;
500 buffer[2] = delay_us & 0xff;
501 buffer[3] = (delay_us >> 8) & 0xff;
502 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
504 if (retval != ERROR_OK || buffer[1] != DAP_OK) {
505 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
506 return ERROR_JTAG_DEVICE_ERROR;
513 static int queued_retval;
515 static void cmsis_dap_swd_read_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t *value)
517 if (queued_retval != ERROR_OK)
520 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
524 DEBUG_IO("CMSIS-DAP: Read Reg 0x%02" PRIx8, cmd);
526 buffer[0] = 0; /* report number */
527 buffer[1] = CMD_DAP_TFER;
531 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
533 /* TODO - need better response checking */
534 if (retval != ERROR_OK || buffer[1] != 0x01) {
535 LOG_ERROR("CMSIS-DAP: Read Error (0x%02" PRIx8 ")", buffer[2]);
536 queued_retval = buffer[2];
540 val = le_to_h_u32(&buffer[3]);
541 DEBUG_IO("0x%08" PRIx32, val);
546 queued_retval = retval;
549 static void cmsis_dap_swd_write_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t value)
551 if (queued_retval != ERROR_OK)
554 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
556 DEBUG_IO("CMSIS-DAP: Write Reg 0x%02" PRIx8 " 0x%08" PRIx32, cmd, value);
558 buffer[0] = 0; /* report number */
559 buffer[1] = CMD_DAP_TFER;
563 buffer[5] = (value) & 0xff;
564 buffer[6] = (value >> 8) & 0xff;
565 buffer[7] = (value >> 16) & 0xff;
566 buffer[8] = (value >> 24) & 0xff;
567 int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 9);
569 if (buffer[1] != 0x01) {
570 LOG_ERROR("CMSIS-DAP: Write Error (0x%02" PRIx8 ")", buffer[2]);
574 queued_retval = retval;
577 static int cmsis_dap_swd_run(struct adiv5_dap *dap)
579 int retval = queued_retval;
580 queued_retval = ERROR_OK;
584 static int cmsis_dap_get_version_info(void)
588 /* INFO_ID_FW_VER - string */
589 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
590 if (retval != ERROR_OK)
593 if (data[0]) /* strlen */
594 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
599 static int cmsis_dap_get_caps_info(void)
603 /* INFO_ID_CAPS - byte */
604 int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
605 if (retval != ERROR_OK)
609 uint8_t caps = data[1];
611 cmsis_dap_handle->caps = caps;
613 if (caps & INFO_CAPS_SWD)
614 LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
615 if (caps & INFO_CAPS_JTAG)
616 LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
622 static int cmsis_dap_get_status(void)
626 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
628 if (retval == ERROR_OK) {
629 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
630 (d & (0x01 << 0)) ? 1 : 0, /* Bit 0: SWCLK/TCK */
631 (d & (0x01 << 1)) ? 1 : 0, /* Bit 1: SWDIO/TMS */
632 (d & (0x01 << 2)) ? 1 : 0, /* Bit 2: TDI */
633 (d & (0x01 << 3)) ? 1 : 0, /* Bit 3: TDO */
634 (d & (0x01 << 5)) ? 1 : 0, /* Bit 5: nTRST */
635 (d & (0x01 << 7)) ? 1 : 0); /* Bit 7: nRESET */
641 static int cmsis_dap_reset_link(void)
643 uint8_t *buffer = cmsis_dap_handle->packet_buffer;
645 LOG_DEBUG("CMSIS-DAP: cmsis_dap_reset_link");
646 LOG_INFO("DAP_SWJ Sequence (reset: 50+ '1' followed by 0)");
648 /* reset line with SWDIO high for >50 cycles */
649 buffer[0] = 0; /* report number */
650 buffer[1] = CMD_DAP_SWJ_SEQ;
659 int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
661 if (retval != ERROR_OK || buffer[1] != DAP_OK)
664 /* 16bit JTAG-SWD sequence */
665 buffer[0] = 0; /* report number */
666 buffer[1] = CMD_DAP_SWJ_SEQ;
670 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
672 if (retval != ERROR_OK || buffer[1] != DAP_OK)
675 /* another reset just incase */
676 buffer[0] = 0; /* report number */
677 buffer[1] = CMD_DAP_SWJ_SEQ;
686 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
688 if (retval != ERROR_OK || buffer[1] != DAP_OK)
691 /* 16 cycle idle period */
692 buffer[0] = 0; /* report number */
693 buffer[1] = CMD_DAP_SWJ_SEQ;
697 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
699 if (retval != ERROR_OK || buffer[1] != DAP_OK)
702 DEBUG_IO("DAP Read IDCODE");
704 /* read the id code is always the next sequence */
705 buffer[0] = 0; /* report number */
706 buffer[1] = CMD_DAP_TFER;
710 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
712 if (retval != ERROR_OK)
715 if (buffer[1] == 0) {
716 LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
718 LOG_DEBUG("DAP Reset Target");
719 buffer[0] = 0; /* report number */
720 buffer[1] = CMD_DAP_RESET_TARGET;
721 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
722 LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
724 LOG_DEBUG("DAP Write Abort");
725 buffer[0] = 0; /* report number */
726 buffer[1] = CMD_DAP_WRITE_ABORT;
728 buffer[3] = 0x1e/*0x1f*/;
732 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
733 LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
735 return 0x80 + buffer[1];
738 LOG_DEBUG("DAP Write Abort");
739 buffer[0] = 0; /* report number */
740 buffer[1] = CMD_DAP_WRITE_ABORT;
746 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
747 LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
752 static int cmsis_dap_swd_open(void)
756 DEBUG_IO("CMSIS-DAP: cmsis_dap_swd_open");
758 if (cmsis_dap_handle == NULL) {
761 retval = cmsis_dap_usb_open();
762 if (retval != ERROR_OK)
765 retval = cmsis_dap_get_caps_info();
766 if (retval != ERROR_OK)
770 if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
771 LOG_ERROR("CMSIS-DAP: SWD not supported");
772 return ERROR_JTAG_DEVICE_ERROR;
775 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
776 if (retval != ERROR_OK)
779 /* Add more setup here.??... */
781 LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
785 static int cmsis_dap_init(void)
791 retval = cmsis_dap_swd_open();
792 if (retval != ERROR_OK)
796 if (cmsis_dap_handle == NULL) {
799 retval = cmsis_dap_usb_open();
800 if (retval != ERROR_OK)
803 retval = cmsis_dap_get_caps_info();
804 if (retval != ERROR_OK)
807 /* Connect in JTAG mode */
808 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
809 LOG_ERROR("CMSIS-DAP: JTAG not supported");
810 return ERROR_JTAG_DEVICE_ERROR;
813 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
814 if (retval != ERROR_OK)
817 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
820 retval = cmsis_dap_get_version_info();
821 if (retval != ERROR_OK)
824 /* INFO_ID_PKT_SZ - short */
825 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
826 if (retval != ERROR_OK)
829 if (data[0] == 2) { /* short */
830 uint16_t pkt_sz = data[1] + (data[2] << 8);
832 if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
833 /* reallocate buffer */
834 cmsis_dap_handle->packet_size = pkt_sz + 1;
835 cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
836 cmsis_dap_handle->packet_size);
837 if (cmsis_dap_handle->packet_buffer == NULL) {
838 LOG_ERROR("unable to reallocate memory");
843 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
846 /* INFO_ID_PKT_CNT - byte */
847 retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
848 if (retval != ERROR_OK)
851 if (data[0] == 1) { /* byte */
852 uint16_t pkt_cnt = data[1];
853 cmsis_dap_handle->packet_count = pkt_cnt;
854 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
857 retval = cmsis_dap_get_status();
858 if (retval != ERROR_OK)
861 /* Now try to connect to the target
862 * TODO: This is all SWD only @ present */
863 retval = cmsis_dap_cmd_DAP_SWJ_Clock(100); /* 100kHz */
864 if (retval != ERROR_OK)
867 retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
868 if (retval != ERROR_OK)
870 retval = cmsis_dap_cmd_DAP_SWD_Configure(0x00);
871 if (retval != ERROR_OK)
874 retval = cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
875 if (retval != ERROR_OK)
878 /* support connecting with srst asserted */
879 enum reset_types jtag_reset_config = jtag_get_reset_config();
881 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
882 if (jtag_reset_config & RESET_SRST_NO_GATING) {
883 retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL);
884 if (retval != ERROR_OK)
886 LOG_INFO("Connecting under reset");
890 retval = cmsis_dap_reset_link();
891 if (retval != ERROR_OK)
894 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
896 LOG_INFO("CMSIS-DAP: Interface ready");
901 static int cmsis_dap_swd_init(void)
907 static int cmsis_dap_quit(void)
909 cmsis_dap_cmd_DAP_Disconnect();
910 cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
912 cmsis_dap_usb_close(cmsis_dap_handle);
917 static void cmsis_dap_execute_reset(struct jtag_command *cmd)
919 int retval = cmsis_dap_cmd_DAP_SWJ_Pins(cmd->cmd.reset->srst ? 0 : (1 << 7), \
921 if (retval != ERROR_OK)
922 LOG_ERROR("CMSIS-DAP: Interface reset failed");
925 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
928 int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
929 if (retval != ERROR_OK)
931 jtag_sleep(cmd->cmd.sleep->us);
934 static void cmsis_dap_execute_command(struct jtag_command *cmd)
938 cmsis_dap_execute_reset(cmd);
941 cmsis_dap_execute_sleep(cmd);
944 LOG_ERROR("BUG: unknown JTAG command type encountered");
949 static int cmsis_dap_execute_queue(void)
951 struct jtag_command *cmd = jtag_command_queue;
953 while (cmd != NULL) {
954 cmsis_dap_execute_command(cmd);
961 static int cmsis_dap_speed(int speed)
963 if (speed > DAP_MAX_CLOCK) {
964 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, DAP_MAX_CLOCK);
965 speed = DAP_MAX_CLOCK;
969 LOG_INFO("RTCK not supported");
970 return ERROR_JTAG_NOT_IMPLEMENTED;
973 return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
976 static int cmsis_dap_speed_div(int speed, int *khz)
982 static int cmsis_dap_khz(int khz, int *jtag_speed)
988 COMMAND_HANDLER(cmsis_dap_handle_info_command)
990 if (cmsis_dap_get_version_info() == ERROR_OK)
991 cmsis_dap_get_status();
996 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
998 if (CMD_ARGC > MAX_USB_IDS * 2) {
999 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1000 "(maximum is %d pairs)", MAX_USB_IDS);
1001 CMD_ARGC = MAX_USB_IDS * 2;
1003 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
1004 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1006 return ERROR_COMMAND_SYNTAX_ERROR;
1007 /* remove the incomplete trailing id */
1012 for (i = 0; i < CMD_ARGC; i += 2) {
1013 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
1014 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
1018 * Explicitly terminate, in case there are multiples instances of
1019 * cmsis_dap_vid_pid.
1021 cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
1026 COMMAND_HANDLER(cmsis_dap_handle_serial_command)
1028 if (CMD_ARGC == 1) {
1029 size_t len = mbstowcs(NULL, CMD_ARGV[0], 0);
1030 cmsis_dap_serial = calloc(len + 1, sizeof(wchar_t));
1031 if (cmsis_dap_serial == NULL) {
1032 LOG_ERROR("unable to allocate memory");
1035 if (mbstowcs(cmsis_dap_serial, CMD_ARGV[0], len + 1) == (size_t)-1) {
1036 free(cmsis_dap_serial);
1037 cmsis_dap_serial = NULL;
1038 LOG_ERROR("unable to convert serial");
1041 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1047 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
1050 .handler = &cmsis_dap_handle_info_command,
1051 .mode = COMMAND_EXEC,
1053 .help = "show cmsis-dap info",
1055 COMMAND_REGISTRATION_DONE
1058 static const struct command_registration cmsis_dap_command_handlers[] = {
1060 .name = "cmsis-dap",
1061 .mode = COMMAND_ANY,
1062 .help = "perform CMSIS-DAP management",
1064 .chain = cmsis_dap_subcommand_handlers,
1067 .name = "cmsis_dap_vid_pid",
1068 .handler = &cmsis_dap_handle_vid_pid_command,
1069 .mode = COMMAND_CONFIG,
1070 .help = "the vendor ID and product ID of the CMSIS-DAP device",
1071 .usage = "(vid pid)* ",
1074 .name = "cmsis_dap_serial",
1075 .handler = &cmsis_dap_handle_serial_command,
1076 .mode = COMMAND_CONFIG,
1077 .help = "set the serial number of the adapter",
1078 .usage = "serial_string",
1080 COMMAND_REGISTRATION_DONE
1083 static const struct swd_driver cmsis_dap_swd_driver = {
1084 .init = cmsis_dap_swd_init,
1085 .read_reg = cmsis_dap_swd_read_reg,
1086 .write_reg = cmsis_dap_swd_write_reg,
1087 .run = cmsis_dap_swd_run,
1090 const char *cmsis_dap_transport[] = {"cmsis-dap", NULL};
1092 struct jtag_interface cmsis_dap_interface = {
1093 .name = "cmsis-dap",
1094 .commands = cmsis_dap_command_handlers,
1095 .swd = &cmsis_dap_swd_driver,
1096 .transports = cmsis_dap_transport,
1098 .execute_queue = cmsis_dap_execute_queue,
1099 .speed = cmsis_dap_speed,
1100 .speed_div = cmsis_dap_speed_div,
1101 .khz = cmsis_dap_khz,
1102 .init = cmsis_dap_init,
1103 .quit = cmsis_dap_quit,