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
);
347 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
348 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
349 static int stlink_speed(void *handle
, int khz
, bool query
);
352 static unsigned int stlink_usb_block(void *handle
)
354 struct stlink_usb_handle_s
*h
= handle
;
356 assert(handle
!= NULL
);
358 if (h
->version
.stlink
== 3)
359 return STLINKV3_MAX_RW8
;
361 return STLINK_MAX_RW8
;
366 #ifdef USE_LIBUSB_ASYNCIO
368 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
370 int *completed
= transfer
->user_data
;
372 /* caller interprets result and frees transfer */
376 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
378 int r
, *completed
= transfer
->user_data
;
380 /* Assuming a single libusb context exists. There no existing interface into this
381 * module to pass a libusb context.
383 struct libusb_context
*ctx
= NULL
;
385 while (!*completed
) {
386 r
= libusb_handle_events_completed(ctx
, completed
);
388 if (r
== LIBUSB_ERROR_INTERRUPTED
)
390 libusb_cancel_transfer(transfer
);
397 static int transfer_error_status(const struct libusb_transfer
*transfer
)
401 switch (transfer
->status
) {
402 case LIBUSB_TRANSFER_COMPLETED
:
405 case LIBUSB_TRANSFER_TIMED_OUT
:
406 r
= LIBUSB_ERROR_TIMEOUT
;
408 case LIBUSB_TRANSFER_STALL
:
409 r
= LIBUSB_ERROR_PIPE
;
411 case LIBUSB_TRANSFER_OVERFLOW
:
412 r
= LIBUSB_ERROR_OVERFLOW
;
414 case LIBUSB_TRANSFER_NO_DEVICE
:
415 r
= LIBUSB_ERROR_NO_DEVICE
;
417 case LIBUSB_TRANSFER_ERROR
:
418 case LIBUSB_TRANSFER_CANCELLED
:
422 r
= LIBUSB_ERROR_OTHER
;
436 size_t transfer_size
;
437 struct libusb_transfer
*transfer
;
440 static int jtag_libusb_bulk_transfer_n(
441 jtag_libusb_device_handle
* dev_handle
,
442 struct jtag_xfer
*transfers
,
447 int returnval
= ERROR_OK
;
450 for (size_t i
= 0; i
< n_transfers
; ++i
) {
451 transfers
[i
].retval
= 0;
452 transfers
[i
].completed
= 0;
453 transfers
[i
].transfer_size
= 0;
454 transfers
[i
].transfer
= libusb_alloc_transfer(0);
456 if (transfers
[i
].transfer
== NULL
) {
457 for (size_t j
= 0; j
< i
; ++j
)
458 libusb_free_transfer(transfers
[j
].transfer
);
460 LOG_DEBUG("ERROR, failed to alloc usb transfers");
461 for (size_t k
= 0; k
< n_transfers
; ++k
)
462 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
467 for (size_t i
= 0; i
< n_transfers
; ++i
) {
468 libusb_fill_bulk_transfer(
469 transfers
[i
].transfer
,
471 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
472 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
473 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
475 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
477 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
479 /* Probably no point continuing to submit transfers once a submission fails.
480 * As a result, tag all remaining transfers as errors.
482 for (size_t j
= i
; j
< n_transfers
; ++j
)
483 transfers
[j
].retval
= retval
;
485 returnval
= ERROR_FAIL
;
490 /* Wait for every submitted USB transfer to complete.
492 for (size_t i
= 0; i
< n_transfers
; ++i
) {
493 if (transfers
[i
].retval
== 0) {
494 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
496 retval
= transfer_error_status(transfers
[i
].transfer
);
498 returnval
= ERROR_FAIL
;
499 transfers
[i
].retval
= retval
;
500 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
502 /* Assuming actual_length is only valid if there is no transfer error.
504 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
508 libusb_free_transfer(transfers
[i
].transfer
);
509 transfers
[i
].transfer
= NULL
;
519 static int stlink_usb_xfer_v1_get_status(void *handle
)
521 struct stlink_usb_handle_s
*h
= handle
;
523 assert(handle
!= NULL
);
526 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
528 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
529 13, STLINK_READ_TIMEOUT
) != 13)
534 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
537 if (t1
!= 0x53425355)
545 if (h
->cmdbuf
[12] != 0)
551 #ifdef USE_LIBUSB_ASYNCIO
552 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
554 struct stlink_usb_handle_s
*h
= handle
;
556 assert(handle
!= NULL
);
558 size_t n_transfers
= 0;
559 struct jtag_xfer transfers
[2];
561 memset(transfers
, 0, sizeof(transfers
));
563 transfers
[0].ep
= h
->tx_ep
;
564 transfers
[0].buf
= h
->cmdbuf
;
565 transfers
[0].size
= cmdsize
;
569 if (h
->direction
== h
->tx_ep
&& size
) {
570 transfers
[1].ep
= h
->tx_ep
;
571 transfers
[1].buf
= (uint8_t *)buf
;
572 transfers
[1].size
= size
;
575 } else if (h
->direction
== h
->rx_ep
&& size
) {
576 transfers
[1].ep
= h
->rx_ep
;
577 transfers
[1].buf
= (uint8_t *)buf
;
578 transfers
[1].size
= size
;
583 return jtag_libusb_bulk_transfer_n(
587 STLINK_WRITE_TIMEOUT
);
590 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
592 struct stlink_usb_handle_s
*h
= handle
;
594 assert(handle
!= NULL
);
596 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
597 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
601 if (h
->direction
== h
->tx_ep
&& size
) {
602 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
603 size
, STLINK_WRITE_TIMEOUT
) != size
) {
604 LOG_DEBUG("bulk write failed");
607 } else if (h
->direction
== h
->rx_ep
&& size
) {
608 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
609 size
, STLINK_READ_TIMEOUT
) != size
) {
610 LOG_DEBUG("bulk read failed");
620 static int stlink_usb_xfer_v1_get_sense(void *handle
)
623 struct stlink_usb_handle_s
*h
= handle
;
625 assert(handle
!= NULL
);
627 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
629 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
630 h
->cmdbuf
[h
->cmdidx
++] = 0;
631 h
->cmdbuf
[h
->cmdidx
++] = 0;
632 h
->cmdbuf
[h
->cmdidx
++] = 0;
633 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
635 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
640 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
647 transfers block in cmdbuf
648 <size> indicates number of bytes in the following
650 Ignore the (eventual) error code in the received packet.
652 static int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
654 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
655 struct stlink_usb_handle_s
*h
= handle
;
657 assert(handle
!= NULL
);
659 if (h
->version
.stlink
== 1) {
660 cmdsize
= STLINK_SG_SIZE
;
661 /* put length in bCBWCBLength */
662 h
->cmdbuf
[14] = h
->cmdidx
-15;
665 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
670 if (h
->version
.stlink
== 1) {
671 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
672 /* check csw status */
673 if (h
->cmdbuf
[12] == 1) {
674 LOG_DEBUG("get sense");
675 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
686 Converts an STLINK status code held in the first byte of a response
687 to an openocd error, logs any error/wait status as debug output.
689 static int stlink_usb_error_check(void *handle
)
691 struct stlink_usb_handle_s
*h
= handle
;
693 assert(handle
!= NULL
);
695 if (h
->transport
== HL_TRANSPORT_SWIM
) {
696 switch (h
->databuf
[0]) {
697 case STLINK_SWIM_ERR_OK
:
699 case STLINK_SWIM_BUSY
:
702 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
707 /* TODO: no error checking yet on api V1 */
708 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
709 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
711 switch (h
->databuf
[0]) {
712 case STLINK_DEBUG_ERR_OK
:
714 case STLINK_DEBUG_ERR_FAULT
:
715 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
717 case STLINK_SWD_AP_WAIT
:
718 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
720 case STLINK_SWD_DP_WAIT
:
721 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
723 case STLINK_JTAG_GET_IDCODE_ERROR
:
724 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
726 case STLINK_JTAG_WRITE_ERROR
:
727 LOG_DEBUG("Write error");
729 case STLINK_JTAG_WRITE_VERIF_ERROR
:
730 LOG_DEBUG("Write verify error, ignoring");
732 case STLINK_SWD_AP_FAULT
:
733 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
734 * returns ERROR_OK with the comment:
735 * Change in error status when reading outside RAM.
736 * This fix allows CDT plugin to visualize memory.
738 LOG_DEBUG("STLINK_SWD_AP_FAULT");
740 case STLINK_SWD_AP_ERROR
:
741 LOG_DEBUG("STLINK_SWD_AP_ERROR");
743 case STLINK_SWD_AP_PARITY_ERROR
:
744 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
746 case STLINK_SWD_DP_FAULT
:
747 LOG_DEBUG("STLINK_SWD_DP_FAULT");
749 case STLINK_SWD_DP_ERROR
:
750 LOG_DEBUG("STLINK_SWD_DP_ERROR");
752 case STLINK_SWD_DP_PARITY_ERROR
:
753 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
755 case STLINK_SWD_AP_WDATA_ERROR
:
756 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
758 case STLINK_SWD_AP_STICKY_ERROR
:
759 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
761 case STLINK_SWD_AP_STICKYORUN_ERROR
:
762 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
764 case STLINK_BAD_AP_ERROR
:
765 LOG_DEBUG("STLINK_BAD_AP_ERROR");
768 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
774 * Wrapper around stlink_usb_xfer_noerrcheck()
775 * to check the error code in the received packet
777 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
783 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
784 if (retval
!= ERROR_OK
)
787 return stlink_usb_error_check(handle
);
790 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
792 Works for commands where the STLINK_DEBUG status is returned in the first
793 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
795 Returns an openocd result code.
797 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
801 struct stlink_usb_handle_s
*h
= handle
;
804 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
805 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
810 if (h
->transport
== HL_TRANSPORT_SWIM
) {
811 res
= stlink_swim_status(handle
);
816 res
= stlink_usb_error_check(handle
);
817 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
818 useconds_t delay_us
= (1<<retries
++) * 1000;
819 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
828 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
830 struct stlink_usb_handle_s
*h
= handle
;
832 assert(handle
!= NULL
);
834 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
836 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
837 size
, STLINK_READ_TIMEOUT
) != size
) {
838 LOG_ERROR("bulk trace read failed");
846 this function writes transfer length in
847 the right place in the cb
849 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
851 struct stlink_usb_handle_s
*h
= handle
;
853 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
856 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
858 struct stlink_usb_handle_s
*h
= handle
;
860 /* fill the send buffer */
861 strcpy((char *)h
->cmdbuf
, "USBC");
863 /* csw tag not used */
864 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
866 /* cbw data transfer length (in the following data phase in or out) */
867 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
870 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
871 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
872 /* cdb clength (is filled in at xfer) */
873 h
->cmdbuf
[h
->cmdidx
++] = 0;
877 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
879 struct stlink_usb_handle_s
*h
= handle
;
881 h
->direction
= direction
;
885 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
886 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
888 if (h
->version
.stlink
== 1)
889 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
893 static int stlink_usb_version(void *handle
)
898 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
899 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
901 struct stlink_usb_handle_s
*h
= handle
;
903 assert(handle
!= NULL
);
905 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
907 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
909 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
914 version
= be_to_h_u16(h
->databuf
);
915 v
= (version
>> 12) & 0x0f;
916 x
= (version
>> 6) & 0x3f;
919 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
920 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
923 case STLINK_V2_1_PID
:
924 case STLINK_V2_1_NO_MSD_PID
:
925 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
926 /* MxSy : STM8 V2.1 - SWIM only */
931 /* JxMy : STM32 V2.1 - JTAG/SWD only */
944 /* STLINK-V3 requires a specific command */
945 if (v
== 3 && x
== 0 && y
== 0) {
946 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
948 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
950 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
955 swim
= h
->databuf
[1];
956 jtag
= h
->databuf
[2];
958 bridge
= h
->databuf
[4];
959 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
960 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
963 h
->version
.stlink
= v
;
964 h
->version
.jtag
= jtag
;
965 h
->version
.swim
= swim
;
968 switch (h
->version
.stlink
) {
970 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
971 if (h
->version
.jtag
>= 11)
972 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
974 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
978 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
979 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
981 /* API for trace from J13 */
982 /* API for target voltage from J13 */
983 if (h
->version
.jtag
>= 13)
984 flags
|= STLINK_F_HAS_TRACE
;
986 /* preferred API to get last R/W status from J15 */
987 if (h
->version
.jtag
>= 15)
988 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
990 /* API to set SWD frequency from J22 */
991 if (h
->version
.jtag
>= 22)
992 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
994 /* API to set JTAG frequency from J24 */
995 if (h
->version
.jtag
>= 24)
996 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
998 /* API to read/write memory at 16 bit from J26 */
999 if (h
->version
.jtag
>= 26)
1000 flags
|= STLINK_F_HAS_MEM_16BIT
;
1004 /* all STLINK-V3 use api-v3 */
1005 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1007 /* STLINK-V3 is a superset of ST-LINK/V2 */
1010 /* API for target voltage */
1011 flags
|= STLINK_F_HAS_TRACE
;
1013 /* preferred API to get last R/W status */
1014 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1016 /* API to read/write memory at 16 bit */
1017 flags
|= STLINK_F_HAS_MEM_16BIT
;
1023 h
->version
.flags
= flags
;
1026 p
+= sprintf(p
, "V%d", v
);
1028 p
+= sprintf(p
, "J%d", jtag
);
1030 p
+= sprintf(p
, "M%d", msd
);
1032 p
+= sprintf(p
, "B%d", bridge
);
1034 sprintf(p
, "S%d", swim
);
1036 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1038 h
->version
.jtag_api
,
1045 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1047 struct stlink_usb_handle_s
*h
= handle
;
1048 uint32_t adc_results
[2];
1050 /* no error message, simply quit with error */
1051 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1052 return ERROR_COMMAND_NOTFOUND
;
1054 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1056 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1058 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1060 if (result
!= ERROR_OK
)
1063 /* convert result */
1064 adc_results
[0] = le_to_h_u32(h
->databuf
);
1065 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1067 *target_voltage
= 0;
1070 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1072 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1077 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1079 struct stlink_usb_handle_s
*h
= handle
;
1081 assert(handle
!= NULL
);
1083 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1084 return ERROR_COMMAND_NOTFOUND
;
1086 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1088 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1089 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1090 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1093 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1095 if (result
!= ERROR_OK
)
1101 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1103 struct stlink_usb_handle_s
*h
= handle
;
1105 assert(handle
!= NULL
);
1107 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1108 return ERROR_COMMAND_NOTFOUND
;
1110 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1112 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1113 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1114 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1117 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1119 if (result
!= ERROR_OK
)
1126 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1129 struct stlink_usb_handle_s
*h
= handle
;
1131 assert(handle
!= NULL
);
1133 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1135 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1137 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1139 if (res
!= ERROR_OK
)
1142 *mode
= h
->databuf
[0];
1148 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1151 struct stlink_usb_handle_s
*h
= handle
;
1153 assert(handle
!= NULL
);
1155 /* on api V2 we are able the read the latest command
1157 * TODO: we need the test on api V1 too
1159 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1162 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1165 case STLINK_MODE_DEBUG_JTAG
:
1166 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1167 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1168 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1171 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1173 case STLINK_MODE_DEBUG_SWD
:
1174 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1175 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1176 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1178 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1179 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1181 case STLINK_MODE_DEBUG_SWIM
:
1182 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1183 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1184 /* no answer for this function... */
1187 case STLINK_MODE_DFU
:
1188 case STLINK_MODE_MASS
:
1193 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1197 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1200 struct stlink_usb_handle_s
*h
= handle
;
1202 assert(handle
!= NULL
);
1204 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
1207 case STLINK_MODE_DEBUG_JTAG
:
1208 case STLINK_MODE_DEBUG_SWD
:
1209 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1212 case STLINK_MODE_DEBUG_SWIM
:
1213 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1214 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1216 case STLINK_MODE_DFU
:
1217 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1218 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1220 case STLINK_MODE_MASS
:
1225 res
= stlink_usb_xfer_noerrcheck(handle
, 0, 0);
1227 if (res
!= ERROR_OK
)
1233 static int stlink_usb_assert_srst(void *handle
, int srst
);
1235 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1238 case HL_TRANSPORT_SWD
:
1239 return STLINK_MODE_DEBUG_SWD
;
1240 case HL_TRANSPORT_JTAG
:
1241 return STLINK_MODE_DEBUG_JTAG
;
1242 case HL_TRANSPORT_SWIM
:
1243 return STLINK_MODE_DEBUG_SWIM
;
1245 return STLINK_MODE_UNKNOWN
;
1250 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1254 enum stlink_mode emode
;
1255 struct stlink_usb_handle_s
*h
= handle
;
1257 assert(handle
!= NULL
);
1259 res
= stlink_usb_current_mode(handle
, &mode
);
1261 if (res
!= ERROR_OK
)
1264 LOG_DEBUG("MODE: 0x%02X", mode
);
1266 /* try to exit current mode */
1268 case STLINK_DEV_DFU_MODE
:
1269 emode
= STLINK_MODE_DFU
;
1271 case STLINK_DEV_DEBUG_MODE
:
1272 emode
= STLINK_MODE_DEBUG_SWD
;
1274 case STLINK_DEV_SWIM_MODE
:
1275 emode
= STLINK_MODE_DEBUG_SWIM
;
1277 case STLINK_DEV_BOOTLOADER_MODE
:
1278 case STLINK_DEV_MASS_MODE
:
1280 emode
= STLINK_MODE_UNKNOWN
;
1284 if (emode
!= STLINK_MODE_UNKNOWN
) {
1285 res
= stlink_usb_mode_leave(handle
, emode
);
1287 if (res
!= ERROR_OK
)
1291 res
= stlink_usb_current_mode(handle
, &mode
);
1293 if (res
!= ERROR_OK
)
1296 /* we check the target voltage here as an aid to debugging connection problems.
1297 * the stlink requires the target Vdd to be connected for reliable debugging.
1298 * this cmd is supported in all modes except DFU
1300 if (mode
!= STLINK_DEV_DFU_MODE
) {
1302 float target_voltage
;
1304 /* check target voltage (if supported) */
1305 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1307 if (res
!= ERROR_OK
) {
1308 if (res
!= ERROR_COMMAND_NOTFOUND
)
1309 LOG_ERROR("voltage check failed");
1310 /* attempt to continue as it is not a catastrophic failure */
1312 /* check for a sensible target voltage, operating range is 1.65-5.5v
1313 * according to datasheet */
1314 if (target_voltage
< 1.5)
1315 LOG_ERROR("target voltage may be too low for reliable debugging");
1319 LOG_DEBUG("MODE: 0x%02X", mode
);
1321 /* set selected mode */
1322 emode
= stlink_get_mode(h
->transport
);
1324 if (emode
== STLINK_MODE_UNKNOWN
) {
1325 LOG_ERROR("selected mode (transport) not supported");
1329 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1330 if (h
->transport
== HL_TRANSPORT_JTAG
) {
1331 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1332 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1333 stlink_speed(h
, initial_interface_speed
, false);
1335 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
1336 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1337 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1338 stlink_speed(h
, initial_interface_speed
, false);
1342 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
1343 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1345 stlink_get_com_freq(h
, (h
->transport
== HL_TRANSPORT_JTAG
), map
);
1346 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1347 stlink_speed(h
, initial_interface_speed
, false);
1350 /* preliminary SRST assert:
1351 * We want SRST is asserted before activating debug signals (mode_enter).
1352 * As the required mode has not been set, the adapter may not know what pin to use.
1353 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1354 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1355 * after power on, SWIM_RST stays unchanged */
1356 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1357 stlink_usb_assert_srst(handle
, 0);
1358 /* do not check the return status here, we will
1359 proceed and enter the desired mode below
1360 and try asserting srst again. */
1362 res
= stlink_usb_mode_enter(handle
, emode
);
1363 if (res
!= ERROR_OK
)
1366 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1367 if (connect_under_reset
) {
1368 res
= stlink_usb_assert_srst(handle
, 0);
1369 if (res
!= ERROR_OK
)
1373 res
= stlink_usb_current_mode(handle
, &mode
);
1375 if (res
!= ERROR_OK
)
1378 LOG_DEBUG("MODE: 0x%02X", mode
);
1383 /* request status from last swim request */
1384 static int stlink_swim_status(void *handle
)
1386 struct stlink_usb_handle_s
*h
= handle
;
1389 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1390 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1391 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1392 /* error is checked by the caller */
1393 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1394 if (res
!= ERROR_OK
)
1399 the purpose of this function is unknown...
1400 capabilites? anyway for swim v6 it returns
1403 __attribute__((unused
))
1404 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1406 struct stlink_usb_handle_s
*h
= handle
;
1409 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1410 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1411 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1412 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1413 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1414 if (res
!= ERROR_OK
)
1416 memcpy(cap
, h
->databuf
, 8);
1420 /* debug dongle assert/deassert sreset line */
1421 static int stlink_swim_assert_reset(void *handle
, int reset
)
1423 struct stlink_usb_handle_s
*h
= handle
;
1426 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1427 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1429 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1431 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1432 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1433 if (res
!= ERROR_OK
)
1440 1.3ms low then 750Hz then 1.5kHz
1442 static int stlink_swim_enter(void *handle
)
1444 struct stlink_usb_handle_s
*h
= handle
;
1447 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1448 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1449 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1450 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1451 if (res
!= ERROR_OK
)
1456 /* switch high/low speed swim */
1457 static int stlink_swim_speed(void *handle
, int speed
)
1459 struct stlink_usb_handle_s
*h
= handle
;
1462 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1463 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1464 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1466 h
->cmdbuf
[h
->cmdidx
++] = 1;
1468 h
->cmdbuf
[h
->cmdidx
++] = 0;
1469 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1470 if (res
!= ERROR_OK
)
1476 initiate srst from swim.
1477 nrst is pulled low for 50us.
1479 static int stlink_swim_generate_rst(void *handle
)
1481 struct stlink_usb_handle_s
*h
= handle
;
1484 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1485 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1487 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1488 if (res
!= ERROR_OK
)
1494 send resyncronize sequence
1495 swim is pulled low for 16us
1496 reply is 64 clks low
1498 static int stlink_swim_resync(void *handle
)
1500 struct stlink_usb_handle_s
*h
= handle
;
1503 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1504 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1505 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1506 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1507 if (res
!= ERROR_OK
)
1512 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1514 struct stlink_usb_handle_s
*h
= handle
;
1517 unsigned int datalen
= 0;
1518 int cmdsize
= STLINK_CMD_SIZE_V2
;
1520 if (len
> STLINK_DATA_SIZE
)
1523 if (h
->version
.stlink
== 1)
1524 cmdsize
= STLINK_SG_SIZE
;
1526 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1527 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1528 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1529 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1531 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1533 for (i
= 0; i
< len
; i
++) {
1534 if (h
->cmdidx
== cmdsize
)
1535 h
->databuf
[datalen
++] = *(data
++);
1537 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1539 if (h
->version
.stlink
== 1)
1540 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1542 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1543 if (res
!= ERROR_OK
)
1548 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1550 struct stlink_usb_handle_s
*h
= handle
;
1553 if (len
> STLINK_DATA_SIZE
)
1556 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1557 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1558 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1559 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1561 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1563 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1564 if (res
!= ERROR_OK
)
1567 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1568 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1569 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1570 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1571 if (res
!= ERROR_OK
)
1578 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1581 struct stlink_usb_handle_s
*h
= handle
;
1583 assert(handle
!= NULL
);
1585 /* there is no swim read core id cmd */
1586 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1591 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1593 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1594 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1595 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1597 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1600 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1602 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1606 if (res
!= ERROR_OK
)
1609 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1611 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1616 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1618 struct stlink_usb_handle_s
*h
= handle
;
1621 assert(handle
!= NULL
);
1623 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1625 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1626 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1627 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1630 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1631 if (res
!= ERROR_OK
)
1634 *val
= le_to_h_u32(h
->databuf
+ 4);
1638 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1640 struct stlink_usb_handle_s
*h
= handle
;
1642 assert(handle
!= NULL
);
1644 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1646 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1647 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1648 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1650 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1651 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1653 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1656 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1660 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1662 struct stlink_usb_handle_s
*h
= handle
;
1664 assert(handle
!= NULL
);
1666 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1669 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1671 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1672 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1674 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1675 if (res
!= ERROR_OK
)
1678 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1679 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1682 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1683 if (res
!= ERROR_OK
)
1692 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1697 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1698 if (result
!= ERROR_OK
)
1699 return TARGET_UNKNOWN
;
1701 if (status
& S_HALT
)
1702 return TARGET_HALTED
;
1703 else if (status
& S_RESET_ST
)
1704 return TARGET_RESET
;
1706 return TARGET_RUNNING
;
1710 static enum target_state
stlink_usb_state(void *handle
)
1713 struct stlink_usb_handle_s
*h
= handle
;
1715 assert(handle
!= NULL
);
1717 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1718 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1719 if (res
!= ERROR_OK
)
1720 return TARGET_UNKNOWN
;
1722 res
= stlink_swim_resync(handle
);
1723 if (res
!= ERROR_OK
)
1724 return TARGET_UNKNOWN
;
1729 if (h
->reconnect_pending
) {
1730 LOG_INFO("Previous state query failed, trying to reconnect");
1731 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1733 if (res
!= ERROR_OK
)
1734 return TARGET_UNKNOWN
;
1736 h
->reconnect_pending
= false;
1739 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1740 res
= stlink_usb_v2_get_status(handle
);
1741 if (res
== TARGET_UNKNOWN
)
1742 h
->reconnect_pending
= true;
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_GETSTATUS
;
1751 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1753 if (res
!= ERROR_OK
)
1754 return TARGET_UNKNOWN
;
1756 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1757 return TARGET_RUNNING
;
1758 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1759 return TARGET_HALTED
;
1761 h
->reconnect_pending
= true;
1763 return TARGET_UNKNOWN
;
1766 static int stlink_usb_assert_srst(void *handle
, int srst
)
1768 struct stlink_usb_handle_s
*h
= handle
;
1770 assert(handle
!= NULL
);
1772 if (h
->transport
== HL_TRANSPORT_SWIM
)
1773 return stlink_swim_assert_reset(handle
, srst
);
1775 if (h
->version
.stlink
== 1)
1776 return ERROR_COMMAND_NOTFOUND
;
1778 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1780 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1781 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1782 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1784 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1788 static void stlink_usb_trace_disable(void *handle
)
1791 struct stlink_usb_handle_s
*h
= handle
;
1793 assert(handle
!= NULL
);
1795 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1797 LOG_DEBUG("Tracing: disable");
1799 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1800 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1801 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1802 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1804 if (res
== ERROR_OK
)
1805 h
->trace
.enabled
= false;
1810 static int stlink_usb_trace_enable(void *handle
)
1813 struct stlink_usb_handle_s
*h
= handle
;
1815 assert(handle
!= NULL
);
1817 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1818 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1820 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1821 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1822 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1824 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1827 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1829 if (res
== ERROR_OK
) {
1830 h
->trace
.enabled
= true;
1831 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1834 LOG_ERROR("Tracing is not supported by this version.");
1842 static int stlink_usb_reset(void *handle
)
1844 struct stlink_usb_handle_s
*h
= handle
;
1847 assert(handle
!= NULL
);
1849 if (h
->transport
== HL_TRANSPORT_SWIM
)
1850 return stlink_swim_generate_rst(handle
);
1852 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1854 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1856 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1857 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1859 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1861 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1862 if (retval
!= ERROR_OK
)
1865 if (h
->trace
.enabled
) {
1866 stlink_usb_trace_disable(h
);
1867 return stlink_usb_trace_enable(h
);
1874 static int stlink_usb_run(void *handle
)
1877 struct stlink_usb_handle_s
*h
= handle
;
1879 assert(handle
!= NULL
);
1881 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1882 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1887 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1889 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1890 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1892 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1896 static int stlink_usb_halt(void *handle
)
1899 struct stlink_usb_handle_s
*h
= handle
;
1901 assert(handle
!= NULL
);
1903 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1904 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1909 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1911 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1912 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1914 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1918 static int stlink_usb_step(void *handle
)
1920 struct stlink_usb_handle_s
*h
= handle
;
1922 assert(handle
!= NULL
);
1924 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1925 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1926 * that the Cortex-M3 currently does. */
1927 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1928 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1929 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1932 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1934 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1935 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1937 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1941 static int stlink_usb_read_regs(void *handle
)
1944 struct stlink_usb_handle_s
*h
= handle
;
1946 assert(handle
!= NULL
);
1948 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1950 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1951 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1953 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1954 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
1955 /* regs data from offset 0 */
1957 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1958 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
1959 /* status at offset 0, regs data from offset 4 */
1966 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1969 struct stlink_usb_handle_s
*h
= handle
;
1971 assert(handle
!= NULL
);
1973 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1975 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1976 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1977 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1979 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1980 h
->cmdbuf
[h
->cmdidx
++] = num
;
1982 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1983 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1984 if (res
!= ERROR_OK
)
1986 *val
= le_to_h_u32(h
->databuf
);
1989 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1990 if (res
!= ERROR_OK
)
1992 *val
= le_to_h_u32(h
->databuf
+ 4);
1998 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
2000 struct stlink_usb_handle_s
*h
= handle
;
2002 assert(handle
!= NULL
);
2004 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2006 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2007 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2008 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2010 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2011 h
->cmdbuf
[h
->cmdidx
++] = num
;
2012 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2015 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2018 static int stlink_usb_get_rw_status(void *handle
)
2020 struct stlink_usb_handle_s
*h
= handle
;
2022 assert(handle
!= NULL
);
2024 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2027 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2029 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2030 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2031 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2032 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2034 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2035 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2040 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2044 uint16_t read_len
= len
;
2045 struct stlink_usb_handle_s
*h
= handle
;
2047 assert(handle
!= NULL
);
2049 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2050 if (len
> stlink_usb_block(h
)) {
2051 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2055 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2057 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2058 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2059 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2061 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2064 /* we need to fix read length for single bytes */
2068 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2070 if (res
!= ERROR_OK
)
2073 memcpy(buffer
, h
->databuf
, len
);
2075 return stlink_usb_get_rw_status(handle
);
2079 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2080 const uint8_t *buffer
)
2083 struct stlink_usb_handle_s
*h
= handle
;
2085 assert(handle
!= NULL
);
2087 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2088 if (len
> stlink_usb_block(h
)) {
2089 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2093 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2095 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2096 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2097 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2099 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2102 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2104 if (res
!= ERROR_OK
)
2107 return stlink_usb_get_rw_status(handle
);
2111 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2115 struct stlink_usb_handle_s
*h
= handle
;
2117 assert(handle
!= NULL
);
2119 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2120 return ERROR_COMMAND_NOTFOUND
;
2122 /* data must be a multiple of 2 and half-word aligned */
2123 if (len
% 2 || addr
% 2) {
2124 LOG_DEBUG("Invalid data alignment");
2125 return ERROR_TARGET_UNALIGNED_ACCESS
;
2128 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2130 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2131 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2132 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2134 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2137 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2139 if (res
!= ERROR_OK
)
2142 memcpy(buffer
, h
->databuf
, len
);
2144 return stlink_usb_get_rw_status(handle
);
2148 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2149 const uint8_t *buffer
)
2152 struct stlink_usb_handle_s
*h
= handle
;
2154 assert(handle
!= NULL
);
2156 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2157 return ERROR_COMMAND_NOTFOUND
;
2159 /* data must be a multiple of 2 and half-word aligned */
2160 if (len
% 2 || addr
% 2) {
2161 LOG_DEBUG("Invalid data alignment");
2162 return ERROR_TARGET_UNALIGNED_ACCESS
;
2165 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2167 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2168 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
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
, buffer
, len
);
2176 if (res
!= ERROR_OK
)
2179 return stlink_usb_get_rw_status(handle
);
2183 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2187 struct stlink_usb_handle_s
*h
= handle
;
2189 assert(handle
!= NULL
);
2191 /* data must be a multiple of 4 and word aligned */
2192 if (len
% 4 || addr
% 4) {
2193 LOG_DEBUG("Invalid data alignment");
2194 return ERROR_TARGET_UNALIGNED_ACCESS
;
2197 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2199 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2200 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2201 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2203 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2206 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2208 if (res
!= ERROR_OK
)
2211 memcpy(buffer
, h
->databuf
, len
);
2213 return stlink_usb_get_rw_status(handle
);
2217 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2218 const uint8_t *buffer
)
2221 struct stlink_usb_handle_s
*h
= handle
;
2223 assert(handle
!= NULL
);
2225 /* data must be a multiple of 4 and word aligned */
2226 if (len
% 4 || addr
% 4) {
2227 LOG_DEBUG("Invalid data alignment");
2228 return ERROR_TARGET_UNALIGNED_ACCESS
;
2231 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2233 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2234 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2235 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2237 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2240 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2242 if (res
!= ERROR_OK
)
2245 return stlink_usb_get_rw_status(handle
);
2248 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2250 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2251 if (max_tar_block
== 0)
2253 return max_tar_block
;
2256 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2257 uint32_t count
, uint8_t *buffer
)
2259 int retval
= ERROR_OK
;
2260 uint32_t bytes_remaining
;
2262 struct stlink_usb_handle_s
*h
= handle
;
2264 /* calculate byte count */
2267 /* switch to 8 bit if stlink does not support 16 bit memory read */
2268 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2273 bytes_remaining
= (size
!= 1) ? \
2274 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2276 if (count
< bytes_remaining
)
2277 bytes_remaining
= count
;
2279 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2280 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
2281 if (retval
!= ERROR_OK
)
2285 * all stlink support 8/32bit memory read/writes and only from
2286 * stlink V2J26 there is support for 16 bit memory read/write.
2287 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2292 /* When in jtag mode the stlink uses the auto-increment functionality.
2293 * However it expects us to pass the data correctly, this includes
2294 * alignment and any page boundaries. We already do this as part of the
2295 * adi_v5 implementation, but the stlink is a hla adapter and so this
2296 * needs implementing manually.
2297 * currently this only affects jtag mode, according to ST they do single
2298 * access in SWD mode - but this may change and so we do it for both modes */
2300 /* we first need to check for any unaligned bytes */
2301 if (addr
& (size
- 1)) {
2303 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2304 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2305 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2306 usleep((1<<retries
++) * 1000);
2309 if (retval
!= ERROR_OK
)
2311 buffer
+= head_bytes
;
2313 count
-= head_bytes
;
2314 bytes_remaining
-= head_bytes
;
2317 if (bytes_remaining
& (size
- 1))
2318 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2320 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2322 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2324 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2326 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2327 usleep((1<<retries
++) * 1000);
2330 if (retval
!= ERROR_OK
)
2333 buffer
+= bytes_remaining
;
2334 addr
+= bytes_remaining
;
2335 count
-= bytes_remaining
;
2341 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2342 uint32_t count
, const uint8_t *buffer
)
2344 int retval
= ERROR_OK
;
2345 uint32_t bytes_remaining
;
2347 struct stlink_usb_handle_s
*h
= handle
;
2349 /* calculate byte count */
2352 /* switch to 8 bit if stlink does not support 16 bit memory read */
2353 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2358 bytes_remaining
= (size
!= 1) ? \
2359 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2361 if (count
< bytes_remaining
)
2362 bytes_remaining
= count
;
2364 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2365 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
2366 if (retval
!= ERROR_OK
)
2370 * all stlink support 8/32bit memory read/writes and only from
2371 * stlink V2J26 there is support for 16 bit memory read/write.
2372 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2377 /* When in jtag mode the stlink uses the auto-increment functionality.
2378 * However it expects us to pass the data correctly, this includes
2379 * alignment and any page boundaries. We already do this as part of the
2380 * adi_v5 implementation, but the stlink is a hla adapter and so this
2381 * needs implementing manually.
2382 * currently this only affects jtag mode, according to ST they do single
2383 * access in SWD mode - but this may change and so we do it for both modes */
2385 /* we first need to check for any unaligned bytes */
2386 if (addr
& (size
- 1)) {
2388 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2389 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2390 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2391 usleep((1<<retries
++) * 1000);
2394 if (retval
!= ERROR_OK
)
2396 buffer
+= head_bytes
;
2398 count
-= head_bytes
;
2399 bytes_remaining
-= head_bytes
;
2402 if (bytes_remaining
& (size
- 1))
2403 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2405 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2407 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2410 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2411 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2412 usleep((1<<retries
++) * 1000);
2415 if (retval
!= ERROR_OK
)
2418 buffer
+= bytes_remaining
;
2419 addr
+= bytes_remaining
;
2420 count
-= bytes_remaining
;
2427 static int stlink_usb_override_target(const char *targetname
)
2429 return !strcmp(targetname
, "cortex_m");
2432 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2435 we dont care what the khz rate is
2436 we only have low and high speed...
2437 before changing speed the SWIM_CSR HS bit
2441 stlink_swim_speed(handle
, 0);
2443 stlink_swim_speed(handle
, 1);
2447 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2450 int speed_index
= -1;
2451 int speed_diff
= INT_MAX
;
2452 int last_valid_speed
= -1;
2455 for (i
= 0; i
< map_size
; i
++) {
2458 last_valid_speed
= i
;
2459 if (khz
== map
[i
].speed
) {
2463 int current_diff
= khz
- map
[i
].speed
;
2464 /* get abs value for comparison */
2465 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2466 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2467 speed_diff
= current_diff
;
2473 if (speed_index
== -1) {
2474 /* this will only be here if we cannot match the slow speed.
2475 * use the slowest speed we support.*/
2476 speed_index
= last_valid_speed
;
2478 } else if (i
== map_size
)
2481 if (!match
&& query
) {
2482 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2483 khz
, map
[speed_index
].speed
);
2489 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2492 struct stlink_usb_handle_s
*h
= handle
;
2494 /* old firmware cannot change it */
2495 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2498 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2499 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2502 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2503 if (result
!= ERROR_OK
) {
2504 LOG_ERROR("Unable to set adapter speed");
2509 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2512 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2515 struct stlink_usb_handle_s
*h
= handle
;
2517 /* old firmware cannot change it */
2518 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2521 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2522 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2525 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2526 if (result
!= ERROR_OK
) {
2527 LOG_ERROR("Unable to set adapter speed");
2532 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2535 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2539 LOG_DEBUG("Supported clock speeds are:");
2540 for (i
= 0; i
< map_size
; i
++)
2542 LOG_DEBUG("%d kHz", map
[i
].speed
);
2545 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2547 struct stlink_usb_handle_s
*h
= handle
;
2550 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2551 LOG_ERROR("Unknown command");
2555 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2557 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2558 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2559 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2561 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2563 int size
= h
->databuf
[8];
2565 if (size
> STLINK_V3_MAX_FREQ_NB
)
2566 size
= STLINK_V3_MAX_FREQ_NB
;
2568 for (i
= 0; i
< size
; i
++) {
2569 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2570 map
[i
].speed_divisor
= i
;
2573 /* set to zero all the next entries */
2574 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2580 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2582 struct stlink_usb_handle_s
*h
= handle
;
2584 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2585 LOG_ERROR("Unknown command");
2589 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2591 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2592 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2593 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2594 h
->cmdbuf
[h
->cmdidx
++] = 0;
2596 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2598 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2601 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2603 struct stlink_usb_handle_s
*h
= handle
;
2605 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2607 stlink_get_com_freq(h
, is_jtag
, map
);
2609 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2612 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2613 if (result
!= ERROR_OK
) {
2614 LOG_ERROR("Unable to set adapter speed");
2618 return map
[speed_index
].speed
;
2621 static int stlink_speed(void *handle
, int khz
, bool query
)
2623 struct stlink_usb_handle_s
*h
= handle
;
2628 switch (h
->transport
) {
2629 case HL_TRANSPORT_SWIM
:
2630 return stlink_speed_swim(handle
, khz
, query
);
2632 case HL_TRANSPORT_SWD
:
2633 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2634 return stlink_speed_v3(handle
, false, khz
, query
);
2636 return stlink_speed_swd(handle
, khz
, query
);
2638 case HL_TRANSPORT_JTAG
:
2639 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2640 return stlink_speed_v3(handle
, true, khz
, query
);
2642 return stlink_speed_jtag(handle
, khz
, query
);
2652 static int stlink_usb_close(void *handle
)
2656 enum stlink_mode emode
;
2657 struct stlink_usb_handle_s
*h
= handle
;
2660 res
= stlink_usb_current_mode(handle
, &mode
);
2663 /* do not exit if return code != ERROR_OK,
2664 it prevents us from closing jtag_libusb */
2666 if (res
== ERROR_OK
) {
2667 /* try to exit current mode */
2669 case STLINK_DEV_DFU_MODE
:
2670 emode
= STLINK_MODE_DFU
;
2672 case STLINK_DEV_DEBUG_MODE
:
2673 emode
= STLINK_MODE_DEBUG_SWD
;
2675 case STLINK_DEV_SWIM_MODE
:
2676 emode
= STLINK_MODE_DEBUG_SWIM
;
2678 case STLINK_DEV_BOOTLOADER_MODE
:
2679 case STLINK_DEV_MASS_MODE
:
2681 emode
= STLINK_MODE_UNKNOWN
;
2685 if (emode
!= STLINK_MODE_UNKNOWN
)
2686 stlink_usb_mode_leave(handle
, emode
);
2687 /* do not check return code, it prevent
2688 us from closing jtag_libusb */
2692 jtag_libusb_close(h
->fd
);
2700 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2702 int err
, retry_count
= 1;
2703 struct stlink_usb_handle_s
*h
;
2705 LOG_DEBUG("stlink_usb_open");
2707 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2710 LOG_DEBUG("malloc failed");
2714 h
->transport
= param
->transport
;
2716 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2717 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2718 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2719 param
->serial
? param
->serial
: "");
2723 On certain host USB configurations(e.g. MacBook Air)
2724 STLINKv2 dongle seems to have its FW in a funky state if,
2725 after plugging it in, you try to use openocd with it more
2726 then once (by launching and closing openocd). In cases like
2727 that initial attempt to read the FW info via
2728 stlink_usb_version will fail and the device has to be reset
2729 in order to become operational.
2732 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2733 LOG_ERROR("open failed");
2737 jtag_libusb_set_configuration(h
->fd
, 0);
2739 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2740 LOG_DEBUG("claim interface failed");
2744 /* RX EP is common for all versions */
2745 h
->rx_ep
= STLINK_RX_EP
;
2748 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2749 LOG_DEBUG("libusb_get_pid failed");
2753 /* wrap version for first read */
2756 h
->version
.stlink
= 1;
2757 h
->tx_ep
= STLINK_TX_EP
;
2759 case STLINK_V3_USBLOADER_PID
:
2760 case STLINK_V3E_PID
:
2761 case STLINK_V3S_PID
:
2762 case STLINK_V3_2VCP_PID
:
2763 h
->version
.stlink
= 3;
2764 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2765 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2767 case STLINK_V2_1_PID
:
2768 case STLINK_V2_1_NO_MSD_PID
:
2769 h
->version
.stlink
= 2;
2770 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2771 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2774 /* fall through - we assume V2 to be the default version*/
2776 h
->version
.stlink
= 2;
2777 h
->tx_ep
= STLINK_TX_EP
;
2778 h
->trace_ep
= STLINK_TRACE_EP
;
2782 /* get the device version */
2783 err
= stlink_usb_version(h
);
2785 if (err
== ERROR_OK
) {
2787 } else if (h
->version
.stlink
== 1 ||
2789 LOG_ERROR("read version failed");
2792 err
= jtag_libusb_release_interface(h
->fd
, 0);
2793 if (err
!= ERROR_OK
) {
2794 LOG_ERROR("release interface failed");
2798 err
= jtag_libusb_reset_device(h
->fd
);
2799 if (err
!= ERROR_OK
) {
2800 LOG_ERROR("reset device failed");
2804 jtag_libusb_close(h
->fd
);
2806 Give the device one second to settle down and
2809 usleep(1 * 1000 * 1000);
2814 /* check if mode is supported */
2817 switch (h
->transport
) {
2818 case HL_TRANSPORT_SWD
:
2819 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2822 case HL_TRANSPORT_JTAG
:
2823 if (h
->version
.jtag
== 0)
2826 case HL_TRANSPORT_SWIM
:
2827 if (h
->version
.swim
== 0)
2835 if (err
!= ERROR_OK
) {
2836 LOG_ERROR("mode (transport) not supported by device");
2840 /* initialize the debug hardware */
2841 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
2843 if (err
!= ERROR_OK
) {
2844 LOG_ERROR("init mode failed (unable to connect to the target)");
2848 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2849 err
= stlink_swim_enter(h
);
2850 if (err
!= ERROR_OK
) {
2851 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2855 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2859 /* get cpuid, so we can determine the max page size
2860 * start with a safe default */
2861 h
->max_mem_packet
= (1 << 10);
2864 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2865 if (err
== ERROR_OK
) {
2866 uint32_t cpuid
= le_to_h_u32(buffer
);
2867 int i
= (cpuid
>> 4) & 0xf;
2868 if (i
== 4 || i
== 3) {
2869 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2870 h
->max_mem_packet
= (1 << 12);
2874 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2881 stlink_usb_close(h
);
2886 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2887 uint32_t port_size
, unsigned int *trace_freq
)
2889 struct stlink_usb_handle_s
*h
= handle
;
2891 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2892 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2893 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2898 stlink_usb_trace_disable(h
);
2902 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2903 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2904 STLINK_TRACE_MAX_HZ
);
2908 stlink_usb_trace_disable(h
);
2911 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2912 h
->trace
.source_hz
= *trace_freq
;
2914 return stlink_usb_trace_enable(h
);
2918 struct hl_layout_api_s stlink_usb_layout_api
= {
2920 .open
= stlink_usb_open
,
2922 .close
= stlink_usb_close
,
2924 .idcode
= stlink_usb_idcode
,
2926 .state
= stlink_usb_state
,
2928 .reset
= stlink_usb_reset
,
2930 .assert_srst
= stlink_usb_assert_srst
,
2932 .run
= stlink_usb_run
,
2934 .halt
= stlink_usb_halt
,
2936 .step
= stlink_usb_step
,
2938 .read_regs
= stlink_usb_read_regs
,
2940 .read_reg
= stlink_usb_read_reg
,
2942 .write_reg
= stlink_usb_write_reg
,
2944 .read_mem
= stlink_usb_read_mem
,
2946 .write_mem
= stlink_usb_write_mem
,
2948 .write_debug_reg
= stlink_usb_write_debug_reg
,
2950 .override_target
= stlink_usb_override_target
,
2952 .speed
= stlink_speed
,
2954 .config_trace
= stlink_config_trace
,
2956 .poll_trace
= stlink_usb_trace_read
,