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
);
366 static unsigned int stlink_usb_block(void *handle
)
368 struct stlink_usb_handle_s
*h
= handle
;
370 assert(handle
!= NULL
);
372 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
373 return STLINKV3_MAX_RW8
;
375 return STLINK_MAX_RW8
;
380 #ifdef USE_LIBUSB_ASYNCIO
382 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
384 int *completed
= transfer
->user_data
;
386 /* caller interprets result and frees transfer */
390 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
392 int r
, *completed
= transfer
->user_data
;
394 /* Assuming a single libusb context exists. There no existing interface into this
395 * module to pass a libusb context.
397 struct libusb_context
*ctx
= NULL
;
399 while (!*completed
) {
400 r
= libusb_handle_events_completed(ctx
, completed
);
402 if (r
== LIBUSB_ERROR_INTERRUPTED
)
404 libusb_cancel_transfer(transfer
);
411 static int transfer_error_status(const struct libusb_transfer
*transfer
)
415 switch (transfer
->status
) {
416 case LIBUSB_TRANSFER_COMPLETED
:
419 case LIBUSB_TRANSFER_TIMED_OUT
:
420 r
= LIBUSB_ERROR_TIMEOUT
;
422 case LIBUSB_TRANSFER_STALL
:
423 r
= LIBUSB_ERROR_PIPE
;
425 case LIBUSB_TRANSFER_OVERFLOW
:
426 r
= LIBUSB_ERROR_OVERFLOW
;
428 case LIBUSB_TRANSFER_NO_DEVICE
:
429 r
= LIBUSB_ERROR_NO_DEVICE
;
431 case LIBUSB_TRANSFER_ERROR
:
432 case LIBUSB_TRANSFER_CANCELLED
:
436 r
= LIBUSB_ERROR_OTHER
;
450 size_t transfer_size
;
451 struct libusb_transfer
*transfer
;
454 static int jtag_libusb_bulk_transfer_n(
455 struct libusb_device_handle
*dev_handle
,
456 struct jtag_xfer
*transfers
,
461 int returnval
= ERROR_OK
;
464 for (size_t i
= 0; i
< n_transfers
; ++i
) {
465 transfers
[i
].retval
= 0;
466 transfers
[i
].completed
= 0;
467 transfers
[i
].transfer_size
= 0;
468 transfers
[i
].transfer
= libusb_alloc_transfer(0);
470 if (transfers
[i
].transfer
== NULL
) {
471 for (size_t j
= 0; j
< i
; ++j
)
472 libusb_free_transfer(transfers
[j
].transfer
);
474 LOG_DEBUG("ERROR, failed to alloc usb transfers");
475 for (size_t k
= 0; k
< n_transfers
; ++k
)
476 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
481 for (size_t i
= 0; i
< n_transfers
; ++i
) {
482 libusb_fill_bulk_transfer(
483 transfers
[i
].transfer
,
485 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
486 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
487 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
489 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
491 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
493 /* Probably no point continuing to submit transfers once a submission fails.
494 * As a result, tag all remaining transfers as errors.
496 for (size_t j
= i
; j
< n_transfers
; ++j
)
497 transfers
[j
].retval
= retval
;
499 returnval
= ERROR_FAIL
;
504 /* Wait for every submitted USB transfer to complete.
506 for (size_t i
= 0; i
< n_transfers
; ++i
) {
507 if (transfers
[i
].retval
== 0) {
508 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
510 retval
= transfer_error_status(transfers
[i
].transfer
);
512 returnval
= ERROR_FAIL
;
513 transfers
[i
].retval
= retval
;
514 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
516 /* Assuming actual_length is only valid if there is no transfer error.
518 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
522 libusb_free_transfer(transfers
[i
].transfer
);
523 transfers
[i
].transfer
= NULL
;
533 static int stlink_usb_xfer_v1_get_status(void *handle
)
535 struct stlink_usb_handle_s
*h
= handle
;
538 assert(handle
!= NULL
);
541 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
543 ret
= jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
544 STLINK_READ_TIMEOUT
, &tr
);
550 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
553 if (t1
!= 0x53425355)
561 if (h
->cmdbuf
[12] != 0)
567 #ifdef USE_LIBUSB_ASYNCIO
568 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
570 struct stlink_usb_handle_s
*h
= handle
;
572 assert(handle
!= NULL
);
574 size_t n_transfers
= 0;
575 struct jtag_xfer transfers
[2];
577 memset(transfers
, 0, sizeof(transfers
));
579 transfers
[0].ep
= h
->tx_ep
;
580 transfers
[0].buf
= h
->cmdbuf
;
581 transfers
[0].size
= cmdsize
;
585 if (h
->direction
== h
->tx_ep
&& size
) {
586 transfers
[1].ep
= h
->tx_ep
;
587 transfers
[1].buf
= (uint8_t *)buf
;
588 transfers
[1].size
= size
;
591 } else if (h
->direction
== h
->rx_ep
&& size
) {
592 transfers
[1].ep
= h
->rx_ep
;
593 transfers
[1].buf
= (uint8_t *)buf
;
594 transfers
[1].size
= size
;
599 return jtag_libusb_bulk_transfer_n(
603 STLINK_WRITE_TIMEOUT
);
606 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
608 struct stlink_usb_handle_s
*h
= handle
;
611 assert(handle
!= NULL
);
613 ret
= jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
614 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
615 if (ret
|| tr
!= cmdsize
)
618 if (h
->direction
== h
->tx_ep
&& size
) {
619 ret
= jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
620 size
, STLINK_WRITE_TIMEOUT
, &tr
);
621 if (ret
|| tr
!= size
) {
622 LOG_DEBUG("bulk write failed");
625 } else if (h
->direction
== h
->rx_ep
&& size
) {
626 ret
= jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
627 size
, STLINK_READ_TIMEOUT
, &tr
);
628 if (ret
|| tr
!= size
) {
629 LOG_DEBUG("bulk read failed");
639 static int stlink_usb_xfer_v1_get_sense(void *handle
)
642 struct stlink_usb_handle_s
*h
= handle
;
644 assert(handle
!= NULL
);
646 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
648 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
649 h
->cmdbuf
[h
->cmdidx
++] = 0;
650 h
->cmdbuf
[h
->cmdidx
++] = 0;
651 h
->cmdbuf
[h
->cmdidx
++] = 0;
652 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
654 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
659 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
666 transfers block in cmdbuf
667 <size> indicates number of bytes in the following
669 Ignore the (eventual) error code in the received packet.
671 static int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
673 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
674 struct stlink_usb_handle_s
*h
= handle
;
676 assert(handle
!= NULL
);
678 if (h
->version
.stlink
== 1) {
679 cmdsize
= STLINK_SG_SIZE
;
680 /* put length in bCBWCBLength */
681 h
->cmdbuf
[14] = h
->cmdidx
-15;
684 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
689 if (h
->version
.stlink
== 1) {
690 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
691 /* check csw status */
692 if (h
->cmdbuf
[12] == 1) {
693 LOG_DEBUG("get sense");
694 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
705 Converts an STLINK status code held in the first byte of a response
706 to an openocd error, logs any error/wait status as debug output.
708 static int stlink_usb_error_check(void *handle
)
710 struct stlink_usb_handle_s
*h
= handle
;
712 assert(handle
!= NULL
);
714 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
715 switch (h
->databuf
[0]) {
716 case STLINK_SWIM_ERR_OK
:
718 case STLINK_SWIM_BUSY
:
721 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
726 /* TODO: no error checking yet on api V1 */
727 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
728 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
730 switch (h
->databuf
[0]) {
731 case STLINK_DEBUG_ERR_OK
:
733 case STLINK_DEBUG_ERR_FAULT
:
734 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
736 case STLINK_SWD_AP_WAIT
:
737 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
739 case STLINK_SWD_DP_WAIT
:
740 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
742 case STLINK_JTAG_GET_IDCODE_ERROR
:
743 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
745 case STLINK_JTAG_WRITE_ERROR
:
746 LOG_DEBUG("Write error");
748 case STLINK_JTAG_WRITE_VERIF_ERROR
:
749 LOG_DEBUG("Write verify error, ignoring");
751 case STLINK_SWD_AP_FAULT
:
752 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
753 * returns ERROR_OK with the comment:
754 * Change in error status when reading outside RAM.
755 * This fix allows CDT plugin to visualize memory.
757 LOG_DEBUG("STLINK_SWD_AP_FAULT");
759 case STLINK_SWD_AP_ERROR
:
760 LOG_DEBUG("STLINK_SWD_AP_ERROR");
762 case STLINK_SWD_AP_PARITY_ERROR
:
763 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
765 case STLINK_SWD_DP_FAULT
:
766 LOG_DEBUG("STLINK_SWD_DP_FAULT");
768 case STLINK_SWD_DP_ERROR
:
769 LOG_DEBUG("STLINK_SWD_DP_ERROR");
771 case STLINK_SWD_DP_PARITY_ERROR
:
772 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
774 case STLINK_SWD_AP_WDATA_ERROR
:
775 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
777 case STLINK_SWD_AP_STICKY_ERROR
:
778 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
780 case STLINK_SWD_AP_STICKYORUN_ERROR
:
781 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
783 case STLINK_BAD_AP_ERROR
:
784 LOG_DEBUG("STLINK_BAD_AP_ERROR");
787 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
793 * Wrapper around stlink_usb_xfer_noerrcheck()
794 * to check the error code in the received packet
796 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
802 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
803 if (retval
!= ERROR_OK
)
806 return stlink_usb_error_check(handle
);
809 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
811 Works for commands where the STLINK_DEBUG status is returned in the first
812 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
814 Returns an openocd result code.
816 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
820 struct stlink_usb_handle_s
*h
= handle
;
823 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
824 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
829 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
830 res
= stlink_swim_status(handle
);
835 res
= stlink_usb_error_check(handle
);
836 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
837 unsigned int delay_us
= (1<<retries
++) * 1000;
838 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
847 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
849 struct stlink_usb_handle_s
*h
= handle
;
852 assert(handle
!= NULL
);
854 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
856 ret
= jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
, size
,
857 STLINK_READ_TIMEOUT
, &tr
);
858 if (ret
|| tr
!= size
) {
859 LOG_ERROR("bulk trace read failed");
867 this function writes transfer length in
868 the right place in the cb
870 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
872 struct stlink_usb_handle_s
*h
= handle
;
874 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
877 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
879 struct stlink_usb_handle_s
*h
= handle
;
881 /* fill the send buffer */
882 strcpy((char *)h
->cmdbuf
, "USBC");
884 /* csw tag not used */
885 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
887 /* cbw data transfer length (in the following data phase in or out) */
888 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
891 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
892 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
893 /* cdb clength (is filled in at xfer) */
894 h
->cmdbuf
[h
->cmdidx
++] = 0;
898 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
900 struct stlink_usb_handle_s
*h
= handle
;
902 h
->direction
= direction
;
906 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
907 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
909 if (h
->version
.stlink
== 1)
910 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
914 static int stlink_usb_version(void *handle
)
919 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
920 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
922 struct stlink_usb_handle_s
*h
= handle
;
924 assert(handle
!= NULL
);
926 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
928 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
930 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
935 version
= be_to_h_u16(h
->databuf
);
936 v
= (version
>> 12) & 0x0f;
937 x
= (version
>> 6) & 0x3f;
940 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
941 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
944 case STLINK_V2_1_PID
:
945 case STLINK_V2_1_NO_MSD_PID
:
946 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
947 /* MxSy : STM8 V2.1 - SWIM only */
952 /* JxMy : STM32 V2.1 - JTAG/SWD only */
965 /* STLINK-V3 requires a specific command */
966 if (v
== 3 && x
== 0 && y
== 0) {
967 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
969 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
971 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
976 swim
= h
->databuf
[1];
977 jtag
= h
->databuf
[2];
979 bridge
= h
->databuf
[4];
980 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
981 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
984 h
->version
.stlink
= v
;
985 h
->version
.jtag
= jtag
;
986 h
->version
.swim
= swim
;
989 switch (h
->version
.stlink
) {
991 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
992 if (h
->version
.jtag
>= 11)
993 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
995 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
999 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1000 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1002 /* API for trace from J13 */
1003 /* API for target voltage from J13 */
1004 if (h
->version
.jtag
>= 13)
1005 flags
|= STLINK_F_HAS_TRACE
;
1007 /* preferred API to get last R/W status from J15 */
1008 if (h
->version
.jtag
>= 15)
1009 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1011 /* API to set SWD frequency from J22 */
1012 if (h
->version
.jtag
>= 22)
1013 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1015 /* API to set JTAG frequency from J24 */
1016 /* API to access DAP registers from J24 */
1017 if (h
->version
.jtag
>= 24) {
1018 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1019 flags
|= STLINK_F_HAS_DAP_REG
;
1022 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1023 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1024 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1026 /* API to read/write memory at 16 bit from J26 */
1027 if (h
->version
.jtag
>= 26)
1028 flags
|= STLINK_F_HAS_MEM_16BIT
;
1030 /* API required to init AP before any AP access from J28 */
1031 if (h
->version
.jtag
>= 28)
1032 flags
|= STLINK_F_HAS_AP_INIT
;
1034 /* API required to return proper error code on close AP from J29 */
1035 if (h
->version
.jtag
>= 29)
1036 flags
|= STLINK_F_FIX_CLOSE_AP
;
1038 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1039 if (h
->version
.jtag
>= 32)
1040 flags
|= STLINK_F_HAS_DPBANKSEL
;
1044 /* all STLINK-V3 use api-v3 */
1045 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1047 /* STLINK-V3 is a superset of ST-LINK/V2 */
1050 /* API for target voltage */
1051 flags
|= STLINK_F_HAS_TRACE
;
1053 /* preferred API to get last R/W status */
1054 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1056 /* API to access DAP registers */
1057 flags
|= STLINK_F_HAS_DAP_REG
;
1059 /* API to read/write memory at 16 bit */
1060 flags
|= STLINK_F_HAS_MEM_16BIT
;
1062 /* API required to init AP before any AP access */
1063 flags
|= STLINK_F_HAS_AP_INIT
;
1065 /* API required to return proper error code on close AP */
1066 flags
|= STLINK_F_FIX_CLOSE_AP
;
1068 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1069 if (h
->version
.jtag
>= 2)
1070 flags
|= STLINK_F_HAS_DPBANKSEL
;
1072 /* 8bit read/write max packet size 512 bytes from V3J6 */
1073 if (h
->version
.jtag
>= 6)
1074 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1080 h
->version
.flags
= flags
;
1083 p
+= sprintf(p
, "V%d", v
);
1085 p
+= sprintf(p
, "J%d", jtag
);
1087 p
+= sprintf(p
, "M%d", msd
);
1089 p
+= sprintf(p
, "B%d", bridge
);
1091 sprintf(p
, "S%d", swim
);
1093 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1095 h
->version
.jtag_api
,
1102 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1104 struct stlink_usb_handle_s
*h
= handle
;
1105 uint32_t adc_results
[2];
1107 /* no error message, simply quit with error */
1108 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1109 return ERROR_COMMAND_NOTFOUND
;
1111 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1113 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1115 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1117 if (result
!= ERROR_OK
)
1120 /* convert result */
1121 adc_results
[0] = le_to_h_u32(h
->databuf
);
1122 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1124 *target_voltage
= 0;
1127 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1129 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1134 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1136 struct stlink_usb_handle_s
*h
= handle
;
1138 assert(handle
!= NULL
);
1140 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1141 return ERROR_COMMAND_NOTFOUND
;
1143 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1145 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1146 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1147 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1150 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1152 if (result
!= ERROR_OK
)
1158 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1160 struct stlink_usb_handle_s
*h
= handle
;
1162 assert(handle
!= NULL
);
1164 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1165 return ERROR_COMMAND_NOTFOUND
;
1167 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1169 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1171 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1174 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1176 if (result
!= ERROR_OK
)
1183 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1186 struct stlink_usb_handle_s
*h
= handle
;
1188 assert(handle
!= NULL
);
1190 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1192 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1194 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1196 if (res
!= ERROR_OK
)
1199 *mode
= h
->databuf
[0];
1205 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1208 struct stlink_usb_handle_s
*h
= handle
;
1210 assert(handle
!= NULL
);
1212 /* on api V2 we are able the read the latest command
1214 * TODO: we need the test on api V1 too
1216 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1219 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1222 case STLINK_MODE_DEBUG_JTAG
:
1223 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1224 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1225 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1227 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1228 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1230 case STLINK_MODE_DEBUG_SWD
:
1231 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1232 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1233 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1235 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1236 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1238 case STLINK_MODE_DEBUG_SWIM
:
1239 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1240 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1241 /* swim enter does not return any response or status */
1242 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1243 case STLINK_MODE_DFU
:
1244 case STLINK_MODE_MASS
:
1249 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1253 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1256 struct stlink_usb_handle_s
*h
= handle
;
1258 assert(handle
!= NULL
);
1260 /* command with no reply, use a valid endpoint but zero size */
1261 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1264 case STLINK_MODE_DEBUG_JTAG
:
1265 case STLINK_MODE_DEBUG_SWD
:
1266 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1267 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1269 case STLINK_MODE_DEBUG_SWIM
:
1270 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1271 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1273 case STLINK_MODE_DFU
:
1274 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1275 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1277 case STLINK_MODE_MASS
:
1282 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1284 if (res
!= ERROR_OK
)
1290 static int stlink_usb_assert_srst(void *handle
, int srst
);
1292 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1295 case HL_TRANSPORT_SWD
:
1296 return STLINK_MODE_DEBUG_SWD
;
1297 case HL_TRANSPORT_JTAG
:
1298 return STLINK_MODE_DEBUG_JTAG
;
1300 return STLINK_MODE_UNKNOWN
;
1305 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1309 enum stlink_mode emode
;
1310 struct stlink_usb_handle_s
*h
= handle
;
1312 assert(handle
!= NULL
);
1314 res
= stlink_usb_current_mode(handle
, &mode
);
1316 if (res
!= ERROR_OK
)
1319 LOG_DEBUG("MODE: 0x%02X", mode
);
1321 /* try to exit current mode */
1323 case STLINK_DEV_DFU_MODE
:
1324 emode
= STLINK_MODE_DFU
;
1326 case STLINK_DEV_DEBUG_MODE
:
1327 emode
= STLINK_MODE_DEBUG_SWD
;
1329 case STLINK_DEV_SWIM_MODE
:
1330 emode
= STLINK_MODE_DEBUG_SWIM
;
1332 case STLINK_DEV_BOOTLOADER_MODE
:
1333 case STLINK_DEV_MASS_MODE
:
1335 emode
= STLINK_MODE_UNKNOWN
;
1339 if (emode
!= STLINK_MODE_UNKNOWN
) {
1340 res
= stlink_usb_mode_leave(handle
, emode
);
1342 if (res
!= ERROR_OK
)
1346 res
= stlink_usb_current_mode(handle
, &mode
);
1348 if (res
!= ERROR_OK
)
1351 /* we check the target voltage here as an aid to debugging connection problems.
1352 * the stlink requires the target Vdd to be connected for reliable debugging.
1353 * this cmd is supported in all modes except DFU
1355 if (mode
!= STLINK_DEV_DFU_MODE
) {
1357 float target_voltage
;
1359 /* check target voltage (if supported) */
1360 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1362 if (res
!= ERROR_OK
) {
1363 if (res
!= ERROR_COMMAND_NOTFOUND
)
1364 LOG_ERROR("voltage check failed");
1365 /* attempt to continue as it is not a catastrophic failure */
1367 /* check for a sensible target voltage, operating range is 1.65-5.5v
1368 * according to datasheet */
1369 if (target_voltage
< 1.5)
1370 LOG_ERROR("target voltage may be too low for reliable debugging");
1374 LOG_DEBUG("MODE: 0x%02X", mode
);
1376 /* set selected mode */
1379 if (emode
== STLINK_MODE_UNKNOWN
) {
1380 LOG_ERROR("selected mode (transport) not supported");
1384 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1385 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1386 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1387 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1388 stlink_speed(h
, initial_interface_speed
, false);
1390 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1391 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1392 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1393 stlink_speed(h
, initial_interface_speed
, false);
1397 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
1398 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1400 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1401 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1402 stlink_speed(h
, initial_interface_speed
, false);
1405 /* preliminary SRST assert:
1406 * We want SRST is asserted before activating debug signals (mode_enter).
1407 * As the required mode has not been set, the adapter may not know what pin to use.
1408 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1409 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1410 * after power on, SWIM_RST stays unchanged */
1411 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1412 stlink_usb_assert_srst(handle
, 0);
1413 /* do not check the return status here, we will
1414 proceed and enter the desired mode below
1415 and try asserting srst again. */
1417 res
= stlink_usb_mode_enter(handle
, emode
);
1418 if (res
!= ERROR_OK
)
1421 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1422 if (connect_under_reset
) {
1423 res
= stlink_usb_assert_srst(handle
, 0);
1424 if (res
!= ERROR_OK
)
1428 res
= stlink_usb_current_mode(handle
, &mode
);
1430 if (res
!= ERROR_OK
)
1433 LOG_DEBUG("MODE: 0x%02X", mode
);
1438 /* request status from last swim request */
1439 static int stlink_swim_status(void *handle
)
1441 struct stlink_usb_handle_s
*h
= handle
;
1444 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1445 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1446 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1447 /* error is checked by the caller */
1448 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1449 if (res
!= ERROR_OK
)
1454 the purpose of this function is unknown...
1455 capabilites? anyway for swim v6 it returns
1458 __attribute__((unused
))
1459 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1461 struct stlink_usb_handle_s
*h
= handle
;
1464 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1465 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1466 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1467 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1468 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1469 if (res
!= ERROR_OK
)
1471 memcpy(cap
, h
->databuf
, 8);
1475 /* debug dongle assert/deassert sreset line */
1476 static int stlink_swim_assert_reset(void *handle
, int reset
)
1478 struct stlink_usb_handle_s
*h
= handle
;
1481 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1482 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1484 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1487 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1488 if (res
!= ERROR_OK
)
1495 1.3ms low then 750Hz then 1.5kHz
1497 static int stlink_swim_enter(void *handle
)
1499 struct stlink_usb_handle_s
*h
= handle
;
1502 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1503 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1504 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1505 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1506 if (res
!= ERROR_OK
)
1511 /* switch high/low speed swim */
1512 static int stlink_swim_speed(void *handle
, int speed
)
1514 struct stlink_usb_handle_s
*h
= handle
;
1517 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1518 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1519 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1521 h
->cmdbuf
[h
->cmdidx
++] = 1;
1523 h
->cmdbuf
[h
->cmdidx
++] = 0;
1524 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1525 if (res
!= ERROR_OK
)
1531 initiate srst from swim.
1532 nrst is pulled low for 50us.
1534 static int stlink_swim_generate_rst(void *handle
)
1536 struct stlink_usb_handle_s
*h
= handle
;
1539 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1540 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1541 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1542 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1543 if (res
!= ERROR_OK
)
1549 send resyncronize sequence
1550 swim is pulled low for 16us
1551 reply is 64 clks low
1553 static int stlink_swim_resync(void *handle
)
1555 struct stlink_usb_handle_s
*h
= handle
;
1558 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1559 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1560 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1561 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1562 if (res
!= ERROR_OK
)
1567 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1569 struct stlink_usb_handle_s
*h
= handle
;
1572 unsigned int datalen
= 0;
1573 int cmdsize
= STLINK_CMD_SIZE_V2
;
1575 if (len
> STLINK_DATA_SIZE
)
1578 if (h
->version
.stlink
== 1)
1579 cmdsize
= STLINK_SG_SIZE
;
1581 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1582 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1583 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1584 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1586 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1588 for (i
= 0; i
< len
; i
++) {
1589 if (h
->cmdidx
== cmdsize
)
1590 h
->databuf
[datalen
++] = *(data
++);
1592 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1594 if (h
->version
.stlink
== 1)
1595 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1597 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1598 if (res
!= ERROR_OK
)
1603 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1605 struct stlink_usb_handle_s
*h
= handle
;
1608 if (len
> STLINK_DATA_SIZE
)
1611 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1612 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1613 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1614 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1616 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1618 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1619 if (res
!= ERROR_OK
)
1622 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1623 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1625 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1626 if (res
!= ERROR_OK
)
1633 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1636 struct stlink_usb_handle_s
*h
= handle
;
1638 assert(handle
!= NULL
);
1640 /* there is no swim read core id cmd */
1641 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1646 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1648 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1649 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1650 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1652 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1655 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1657 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1661 if (res
!= ERROR_OK
)
1664 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1666 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1671 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1673 struct stlink_usb_handle_s
*h
= handle
;
1676 assert(handle
!= NULL
);
1678 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1680 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1681 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1682 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1685 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1686 if (res
!= ERROR_OK
)
1689 *val
= le_to_h_u32(h
->databuf
+ 4);
1693 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1695 struct stlink_usb_handle_s
*h
= handle
;
1697 assert(handle
!= NULL
);
1699 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1701 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1702 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1703 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1705 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1706 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1708 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1711 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1715 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1717 struct stlink_usb_handle_s
*h
= handle
;
1719 assert(handle
!= NULL
);
1721 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1724 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1726 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1727 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1729 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1730 if (res
!= ERROR_OK
)
1733 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1734 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1737 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1738 if (res
!= ERROR_OK
)
1747 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1752 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1753 if (result
!= ERROR_OK
)
1754 return TARGET_UNKNOWN
;
1756 if (status
& S_HALT
)
1757 return TARGET_HALTED
;
1758 else if (status
& S_RESET_ST
)
1759 return TARGET_RESET
;
1761 return TARGET_RUNNING
;
1765 static enum target_state
stlink_usb_state(void *handle
)
1768 struct stlink_usb_handle_s
*h
= handle
;
1770 assert(handle
!= NULL
);
1772 if (h
->reconnect_pending
) {
1773 LOG_INFO("Previous state query failed, trying to reconnect");
1774 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
1775 if (res
!= ERROR_OK
)
1776 return TARGET_UNKNOWN
;
1778 h
->reconnect_pending
= false;
1781 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1782 res
= stlink_usb_v2_get_status(handle
);
1783 if (res
== TARGET_UNKNOWN
)
1784 h
->reconnect_pending
= true;
1788 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1790 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1791 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1793 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1795 if (res
!= ERROR_OK
)
1796 return TARGET_UNKNOWN
;
1798 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1799 return TARGET_RUNNING
;
1800 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1801 return TARGET_HALTED
;
1803 h
->reconnect_pending
= true;
1805 return TARGET_UNKNOWN
;
1808 static int stlink_usb_assert_srst(void *handle
, int srst
)
1810 struct stlink_usb_handle_s
*h
= handle
;
1812 assert(handle
!= NULL
);
1814 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
1815 return stlink_swim_assert_reset(handle
, srst
);
1817 if (h
->version
.stlink
== 1)
1818 return ERROR_COMMAND_NOTFOUND
;
1820 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1822 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1823 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1824 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1826 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1830 static void stlink_usb_trace_disable(void *handle
)
1833 struct stlink_usb_handle_s
*h
= handle
;
1835 assert(handle
!= NULL
);
1837 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1839 LOG_DEBUG("Tracing: disable");
1841 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1842 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1843 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1844 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1846 if (res
== ERROR_OK
)
1847 h
->trace
.enabled
= false;
1852 static int stlink_usb_trace_enable(void *handle
)
1855 struct stlink_usb_handle_s
*h
= handle
;
1857 assert(handle
!= NULL
);
1859 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1860 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1862 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1863 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1864 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1866 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1869 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1871 if (res
== ERROR_OK
) {
1872 h
->trace
.enabled
= true;
1873 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1876 LOG_ERROR("Tracing is not supported by this version.");
1884 static int stlink_usb_reset(void *handle
)
1886 struct stlink_usb_handle_s
*h
= handle
;
1889 assert(handle
!= NULL
);
1891 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1893 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1895 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1896 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1898 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1900 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1901 if (retval
!= ERROR_OK
)
1904 if (h
->trace
.enabled
) {
1905 stlink_usb_trace_disable(h
);
1906 return stlink_usb_trace_enable(h
);
1913 static int stlink_usb_run(void *handle
)
1916 struct stlink_usb_handle_s
*h
= handle
;
1918 assert(handle
!= NULL
);
1920 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1921 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1926 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1928 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1929 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1931 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1935 static int stlink_usb_halt(void *handle
)
1938 struct stlink_usb_handle_s
*h
= handle
;
1940 assert(handle
!= NULL
);
1942 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1943 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1948 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1950 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1951 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1953 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1957 static int stlink_usb_step(void *handle
)
1959 struct stlink_usb_handle_s
*h
= handle
;
1961 assert(handle
!= NULL
);
1963 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1964 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1965 * that the Cortex-M3 currently does. */
1966 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1967 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1968 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1971 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1973 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1974 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1976 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1980 static int stlink_usb_read_regs(void *handle
)
1983 struct stlink_usb_handle_s
*h
= handle
;
1985 assert(handle
!= NULL
);
1987 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1989 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1990 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1992 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1993 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
1994 /* regs data from offset 0 */
1996 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1997 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
1998 /* status at offset 0, regs data from offset 4 */
2005 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
2008 struct stlink_usb_handle_s
*h
= handle
;
2010 assert(handle
!= NULL
);
2012 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2014 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2015 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2016 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2018 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2019 h
->cmdbuf
[h
->cmdidx
++] = num
;
2021 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2022 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2023 if (res
!= ERROR_OK
)
2025 *val
= le_to_h_u32(h
->databuf
);
2028 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2029 if (res
!= ERROR_OK
)
2031 *val
= le_to_h_u32(h
->databuf
+ 4);
2037 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
2039 struct stlink_usb_handle_s
*h
= handle
;
2041 assert(handle
!= NULL
);
2043 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2045 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2046 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2047 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2049 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2050 h
->cmdbuf
[h
->cmdidx
++] = num
;
2051 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2054 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2057 static int stlink_usb_get_rw_status(void *handle
)
2059 struct stlink_usb_handle_s
*h
= handle
;
2061 assert(handle
!= NULL
);
2063 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2066 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2068 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2069 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2070 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2071 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2073 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2074 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2079 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2083 uint16_t read_len
= len
;
2084 struct stlink_usb_handle_s
*h
= handle
;
2086 assert(handle
!= NULL
);
2088 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2089 if (len
> stlink_usb_block(h
)) {
2090 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2094 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2096 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2097 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2098 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2100 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2103 /* we need to fix read length for single bytes */
2107 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2109 if (res
!= ERROR_OK
)
2112 memcpy(buffer
, h
->databuf
, len
);
2114 return stlink_usb_get_rw_status(handle
);
2118 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2119 const uint8_t *buffer
)
2122 struct stlink_usb_handle_s
*h
= handle
;
2124 assert(handle
!= NULL
);
2126 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2127 if (len
> stlink_usb_block(h
)) {
2128 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2132 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2134 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2135 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2136 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2138 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2141 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2143 if (res
!= ERROR_OK
)
2146 return stlink_usb_get_rw_status(handle
);
2150 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2154 struct stlink_usb_handle_s
*h
= handle
;
2156 assert(handle
!= NULL
);
2158 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2159 return ERROR_COMMAND_NOTFOUND
;
2161 /* data must be a multiple of 2 and half-word aligned */
2162 if (len
% 2 || addr
% 2) {
2163 LOG_DEBUG("Invalid data alignment");
2164 return ERROR_TARGET_UNALIGNED_ACCESS
;
2167 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2169 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2171 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2173 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2176 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2178 if (res
!= ERROR_OK
)
2181 memcpy(buffer
, h
->databuf
, len
);
2183 return stlink_usb_get_rw_status(handle
);
2187 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2188 const uint8_t *buffer
)
2191 struct stlink_usb_handle_s
*h
= handle
;
2193 assert(handle
!= NULL
);
2195 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2196 return ERROR_COMMAND_NOTFOUND
;
2198 /* data must be a multiple of 2 and half-word aligned */
2199 if (len
% 2 || addr
% 2) {
2200 LOG_DEBUG("Invalid data alignment");
2201 return ERROR_TARGET_UNALIGNED_ACCESS
;
2204 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2206 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2207 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2208 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2210 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2213 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2215 if (res
!= ERROR_OK
)
2218 return stlink_usb_get_rw_status(handle
);
2222 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2226 struct stlink_usb_handle_s
*h
= handle
;
2228 assert(handle
!= NULL
);
2230 /* data must be a multiple of 4 and word aligned */
2231 if (len
% 4 || addr
% 4) {
2232 LOG_DEBUG("Invalid data alignment");
2233 return ERROR_TARGET_UNALIGNED_ACCESS
;
2236 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2238 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2239 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2240 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2242 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2245 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2247 if (res
!= ERROR_OK
)
2250 memcpy(buffer
, h
->databuf
, len
);
2252 return stlink_usb_get_rw_status(handle
);
2256 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2257 const uint8_t *buffer
)
2260 struct stlink_usb_handle_s
*h
= handle
;
2262 assert(handle
!= NULL
);
2264 /* data must be a multiple of 4 and word aligned */
2265 if (len
% 4 || addr
% 4) {
2266 LOG_DEBUG("Invalid data alignment");
2267 return ERROR_TARGET_UNALIGNED_ACCESS
;
2270 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2272 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2273 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2274 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2276 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2279 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2281 if (res
!= ERROR_OK
)
2284 return stlink_usb_get_rw_status(handle
);
2287 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2289 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2290 if (max_tar_block
== 0)
2292 return max_tar_block
;
2295 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2296 uint32_t count
, uint8_t *buffer
)
2298 int retval
= ERROR_OK
;
2299 uint32_t bytes_remaining
;
2301 struct stlink_usb_handle_s
*h
= handle
;
2303 /* calculate byte count */
2306 /* switch to 8 bit if stlink does not support 16 bit memory read */
2307 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2312 bytes_remaining
= (size
!= 1) ?
2313 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2315 if (count
< bytes_remaining
)
2316 bytes_remaining
= count
;
2319 * all stlink support 8/32bit memory read/writes and only from
2320 * stlink V2J26 there is support for 16 bit memory read/write.
2321 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2326 /* When in jtag mode the stlink uses the auto-increment functionality.
2327 * However it expects us to pass the data correctly, this includes
2328 * alignment and any page boundaries. We already do this as part of the
2329 * adi_v5 implementation, but the stlink is a hla adapter and so this
2330 * needs implementing manually.
2331 * currently this only affects jtag mode, according to ST they do single
2332 * access in SWD mode - but this may change and so we do it for both modes */
2334 /* we first need to check for any unaligned bytes */
2335 if (addr
& (size
- 1)) {
2337 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2338 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2339 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2340 usleep((1<<retries
++) * 1000);
2343 if (retval
!= ERROR_OK
)
2345 buffer
+= head_bytes
;
2347 count
-= head_bytes
;
2348 bytes_remaining
-= head_bytes
;
2351 if (bytes_remaining
& (size
- 1))
2352 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2354 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2356 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2358 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2360 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2361 usleep((1<<retries
++) * 1000);
2364 if (retval
!= ERROR_OK
)
2367 buffer
+= bytes_remaining
;
2368 addr
+= bytes_remaining
;
2369 count
-= bytes_remaining
;
2375 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2376 uint32_t count
, const uint8_t *buffer
)
2378 int retval
= ERROR_OK
;
2379 uint32_t bytes_remaining
;
2381 struct stlink_usb_handle_s
*h
= handle
;
2383 /* calculate byte count */
2386 /* switch to 8 bit if stlink does not support 16 bit memory read */
2387 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2392 bytes_remaining
= (size
!= 1) ?
2393 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2395 if (count
< bytes_remaining
)
2396 bytes_remaining
= count
;
2399 * all stlink support 8/32bit memory read/writes and only from
2400 * stlink V2J26 there is support for 16 bit memory read/write.
2401 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2406 /* When in jtag mode the stlink uses the auto-increment functionality.
2407 * However it expects us to pass the data correctly, this includes
2408 * alignment and any page boundaries. We already do this as part of the
2409 * adi_v5 implementation, but the stlink is a hla adapter and so this
2410 * needs implementing manually.
2411 * currently this only affects jtag mode, according to ST they do single
2412 * access in SWD mode - but this may change and so we do it for both modes */
2414 /* we first need to check for any unaligned bytes */
2415 if (addr
& (size
- 1)) {
2417 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2418 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2419 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2420 usleep((1<<retries
++) * 1000);
2423 if (retval
!= ERROR_OK
)
2425 buffer
+= head_bytes
;
2427 count
-= head_bytes
;
2428 bytes_remaining
-= head_bytes
;
2431 if (bytes_remaining
& (size
- 1))
2432 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2434 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2436 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2439 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2440 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2441 usleep((1<<retries
++) * 1000);
2444 if (retval
!= ERROR_OK
)
2447 buffer
+= bytes_remaining
;
2448 addr
+= bytes_remaining
;
2449 count
-= bytes_remaining
;
2456 static int stlink_usb_override_target(const char *targetname
)
2458 return !strcmp(targetname
, "cortex_m");
2461 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2466 we only have low and high speed...
2467 before changing speed the SWIM_CSR HS bit
2471 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2472 if (retval
!= ERROR_OK
)
2473 LOG_ERROR("Unable to set adapter speed");
2476 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2479 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2482 int speed_index
= -1;
2483 int speed_diff
= INT_MAX
;
2484 int last_valid_speed
= -1;
2487 for (i
= 0; i
< map_size
; i
++) {
2490 last_valid_speed
= i
;
2491 if (khz
== map
[i
].speed
) {
2495 int current_diff
= khz
- map
[i
].speed
;
2496 /* get abs value for comparison */
2497 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2498 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2499 speed_diff
= current_diff
;
2505 if (speed_index
== -1) {
2506 /* this will only be here if we cannot match the slow speed.
2507 * use the slowest speed we support.*/
2508 speed_index
= last_valid_speed
;
2510 } else if (i
== map_size
)
2513 if (!match
&& query
) {
2514 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
2515 khz
, map
[speed_index
].speed
);
2521 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2524 struct stlink_usb_handle_s
*h
= handle
;
2526 /* old firmware cannot change it */
2527 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2530 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2531 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2534 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2535 if (result
!= ERROR_OK
) {
2536 LOG_ERROR("Unable to set adapter speed");
2541 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2544 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2547 struct stlink_usb_handle_s
*h
= handle
;
2549 /* old firmware cannot change it */
2550 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2553 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2554 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2557 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2558 if (result
!= ERROR_OK
) {
2559 LOG_ERROR("Unable to set adapter speed");
2564 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2567 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2571 LOG_DEBUG("Supported clock speeds are:");
2572 for (i
= 0; i
< map_size
; i
++)
2574 LOG_DEBUG("%d kHz", map
[i
].speed
);
2577 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2579 struct stlink_usb_handle_s
*h
= handle
;
2582 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2583 LOG_ERROR("Unknown command");
2587 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2589 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2590 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2591 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2593 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2595 int size
= h
->databuf
[8];
2597 if (size
> STLINK_V3_MAX_FREQ_NB
)
2598 size
= STLINK_V3_MAX_FREQ_NB
;
2600 for (i
= 0; i
< size
; i
++) {
2601 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2602 map
[i
].speed_divisor
= i
;
2605 /* set to zero all the next entries */
2606 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2612 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2614 struct stlink_usb_handle_s
*h
= handle
;
2616 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2617 LOG_ERROR("Unknown command");
2621 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2623 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2625 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2626 h
->cmdbuf
[h
->cmdidx
++] = 0;
2628 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2630 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2633 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2635 struct stlink_usb_handle_s
*h
= handle
;
2637 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2639 stlink_get_com_freq(h
, is_jtag
, map
);
2641 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2644 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2645 if (result
!= ERROR_OK
) {
2646 LOG_ERROR("Unable to set adapter speed");
2650 return map
[speed_index
].speed
;
2653 static int stlink_speed(void *handle
, int khz
, bool query
)
2655 struct stlink_usb_handle_s
*h
= handle
;
2660 switch (h
->st_mode
) {
2661 case STLINK_MODE_DEBUG_SWIM
:
2662 return stlink_speed_swim(handle
, khz
, query
);
2663 case STLINK_MODE_DEBUG_SWD
:
2664 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2665 return stlink_speed_v3(handle
, false, khz
, query
);
2667 return stlink_speed_swd(handle
, khz
, query
);
2669 case STLINK_MODE_DEBUG_JTAG
:
2670 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2671 return stlink_speed_v3(handle
, true, khz
, query
);
2673 return stlink_speed_jtag(handle
, khz
, query
);
2683 static int stlink_usb_close(void *handle
)
2687 enum stlink_mode emode
;
2688 struct stlink_usb_handle_s
*h
= handle
;
2691 res
= stlink_usb_current_mode(handle
, &mode
);
2694 /* do not exit if return code != ERROR_OK,
2695 it prevents us from closing jtag_libusb */
2697 if (res
== ERROR_OK
) {
2698 /* try to exit current mode */
2700 case STLINK_DEV_DFU_MODE
:
2701 emode
= STLINK_MODE_DFU
;
2703 case STLINK_DEV_DEBUG_MODE
:
2704 emode
= STLINK_MODE_DEBUG_SWD
;
2706 case STLINK_DEV_SWIM_MODE
:
2707 emode
= STLINK_MODE_DEBUG_SWIM
;
2709 case STLINK_DEV_BOOTLOADER_MODE
:
2710 case STLINK_DEV_MASS_MODE
:
2712 emode
= STLINK_MODE_UNKNOWN
;
2716 if (emode
!= STLINK_MODE_UNKNOWN
)
2717 stlink_usb_mode_leave(handle
, emode
);
2718 /* do not check return code, it prevent
2719 us from closing jtag_libusb */
2723 jtag_libusb_close(h
->fd
);
2730 /* Compute ST-Link serial number from the device descriptor
2731 * this function will help to work-around a bug in old ST-Link/V2 DFU
2732 * the buggy DFU returns an incorrect serial in the USB descriptor
2733 * example for the following serial "57FF72067265575742132067"
2734 * - the correct descriptor serial is:
2735 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
2736 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
2737 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
2738 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
2739 * so this case is managed by libusb_helper::string_descriptor_equal
2740 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
2741 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
2743 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
2744 * and then we have just to convert the raw data into printable characters using sprintf
2746 char *stlink_usb_get_alternate_serial(libusb_device_handle
*device
,
2747 struct libusb_device_descriptor
*dev_desc
)
2750 unsigned char desc_serial
[(STLINK_SERIAL_LEN
+ 1) * 2];
2752 if (dev_desc
->iSerialNumber
== 0)
2755 /* get the LANGID from String Descriptor Zero */
2756 usb_retval
= libusb_get_string_descriptor(device
, 0, 0, desc_serial
,
2757 sizeof(desc_serial
));
2759 if (usb_retval
< LIBUSB_SUCCESS
) {
2760 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2761 libusb_error_name(usb_retval
), usb_retval
);
2763 } else if (usb_retval
< 4) {
2764 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
2765 LOG_ERROR("could not get the LANGID");
2769 uint32_t langid
= desc_serial
[2] | (desc_serial
[3] << 8);
2771 /* get the serial */
2772 usb_retval
= libusb_get_string_descriptor(device
, dev_desc
->iSerialNumber
,
2773 langid
, desc_serial
, sizeof(desc_serial
));
2775 unsigned char len
= desc_serial
[0];
2777 if (usb_retval
< LIBUSB_SUCCESS
) {
2778 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2779 libusb_error_name(usb_retval
), usb_retval
);
2781 } else if (desc_serial
[1] != LIBUSB_DT_STRING
|| len
> usb_retval
) {
2782 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
2786 if (len
== ((STLINK_SERIAL_LEN
+ 1) * 2)) {
2787 /* good ST-Link adapter, this case is managed by
2788 * libusb::libusb_get_string_descriptor_ascii */
2790 } else if (len
!= ((STLINK_SERIAL_LEN
/ 2 + 1) * 2)) {
2791 LOG_ERROR("unexpected serial length (%d) in descriptor", len
);
2795 /* else (len == 26) => buggy ST-Link */
2797 char *alternate_serial
= malloc((STLINK_SERIAL_LEN
+ 1) * sizeof(char));
2798 if (alternate_serial
== NULL
)
2801 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
2802 sprintf(alternate_serial
+ i
, "%02X", desc_serial
[i
+ 2]);
2804 alternate_serial
[STLINK_SERIAL_LEN
] = '\0';
2806 return alternate_serial
;
2810 static int stlink_usb_open(struct hl_interface_param_s
*param
, enum stlink_mode mode
, void **fd
)
2812 int err
, retry_count
= 1;
2813 struct stlink_usb_handle_s
*h
;
2815 LOG_DEBUG("stlink_usb_open");
2817 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2820 LOG_DEBUG("malloc failed");
2826 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2827 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2828 h
->st_mode
, param
->vid
[i
], param
->pid
[i
],
2829 param
->serial
? param
->serial
: "");
2833 On certain host USB configurations(e.g. MacBook Air)
2834 STLINKv2 dongle seems to have its FW in a funky state if,
2835 after plugging it in, you try to use openocd with it more
2836 then once (by launching and closing openocd). In cases like
2837 that initial attempt to read the FW info via
2838 stlink_usb_version will fail and the device has to be reset
2839 in order to become operational.
2842 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
,
2843 &h
->fd
, stlink_usb_get_alternate_serial
) != ERROR_OK
) {
2844 LOG_ERROR("open failed");
2848 jtag_libusb_set_configuration(h
->fd
, 0);
2850 if (libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2851 LOG_DEBUG("claim interface failed");
2855 /* RX EP is common for all versions */
2856 h
->rx_ep
= STLINK_RX_EP
;
2859 if (jtag_libusb_get_pid(libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2860 LOG_DEBUG("libusb_get_pid failed");
2864 /* wrap version for first read */
2867 h
->version
.stlink
= 1;
2868 h
->tx_ep
= STLINK_TX_EP
;
2870 case STLINK_V3_USBLOADER_PID
:
2871 case STLINK_V3E_PID
:
2872 case STLINK_V3S_PID
:
2873 case STLINK_V3_2VCP_PID
:
2874 h
->version
.stlink
= 3;
2875 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2876 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2878 case STLINK_V2_1_PID
:
2879 case STLINK_V2_1_NO_MSD_PID
:
2880 h
->version
.stlink
= 2;
2881 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2882 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2885 /* fall through - we assume V2 to be the default version*/
2887 h
->version
.stlink
= 2;
2888 h
->tx_ep
= STLINK_TX_EP
;
2889 h
->trace_ep
= STLINK_TRACE_EP
;
2893 /* get the device version */
2894 err
= stlink_usb_version(h
);
2896 if (err
== ERROR_OK
) {
2898 } else if (h
->version
.stlink
== 1 ||
2900 LOG_ERROR("read version failed");
2903 err
= libusb_release_interface(h
->fd
, 0);
2904 if (err
!= ERROR_OK
) {
2905 LOG_ERROR("release interface failed");
2909 err
= libusb_reset_device(h
->fd
);
2910 if (err
!= ERROR_OK
) {
2911 LOG_ERROR("reset device failed");
2915 jtag_libusb_close(h
->fd
);
2917 Give the device one second to settle down and
2920 usleep(1 * 1000 * 1000);
2925 /* check if mode is supported */
2928 switch (h
->st_mode
) {
2929 case STLINK_MODE_DEBUG_SWD
:
2930 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2933 case STLINK_MODE_DEBUG_JTAG
:
2934 if (h
->version
.jtag
== 0)
2937 case STLINK_MODE_DEBUG_SWIM
:
2938 if (h
->version
.swim
== 0)
2946 if (err
!= ERROR_OK
) {
2947 LOG_ERROR("mode (transport) not supported by device");
2951 /* initialize the debug hardware */
2952 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
2954 if (err
!= ERROR_OK
) {
2955 LOG_ERROR("init mode failed (unable to connect to the target)");
2959 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
2960 err
= stlink_swim_enter(h
);
2961 if (err
!= ERROR_OK
) {
2962 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2966 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2970 /* get cpuid, so we can determine the max page size
2971 * start with a safe default */
2972 h
->max_mem_packet
= (1 << 10);
2975 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2976 if (err
== ERROR_OK
) {
2977 uint32_t cpuid
= le_to_h_u32(buffer
);
2978 int i
= (cpuid
>> 4) & 0xf;
2979 if (i
== 4 || i
== 3) {
2980 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2981 h
->max_mem_packet
= (1 << 12);
2985 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2992 stlink_usb_close(h
);
2997 static int stlink_usb_hl_open(struct hl_interface_param_s
*param
, void **fd
)
2999 return stlink_usb_open(param
, stlink_get_mode(param
->transport
), fd
);
3002 int stlink_config_trace(void *handle
, bool enabled
,
3003 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3004 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3005 uint16_t *prescaler
)
3007 struct stlink_usb_handle_s
*h
= handle
;
3010 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
3011 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
3012 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3017 stlink_usb_trace_disable(h
);
3021 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
3022 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3023 STLINK_TRACE_MAX_HZ
);
3027 stlink_usb_trace_disable(h
);
3030 *trace_freq
= STLINK_TRACE_MAX_HZ
;
3032 presc
= traceclkin_freq
/ *trace_freq
;
3034 if (traceclkin_freq
% *trace_freq
> 0)
3037 if (presc
> TPIU_ACPR_MAX_SWOSCALER
) {
3038 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3044 h
->trace
.source_hz
= *trace_freq
;
3046 return stlink_usb_trace_enable(h
);
3050 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
3052 struct stlink_usb_handle_s
*h
= handle
;
3054 assert(handle
!= NULL
);
3056 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3057 return ERROR_COMMAND_NOTFOUND
;
3059 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
3060 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3061 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3062 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
3063 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3065 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3069 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
3071 struct stlink_usb_handle_s
*h
= handle
;
3073 assert(handle
!= NULL
);
3075 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3076 return ERROR_COMMAND_NOTFOUND
;
3078 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
3079 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3080 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3081 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG
;
3082 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3084 /* ignore incorrectly returned error on bogus FW */
3085 if (h
->version
.flags
& STLINK_F_FIX_CLOSE_AP
)
3086 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3088 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
3093 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3094 unsigned short addr
, uint32_t *val
)
3096 struct stlink_usb_handle_s
*h
= handle
;
3099 assert(handle
!= NULL
);
3101 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3102 return ERROR_COMMAND_NOTFOUND
;
3104 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3105 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3106 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3107 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3108 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3110 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3111 *val
= le_to_h_u32(h
->databuf
+ 4);
3112 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, *val
);
3117 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3118 unsigned short addr
, uint32_t val
)
3120 struct stlink_usb_handle_s
*h
= handle
;
3122 assert(handle
!= NULL
);
3124 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3125 return ERROR_COMMAND_NOTFOUND
;
3127 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, val
);
3128 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3129 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3130 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3131 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3132 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3133 h_u32_to_le(&h
->cmdbuf
[6], val
);
3134 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3138 struct hl_layout_api_s stlink_usb_layout_api
= {
3140 .open
= stlink_usb_hl_open
,
3142 .close
= stlink_usb_close
,
3144 .idcode
= stlink_usb_idcode
,
3146 .state
= stlink_usb_state
,
3148 .reset
= stlink_usb_reset
,
3150 .assert_srst
= stlink_usb_assert_srst
,
3152 .run
= stlink_usb_run
,
3154 .halt
= stlink_usb_halt
,
3156 .step
= stlink_usb_step
,
3158 .read_regs
= stlink_usb_read_regs
,
3160 .read_reg
= stlink_usb_read_reg
,
3162 .write_reg
= stlink_usb_write_reg
,
3164 .read_mem
= stlink_usb_read_mem
,
3166 .write_mem
= stlink_usb_write_mem
,
3168 .write_debug_reg
= stlink_usb_write_debug_reg
,
3170 .override_target
= stlink_usb_override_target
,
3172 .speed
= stlink_speed
,
3174 .config_trace
= stlink_config_trace
,
3176 .poll_trace
= stlink_usb_trace_read
,
3179 /*****************************************************************************
3180 * DAP direct interface
3183 static struct stlink_usb_handle_s
*stlink_dap_handle
;
3184 static struct hl_interface_param_s stlink_dap_param
;
3185 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
3186 static int stlink_dap_error
= ERROR_OK
;
3188 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3192 static int stlink_dap_record_error(int error
)
3194 if (stlink_dap_error
== ERROR_OK
)
3195 stlink_dap_error
= error
;
3200 static int stlink_dap_get_and_clear_error(void)
3202 int retval
= stlink_dap_error
;
3203 stlink_dap_error
= ERROR_OK
;
3208 static int stlink_dap_open_ap(unsigned short apsel
)
3212 /* nothing to do on old versions */
3213 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3216 if (apsel
> DP_APSEL_MAX
)
3219 if (test_bit(apsel
, opened_ap
))
3222 retval
= stlink_usb_init_access_port(stlink_dap_handle
, apsel
);
3223 if (retval
!= ERROR_OK
)
3226 LOG_DEBUG("AP %d enabled", apsel
);
3227 set_bit(apsel
, opened_ap
);
3232 static int stlink_dap_closeall_ap(void)
3236 /* nothing to do on old versions */
3237 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3240 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
3241 if (!test_bit(apsel
, opened_ap
))
3243 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
3244 if (retval
!= ERROR_OK
)
3246 clear_bit(apsel
, opened_ap
);
3252 static int stlink_dap_reinit_interface(void)
3257 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
3258 * some firmware version it does not work as expected, and there is no
3259 * equivalent for SWD.
3260 * At least for now, to reset the interface quit from JTAG/SWD mode then
3261 * select the mode again.
3264 if (!stlink_dap_handle
->reconnect_pending
) {
3265 stlink_dap_handle
->reconnect_pending
= true;
3266 stlink_usb_mode_leave(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
3269 retval
= stlink_usb_mode_enter(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
3270 if (retval
!= ERROR_OK
)
3273 stlink_dap_handle
->reconnect_pending
= false;
3274 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
3275 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
3276 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
3277 if (test_bit(apsel
, opened_ap
)) {
3278 clear_bit(apsel
, opened_ap
);
3279 stlink_dap_open_ap(apsel
);
3285 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
3290 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
3292 /* Check if we should reset srst already when connecting, but not if reconnecting. */
3293 if (!dap
->do_reconnect
) {
3294 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3296 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3297 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3298 adapter_assert_reset();
3300 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3304 dap
->do_reconnect
= false;
3305 dap_invalidate_cache(dap
);
3307 retval
= dap_dp_init(dap
);
3308 if (retval
!= ERROR_OK
) {
3309 dap
->do_reconnect
= true;
3313 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
3314 if (retval
== ERROR_OK
)
3315 LOG_INFO("%s %#8.8" PRIx32
,
3316 (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
3319 dap
->do_reconnect
= true;
3325 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
3329 if (!dap
->do_reconnect
)
3332 retval
= stlink_dap_reinit_interface();
3333 if (retval
!= ERROR_OK
)
3336 return stlink_dap_op_connect(dap
);
3340 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
3342 /* Ignore the request */
3347 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3353 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3354 if (reg
& 0x000000F0) {
3355 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3356 return ERROR_COMMAND_NOTFOUND
;
3359 retval
= stlink_dap_check_reconnect(dap
);
3360 if (retval
!= ERROR_OK
)
3363 data
= data
? : &dummy
;
3364 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
3365 && stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) {
3366 /* Quirk required in JTAG. Read RDBUFF to get the data */
3367 retval
= stlink_read_dap_register(stlink_dap_handle
,
3368 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
3369 if (retval
== ERROR_OK
)
3370 retval
= stlink_read_dap_register(stlink_dap_handle
,
3371 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
3373 retval
= stlink_read_dap_register(stlink_dap_handle
,
3374 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3377 return stlink_dap_record_error(retval
);
3381 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned reg
,
3386 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3387 if (reg
& 0x000000F0) {
3388 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3389 return ERROR_COMMAND_NOTFOUND
;
3392 if (reg
== DP_SELECT
&& (data
& DP_SELECT_DPBANK
) != 0) {
3393 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
3394 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
3395 data
&= ~DP_SELECT_DPBANK
;
3398 retval
= stlink_dap_check_reconnect(dap
);
3399 if (retval
!= ERROR_OK
)
3402 /* ST-Link does not like that we set CORUNDETECT */
3403 if (reg
== DP_CTRL_STAT
)
3404 data
&= ~CORUNDETECT
;
3406 retval
= stlink_write_dap_register(stlink_dap_handle
,
3407 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3408 return stlink_dap_record_error(retval
);
3412 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned reg
,
3415 struct adiv5_dap
*dap
= ap
->dap
;
3419 retval
= stlink_dap_check_reconnect(dap
);
3420 if (retval
!= ERROR_OK
)
3423 if (reg
!= AP_REG_IDR
) {
3424 retval
= stlink_dap_open_ap(ap
->ap_num
);
3425 if (retval
!= ERROR_OK
)
3428 data
= data
? : &dummy
;
3429 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3431 dap
->stlink_flush_ap_write
= false;
3432 return stlink_dap_record_error(retval
);
3436 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned reg
,
3439 struct adiv5_dap
*dap
= ap
->dap
;
3442 retval
= stlink_dap_check_reconnect(dap
);
3443 if (retval
!= ERROR_OK
)
3446 retval
= stlink_dap_open_ap(ap
->ap_num
);
3447 if (retval
!= ERROR_OK
)
3450 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3452 dap
->stlink_flush_ap_write
= true;
3453 return stlink_dap_record_error(retval
);
3457 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
3459 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
3464 static int stlink_dap_op_run(struct adiv5_dap
*dap
)
3466 uint32_t ctrlstat
, pwrmask
;
3467 int retval
, saved_retval
;
3469 /* Here no LOG_DEBUG. This is called continuously! */
3472 * ST-Link returns immediately after a DAP write, without waiting for it
3474 * Run a dummy read to DP_RDBUFF, as suggested in
3475 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
3477 if (dap
->stlink_flush_ap_write
) {
3478 dap
->stlink_flush_ap_write
= false;
3479 retval
= stlink_dap_op_queue_dp_read(dap
, DP_RDBUFF
, NULL
);
3480 if (retval
!= ERROR_OK
) {
3481 dap
->do_reconnect
= true;
3486 saved_retval
= stlink_dap_get_and_clear_error();
3488 retval
= stlink_dap_op_queue_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
3489 if (retval
!= ERROR_OK
) {
3490 dap
->do_reconnect
= true;
3493 retval
= stlink_dap_get_and_clear_error();
3494 if (retval
!= ERROR_OK
) {
3495 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
3496 dap
->do_reconnect
= true;
3500 if (ctrlstat
& SSTICKYERR
) {
3501 if (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
)
3502 retval
= stlink_dap_op_queue_dp_write(dap
, DP_CTRL_STAT
,
3503 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
3505 retval
= stlink_dap_op_queue_dp_write(dap
, DP_ABORT
, STKERRCLR
);
3506 if (retval
!= ERROR_OK
) {
3507 dap
->do_reconnect
= true;
3510 retval
= stlink_dap_get_and_clear_error();
3511 if (retval
!= ERROR_OK
) {
3512 dap
->do_reconnect
= true;
3517 /* check for power lost */
3518 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
3519 if ((ctrlstat
& pwrmask
) != pwrmask
)
3520 dap
->do_reconnect
= true;
3522 return saved_retval
;
3526 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
3530 retval
= stlink_dap_closeall_ap();
3531 if (retval
!= ERROR_OK
)
3532 LOG_ERROR("Error closing APs");
3535 static int stlink_swim_op_srst(void)
3537 return stlink_swim_generate_rst(stlink_dap_handle
);
3540 static int stlink_swim_op_read_mem(uint32_t addr
, uint32_t size
,
3541 uint32_t count
, uint8_t *buffer
)
3544 uint32_t bytes_remaining
;
3546 LOG_DEBUG_IO("read at 0x%08x len %d*0x%08x", addr
, size
, count
);
3550 bytes_remaining
= (count
> STLINK_DATA_SIZE
) ? STLINK_DATA_SIZE
: count
;
3551 retval
= stlink_swim_readbytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
3552 if (retval
!= ERROR_OK
)
3555 buffer
+= bytes_remaining
;
3556 addr
+= bytes_remaining
;
3557 count
-= bytes_remaining
;
3563 static int stlink_swim_op_write_mem(uint32_t addr
, uint32_t size
,
3564 uint32_t count
, const uint8_t *buffer
)
3567 uint32_t bytes_remaining
;
3569 LOG_DEBUG_IO("write at 0x%08x len %d*0x%08x", addr
, size
, count
);
3573 bytes_remaining
= (count
> STLINK_DATA_SIZE
) ? STLINK_DATA_SIZE
: count
;
3574 retval
= stlink_swim_writebytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
3575 if (retval
!= ERROR_OK
)
3578 buffer
+= bytes_remaining
;
3579 addr
+= bytes_remaining
;
3580 count
-= bytes_remaining
;
3586 static int stlink_swim_op_reconnect(void)
3590 retval
= stlink_usb_mode_enter(stlink_dap_handle
, STLINK_MODE_DEBUG_SWIM
);
3591 if (retval
!= ERROR_OK
)
3594 return stlink_swim_resync(stlink_dap_handle
);
3597 static int stlink_dap_config_trace(bool enabled
,
3598 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3599 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3600 uint16_t *prescaler
)
3602 return stlink_config_trace(stlink_dap_handle
, enabled
, pin_protocol
,
3603 port_size
, trace_freq
, traceclkin_freq
,
3607 static int stlink_dap_trace_read(uint8_t *buf
, size_t *size
)
3609 return stlink_usb_trace_read(stlink_dap_handle
, buf
, size
);
3613 COMMAND_HANDLER(stlink_dap_serial_command
)
3615 LOG_DEBUG("stlink_dap_serial_command");
3617 if (CMD_ARGC
!= 1) {
3618 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
3619 return ERROR_COMMAND_SYNTAX_ERROR
;
3622 if (stlink_dap_param
.serial
) {
3623 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
3624 free((void *)stlink_dap_param
.serial
);
3627 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
3632 COMMAND_HANDLER(stlink_dap_vid_pid
)
3634 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
3636 if (CMD_ARGC
> max_usb_ids
* 2) {
3637 LOG_WARNING("ignoring extra IDs in vid_pid "
3638 "(maximum is %d pairs)", max_usb_ids
);
3639 CMD_ARGC
= max_usb_ids
* 2;
3641 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3642 LOG_WARNING("incomplete vid_pid configuration directive");
3643 return ERROR_COMMAND_SYNTAX_ERROR
;
3645 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3646 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
3647 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
3650 /* null termination */
3651 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
3657 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
3660 .handler
= stlink_dap_serial_command
,
3661 .mode
= COMMAND_CONFIG
,
3662 .help
= "set the serial number of the adapter",
3663 .usage
= "<serial_number>",
3667 .handler
= stlink_dap_vid_pid
,
3668 .mode
= COMMAND_CONFIG
,
3669 .help
= "USB VID and PID of the adapter",
3670 .usage
= "(vid pid)+",
3672 COMMAND_REGISTRATION_DONE
3676 static const struct command_registration stlink_dap_command_handlers
[] = {
3679 .mode
= COMMAND_ANY
,
3680 .help
= "perform st-link management",
3681 .chain
= stlink_dap_subcommand_handlers
,
3684 COMMAND_REGISTRATION_DONE
3688 static int stlink_dap_init(void)
3690 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3691 enum stlink_mode mode
;
3694 LOG_DEBUG("stlink_dap_init()");
3696 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3697 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3698 stlink_dap_param
.connect_under_reset
= true;
3700 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3703 if (transport_is_dapdirect_swd())
3704 mode
= STLINK_MODE_DEBUG_SWD
;
3705 else if (transport_is_dapdirect_jtag())
3706 mode
= STLINK_MODE_DEBUG_JTAG
;
3707 else if (transport_is_swim())
3708 mode
= STLINK_MODE_DEBUG_SWIM
;
3710 LOG_ERROR("Unsupported transport");
3714 retval
= stlink_usb_open(&stlink_dap_param
, mode
, (void **)&stlink_dap_handle
);
3715 if (retval
!= ERROR_OK
)
3718 if ((mode
!= STLINK_MODE_DEBUG_SWIM
) &&
3719 !(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
3720 LOG_ERROR("ST-Link version does not support DAP direct transport");
3727 static int stlink_dap_quit(void)
3729 LOG_DEBUG("stlink_dap_quit()");
3731 free((void *)stlink_dap_param
.serial
);
3732 stlink_dap_param
.serial
= NULL
;
3734 return stlink_usb_close(stlink_dap_handle
);
3738 static int stlink_dap_reset(int req_trst
, int req_srst
)
3740 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
3741 return stlink_usb_assert_srst(stlink_dap_handle
,
3742 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
3743 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
3747 static int stlink_dap_speed(int speed
)
3750 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
3751 return ERROR_JTAG_NOT_IMPLEMENTED
;
3754 stlink_dap_param
.initial_interface_speed
= speed
;
3755 stlink_speed(stlink_dap_handle
, speed
, false);
3760 static int stlink_dap_khz(int khz
, int *jtag_speed
)
3763 LOG_ERROR("RCLK not supported");
3767 *jtag_speed
= stlink_speed(stlink_dap_handle
, khz
, true);
3772 static int stlink_dap_speed_div(int speed
, int *khz
)
3778 static const struct dap_ops stlink_dap_ops
= {
3779 .connect
= stlink_dap_op_connect
,
3780 .send_sequence
= stlink_dap_op_send_sequence
,
3781 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
3782 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
3783 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
3784 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
3785 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
3786 .run
= stlink_dap_op_run
,
3787 .sync
= NULL
, /* optional */
3788 .quit
= stlink_dap_op_quit
, /* optional */
3791 static const struct swim_driver stlink_swim_ops
= {
3792 .srst
= stlink_swim_op_srst
,
3793 .read_mem
= stlink_swim_op_read_mem
,
3794 .write_mem
= stlink_swim_op_write_mem
,
3795 .reconnect
= stlink_swim_op_reconnect
,
3798 static const char *const stlink_dap_transport
[] = { "dapdirect_swd", "dapdirect_jtag", "swim", NULL
};
3800 struct adapter_driver stlink_dap_adapter_driver
= {
3802 .transports
= stlink_dap_transport
,
3803 .commands
= stlink_dap_command_handlers
,
3805 .init
= stlink_dap_init
,
3806 .quit
= stlink_dap_quit
,
3807 .reset
= stlink_dap_reset
,
3808 .speed
= stlink_dap_speed
,
3809 .khz
= stlink_dap_khz
,
3810 .speed_div
= stlink_dap_speed_div
,
3811 .config_trace
= stlink_dap_config_trace
,
3812 .poll_trace
= stlink_dap_trace_read
,
3814 .dap_jtag_ops
= &stlink_dap_ops
,
3815 .dap_swd_ops
= &stlink_dap_ops
,
3816 .swim_ops
= &stlink_swim_ops
,