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 <jtag/interface.h>
35 #include <jtag/hla/hla_layout.h>
36 #include <jtag/hla/hla_transport.h>
37 #include <jtag/hla/hla_interface.h>
38 #include <target/target.h>
40 #include <target/cortex_m.h>
42 #include "libusb_common.h"
45 #define USE_LIBUSB_ASYNCIO
48 #define ENDPOINT_IN 0x80
49 #define ENDPOINT_OUT 0x00
51 #define STLINK_WRITE_TIMEOUT 1000
52 #define STLINK_READ_TIMEOUT 1000
54 #define STLINK_NULL_EP 0
55 #define STLINK_RX_EP (1|ENDPOINT_IN)
56 #define STLINK_TX_EP (2|ENDPOINT_OUT)
57 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
59 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
60 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
62 #define STLINK_SG_SIZE (31)
63 #define STLINK_DATA_SIZE (4096)
64 #define STLINK_CMD_SIZE_V2 (16)
65 #define STLINK_CMD_SIZE_V1 (10)
67 #define STLINK_V1_PID (0x3744)
68 #define STLINK_V2_PID (0x3748)
69 #define STLINK_V2_1_PID (0x374B)
70 #define STLINK_V2_1_NO_MSD_PID (0x3752)
71 #define STLINK_V3_USBLOADER_PID (0x374D)
72 #define STLINK_V3E_PID (0x374E)
73 #define STLINK_V3S_PID (0x374F)
74 #define STLINK_V3_2VCP_PID (0x3753)
77 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
78 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
79 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes.
81 #define STLINK_MAX_RW8 (64)
82 #define STLINKV3_MAX_RW8 (512)
84 /* "WAIT" responses will be retried (with exponential backoff) at
85 * most this many times before failing to caller.
87 #define MAX_WAIT_RETRIES 8
89 enum stlink_jtag_api_version
{
90 STLINK_JTAG_API_V1
= 1,
96 struct stlink_usb_version
{
103 /** jtag api version supported */
104 enum stlink_jtag_api_version jtag_api
;
105 /** one bit for each feature supported. See macros STLINK_F_* */
110 struct stlink_usb_handle_s
{
112 struct jtag_libusb_device_handle
*fd
;
114 struct libusb_transfer
*trans
;
122 uint8_t cmdbuf
[STLINK_SG_SIZE
];
128 uint8_t databuf
[STLINK_DATA_SIZE
];
130 uint32_t max_mem_packet
;
132 enum hl_transports transport
;
134 struct stlink_usb_version version
;
141 /** whether SWO tracing is enabled or not */
143 /** trace module source clock */
146 /** reconnect is needed next time we try to query the
148 bool reconnect_pending
;
151 #define STLINK_SWIM_ERR_OK 0x00
152 #define STLINK_SWIM_BUSY 0x01
153 #define STLINK_DEBUG_ERR_OK 0x80
154 #define STLINK_DEBUG_ERR_FAULT 0x81
155 #define STLINK_SWD_AP_WAIT 0x10
156 #define STLINK_SWD_AP_FAULT 0x11
157 #define STLINK_SWD_AP_ERROR 0x12
158 #define STLINK_SWD_AP_PARITY_ERROR 0x13
159 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
160 #define STLINK_JTAG_WRITE_ERROR 0x0c
161 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
162 #define STLINK_SWD_DP_WAIT 0x14
163 #define STLINK_SWD_DP_FAULT 0x15
164 #define STLINK_SWD_DP_ERROR 0x16
165 #define STLINK_SWD_DP_PARITY_ERROR 0x17
167 #define STLINK_SWD_AP_WDATA_ERROR 0x18
168 #define STLINK_SWD_AP_STICKY_ERROR 0x19
169 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
171 #define STLINK_BAD_AP_ERROR 0x1d
173 #define STLINK_CORE_RUNNING 0x80
174 #define STLINK_CORE_HALTED 0x81
175 #define STLINK_CORE_STAT_UNKNOWN -1
177 #define STLINK_GET_VERSION 0xF1
178 #define STLINK_DEBUG_COMMAND 0xF2
179 #define STLINK_DFU_COMMAND 0xF3
180 #define STLINK_SWIM_COMMAND 0xF4
181 #define STLINK_GET_CURRENT_MODE 0xF5
182 #define STLINK_GET_TARGET_VOLTAGE 0xF7
184 #define STLINK_DEV_DFU_MODE 0x00
185 #define STLINK_DEV_MASS_MODE 0x01
186 #define STLINK_DEV_DEBUG_MODE 0x02
187 #define STLINK_DEV_SWIM_MODE 0x03
188 #define STLINK_DEV_BOOTLOADER_MODE 0x04
189 #define STLINK_DEV_UNKNOWN_MODE -1
191 #define STLINK_DFU_EXIT 0x07
194 STLINK_SWIM_ENTER_SEQ
195 1.3ms low then 750Hz then 1.5kHz
198 STM8 DM pulls reset pin low 50us
201 uint8_t (0=low|1=high)
208 send syncronization seq (16us low, response 64 clocks low)
210 #define STLINK_SWIM_ENTER 0x00
211 #define STLINK_SWIM_EXIT 0x01
212 #define STLINK_SWIM_READ_CAP 0x02
213 #define STLINK_SWIM_SPEED 0x03
214 #define STLINK_SWIM_ENTER_SEQ 0x04
215 #define STLINK_SWIM_GEN_RST 0x05
216 #define STLINK_SWIM_RESET 0x06
217 #define STLINK_SWIM_ASSERT_RESET 0x07
218 #define STLINK_SWIM_DEASSERT_RESET 0x08
219 #define STLINK_SWIM_READSTATUS 0x09
220 #define STLINK_SWIM_WRITEMEM 0x0a
221 #define STLINK_SWIM_READMEM 0x0b
222 #define STLINK_SWIM_READBUF 0x0c
224 #define STLINK_DEBUG_GETSTATUS 0x01
225 #define STLINK_DEBUG_FORCEDEBUG 0x02
226 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
227 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
228 #define STLINK_DEBUG_APIV1_READREG 0x05
229 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
230 #define STLINK_DEBUG_READMEM_32BIT 0x07
231 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
232 #define STLINK_DEBUG_RUNCORE 0x09
233 #define STLINK_DEBUG_STEPCORE 0x0a
234 #define STLINK_DEBUG_APIV1_SETFP 0x0b
235 #define STLINK_DEBUG_READMEM_8BIT 0x0c
236 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
237 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
238 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
239 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
241 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
242 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
243 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
245 #define STLINK_DEBUG_APIV1_ENTER 0x20
246 #define STLINK_DEBUG_EXIT 0x21
247 #define STLINK_DEBUG_READCOREID 0x22
249 #define STLINK_DEBUG_APIV2_ENTER 0x30
250 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
251 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
252 #define STLINK_DEBUG_APIV2_READREG 0x33
253 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
254 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
255 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
257 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
258 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
259 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
261 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
263 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
264 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
265 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
266 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
267 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
269 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
270 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
272 #define STLINK_APIV3_SET_COM_FREQ 0x61
273 #define STLINK_APIV3_GET_COM_FREQ 0x62
275 #define STLINK_APIV3_GET_VERSION_EX 0xFB
277 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
278 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
279 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
281 #define STLINK_TRACE_SIZE 4096
282 #define STLINK_TRACE_MAX_HZ 2000000
284 #define STLINK_V3_MAX_FREQ_NB 10
288 STLINK_MODE_UNKNOWN
= 0,
291 STLINK_MODE_DEBUG_JTAG
,
292 STLINK_MODE_DEBUG_SWD
,
293 STLINK_MODE_DEBUG_SWIM
296 #define REQUEST_SENSE 0x03
297 #define REQUEST_SENSE_LENGTH 18
300 * Map the relevant features, quirks and workaround for specific firmware
303 #define STLINK_F_HAS_TRACE (1UL << 0)
304 #define STLINK_F_HAS_SWD_SET_FREQ (1UL << 1)
305 #define STLINK_F_HAS_JTAG_SET_FREQ (1UL << 2)
306 #define STLINK_F_HAS_MEM_16BIT (1UL << 3)
307 #define STLINK_F_HAS_GETLASTRWSTATUS2 (1UL << 4)
310 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
317 /* SWD clock speed */
318 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
320 {1800, 1}, /* default */
333 /* JTAG clock speed */
334 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
339 {1125, 32}, /* default */
345 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
346 static int stlink_swim_status(void *handle
);
349 static unsigned int stlink_usb_block(void *handle
)
351 struct stlink_usb_handle_s
*h
= handle
;
353 assert(handle
!= NULL
);
355 if (h
->version
.stlink
== 3)
356 return STLINKV3_MAX_RW8
;
358 return STLINK_MAX_RW8
;
363 #ifdef USE_LIBUSB_ASYNCIO
365 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
367 int *completed
= transfer
->user_data
;
369 /* caller interprets result and frees transfer */
373 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
375 int r
, *completed
= transfer
->user_data
;
377 /* Assuming a single libusb context exists. There no existing interface into this
378 * module to pass a libusb context.
380 struct libusb_context
*ctx
= NULL
;
382 while (!*completed
) {
383 r
= libusb_handle_events_completed(ctx
, completed
);
385 if (r
== LIBUSB_ERROR_INTERRUPTED
)
387 libusb_cancel_transfer(transfer
);
394 static int transfer_error_status(const struct libusb_transfer
*transfer
)
398 switch (transfer
->status
) {
399 case LIBUSB_TRANSFER_COMPLETED
:
402 case LIBUSB_TRANSFER_TIMED_OUT
:
403 r
= LIBUSB_ERROR_TIMEOUT
;
405 case LIBUSB_TRANSFER_STALL
:
406 r
= LIBUSB_ERROR_PIPE
;
408 case LIBUSB_TRANSFER_OVERFLOW
:
409 r
= LIBUSB_ERROR_OVERFLOW
;
411 case LIBUSB_TRANSFER_NO_DEVICE
:
412 r
= LIBUSB_ERROR_NO_DEVICE
;
414 case LIBUSB_TRANSFER_ERROR
:
415 case LIBUSB_TRANSFER_CANCELLED
:
419 r
= LIBUSB_ERROR_OTHER
;
433 size_t transfer_size
;
434 struct libusb_transfer
*transfer
;
437 static int jtag_libusb_bulk_transfer_n(
438 jtag_libusb_device_handle
* dev_handle
,
439 struct jtag_xfer
*transfers
,
444 int returnval
= ERROR_OK
;
447 for (size_t i
= 0; i
< n_transfers
; ++i
) {
448 transfers
[i
].retval
= 0;
449 transfers
[i
].completed
= 0;
450 transfers
[i
].transfer_size
= 0;
451 transfers
[i
].transfer
= libusb_alloc_transfer(0);
453 if (transfers
[i
].transfer
== NULL
) {
454 for (size_t j
= 0; j
< i
; ++j
)
455 libusb_free_transfer(transfers
[j
].transfer
);
457 LOG_DEBUG("ERROR, failed to alloc usb transfers");
458 for (size_t k
= 0; k
< n_transfers
; ++k
)
459 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
464 for (size_t i
= 0; i
< n_transfers
; ++i
) {
465 libusb_fill_bulk_transfer(
466 transfers
[i
].transfer
,
468 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
469 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
470 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
472 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
474 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
476 /* Probably no point continuing to submit transfers once a submission fails.
477 * As a result, tag all remaining transfers as errors.
479 for (size_t j
= i
; j
< n_transfers
; ++j
)
480 transfers
[j
].retval
= retval
;
482 returnval
= ERROR_FAIL
;
487 /* Wait for every submitted USB transfer to complete.
489 for (size_t i
= 0; i
< n_transfers
; ++i
) {
490 if (transfers
[i
].retval
== 0) {
491 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
493 retval
= transfer_error_status(transfers
[i
].transfer
);
495 returnval
= ERROR_FAIL
;
496 transfers
[i
].retval
= retval
;
497 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
499 /* Assuming actual_length is only valid if there is no transfer error.
501 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
505 libusb_free_transfer(transfers
[i
].transfer
);
506 transfers
[i
].transfer
= NULL
;
516 static int stlink_usb_xfer_v1_get_status(void *handle
)
518 struct stlink_usb_handle_s
*h
= handle
;
520 assert(handle
!= NULL
);
523 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
525 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
526 13, STLINK_READ_TIMEOUT
) != 13)
531 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
534 if (t1
!= 0x53425355)
542 if (h
->cmdbuf
[12] != 0)
548 #ifdef USE_LIBUSB_ASYNCIO
549 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
551 struct stlink_usb_handle_s
*h
= handle
;
553 assert(handle
!= NULL
);
555 size_t n_transfers
= 0;
556 struct jtag_xfer transfers
[2];
558 memset(transfers
, 0, sizeof(transfers
));
560 transfers
[0].ep
= h
->tx_ep
;
561 transfers
[0].buf
= h
->cmdbuf
;
562 transfers
[0].size
= cmdsize
;
566 if (h
->direction
== h
->tx_ep
&& size
) {
567 transfers
[1].ep
= h
->tx_ep
;
568 transfers
[1].buf
= (uint8_t *)buf
;
569 transfers
[1].size
= size
;
572 } else if (h
->direction
== h
->rx_ep
&& size
) {
573 transfers
[1].ep
= h
->rx_ep
;
574 transfers
[1].buf
= (uint8_t *)buf
;
575 transfers
[1].size
= size
;
580 return jtag_libusb_bulk_transfer_n(
584 STLINK_WRITE_TIMEOUT
);
587 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
589 struct stlink_usb_handle_s
*h
= handle
;
591 assert(handle
!= NULL
);
593 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
594 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
598 if (h
->direction
== h
->tx_ep
&& size
) {
599 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
600 size
, STLINK_WRITE_TIMEOUT
) != size
) {
601 LOG_DEBUG("bulk write failed");
604 } else if (h
->direction
== h
->rx_ep
&& size
) {
605 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
606 size
, STLINK_READ_TIMEOUT
) != size
) {
607 LOG_DEBUG("bulk read failed");
617 static int stlink_usb_xfer_v1_get_sense(void *handle
)
620 struct stlink_usb_handle_s
*h
= handle
;
622 assert(handle
!= NULL
);
624 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
626 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
627 h
->cmdbuf
[h
->cmdidx
++] = 0;
628 h
->cmdbuf
[h
->cmdidx
++] = 0;
629 h
->cmdbuf
[h
->cmdidx
++] = 0;
630 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
632 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
637 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
644 transfers block in cmdbuf
645 <size> indicates number of bytes in the following
647 Ignore the (eventual) error code in the received packet.
649 static int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
651 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
652 struct stlink_usb_handle_s
*h
= handle
;
654 assert(handle
!= NULL
);
656 if (h
->version
.stlink
== 1) {
657 cmdsize
= STLINK_SG_SIZE
;
658 /* put length in bCBWCBLength */
659 h
->cmdbuf
[14] = h
->cmdidx
-15;
662 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
667 if (h
->version
.stlink
== 1) {
668 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
669 /* check csw status */
670 if (h
->cmdbuf
[12] == 1) {
671 LOG_DEBUG("get sense");
672 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
683 Converts an STLINK status code held in the first byte of a response
684 to an openocd error, logs any error/wait status as debug output.
686 static int stlink_usb_error_check(void *handle
)
688 struct stlink_usb_handle_s
*h
= handle
;
690 assert(handle
!= NULL
);
692 if (h
->transport
== HL_TRANSPORT_SWIM
) {
693 switch (h
->databuf
[0]) {
694 case STLINK_SWIM_ERR_OK
:
696 case STLINK_SWIM_BUSY
:
699 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
704 /* TODO: no error checking yet on api V1 */
705 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
706 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
708 switch (h
->databuf
[0]) {
709 case STLINK_DEBUG_ERR_OK
:
711 case STLINK_DEBUG_ERR_FAULT
:
712 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
714 case STLINK_SWD_AP_WAIT
:
715 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
717 case STLINK_SWD_DP_WAIT
:
718 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
720 case STLINK_JTAG_GET_IDCODE_ERROR
:
721 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
723 case STLINK_JTAG_WRITE_ERROR
:
724 LOG_DEBUG("Write error");
726 case STLINK_JTAG_WRITE_VERIF_ERROR
:
727 LOG_DEBUG("Write verify error, ignoring");
729 case STLINK_SWD_AP_FAULT
:
730 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
731 * returns ERROR_OK with the comment:
732 * Change in error status when reading outside RAM.
733 * This fix allows CDT plugin to visualize memory.
735 LOG_DEBUG("STLINK_SWD_AP_FAULT");
737 case STLINK_SWD_AP_ERROR
:
738 LOG_DEBUG("STLINK_SWD_AP_ERROR");
740 case STLINK_SWD_AP_PARITY_ERROR
:
741 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
743 case STLINK_SWD_DP_FAULT
:
744 LOG_DEBUG("STLINK_SWD_DP_FAULT");
746 case STLINK_SWD_DP_ERROR
:
747 LOG_DEBUG("STLINK_SWD_DP_ERROR");
749 case STLINK_SWD_DP_PARITY_ERROR
:
750 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
752 case STLINK_SWD_AP_WDATA_ERROR
:
753 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
755 case STLINK_SWD_AP_STICKY_ERROR
:
756 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
758 case STLINK_SWD_AP_STICKYORUN_ERROR
:
759 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
761 case STLINK_BAD_AP_ERROR
:
762 LOG_DEBUG("STLINK_BAD_AP_ERROR");
765 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
771 * Wrapper around stlink_usb_xfer_noerrcheck()
772 * to check the error code in the received packet
774 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
780 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
781 if (retval
!= ERROR_OK
)
784 return stlink_usb_error_check(handle
);
787 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
789 Works for commands where the STLINK_DEBUG status is returned in the first
790 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
792 Returns an openocd result code.
794 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
798 struct stlink_usb_handle_s
*h
= handle
;
801 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
802 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
807 if (h
->transport
== HL_TRANSPORT_SWIM
) {
808 res
= stlink_swim_status(handle
);
813 res
= stlink_usb_error_check(handle
);
814 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
815 useconds_t delay_us
= (1<<retries
++) * 1000;
816 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
825 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
827 struct stlink_usb_handle_s
*h
= handle
;
829 assert(handle
!= NULL
);
831 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
833 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
834 size
, STLINK_READ_TIMEOUT
) != size
) {
835 LOG_ERROR("bulk trace read failed");
843 this function writes transfer length in
844 the right place in the cb
846 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
848 struct stlink_usb_handle_s
*h
= handle
;
850 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
853 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
855 struct stlink_usb_handle_s
*h
= handle
;
857 /* fill the send buffer */
858 strcpy((char *)h
->cmdbuf
, "USBC");
860 /* csw tag not used */
861 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
863 /* cbw data transfer length (in the following data phase in or out) */
864 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
867 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
868 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
869 /* cdb clength (is filled in at xfer) */
870 h
->cmdbuf
[h
->cmdidx
++] = 0;
874 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
876 struct stlink_usb_handle_s
*h
= handle
;
878 h
->direction
= direction
;
882 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
883 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
885 if (h
->version
.stlink
== 1)
886 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
890 static int stlink_usb_version(void *handle
)
895 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
896 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
898 struct stlink_usb_handle_s
*h
= handle
;
900 assert(handle
!= NULL
);
902 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
904 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
906 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
911 version
= be_to_h_u16(h
->databuf
);
912 v
= (version
>> 12) & 0x0f;
913 x
= (version
>> 6) & 0x3f;
916 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
917 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
920 case STLINK_V2_1_PID
:
921 case STLINK_V2_1_NO_MSD_PID
:
922 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
923 /* MxSy : STM8 V2.1 - SWIM only */
928 /* JxMy : STM32 V2.1 - JTAG/SWD only */
941 /* STLINK-V3 requires a specific command */
942 if (v
== 3 && x
== 0 && y
== 0) {
943 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
945 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
947 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
952 swim
= h
->databuf
[1];
953 jtag
= h
->databuf
[2];
955 bridge
= h
->databuf
[4];
956 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
957 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
960 h
->version
.stlink
= v
;
961 h
->version
.jtag
= jtag
;
962 h
->version
.swim
= swim
;
965 switch (h
->version
.stlink
) {
967 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
968 if (h
->version
.jtag
>= 11)
969 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
971 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
975 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
976 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
978 /* API for trace from J13 */
979 /* API for target voltage from J13 */
980 if (h
->version
.jtag
>= 13)
981 flags
|= STLINK_F_HAS_TRACE
;
983 /* preferred API to get last R/W status from J15 */
984 if (h
->version
.jtag
>= 15)
985 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
987 /* API to set SWD frequency from J22 */
988 if (h
->version
.jtag
>= 22)
989 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
991 /* API to set JTAG frequency from J24 */
992 if (h
->version
.jtag
>= 24)
993 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
995 /* API to read/write memory at 16 bit from J26 */
996 if (h
->version
.jtag
>= 26)
997 flags
|= STLINK_F_HAS_MEM_16BIT
;
1001 /* all STLINK-V3 use api-v3 */
1002 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1004 /* STLINK-V3 is a superset of ST-LINK/V2 */
1007 /* API for target voltage */
1008 flags
|= STLINK_F_HAS_TRACE
;
1010 /* preferred API to get last R/W status */
1011 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1013 /* API to read/write memory at 16 bit */
1014 flags
|= STLINK_F_HAS_MEM_16BIT
;
1020 h
->version
.flags
= flags
;
1023 p
+= sprintf(p
, "V%d", v
);
1025 p
+= sprintf(p
, "J%d", jtag
);
1027 p
+= sprintf(p
, "M%d", msd
);
1029 p
+= sprintf(p
, "B%d", bridge
);
1031 sprintf(p
, "S%d", swim
);
1033 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1035 h
->version
.jtag_api
,
1042 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1044 struct stlink_usb_handle_s
*h
= handle
;
1045 uint32_t adc_results
[2];
1047 /* no error message, simply quit with error */
1048 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1049 return ERROR_COMMAND_NOTFOUND
;
1051 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1053 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1055 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1057 if (result
!= ERROR_OK
)
1060 /* convert result */
1061 adc_results
[0] = le_to_h_u32(h
->databuf
);
1062 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1064 *target_voltage
= 0;
1067 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1069 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1074 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1076 struct stlink_usb_handle_s
*h
= handle
;
1078 assert(handle
!= NULL
);
1080 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1081 return ERROR_COMMAND_NOTFOUND
;
1083 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1085 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1086 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1087 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1090 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1092 if (result
!= ERROR_OK
)
1098 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1100 struct stlink_usb_handle_s
*h
= handle
;
1102 assert(handle
!= NULL
);
1104 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1105 return ERROR_COMMAND_NOTFOUND
;
1107 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1109 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1110 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1111 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1114 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1116 if (result
!= ERROR_OK
)
1123 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1126 struct stlink_usb_handle_s
*h
= handle
;
1128 assert(handle
!= NULL
);
1130 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1132 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1134 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1136 if (res
!= ERROR_OK
)
1139 *mode
= h
->databuf
[0];
1145 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1148 struct stlink_usb_handle_s
*h
= handle
;
1150 assert(handle
!= NULL
);
1152 /* on api V2 we are able the read the latest command
1154 * TODO: we need the test on api V1 too
1156 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1159 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1162 case STLINK_MODE_DEBUG_JTAG
:
1163 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1164 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1165 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1167 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1168 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1170 case STLINK_MODE_DEBUG_SWD
:
1171 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1172 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1173 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1175 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1176 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1178 case STLINK_MODE_DEBUG_SWIM
:
1179 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1180 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1181 /* no answer for this function... */
1184 case STLINK_MODE_DFU
:
1185 case STLINK_MODE_MASS
:
1190 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1194 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1197 struct stlink_usb_handle_s
*h
= handle
;
1199 assert(handle
!= NULL
);
1201 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
1204 case STLINK_MODE_DEBUG_JTAG
:
1205 case STLINK_MODE_DEBUG_SWD
:
1206 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1207 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1209 case STLINK_MODE_DEBUG_SWIM
:
1210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1211 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1213 case STLINK_MODE_DFU
:
1214 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1215 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1217 case STLINK_MODE_MASS
:
1222 res
= stlink_usb_xfer_noerrcheck(handle
, 0, 0);
1224 if (res
!= ERROR_OK
)
1230 static int stlink_usb_assert_srst(void *handle
, int srst
);
1232 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1235 case HL_TRANSPORT_SWD
:
1236 return STLINK_MODE_DEBUG_SWD
;
1237 case HL_TRANSPORT_JTAG
:
1238 return STLINK_MODE_DEBUG_JTAG
;
1239 case HL_TRANSPORT_SWIM
:
1240 return STLINK_MODE_DEBUG_SWIM
;
1242 return STLINK_MODE_UNKNOWN
;
1247 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
1251 enum stlink_mode emode
;
1252 struct stlink_usb_handle_s
*h
= handle
;
1254 assert(handle
!= NULL
);
1256 res
= stlink_usb_current_mode(handle
, &mode
);
1258 if (res
!= ERROR_OK
)
1261 LOG_DEBUG("MODE: 0x%02X", mode
);
1263 /* try to exit current mode */
1265 case STLINK_DEV_DFU_MODE
:
1266 emode
= STLINK_MODE_DFU
;
1268 case STLINK_DEV_DEBUG_MODE
:
1269 emode
= STLINK_MODE_DEBUG_SWD
;
1271 case STLINK_DEV_SWIM_MODE
:
1272 emode
= STLINK_MODE_DEBUG_SWIM
;
1274 case STLINK_DEV_BOOTLOADER_MODE
:
1275 case STLINK_DEV_MASS_MODE
:
1277 emode
= STLINK_MODE_UNKNOWN
;
1281 if (emode
!= STLINK_MODE_UNKNOWN
) {
1282 res
= stlink_usb_mode_leave(handle
, emode
);
1284 if (res
!= ERROR_OK
)
1288 res
= stlink_usb_current_mode(handle
, &mode
);
1290 if (res
!= ERROR_OK
)
1293 /* we check the target voltage here as an aid to debugging connection problems.
1294 * the stlink requires the target Vdd to be connected for reliable debugging.
1295 * this cmd is supported in all modes except DFU
1297 if (mode
!= STLINK_DEV_DFU_MODE
) {
1299 float target_voltage
;
1301 /* check target voltage (if supported) */
1302 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1304 if (res
!= ERROR_OK
) {
1305 if (res
!= ERROR_COMMAND_NOTFOUND
)
1306 LOG_ERROR("voltage check failed");
1307 /* attempt to continue as it is not a catastrophic failure */
1309 /* check for a sensible target voltage, operating range is 1.65-5.5v
1310 * according to datasheet */
1311 if (target_voltage
< 1.5)
1312 LOG_ERROR("target voltage may be too low for reliable debugging");
1316 LOG_DEBUG("MODE: 0x%02X", mode
);
1318 /* set selected mode */
1319 emode
= stlink_get_mode(h
->transport
);
1321 if (emode
== STLINK_MODE_UNKNOWN
) {
1322 LOG_ERROR("selected mode (transport) not supported");
1326 /* preliminary SRST assert:
1327 * We want SRST is asserted before activating debug signals (mode_enter).
1328 * As the required mode has not been set, the adapter may not know what pin to use.
1329 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1330 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1331 * after power on, SWIM_RST stays unchanged */
1332 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1333 stlink_usb_assert_srst(handle
, 0);
1334 /* do not check the return status here, we will
1335 proceed and enter the desired mode below
1336 and try asserting srst again. */
1338 res
= stlink_usb_mode_enter(handle
, emode
);
1339 if (res
!= ERROR_OK
)
1342 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1343 if (connect_under_reset
) {
1344 res
= stlink_usb_assert_srst(handle
, 0);
1345 if (res
!= ERROR_OK
)
1349 res
= stlink_usb_current_mode(handle
, &mode
);
1351 if (res
!= ERROR_OK
)
1354 LOG_DEBUG("MODE: 0x%02X", mode
);
1359 /* request status from last swim request */
1360 static int stlink_swim_status(void *handle
)
1362 struct stlink_usb_handle_s
*h
= handle
;
1365 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1366 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1367 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1368 /* error is checked by the caller */
1369 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1370 if (res
!= ERROR_OK
)
1375 the purpose of this function is unknown...
1376 capabilites? anyway for swim v6 it returns
1379 __attribute__((unused
))
1380 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1382 struct stlink_usb_handle_s
*h
= handle
;
1385 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1386 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1387 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1388 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1389 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1390 if (res
!= ERROR_OK
)
1392 memcpy(cap
, h
->databuf
, 8);
1396 /* debug dongle assert/deassert sreset line */
1397 static int stlink_swim_assert_reset(void *handle
, int reset
)
1399 struct stlink_usb_handle_s
*h
= handle
;
1402 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1403 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1405 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1407 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1408 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1409 if (res
!= ERROR_OK
)
1416 1.3ms low then 750Hz then 1.5kHz
1418 static int stlink_swim_enter(void *handle
)
1420 struct stlink_usb_handle_s
*h
= handle
;
1423 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1424 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1425 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1426 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1427 if (res
!= ERROR_OK
)
1432 /* switch high/low speed swim */
1433 static int stlink_swim_speed(void *handle
, int speed
)
1435 struct stlink_usb_handle_s
*h
= handle
;
1438 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1439 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1440 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1442 h
->cmdbuf
[h
->cmdidx
++] = 1;
1444 h
->cmdbuf
[h
->cmdidx
++] = 0;
1445 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1446 if (res
!= ERROR_OK
)
1452 initiate srst from swim.
1453 nrst is pulled low for 50us.
1455 static int stlink_swim_generate_rst(void *handle
)
1457 struct stlink_usb_handle_s
*h
= handle
;
1460 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1461 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1462 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1463 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1464 if (res
!= ERROR_OK
)
1470 send resyncronize sequence
1471 swim is pulled low for 16us
1472 reply is 64 clks low
1474 static int stlink_swim_resync(void *handle
)
1476 struct stlink_usb_handle_s
*h
= handle
;
1479 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1480 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1481 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1482 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1483 if (res
!= ERROR_OK
)
1488 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1490 struct stlink_usb_handle_s
*h
= handle
;
1493 unsigned int datalen
= 0;
1494 int cmdsize
= STLINK_CMD_SIZE_V2
;
1496 if (len
> STLINK_DATA_SIZE
)
1499 if (h
->version
.stlink
== 1)
1500 cmdsize
= STLINK_SG_SIZE
;
1502 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1503 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1504 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1505 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1507 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1509 for (i
= 0; i
< len
; i
++) {
1510 if (h
->cmdidx
== cmdsize
)
1511 h
->databuf
[datalen
++] = *(data
++);
1513 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1515 if (h
->version
.stlink
== 1)
1516 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1518 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1519 if (res
!= ERROR_OK
)
1524 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1526 struct stlink_usb_handle_s
*h
= handle
;
1529 if (len
> STLINK_DATA_SIZE
)
1532 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1533 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1534 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1535 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1537 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1539 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1540 if (res
!= ERROR_OK
)
1543 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1544 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1545 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1546 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1547 if (res
!= ERROR_OK
)
1554 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1557 struct stlink_usb_handle_s
*h
= handle
;
1559 assert(handle
!= NULL
);
1561 /* there is no swim read core id cmd */
1562 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1567 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1569 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1570 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1572 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1574 if (res
!= ERROR_OK
)
1577 *idcode
= le_to_h_u32(h
->databuf
);
1579 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1584 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1586 struct stlink_usb_handle_s
*h
= handle
;
1589 assert(handle
!= NULL
);
1591 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1593 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1594 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1595 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1598 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1599 if (res
!= ERROR_OK
)
1602 *val
= le_to_h_u32(h
->databuf
+ 4);
1606 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1608 struct stlink_usb_handle_s
*h
= handle
;
1610 assert(handle
!= NULL
);
1612 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1614 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1615 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1616 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1618 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1619 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1621 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1624 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1628 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1630 struct stlink_usb_handle_s
*h
= handle
;
1632 assert(handle
!= NULL
);
1634 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1637 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1639 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1640 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1642 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1643 if (res
!= ERROR_OK
)
1646 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1647 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1650 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1651 if (res
!= ERROR_OK
)
1660 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1665 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1666 if (result
!= ERROR_OK
)
1667 return TARGET_UNKNOWN
;
1669 if (status
& S_HALT
)
1670 return TARGET_HALTED
;
1671 else if (status
& S_RESET_ST
)
1672 return TARGET_RESET
;
1674 return TARGET_RUNNING
;
1678 static enum target_state
stlink_usb_state(void *handle
)
1681 struct stlink_usb_handle_s
*h
= handle
;
1683 assert(handle
!= NULL
);
1685 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1686 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1687 if (res
!= ERROR_OK
)
1688 return TARGET_UNKNOWN
;
1690 res
= stlink_swim_resync(handle
);
1691 if (res
!= ERROR_OK
)
1692 return TARGET_UNKNOWN
;
1697 if (h
->reconnect_pending
) {
1698 LOG_INFO("Previous state query failed, trying to reconnect");
1699 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1701 if (res
!= ERROR_OK
)
1702 return TARGET_UNKNOWN
;
1704 h
->reconnect_pending
= false;
1707 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1708 res
= stlink_usb_v2_get_status(handle
);
1709 if (res
== TARGET_UNKNOWN
)
1710 h
->reconnect_pending
= true;
1714 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1716 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1717 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1719 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1721 if (res
!= ERROR_OK
)
1722 return TARGET_UNKNOWN
;
1724 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1725 return TARGET_RUNNING
;
1726 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1727 return TARGET_HALTED
;
1729 h
->reconnect_pending
= true;
1731 return TARGET_UNKNOWN
;
1734 static int stlink_usb_assert_srst(void *handle
, int srst
)
1736 struct stlink_usb_handle_s
*h
= handle
;
1738 assert(handle
!= NULL
);
1740 if (h
->transport
== HL_TRANSPORT_SWIM
)
1741 return stlink_swim_assert_reset(handle
, srst
);
1743 if (h
->version
.stlink
== 1)
1744 return ERROR_COMMAND_NOTFOUND
;
1746 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1748 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1749 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1750 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1752 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1756 static void stlink_usb_trace_disable(void *handle
)
1759 struct stlink_usb_handle_s
*h
= handle
;
1761 assert(handle
!= NULL
);
1763 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1765 LOG_DEBUG("Tracing: disable");
1767 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1768 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1769 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1770 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1772 if (res
== ERROR_OK
)
1773 h
->trace
.enabled
= false;
1778 static int stlink_usb_trace_enable(void *handle
)
1781 struct stlink_usb_handle_s
*h
= handle
;
1783 assert(handle
!= NULL
);
1785 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1786 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1788 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1789 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1790 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1792 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1795 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1797 if (res
== ERROR_OK
) {
1798 h
->trace
.enabled
= true;
1799 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1802 LOG_ERROR("Tracing is not supported by this version.");
1810 static int stlink_usb_reset(void *handle
)
1812 struct stlink_usb_handle_s
*h
= handle
;
1815 assert(handle
!= NULL
);
1817 if (h
->transport
== HL_TRANSPORT_SWIM
)
1818 return stlink_swim_generate_rst(handle
);
1820 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1822 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1824 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1825 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1827 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1829 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1830 if (retval
!= ERROR_OK
)
1833 if (h
->trace
.enabled
) {
1834 stlink_usb_trace_disable(h
);
1835 return stlink_usb_trace_enable(h
);
1842 static int stlink_usb_run(void *handle
)
1845 struct stlink_usb_handle_s
*h
= handle
;
1847 assert(handle
!= NULL
);
1849 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1850 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1855 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1857 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1858 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1860 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1864 static int stlink_usb_halt(void *handle
)
1867 struct stlink_usb_handle_s
*h
= handle
;
1869 assert(handle
!= NULL
);
1871 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1872 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1877 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1879 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1880 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1882 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1886 static int stlink_usb_step(void *handle
)
1888 struct stlink_usb_handle_s
*h
= handle
;
1890 assert(handle
!= NULL
);
1892 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1893 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1894 * that the Cortex-M3 currently does. */
1895 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1896 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1897 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1900 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1902 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1903 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1905 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1909 static int stlink_usb_read_regs(void *handle
)
1912 struct stlink_usb_handle_s
*h
= handle
;
1914 assert(handle
!= NULL
);
1916 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1918 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1919 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1921 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1922 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
1923 /* regs data from offset 0 */
1925 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1926 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
1927 /* status at offset 0, regs data from offset 4 */
1934 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1937 struct stlink_usb_handle_s
*h
= handle
;
1939 assert(handle
!= NULL
);
1941 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1943 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1944 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1945 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1947 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1948 h
->cmdbuf
[h
->cmdidx
++] = num
;
1950 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1951 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1952 if (res
!= ERROR_OK
)
1954 *val
= le_to_h_u32(h
->databuf
);
1957 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1958 if (res
!= ERROR_OK
)
1960 *val
= le_to_h_u32(h
->databuf
+ 4);
1966 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1968 struct stlink_usb_handle_s
*h
= handle
;
1970 assert(handle
!= NULL
);
1972 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1974 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1975 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1976 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1978 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1979 h
->cmdbuf
[h
->cmdidx
++] = num
;
1980 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1983 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1986 static int stlink_usb_get_rw_status(void *handle
)
1988 struct stlink_usb_handle_s
*h
= handle
;
1990 assert(handle
!= NULL
);
1992 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1995 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1997 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1998 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
1999 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2000 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2002 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2003 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2008 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2012 uint16_t read_len
= len
;
2013 struct stlink_usb_handle_s
*h
= handle
;
2015 assert(handle
!= NULL
);
2017 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2018 if (len
> stlink_usb_block(h
)) {
2019 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2023 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2025 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2026 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2027 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2029 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2032 /* we need to fix read length for single bytes */
2036 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2038 if (res
!= ERROR_OK
)
2041 memcpy(buffer
, h
->databuf
, len
);
2043 return stlink_usb_get_rw_status(handle
);
2047 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2048 const uint8_t *buffer
)
2051 struct stlink_usb_handle_s
*h
= handle
;
2053 assert(handle
!= NULL
);
2055 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2056 if (len
> stlink_usb_block(h
)) {
2057 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2061 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2063 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2064 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2065 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2067 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2070 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2072 if (res
!= ERROR_OK
)
2075 return stlink_usb_get_rw_status(handle
);
2079 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2083 struct stlink_usb_handle_s
*h
= handle
;
2085 assert(handle
!= NULL
);
2087 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2088 return ERROR_COMMAND_NOTFOUND
;
2090 /* data must be a multiple of 2 and half-word aligned */
2091 if (len
% 2 || addr
% 2) {
2092 LOG_DEBUG("Invalid data alignment");
2093 return ERROR_TARGET_UNALIGNED_ACCESS
;
2096 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2098 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2099 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2100 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2102 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2105 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2107 if (res
!= ERROR_OK
)
2110 memcpy(buffer
, h
->databuf
, len
);
2112 return stlink_usb_get_rw_status(handle
);
2116 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2117 const uint8_t *buffer
)
2120 struct stlink_usb_handle_s
*h
= handle
;
2122 assert(handle
!= NULL
);
2124 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2125 return ERROR_COMMAND_NOTFOUND
;
2127 /* data must be a multiple of 2 and half-word aligned */
2128 if (len
% 2 || addr
% 2) {
2129 LOG_DEBUG("Invalid data alignment");
2130 return ERROR_TARGET_UNALIGNED_ACCESS
;
2133 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2135 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2136 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2137 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2139 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2142 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2144 if (res
!= ERROR_OK
)
2147 return stlink_usb_get_rw_status(handle
);
2151 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2155 struct stlink_usb_handle_s
*h
= handle
;
2157 assert(handle
!= NULL
);
2159 /* data must be a multiple of 4 and word aligned */
2160 if (len
% 4 || addr
% 4) {
2161 LOG_DEBUG("Invalid data alignment");
2162 return ERROR_TARGET_UNALIGNED_ACCESS
;
2165 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2167 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2168 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2169 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2171 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2174 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2176 if (res
!= ERROR_OK
)
2179 memcpy(buffer
, h
->databuf
, len
);
2181 return stlink_usb_get_rw_status(handle
);
2185 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2186 const uint8_t *buffer
)
2189 struct stlink_usb_handle_s
*h
= handle
;
2191 assert(handle
!= NULL
);
2193 /* data must be a multiple of 4 and word aligned */
2194 if (len
% 4 || addr
% 4) {
2195 LOG_DEBUG("Invalid data alignment");
2196 return ERROR_TARGET_UNALIGNED_ACCESS
;
2199 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2201 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2202 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2203 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2205 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2208 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2210 if (res
!= ERROR_OK
)
2213 return stlink_usb_get_rw_status(handle
);
2216 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2218 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2219 if (max_tar_block
== 0)
2221 return max_tar_block
;
2224 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2225 uint32_t count
, uint8_t *buffer
)
2227 int retval
= ERROR_OK
;
2228 uint32_t bytes_remaining
;
2230 struct stlink_usb_handle_s
*h
= handle
;
2232 /* calculate byte count */
2235 /* switch to 8 bit if stlink does not support 16 bit memory read */
2236 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2241 bytes_remaining
= (size
!= 1) ? \
2242 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2244 if (count
< bytes_remaining
)
2245 bytes_remaining
= count
;
2247 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2248 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
2249 if (retval
!= ERROR_OK
)
2253 * all stlink support 8/32bit memory read/writes and only from
2254 * stlink V2J26 there is support for 16 bit memory read/write.
2255 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2260 /* When in jtag mode the stlink uses the auto-increment functionality.
2261 * However it expects us to pass the data correctly, this includes
2262 * alignment and any page boundaries. We already do this as part of the
2263 * adi_v5 implementation, but the stlink is a hla adapter and so this
2264 * needs implementing manually.
2265 * currently this only affects jtag mode, according to ST they do single
2266 * access in SWD mode - but this may change and so we do it for both modes */
2268 /* we first need to check for any unaligned bytes */
2269 if (addr
& (size
- 1)) {
2271 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2272 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2273 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2274 usleep((1<<retries
++) * 1000);
2277 if (retval
!= ERROR_OK
)
2279 buffer
+= head_bytes
;
2281 count
-= head_bytes
;
2282 bytes_remaining
-= head_bytes
;
2285 if (bytes_remaining
& (size
- 1))
2286 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2288 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2290 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2292 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2294 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2295 usleep((1<<retries
++) * 1000);
2298 if (retval
!= ERROR_OK
)
2301 buffer
+= bytes_remaining
;
2302 addr
+= bytes_remaining
;
2303 count
-= bytes_remaining
;
2309 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2310 uint32_t count
, const uint8_t *buffer
)
2312 int retval
= ERROR_OK
;
2313 uint32_t bytes_remaining
;
2315 struct stlink_usb_handle_s
*h
= handle
;
2317 /* calculate byte count */
2320 /* switch to 8 bit if stlink does not support 16 bit memory read */
2321 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2326 bytes_remaining
= (size
!= 1) ? \
2327 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2329 if (count
< bytes_remaining
)
2330 bytes_remaining
= count
;
2332 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2333 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
2334 if (retval
!= ERROR_OK
)
2338 * all stlink support 8/32bit memory read/writes and only from
2339 * stlink V2J26 there is support for 16 bit memory read/write.
2340 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2345 /* When in jtag mode the stlink uses the auto-increment functionality.
2346 * However it expects us to pass the data correctly, this includes
2347 * alignment and any page boundaries. We already do this as part of the
2348 * adi_v5 implementation, but the stlink is a hla adapter and so this
2349 * needs implementing manually.
2350 * currently this only affects jtag mode, according to ST they do single
2351 * access in SWD mode - but this may change and so we do it for both modes */
2353 /* we first need to check for any unaligned bytes */
2354 if (addr
& (size
- 1)) {
2356 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2357 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2358 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2359 usleep((1<<retries
++) * 1000);
2362 if (retval
!= ERROR_OK
)
2364 buffer
+= head_bytes
;
2366 count
-= head_bytes
;
2367 bytes_remaining
-= head_bytes
;
2370 if (bytes_remaining
& (size
- 1))
2371 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2373 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2375 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2378 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2379 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2380 usleep((1<<retries
++) * 1000);
2383 if (retval
!= ERROR_OK
)
2386 buffer
+= bytes_remaining
;
2387 addr
+= bytes_remaining
;
2388 count
-= bytes_remaining
;
2395 static int stlink_usb_override_target(const char *targetname
)
2397 return !strcmp(targetname
, "cortex_m");
2400 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2403 we dont care what the khz rate is
2404 we only have low and high speed...
2405 before changing speed the SWIM_CSR HS bit
2409 stlink_swim_speed(handle
, 0);
2411 stlink_swim_speed(handle
, 1);
2415 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2418 int speed_index
= -1;
2419 int speed_diff
= INT_MAX
;
2420 int last_valid_speed
= -1;
2423 for (i
= 0; i
< map_size
; i
++) {
2426 last_valid_speed
= i
;
2427 if (khz
== map
[i
].speed
) {
2431 int current_diff
= khz
- map
[i
].speed
;
2432 /* get abs value for comparison */
2433 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2434 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2435 speed_diff
= current_diff
;
2441 if (speed_index
== -1) {
2442 /* this will only be here if we cannot match the slow speed.
2443 * use the slowest speed we support.*/
2444 speed_index
= last_valid_speed
;
2446 } else if (i
== map_size
)
2449 if (!match
&& query
) {
2450 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2451 khz
, map
[speed_index
].speed
);
2457 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2460 struct stlink_usb_handle_s
*h
= handle
;
2462 /* old firmware cannot change it */
2463 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2466 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2467 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2470 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2471 if (result
!= ERROR_OK
) {
2472 LOG_ERROR("Unable to set adapter speed");
2477 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2480 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2483 struct stlink_usb_handle_s
*h
= handle
;
2485 /* old firmware cannot change it */
2486 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2489 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2490 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2493 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2494 if (result
!= ERROR_OK
) {
2495 LOG_ERROR("Unable to set adapter speed");
2500 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2503 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2507 LOG_DEBUG("Supported clock speeds are:");
2508 for (i
= 0; i
< map_size
; i
++)
2510 LOG_DEBUG("%d kHz", map
[i
].speed
);
2513 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2515 struct stlink_usb_handle_s
*h
= handle
;
2518 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2519 LOG_ERROR("Unknown command");
2523 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2525 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2526 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2527 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2529 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2531 int size
= h
->databuf
[8];
2533 if (size
> STLINK_V3_MAX_FREQ_NB
)
2534 size
= STLINK_V3_MAX_FREQ_NB
;
2536 for (i
= 0; i
< size
; i
++) {
2537 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2538 map
[i
].speed_divisor
= i
;
2541 /* set to zero all the next entries */
2542 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2548 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2550 struct stlink_usb_handle_s
*h
= handle
;
2552 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2553 LOG_ERROR("Unknown command");
2557 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2559 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2560 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2561 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2562 h
->cmdbuf
[h
->cmdidx
++] = 0;
2564 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2566 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2569 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2571 struct stlink_usb_handle_s
*h
= handle
;
2573 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2575 stlink_get_com_freq(h
, is_jtag
, map
);
2577 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2580 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2581 if (result
!= ERROR_OK
) {
2582 LOG_ERROR("Unable to set adapter speed");
2586 return map
[speed_index
].speed
;
2589 static int stlink_speed(void *handle
, int khz
, bool query
)
2591 struct stlink_usb_handle_s
*h
= handle
;
2596 switch (h
->transport
) {
2597 case HL_TRANSPORT_SWIM
:
2598 return stlink_speed_swim(handle
, khz
, query
);
2600 case HL_TRANSPORT_SWD
:
2601 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2602 return stlink_speed_v3(handle
, false, khz
, query
);
2604 return stlink_speed_swd(handle
, khz
, query
);
2606 case HL_TRANSPORT_JTAG
:
2607 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2608 return stlink_speed_v3(handle
, true, khz
, query
);
2610 return stlink_speed_jtag(handle
, khz
, query
);
2620 static int stlink_usb_close(void *handle
)
2624 enum stlink_mode emode
;
2625 struct stlink_usb_handle_s
*h
= handle
;
2628 res
= stlink_usb_current_mode(handle
, &mode
);
2631 /* do not exit if return code != ERROR_OK,
2632 it prevents us from closing jtag_libusb */
2634 if (res
== ERROR_OK
) {
2635 /* try to exit current mode */
2637 case STLINK_DEV_DFU_MODE
:
2638 emode
= STLINK_MODE_DFU
;
2640 case STLINK_DEV_DEBUG_MODE
:
2641 emode
= STLINK_MODE_DEBUG_SWD
;
2643 case STLINK_DEV_SWIM_MODE
:
2644 emode
= STLINK_MODE_DEBUG_SWIM
;
2646 case STLINK_DEV_BOOTLOADER_MODE
:
2647 case STLINK_DEV_MASS_MODE
:
2649 emode
= STLINK_MODE_UNKNOWN
;
2653 if (emode
!= STLINK_MODE_UNKNOWN
)
2654 stlink_usb_mode_leave(handle
, emode
);
2655 /* do not check return code, it prevent
2656 us from closing jtag_libusb */
2660 jtag_libusb_close(h
->fd
);
2668 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2670 int err
, retry_count
= 1;
2671 struct stlink_usb_handle_s
*h
;
2673 LOG_DEBUG("stlink_usb_open");
2675 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2678 LOG_DEBUG("malloc failed");
2682 h
->transport
= param
->transport
;
2684 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2685 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2686 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2687 param
->serial
? param
->serial
: "");
2691 On certain host USB configurations(e.g. MacBook Air)
2692 STLINKv2 dongle seems to have its FW in a funky state if,
2693 after plugging it in, you try to use openocd with it more
2694 then once (by launching and closing openocd). In cases like
2695 that initial attempt to read the FW info via
2696 stlink_usb_version will fail and the device has to be reset
2697 in order to become operational.
2700 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2701 LOG_ERROR("open failed");
2705 jtag_libusb_set_configuration(h
->fd
, 0);
2707 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2708 LOG_DEBUG("claim interface failed");
2712 /* RX EP is common for all versions */
2713 h
->rx_ep
= STLINK_RX_EP
;
2716 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2717 LOG_DEBUG("libusb_get_pid failed");
2721 /* wrap version for first read */
2724 h
->version
.stlink
= 1;
2725 h
->tx_ep
= STLINK_TX_EP
;
2727 case STLINK_V3_USBLOADER_PID
:
2728 case STLINK_V3E_PID
:
2729 case STLINK_V3S_PID
:
2730 case STLINK_V3_2VCP_PID
:
2731 h
->version
.stlink
= 3;
2732 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2733 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2735 case STLINK_V2_1_PID
:
2736 case STLINK_V2_1_NO_MSD_PID
:
2737 h
->version
.stlink
= 2;
2738 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2739 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2742 /* fall through - we assume V2 to be the default version*/
2744 h
->version
.stlink
= 2;
2745 h
->tx_ep
= STLINK_TX_EP
;
2746 h
->trace_ep
= STLINK_TRACE_EP
;
2750 /* get the device version */
2751 err
= stlink_usb_version(h
);
2753 if (err
== ERROR_OK
) {
2755 } else if (h
->version
.stlink
== 1 ||
2757 LOG_ERROR("read version failed");
2760 err
= jtag_libusb_release_interface(h
->fd
, 0);
2761 if (err
!= ERROR_OK
) {
2762 LOG_ERROR("release interface failed");
2766 err
= jtag_libusb_reset_device(h
->fd
);
2767 if (err
!= ERROR_OK
) {
2768 LOG_ERROR("reset device failed");
2772 jtag_libusb_close(h
->fd
);
2774 Give the device one second to settle down and
2777 usleep(1 * 1000 * 1000);
2782 /* check if mode is supported */
2785 switch (h
->transport
) {
2786 case HL_TRANSPORT_SWD
:
2787 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2790 case HL_TRANSPORT_JTAG
:
2791 if (h
->version
.jtag
== 0)
2794 case HL_TRANSPORT_SWIM
:
2795 if (h
->version
.swim
== 0)
2803 if (err
!= ERROR_OK
) {
2804 LOG_ERROR("mode (transport) not supported by device");
2808 /* initialize the debug hardware */
2809 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2811 if (err
!= ERROR_OK
) {
2812 LOG_ERROR("init mode failed (unable to connect to the target)");
2816 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2817 err
= stlink_swim_enter(h
);
2818 if (err
!= ERROR_OK
) {
2819 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2823 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2827 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2828 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
2829 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2830 stlink_speed(h
, param
->initial_interface_speed
, false);
2832 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2833 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
2834 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2835 stlink_speed(h
, param
->initial_interface_speed
, false);
2839 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
2840 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2842 stlink_get_com_freq(h
, (h
->transport
== HL_TRANSPORT_JTAG
), map
);
2843 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
2844 stlink_speed(h
, param
->initial_interface_speed
, false);
2847 /* get cpuid, so we can determine the max page size
2848 * start with a safe default */
2849 h
->max_mem_packet
= (1 << 10);
2852 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2853 if (err
== ERROR_OK
) {
2854 uint32_t cpuid
= le_to_h_u32(buffer
);
2855 int i
= (cpuid
>> 4) & 0xf;
2856 if (i
== 4 || i
== 3) {
2857 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2858 h
->max_mem_packet
= (1 << 12);
2862 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2869 stlink_usb_close(h
);
2874 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2875 uint32_t port_size
, unsigned int *trace_freq
)
2877 struct stlink_usb_handle_s
*h
= handle
;
2879 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2880 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2881 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2886 stlink_usb_trace_disable(h
);
2890 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2891 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2892 STLINK_TRACE_MAX_HZ
);
2896 stlink_usb_trace_disable(h
);
2899 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2900 h
->trace
.source_hz
= *trace_freq
;
2902 return stlink_usb_trace_enable(h
);
2906 struct hl_layout_api_s stlink_usb_layout_api
= {
2908 .open
= stlink_usb_open
,
2910 .close
= stlink_usb_close
,
2912 .idcode
= stlink_usb_idcode
,
2914 .state
= stlink_usb_state
,
2916 .reset
= stlink_usb_reset
,
2918 .assert_srst
= stlink_usb_assert_srst
,
2920 .run
= stlink_usb_run
,
2922 .halt
= stlink_usb_halt
,
2924 .step
= stlink_usb_step
,
2926 .read_regs
= stlink_usb_read_regs
,
2928 .read_reg
= stlink_usb_read_reg
,
2930 .write_reg
= stlink_usb_write_reg
,
2932 .read_mem
= stlink_usb_read_mem
,
2934 .write_mem
= stlink_usb_write_mem
,
2936 .write_debug_reg
= stlink_usb_write_debug_reg
,
2938 .override_target
= stlink_usb_override_target
,
2940 .speed
= stlink_speed
,
2942 .config_trace
= stlink_config_trace
,
2944 .poll_trace
= stlink_usb_trace_read
,