1 /***************************************************************************
2 * SWIM contributions by Ake Rehnman *
3 * Copyright (C) 2017 Ake Rehnman *
4 * ake.rehnman(at)gmail.com *
6 * Copyright (C) 2011-2012 by Mathias Kuester *
7 * Mathias Kuester <kesmtp@freenet.de> *
9 * Copyright (C) 2012 by Spencer Oliver *
10 * spen@spen-soft.co.uk *
12 * This code is based on https://github.com/texane/stlink *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
32 /* project specific includes */
33 #include <helper/binarybuffer.h>
34 #include <helper/bits.h>
35 #include <jtag/interface.h>
36 #include <jtag/hla/hla_layout.h>
37 #include <jtag/hla/hla_transport.h>
38 #include <jtag/hla/hla_interface.h>
39 #include <jtag/swim.h>
40 #include <target/target.h>
41 #include <transport/transport.h>
43 #include <target/cortex_m.h>
45 #include "libusb_helper.h"
48 #define USE_LIBUSB_ASYNCIO
51 #define STLINK_SERIAL_LEN 24
53 #define ENDPOINT_IN 0x80
54 #define ENDPOINT_OUT 0x00
56 #define STLINK_WRITE_TIMEOUT 1000
57 #define STLINK_READ_TIMEOUT 1000
59 #define STLINK_RX_EP (1|ENDPOINT_IN)
60 #define STLINK_TX_EP (2|ENDPOINT_OUT)
61 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
63 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
64 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
66 #define STLINK_SG_SIZE (31)
67 #define STLINK_DATA_SIZE (4096)
68 #define STLINK_CMD_SIZE_V2 (16)
69 #define STLINK_CMD_SIZE_V1 (10)
71 #define STLINK_V1_PID (0x3744)
72 #define STLINK_V2_PID (0x3748)
73 #define STLINK_V2_1_PID (0x374B)
74 #define STLINK_V2_1_NO_MSD_PID (0x3752)
75 #define STLINK_V3_USBLOADER_PID (0x374D)
76 #define STLINK_V3E_PID (0x374E)
77 #define STLINK_V3S_PID (0x374F)
78 #define STLINK_V3_2VCP_PID (0x3753)
81 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
82 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
83 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
85 #define STLINK_MAX_RW8 (64)
86 #define STLINKV3_MAX_RW8 (512)
88 /* "WAIT" responses will be retried (with exponential backoff) at
89 * most this many times before failing to caller.
91 #define MAX_WAIT_RETRIES 8
93 enum stlink_jtag_api_version
{
94 STLINK_JTAG_API_V1
= 1,
100 STLINK_MODE_UNKNOWN
= 0,
103 STLINK_MODE_DEBUG_JTAG
,
104 STLINK_MODE_DEBUG_SWD
,
105 STLINK_MODE_DEBUG_SWIM
109 struct stlink_usb_version
{
116 /** jtag api version supported */
117 enum stlink_jtag_api_version jtag_api
;
118 /** one bit for each feature supported. See macros STLINK_F_* */
123 struct stlink_usb_handle_s
{
125 struct libusb_device_handle
*fd
;
127 struct libusb_transfer
*trans
;
135 uint8_t cmdbuf
[STLINK_SG_SIZE
];
141 uint8_t databuf
[STLINK_DATA_SIZE
];
143 uint32_t max_mem_packet
;
145 enum stlink_mode st_mode
;
147 struct stlink_usb_version version
;
154 /** whether SWO tracing is enabled or not */
156 /** trace module source clock */
159 /** reconnect is needed next time we try to query the
161 bool reconnect_pending
;
164 #define STLINK_SWIM_ERR_OK 0x00
165 #define STLINK_SWIM_BUSY 0x01
166 #define STLINK_DEBUG_ERR_OK 0x80
167 #define STLINK_DEBUG_ERR_FAULT 0x81
168 #define STLINK_SWD_AP_WAIT 0x10
169 #define STLINK_SWD_AP_FAULT 0x11
170 #define STLINK_SWD_AP_ERROR 0x12
171 #define STLINK_SWD_AP_PARITY_ERROR 0x13
172 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
173 #define STLINK_JTAG_WRITE_ERROR 0x0c
174 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
175 #define STLINK_SWD_DP_WAIT 0x14
176 #define STLINK_SWD_DP_FAULT 0x15
177 #define STLINK_SWD_DP_ERROR 0x16
178 #define STLINK_SWD_DP_PARITY_ERROR 0x17
180 #define STLINK_SWD_AP_WDATA_ERROR 0x18
181 #define STLINK_SWD_AP_STICKY_ERROR 0x19
182 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
184 #define STLINK_BAD_AP_ERROR 0x1d
186 #define STLINK_CORE_RUNNING 0x80
187 #define STLINK_CORE_HALTED 0x81
188 #define STLINK_CORE_STAT_UNKNOWN -1
190 #define STLINK_GET_VERSION 0xF1
191 #define STLINK_DEBUG_COMMAND 0xF2
192 #define STLINK_DFU_COMMAND 0xF3
193 #define STLINK_SWIM_COMMAND 0xF4
194 #define STLINK_GET_CURRENT_MODE 0xF5
195 #define STLINK_GET_TARGET_VOLTAGE 0xF7
197 #define STLINK_DEV_DFU_MODE 0x00
198 #define STLINK_DEV_MASS_MODE 0x01
199 #define STLINK_DEV_DEBUG_MODE 0x02
200 #define STLINK_DEV_SWIM_MODE 0x03
201 #define STLINK_DEV_BOOTLOADER_MODE 0x04
202 #define STLINK_DEV_UNKNOWN_MODE -1
204 #define STLINK_DFU_EXIT 0x07
207 STLINK_SWIM_ENTER_SEQ
208 1.3ms low then 750Hz then 1.5kHz
211 STM8 DM pulls reset pin low 50us
214 uint8_t (0=low|1=high)
221 send syncronization seq (16us low, response 64 clocks low)
223 #define STLINK_SWIM_ENTER 0x00
224 #define STLINK_SWIM_EXIT 0x01
225 #define STLINK_SWIM_READ_CAP 0x02
226 #define STLINK_SWIM_SPEED 0x03
227 #define STLINK_SWIM_ENTER_SEQ 0x04
228 #define STLINK_SWIM_GEN_RST 0x05
229 #define STLINK_SWIM_RESET 0x06
230 #define STLINK_SWIM_ASSERT_RESET 0x07
231 #define STLINK_SWIM_DEASSERT_RESET 0x08
232 #define STLINK_SWIM_READSTATUS 0x09
233 #define STLINK_SWIM_WRITEMEM 0x0a
234 #define STLINK_SWIM_READMEM 0x0b
235 #define STLINK_SWIM_READBUF 0x0c
237 #define STLINK_DEBUG_GETSTATUS 0x01
238 #define STLINK_DEBUG_FORCEDEBUG 0x02
239 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
240 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
241 #define STLINK_DEBUG_APIV1_READREG 0x05
242 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
243 #define STLINK_DEBUG_READMEM_32BIT 0x07
244 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
245 #define STLINK_DEBUG_RUNCORE 0x09
246 #define STLINK_DEBUG_STEPCORE 0x0a
247 #define STLINK_DEBUG_APIV1_SETFP 0x0b
248 #define STLINK_DEBUG_READMEM_8BIT 0x0c
249 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
250 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
251 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
252 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
254 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
255 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
256 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
258 #define STLINK_DEBUG_APIV1_ENTER 0x20
259 #define STLINK_DEBUG_EXIT 0x21
260 #define STLINK_DEBUG_READCOREID 0x22
262 #define STLINK_DEBUG_APIV2_ENTER 0x30
263 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
264 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
265 #define STLINK_DEBUG_APIV2_READREG 0x33
266 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
267 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
268 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
270 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
271 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
272 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
274 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
276 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
277 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
278 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
279 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
280 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
281 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
282 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
283 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
284 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
286 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
287 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
289 #define STLINK_APIV3_SET_COM_FREQ 0x61
290 #define STLINK_APIV3_GET_COM_FREQ 0x62
292 #define STLINK_APIV3_GET_VERSION_EX 0xFB
294 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
295 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
296 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
298 #define STLINK_DEBUG_PORT_ACCESS 0xffff
300 #define STLINK_TRACE_SIZE 4096
301 #define STLINK_TRACE_MAX_HZ 2000000
303 #define STLINK_V3_MAX_FREQ_NB 10
305 #define REQUEST_SENSE 0x03
306 #define REQUEST_SENSE_LENGTH 18
309 * Map the relevant features, quirks and workaround for specific firmware
312 #define STLINK_F_HAS_TRACE BIT(0)
313 #define STLINK_F_HAS_SWD_SET_FREQ BIT(1)
314 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(2)
315 #define STLINK_F_HAS_MEM_16BIT BIT(3)
316 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(4)
317 #define STLINK_F_HAS_DAP_REG BIT(5)
318 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(6)
319 #define STLINK_F_HAS_AP_INIT BIT(7)
320 #define STLINK_F_HAS_DPBANKSEL BIT(8)
321 #define STLINK_F_HAS_RW8_512BYTES BIT(9)
322 #define STLINK_F_FIX_CLOSE_AP BIT(10)
325 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
332 /* SWD clock speed */
333 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
335 {1800, 1}, /* default */
348 /* JTAG clock speed */
349 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
353 {1125, 32}, /* default */
359 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
360 static int stlink_swim_status(void *handle
);
361 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
362 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
363 static int stlink_speed(void *handle
, int khz
, bool query
);
364 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
);
367 static unsigned int stlink_usb_block(void *handle
)
369 struct stlink_usb_handle_s
*h
= handle
;
371 assert(handle
!= NULL
);
373 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
374 return STLINKV3_MAX_RW8
;
376 return STLINK_MAX_RW8
;
381 #ifdef USE_LIBUSB_ASYNCIO
383 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
385 int *completed
= transfer
->user_data
;
387 /* caller interprets result and frees transfer */
391 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
393 int r
, *completed
= transfer
->user_data
;
395 /* Assuming a single libusb context exists. There no existing interface into this
396 * module to pass a libusb context.
398 struct libusb_context
*ctx
= NULL
;
400 while (!*completed
) {
401 r
= libusb_handle_events_completed(ctx
, completed
);
403 if (r
== LIBUSB_ERROR_INTERRUPTED
)
405 libusb_cancel_transfer(transfer
);
412 static int transfer_error_status(const struct libusb_transfer
*transfer
)
416 switch (transfer
->status
) {
417 case LIBUSB_TRANSFER_COMPLETED
:
420 case LIBUSB_TRANSFER_TIMED_OUT
:
421 r
= LIBUSB_ERROR_TIMEOUT
;
423 case LIBUSB_TRANSFER_STALL
:
424 r
= LIBUSB_ERROR_PIPE
;
426 case LIBUSB_TRANSFER_OVERFLOW
:
427 r
= LIBUSB_ERROR_OVERFLOW
;
429 case LIBUSB_TRANSFER_NO_DEVICE
:
430 r
= LIBUSB_ERROR_NO_DEVICE
;
432 case LIBUSB_TRANSFER_ERROR
:
433 case LIBUSB_TRANSFER_CANCELLED
:
437 r
= LIBUSB_ERROR_OTHER
;
451 size_t transfer_size
;
452 struct libusb_transfer
*transfer
;
455 static int jtag_libusb_bulk_transfer_n(
456 struct libusb_device_handle
*dev_handle
,
457 struct jtag_xfer
*transfers
,
462 int returnval
= ERROR_OK
;
465 for (size_t i
= 0; i
< n_transfers
; ++i
) {
466 transfers
[i
].retval
= 0;
467 transfers
[i
].completed
= 0;
468 transfers
[i
].transfer_size
= 0;
469 transfers
[i
].transfer
= libusb_alloc_transfer(0);
471 if (transfers
[i
].transfer
== NULL
) {
472 for (size_t j
= 0; j
< i
; ++j
)
473 libusb_free_transfer(transfers
[j
].transfer
);
475 LOG_DEBUG("ERROR, failed to alloc usb transfers");
476 for (size_t k
= 0; k
< n_transfers
; ++k
)
477 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
482 for (size_t i
= 0; i
< n_transfers
; ++i
) {
483 libusb_fill_bulk_transfer(
484 transfers
[i
].transfer
,
486 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
487 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
488 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
490 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
492 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
494 /* Probably no point continuing to submit transfers once a submission fails.
495 * As a result, tag all remaining transfers as errors.
497 for (size_t j
= i
; j
< n_transfers
; ++j
)
498 transfers
[j
].retval
= retval
;
500 returnval
= ERROR_FAIL
;
505 /* Wait for every submitted USB transfer to complete.
507 for (size_t i
= 0; i
< n_transfers
; ++i
) {
508 if (transfers
[i
].retval
== 0) {
509 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
511 retval
= transfer_error_status(transfers
[i
].transfer
);
513 returnval
= ERROR_FAIL
;
514 transfers
[i
].retval
= retval
;
515 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
517 /* Assuming actual_length is only valid if there is no transfer error.
519 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
523 libusb_free_transfer(transfers
[i
].transfer
);
524 transfers
[i
].transfer
= NULL
;
534 static int stlink_usb_xfer_v1_get_status(void *handle
)
536 struct stlink_usb_handle_s
*h
= handle
;
539 assert(handle
!= NULL
);
542 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
544 ret
= jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
545 STLINK_READ_TIMEOUT
, &tr
);
551 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
554 if (t1
!= 0x53425355)
562 if (h
->cmdbuf
[12] != 0)
568 #ifdef USE_LIBUSB_ASYNCIO
569 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
571 struct stlink_usb_handle_s
*h
= handle
;
573 assert(handle
!= NULL
);
575 size_t n_transfers
= 0;
576 struct jtag_xfer transfers
[2];
578 memset(transfers
, 0, sizeof(transfers
));
580 transfers
[0].ep
= h
->tx_ep
;
581 transfers
[0].buf
= h
->cmdbuf
;
582 transfers
[0].size
= cmdsize
;
586 if (h
->direction
== h
->tx_ep
&& size
) {
587 transfers
[1].ep
= h
->tx_ep
;
588 transfers
[1].buf
= (uint8_t *)buf
;
589 transfers
[1].size
= size
;
592 } else if (h
->direction
== h
->rx_ep
&& size
) {
593 transfers
[1].ep
= h
->rx_ep
;
594 transfers
[1].buf
= (uint8_t *)buf
;
595 transfers
[1].size
= size
;
600 return jtag_libusb_bulk_transfer_n(
604 STLINK_WRITE_TIMEOUT
);
607 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
609 struct stlink_usb_handle_s
*h
= handle
;
612 assert(handle
!= NULL
);
614 ret
= jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
615 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
616 if (ret
|| tr
!= cmdsize
)
619 if (h
->direction
== h
->tx_ep
&& size
) {
620 ret
= jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
621 size
, STLINK_WRITE_TIMEOUT
, &tr
);
622 if (ret
|| tr
!= size
) {
623 LOG_DEBUG("bulk write failed");
626 } else if (h
->direction
== h
->rx_ep
&& size
) {
627 ret
= jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
628 size
, STLINK_READ_TIMEOUT
, &tr
);
629 if (ret
|| tr
!= size
) {
630 LOG_DEBUG("bulk read failed");
640 static int stlink_usb_xfer_v1_get_sense(void *handle
)
643 struct stlink_usb_handle_s
*h
= handle
;
645 assert(handle
!= NULL
);
647 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
649 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
650 h
->cmdbuf
[h
->cmdidx
++] = 0;
651 h
->cmdbuf
[h
->cmdidx
++] = 0;
652 h
->cmdbuf
[h
->cmdidx
++] = 0;
653 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
655 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
660 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
667 transfers block in cmdbuf
668 <size> indicates number of bytes in the following
670 Ignore the (eventual) error code in the received packet.
672 static int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
674 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
675 struct stlink_usb_handle_s
*h
= handle
;
677 assert(handle
!= NULL
);
679 if (h
->version
.stlink
== 1) {
680 cmdsize
= STLINK_SG_SIZE
;
681 /* put length in bCBWCBLength */
682 h
->cmdbuf
[14] = h
->cmdidx
-15;
685 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
690 if (h
->version
.stlink
== 1) {
691 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
692 /* check csw status */
693 if (h
->cmdbuf
[12] == 1) {
694 LOG_DEBUG("get sense");
695 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
706 Converts an STLINK status code held in the first byte of a response
707 to an openocd error, logs any error/wait status as debug output.
709 static int stlink_usb_error_check(void *handle
)
711 struct stlink_usb_handle_s
*h
= handle
;
713 assert(handle
!= NULL
);
715 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
716 switch (h
->databuf
[0]) {
717 case STLINK_SWIM_ERR_OK
:
719 case STLINK_SWIM_BUSY
:
722 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
727 /* TODO: no error checking yet on api V1 */
728 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
729 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
731 switch (h
->databuf
[0]) {
732 case STLINK_DEBUG_ERR_OK
:
734 case STLINK_DEBUG_ERR_FAULT
:
735 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
737 case STLINK_SWD_AP_WAIT
:
738 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
740 case STLINK_SWD_DP_WAIT
:
741 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
743 case STLINK_JTAG_GET_IDCODE_ERROR
:
744 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
746 case STLINK_JTAG_WRITE_ERROR
:
747 LOG_DEBUG("Write error");
749 case STLINK_JTAG_WRITE_VERIF_ERROR
:
750 LOG_DEBUG("Write verify error, ignoring");
752 case STLINK_SWD_AP_FAULT
:
753 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
754 * returns ERROR_OK with the comment:
755 * Change in error status when reading outside RAM.
756 * This fix allows CDT plugin to visualize memory.
758 LOG_DEBUG("STLINK_SWD_AP_FAULT");
760 case STLINK_SWD_AP_ERROR
:
761 LOG_DEBUG("STLINK_SWD_AP_ERROR");
763 case STLINK_SWD_AP_PARITY_ERROR
:
764 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
766 case STLINK_SWD_DP_FAULT
:
767 LOG_DEBUG("STLINK_SWD_DP_FAULT");
769 case STLINK_SWD_DP_ERROR
:
770 LOG_DEBUG("STLINK_SWD_DP_ERROR");
772 case STLINK_SWD_DP_PARITY_ERROR
:
773 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
775 case STLINK_SWD_AP_WDATA_ERROR
:
776 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
778 case STLINK_SWD_AP_STICKY_ERROR
:
779 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
781 case STLINK_SWD_AP_STICKYORUN_ERROR
:
782 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
784 case STLINK_BAD_AP_ERROR
:
785 LOG_DEBUG("STLINK_BAD_AP_ERROR");
788 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
794 * Wrapper around stlink_usb_xfer_noerrcheck()
795 * to check the error code in the received packet
797 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
803 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
804 if (retval
!= ERROR_OK
)
807 return stlink_usb_error_check(handle
);
810 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
812 Works for commands where the STLINK_DEBUG status is returned in the first
813 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
815 Returns an openocd result code.
817 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
821 struct stlink_usb_handle_s
*h
= handle
;
824 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
825 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
830 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
831 res
= stlink_swim_status(handle
);
836 res
= stlink_usb_error_check(handle
);
837 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
838 unsigned int delay_us
= (1<<retries
++) * 1000;
839 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
848 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
850 struct stlink_usb_handle_s
*h
= handle
;
853 assert(handle
!= NULL
);
855 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
857 ret
= jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
, size
,
858 STLINK_READ_TIMEOUT
, &tr
);
859 if (ret
|| tr
!= size
) {
860 LOG_ERROR("bulk trace read failed");
868 this function writes transfer length in
869 the right place in the cb
871 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
873 struct stlink_usb_handle_s
*h
= handle
;
875 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
878 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
880 struct stlink_usb_handle_s
*h
= handle
;
882 /* fill the send buffer */
883 strcpy((char *)h
->cmdbuf
, "USBC");
885 /* csw tag not used */
886 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
888 /* cbw data transfer length (in the following data phase in or out) */
889 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
892 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
893 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
894 /* cdb clength (is filled in at xfer) */
895 h
->cmdbuf
[h
->cmdidx
++] = 0;
899 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
901 struct stlink_usb_handle_s
*h
= handle
;
903 h
->direction
= direction
;
907 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
908 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
910 if (h
->version
.stlink
== 1)
911 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
915 static int stlink_usb_version(void *handle
)
920 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
921 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
923 struct stlink_usb_handle_s
*h
= handle
;
925 assert(handle
!= NULL
);
927 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
929 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
931 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
936 version
= be_to_h_u16(h
->databuf
);
937 v
= (version
>> 12) & 0x0f;
938 x
= (version
>> 6) & 0x3f;
941 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
942 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
945 case STLINK_V2_1_PID
:
946 case STLINK_V2_1_NO_MSD_PID
:
947 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
948 /* MxSy : STM8 V2.1 - SWIM only */
953 /* JxMy : STM32 V2.1 - JTAG/SWD only */
966 /* STLINK-V3 requires a specific command */
967 if (v
== 3 && x
== 0 && y
== 0) {
968 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
970 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
972 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
977 swim
= h
->databuf
[1];
978 jtag
= h
->databuf
[2];
980 bridge
= h
->databuf
[4];
981 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
982 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
985 h
->version
.stlink
= v
;
986 h
->version
.jtag
= jtag
;
987 h
->version
.swim
= swim
;
990 switch (h
->version
.stlink
) {
992 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
993 if (h
->version
.jtag
>= 11)
994 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
996 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
1000 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1001 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1003 /* API for trace from J13 */
1004 /* API for target voltage from J13 */
1005 if (h
->version
.jtag
>= 13)
1006 flags
|= STLINK_F_HAS_TRACE
;
1008 /* preferred API to get last R/W status from J15 */
1009 if (h
->version
.jtag
>= 15)
1010 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1012 /* API to set SWD frequency from J22 */
1013 if (h
->version
.jtag
>= 22)
1014 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1016 /* API to set JTAG frequency from J24 */
1017 /* API to access DAP registers from J24 */
1018 if (h
->version
.jtag
>= 24) {
1019 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1020 flags
|= STLINK_F_HAS_DAP_REG
;
1023 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1024 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1025 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1027 /* API to read/write memory at 16 bit from J26 */
1028 if (h
->version
.jtag
>= 26)
1029 flags
|= STLINK_F_HAS_MEM_16BIT
;
1031 /* API required to init AP before any AP access from J28 */
1032 if (h
->version
.jtag
>= 28)
1033 flags
|= STLINK_F_HAS_AP_INIT
;
1035 /* API required to return proper error code on close AP from J29 */
1036 if (h
->version
.jtag
>= 29)
1037 flags
|= STLINK_F_FIX_CLOSE_AP
;
1039 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1040 if (h
->version
.jtag
>= 32)
1041 flags
|= STLINK_F_HAS_DPBANKSEL
;
1045 /* all STLINK-V3 use api-v3 */
1046 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1048 /* STLINK-V3 is a superset of ST-LINK/V2 */
1051 /* API for target voltage */
1052 flags
|= STLINK_F_HAS_TRACE
;
1054 /* preferred API to get last R/W status */
1055 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1057 /* API to access DAP registers */
1058 flags
|= STLINK_F_HAS_DAP_REG
;
1060 /* API to read/write memory at 16 bit */
1061 flags
|= STLINK_F_HAS_MEM_16BIT
;
1063 /* API required to init AP before any AP access */
1064 flags
|= STLINK_F_HAS_AP_INIT
;
1066 /* API required to return proper error code on close AP */
1067 flags
|= STLINK_F_FIX_CLOSE_AP
;
1069 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1070 if (h
->version
.jtag
>= 2)
1071 flags
|= STLINK_F_HAS_DPBANKSEL
;
1073 /* 8bit read/write max packet size 512 bytes from V3J6 */
1074 if (h
->version
.jtag
>= 6)
1075 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1081 h
->version
.flags
= flags
;
1084 p
+= sprintf(p
, "V%d", v
);
1086 p
+= sprintf(p
, "J%d", jtag
);
1088 p
+= sprintf(p
, "M%d", msd
);
1090 p
+= sprintf(p
, "B%d", bridge
);
1092 sprintf(p
, "S%d", swim
);
1094 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1096 h
->version
.jtag_api
,
1103 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1105 struct stlink_usb_handle_s
*h
= handle
;
1106 uint32_t adc_results
[2];
1108 /* no error message, simply quit with error */
1109 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1110 return ERROR_COMMAND_NOTFOUND
;
1112 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1114 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1116 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1118 if (result
!= ERROR_OK
)
1121 /* convert result */
1122 adc_results
[0] = le_to_h_u32(h
->databuf
);
1123 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1125 *target_voltage
= 0;
1128 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1130 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1135 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1137 struct stlink_usb_handle_s
*h
= handle
;
1139 assert(handle
!= NULL
);
1141 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1142 return ERROR_COMMAND_NOTFOUND
;
1144 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1146 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1147 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1148 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1151 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1153 if (result
!= ERROR_OK
)
1159 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1161 struct stlink_usb_handle_s
*h
= handle
;
1163 assert(handle
!= NULL
);
1165 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1166 return ERROR_COMMAND_NOTFOUND
;
1168 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1171 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1172 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1175 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1177 if (result
!= ERROR_OK
)
1184 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1187 struct stlink_usb_handle_s
*h
= handle
;
1189 assert(handle
!= NULL
);
1191 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1193 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1195 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1197 if (res
!= ERROR_OK
)
1200 *mode
= h
->databuf
[0];
1206 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1209 struct stlink_usb_handle_s
*h
= handle
;
1211 assert(handle
!= NULL
);
1213 /* on api V2 we are able the read the latest command
1215 * TODO: we need the test on api V1 too
1217 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1220 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1223 case STLINK_MODE_DEBUG_JTAG
:
1224 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1225 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1226 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1228 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1229 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1231 case STLINK_MODE_DEBUG_SWD
:
1232 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1233 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1234 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1236 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1237 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1239 case STLINK_MODE_DEBUG_SWIM
:
1240 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1241 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1242 /* swim enter does not return any response or status */
1243 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1244 case STLINK_MODE_DFU
:
1245 case STLINK_MODE_MASS
:
1250 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1254 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1257 struct stlink_usb_handle_s
*h
= handle
;
1259 assert(handle
!= NULL
);
1261 /* command with no reply, use a valid endpoint but zero size */
1262 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1265 case STLINK_MODE_DEBUG_JTAG
:
1266 case STLINK_MODE_DEBUG_SWD
:
1267 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1268 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1270 case STLINK_MODE_DEBUG_SWIM
:
1271 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1272 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1274 case STLINK_MODE_DFU
:
1275 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1276 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1278 case STLINK_MODE_MASS
:
1283 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1285 if (res
!= ERROR_OK
)
1291 static int stlink_usb_assert_srst(void *handle
, int srst
);
1293 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1296 case HL_TRANSPORT_SWD
:
1297 return STLINK_MODE_DEBUG_SWD
;
1298 case HL_TRANSPORT_JTAG
:
1299 return STLINK_MODE_DEBUG_JTAG
;
1301 return STLINK_MODE_UNKNOWN
;
1306 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1310 enum stlink_mode emode
;
1311 struct stlink_usb_handle_s
*h
= handle
;
1313 assert(handle
!= NULL
);
1315 res
= stlink_usb_current_mode(handle
, &mode
);
1317 if (res
!= ERROR_OK
)
1320 LOG_DEBUG("MODE: 0x%02X", mode
);
1322 /* try to exit current mode */
1324 case STLINK_DEV_DFU_MODE
:
1325 emode
= STLINK_MODE_DFU
;
1327 case STLINK_DEV_DEBUG_MODE
:
1328 emode
= STLINK_MODE_DEBUG_SWD
;
1330 case STLINK_DEV_SWIM_MODE
:
1331 emode
= STLINK_MODE_DEBUG_SWIM
;
1333 case STLINK_DEV_BOOTLOADER_MODE
:
1334 case STLINK_DEV_MASS_MODE
:
1336 emode
= STLINK_MODE_UNKNOWN
;
1340 if (emode
!= STLINK_MODE_UNKNOWN
) {
1341 res
= stlink_usb_mode_leave(handle
, emode
);
1343 if (res
!= ERROR_OK
)
1347 res
= stlink_usb_current_mode(handle
, &mode
);
1349 if (res
!= ERROR_OK
)
1352 /* we check the target voltage here as an aid to debugging connection problems.
1353 * the stlink requires the target Vdd to be connected for reliable debugging.
1354 * this cmd is supported in all modes except DFU
1356 if (mode
!= STLINK_DEV_DFU_MODE
) {
1358 float target_voltage
;
1360 /* check target voltage (if supported) */
1361 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1363 if (res
!= ERROR_OK
) {
1364 if (res
!= ERROR_COMMAND_NOTFOUND
)
1365 LOG_ERROR("voltage check failed");
1366 /* attempt to continue as it is not a catastrophic failure */
1368 /* check for a sensible target voltage, operating range is 1.65-5.5v
1369 * according to datasheet */
1370 if (target_voltage
< 1.5)
1371 LOG_ERROR("target voltage may be too low for reliable debugging");
1375 LOG_DEBUG("MODE: 0x%02X", mode
);
1377 /* set selected mode */
1380 if (emode
== STLINK_MODE_UNKNOWN
) {
1381 LOG_ERROR("selected mode (transport) not supported");
1385 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1386 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1387 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1388 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1389 stlink_speed(h
, initial_interface_speed
, false);
1391 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1392 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1393 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1394 stlink_speed(h
, initial_interface_speed
, false);
1398 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
1399 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1401 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1402 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1403 stlink_speed(h
, initial_interface_speed
, false);
1406 /* preliminary SRST assert:
1407 * We want SRST is asserted before activating debug signals (mode_enter).
1408 * As the required mode has not been set, the adapter may not know what pin to use.
1409 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1410 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1411 * after power on, SWIM_RST stays unchanged */
1412 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1413 stlink_usb_assert_srst(handle
, 0);
1414 /* do not check the return status here, we will
1415 proceed and enter the desired mode below
1416 and try asserting srst again. */
1418 res
= stlink_usb_mode_enter(handle
, emode
);
1419 if (res
!= ERROR_OK
)
1422 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1423 if (connect_under_reset
) {
1424 res
= stlink_usb_assert_srst(handle
, 0);
1425 if (res
!= ERROR_OK
)
1429 res
= stlink_usb_current_mode(handle
, &mode
);
1431 if (res
!= ERROR_OK
)
1434 LOG_DEBUG("MODE: 0x%02X", mode
);
1439 /* request status from last swim request */
1440 static int stlink_swim_status(void *handle
)
1442 struct stlink_usb_handle_s
*h
= handle
;
1445 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1446 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1447 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1448 /* error is checked by the caller */
1449 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1450 if (res
!= ERROR_OK
)
1455 the purpose of this function is unknown...
1456 capabilites? anyway for swim v6 it returns
1459 __attribute__((unused
))
1460 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1462 struct stlink_usb_handle_s
*h
= handle
;
1465 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1466 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1467 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1468 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1469 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1470 if (res
!= ERROR_OK
)
1472 memcpy(cap
, h
->databuf
, 8);
1476 /* debug dongle assert/deassert sreset line */
1477 static int stlink_swim_assert_reset(void *handle
, int reset
)
1479 struct stlink_usb_handle_s
*h
= handle
;
1482 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1483 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1485 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1487 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1488 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1489 if (res
!= ERROR_OK
)
1496 1.3ms low then 750Hz then 1.5kHz
1498 static int stlink_swim_enter(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_ENTER_SEQ
;
1506 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1507 if (res
!= ERROR_OK
)
1512 /* switch high/low speed swim */
1513 static int stlink_swim_speed(void *handle
, int speed
)
1515 struct stlink_usb_handle_s
*h
= handle
;
1518 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1519 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1520 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1522 h
->cmdbuf
[h
->cmdidx
++] = 1;
1524 h
->cmdbuf
[h
->cmdidx
++] = 0;
1525 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1526 if (res
!= ERROR_OK
)
1532 initiate srst from swim.
1533 nrst is pulled low for 50us.
1535 static int stlink_swim_generate_rst(void *handle
)
1537 struct stlink_usb_handle_s
*h
= handle
;
1540 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1541 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1542 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1543 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1544 if (res
!= ERROR_OK
)
1550 send resyncronize sequence
1551 swim is pulled low for 16us
1552 reply is 64 clks low
1554 static int stlink_swim_resync(void *handle
)
1556 struct stlink_usb_handle_s
*h
= handle
;
1559 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1560 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1561 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1562 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1563 if (res
!= ERROR_OK
)
1568 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1570 struct stlink_usb_handle_s
*h
= handle
;
1573 unsigned int datalen
= 0;
1574 int cmdsize
= STLINK_CMD_SIZE_V2
;
1576 if (len
> STLINK_DATA_SIZE
)
1579 if (h
->version
.stlink
== 1)
1580 cmdsize
= STLINK_SG_SIZE
;
1582 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1583 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1584 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1585 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1587 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1589 for (i
= 0; i
< len
; i
++) {
1590 if (h
->cmdidx
== cmdsize
)
1591 h
->databuf
[datalen
++] = *(data
++);
1593 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1595 if (h
->version
.stlink
== 1)
1596 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1598 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1599 if (res
!= ERROR_OK
)
1604 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1606 struct stlink_usb_handle_s
*h
= handle
;
1609 if (len
> STLINK_DATA_SIZE
)
1612 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1613 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1614 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1615 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1617 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1619 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1620 if (res
!= ERROR_OK
)
1623 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1625 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1626 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1627 if (res
!= ERROR_OK
)
1634 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1637 struct stlink_usb_handle_s
*h
= handle
;
1639 assert(handle
!= NULL
);
1641 /* there is no swim read core id cmd */
1642 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1647 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1649 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1650 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1651 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1653 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1656 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1658 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1662 if (res
!= ERROR_OK
)
1665 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1667 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1672 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1674 struct stlink_usb_handle_s
*h
= handle
;
1677 assert(handle
!= NULL
);
1679 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1681 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1682 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1683 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1686 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1687 if (res
!= ERROR_OK
)
1690 *val
= le_to_h_u32(h
->databuf
+ 4);
1694 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1696 struct stlink_usb_handle_s
*h
= handle
;
1698 assert(handle
!= NULL
);
1700 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1702 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1703 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1704 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1706 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1707 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1709 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1712 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1716 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1718 struct stlink_usb_handle_s
*h
= handle
;
1720 assert(handle
!= NULL
);
1722 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1725 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1727 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1728 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1730 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1731 if (res
!= ERROR_OK
)
1734 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1735 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1738 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1739 if (res
!= ERROR_OK
)
1748 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1753 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1754 if (result
!= ERROR_OK
)
1755 return TARGET_UNKNOWN
;
1757 if (status
& S_HALT
)
1758 return TARGET_HALTED
;
1759 else if (status
& S_RESET_ST
)
1760 return TARGET_RESET
;
1762 return TARGET_RUNNING
;
1766 static enum target_state
stlink_usb_state(void *handle
)
1769 struct stlink_usb_handle_s
*h
= handle
;
1771 assert(handle
!= NULL
);
1773 if (h
->reconnect_pending
) {
1774 LOG_INFO("Previous state query failed, trying to reconnect");
1775 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
1776 if (res
!= ERROR_OK
)
1777 return TARGET_UNKNOWN
;
1779 h
->reconnect_pending
= false;
1782 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1783 res
= stlink_usb_v2_get_status(handle
);
1784 if (res
== TARGET_UNKNOWN
)
1785 h
->reconnect_pending
= true;
1789 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1791 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1792 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1794 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1796 if (res
!= ERROR_OK
)
1797 return TARGET_UNKNOWN
;
1799 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1800 return TARGET_RUNNING
;
1801 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1802 return TARGET_HALTED
;
1804 h
->reconnect_pending
= true;
1806 return TARGET_UNKNOWN
;
1809 static int stlink_usb_assert_srst(void *handle
, int srst
)
1811 struct stlink_usb_handle_s
*h
= handle
;
1813 assert(handle
!= NULL
);
1815 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
1816 return stlink_swim_assert_reset(handle
, srst
);
1818 if (h
->version
.stlink
== 1)
1819 return ERROR_COMMAND_NOTFOUND
;
1821 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1823 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1824 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1825 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1827 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1831 static void stlink_usb_trace_disable(void *handle
)
1834 struct stlink_usb_handle_s
*h
= handle
;
1836 assert(handle
!= NULL
);
1838 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1840 LOG_DEBUG("Tracing: disable");
1842 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1843 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1844 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1845 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1847 if (res
== ERROR_OK
)
1848 h
->trace
.enabled
= false;
1853 static int stlink_usb_trace_enable(void *handle
)
1856 struct stlink_usb_handle_s
*h
= handle
;
1858 assert(handle
!= NULL
);
1860 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1861 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1863 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1864 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1865 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1867 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1870 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1872 if (res
== ERROR_OK
) {
1873 h
->trace
.enabled
= true;
1874 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1877 LOG_ERROR("Tracing is not supported by this version.");
1885 static int stlink_usb_reset(void *handle
)
1887 struct stlink_usb_handle_s
*h
= handle
;
1890 assert(handle
!= NULL
);
1892 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1894 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1896 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1897 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1899 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1901 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1902 if (retval
!= ERROR_OK
)
1905 if (h
->trace
.enabled
) {
1906 stlink_usb_trace_disable(h
);
1907 return stlink_usb_trace_enable(h
);
1914 static int stlink_usb_run(void *handle
)
1917 struct stlink_usb_handle_s
*h
= handle
;
1919 assert(handle
!= NULL
);
1921 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1922 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1927 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1929 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1930 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1932 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1936 static int stlink_usb_halt(void *handle
)
1939 struct stlink_usb_handle_s
*h
= handle
;
1941 assert(handle
!= NULL
);
1943 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1944 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1949 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1951 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1952 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1954 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1958 static int stlink_usb_step(void *handle
)
1960 struct stlink_usb_handle_s
*h
= handle
;
1962 assert(handle
!= NULL
);
1964 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1965 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1966 * that the Cortex-M3 currently does. */
1967 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1968 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1969 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1972 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1974 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1975 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1977 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1981 static int stlink_usb_read_regs(void *handle
)
1984 struct stlink_usb_handle_s
*h
= handle
;
1986 assert(handle
!= NULL
);
1988 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1990 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1991 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1993 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1994 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
1995 /* regs data from offset 0 */
1997 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1998 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
1999 /* status at offset 0, regs data from offset 4 */
2006 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
2009 struct stlink_usb_handle_s
*h
= handle
;
2011 assert(handle
!= NULL
);
2013 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2015 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2016 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2017 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2019 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2020 h
->cmdbuf
[h
->cmdidx
++] = num
;
2022 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2023 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2024 if (res
!= ERROR_OK
)
2026 *val
= le_to_h_u32(h
->databuf
);
2029 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2030 if (res
!= ERROR_OK
)
2032 *val
= le_to_h_u32(h
->databuf
+ 4);
2038 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
2040 struct stlink_usb_handle_s
*h
= handle
;
2042 assert(handle
!= NULL
);
2044 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2046 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2047 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2048 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2050 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2051 h
->cmdbuf
[h
->cmdidx
++] = num
;
2052 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2055 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2058 static int stlink_usb_get_rw_status(void *handle
)
2060 struct stlink_usb_handle_s
*h
= handle
;
2062 assert(handle
!= NULL
);
2064 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2067 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2069 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2070 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2071 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2072 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2074 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2075 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2080 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2084 uint16_t read_len
= len
;
2085 struct stlink_usb_handle_s
*h
= handle
;
2087 assert(handle
!= NULL
);
2089 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2090 if (len
> stlink_usb_block(h
)) {
2091 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2095 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2097 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2098 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2099 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2101 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2104 /* we need to fix read length for single bytes */
2108 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2110 if (res
!= ERROR_OK
)
2113 memcpy(buffer
, h
->databuf
, len
);
2115 return stlink_usb_get_rw_status(handle
);
2119 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2120 const uint8_t *buffer
)
2123 struct stlink_usb_handle_s
*h
= handle
;
2125 assert(handle
!= NULL
);
2127 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2128 if (len
> stlink_usb_block(h
)) {
2129 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
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_WRITEMEM_8BIT
;
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_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2155 struct stlink_usb_handle_s
*h
= handle
;
2157 assert(handle
!= NULL
);
2159 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2160 return ERROR_COMMAND_NOTFOUND
;
2162 /* data must be a multiple of 2 and half-word aligned */
2163 if (len
% 2 || addr
% 2) {
2164 LOG_DEBUG("Invalid data alignment");
2165 return ERROR_TARGET_UNALIGNED_ACCESS
;
2168 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2171 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2172 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2174 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2177 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2179 if (res
!= ERROR_OK
)
2182 memcpy(buffer
, h
->databuf
, len
);
2184 return stlink_usb_get_rw_status(handle
);
2188 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2189 const uint8_t *buffer
)
2192 struct stlink_usb_handle_s
*h
= handle
;
2194 assert(handle
!= NULL
);
2196 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2197 return ERROR_COMMAND_NOTFOUND
;
2199 /* data must be a multiple of 2 and half-word aligned */
2200 if (len
% 2 || addr
% 2) {
2201 LOG_DEBUG("Invalid data alignment");
2202 return ERROR_TARGET_UNALIGNED_ACCESS
;
2205 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2207 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2208 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2209 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2211 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2214 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2216 if (res
!= ERROR_OK
)
2219 return stlink_usb_get_rw_status(handle
);
2223 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2227 struct stlink_usb_handle_s
*h
= handle
;
2229 assert(handle
!= NULL
);
2231 /* data must be a multiple of 4 and word aligned */
2232 if (len
% 4 || addr
% 4) {
2233 LOG_DEBUG("Invalid data alignment");
2234 return ERROR_TARGET_UNALIGNED_ACCESS
;
2237 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2239 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2240 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2241 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2243 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2246 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2248 if (res
!= ERROR_OK
)
2251 memcpy(buffer
, h
->databuf
, len
);
2253 return stlink_usb_get_rw_status(handle
);
2257 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2258 const uint8_t *buffer
)
2261 struct stlink_usb_handle_s
*h
= handle
;
2263 assert(handle
!= NULL
);
2265 /* data must be a multiple of 4 and word aligned */
2266 if (len
% 4 || addr
% 4) {
2267 LOG_DEBUG("Invalid data alignment");
2268 return ERROR_TARGET_UNALIGNED_ACCESS
;
2271 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2273 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2274 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2275 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2277 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2280 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2282 if (res
!= ERROR_OK
)
2285 return stlink_usb_get_rw_status(handle
);
2288 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2290 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2291 if (max_tar_block
== 0)
2293 return max_tar_block
;
2296 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2297 uint32_t count
, uint8_t *buffer
)
2299 int retval
= ERROR_OK
;
2300 uint32_t bytes_remaining
;
2302 struct stlink_usb_handle_s
*h
= handle
;
2304 /* calculate byte count */
2307 /* switch to 8 bit if stlink does not support 16 bit memory read */
2308 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2313 bytes_remaining
= (size
!= 1) ?
2314 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2316 if (count
< bytes_remaining
)
2317 bytes_remaining
= count
;
2320 * all stlink support 8/32bit memory read/writes and only from
2321 * stlink V2J26 there is support for 16 bit memory read/write.
2322 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2327 /* When in jtag mode the stlink uses the auto-increment functionality.
2328 * However it expects us to pass the data correctly, this includes
2329 * alignment and any page boundaries. We already do this as part of the
2330 * adi_v5 implementation, but the stlink is a hla adapter and so this
2331 * needs implementing manually.
2332 * currently this only affects jtag mode, according to ST they do single
2333 * access in SWD mode - but this may change and so we do it for both modes */
2335 /* we first need to check for any unaligned bytes */
2336 if (addr
& (size
- 1)) {
2338 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2339 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2340 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2341 usleep((1<<retries
++) * 1000);
2344 if (retval
!= ERROR_OK
)
2346 buffer
+= head_bytes
;
2348 count
-= head_bytes
;
2349 bytes_remaining
-= head_bytes
;
2352 if (bytes_remaining
& (size
- 1))
2353 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2355 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2357 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2359 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2361 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2362 usleep((1<<retries
++) * 1000);
2365 if (retval
!= ERROR_OK
)
2368 buffer
+= bytes_remaining
;
2369 addr
+= bytes_remaining
;
2370 count
-= bytes_remaining
;
2376 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2377 uint32_t count
, const uint8_t *buffer
)
2379 int retval
= ERROR_OK
;
2380 uint32_t bytes_remaining
;
2382 struct stlink_usb_handle_s
*h
= handle
;
2384 /* calculate byte count */
2387 /* switch to 8 bit if stlink does not support 16 bit memory read */
2388 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2393 bytes_remaining
= (size
!= 1) ?
2394 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2396 if (count
< bytes_remaining
)
2397 bytes_remaining
= count
;
2400 * all stlink support 8/32bit memory read/writes and only from
2401 * stlink V2J26 there is support for 16 bit memory read/write.
2402 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2407 /* When in jtag mode the stlink uses the auto-increment functionality.
2408 * However it expects us to pass the data correctly, this includes
2409 * alignment and any page boundaries. We already do this as part of the
2410 * adi_v5 implementation, but the stlink is a hla adapter and so this
2411 * needs implementing manually.
2412 * currently this only affects jtag mode, according to ST they do single
2413 * access in SWD mode - but this may change and so we do it for both modes */
2415 /* we first need to check for any unaligned bytes */
2416 if (addr
& (size
- 1)) {
2418 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2419 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2420 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2421 usleep((1<<retries
++) * 1000);
2424 if (retval
!= ERROR_OK
)
2426 buffer
+= head_bytes
;
2428 count
-= head_bytes
;
2429 bytes_remaining
-= head_bytes
;
2432 if (bytes_remaining
& (size
- 1))
2433 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2435 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2437 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2440 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2441 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2442 usleep((1<<retries
++) * 1000);
2445 if (retval
!= ERROR_OK
)
2448 buffer
+= bytes_remaining
;
2449 addr
+= bytes_remaining
;
2450 count
-= bytes_remaining
;
2457 static int stlink_usb_override_target(const char *targetname
)
2459 return !strcmp(targetname
, "cortex_m");
2462 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2467 we only have low and high speed...
2468 before changing speed the SWIM_CSR HS bit
2472 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2473 if (retval
!= ERROR_OK
)
2474 LOG_ERROR("Unable to set adapter speed");
2477 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2480 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2483 int speed_index
= -1;
2484 int speed_diff
= INT_MAX
;
2485 int last_valid_speed
= -1;
2488 for (i
= 0; i
< map_size
; i
++) {
2491 last_valid_speed
= i
;
2492 if (khz
== map
[i
].speed
) {
2496 int current_diff
= khz
- map
[i
].speed
;
2497 /* get abs value for comparison */
2498 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2499 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2500 speed_diff
= current_diff
;
2506 if (speed_index
== -1) {
2507 /* this will only be here if we cannot match the slow speed.
2508 * use the slowest speed we support.*/
2509 speed_index
= last_valid_speed
;
2511 } else if (i
== map_size
)
2514 if (!match
&& query
) {
2515 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
2516 khz
, map
[speed_index
].speed
);
2522 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2525 struct stlink_usb_handle_s
*h
= handle
;
2527 /* old firmware cannot change it */
2528 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2531 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2532 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2535 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2536 if (result
!= ERROR_OK
) {
2537 LOG_ERROR("Unable to set adapter speed");
2542 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2545 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2548 struct stlink_usb_handle_s
*h
= handle
;
2550 /* old firmware cannot change it */
2551 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2554 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2555 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2558 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2559 if (result
!= ERROR_OK
) {
2560 LOG_ERROR("Unable to set adapter speed");
2565 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2568 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2572 LOG_DEBUG("Supported clock speeds are:");
2573 for (i
= 0; i
< map_size
; i
++)
2575 LOG_DEBUG("%d kHz", map
[i
].speed
);
2578 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2580 struct stlink_usb_handle_s
*h
= handle
;
2583 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2584 LOG_ERROR("Unknown command");
2588 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2590 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2591 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2592 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2594 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2596 int size
= h
->databuf
[8];
2598 if (size
> STLINK_V3_MAX_FREQ_NB
)
2599 size
= STLINK_V3_MAX_FREQ_NB
;
2601 for (i
= 0; i
< size
; i
++) {
2602 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2603 map
[i
].speed_divisor
= i
;
2606 /* set to zero all the next entries */
2607 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2613 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2615 struct stlink_usb_handle_s
*h
= handle
;
2617 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2618 LOG_ERROR("Unknown command");
2622 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2625 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2626 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2627 h
->cmdbuf
[h
->cmdidx
++] = 0;
2629 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2631 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2634 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2636 struct stlink_usb_handle_s
*h
= handle
;
2638 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2640 stlink_get_com_freq(h
, is_jtag
, map
);
2642 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2645 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2646 if (result
!= ERROR_OK
) {
2647 LOG_ERROR("Unable to set adapter speed");
2651 return map
[speed_index
].speed
;
2654 static int stlink_speed(void *handle
, int khz
, bool query
)
2656 struct stlink_usb_handle_s
*h
= handle
;
2661 switch (h
->st_mode
) {
2662 case STLINK_MODE_DEBUG_SWIM
:
2663 return stlink_speed_swim(handle
, khz
, query
);
2664 case STLINK_MODE_DEBUG_SWD
:
2665 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2666 return stlink_speed_v3(handle
, false, khz
, query
);
2668 return stlink_speed_swd(handle
, khz
, query
);
2670 case STLINK_MODE_DEBUG_JTAG
:
2671 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2672 return stlink_speed_v3(handle
, true, khz
, query
);
2674 return stlink_speed_jtag(handle
, khz
, query
);
2684 static int stlink_usb_close(void *handle
)
2688 enum stlink_mode emode
;
2689 struct stlink_usb_handle_s
*h
= handle
;
2692 res
= stlink_usb_current_mode(handle
, &mode
);
2695 /* do not exit if return code != ERROR_OK,
2696 it prevents us from closing jtag_libusb */
2698 if (res
== ERROR_OK
) {
2699 /* try to exit current mode */
2701 case STLINK_DEV_DFU_MODE
:
2702 emode
= STLINK_MODE_DFU
;
2704 case STLINK_DEV_DEBUG_MODE
:
2705 emode
= STLINK_MODE_DEBUG_SWD
;
2707 case STLINK_DEV_SWIM_MODE
:
2708 emode
= STLINK_MODE_DEBUG_SWIM
;
2710 case STLINK_DEV_BOOTLOADER_MODE
:
2711 case STLINK_DEV_MASS_MODE
:
2713 emode
= STLINK_MODE_UNKNOWN
;
2717 if (emode
!= STLINK_MODE_UNKNOWN
)
2718 stlink_usb_mode_leave(handle
, emode
);
2719 /* do not check return code, it prevent
2720 us from closing jtag_libusb */
2724 jtag_libusb_close(h
->fd
);
2731 /* Compute ST-Link serial number from the device descriptor
2732 * this function will help to work-around a bug in old ST-Link/V2 DFU
2733 * the buggy DFU returns an incorrect serial in the USB descriptor
2734 * example for the following serial "57FF72067265575742132067"
2735 * - the correct descriptor serial is:
2736 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
2737 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
2738 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
2739 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
2740 * so this case is managed by libusb_helper::string_descriptor_equal
2741 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
2742 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
2744 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
2745 * and then we have just to convert the raw data into printable characters using sprintf
2747 char *stlink_usb_get_alternate_serial(libusb_device_handle
*device
,
2748 struct libusb_device_descriptor
*dev_desc
)
2751 unsigned char desc_serial
[(STLINK_SERIAL_LEN
+ 1) * 2];
2753 if (dev_desc
->iSerialNumber
== 0)
2756 /* get the LANGID from String Descriptor Zero */
2757 usb_retval
= libusb_get_string_descriptor(device
, 0, 0, desc_serial
,
2758 sizeof(desc_serial
));
2760 if (usb_retval
< LIBUSB_SUCCESS
) {
2761 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2762 libusb_error_name(usb_retval
), usb_retval
);
2764 } else if (usb_retval
< 4) {
2765 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
2766 LOG_ERROR("could not get the LANGID");
2770 uint32_t langid
= desc_serial
[2] | (desc_serial
[3] << 8);
2772 /* get the serial */
2773 usb_retval
= libusb_get_string_descriptor(device
, dev_desc
->iSerialNumber
,
2774 langid
, desc_serial
, sizeof(desc_serial
));
2776 unsigned char len
= desc_serial
[0];
2778 if (usb_retval
< LIBUSB_SUCCESS
) {
2779 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2780 libusb_error_name(usb_retval
), usb_retval
);
2782 } else if (desc_serial
[1] != LIBUSB_DT_STRING
|| len
> usb_retval
) {
2783 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
2787 if (len
== ((STLINK_SERIAL_LEN
+ 1) * 2)) {
2788 /* good ST-Link adapter, this case is managed by
2789 * libusb::libusb_get_string_descriptor_ascii */
2791 } else if (len
!= ((STLINK_SERIAL_LEN
/ 2 + 1) * 2)) {
2792 LOG_ERROR("unexpected serial length (%d) in descriptor", len
);
2796 /* else (len == 26) => buggy ST-Link */
2798 char *alternate_serial
= malloc((STLINK_SERIAL_LEN
+ 1) * sizeof(char));
2799 if (alternate_serial
== NULL
)
2802 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
2803 sprintf(alternate_serial
+ i
, "%02X", desc_serial
[i
+ 2]);
2805 alternate_serial
[STLINK_SERIAL_LEN
] = '\0';
2807 return alternate_serial
;
2811 static int stlink_usb_open(struct hl_interface_param_s
*param
, enum stlink_mode mode
, void **fd
)
2813 int err
, retry_count
= 1;
2814 struct stlink_usb_handle_s
*h
;
2816 LOG_DEBUG("stlink_usb_open");
2818 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2821 LOG_DEBUG("malloc failed");
2827 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2828 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2829 h
->st_mode
, param
->vid
[i
], param
->pid
[i
],
2830 param
->serial
? param
->serial
: "");
2834 On certain host USB configurations(e.g. MacBook Air)
2835 STLINKv2 dongle seems to have its FW in a funky state if,
2836 after plugging it in, you try to use openocd with it more
2837 then once (by launching and closing openocd). In cases like
2838 that initial attempt to read the FW info via
2839 stlink_usb_version will fail and the device has to be reset
2840 in order to become operational.
2843 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
,
2844 &h
->fd
, stlink_usb_get_alternate_serial
) != ERROR_OK
) {
2845 LOG_ERROR("open failed");
2849 jtag_libusb_set_configuration(h
->fd
, 0);
2851 if (libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2852 LOG_DEBUG("claim interface failed");
2856 /* RX EP is common for all versions */
2857 h
->rx_ep
= STLINK_RX_EP
;
2860 if (jtag_libusb_get_pid(libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2861 LOG_DEBUG("libusb_get_pid failed");
2865 /* wrap version for first read */
2868 h
->version
.stlink
= 1;
2869 h
->tx_ep
= STLINK_TX_EP
;
2871 case STLINK_V3_USBLOADER_PID
:
2872 case STLINK_V3E_PID
:
2873 case STLINK_V3S_PID
:
2874 case STLINK_V3_2VCP_PID
:
2875 h
->version
.stlink
= 3;
2876 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2877 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2879 case STLINK_V2_1_PID
:
2880 case STLINK_V2_1_NO_MSD_PID
:
2881 h
->version
.stlink
= 2;
2882 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2883 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2886 /* fall through - we assume V2 to be the default version*/
2888 h
->version
.stlink
= 2;
2889 h
->tx_ep
= STLINK_TX_EP
;
2890 h
->trace_ep
= STLINK_TRACE_EP
;
2894 /* get the device version */
2895 err
= stlink_usb_version(h
);
2897 if (err
== ERROR_OK
) {
2899 } else if (h
->version
.stlink
== 1 ||
2901 LOG_ERROR("read version failed");
2904 err
= libusb_release_interface(h
->fd
, 0);
2905 if (err
!= ERROR_OK
) {
2906 LOG_ERROR("release interface failed");
2910 err
= libusb_reset_device(h
->fd
);
2911 if (err
!= ERROR_OK
) {
2912 LOG_ERROR("reset device failed");
2916 jtag_libusb_close(h
->fd
);
2918 Give the device one second to settle down and
2921 usleep(1 * 1000 * 1000);
2926 /* check if mode is supported */
2929 switch (h
->st_mode
) {
2930 case STLINK_MODE_DEBUG_SWD
:
2931 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2934 case STLINK_MODE_DEBUG_JTAG
:
2935 if (h
->version
.jtag
== 0)
2938 case STLINK_MODE_DEBUG_SWIM
:
2939 if (h
->version
.swim
== 0)
2947 if (err
!= ERROR_OK
) {
2948 LOG_ERROR("mode (transport) not supported by device");
2952 /* initialize the debug hardware */
2953 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
2955 if (err
!= ERROR_OK
) {
2956 LOG_ERROR("init mode failed (unable to connect to the target)");
2960 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
2961 err
= stlink_swim_enter(h
);
2962 if (err
!= ERROR_OK
) {
2963 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2967 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2971 /* get cpuid, so we can determine the max page size
2972 * start with a safe default */
2973 h
->max_mem_packet
= (1 << 10);
2976 stlink_usb_open_ap(h
, 0);
2977 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2978 if (err
== ERROR_OK
) {
2979 uint32_t cpuid
= le_to_h_u32(buffer
);
2980 int i
= (cpuid
>> 4) & 0xf;
2981 if (i
== 4 || i
== 3) {
2982 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2983 h
->max_mem_packet
= (1 << 12);
2987 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2994 stlink_usb_close(h
);
2999 static int stlink_usb_hl_open(struct hl_interface_param_s
*param
, void **fd
)
3001 return stlink_usb_open(param
, stlink_get_mode(param
->transport
), fd
);
3004 int stlink_config_trace(void *handle
, bool enabled
,
3005 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3006 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3007 uint16_t *prescaler
)
3009 struct stlink_usb_handle_s
*h
= handle
;
3012 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
3013 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
3014 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3019 stlink_usb_trace_disable(h
);
3023 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
3024 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3025 STLINK_TRACE_MAX_HZ
);
3029 stlink_usb_trace_disable(h
);
3032 *trace_freq
= STLINK_TRACE_MAX_HZ
;
3034 presc
= traceclkin_freq
/ *trace_freq
;
3036 if (traceclkin_freq
% *trace_freq
> 0)
3039 if (presc
> TPIU_ACPR_MAX_SWOSCALER
) {
3040 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3046 h
->trace
.source_hz
= *trace_freq
;
3048 return stlink_usb_trace_enable(h
);
3052 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
3054 struct stlink_usb_handle_s
*h
= handle
;
3056 assert(handle
!= NULL
);
3058 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3059 return ERROR_COMMAND_NOTFOUND
;
3061 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
3062 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3063 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3064 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
3065 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3067 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3071 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
3073 struct stlink_usb_handle_s
*h
= handle
;
3075 assert(handle
!= NULL
);
3077 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3078 return ERROR_COMMAND_NOTFOUND
;
3080 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
3081 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3082 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3083 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG
;
3084 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3086 /* ignore incorrectly returned error on bogus FW */
3087 if (h
->version
.flags
& STLINK_F_FIX_CLOSE_AP
)
3088 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3090 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
3095 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3096 unsigned short addr
, uint32_t *val
)
3098 struct stlink_usb_handle_s
*h
= handle
;
3101 assert(handle
!= NULL
);
3103 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3104 return ERROR_COMMAND_NOTFOUND
;
3106 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3107 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3108 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3109 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3110 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3112 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3113 *val
= le_to_h_u32(h
->databuf
+ 4);
3114 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, *val
);
3119 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3120 unsigned short addr
, uint32_t val
)
3122 struct stlink_usb_handle_s
*h
= handle
;
3124 assert(handle
!= NULL
);
3126 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3127 return ERROR_COMMAND_NOTFOUND
;
3129 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, val
);
3130 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3131 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3132 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3133 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3134 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3135 h_u32_to_le(&h
->cmdbuf
[6], val
);
3136 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3140 struct hl_layout_api_s stlink_usb_layout_api
= {
3142 .open
= stlink_usb_hl_open
,
3144 .close
= stlink_usb_close
,
3146 .idcode
= stlink_usb_idcode
,
3148 .state
= stlink_usb_state
,
3150 .reset
= stlink_usb_reset
,
3152 .assert_srst
= stlink_usb_assert_srst
,
3154 .run
= stlink_usb_run
,
3156 .halt
= stlink_usb_halt
,
3158 .step
= stlink_usb_step
,
3160 .read_regs
= stlink_usb_read_regs
,
3162 .read_reg
= stlink_usb_read_reg
,
3164 .write_reg
= stlink_usb_write_reg
,
3166 .read_mem
= stlink_usb_read_mem
,
3168 .write_mem
= stlink_usb_write_mem
,
3170 .write_debug_reg
= stlink_usb_write_debug_reg
,
3172 .override_target
= stlink_usb_override_target
,
3174 .speed
= stlink_speed
,
3176 .config_trace
= stlink_config_trace
,
3178 .poll_trace
= stlink_usb_trace_read
,
3181 /*****************************************************************************
3182 * DAP direct interface
3185 static struct stlink_usb_handle_s
*stlink_dap_handle
;
3186 static struct hl_interface_param_s stlink_dap_param
;
3187 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
3188 static int stlink_dap_error
= ERROR_OK
;
3190 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3194 static int stlink_dap_record_error(int error
)
3196 if (stlink_dap_error
== ERROR_OK
)
3197 stlink_dap_error
= error
;
3202 static int stlink_dap_get_and_clear_error(void)
3204 int retval
= stlink_dap_error
;
3205 stlink_dap_error
= ERROR_OK
;
3209 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
)
3211 struct stlink_usb_handle_s
*h
= handle
;
3214 /* nothing to do on old versions */
3215 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3218 if (apsel
> DP_APSEL_MAX
)
3221 if (test_bit(apsel
, opened_ap
))
3224 retval
= stlink_usb_init_access_port(h
, apsel
);
3225 if (retval
!= ERROR_OK
)
3228 LOG_DEBUG("AP %d enabled", apsel
);
3229 set_bit(apsel
, opened_ap
);
3233 static int stlink_dap_open_ap(unsigned short apsel
)
3235 return stlink_usb_open_ap(stlink_dap_handle
, apsel
);
3239 static int stlink_dap_closeall_ap(void)
3243 /* nothing to do on old versions */
3244 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3247 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
3248 if (!test_bit(apsel
, opened_ap
))
3250 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
3251 if (retval
!= ERROR_OK
)
3253 clear_bit(apsel
, opened_ap
);
3259 static int stlink_dap_reinit_interface(void)
3264 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
3265 * some firmware version it does not work as expected, and there is no
3266 * equivalent for SWD.
3267 * At least for now, to reset the interface quit from JTAG/SWD mode then
3268 * select the mode again.
3271 if (!stlink_dap_handle
->reconnect_pending
) {
3272 stlink_dap_handle
->reconnect_pending
= true;
3273 stlink_usb_mode_leave(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
3276 retval
= stlink_usb_mode_enter(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
3277 if (retval
!= ERROR_OK
)
3280 stlink_dap_handle
->reconnect_pending
= false;
3281 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
3282 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
3283 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
3284 if (test_bit(apsel
, opened_ap
)) {
3285 clear_bit(apsel
, opened_ap
);
3286 stlink_dap_open_ap(apsel
);
3292 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
3297 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
3299 /* Check if we should reset srst already when connecting, but not if reconnecting. */
3300 if (!dap
->do_reconnect
) {
3301 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3303 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3304 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3305 adapter_assert_reset();
3307 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3311 dap
->do_reconnect
= false;
3312 dap_invalidate_cache(dap
);
3314 retval
= dap_dp_init(dap
);
3315 if (retval
!= ERROR_OK
) {
3316 dap
->do_reconnect
= true;
3320 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
3321 if (retval
== ERROR_OK
)
3322 LOG_INFO("%s %#8.8" PRIx32
,
3323 (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
3326 dap
->do_reconnect
= true;
3332 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
3336 if (!dap
->do_reconnect
)
3339 retval
= stlink_dap_reinit_interface();
3340 if (retval
!= ERROR_OK
)
3343 return stlink_dap_op_connect(dap
);
3347 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
3349 /* Ignore the request */
3354 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3360 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3361 if (reg
& 0x000000F0) {
3362 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3363 return ERROR_COMMAND_NOTFOUND
;
3366 retval
= stlink_dap_check_reconnect(dap
);
3367 if (retval
!= ERROR_OK
)
3370 data
= data
? : &dummy
;
3371 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
3372 && stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) {
3373 /* Quirk required in JTAG. Read RDBUFF to get the data */
3374 retval
= stlink_read_dap_register(stlink_dap_handle
,
3375 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
3376 if (retval
== ERROR_OK
)
3377 retval
= stlink_read_dap_register(stlink_dap_handle
,
3378 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
3380 retval
= stlink_read_dap_register(stlink_dap_handle
,
3381 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3384 return stlink_dap_record_error(retval
);
3388 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned reg
,
3393 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3394 if (reg
& 0x000000F0) {
3395 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3396 return ERROR_COMMAND_NOTFOUND
;
3399 if (reg
== DP_SELECT
&& (data
& DP_SELECT_DPBANK
) != 0) {
3400 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
3401 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
3402 data
&= ~DP_SELECT_DPBANK
;
3405 retval
= stlink_dap_check_reconnect(dap
);
3406 if (retval
!= ERROR_OK
)
3409 /* ST-Link does not like that we set CORUNDETECT */
3410 if (reg
== DP_CTRL_STAT
)
3411 data
&= ~CORUNDETECT
;
3413 retval
= stlink_write_dap_register(stlink_dap_handle
,
3414 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3415 return stlink_dap_record_error(retval
);
3419 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned reg
,
3422 struct adiv5_dap
*dap
= ap
->dap
;
3426 retval
= stlink_dap_check_reconnect(dap
);
3427 if (retval
!= ERROR_OK
)
3430 if (reg
!= AP_REG_IDR
) {
3431 retval
= stlink_dap_open_ap(ap
->ap_num
);
3432 if (retval
!= ERROR_OK
)
3435 data
= data
? : &dummy
;
3436 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3438 dap
->stlink_flush_ap_write
= false;
3439 return stlink_dap_record_error(retval
);
3443 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned reg
,
3446 struct adiv5_dap
*dap
= ap
->dap
;
3449 retval
= stlink_dap_check_reconnect(dap
);
3450 if (retval
!= ERROR_OK
)
3453 retval
= stlink_dap_open_ap(ap
->ap_num
);
3454 if (retval
!= ERROR_OK
)
3457 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3459 dap
->stlink_flush_ap_write
= true;
3460 return stlink_dap_record_error(retval
);
3464 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
3466 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
3471 static int stlink_dap_op_run(struct adiv5_dap
*dap
)
3473 uint32_t ctrlstat
, pwrmask
;
3474 int retval
, saved_retval
;
3476 /* Here no LOG_DEBUG. This is called continuously! */
3479 * ST-Link returns immediately after a DAP write, without waiting for it
3481 * Run a dummy read to DP_RDBUFF, as suggested in
3482 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
3484 if (dap
->stlink_flush_ap_write
) {
3485 dap
->stlink_flush_ap_write
= false;
3486 retval
= stlink_dap_op_queue_dp_read(dap
, DP_RDBUFF
, NULL
);
3487 if (retval
!= ERROR_OK
) {
3488 dap
->do_reconnect
= true;
3493 saved_retval
= stlink_dap_get_and_clear_error();
3495 retval
= stlink_dap_op_queue_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
3496 if (retval
!= ERROR_OK
) {
3497 dap
->do_reconnect
= true;
3500 retval
= stlink_dap_get_and_clear_error();
3501 if (retval
!= ERROR_OK
) {
3502 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
3503 dap
->do_reconnect
= true;
3507 if (ctrlstat
& SSTICKYERR
) {
3508 if (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
)
3509 retval
= stlink_dap_op_queue_dp_write(dap
, DP_CTRL_STAT
,
3510 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
3512 retval
= stlink_dap_op_queue_dp_write(dap
, DP_ABORT
, STKERRCLR
);
3513 if (retval
!= ERROR_OK
) {
3514 dap
->do_reconnect
= true;
3517 retval
= stlink_dap_get_and_clear_error();
3518 if (retval
!= ERROR_OK
) {
3519 dap
->do_reconnect
= true;
3524 /* check for power lost */
3525 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
3526 if ((ctrlstat
& pwrmask
) != pwrmask
)
3527 dap
->do_reconnect
= true;
3529 return saved_retval
;
3533 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
3537 retval
= stlink_dap_closeall_ap();
3538 if (retval
!= ERROR_OK
)
3539 LOG_ERROR("Error closing APs");
3542 static int stlink_swim_op_srst(void)
3544 return stlink_swim_generate_rst(stlink_dap_handle
);
3547 static int stlink_swim_op_read_mem(uint32_t addr
, uint32_t size
,
3548 uint32_t count
, uint8_t *buffer
)
3551 uint32_t bytes_remaining
;
3553 LOG_DEBUG_IO("read at 0x%08x len %d*0x%08x", addr
, size
, count
);
3557 bytes_remaining
= (count
> STLINK_DATA_SIZE
) ? STLINK_DATA_SIZE
: count
;
3558 retval
= stlink_swim_readbytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
3559 if (retval
!= ERROR_OK
)
3562 buffer
+= bytes_remaining
;
3563 addr
+= bytes_remaining
;
3564 count
-= bytes_remaining
;
3570 static int stlink_swim_op_write_mem(uint32_t addr
, uint32_t size
,
3571 uint32_t count
, const uint8_t *buffer
)
3574 uint32_t bytes_remaining
;
3576 LOG_DEBUG_IO("write at 0x%08x len %d*0x%08x", addr
, size
, count
);
3580 bytes_remaining
= (count
> STLINK_DATA_SIZE
) ? STLINK_DATA_SIZE
: count
;
3581 retval
= stlink_swim_writebytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
3582 if (retval
!= ERROR_OK
)
3585 buffer
+= bytes_remaining
;
3586 addr
+= bytes_remaining
;
3587 count
-= bytes_remaining
;
3593 static int stlink_swim_op_reconnect(void)
3597 retval
= stlink_usb_mode_enter(stlink_dap_handle
, STLINK_MODE_DEBUG_SWIM
);
3598 if (retval
!= ERROR_OK
)
3601 return stlink_swim_resync(stlink_dap_handle
);
3604 static int stlink_dap_config_trace(bool enabled
,
3605 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3606 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3607 uint16_t *prescaler
)
3609 return stlink_config_trace(stlink_dap_handle
, enabled
, pin_protocol
,
3610 port_size
, trace_freq
, traceclkin_freq
,
3614 static int stlink_dap_trace_read(uint8_t *buf
, size_t *size
)
3616 return stlink_usb_trace_read(stlink_dap_handle
, buf
, size
);
3620 COMMAND_HANDLER(stlink_dap_serial_command
)
3622 LOG_DEBUG("stlink_dap_serial_command");
3624 if (CMD_ARGC
!= 1) {
3625 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
3626 return ERROR_COMMAND_SYNTAX_ERROR
;
3629 if (stlink_dap_param
.serial
) {
3630 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
3631 free((void *)stlink_dap_param
.serial
);
3634 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
3639 COMMAND_HANDLER(stlink_dap_vid_pid
)
3641 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
3643 if (CMD_ARGC
> max_usb_ids
* 2) {
3644 LOG_WARNING("ignoring extra IDs in vid_pid "
3645 "(maximum is %d pairs)", max_usb_ids
);
3646 CMD_ARGC
= max_usb_ids
* 2;
3648 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3649 LOG_WARNING("incomplete vid_pid configuration directive");
3650 return ERROR_COMMAND_SYNTAX_ERROR
;
3652 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3653 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
3654 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
3657 /* null termination */
3658 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
3664 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
3667 .handler
= stlink_dap_serial_command
,
3668 .mode
= COMMAND_CONFIG
,
3669 .help
= "set the serial number of the adapter",
3670 .usage
= "<serial_number>",
3674 .handler
= stlink_dap_vid_pid
,
3675 .mode
= COMMAND_CONFIG
,
3676 .help
= "USB VID and PID of the adapter",
3677 .usage
= "(vid pid)+",
3679 COMMAND_REGISTRATION_DONE
3683 static const struct command_registration stlink_dap_command_handlers
[] = {
3686 .mode
= COMMAND_ANY
,
3687 .help
= "perform st-link management",
3688 .chain
= stlink_dap_subcommand_handlers
,
3691 COMMAND_REGISTRATION_DONE
3695 static int stlink_dap_init(void)
3697 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3698 enum stlink_mode mode
;
3701 LOG_DEBUG("stlink_dap_init()");
3703 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3704 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3705 stlink_dap_param
.connect_under_reset
= true;
3707 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3710 if (transport_is_dapdirect_swd())
3711 mode
= STLINK_MODE_DEBUG_SWD
;
3712 else if (transport_is_dapdirect_jtag())
3713 mode
= STLINK_MODE_DEBUG_JTAG
;
3714 else if (transport_is_swim())
3715 mode
= STLINK_MODE_DEBUG_SWIM
;
3717 LOG_ERROR("Unsupported transport");
3721 retval
= stlink_usb_open(&stlink_dap_param
, mode
, (void **)&stlink_dap_handle
);
3722 if (retval
!= ERROR_OK
)
3725 if ((mode
!= STLINK_MODE_DEBUG_SWIM
) &&
3726 !(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
3727 LOG_ERROR("ST-Link version does not support DAP direct transport");
3734 static int stlink_dap_quit(void)
3736 LOG_DEBUG("stlink_dap_quit()");
3738 free((void *)stlink_dap_param
.serial
);
3739 stlink_dap_param
.serial
= NULL
;
3741 return stlink_usb_close(stlink_dap_handle
);
3745 static int stlink_dap_reset(int req_trst
, int req_srst
)
3747 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
3748 return stlink_usb_assert_srst(stlink_dap_handle
,
3749 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
3750 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
3754 static int stlink_dap_speed(int speed
)
3757 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
3758 return ERROR_JTAG_NOT_IMPLEMENTED
;
3761 stlink_dap_param
.initial_interface_speed
= speed
;
3762 stlink_speed(stlink_dap_handle
, speed
, false);
3767 static int stlink_dap_khz(int khz
, int *jtag_speed
)
3770 LOG_ERROR("RCLK not supported");
3774 *jtag_speed
= stlink_speed(stlink_dap_handle
, khz
, true);
3779 static int stlink_dap_speed_div(int speed
, int *khz
)
3785 static const struct dap_ops stlink_dap_ops
= {
3786 .connect
= stlink_dap_op_connect
,
3787 .send_sequence
= stlink_dap_op_send_sequence
,
3788 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
3789 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
3790 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
3791 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
3792 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
3793 .run
= stlink_dap_op_run
,
3794 .sync
= NULL
, /* optional */
3795 .quit
= stlink_dap_op_quit
, /* optional */
3798 static const struct swim_driver stlink_swim_ops
= {
3799 .srst
= stlink_swim_op_srst
,
3800 .read_mem
= stlink_swim_op_read_mem
,
3801 .write_mem
= stlink_swim_op_write_mem
,
3802 .reconnect
= stlink_swim_op_reconnect
,
3805 static const char *const stlink_dap_transport
[] = { "dapdirect_swd", "dapdirect_jtag", "swim", NULL
};
3807 struct adapter_driver stlink_dap_adapter_driver
= {
3809 .transports
= stlink_dap_transport
,
3810 .commands
= stlink_dap_command_handlers
,
3812 .init
= stlink_dap_init
,
3813 .quit
= stlink_dap_quit
,
3814 .reset
= stlink_dap_reset
,
3815 .speed
= stlink_dap_speed
,
3816 .khz
= stlink_dap_khz
,
3817 .speed_div
= stlink_dap_speed_div
,
3818 .config_trace
= stlink_dap_config_trace
,
3819 .poll_trace
= stlink_dap_trace_read
,
3821 .dap_jtag_ops
= &stlink_dap_ops
,
3822 .dap_swd_ops
= &stlink_dap_ops
,
3823 .swim_ops
= &stlink_swim_ops
,