1 /***************************************************************************
2 * Copyright (C) 2011-2012 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
5 * Copyright (C) 2012 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This code is based on https://github.com/texane/stlink *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
24 ***************************************************************************/
30 /* project specific includes */
31 #include <helper/binarybuffer.h>
32 #include <jtag/interface.h>
33 #include <jtag/hla/hla_layout.h>
34 #include <jtag/hla/hla_transport.h>
35 #include <jtag/hla/hla_interface.h>
36 #include <target/target.h>
38 #include <target/cortex_m.h>
40 #include "libusb_common.h"
42 #define ENDPOINT_IN 0x80
43 #define ENDPOINT_OUT 0x00
45 #define STLINK_WRITE_TIMEOUT 1000
46 #define STLINK_READ_TIMEOUT 1000
48 #define STLINK_NULL_EP 0
49 #define STLINK_RX_EP (1|ENDPOINT_IN)
50 #define STLINK_TX_EP (2|ENDPOINT_OUT)
51 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
52 #define STLINK_SG_SIZE (31)
53 #define STLINK_DATA_SIZE (4*128)
54 #define STLINK_CMD_SIZE_V2 (16)
55 #define STLINK_CMD_SIZE_V1 (10)
57 enum stlink_jtag_api_version {
58 STLINK_JTAG_API_V1 = 1,
63 struct stlink_usb_version {
70 /** highest supported jtag api version */
71 enum stlink_jtag_api_version jtag_api_max;
75 struct stlink_usb_handle_s {
77 struct jtag_libusb_device_handle *fd;
79 struct libusb_transfer *trans;
81 uint8_t cmdbuf[STLINK_SG_SIZE];
87 uint8_t databuf[STLINK_DATA_SIZE];
89 enum hl_transports transport;
91 struct stlink_usb_version version;
96 /** this is the currently used jtag api */
97 enum stlink_jtag_api_version jtag_api;
100 /** whether SWO tracing is enabled or not */
102 /** trace data destination file */
104 /** trace module source clock (for prescaler) */
106 /** trace module clock prescaler */
111 #define STLINK_DEBUG_ERR_OK 0x80
112 #define STLINK_DEBUG_ERR_FAULT 0x81
113 #define STLINK_SWD_AP_WAIT 0x10
114 #define STLINK_SWD_DP_WAIT 0x14
116 #define STLINK_CORE_RUNNING 0x80
117 #define STLINK_CORE_HALTED 0x81
118 #define STLINK_CORE_STAT_UNKNOWN -1
120 #define STLINK_GET_VERSION 0xF1
121 #define STLINK_DEBUG_COMMAND 0xF2
122 #define STLINK_DFU_COMMAND 0xF3
123 #define STLINK_SWIM_COMMAND 0xF4
124 #define STLINK_GET_CURRENT_MODE 0xF5
125 #define STLINK_GET_TARGET_VOLTAGE 0xF7
127 #define STLINK_DEV_DFU_MODE 0x00
128 #define STLINK_DEV_MASS_MODE 0x01
129 #define STLINK_DEV_DEBUG_MODE 0x02
130 #define STLINK_DEV_SWIM_MODE 0x03
131 #define STLINK_DEV_BOOTLOADER_MODE 0x04
132 #define STLINK_DEV_UNKNOWN_MODE -1
134 #define STLINK_DFU_EXIT 0x07
136 #define STLINK_SWIM_ENTER 0x00
137 #define STLINK_SWIM_EXIT 0x01
139 #define STLINK_DEBUG_ENTER_JTAG 0x00
140 #define STLINK_DEBUG_GETSTATUS 0x01
141 #define STLINK_DEBUG_FORCEDEBUG 0x02
142 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
143 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
144 #define STLINK_DEBUG_APIV1_READREG 0x05
145 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
146 #define STLINK_DEBUG_READMEM_32BIT 0x07
147 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
148 #define STLINK_DEBUG_RUNCORE 0x09
149 #define STLINK_DEBUG_STEPCORE 0x0a
150 #define STLINK_DEBUG_APIV1_SETFP 0x0b
151 #define STLINK_DEBUG_READMEM_8BIT 0x0c
152 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
153 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
154 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
155 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
157 #define STLINK_DEBUG_ENTER_JTAG 0x00
158 #define STLINK_DEBUG_ENTER_SWD 0xa3
160 #define STLINK_DEBUG_APIV1_ENTER 0x20
161 #define STLINK_DEBUG_EXIT 0x21
162 #define STLINK_DEBUG_READCOREID 0x22
164 #define STLINK_DEBUG_APIV2_ENTER 0x30
165 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
166 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
167 #define STLINK_DEBUG_APIV2_READREG 0x33
168 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
169 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
170 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
172 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
173 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
174 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
176 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
177 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
178 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
180 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
181 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
182 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
184 #define STLINK_TRACE_SIZE 1024
185 #define STLINK_TRACE_MAX_HZ 2000000
186 #define STLINK_TRACE_MIN_VERSION 13
190 STLINK_MODE_UNKNOWN = 0,
193 STLINK_MODE_DEBUG_JTAG,
194 STLINK_MODE_DEBUG_SWD,
195 STLINK_MODE_DEBUG_SWIM
198 #define REQUEST_SENSE 0x03
199 #define REQUEST_SENSE_LENGTH 18
201 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
204 static int stlink_usb_xfer_v1_get_status(void *handle)
206 struct stlink_usb_handle_s *h;
208 assert(handle != NULL);
210 h = (struct stlink_usb_handle_s *)handle;
213 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
215 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
216 13, STLINK_READ_TIMEOUT) != 13)
221 t1 = buf_get_u32(h->cmdbuf, 0, 32);
224 if (t1 != 0x53425355)
232 if (h->cmdbuf[12] != 0)
239 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
241 struct stlink_usb_handle_s *h;
243 assert(handle != NULL);
245 h = (struct stlink_usb_handle_s *)handle;
247 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
248 STLINK_WRITE_TIMEOUT) != cmdsize) {
252 if (h->direction == STLINK_TX_EP && size) {
253 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
254 size, STLINK_WRITE_TIMEOUT) != size) {
255 LOG_DEBUG("bulk write failed");
258 } else if (h->direction == STLINK_RX_EP && size) {
259 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
260 size, STLINK_READ_TIMEOUT) != size) {
261 LOG_DEBUG("bulk read failed");
270 static int stlink_usb_xfer_v1_get_sense(void *handle)
273 struct stlink_usb_handle_s *h;
275 assert(handle != NULL);
277 h = (struct stlink_usb_handle_s *)handle;
279 stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
281 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
282 h->cmdbuf[h->cmdidx++] = 0;
283 h->cmdbuf[h->cmdidx++] = 0;
284 h->cmdbuf[h->cmdidx++] = 0;
285 h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
287 res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
292 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
299 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
301 int err, cmdsize = STLINK_CMD_SIZE_V2;
302 struct stlink_usb_handle_s *h;
304 assert(handle != NULL);
306 h = (struct stlink_usb_handle_s *)handle;
308 if (h->version.stlink == 1)
309 cmdsize = STLINK_SG_SIZE;
311 err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
316 if (h->version.stlink == 1) {
317 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
318 /* check csw status */
319 if (h->cmdbuf[12] == 1) {
320 LOG_DEBUG("get sense");
321 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
332 static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
334 struct stlink_usb_handle_s *h;
336 assert(handle != NULL);
338 h = (struct stlink_usb_handle_s *)handle;
340 assert(h->version.stlink >= 2);
342 if (jtag_libusb_bulk_read(h->fd, STLINK_TRACE_EP, (char *)buf,
343 size, STLINK_READ_TIMEOUT) != size) {
344 LOG_ERROR("bulk trace read failed");
352 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
354 struct stlink_usb_handle_s *h;
356 h = (struct stlink_usb_handle_s *)handle;
358 /* fill the send buffer */
359 strcpy((char *)h->cmdbuf, "USBC");
361 /* csw tag not used */
363 buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
365 h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
366 h->cmdbuf[h->cmdidx++] = 0; /* lun */
367 h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
371 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
373 struct stlink_usb_handle_s *h;
375 h = (struct stlink_usb_handle_s *)handle;
377 h->direction = direction;
381 memset(h->cmdbuf, 0, STLINK_SG_SIZE);
382 memset(h->databuf, 0, STLINK_DATA_SIZE);
384 if (h->version.stlink == 1)
385 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
388 static const char * const stlink_usb_error_msg[] = {
393 static int stlink_usb_error_check(void *handle)
396 const char *err_msg = 0;
397 struct stlink_usb_handle_s *h;
399 assert(handle != NULL);
401 h = (struct stlink_usb_handle_s *)handle;
403 /* TODO: no error checking yet on api V1 */
404 if (h->jtag_api == STLINK_JTAG_API_V1)
405 h->databuf[0] = STLINK_DEBUG_ERR_OK;
407 switch (h->databuf[0]) {
408 case STLINK_DEBUG_ERR_OK:
411 case STLINK_DEBUG_ERR_FAULT:
413 err_msg = stlink_usb_error_msg[0];
419 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
425 static int stlink_usb_version(void *handle)
429 struct stlink_usb_handle_s *h;
431 assert(handle != NULL);
433 h = (struct stlink_usb_handle_s *)handle;
435 stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
437 h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
439 res = stlink_usb_xfer(handle, h->databuf, 6);
444 v = (h->databuf[0] << 8) | h->databuf[1];
446 h->version.stlink = (v >> 12) & 0x0f;
447 h->version.jtag = (v >> 6) & 0x3f;
448 h->version.swim = v & 0x3f;
449 h->vid = buf_get_u32(h->databuf, 16, 16);
450 h->pid = buf_get_u32(h->databuf, 32, 16);
452 /* set the supported jtag api version
453 * API V2 is supported since JTAG V11
455 if (h->version.jtag >= 11)
456 h->version.jtag_api_max = STLINK_JTAG_API_V2;
458 h->version.jtag_api_max = STLINK_JTAG_API_V1;
460 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
463 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
471 static int stlink_usb_check_voltage(void *handle, float *target_voltage)
473 struct stlink_usb_handle_s *h;
474 uint32_t adc_results[2];
476 h = (struct stlink_usb_handle_s *)handle;
478 /* only supported by stlink/v2 and for firmware >= 13 */
479 if (h->version.stlink == 1 || h->version.jtag < 13)
480 return ERROR_COMMAND_NOTFOUND;
482 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
484 h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
486 int result = stlink_usb_xfer(handle, h->databuf, 8);
488 if (result != ERROR_OK)
492 adc_results[0] = le_to_h_u32(h->databuf);
493 adc_results[1] = le_to_h_u32(h->databuf + 4);
498 *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
500 LOG_INFO("Target voltage: %f", (double)*target_voltage);
506 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
509 struct stlink_usb_handle_s *h;
511 assert(handle != NULL);
513 h = (struct stlink_usb_handle_s *)handle;
515 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
517 h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
519 res = stlink_usb_xfer(handle, h->databuf, 2);
524 *mode = h->databuf[0];
530 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
534 struct stlink_usb_handle_s *h;
536 assert(handle != NULL);
538 h = (struct stlink_usb_handle_s *)handle;
540 /* on api V2 we are able the read the latest command
542 * TODO: we need the test on api V1 too
544 if (h->jtag_api == STLINK_JTAG_API_V2)
547 stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
550 case STLINK_MODE_DEBUG_JTAG:
551 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
552 if (h->jtag_api == STLINK_JTAG_API_V1)
553 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
555 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
556 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
558 case STLINK_MODE_DEBUG_SWD:
559 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
560 if (h->jtag_api == STLINK_JTAG_API_V1)
561 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
563 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
564 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
566 case STLINK_MODE_DEBUG_SWIM:
567 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
568 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
570 case STLINK_MODE_DFU:
571 case STLINK_MODE_MASS:
576 res = stlink_usb_xfer(handle, h->databuf, rx_size);
581 res = stlink_usb_error_check(h);
587 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
590 struct stlink_usb_handle_s *h;
592 assert(handle != NULL);
594 h = (struct stlink_usb_handle_s *)handle;
596 stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
599 case STLINK_MODE_DEBUG_JTAG:
600 case STLINK_MODE_DEBUG_SWD:
601 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
602 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
604 case STLINK_MODE_DEBUG_SWIM:
605 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
606 h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
608 case STLINK_MODE_DFU:
609 h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
610 h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
612 case STLINK_MODE_MASS:
617 res = stlink_usb_xfer(handle, 0, 0);
625 static int stlink_usb_assert_srst(void *handle, int srst);
628 static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
632 enum stlink_mode emode;
633 struct stlink_usb_handle_s *h;
635 assert(handle != NULL);
637 h = (struct stlink_usb_handle_s *)handle;
639 res = stlink_usb_current_mode(handle, &mode);
644 LOG_DEBUG("MODE: 0x%02X", mode);
646 /* try to exit current mode */
648 case STLINK_DEV_DFU_MODE:
649 emode = STLINK_MODE_DFU;
651 case STLINK_DEV_DEBUG_MODE:
652 emode = STLINK_MODE_DEBUG_SWD;
654 case STLINK_DEV_SWIM_MODE:
655 emode = STLINK_MODE_DEBUG_SWIM;
657 case STLINK_DEV_BOOTLOADER_MODE:
658 case STLINK_DEV_MASS_MODE:
660 emode = STLINK_MODE_UNKNOWN;
664 if (emode != STLINK_MODE_UNKNOWN) {
665 res = stlink_usb_mode_leave(handle, emode);
671 res = stlink_usb_current_mode(handle, &mode);
676 /* we check the target voltage here as an aid to debugging connection problems.
677 * the stlink requires the target Vdd to be connected for reliable debugging.
678 * this cmd is supported in all modes except DFU
680 if (mode != STLINK_DEV_DFU_MODE) {
682 float target_voltage;
684 /* check target voltage (if supported) */
685 res = stlink_usb_check_voltage(h, &target_voltage);
687 if (res != ERROR_OK) {
688 if (res != ERROR_COMMAND_NOTFOUND)
689 LOG_ERROR("voltage check failed");
690 /* attempt to continue as it is not a catastrophic failure */
692 /* check for a sensible target voltage, operating range is 1.65-5.5v
693 * according to datasheet */
694 if (target_voltage < 1.5)
695 LOG_ERROR("target voltage may be too low for reliable debugging");
699 LOG_DEBUG("MODE: 0x%02X", mode);
701 /* set selected mode */
702 switch (h->transport) {
703 case HL_TRANSPORT_SWD:
704 emode = STLINK_MODE_DEBUG_SWD;
706 case HL_TRANSPORT_JTAG:
707 emode = STLINK_MODE_DEBUG_JTAG;
709 case HL_TRANSPORT_SWIM:
710 emode = STLINK_MODE_DEBUG_SWIM;
713 emode = STLINK_MODE_UNKNOWN;
717 if (emode == STLINK_MODE_UNKNOWN) {
718 LOG_ERROR("selected mode (transport) not supported");
722 if (connect_under_reset) {
723 res = stlink_usb_assert_srst(handle, 0);
728 res = stlink_usb_mode_enter(handle, emode);
733 res = stlink_usb_current_mode(handle, &mode);
738 LOG_DEBUG("MODE: 0x%02X", mode);
744 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
747 struct stlink_usb_handle_s *h;
749 assert(handle != NULL);
751 h = (struct stlink_usb_handle_s *)handle;
753 stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
755 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
756 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
758 res = stlink_usb_xfer(handle, h->databuf, 4);
763 *idcode = le_to_h_u32(h->databuf);
765 LOG_DEBUG("IDCODE: 0x%08X", *idcode);
770 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
772 struct stlink_usb_handle_s *h;
775 assert(handle != NULL);
777 h = (struct stlink_usb_handle_s *)handle;
779 stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
781 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
782 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
783 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
786 res = stlink_usb_xfer(handle, h->databuf, 8);
791 *val = le_to_h_u32(h->databuf + 4);
793 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
796 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
799 struct stlink_usb_handle_s *h;
801 assert(handle != NULL);
803 h = (struct stlink_usb_handle_s *)handle;
805 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
807 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
808 if (h->jtag_api == STLINK_JTAG_API_V1)
809 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
811 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
812 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
814 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
817 res = stlink_usb_xfer(handle, h->databuf, 2);
822 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
826 static void stlink_usb_trace_read(void *handle)
828 struct stlink_usb_handle_s *h;
830 assert(handle != NULL);
832 h = (struct stlink_usb_handle_s *)handle;
834 if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
837 stlink_usb_init_buffer(handle, STLINK_RX_EP, 10);
839 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
840 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
842 res = stlink_usb_xfer(handle, h->databuf, 2);
843 if (res == ERROR_OK) {
844 uint8_t buf[STLINK_TRACE_SIZE];
845 size_t size = le_to_h_u16(h->databuf);
848 size = size < sizeof(buf) ? size : sizeof(buf) - 1;
850 res = stlink_usb_read_trace(handle, buf, size);
851 if (res == ERROR_OK) {
852 /* Log retrieved trace output */
853 if (fwrite(buf, 1, size, h->trace.output_f) > 0)
854 fflush(h->trace.output_f);
861 static enum target_state stlink_usb_v2_get_status(void *handle)
866 result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
867 if (result != ERROR_OK)
868 return TARGET_UNKNOWN;
871 return TARGET_HALTED;
872 else if (status & S_RESET_ST)
875 stlink_usb_trace_read(handle);
877 return TARGET_RUNNING;
881 static enum target_state stlink_usb_state(void *handle)
884 struct stlink_usb_handle_s *h;
886 assert(handle != NULL);
888 h = (struct stlink_usb_handle_s *)handle;
890 if (h->jtag_api == STLINK_JTAG_API_V2)
891 return stlink_usb_v2_get_status(handle);
893 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
895 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
896 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
898 res = stlink_usb_xfer(handle, h->databuf, 2);
901 return TARGET_UNKNOWN;
903 if (h->databuf[0] == STLINK_CORE_RUNNING)
904 return TARGET_RUNNING;
905 if (h->databuf[0] == STLINK_CORE_HALTED)
906 return TARGET_HALTED;
908 return TARGET_UNKNOWN;
912 static int stlink_usb_reset(void *handle)
915 struct stlink_usb_handle_s *h;
917 assert(handle != NULL);
919 h = (struct stlink_usb_handle_s *)handle;
921 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
923 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
925 if (h->jtag_api == STLINK_JTAG_API_V1)
926 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
928 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
930 res = stlink_usb_xfer(handle, h->databuf, 2);
935 LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
937 /* the following is not a error under swd (using hardware srst), so return success */
938 if (h->databuf[0] == STLINK_SWD_AP_WAIT || h->databuf[0] == STLINK_SWD_DP_WAIT)
941 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
944 static int stlink_usb_assert_srst(void *handle, int srst)
947 struct stlink_usb_handle_s *h;
949 assert(handle != NULL);
951 h = (struct stlink_usb_handle_s *)handle;
953 if (h->jtag_api == STLINK_JTAG_API_V1)
954 return ERROR_COMMAND_NOTFOUND;
956 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
958 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
959 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
960 h->cmdbuf[h->cmdidx++] = srst;
962 res = stlink_usb_xfer(handle, h->databuf, 2);
967 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
971 static int stlink_configure_target_trace_port(void *handle)
975 struct stlink_usb_handle_s *h;
977 assert(handle != NULL);
979 h = (struct stlink_usb_handle_s *)handle;
981 /* configure the TPI */
983 /* enable the trace subsystem */
984 res = stlink_usb_v2_read_debug_reg(handle, DCB_DEMCR, ®);
987 res = stlink_usb_write_debug_reg(handle, DCB_DEMCR, TRCENA|reg);
990 /* set the TPI clock prescaler */
991 res = stlink_usb_write_debug_reg(handle, TPI_ACPR, h->trace.prescale);
994 /* select the pin protocol. The STLinkv2 only supports asynchronous
995 * UART emulation (NRZ) mode, so that's what we pick. */
996 res = stlink_usb_write_debug_reg(handle, TPI_SPPR, 0x02);
999 /* disable continuous formatting */
1000 res = stlink_usb_write_debug_reg(handle, TPI_FFCR, (1<<8));
1001 if (res != ERROR_OK)
1004 /* configure the ITM */
1006 /* unlock access to the ITM registers */
1007 res = stlink_usb_write_debug_reg(handle, ITM_LAR, 0xC5ACCE55);
1008 if (res != ERROR_OK)
1010 /* enable trace with ATB ID 1 */
1011 res = stlink_usb_write_debug_reg(handle, ITM_TCR, (1<<16)|(1<<0)|(1<<2));
1012 if (res != ERROR_OK)
1014 /* trace privilege */
1015 res = stlink_usb_write_debug_reg(handle, ITM_TPR, 1);
1016 if (res != ERROR_OK)
1018 /* trace port enable (port 0) */
1019 res = stlink_usb_write_debug_reg(handle, ITM_TER, (1<<0));
1020 if (res != ERROR_OK)
1029 static void stlink_usb_trace_disable(void *handle)
1032 struct stlink_usb_handle_s *h;
1034 assert(handle != NULL);
1036 h = (struct stlink_usb_handle_s *)handle;
1038 assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION);
1040 LOG_DEBUG("Tracing: disable\n");
1042 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1043 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1044 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
1045 res = stlink_usb_xfer(handle, h->databuf, 2);
1047 if (res == ERROR_OK)
1048 h->trace.enabled = false;
1053 static int stlink_usb_trace_enable(void *handle)
1056 struct stlink_usb_handle_s *h;
1058 assert(handle != NULL);
1060 h = (struct stlink_usb_handle_s *)handle;
1062 if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
1065 res = stlink_configure_target_trace_port(handle);
1066 if (res != ERROR_OK)
1067 LOG_ERROR("Unable to configure tracing on target\n");
1069 trace_hz = h->trace.prescale > 0 ?
1070 h->trace.source_hz / (h->trace.prescale + 1) :
1073 stlink_usb_init_buffer(handle, STLINK_RX_EP, 10);
1075 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1076 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
1077 h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
1079 h_u32_to_le(h->cmdbuf+h->cmdidx, trace_hz);
1082 res = stlink_usb_xfer(handle, h->databuf, 2);
1084 if (res == ERROR_OK) {
1085 h->trace.enabled = true;
1086 LOG_DEBUG("Tracing: recording at %uHz\n", trace_hz);
1089 LOG_ERROR("Tracing is not supported by this version.");
1097 static int stlink_usb_run(void *handle)
1100 struct stlink_usb_handle_s *h;
1102 assert(handle != NULL);
1104 h = (struct stlink_usb_handle_s *)handle;
1106 if (h->jtag_api == STLINK_JTAG_API_V2) {
1107 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
1109 /* Try to start tracing, if requested */
1110 if (res == ERROR_OK && h->trace.output_f) {
1111 if (stlink_usb_trace_enable(handle) == ERROR_OK)
1112 LOG_DEBUG("Tracing: enabled\n");
1114 LOG_ERROR("Tracing: enable failed\n");
1120 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1122 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1123 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
1125 res = stlink_usb_xfer(handle, h->databuf, 2);
1127 if (res != ERROR_OK)
1130 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1134 static int stlink_usb_halt(void *handle)
1137 struct stlink_usb_handle_s *h;
1139 assert(handle != NULL);
1141 h = (struct stlink_usb_handle_s *)handle;
1143 if (h->jtag_api == STLINK_JTAG_API_V2) {
1144 res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1146 if (res == ERROR_OK && h->trace.enabled)
1147 stlink_usb_trace_disable(handle);
1152 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1154 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1155 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
1157 res = stlink_usb_xfer(handle, h->databuf, 2);
1159 if (res != ERROR_OK)
1162 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1166 static int stlink_usb_step(void *handle)
1169 struct stlink_usb_handle_s *h;
1171 assert(handle != NULL);
1173 h = (struct stlink_usb_handle_s *)handle;
1175 if (h->jtag_api == STLINK_JTAG_API_V2) {
1176 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1177 * that the cortex-m3 currently does. */
1178 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
1179 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
1180 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
1183 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1185 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1186 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
1188 res = stlink_usb_xfer(handle, h->databuf, 2);
1190 if (res != ERROR_OK)
1193 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1197 static int stlink_usb_read_regs(void *handle)
1200 struct stlink_usb_handle_s *h;
1202 assert(handle != NULL);
1204 h = (struct stlink_usb_handle_s *)handle;
1206 stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
1208 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1209 if (h->jtag_api == STLINK_JTAG_API_V1)
1210 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
1212 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
1214 res = stlink_usb_xfer(handle, h->databuf, 84);
1216 if (res != ERROR_OK)
1223 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
1226 struct stlink_usb_handle_s *h;
1228 assert(handle != NULL);
1230 h = (struct stlink_usb_handle_s *)handle;
1232 stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1234 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1235 if (h->jtag_api == STLINK_JTAG_API_V1)
1236 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
1238 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
1239 h->cmdbuf[h->cmdidx++] = num;
1241 res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
1243 if (res != ERROR_OK)
1246 if (h->jtag_api == STLINK_JTAG_API_V1)
1247 *val = le_to_h_u32(h->databuf);
1249 *val = le_to_h_u32(h->databuf + 4);
1250 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1257 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
1260 struct stlink_usb_handle_s *h;
1262 assert(handle != NULL);
1264 h = (struct stlink_usb_handle_s *)handle;
1266 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1268 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1269 if (h->jtag_api == STLINK_JTAG_API_V1)
1270 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
1272 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
1273 h->cmdbuf[h->cmdidx++] = num;
1274 h_u32_to_le(h->cmdbuf+h->cmdidx, val);
1277 res = stlink_usb_xfer(handle, h->databuf, 2);
1279 if (res != ERROR_OK)
1282 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
1285 static int stlink_usb_get_rw_status(void *handle)
1288 struct stlink_usb_handle_s *h;
1290 assert(handle != NULL);
1292 h = (struct stlink_usb_handle_s *)handle;
1294 if (h->jtag_api == STLINK_JTAG_API_V1)
1297 stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1299 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1300 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1302 res = stlink_usb_xfer(handle, h->databuf, 2);
1304 if (res != ERROR_OK)
1307 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1311 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1315 uint16_t read_len = len;
1316 struct stlink_usb_handle_s *h;
1318 assert(handle != NULL);
1320 h = (struct stlink_usb_handle_s *)handle;
1322 stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1324 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1325 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1326 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1328 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1331 /* we need to fix read length for single bytes */
1335 res = stlink_usb_xfer(handle, h->databuf, read_len);
1337 if (res != ERROR_OK)
1340 memcpy(buffer, h->databuf, len);
1342 return stlink_usb_get_rw_status(handle);
1346 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1347 const uint8_t *buffer)
1350 struct stlink_usb_handle_s *h;
1352 assert(handle != NULL);
1354 h = (struct stlink_usb_handle_s *)handle;
1356 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1358 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1359 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1360 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1362 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1365 res = stlink_usb_xfer(handle, buffer, len);
1367 if (res != ERROR_OK)
1370 return stlink_usb_get_rw_status(handle);
1374 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1378 struct stlink_usb_handle_s *h;
1380 assert(handle != NULL);
1382 h = (struct stlink_usb_handle_s *)handle;
1386 stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1388 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1389 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1390 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1392 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1395 res = stlink_usb_xfer(handle, h->databuf, len);
1397 if (res != ERROR_OK)
1400 memcpy(buffer, h->databuf, len);
1402 return stlink_usb_get_rw_status(handle);
1406 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1407 const uint8_t *buffer)
1410 struct stlink_usb_handle_s *h;
1412 assert(handle != NULL);
1414 h = (struct stlink_usb_handle_s *)handle;
1418 stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1420 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1421 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1422 h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1424 h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1427 res = stlink_usb_xfer(handle, buffer, len);
1429 if (res != ERROR_OK)
1432 return stlink_usb_get_rw_status(handle);
1435 static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
1436 uint32_t count, uint8_t *buffer)
1439 return stlink_usb_read_mem32(handle, addr, count, buffer);
1441 return stlink_usb_read_mem8(handle, addr, count, buffer);
1444 static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
1445 uint32_t count, const uint8_t *buffer)
1448 return stlink_usb_write_mem32(handle, addr, count, buffer);
1450 return stlink_usb_write_mem8(handle, addr, count, buffer);
1454 static int stlink_usb_close(void *fd)
1456 struct stlink_usb_handle_s *h;
1458 h = (struct stlink_usb_handle_s *)fd;
1461 jtag_libusb_close(h->fd);
1469 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1471 int err, retry_count = 1;
1472 struct stlink_usb_handle_s *h;
1473 enum stlink_jtag_api_version api;
1475 LOG_DEBUG("stlink_usb_open");
1477 h = calloc(1, sizeof(struct stlink_usb_handle_s));
1480 LOG_DEBUG("malloc failed");
1484 h->transport = param->transport;
1486 /* set max read/write buffer size in bytes */
1487 param->max_buffer = 512;
1489 const uint16_t vids[] = { param->vid, 0 };
1490 const uint16_t pids[] = { param->pid, 0 };
1492 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1493 param->vid, param->pid);
1496 On certain host USB configurations(e.g. MacBook Air)
1497 STLINKv2 dongle seems to have its FW in a funky state if,
1498 after plugging it in, you try to use openocd with it more
1499 then once (by launching and closing openocd). In cases like
1500 that initial attempt to read the FW info via
1501 stlink_usb_version will fail and the device has to be reset
1502 in order to become operational.
1505 if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1506 LOG_ERROR("open failed");
1510 jtag_libusb_set_configuration(h->fd, 0);
1512 if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1513 LOG_DEBUG("claim interface failed");
1517 /* wrap version for first read */
1518 switch (param->pid) {
1520 h->version.stlink = 1;
1523 h->version.stlink = 2;
1527 /* get the device version */
1528 err = stlink_usb_version(h);
1530 if (err == ERROR_OK) {
1532 } else if (h->version.stlink == 1 ||
1534 LOG_ERROR("read version failed");
1537 err = jtag_libusb_release_interface(h->fd, 0);
1538 if (err != ERROR_OK) {
1539 LOG_ERROR("release interface failed");
1543 err = jtag_libusb_reset_device(h->fd);
1544 if (err != ERROR_OK) {
1545 LOG_ERROR("reset device failed");
1549 jtag_libusb_close(h->fd);
1551 Give the device one second to settle down and
1554 usleep(1 * 1000 * 1000);
1559 /* compare usb vid/pid */
1560 if ((param->vid != h->vid) || (param->pid != h->pid))
1561 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1562 param->vid, param->pid,
1565 /* check if mode is supported */
1568 switch (h->transport) {
1569 case HL_TRANSPORT_SWD:
1570 case HL_TRANSPORT_JTAG:
1571 if (h->version.jtag == 0)
1574 case HL_TRANSPORT_SWIM:
1575 if (h->version.swim == 0)
1583 if (err != ERROR_OK) {
1584 LOG_ERROR("mode (transport) not supported by device");
1588 api = h->version.jtag_api_max;
1590 /* check that user has not requested certain api version
1591 * and if they have check it is supported */
1592 if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1594 LOG_INFO("using stlink api v%d", api);
1597 /* set the used jtag api, this will default to the newest supported version */
1600 if (h->jtag_api >= 2 && param->trace_f && param->trace_source_hz > 0) {
1603 prescale = param->trace_source_hz > STLINK_TRACE_MAX_HZ ?
1604 (param->trace_source_hz / STLINK_TRACE_MAX_HZ) - 1 : 0;
1606 h->trace.output_f = param->trace_f;
1607 h->trace.source_hz = param->trace_source_hz;
1608 h->trace.prescale = prescale;
1611 /* initialize the debug hardware */
1612 err = stlink_usb_init_mode(h, param->connect_under_reset);
1614 if (err != ERROR_OK) {
1615 LOG_ERROR("init mode failed");
1624 stlink_usb_close(h);
1630 struct hl_layout_api_s stlink_usb_layout_api = {
1632 .open = stlink_usb_open,
1634 .close = stlink_usb_close,
1636 .idcode = stlink_usb_idcode,
1638 .state = stlink_usb_state,
1640 .reset = stlink_usb_reset,
1642 .assert_srst = stlink_usb_assert_srst,
1644 .run = stlink_usb_run,
1646 .halt = stlink_usb_halt,
1648 .step = stlink_usb_step,
1650 .read_regs = stlink_usb_read_regs,
1652 .read_reg = stlink_usb_read_reg,
1654 .write_reg = stlink_usb_write_reg,
1656 .read_mem = stlink_usb_read_mem,
1658 .write_mem = stlink_usb_write_mem,
1660 .write_debug_reg = stlink_usb_write_debug_reg