1 /***************************************************************************
2 * SWIM contributions by Ake Rehnman *
3 * Copyright (C) 2017 Ake Rehnman *
4 * ake.rehnman(at)gmail.com *
6 * Copyright (C) 2011-2012 by Mathias Kuester *
7 * Mathias Kuester <kesmtp@freenet.de> *
9 * Copyright (C) 2012 by Spencer Oliver *
10 * spen@spen-soft.co.uk *
12 * This code is based on https://github.com/texane/stlink *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
32 /* project specific includes */
33 #include <helper/binarybuffer.h>
34 #include <helper/bits.h>
35 #include <jtag/interface.h>
36 #include <jtag/hla/hla_layout.h>
37 #include <jtag/hla/hla_transport.h>
38 #include <jtag/hla/hla_interface.h>
39 #include <target/target.h>
40 #include <transport/transport.h>
42 #include <target/cortex_m.h>
44 #include "libusb_common.h"
47 #define USE_LIBUSB_ASYNCIO
50 #define ENDPOINT_IN 0x80
51 #define ENDPOINT_OUT 0x00
53 #define STLINK_WRITE_TIMEOUT 1000
54 #define STLINK_READ_TIMEOUT 1000
56 #define STLINK_NULL_EP 0
57 #define STLINK_RX_EP (1|ENDPOINT_IN)
58 #define STLINK_TX_EP (2|ENDPOINT_OUT)
59 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
61 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
62 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
64 #define STLINK_SG_SIZE (31)
65 #define STLINK_DATA_SIZE (4096)
66 #define STLINK_CMD_SIZE_V2 (16)
67 #define STLINK_CMD_SIZE_V1 (10)
69 #define STLINK_V1_PID (0x3744)
70 #define STLINK_V2_PID (0x3748)
71 #define STLINK_V2_1_PID (0x374B)
72 #define STLINK_V2_1_NO_MSD_PID (0x3752)
73 #define STLINK_V3_USBLOADER_PID (0x374D)
74 #define STLINK_V3E_PID (0x374E)
75 #define STLINK_V3S_PID (0x374F)
76 #define STLINK_V3_2VCP_PID (0x3753)
79 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
80 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
81 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes.
83 #define STLINK_MAX_RW8 (64)
84 #define STLINKV3_MAX_RW8 (512)
86 /* "WAIT" responses will be retried (with exponential backoff) at
87 * most this many times before failing to caller.
89 #define MAX_WAIT_RETRIES 8
91 enum stlink_jtag_api_version
{
92 STLINK_JTAG_API_V1
= 1,
98 struct stlink_usb_version
{
105 /** jtag api version supported */
106 enum stlink_jtag_api_version jtag_api
;
107 /** one bit for each feature supported. See macros STLINK_F_* */
112 struct stlink_usb_handle_s
{
114 struct jtag_libusb_device_handle
*fd
;
116 struct libusb_transfer
*trans
;
124 uint8_t cmdbuf
[STLINK_SG_SIZE
];
130 uint8_t databuf
[STLINK_DATA_SIZE
];
132 uint32_t max_mem_packet
;
134 enum hl_transports transport
;
136 struct stlink_usb_version version
;
143 /** whether SWO tracing is enabled or not */
145 /** trace module source clock */
148 /** reconnect is needed next time we try to query the
150 bool reconnect_pending
;
153 #define STLINK_SWIM_ERR_OK 0x00
154 #define STLINK_SWIM_BUSY 0x01
155 #define STLINK_DEBUG_ERR_OK 0x80
156 #define STLINK_DEBUG_ERR_FAULT 0x81
157 #define STLINK_SWD_AP_WAIT 0x10
158 #define STLINK_SWD_AP_FAULT 0x11
159 #define STLINK_SWD_AP_ERROR 0x12
160 #define STLINK_SWD_AP_PARITY_ERROR 0x13
161 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
162 #define STLINK_JTAG_WRITE_ERROR 0x0c
163 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
164 #define STLINK_SWD_DP_WAIT 0x14
165 #define STLINK_SWD_DP_FAULT 0x15
166 #define STLINK_SWD_DP_ERROR 0x16
167 #define STLINK_SWD_DP_PARITY_ERROR 0x17
169 #define STLINK_SWD_AP_WDATA_ERROR 0x18
170 #define STLINK_SWD_AP_STICKY_ERROR 0x19
171 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
173 #define STLINK_BAD_AP_ERROR 0x1d
175 #define STLINK_CORE_RUNNING 0x80
176 #define STLINK_CORE_HALTED 0x81
177 #define STLINK_CORE_STAT_UNKNOWN -1
179 #define STLINK_GET_VERSION 0xF1
180 #define STLINK_DEBUG_COMMAND 0xF2
181 #define STLINK_DFU_COMMAND 0xF3
182 #define STLINK_SWIM_COMMAND 0xF4
183 #define STLINK_GET_CURRENT_MODE 0xF5
184 #define STLINK_GET_TARGET_VOLTAGE 0xF7
186 #define STLINK_DEV_DFU_MODE 0x00
187 #define STLINK_DEV_MASS_MODE 0x01
188 #define STLINK_DEV_DEBUG_MODE 0x02
189 #define STLINK_DEV_SWIM_MODE 0x03
190 #define STLINK_DEV_BOOTLOADER_MODE 0x04
191 #define STLINK_DEV_UNKNOWN_MODE -1
193 #define STLINK_DFU_EXIT 0x07
196 STLINK_SWIM_ENTER_SEQ
197 1.3ms low then 750Hz then 1.5kHz
200 STM8 DM pulls reset pin low 50us
203 uint8_t (0=low|1=high)
210 send syncronization seq (16us low, response 64 clocks low)
212 #define STLINK_SWIM_ENTER 0x00
213 #define STLINK_SWIM_EXIT 0x01
214 #define STLINK_SWIM_READ_CAP 0x02
215 #define STLINK_SWIM_SPEED 0x03
216 #define STLINK_SWIM_ENTER_SEQ 0x04
217 #define STLINK_SWIM_GEN_RST 0x05
218 #define STLINK_SWIM_RESET 0x06
219 #define STLINK_SWIM_ASSERT_RESET 0x07
220 #define STLINK_SWIM_DEASSERT_RESET 0x08
221 #define STLINK_SWIM_READSTATUS 0x09
222 #define STLINK_SWIM_WRITEMEM 0x0a
223 #define STLINK_SWIM_READMEM 0x0b
224 #define STLINK_SWIM_READBUF 0x0c
226 #define STLINK_DEBUG_GETSTATUS 0x01
227 #define STLINK_DEBUG_FORCEDEBUG 0x02
228 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
229 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
230 #define STLINK_DEBUG_APIV1_READREG 0x05
231 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
232 #define STLINK_DEBUG_READMEM_32BIT 0x07
233 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
234 #define STLINK_DEBUG_RUNCORE 0x09
235 #define STLINK_DEBUG_STEPCORE 0x0a
236 #define STLINK_DEBUG_APIV1_SETFP 0x0b
237 #define STLINK_DEBUG_READMEM_8BIT 0x0c
238 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
239 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
240 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
241 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
243 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
244 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
245 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
247 #define STLINK_DEBUG_APIV1_ENTER 0x20
248 #define STLINK_DEBUG_EXIT 0x21
249 #define STLINK_DEBUG_READCOREID 0x22
251 #define STLINK_DEBUG_APIV2_ENTER 0x30
252 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
253 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
254 #define STLINK_DEBUG_APIV2_READREG 0x33
255 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
256 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
257 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
259 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
260 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
261 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
263 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
265 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
266 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
267 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
268 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
269 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
270 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
271 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
272 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
273 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
275 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
276 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
278 #define STLINK_APIV3_SET_COM_FREQ 0x61
279 #define STLINK_APIV3_GET_COM_FREQ 0x62
281 #define STLINK_APIV3_GET_VERSION_EX 0xFB
283 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
284 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
285 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
287 #define STLINK_DEBUG_PORT_ACCESS 0xffff
289 #define STLINK_TRACE_SIZE 4096
290 #define STLINK_TRACE_MAX_HZ 2000000
292 #define STLINK_V3_MAX_FREQ_NB 10
296 STLINK_MODE_UNKNOWN
= 0,
299 STLINK_MODE_DEBUG_JTAG
,
300 STLINK_MODE_DEBUG_SWD
,
301 STLINK_MODE_DEBUG_SWIM
304 #define REQUEST_SENSE 0x03
305 #define REQUEST_SENSE_LENGTH 18
308 * Map the relevant features, quirks and workaround for specific firmware
311 #define STLINK_F_HAS_TRACE BIT(0)
312 #define STLINK_F_HAS_SWD_SET_FREQ BIT(1)
313 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(2)
314 #define STLINK_F_HAS_MEM_16BIT BIT(3)
315 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(4)
316 #define STLINK_F_HAS_DAP_REG BIT(5)
317 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(6)
318 #define STLINK_F_HAS_AP_INIT BIT(7)
321 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
328 /* SWD clock speed */
329 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
331 {1800, 1}, /* default */
344 /* JTAG clock speed */
345 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
350 {1125, 32}, /* default */
356 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
357 static int stlink_swim_status(void *handle
);
358 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
359 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
360 static int stlink_speed(void *handle
, int khz
, bool query
);
363 static unsigned int stlink_usb_block(void *handle
)
365 struct stlink_usb_handle_s
*h
= handle
;
367 assert(handle
!= NULL
);
369 if (h
->version
.stlink
== 3)
370 return STLINKV3_MAX_RW8
;
372 return STLINK_MAX_RW8
;
377 #ifdef USE_LIBUSB_ASYNCIO
379 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
381 int *completed
= transfer
->user_data
;
383 /* caller interprets result and frees transfer */
387 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
389 int r
, *completed
= transfer
->user_data
;
391 /* Assuming a single libusb context exists. There no existing interface into this
392 * module to pass a libusb context.
394 struct libusb_context
*ctx
= NULL
;
396 while (!*completed
) {
397 r
= libusb_handle_events_completed(ctx
, completed
);
399 if (r
== LIBUSB_ERROR_INTERRUPTED
)
401 libusb_cancel_transfer(transfer
);
408 static int transfer_error_status(const struct libusb_transfer
*transfer
)
412 switch (transfer
->status
) {
413 case LIBUSB_TRANSFER_COMPLETED
:
416 case LIBUSB_TRANSFER_TIMED_OUT
:
417 r
= LIBUSB_ERROR_TIMEOUT
;
419 case LIBUSB_TRANSFER_STALL
:
420 r
= LIBUSB_ERROR_PIPE
;
422 case LIBUSB_TRANSFER_OVERFLOW
:
423 r
= LIBUSB_ERROR_OVERFLOW
;
425 case LIBUSB_TRANSFER_NO_DEVICE
:
426 r
= LIBUSB_ERROR_NO_DEVICE
;
428 case LIBUSB_TRANSFER_ERROR
:
429 case LIBUSB_TRANSFER_CANCELLED
:
433 r
= LIBUSB_ERROR_OTHER
;
447 size_t transfer_size
;
448 struct libusb_transfer
*transfer
;
451 static int jtag_libusb_bulk_transfer_n(
452 jtag_libusb_device_handle
* dev_handle
,
453 struct jtag_xfer
*transfers
,
458 int returnval
= ERROR_OK
;
461 for (size_t i
= 0; i
< n_transfers
; ++i
) {
462 transfers
[i
].retval
= 0;
463 transfers
[i
].completed
= 0;
464 transfers
[i
].transfer_size
= 0;
465 transfers
[i
].transfer
= libusb_alloc_transfer(0);
467 if (transfers
[i
].transfer
== NULL
) {
468 for (size_t j
= 0; j
< i
; ++j
)
469 libusb_free_transfer(transfers
[j
].transfer
);
471 LOG_DEBUG("ERROR, failed to alloc usb transfers");
472 for (size_t k
= 0; k
< n_transfers
; ++k
)
473 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
478 for (size_t i
= 0; i
< n_transfers
; ++i
) {
479 libusb_fill_bulk_transfer(
480 transfers
[i
].transfer
,
482 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
483 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
484 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
486 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
488 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
490 /* Probably no point continuing to submit transfers once a submission fails.
491 * As a result, tag all remaining transfers as errors.
493 for (size_t j
= i
; j
< n_transfers
; ++j
)
494 transfers
[j
].retval
= retval
;
496 returnval
= ERROR_FAIL
;
501 /* Wait for every submitted USB transfer to complete.
503 for (size_t i
= 0; i
< n_transfers
; ++i
) {
504 if (transfers
[i
].retval
== 0) {
505 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
507 retval
= transfer_error_status(transfers
[i
].transfer
);
509 returnval
= ERROR_FAIL
;
510 transfers
[i
].retval
= retval
;
511 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
513 /* Assuming actual_length is only valid if there is no transfer error.
515 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
519 libusb_free_transfer(transfers
[i
].transfer
);
520 transfers
[i
].transfer
= NULL
;
530 static int stlink_usb_xfer_v1_get_status(void *handle
)
532 struct stlink_usb_handle_s
*h
= handle
;
534 assert(handle
!= NULL
);
537 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
539 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
540 13, STLINK_READ_TIMEOUT
) != 13)
545 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
548 if (t1
!= 0x53425355)
556 if (h
->cmdbuf
[12] != 0)
562 #ifdef USE_LIBUSB_ASYNCIO
563 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
565 struct stlink_usb_handle_s
*h
= handle
;
567 assert(handle
!= NULL
);
569 size_t n_transfers
= 0;
570 struct jtag_xfer transfers
[2];
572 memset(transfers
, 0, sizeof(transfers
));
574 transfers
[0].ep
= h
->tx_ep
;
575 transfers
[0].buf
= h
->cmdbuf
;
576 transfers
[0].size
= cmdsize
;
580 if (h
->direction
== h
->tx_ep
&& size
) {
581 transfers
[1].ep
= h
->tx_ep
;
582 transfers
[1].buf
= (uint8_t *)buf
;
583 transfers
[1].size
= size
;
586 } else if (h
->direction
== h
->rx_ep
&& size
) {
587 transfers
[1].ep
= h
->rx_ep
;
588 transfers
[1].buf
= (uint8_t *)buf
;
589 transfers
[1].size
= size
;
594 return jtag_libusb_bulk_transfer_n(
598 STLINK_WRITE_TIMEOUT
);
601 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
603 struct stlink_usb_handle_s
*h
= handle
;
605 assert(handle
!= NULL
);
607 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
608 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
612 if (h
->direction
== h
->tx_ep
&& size
) {
613 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
614 size
, STLINK_WRITE_TIMEOUT
) != size
) {
615 LOG_DEBUG("bulk write failed");
618 } else if (h
->direction
== h
->rx_ep
&& size
) {
619 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
620 size
, STLINK_READ_TIMEOUT
) != size
) {
621 LOG_DEBUG("bulk read failed");
631 static int stlink_usb_xfer_v1_get_sense(void *handle
)
634 struct stlink_usb_handle_s
*h
= handle
;
636 assert(handle
!= NULL
);
638 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
640 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
641 h
->cmdbuf
[h
->cmdidx
++] = 0;
642 h
->cmdbuf
[h
->cmdidx
++] = 0;
643 h
->cmdbuf
[h
->cmdidx
++] = 0;
644 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
646 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
651 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
658 transfers block in cmdbuf
659 <size> indicates number of bytes in the following
661 Ignore the (eventual) error code in the received packet.
663 static int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
665 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
666 struct stlink_usb_handle_s
*h
= handle
;
668 assert(handle
!= NULL
);
670 if (h
->version
.stlink
== 1) {
671 cmdsize
= STLINK_SG_SIZE
;
672 /* put length in bCBWCBLength */
673 h
->cmdbuf
[14] = h
->cmdidx
-15;
676 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
681 if (h
->version
.stlink
== 1) {
682 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
683 /* check csw status */
684 if (h
->cmdbuf
[12] == 1) {
685 LOG_DEBUG("get sense");
686 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
697 Converts an STLINK status code held in the first byte of a response
698 to an openocd error, logs any error/wait status as debug output.
700 static int stlink_usb_error_check(void *handle
)
702 struct stlink_usb_handle_s
*h
= handle
;
704 assert(handle
!= NULL
);
706 if (h
->transport
== HL_TRANSPORT_SWIM
) {
707 switch (h
->databuf
[0]) {
708 case STLINK_SWIM_ERR_OK
:
710 case STLINK_SWIM_BUSY
:
713 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
718 /* TODO: no error checking yet on api V1 */
719 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
720 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
722 switch (h
->databuf
[0]) {
723 case STLINK_DEBUG_ERR_OK
:
725 case STLINK_DEBUG_ERR_FAULT
:
726 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
728 case STLINK_SWD_AP_WAIT
:
729 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
731 case STLINK_SWD_DP_WAIT
:
732 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
734 case STLINK_JTAG_GET_IDCODE_ERROR
:
735 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
737 case STLINK_JTAG_WRITE_ERROR
:
738 LOG_DEBUG("Write error");
740 case STLINK_JTAG_WRITE_VERIF_ERROR
:
741 LOG_DEBUG("Write verify error, ignoring");
743 case STLINK_SWD_AP_FAULT
:
744 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
745 * returns ERROR_OK with the comment:
746 * Change in error status when reading outside RAM.
747 * This fix allows CDT plugin to visualize memory.
749 LOG_DEBUG("STLINK_SWD_AP_FAULT");
751 case STLINK_SWD_AP_ERROR
:
752 LOG_DEBUG("STLINK_SWD_AP_ERROR");
754 case STLINK_SWD_AP_PARITY_ERROR
:
755 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
757 case STLINK_SWD_DP_FAULT
:
758 LOG_DEBUG("STLINK_SWD_DP_FAULT");
760 case STLINK_SWD_DP_ERROR
:
761 LOG_DEBUG("STLINK_SWD_DP_ERROR");
763 case STLINK_SWD_DP_PARITY_ERROR
:
764 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
766 case STLINK_SWD_AP_WDATA_ERROR
:
767 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
769 case STLINK_SWD_AP_STICKY_ERROR
:
770 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
772 case STLINK_SWD_AP_STICKYORUN_ERROR
:
773 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
775 case STLINK_BAD_AP_ERROR
:
776 LOG_DEBUG("STLINK_BAD_AP_ERROR");
779 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
785 * Wrapper around stlink_usb_xfer_noerrcheck()
786 * to check the error code in the received packet
788 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
794 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
795 if (retval
!= ERROR_OK
)
798 return stlink_usb_error_check(handle
);
801 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
803 Works for commands where the STLINK_DEBUG status is returned in the first
804 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
806 Returns an openocd result code.
808 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
812 struct stlink_usb_handle_s
*h
= handle
;
815 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
816 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
821 if (h
->transport
== HL_TRANSPORT_SWIM
) {
822 res
= stlink_swim_status(handle
);
827 res
= stlink_usb_error_check(handle
);
828 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
829 useconds_t delay_us
= (1<<retries
++) * 1000;
830 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
839 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
841 struct stlink_usb_handle_s
*h
= handle
;
843 assert(handle
!= NULL
);
845 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
847 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
848 size
, STLINK_READ_TIMEOUT
) != size
) {
849 LOG_ERROR("bulk trace read failed");
857 this function writes transfer length in
858 the right place in the cb
860 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
862 struct stlink_usb_handle_s
*h
= handle
;
864 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
867 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
869 struct stlink_usb_handle_s
*h
= handle
;
871 /* fill the send buffer */
872 strcpy((char *)h
->cmdbuf
, "USBC");
874 /* csw tag not used */
875 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
877 /* cbw data transfer length (in the following data phase in or out) */
878 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
881 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
882 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
883 /* cdb clength (is filled in at xfer) */
884 h
->cmdbuf
[h
->cmdidx
++] = 0;
888 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
890 struct stlink_usb_handle_s
*h
= handle
;
892 h
->direction
= direction
;
896 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
897 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
899 if (h
->version
.stlink
== 1)
900 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
904 static int stlink_usb_version(void *handle
)
909 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
910 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
912 struct stlink_usb_handle_s
*h
= handle
;
914 assert(handle
!= NULL
);
916 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
918 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
920 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
925 version
= be_to_h_u16(h
->databuf
);
926 v
= (version
>> 12) & 0x0f;
927 x
= (version
>> 6) & 0x3f;
930 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
931 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
934 case STLINK_V2_1_PID
:
935 case STLINK_V2_1_NO_MSD_PID
:
936 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
937 /* MxSy : STM8 V2.1 - SWIM only */
942 /* JxMy : STM32 V2.1 - JTAG/SWD only */
955 /* STLINK-V3 requires a specific command */
956 if (v
== 3 && x
== 0 && y
== 0) {
957 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
959 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
961 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
966 swim
= h
->databuf
[1];
967 jtag
= h
->databuf
[2];
969 bridge
= h
->databuf
[4];
970 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
971 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
974 h
->version
.stlink
= v
;
975 h
->version
.jtag
= jtag
;
976 h
->version
.swim
= swim
;
979 switch (h
->version
.stlink
) {
981 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
982 if (h
->version
.jtag
>= 11)
983 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
985 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
989 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
990 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
992 /* API for trace from J13 */
993 /* API for target voltage from J13 */
994 if (h
->version
.jtag
>= 13)
995 flags
|= STLINK_F_HAS_TRACE
;
997 /* preferred API to get last R/W status from J15 */
998 if (h
->version
.jtag
>= 15)
999 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1001 /* API to set SWD frequency from J22 */
1002 if (h
->version
.jtag
>= 22)
1003 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1005 /* API to set JTAG frequency from J24 */
1006 /* API to access DAP registers from J24 */
1007 if (h
->version
.jtag
>= 24) {
1008 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1009 flags
|= STLINK_F_HAS_DAP_REG
;
1012 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1013 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1014 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1016 /* API to read/write memory at 16 bit from J26 */
1017 if (h
->version
.jtag
>= 26)
1018 flags
|= STLINK_F_HAS_MEM_16BIT
;
1020 /* API required to init AP before any AP access from J28 */
1021 if (h
->version
.jtag
>= 28)
1022 flags
|= STLINK_F_HAS_AP_INIT
;
1026 /* all STLINK-V3 use api-v3 */
1027 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1029 /* STLINK-V3 is a superset of ST-LINK/V2 */
1032 /* API for target voltage */
1033 flags
|= STLINK_F_HAS_TRACE
;
1035 /* preferred API to get last R/W status */
1036 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1038 /* API to access DAP registers */
1039 flags
|= STLINK_F_HAS_DAP_REG
;
1041 /* API to read/write memory at 16 bit */
1042 flags
|= STLINK_F_HAS_MEM_16BIT
;
1044 /* API required to init AP before any AP access */
1045 flags
|= STLINK_F_HAS_AP_INIT
;
1051 h
->version
.flags
= flags
;
1054 p
+= sprintf(p
, "V%d", v
);
1056 p
+= sprintf(p
, "J%d", jtag
);
1058 p
+= sprintf(p
, "M%d", msd
);
1060 p
+= sprintf(p
, "B%d", bridge
);
1062 sprintf(p
, "S%d", swim
);
1064 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1066 h
->version
.jtag_api
,
1073 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1075 struct stlink_usb_handle_s
*h
= handle
;
1076 uint32_t adc_results
[2];
1078 /* no error message, simply quit with error */
1079 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1080 return ERROR_COMMAND_NOTFOUND
;
1082 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1084 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1086 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1088 if (result
!= ERROR_OK
)
1091 /* convert result */
1092 adc_results
[0] = le_to_h_u32(h
->databuf
);
1093 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1095 *target_voltage
= 0;
1098 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1100 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1105 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1107 struct stlink_usb_handle_s
*h
= handle
;
1109 assert(handle
!= NULL
);
1111 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1112 return ERROR_COMMAND_NOTFOUND
;
1114 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1116 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1117 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1118 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1121 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1123 if (result
!= ERROR_OK
)
1129 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1131 struct stlink_usb_handle_s
*h
= handle
;
1133 assert(handle
!= NULL
);
1135 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1136 return ERROR_COMMAND_NOTFOUND
;
1138 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1140 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1141 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1142 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1145 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1147 if (result
!= ERROR_OK
)
1154 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1157 struct stlink_usb_handle_s
*h
= handle
;
1159 assert(handle
!= NULL
);
1161 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1163 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1165 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1167 if (res
!= ERROR_OK
)
1170 *mode
= h
->databuf
[0];
1176 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1179 struct stlink_usb_handle_s
*h
= handle
;
1181 assert(handle
!= NULL
);
1183 /* on api V2 we are able the read the latest command
1185 * TODO: we need the test on api V1 too
1187 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1190 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1193 case STLINK_MODE_DEBUG_JTAG
:
1194 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1195 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1196 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1198 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1199 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1201 case STLINK_MODE_DEBUG_SWD
:
1202 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1203 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1204 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1206 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1207 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1209 case STLINK_MODE_DEBUG_SWIM
:
1210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1211 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1212 /* no answer for this function... */
1215 case STLINK_MODE_DFU
:
1216 case STLINK_MODE_MASS
:
1221 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1225 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1228 struct stlink_usb_handle_s
*h
= handle
;
1230 assert(handle
!= NULL
);
1232 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
1235 case STLINK_MODE_DEBUG_JTAG
:
1236 case STLINK_MODE_DEBUG_SWD
:
1237 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1238 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1240 case STLINK_MODE_DEBUG_SWIM
:
1241 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1242 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1244 case STLINK_MODE_DFU
:
1245 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1246 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1248 case STLINK_MODE_MASS
:
1253 res
= stlink_usb_xfer_noerrcheck(handle
, 0, 0);
1255 if (res
!= ERROR_OK
)
1261 static int stlink_usb_assert_srst(void *handle
, int srst
);
1263 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1266 case HL_TRANSPORT_SWD
:
1267 return STLINK_MODE_DEBUG_SWD
;
1268 case HL_TRANSPORT_JTAG
:
1269 return STLINK_MODE_DEBUG_JTAG
;
1270 case HL_TRANSPORT_SWIM
:
1271 return STLINK_MODE_DEBUG_SWIM
;
1273 return STLINK_MODE_UNKNOWN
;
1278 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1282 enum stlink_mode emode
;
1283 struct stlink_usb_handle_s
*h
= handle
;
1285 assert(handle
!= NULL
);
1287 res
= stlink_usb_current_mode(handle
, &mode
);
1289 if (res
!= ERROR_OK
)
1292 LOG_DEBUG("MODE: 0x%02X", mode
);
1294 /* try to exit current mode */
1296 case STLINK_DEV_DFU_MODE
:
1297 emode
= STLINK_MODE_DFU
;
1299 case STLINK_DEV_DEBUG_MODE
:
1300 emode
= STLINK_MODE_DEBUG_SWD
;
1302 case STLINK_DEV_SWIM_MODE
:
1303 emode
= STLINK_MODE_DEBUG_SWIM
;
1305 case STLINK_DEV_BOOTLOADER_MODE
:
1306 case STLINK_DEV_MASS_MODE
:
1308 emode
= STLINK_MODE_UNKNOWN
;
1312 if (emode
!= STLINK_MODE_UNKNOWN
) {
1313 res
= stlink_usb_mode_leave(handle
, emode
);
1315 if (res
!= ERROR_OK
)
1319 res
= stlink_usb_current_mode(handle
, &mode
);
1321 if (res
!= ERROR_OK
)
1324 /* we check the target voltage here as an aid to debugging connection problems.
1325 * the stlink requires the target Vdd to be connected for reliable debugging.
1326 * this cmd is supported in all modes except DFU
1328 if (mode
!= STLINK_DEV_DFU_MODE
) {
1330 float target_voltage
;
1332 /* check target voltage (if supported) */
1333 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1335 if (res
!= ERROR_OK
) {
1336 if (res
!= ERROR_COMMAND_NOTFOUND
)
1337 LOG_ERROR("voltage check failed");
1338 /* attempt to continue as it is not a catastrophic failure */
1340 /* check for a sensible target voltage, operating range is 1.65-5.5v
1341 * according to datasheet */
1342 if (target_voltage
< 1.5)
1343 LOG_ERROR("target voltage may be too low for reliable debugging");
1347 LOG_DEBUG("MODE: 0x%02X", mode
);
1349 /* set selected mode */
1350 emode
= stlink_get_mode(h
->transport
);
1352 if (emode
== STLINK_MODE_UNKNOWN
) {
1353 LOG_ERROR("selected mode (transport) not supported");
1357 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1358 if (h
->transport
== HL_TRANSPORT_JTAG
) {
1359 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1360 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1361 stlink_speed(h
, initial_interface_speed
, false);
1363 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
1364 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1365 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1366 stlink_speed(h
, initial_interface_speed
, false);
1370 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
1371 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1373 stlink_get_com_freq(h
, (h
->transport
== HL_TRANSPORT_JTAG
), map
);
1374 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1375 stlink_speed(h
, initial_interface_speed
, false);
1378 /* preliminary SRST assert:
1379 * We want SRST is asserted before activating debug signals (mode_enter).
1380 * As the required mode has not been set, the adapter may not know what pin to use.
1381 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1382 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1383 * after power on, SWIM_RST stays unchanged */
1384 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1385 stlink_usb_assert_srst(handle
, 0);
1386 /* do not check the return status here, we will
1387 proceed and enter the desired mode below
1388 and try asserting srst again. */
1390 res
= stlink_usb_mode_enter(handle
, emode
);
1391 if (res
!= ERROR_OK
)
1394 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1395 if (connect_under_reset
) {
1396 res
= stlink_usb_assert_srst(handle
, 0);
1397 if (res
!= ERROR_OK
)
1401 res
= stlink_usb_current_mode(handle
, &mode
);
1403 if (res
!= ERROR_OK
)
1406 LOG_DEBUG("MODE: 0x%02X", mode
);
1411 /* request status from last swim request */
1412 static int stlink_swim_status(void *handle
)
1414 struct stlink_usb_handle_s
*h
= handle
;
1417 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1418 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1419 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1420 /* error is checked by the caller */
1421 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1422 if (res
!= ERROR_OK
)
1427 the purpose of this function is unknown...
1428 capabilites? anyway for swim v6 it returns
1431 __attribute__((unused
))
1432 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1434 struct stlink_usb_handle_s
*h
= handle
;
1437 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1438 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1439 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1440 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1441 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1442 if (res
!= ERROR_OK
)
1444 memcpy(cap
, h
->databuf
, 8);
1448 /* debug dongle assert/deassert sreset line */
1449 static int stlink_swim_assert_reset(void *handle
, int reset
)
1451 struct stlink_usb_handle_s
*h
= handle
;
1454 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1455 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1457 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1459 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1460 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1461 if (res
!= ERROR_OK
)
1468 1.3ms low then 750Hz then 1.5kHz
1470 static int stlink_swim_enter(void *handle
)
1472 struct stlink_usb_handle_s
*h
= handle
;
1475 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1476 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1477 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1478 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1479 if (res
!= ERROR_OK
)
1484 /* switch high/low speed swim */
1485 static int stlink_swim_speed(void *handle
, int speed
)
1487 struct stlink_usb_handle_s
*h
= handle
;
1490 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1491 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1492 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1494 h
->cmdbuf
[h
->cmdidx
++] = 1;
1496 h
->cmdbuf
[h
->cmdidx
++] = 0;
1497 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1498 if (res
!= ERROR_OK
)
1504 initiate srst from swim.
1505 nrst is pulled low for 50us.
1507 static int stlink_swim_generate_rst(void *handle
)
1509 struct stlink_usb_handle_s
*h
= handle
;
1512 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1513 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1514 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1515 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1516 if (res
!= ERROR_OK
)
1522 send resyncronize sequence
1523 swim is pulled low for 16us
1524 reply is 64 clks low
1526 static int stlink_swim_resync(void *handle
)
1528 struct stlink_usb_handle_s
*h
= handle
;
1531 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1532 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1533 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1534 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1535 if (res
!= ERROR_OK
)
1540 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1542 struct stlink_usb_handle_s
*h
= handle
;
1545 unsigned int datalen
= 0;
1546 int cmdsize
= STLINK_CMD_SIZE_V2
;
1548 if (len
> STLINK_DATA_SIZE
)
1551 if (h
->version
.stlink
== 1)
1552 cmdsize
= STLINK_SG_SIZE
;
1554 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1555 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1556 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1557 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1559 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1561 for (i
= 0; i
< len
; i
++) {
1562 if (h
->cmdidx
== cmdsize
)
1563 h
->databuf
[datalen
++] = *(data
++);
1565 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1567 if (h
->version
.stlink
== 1)
1568 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1570 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1571 if (res
!= ERROR_OK
)
1576 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1578 struct stlink_usb_handle_s
*h
= handle
;
1581 if (len
> STLINK_DATA_SIZE
)
1584 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1585 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1586 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1587 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1589 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1591 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1592 if (res
!= ERROR_OK
)
1595 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1596 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1597 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1598 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1599 if (res
!= ERROR_OK
)
1606 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1609 struct stlink_usb_handle_s
*h
= handle
;
1611 assert(handle
!= NULL
);
1613 /* there is no swim read core id cmd */
1614 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1619 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1621 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1622 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1623 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1625 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1628 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1630 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1634 if (res
!= ERROR_OK
)
1637 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1639 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1644 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1646 struct stlink_usb_handle_s
*h
= handle
;
1649 assert(handle
!= NULL
);
1651 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1653 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1654 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1655 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1658 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1659 if (res
!= ERROR_OK
)
1662 *val
= le_to_h_u32(h
->databuf
+ 4);
1666 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1668 struct stlink_usb_handle_s
*h
= handle
;
1670 assert(handle
!= NULL
);
1672 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1674 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1675 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1676 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1678 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1679 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1681 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1684 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1688 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1690 struct stlink_usb_handle_s
*h
= handle
;
1692 assert(handle
!= NULL
);
1694 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1697 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1699 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1700 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1702 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1703 if (res
!= ERROR_OK
)
1706 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1707 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1710 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1711 if (res
!= ERROR_OK
)
1720 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1725 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1726 if (result
!= ERROR_OK
)
1727 return TARGET_UNKNOWN
;
1729 if (status
& S_HALT
)
1730 return TARGET_HALTED
;
1731 else if (status
& S_RESET_ST
)
1732 return TARGET_RESET
;
1734 return TARGET_RUNNING
;
1738 static enum target_state
stlink_usb_state(void *handle
)
1741 struct stlink_usb_handle_s
*h
= handle
;
1743 assert(handle
!= NULL
);
1745 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1746 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1747 if (res
!= ERROR_OK
)
1748 return TARGET_UNKNOWN
;
1750 res
= stlink_swim_resync(handle
);
1751 if (res
!= ERROR_OK
)
1752 return TARGET_UNKNOWN
;
1757 if (h
->reconnect_pending
) {
1758 LOG_INFO("Previous state query failed, trying to reconnect");
1759 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1761 if (res
!= ERROR_OK
)
1762 return TARGET_UNKNOWN
;
1764 h
->reconnect_pending
= false;
1767 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1768 res
= stlink_usb_v2_get_status(handle
);
1769 if (res
== TARGET_UNKNOWN
)
1770 h
->reconnect_pending
= true;
1774 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1776 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1777 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1779 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1781 if (res
!= ERROR_OK
)
1782 return TARGET_UNKNOWN
;
1784 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1785 return TARGET_RUNNING
;
1786 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1787 return TARGET_HALTED
;
1789 h
->reconnect_pending
= true;
1791 return TARGET_UNKNOWN
;
1794 static int stlink_usb_assert_srst(void *handle
, int srst
)
1796 struct stlink_usb_handle_s
*h
= handle
;
1798 assert(handle
!= NULL
);
1800 if (h
->transport
== HL_TRANSPORT_SWIM
)
1801 return stlink_swim_assert_reset(handle
, srst
);
1803 if (h
->version
.stlink
== 1)
1804 return ERROR_COMMAND_NOTFOUND
;
1806 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1808 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1809 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1810 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1812 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1816 static void stlink_usb_trace_disable(void *handle
)
1819 struct stlink_usb_handle_s
*h
= handle
;
1821 assert(handle
!= NULL
);
1823 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1825 LOG_DEBUG("Tracing: disable");
1827 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1828 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1830 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1832 if (res
== ERROR_OK
)
1833 h
->trace
.enabled
= false;
1838 static int stlink_usb_trace_enable(void *handle
)
1841 struct stlink_usb_handle_s
*h
= handle
;
1843 assert(handle
!= NULL
);
1845 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1846 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1848 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1849 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1850 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1852 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1855 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1857 if (res
== ERROR_OK
) {
1858 h
->trace
.enabled
= true;
1859 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1862 LOG_ERROR("Tracing is not supported by this version.");
1870 static int stlink_usb_reset(void *handle
)
1872 struct stlink_usb_handle_s
*h
= handle
;
1875 assert(handle
!= NULL
);
1877 if (h
->transport
== HL_TRANSPORT_SWIM
)
1878 return stlink_swim_generate_rst(handle
);
1880 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1882 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1884 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1885 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1887 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1889 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1890 if (retval
!= ERROR_OK
)
1893 if (h
->trace
.enabled
) {
1894 stlink_usb_trace_disable(h
);
1895 return stlink_usb_trace_enable(h
);
1902 static int stlink_usb_run(void *handle
)
1905 struct stlink_usb_handle_s
*h
= handle
;
1907 assert(handle
!= NULL
);
1909 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1910 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1915 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1917 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1918 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1920 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1924 static int stlink_usb_halt(void *handle
)
1927 struct stlink_usb_handle_s
*h
= handle
;
1929 assert(handle
!= NULL
);
1931 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1932 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1937 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1939 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1940 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1942 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1946 static int stlink_usb_step(void *handle
)
1948 struct stlink_usb_handle_s
*h
= handle
;
1950 assert(handle
!= NULL
);
1952 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1953 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1954 * that the Cortex-M3 currently does. */
1955 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1956 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1957 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1960 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1962 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1963 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1965 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1969 static int stlink_usb_read_regs(void *handle
)
1972 struct stlink_usb_handle_s
*h
= handle
;
1974 assert(handle
!= NULL
);
1976 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1978 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1979 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1981 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1982 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
1983 /* regs data from offset 0 */
1985 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1986 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
1987 /* status at offset 0, regs data from offset 4 */
1994 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1997 struct stlink_usb_handle_s
*h
= handle
;
1999 assert(handle
!= NULL
);
2001 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2003 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2004 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2005 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2007 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2008 h
->cmdbuf
[h
->cmdidx
++] = num
;
2010 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2011 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2012 if (res
!= ERROR_OK
)
2014 *val
= le_to_h_u32(h
->databuf
);
2017 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2018 if (res
!= ERROR_OK
)
2020 *val
= le_to_h_u32(h
->databuf
+ 4);
2026 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
2028 struct stlink_usb_handle_s
*h
= handle
;
2030 assert(handle
!= NULL
);
2032 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2034 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2035 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2036 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2038 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2039 h
->cmdbuf
[h
->cmdidx
++] = num
;
2040 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2043 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2046 static int stlink_usb_get_rw_status(void *handle
)
2048 struct stlink_usb_handle_s
*h
= handle
;
2050 assert(handle
!= NULL
);
2052 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2055 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2057 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2058 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2059 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2060 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2062 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2063 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2068 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2072 uint16_t read_len
= len
;
2073 struct stlink_usb_handle_s
*h
= handle
;
2075 assert(handle
!= NULL
);
2077 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2078 if (len
> stlink_usb_block(h
)) {
2079 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2083 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2085 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2086 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2087 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2089 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2092 /* we need to fix read length for single bytes */
2096 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2098 if (res
!= ERROR_OK
)
2101 memcpy(buffer
, h
->databuf
, len
);
2103 return stlink_usb_get_rw_status(handle
);
2107 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2108 const uint8_t *buffer
)
2111 struct stlink_usb_handle_s
*h
= handle
;
2113 assert(handle
!= NULL
);
2115 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2116 if (len
> stlink_usb_block(h
)) {
2117 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2121 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2123 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2124 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2125 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2127 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2130 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2132 if (res
!= ERROR_OK
)
2135 return stlink_usb_get_rw_status(handle
);
2139 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2143 struct stlink_usb_handle_s
*h
= handle
;
2145 assert(handle
!= NULL
);
2147 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2148 return ERROR_COMMAND_NOTFOUND
;
2150 /* data must be a multiple of 2 and half-word aligned */
2151 if (len
% 2 || addr
% 2) {
2152 LOG_DEBUG("Invalid data alignment");
2153 return ERROR_TARGET_UNALIGNED_ACCESS
;
2156 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2158 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2159 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2160 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2162 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2165 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2167 if (res
!= ERROR_OK
)
2170 memcpy(buffer
, h
->databuf
, len
);
2172 return stlink_usb_get_rw_status(handle
);
2176 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2177 const uint8_t *buffer
)
2180 struct stlink_usb_handle_s
*h
= handle
;
2182 assert(handle
!= NULL
);
2184 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2185 return ERROR_COMMAND_NOTFOUND
;
2187 /* data must be a multiple of 2 and half-word aligned */
2188 if (len
% 2 || addr
% 2) {
2189 LOG_DEBUG("Invalid data alignment");
2190 return ERROR_TARGET_UNALIGNED_ACCESS
;
2193 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2195 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2196 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2197 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2199 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2202 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2204 if (res
!= ERROR_OK
)
2207 return stlink_usb_get_rw_status(handle
);
2211 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2215 struct stlink_usb_handle_s
*h
= handle
;
2217 assert(handle
!= NULL
);
2219 /* data must be a multiple of 4 and word aligned */
2220 if (len
% 4 || addr
% 4) {
2221 LOG_DEBUG("Invalid data alignment");
2222 return ERROR_TARGET_UNALIGNED_ACCESS
;
2225 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2227 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2228 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2229 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2231 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2234 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2236 if (res
!= ERROR_OK
)
2239 memcpy(buffer
, h
->databuf
, len
);
2241 return stlink_usb_get_rw_status(handle
);
2245 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2246 const uint8_t *buffer
)
2249 struct stlink_usb_handle_s
*h
= handle
;
2251 assert(handle
!= NULL
);
2253 /* data must be a multiple of 4 and word aligned */
2254 if (len
% 4 || addr
% 4) {
2255 LOG_DEBUG("Invalid data alignment");
2256 return ERROR_TARGET_UNALIGNED_ACCESS
;
2259 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2261 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2262 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2263 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2265 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2268 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2270 if (res
!= ERROR_OK
)
2273 return stlink_usb_get_rw_status(handle
);
2276 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2278 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2279 if (max_tar_block
== 0)
2281 return max_tar_block
;
2284 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2285 uint32_t count
, uint8_t *buffer
)
2287 int retval
= ERROR_OK
;
2288 uint32_t bytes_remaining
;
2290 struct stlink_usb_handle_s
*h
= handle
;
2292 /* calculate byte count */
2295 /* switch to 8 bit if stlink does not support 16 bit memory read */
2296 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2301 bytes_remaining
= (size
!= 1) ? \
2302 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2304 if (count
< bytes_remaining
)
2305 bytes_remaining
= count
;
2307 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2308 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
2309 if (retval
!= ERROR_OK
)
2313 * all stlink support 8/32bit memory read/writes and only from
2314 * stlink V2J26 there is support for 16 bit memory read/write.
2315 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2320 /* When in jtag mode the stlink uses the auto-increment functionality.
2321 * However it expects us to pass the data correctly, this includes
2322 * alignment and any page boundaries. We already do this as part of the
2323 * adi_v5 implementation, but the stlink is a hla adapter and so this
2324 * needs implementing manually.
2325 * currently this only affects jtag mode, according to ST they do single
2326 * access in SWD mode - but this may change and so we do it for both modes */
2328 /* we first need to check for any unaligned bytes */
2329 if (addr
& (size
- 1)) {
2331 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2332 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2333 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2334 usleep((1<<retries
++) * 1000);
2337 if (retval
!= ERROR_OK
)
2339 buffer
+= head_bytes
;
2341 count
-= head_bytes
;
2342 bytes_remaining
-= head_bytes
;
2345 if (bytes_remaining
& (size
- 1))
2346 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2348 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2350 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2352 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2354 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2355 usleep((1<<retries
++) * 1000);
2358 if (retval
!= ERROR_OK
)
2361 buffer
+= bytes_remaining
;
2362 addr
+= bytes_remaining
;
2363 count
-= bytes_remaining
;
2369 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2370 uint32_t count
, const uint8_t *buffer
)
2372 int retval
= ERROR_OK
;
2373 uint32_t bytes_remaining
;
2375 struct stlink_usb_handle_s
*h
= handle
;
2377 /* calculate byte count */
2380 /* switch to 8 bit if stlink does not support 16 bit memory read */
2381 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2386 bytes_remaining
= (size
!= 1) ? \
2387 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2389 if (count
< bytes_remaining
)
2390 bytes_remaining
= count
;
2392 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2393 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
2394 if (retval
!= ERROR_OK
)
2398 * all stlink support 8/32bit memory read/writes and only from
2399 * stlink V2J26 there is support for 16 bit memory read/write.
2400 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2405 /* When in jtag mode the stlink uses the auto-increment functionality.
2406 * However it expects us to pass the data correctly, this includes
2407 * alignment and any page boundaries. We already do this as part of the
2408 * adi_v5 implementation, but the stlink is a hla adapter and so this
2409 * needs implementing manually.
2410 * currently this only affects jtag mode, according to ST they do single
2411 * access in SWD mode - but this may change and so we do it for both modes */
2413 /* we first need to check for any unaligned bytes */
2414 if (addr
& (size
- 1)) {
2416 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2417 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2418 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2419 usleep((1<<retries
++) * 1000);
2422 if (retval
!= ERROR_OK
)
2424 buffer
+= head_bytes
;
2426 count
-= head_bytes
;
2427 bytes_remaining
-= head_bytes
;
2430 if (bytes_remaining
& (size
- 1))
2431 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2433 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2435 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2438 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2439 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2440 usleep((1<<retries
++) * 1000);
2443 if (retval
!= ERROR_OK
)
2446 buffer
+= bytes_remaining
;
2447 addr
+= bytes_remaining
;
2448 count
-= bytes_remaining
;
2455 static int stlink_usb_override_target(const char *targetname
)
2457 return !strcmp(targetname
, "cortex_m");
2460 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2463 we dont care what the khz rate is
2464 we only have low and high speed...
2465 before changing speed the SWIM_CSR HS bit
2469 stlink_swim_speed(handle
, 0);
2471 stlink_swim_speed(handle
, 1);
2475 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2478 int speed_index
= -1;
2479 int speed_diff
= INT_MAX
;
2480 int last_valid_speed
= -1;
2483 for (i
= 0; i
< map_size
; i
++) {
2486 last_valid_speed
= i
;
2487 if (khz
== map
[i
].speed
) {
2491 int current_diff
= khz
- map
[i
].speed
;
2492 /* get abs value for comparison */
2493 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2494 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2495 speed_diff
= current_diff
;
2501 if (speed_index
== -1) {
2502 /* this will only be here if we cannot match the slow speed.
2503 * use the slowest speed we support.*/
2504 speed_index
= last_valid_speed
;
2506 } else if (i
== map_size
)
2509 if (!match
&& query
) {
2510 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2511 khz
, map
[speed_index
].speed
);
2517 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2520 struct stlink_usb_handle_s
*h
= handle
;
2522 /* old firmware cannot change it */
2523 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2526 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2527 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2530 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2531 if (result
!= ERROR_OK
) {
2532 LOG_ERROR("Unable to set adapter speed");
2537 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2540 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2543 struct stlink_usb_handle_s
*h
= handle
;
2545 /* old firmware cannot change it */
2546 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2549 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2550 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2553 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2554 if (result
!= ERROR_OK
) {
2555 LOG_ERROR("Unable to set adapter speed");
2560 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2563 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2567 LOG_DEBUG("Supported clock speeds are:");
2568 for (i
= 0; i
< map_size
; i
++)
2570 LOG_DEBUG("%d kHz", map
[i
].speed
);
2573 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2575 struct stlink_usb_handle_s
*h
= handle
;
2578 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2579 LOG_ERROR("Unknown command");
2583 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2585 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2586 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2587 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2589 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2591 int size
= h
->databuf
[8];
2593 if (size
> STLINK_V3_MAX_FREQ_NB
)
2594 size
= STLINK_V3_MAX_FREQ_NB
;
2596 for (i
= 0; i
< size
; i
++) {
2597 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2598 map
[i
].speed_divisor
= i
;
2601 /* set to zero all the next entries */
2602 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2608 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2610 struct stlink_usb_handle_s
*h
= handle
;
2612 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2613 LOG_ERROR("Unknown command");
2617 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2619 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2620 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2621 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2622 h
->cmdbuf
[h
->cmdidx
++] = 0;
2624 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2626 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2629 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2631 struct stlink_usb_handle_s
*h
= handle
;
2633 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2635 stlink_get_com_freq(h
, is_jtag
, map
);
2637 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2640 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2641 if (result
!= ERROR_OK
) {
2642 LOG_ERROR("Unable to set adapter speed");
2646 return map
[speed_index
].speed
;
2649 static int stlink_speed(void *handle
, int khz
, bool query
)
2651 struct stlink_usb_handle_s
*h
= handle
;
2656 switch (h
->transport
) {
2657 case HL_TRANSPORT_SWIM
:
2658 return stlink_speed_swim(handle
, khz
, query
);
2660 case HL_TRANSPORT_SWD
:
2661 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2662 return stlink_speed_v3(handle
, false, khz
, query
);
2664 return stlink_speed_swd(handle
, khz
, query
);
2666 case HL_TRANSPORT_JTAG
:
2667 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2668 return stlink_speed_v3(handle
, true, khz
, query
);
2670 return stlink_speed_jtag(handle
, khz
, query
);
2680 static int stlink_usb_close(void *handle
)
2684 enum stlink_mode emode
;
2685 struct stlink_usb_handle_s
*h
= handle
;
2688 res
= stlink_usb_current_mode(handle
, &mode
);
2691 /* do not exit if return code != ERROR_OK,
2692 it prevents us from closing jtag_libusb */
2694 if (res
== ERROR_OK
) {
2695 /* try to exit current mode */
2697 case STLINK_DEV_DFU_MODE
:
2698 emode
= STLINK_MODE_DFU
;
2700 case STLINK_DEV_DEBUG_MODE
:
2701 emode
= STLINK_MODE_DEBUG_SWD
;
2703 case STLINK_DEV_SWIM_MODE
:
2704 emode
= STLINK_MODE_DEBUG_SWIM
;
2706 case STLINK_DEV_BOOTLOADER_MODE
:
2707 case STLINK_DEV_MASS_MODE
:
2709 emode
= STLINK_MODE_UNKNOWN
;
2713 if (emode
!= STLINK_MODE_UNKNOWN
)
2714 stlink_usb_mode_leave(handle
, emode
);
2715 /* do not check return code, it prevent
2716 us from closing jtag_libusb */
2720 jtag_libusb_close(h
->fd
);
2728 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2730 int err
, retry_count
= 1;
2731 struct stlink_usb_handle_s
*h
;
2733 LOG_DEBUG("stlink_usb_open");
2735 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2738 LOG_DEBUG("malloc failed");
2742 h
->transport
= param
->transport
;
2744 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2745 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2746 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2747 param
->serial
? param
->serial
: "");
2751 On certain host USB configurations(e.g. MacBook Air)
2752 STLINKv2 dongle seems to have its FW in a funky state if,
2753 after plugging it in, you try to use openocd with it more
2754 then once (by launching and closing openocd). In cases like
2755 that initial attempt to read the FW info via
2756 stlink_usb_version will fail and the device has to be reset
2757 in order to become operational.
2760 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2761 LOG_ERROR("open failed");
2765 jtag_libusb_set_configuration(h
->fd
, 0);
2767 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2768 LOG_DEBUG("claim interface failed");
2772 /* RX EP is common for all versions */
2773 h
->rx_ep
= STLINK_RX_EP
;
2776 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2777 LOG_DEBUG("libusb_get_pid failed");
2781 /* wrap version for first read */
2784 h
->version
.stlink
= 1;
2785 h
->tx_ep
= STLINK_TX_EP
;
2787 case STLINK_V3_USBLOADER_PID
:
2788 case STLINK_V3E_PID
:
2789 case STLINK_V3S_PID
:
2790 case STLINK_V3_2VCP_PID
:
2791 h
->version
.stlink
= 3;
2792 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2793 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2795 case STLINK_V2_1_PID
:
2796 case STLINK_V2_1_NO_MSD_PID
:
2797 h
->version
.stlink
= 2;
2798 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2799 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2802 /* fall through - we assume V2 to be the default version*/
2804 h
->version
.stlink
= 2;
2805 h
->tx_ep
= STLINK_TX_EP
;
2806 h
->trace_ep
= STLINK_TRACE_EP
;
2810 /* get the device version */
2811 err
= stlink_usb_version(h
);
2813 if (err
== ERROR_OK
) {
2815 } else if (h
->version
.stlink
== 1 ||
2817 LOG_ERROR("read version failed");
2820 err
= jtag_libusb_release_interface(h
->fd
, 0);
2821 if (err
!= ERROR_OK
) {
2822 LOG_ERROR("release interface failed");
2826 err
= jtag_libusb_reset_device(h
->fd
);
2827 if (err
!= ERROR_OK
) {
2828 LOG_ERROR("reset device failed");
2832 jtag_libusb_close(h
->fd
);
2834 Give the device one second to settle down and
2837 usleep(1 * 1000 * 1000);
2842 /* check if mode is supported */
2845 switch (h
->transport
) {
2846 case HL_TRANSPORT_SWD
:
2847 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2850 case HL_TRANSPORT_JTAG
:
2851 if (h
->version
.jtag
== 0)
2854 case HL_TRANSPORT_SWIM
:
2855 if (h
->version
.swim
== 0)
2863 if (err
!= ERROR_OK
) {
2864 LOG_ERROR("mode (transport) not supported by device");
2868 /* initialize the debug hardware */
2869 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
2871 if (err
!= ERROR_OK
) {
2872 LOG_ERROR("init mode failed (unable to connect to the target)");
2876 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2877 err
= stlink_swim_enter(h
);
2878 if (err
!= ERROR_OK
) {
2879 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2883 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2887 /* get cpuid, so we can determine the max page size
2888 * start with a safe default */
2889 h
->max_mem_packet
= (1 << 10);
2892 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2893 if (err
== ERROR_OK
) {
2894 uint32_t cpuid
= le_to_h_u32(buffer
);
2895 int i
= (cpuid
>> 4) & 0xf;
2896 if (i
== 4 || i
== 3) {
2897 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2898 h
->max_mem_packet
= (1 << 12);
2902 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2909 stlink_usb_close(h
);
2914 int stlink_config_trace(void *handle
, bool enabled
,
2915 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
2916 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
2917 uint16_t *prescaler
)
2919 struct stlink_usb_handle_s
*h
= handle
;
2922 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2923 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2924 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2929 stlink_usb_trace_disable(h
);
2933 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2934 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2935 STLINK_TRACE_MAX_HZ
);
2939 stlink_usb_trace_disable(h
);
2942 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2944 presc
= traceclkin_freq
/ *trace_freq
;
2946 if (traceclkin_freq
% *trace_freq
> 0)
2949 if (presc
> TPIU_ACPR_MAX_SWOSCALER
) {
2950 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
2956 h
->trace
.source_hz
= *trace_freq
;
2958 return stlink_usb_trace_enable(h
);
2962 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
2964 struct stlink_usb_handle_s
*h
= handle
;
2966 assert(handle
!= NULL
);
2968 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
2969 return ERROR_COMMAND_NOTFOUND
;
2971 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
2972 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2973 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2974 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
2975 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2977 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2981 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
2983 struct stlink_usb_handle_s
*h
= handle
;
2985 assert(handle
!= NULL
);
2987 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
2988 return ERROR_COMMAND_NOTFOUND
;
2990 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
2991 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2992 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2993 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG
;
2994 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2996 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3000 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3001 unsigned short addr
, uint32_t *val
)
3003 struct stlink_usb_handle_s
*h
= handle
;
3006 assert(handle
!= NULL
);
3008 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3009 return ERROR_COMMAND_NOTFOUND
;
3011 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3012 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3013 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3014 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3015 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3017 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3018 *val
= le_to_h_u32(h
->databuf
+ 4);
3019 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, *val
);
3024 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3025 unsigned short addr
, uint32_t val
)
3027 struct stlink_usb_handle_s
*h
= handle
;
3029 assert(handle
!= NULL
);
3031 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3032 return ERROR_COMMAND_NOTFOUND
;
3034 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, val
);
3035 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3036 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3037 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3038 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3039 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3040 h_u32_to_le(&h
->cmdbuf
[6], val
);
3041 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3045 struct hl_layout_api_s stlink_usb_layout_api
= {
3047 .open
= stlink_usb_open
,
3049 .close
= stlink_usb_close
,
3051 .idcode
= stlink_usb_idcode
,
3053 .state
= stlink_usb_state
,
3055 .reset
= stlink_usb_reset
,
3057 .assert_srst
= stlink_usb_assert_srst
,
3059 .run
= stlink_usb_run
,
3061 .halt
= stlink_usb_halt
,
3063 .step
= stlink_usb_step
,
3065 .read_regs
= stlink_usb_read_regs
,
3067 .read_reg
= stlink_usb_read_reg
,
3069 .write_reg
= stlink_usb_write_reg
,
3071 .read_mem
= stlink_usb_read_mem
,
3073 .write_mem
= stlink_usb_write_mem
,
3075 .write_debug_reg
= stlink_usb_write_debug_reg
,
3077 .override_target
= stlink_usb_override_target
,
3079 .speed
= stlink_speed
,
3081 .config_trace
= stlink_config_trace
,
3083 .poll_trace
= stlink_usb_trace_read
,
3086 /*****************************************************************************
3087 * DAP direct interface
3090 static struct stlink_usb_handle_s
*stlink_dap_handle
;
3091 static struct hl_interface_param_s stlink_dap_param
;
3092 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
3093 static int stlink_dap_error
= ERROR_OK
;
3095 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3099 static int stlink_dap_record_error(int error
)
3101 if (stlink_dap_error
== ERROR_OK
)
3102 stlink_dap_error
= error
;
3107 static int stlink_dap_get_and_clear_error(void)
3109 int retval
= stlink_dap_error
;
3110 stlink_dap_error
= ERROR_OK
;
3115 static int stlink_dap_open_ap(unsigned short apsel
)
3119 /* nothing to do on old versions */
3120 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3123 if (apsel
> DP_APSEL_MAX
)
3126 if (test_bit(apsel
, opened_ap
))
3129 retval
= stlink_usb_init_access_port(stlink_dap_handle
, apsel
);
3130 if (retval
!= ERROR_OK
)
3133 LOG_DEBUG("AP %d enabled", apsel
);
3134 set_bit(apsel
, opened_ap
);
3139 static int stlink_dap_closeall_ap(void)
3143 /* nothing to do on old versions */
3144 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3147 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
3148 if (!test_bit(apsel
, opened_ap
))
3150 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
3151 if (retval
!= ERROR_OK
)
3153 clear_bit(apsel
, opened_ap
);
3159 static int stlink_dap_reinit_interface(void)
3162 enum stlink_mode mode
;
3165 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
3166 * some firmware version it does not work as expected, and there is no
3167 * equivalent for SWD.
3168 * At least for now, to reset the interface quit from JTAG/SWD mode then
3169 * select the mode again.
3172 mode
= stlink_get_mode(stlink_dap_param
.transport
);
3173 if (!stlink_dap_handle
->reconnect_pending
) {
3174 stlink_dap_handle
->reconnect_pending
= true;
3175 stlink_usb_mode_leave(stlink_dap_handle
, mode
);
3178 retval
= stlink_usb_mode_enter(stlink_dap_handle
, mode
);
3179 if (retval
!= ERROR_OK
)
3182 stlink_dap_handle
->reconnect_pending
= false;
3183 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
3184 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
3185 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
3186 if (test_bit(apsel
, opened_ap
)) {
3187 clear_bit(apsel
, opened_ap
);
3188 stlink_dap_open_ap(apsel
);
3194 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
3199 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
3201 /* Check if we should reset srst already when connecting, but not if reconnecting. */
3202 if (!dap
->do_reconnect
) {
3203 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3205 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3206 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3207 adapter_assert_reset();
3209 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3213 dap
->do_reconnect
= false;
3214 dap_invalidate_cache(dap
);
3216 retval
= dap_dp_init(dap
);
3217 if (retval
!= ERROR_OK
) {
3218 dap
->do_reconnect
= true;
3222 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
3223 if (retval
== ERROR_OK
)
3224 LOG_INFO("%s %#8.8" PRIx32
,
3225 (stlink_dap_handle
->transport
== HL_TRANSPORT_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
3228 dap
->do_reconnect
= true;
3234 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
3238 if (!dap
->do_reconnect
)
3241 retval
= stlink_dap_reinit_interface();
3242 if (retval
!= ERROR_OK
)
3245 return stlink_dap_op_connect(dap
);
3249 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
3251 /* Ignore the request */
3256 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3262 retval
= stlink_dap_check_reconnect(dap
);
3263 if (retval
!= ERROR_OK
)
3266 data
= data
? : &dummy
;
3267 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
3268 && stlink_dap_handle
->transport
== HL_TRANSPORT_JTAG
) {
3269 /* Quirk required in JTAG. Read RDBUFF to get the data */
3270 retval
= stlink_read_dap_register(stlink_dap_handle
,
3271 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
3272 if (retval
== ERROR_OK
)
3273 retval
= stlink_read_dap_register(stlink_dap_handle
,
3274 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
3276 retval
= stlink_read_dap_register(stlink_dap_handle
,
3277 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3280 return stlink_dap_record_error(retval
);
3284 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned reg
,
3289 retval
= stlink_dap_check_reconnect(dap
);
3290 if (retval
!= ERROR_OK
)
3293 /* ST-Link does not like that we set CORUNDETECT */
3294 if (reg
== DP_CTRL_STAT
)
3295 data
&= ~CORUNDETECT
;
3297 retval
= stlink_write_dap_register(stlink_dap_handle
,
3298 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3299 return stlink_dap_record_error(retval
);
3303 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned reg
,
3306 struct adiv5_dap
*dap
= ap
->dap
;
3310 retval
= stlink_dap_check_reconnect(dap
);
3311 if (retval
!= ERROR_OK
)
3314 if (reg
!= AP_REG_IDR
) {
3315 retval
= stlink_dap_open_ap(ap
->ap_num
);
3316 if (retval
!= ERROR_OK
)
3319 data
= data
? : &dummy
;
3320 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3322 dap
->stlink_flush_ap_write
= false;
3323 return stlink_dap_record_error(retval
);
3327 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned reg
,
3330 struct adiv5_dap
*dap
= ap
->dap
;
3333 retval
= stlink_dap_check_reconnect(dap
);
3334 if (retval
!= ERROR_OK
)
3337 retval
= stlink_dap_open_ap(ap
->ap_num
);
3338 if (retval
!= ERROR_OK
)
3341 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3343 dap
->stlink_flush_ap_write
= true;
3344 return stlink_dap_record_error(retval
);
3348 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
3350 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
3355 static int stlink_dap_op_run(struct adiv5_dap
*dap
)
3357 uint32_t ctrlstat
, pwrmask
;
3358 int retval
, saved_retval
;
3360 /* Here no LOG_DEBUG. This is called continuously! */
3363 * ST-Link returns immediately after a DAP write, without waiting for it
3365 * Run a dummy read to DP_RDBUFF, as suggested in
3366 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
3368 if (dap
->stlink_flush_ap_write
) {
3369 dap
->stlink_flush_ap_write
= false;
3370 retval
= stlink_dap_op_queue_dp_read(dap
, DP_RDBUFF
, NULL
);
3371 if (retval
!= ERROR_OK
) {
3372 dap
->do_reconnect
= true;
3377 saved_retval
= stlink_dap_get_and_clear_error();
3379 retval
= stlink_dap_op_queue_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
3380 if (retval
!= ERROR_OK
) {
3381 dap
->do_reconnect
= true;
3384 retval
= stlink_dap_get_and_clear_error();
3385 if (retval
!= ERROR_OK
) {
3386 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
3387 dap
->do_reconnect
= true;
3391 if (ctrlstat
& SSTICKYERR
) {
3392 if (stlink_dap_param
.transport
== HL_TRANSPORT_JTAG
)
3393 retval
= stlink_dap_op_queue_dp_write(dap
, DP_CTRL_STAT
,
3394 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
3396 retval
= stlink_dap_op_queue_dp_write(dap
, DP_ABORT
, STKERRCLR
);
3397 if (retval
!= ERROR_OK
) {
3398 dap
->do_reconnect
= true;
3401 retval
= stlink_dap_get_and_clear_error();
3402 if (retval
!= ERROR_OK
) {
3403 dap
->do_reconnect
= true;
3408 /* check for power lost */
3409 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
3410 if ((ctrlstat
& pwrmask
) != pwrmask
)
3411 dap
->do_reconnect
= true;
3413 return saved_retval
;
3417 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
3421 retval
= stlink_dap_closeall_ap();
3422 if (retval
!= ERROR_OK
)
3423 LOG_ERROR("Error closing APs");
3427 COMMAND_HANDLER(stlink_dap_serial_command
)
3429 LOG_DEBUG("stlink_dap_serial_command");
3431 if (CMD_ARGC
!= 1) {
3432 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
3433 return ERROR_COMMAND_SYNTAX_ERROR
;
3436 if (stlink_dap_param
.serial
) {
3437 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
3438 free((void *)stlink_dap_param
.serial
);
3441 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
3446 COMMAND_HANDLER(stlink_dap_vid_pid
)
3448 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
3450 if (CMD_ARGC
> max_usb_ids
* 2) {
3451 LOG_WARNING("ignoring extra IDs in vid_pid "
3452 "(maximum is %d pairs)", max_usb_ids
);
3453 CMD_ARGC
= max_usb_ids
* 2;
3455 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3456 LOG_WARNING("incomplete vid_pid configuration directive");
3457 return ERROR_COMMAND_SYNTAX_ERROR
;
3459 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3460 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
3461 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
3464 /* null termination */
3465 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
3471 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
3474 .handler
= stlink_dap_serial_command
,
3475 .mode
= COMMAND_CONFIG
,
3476 .help
= "set the serial number of the adapter",
3477 .usage
= "<serial_number>",
3481 .handler
= stlink_dap_vid_pid
,
3482 .mode
= COMMAND_CONFIG
,
3483 .help
= "USB VID and PID of the adapter",
3484 .usage
= "(vid pid)+",
3486 COMMAND_REGISTRATION_DONE
3490 static const struct command_registration stlink_dap_command_handlers
[] = {
3493 .mode
= COMMAND_ANY
,
3494 .help
= "perform st-link management",
3495 .chain
= stlink_dap_subcommand_handlers
,
3498 COMMAND_REGISTRATION_DONE
3502 static int stlink_dap_init(void)
3504 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3507 LOG_DEBUG("stlink_dap_init()");
3509 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3510 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3511 stlink_dap_param
.connect_under_reset
= true;
3513 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3516 if (transport_is_dapdirect_swd())
3517 stlink_dap_param
.transport
= HL_TRANSPORT_SWD
;
3518 else if (transport_is_dapdirect_jtag())
3519 stlink_dap_param
.transport
= HL_TRANSPORT_JTAG
;
3521 LOG_ERROR("Unsupported transport");
3525 retval
= stlink_usb_open(&stlink_dap_param
, (void **)&stlink_dap_handle
);
3526 if (retval
!= ERROR_OK
)
3529 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
3530 LOG_ERROR("ST-Link version does not support DAP direct transport");
3537 static int stlink_dap_quit(void)
3539 LOG_DEBUG("stlink_dap_quit()");
3541 free((void *)stlink_dap_param
.serial
);
3542 stlink_dap_param
.serial
= NULL
;
3544 return stlink_usb_close(stlink_dap_handle
);
3548 static int stlink_dap_reset(int req_trst
, int req_srst
)
3550 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
3551 return stlink_usb_assert_srst(stlink_dap_handle
,
3552 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
3553 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
3557 static int stlink_dap_speed(int speed
)
3560 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
3561 return ERROR_JTAG_NOT_IMPLEMENTED
;
3564 stlink_dap_param
.initial_interface_speed
= speed
;
3565 stlink_speed(stlink_dap_handle
, speed
, false);
3570 static int stlink_dap_khz(int khz
, int *jtag_speed
)
3577 static int stlink_dap_speed_div(int speed
, int *khz
)
3583 static const struct dap_ops stlink_dap_ops
= {
3584 .connect
= stlink_dap_op_connect
,
3585 .send_sequence
= stlink_dap_op_send_sequence
,
3586 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
3587 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
3588 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
3589 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
3590 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
3591 .run
= stlink_dap_op_run
,
3592 .sync
= NULL
, /* optional */
3593 .quit
= stlink_dap_op_quit
, /* optional */
3596 static const char *const stlink_dap_transport
[] = { "dapdirect_jtag", "dapdirect_swd", NULL
};
3598 struct adapter_driver stlink_dap_adapter_driver
= {
3600 .transports
= stlink_dap_transport
,
3601 .commands
= stlink_dap_command_handlers
,
3603 .init
= stlink_dap_init
,
3604 .quit
= stlink_dap_quit
,
3605 .reset
= stlink_dap_reset
,
3606 .speed
= stlink_dap_speed
,
3607 .khz
= stlink_dap_khz
,
3608 .speed_div
= stlink_dap_speed_div
,
3610 .dap_jtag_ops
= &stlink_dap_ops
,
3611 .dap_swd_ops
= &stlink_dap_ops
,