1 /***************************************************************************
2 * Copyright (C) 2020 by Tarek Bochkati *
3 * Tarek Bochkati <tarek.bouchkati@gmail.com> *
5 * SWIM contributions by Ake Rehnman *
6 * Copyright (C) 2017 Ake Rehnman *
7 * ake.rehnman(at)gmail.com *
9 * Copyright (C) 2011-2012 by Mathias Kuester *
10 * Mathias Kuester <kesmtp@freenet.de> *
12 * Copyright (C) 2012 by Spencer Oliver *
13 * spen@spen-soft.co.uk *
15 * This code is based on https://github.com/texane/stlink *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
29 ***************************************************************************/
35 /* project specific includes */
36 #include <helper/align.h>
37 #include <helper/binarybuffer.h>
38 #include <helper/bits.h>
39 #include <helper/system.h>
40 #include <helper/time_support.h>
41 #include <jtag/interface.h>
42 #include <jtag/hla/hla_layout.h>
43 #include <jtag/hla/hla_transport.h>
44 #include <jtag/hla/hla_interface.h>
45 #include <jtag/swim.h>
46 #include <target/arm_adi_v5.h>
47 #include <target/target.h>
48 #include <transport/transport.h>
50 #include <target/cortex_m.h>
52 #include <helper/system.h>
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
58 #ifdef HAVE_NETINET_TCP_H
59 #include <netinet/tcp.h>
62 #include "libusb_helper.h"
65 #define USE_LIBUSB_ASYNCIO
68 #define STLINK_SERIAL_LEN 24
70 #define ENDPOINT_IN 0x80
71 #define ENDPOINT_OUT 0x00
73 #define STLINK_WRITE_TIMEOUT 1000
74 #define STLINK_READ_TIMEOUT 1000
76 #define STLINK_RX_EP (1|ENDPOINT_IN)
77 #define STLINK_TX_EP (2|ENDPOINT_OUT)
78 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
80 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
81 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
83 #define STLINK_SG_SIZE (31)
84 #define STLINK_DATA_SIZE (6144)
85 #define STLINK_CMD_SIZE_V2 (16)
86 #define STLINK_CMD_SIZE_V1 (10)
88 #define STLINK_V1_PID (0x3744)
89 #define STLINK_V2_PID (0x3748)
90 #define STLINK_V2_1_PID (0x374B)
91 #define STLINK_V2_1_NO_MSD_PID (0x3752)
92 #define STLINK_V3_USBLOADER_PID (0x374D)
93 #define STLINK_V3E_PID (0x374E)
94 #define STLINK_V3S_PID (0x374F)
95 #define STLINK_V3_2VCP_PID (0x3753)
96 #define STLINK_V3E_NO_MSD_PID (0x3754)
99 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
100 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
101 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
103 * For 16 and 32bit read/writes stlink handles USB packet split and the limit
104 * is the internal buffer size of 6144 bytes.
105 * TODO: override ADIv5 layer's tar_autoincr_block that limits the transfer
106 * to 1024 or 4096 bytes
108 #define STLINK_MAX_RW8 (64)
109 #define STLINKV3_MAX_RW8 (512)
110 #define STLINK_MAX_RW16_32 STLINK_DATA_SIZE
111 #define STLINK_SWIM_DATA_SIZE STLINK_DATA_SIZE
113 /* "WAIT" responses will be retried (with exponential backoff) at
114 * most this many times before failing to caller.
116 #define MAX_WAIT_RETRIES 8
118 /* HLA is currently limited at AP#0 and no control on CSW */
119 #define STLINK_HLA_AP_NUM 0
120 #define STLINK_HLA_CSW 0
122 enum stlink_jtag_api_version
{
123 STLINK_JTAG_API_V1
= 1,
129 STLINK_MODE_UNKNOWN
= 0,
132 STLINK_MODE_DEBUG_JTAG
,
133 STLINK_MODE_DEBUG_SWD
,
134 STLINK_MODE_DEBUG_SWIM
138 struct stlink_usb_version
{
145 /** jtag api version supported */
146 enum stlink_jtag_api_version jtag_api
;
147 /** one bit for each feature supported. See macros STLINK_F_* */
151 struct stlink_usb_priv_s
{
153 struct libusb_device_handle
*fd
;
155 struct libusb_transfer
*trans
;
158 struct stlink_tcp_priv_s
{
173 struct stlink_backend_s
{
175 int (*open
)(void *handle
, struct hl_interface_param_s
*param
);
177 int (*close
)(void *handle
);
179 int (*xfer_noerrcheck
)(void *handle
, const uint8_t *buf
, int size
);
181 int (*read_trace
)(void *handle
, const uint8_t *buf
, int size
);
184 /* TODO: make queue size dynamic */
185 /* TODO: don't allocate queue for HLA */
186 #define MAX_QUEUE_DEPTH (4096)
196 * encode the bytes size in the enum's value. This makes easy to extract it
197 * with a simple logic AND, by using the macro CMD_MEM_AP_2_SIZE() below
199 CMD_MEM_AP_READ8
= 0x10 + 1,
200 CMD_MEM_AP_READ16
= 0x10 + 2,
201 CMD_MEM_AP_READ32
= 0x10 + 4,
203 CMD_MEM_AP_WRITE8
= 0x20 + 1,
204 CMD_MEM_AP_WRITE16
= 0x20 + 2,
205 CMD_MEM_AP_WRITE32
= 0x20 + 4,
208 #define CMD_MEM_AP_2_SIZE(cmd) ((cmd) & 7)
215 struct adiv5_dap
*dap
;
220 struct adiv5_dap
*dap
;
232 bool changes_csw_default
;
247 struct stlink_usb_handle_s
{
249 struct stlink_backend_s
*backend
;
252 struct stlink_usb_priv_s usb_backend_priv
;
253 struct stlink_tcp_priv_s tcp_backend_priv
;
270 uint32_t max_mem_packet
;
272 enum stlink_mode st_mode
;
274 struct stlink_usb_version version
;
281 /** whether SWO tracing is enabled or not */
283 /** trace module source clock */
286 /** reconnect is needed next time we try to query the
288 bool reconnect_pending
;
289 /** queue of dap_direct operations */
290 struct dap_queue queue
[MAX_QUEUE_DEPTH
];
291 /** first element available in the queue */
292 unsigned int queue_index
;
296 static inline int stlink_usb_open(void *handle
, struct hl_interface_param_s
*param
)
298 struct stlink_usb_handle_s
*h
= handle
;
299 return h
->backend
->open(handle
, param
);
303 static inline int stlink_usb_close(void *handle
)
305 struct stlink_usb_handle_s
*h
= handle
;
306 return h
->backend
->close(handle
);
309 static inline int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
311 struct stlink_usb_handle_s
*h
= handle
;
312 return h
->backend
->xfer_noerrcheck(handle
, buf
, size
);
315 #define STLINK_SWIM_ERR_OK 0x00
316 #define STLINK_SWIM_BUSY 0x01
317 #define STLINK_DEBUG_ERR_OK 0x80
318 #define STLINK_DEBUG_ERR_FAULT 0x81
319 #define STLINK_SWD_AP_WAIT 0x10
320 #define STLINK_SWD_AP_FAULT 0x11
321 #define STLINK_SWD_AP_ERROR 0x12
322 #define STLINK_SWD_AP_PARITY_ERROR 0x13
323 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
324 #define STLINK_JTAG_WRITE_ERROR 0x0c
325 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
326 #define STLINK_SWD_DP_WAIT 0x14
327 #define STLINK_SWD_DP_FAULT 0x15
328 #define STLINK_SWD_DP_ERROR 0x16
329 #define STLINK_SWD_DP_PARITY_ERROR 0x17
331 #define STLINK_SWD_AP_WDATA_ERROR 0x18
332 #define STLINK_SWD_AP_STICKY_ERROR 0x19
333 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
335 #define STLINK_BAD_AP_ERROR 0x1d
337 #define STLINK_CORE_RUNNING 0x80
338 #define STLINK_CORE_HALTED 0x81
339 #define STLINK_CORE_STAT_UNKNOWN -1
341 #define STLINK_GET_VERSION 0xF1
342 #define STLINK_DEBUG_COMMAND 0xF2
343 #define STLINK_DFU_COMMAND 0xF3
344 #define STLINK_SWIM_COMMAND 0xF4
345 #define STLINK_GET_CURRENT_MODE 0xF5
346 #define STLINK_GET_TARGET_VOLTAGE 0xF7
348 #define STLINK_DEV_DFU_MODE 0x00
349 #define STLINK_DEV_MASS_MODE 0x01
350 #define STLINK_DEV_DEBUG_MODE 0x02
351 #define STLINK_DEV_SWIM_MODE 0x03
352 #define STLINK_DEV_BOOTLOADER_MODE 0x04
353 #define STLINK_DEV_UNKNOWN_MODE -1
355 #define STLINK_DFU_EXIT 0x07
358 STLINK_SWIM_ENTER_SEQ
359 1.3ms low then 750Hz then 1.5kHz
362 STM8 DM pulls reset pin low 50us
365 uint8_t (0=low|1=high)
372 send synchronization seq (16us low, response 64 clocks low)
374 #define STLINK_SWIM_ENTER 0x00
375 #define STLINK_SWIM_EXIT 0x01
376 #define STLINK_SWIM_READ_CAP 0x02
377 #define STLINK_SWIM_SPEED 0x03
378 #define STLINK_SWIM_ENTER_SEQ 0x04
379 #define STLINK_SWIM_GEN_RST 0x05
380 #define STLINK_SWIM_RESET 0x06
381 #define STLINK_SWIM_ASSERT_RESET 0x07
382 #define STLINK_SWIM_DEASSERT_RESET 0x08
383 #define STLINK_SWIM_READSTATUS 0x09
384 #define STLINK_SWIM_WRITEMEM 0x0a
385 #define STLINK_SWIM_READMEM 0x0b
386 #define STLINK_SWIM_READBUF 0x0c
388 #define STLINK_DEBUG_GETSTATUS 0x01
389 #define STLINK_DEBUG_FORCEDEBUG 0x02
390 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
391 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
392 #define STLINK_DEBUG_APIV1_READREG 0x05
393 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
394 #define STLINK_DEBUG_READMEM_32BIT 0x07
395 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
396 #define STLINK_DEBUG_RUNCORE 0x09
397 #define STLINK_DEBUG_STEPCORE 0x0a
398 #define STLINK_DEBUG_APIV1_SETFP 0x0b
399 #define STLINK_DEBUG_READMEM_8BIT 0x0c
400 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
401 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
402 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
403 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
405 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
406 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
407 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
409 #define STLINK_DEBUG_APIV1_ENTER 0x20
410 #define STLINK_DEBUG_EXIT 0x21
411 #define STLINK_DEBUG_READCOREID 0x22
413 #define STLINK_DEBUG_APIV2_ENTER 0x30
414 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
415 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
416 #define STLINK_DEBUG_APIV2_READREG 0x33
417 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
418 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
419 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
421 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
422 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
423 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
425 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
427 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
428 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
429 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
430 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
431 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
432 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
433 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
434 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
435 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
437 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
438 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
440 #define STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC 0x50
441 #define STLINK_DEBUG_APIV2_RW_MISC_OUT 0x51
442 #define STLINK_DEBUG_APIV2_RW_MISC_IN 0x52
444 #define STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC 0x54
446 #define STLINK_APIV3_SET_COM_FREQ 0x61
447 #define STLINK_APIV3_GET_COM_FREQ 0x62
449 #define STLINK_APIV3_GET_VERSION_EX 0xFB
451 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
452 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
453 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
455 #define STLINK_DEBUG_PORT_ACCESS 0xffff
457 #define STLINK_TRACE_SIZE 4096
458 #define STLINK_TRACE_MAX_HZ 2000000
459 #define STLINK_V3_TRACE_MAX_HZ 24000000
461 #define STLINK_V3_MAX_FREQ_NB 10
463 #define REQUEST_SENSE 0x03
464 #define REQUEST_SENSE_LENGTH 18
466 /* STLINK TCP commands */
467 #define STLINK_TCP_CMD_REFRESH_DEVICE_LIST 0x00
468 #define STLINK_TCP_CMD_GET_NB_DEV 0x01
469 #define STLINK_TCP_CMD_GET_DEV_INFO 0x02
470 #define STLINK_TCP_CMD_OPEN_DEV 0x03
471 #define STLINK_TCP_CMD_CLOSE_DEV 0x04
472 #define STLINK_TCP_CMD_SEND_USB_CMD 0x05
473 #define STLINK_TCP_CMD_GET_SERVER_VERSION 0x06
474 #define STLINK_TCP_CMD_GET_NB_OF_DEV_CLIENTS 0x07
476 /* STLINK TCP constants */
477 #define OPENOCD_STLINK_TCP_API_VERSION 1
478 #define STLINK_TCP_REQUEST_WRITE 0
479 #define STLINK_TCP_REQUEST_READ 1
480 #define STLINK_TCP_REQUEST_READ_SWO 3
481 #define STLINK_TCP_SS_SIZE 4
482 #define STLINK_TCP_USB_CMD_SIZE 32
483 #define STLINK_TCP_SERIAL_SIZE 32
484 #define STLINK_TCP_SEND_BUFFER_SIZE 10240
485 #define STLINK_TCP_RECV_BUFFER_SIZE 10240
487 /* STLINK TCP command status */
488 #define STLINK_TCP_SS_OK 0x00000001
489 #define STLINK_TCP_SS_MEMORY_PROBLEM 0x00001000
490 #define STLINK_TCP_SS_TIMEOUT 0x00001001
491 #define STLINK_TCP_SS_BAD_PARAMETER 0x00001002
492 #define STLINK_TCP_SS_OPEN_ERR 0x00001003
493 #define STLINK_TCP_SS_TRUNCATED_DATA 0x00001052
494 #define STLINK_TCP_SS_CMD_NOT_AVAILABLE 0x00001053
495 #define STLINK_TCP_SS_TCP_ERROR 0x00002001
496 #define STLINK_TCP_SS_TCP_CANT_CONNECT 0x00002002
497 #define STLINK_TCP_SS_WIN32_ERROR 0x00010000
500 * Map the relevant features, quirks and workaround for specific firmware
503 #define STLINK_F_HAS_TRACE BIT(0) /* v2>=j13 || v3 */
504 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(1) /* v2>=j15 || v3 */
505 #define STLINK_F_HAS_SWD_SET_FREQ BIT(2) /* v2>=j22 */
506 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(3) /* v2>=j24 */
507 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(4) /* v2>=j24 && v2<j32 */
508 #define STLINK_F_HAS_DAP_REG BIT(5) /* v2>=j24 || v3 */
509 #define STLINK_F_HAS_MEM_16BIT BIT(6) /* v2>=j26 || v3 */
510 #define STLINK_F_HAS_AP_INIT BIT(7) /* v2>=j28 || v3 */
511 #define STLINK_F_FIX_CLOSE_AP BIT(8) /* v2>=j29 || v3 */
512 #define STLINK_F_HAS_DPBANKSEL BIT(9) /* v2>=j32 || v3>=j2 */
513 #define STLINK_F_HAS_RW8_512BYTES BIT(10) /* v3>=j6 */
516 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
517 #define STLINK_F_HAS_FPU_REG STLINK_F_HAS_GETLASTRWSTATUS2
518 #define STLINK_F_HAS_MEM_WR_NO_INC STLINK_F_HAS_MEM_16BIT
519 #define STLINK_F_HAS_MEM_RD_NO_INC STLINK_F_HAS_DPBANKSEL
520 #define STLINK_F_HAS_RW_MISC STLINK_F_HAS_DPBANKSEL
521 #define STLINK_F_HAS_CSW STLINK_F_HAS_DPBANKSEL
523 #define STLINK_REGSEL_IS_FPU(x) ((x) > 0x1F)
530 /* SWD clock speed */
531 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
533 {1800, 1}, /* default */
546 /* JTAG clock speed */
547 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
551 {1125, 32}, /* default */
557 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
558 static int stlink_swim_status(void *handle
);
559 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
560 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
561 static int stlink_speed(void *handle
, int khz
, bool query
);
562 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
);
565 static unsigned int stlink_usb_block(void *handle
)
567 struct stlink_usb_handle_s
*h
= handle
;
571 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
572 return STLINKV3_MAX_RW8
;
574 return STLINK_MAX_RW8
;
577 #ifdef USE_LIBUSB_ASYNCIO
579 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
581 int *completed
= transfer
->user_data
;
583 /* caller interprets result and frees transfer */
587 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
589 int r
, *completed
= transfer
->user_data
;
591 while (!*completed
) {
592 r
= jtag_libusb_handle_events_completed(completed
);
594 if (r
== LIBUSB_ERROR_INTERRUPTED
)
596 libusb_cancel_transfer(transfer
);
603 static int transfer_error_status(const struct libusb_transfer
*transfer
)
607 switch (transfer
->status
) {
608 case LIBUSB_TRANSFER_COMPLETED
:
611 case LIBUSB_TRANSFER_TIMED_OUT
:
612 r
= LIBUSB_ERROR_TIMEOUT
;
614 case LIBUSB_TRANSFER_STALL
:
615 r
= LIBUSB_ERROR_PIPE
;
617 case LIBUSB_TRANSFER_OVERFLOW
:
618 r
= LIBUSB_ERROR_OVERFLOW
;
620 case LIBUSB_TRANSFER_NO_DEVICE
:
621 r
= LIBUSB_ERROR_NO_DEVICE
;
623 case LIBUSB_TRANSFER_ERROR
:
624 case LIBUSB_TRANSFER_CANCELLED
:
628 r
= LIBUSB_ERROR_OTHER
;
642 size_t transfer_size
;
643 struct libusb_transfer
*transfer
;
646 static int jtag_libusb_bulk_transfer_n(
647 struct libusb_device_handle
*dev_handle
,
648 struct jtag_xfer
*transfers
,
653 int returnval
= ERROR_OK
;
656 for (size_t i
= 0; i
< n_transfers
; ++i
) {
657 transfers
[i
].retval
= 0;
658 transfers
[i
].completed
= 0;
659 transfers
[i
].transfer_size
= 0;
660 transfers
[i
].transfer
= libusb_alloc_transfer(0);
662 if (!transfers
[i
].transfer
) {
663 for (size_t j
= 0; j
< i
; ++j
)
664 libusb_free_transfer(transfers
[j
].transfer
);
666 LOG_DEBUG("ERROR, failed to alloc usb transfers");
667 for (size_t k
= 0; k
< n_transfers
; ++k
)
668 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
673 for (size_t i
= 0; i
< n_transfers
; ++i
) {
674 libusb_fill_bulk_transfer(
675 transfers
[i
].transfer
,
677 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
678 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
679 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
681 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
683 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
685 /* Probably no point continuing to submit transfers once a submission fails.
686 * As a result, tag all remaining transfers as errors.
688 for (size_t j
= i
; j
< n_transfers
; ++j
)
689 transfers
[j
].retval
= retval
;
691 returnval
= ERROR_FAIL
;
696 /* Wait for every submitted USB transfer to complete.
698 for (size_t i
= 0; i
< n_transfers
; ++i
) {
699 if (transfers
[i
].retval
== 0) {
700 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
702 retval
= transfer_error_status(transfers
[i
].transfer
);
704 returnval
= ERROR_FAIL
;
705 transfers
[i
].retval
= retval
;
706 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
708 /* Assuming actual_length is only valid if there is no transfer error.
710 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
714 libusb_free_transfer(transfers
[i
].transfer
);
715 transfers
[i
].transfer
= NULL
;
725 static int stlink_usb_xfer_v1_get_status(void *handle
)
727 struct stlink_usb_handle_s
*h
= handle
;
733 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
735 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
736 STLINK_READ_TIMEOUT
, &tr
);
742 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
745 if (t1
!= 0x53425355)
753 if (h
->cmdbuf
[12] != 0)
759 #ifdef USE_LIBUSB_ASYNCIO
760 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
762 struct stlink_usb_handle_s
*h
= handle
;
766 size_t n_transfers
= 0;
767 struct jtag_xfer transfers
[2];
769 memset(transfers
, 0, sizeof(transfers
));
771 transfers
[0].ep
= h
->tx_ep
;
772 transfers
[0].buf
= h
->cmdbuf
;
773 transfers
[0].size
= cmdsize
;
777 if (h
->direction
== h
->tx_ep
&& size
) {
778 transfers
[1].ep
= h
->tx_ep
;
779 transfers
[1].buf
= (uint8_t *)buf
;
780 transfers
[1].size
= size
;
783 } else if (h
->direction
== h
->rx_ep
&& size
) {
784 transfers
[1].ep
= h
->rx_ep
;
785 transfers
[1].buf
= (uint8_t *)buf
;
786 transfers
[1].size
= size
;
791 return jtag_libusb_bulk_transfer_n(
792 h
->usb_backend_priv
.fd
,
795 STLINK_WRITE_TIMEOUT
);
798 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
800 struct stlink_usb_handle_s
*h
= handle
;
805 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
806 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
807 if (ret
|| tr
!= cmdsize
)
810 if (h
->direction
== h
->tx_ep
&& size
) {
811 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)buf
,
812 size
, STLINK_WRITE_TIMEOUT
, &tr
);
813 if (ret
|| tr
!= size
) {
814 LOG_DEBUG("bulk write failed");
817 } else if (h
->direction
== h
->rx_ep
&& size
) {
818 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)buf
,
819 size
, STLINK_READ_TIMEOUT
, &tr
);
820 if (ret
|| tr
!= size
) {
821 LOG_DEBUG("bulk read failed");
831 static int stlink_usb_xfer_v1_get_sense(void *handle
)
834 struct stlink_usb_handle_s
*h
= handle
;
838 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
840 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
841 h
->cmdbuf
[h
->cmdidx
++] = 0;
842 h
->cmdbuf
[h
->cmdidx
++] = 0;
843 h
->cmdbuf
[h
->cmdidx
++] = 0;
844 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
846 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
851 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
858 static int stlink_usb_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
860 struct stlink_usb_handle_s
*h
= handle
;
863 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->trace_ep
, (char *)buf
, size
,
864 STLINK_READ_TIMEOUT
, &tr
);
865 if (ret
|| tr
!= size
) {
866 LOG_ERROR("bulk trace read failed");
874 transfers block in cmdbuf
875 <size> indicates number of bytes in the following
877 Ignore the (eventual) error code in the received packet.
879 static int stlink_usb_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
881 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
882 struct stlink_usb_handle_s
*h
= handle
;
886 if (h
->version
.stlink
== 1) {
887 cmdsize
= STLINK_SG_SIZE
;
888 /* put length in bCBWCBLength */
889 h
->cmdbuf
[14] = h
->cmdidx
-15;
892 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
897 if (h
->version
.stlink
== 1) {
898 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
899 /* check csw status */
900 if (h
->cmdbuf
[12] == 1) {
901 LOG_DEBUG("get sense");
902 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
913 static int stlink_tcp_send_cmd(void *handle
, int send_size
, int recv_size
, bool check_tcp_status
)
915 struct stlink_usb_handle_s
*h
= handle
;
919 /* send the TCP command */
920 int sent_size
= send(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.send_buf
, send_size
, 0);
921 if (sent_size
!= send_size
) {
922 LOG_ERROR("failed to send USB CMD");
924 LOG_DEBUG("socket send error: %s (errno %d)", strerror(errno
), errno
);
926 LOG_DEBUG("sent size %d (expected %d)", sent_size
, send_size
);
930 /* read the TCP response */
931 int retval
= ERROR_OK
;
932 int remaining_bytes
= recv_size
;
933 uint8_t *recv_buf
= h
->tcp_backend_priv
.recv_buf
;
934 const int64_t timeout
= timeval_ms() + 1000; /* 1 second */
936 while (remaining_bytes
> 0) {
937 if (timeval_ms() > timeout
) {
938 LOG_DEBUG("received size %d (expected %d)", recv_size
- remaining_bytes
, recv_size
);
939 retval
= ERROR_TIMEOUT_REACHED
;
944 int received
= recv(h
->tcp_backend_priv
.fd
, (void *)recv_buf
, remaining_bytes
, 0);
946 if (received
== -1) {
947 LOG_DEBUG("socket recv error: %s (errno %d)", strerror(errno
), errno
);
952 recv_buf
+= received
;
953 remaining_bytes
-= received
;
956 if (retval
!= ERROR_OK
) {
957 LOG_ERROR("failed to receive USB CMD response");
961 if (check_tcp_status
) {
962 uint32_t tcp_ss
= le_to_h_u32(h
->tcp_backend_priv
.recv_buf
);
963 if (tcp_ss
!= STLINK_TCP_SS_OK
) {
964 LOG_ERROR("TCP error status 0x%X", tcp_ss
);
973 static int stlink_tcp_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
975 struct stlink_usb_handle_s
*h
= handle
;
977 int send_size
= STLINK_TCP_USB_CMD_SIZE
;
978 int recv_size
= STLINK_TCP_SS_SIZE
;
982 /* prepare the TCP command */
983 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_SEND_USB_CMD
;
984 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 3); /* reserved for alignment and future use, must be zero */
985 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
986 /* tcp_backend_priv.send_buf[8..23] already contains the constructed stlink command */
987 h
->tcp_backend_priv
.send_buf
[24] = h
->direction
;
988 memset(&h
->tcp_backend_priv
.send_buf
[25], 0, 3); /* reserved for alignment and future use, must be zero */
990 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[28], size
);
993 * if the xfer is a write request (tx_ep)
994 * > then buf content will be copied
996 * else : the xfer is a read or trace read request (rx_ep or trace_ep)
997 * > the buf content will be filled from &databuf[4].
999 * note : if h->direction is trace_ep, h->cmdbuf is zeros.
1002 if (h
->direction
== h
->tx_ep
) { /* STLINK_TCP_REQUEST_WRITE */
1004 if (send_size
> STLINK_TCP_SEND_BUFFER_SIZE
) {
1005 LOG_ERROR("STLINK_TCP command buffer overflow");
1008 memcpy(&h
->tcp_backend_priv
.send_buf
[32], buf
, size
);
1009 } else { /* STLINK_TCP_REQUEST_READ or STLINK_TCP_REQUEST_READ_SWO */
1011 if (recv_size
> STLINK_TCP_RECV_BUFFER_SIZE
) {
1012 LOG_ERROR("STLINK_TCP data buffer overflow");
1017 int ret
= stlink_tcp_send_cmd(h
, send_size
, recv_size
, true);
1018 if (ret
!= ERROR_OK
)
1021 if (h
->direction
!= h
->tx_ep
) {
1022 /* the read data is located in tcp_backend_priv.recv_buf[4] */
1023 /* most of the case it will be copying the data from tcp_backend_priv.recv_buf[4]
1024 * to handle->cmd_buff which are the same, so let's avoid unnecessary copying */
1025 if (buf
!= &h
->tcp_backend_priv
.recv_buf
[4])
1026 memcpy((uint8_t *)buf
, &h
->tcp_backend_priv
.recv_buf
[4], size
);
1033 static int stlink_tcp_read_trace(void *handle
, const uint8_t *buf
, int size
)
1035 struct stlink_usb_handle_s
*h
= handle
;
1037 stlink_usb_init_buffer(h
, h
->trace_ep
, 0);
1038 return stlink_tcp_xfer_noerrcheck(handle
, buf
, size
);
1042 Converts an STLINK status code held in the first byte of a response
1043 to an openocd error, logs any error/wait status as debug output.
1045 static int stlink_usb_error_check(void *handle
)
1047 struct stlink_usb_handle_s
*h
= handle
;
1051 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1052 switch (h
->databuf
[0]) {
1053 case STLINK_SWIM_ERR_OK
:
1055 case STLINK_SWIM_BUSY
:
1058 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1063 /* TODO: no error checking yet on api V1 */
1064 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1065 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
1067 switch (h
->databuf
[0]) {
1068 case STLINK_DEBUG_ERR_OK
:
1070 case STLINK_DEBUG_ERR_FAULT
:
1071 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
1073 case STLINK_SWD_AP_WAIT
:
1074 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
1076 case STLINK_SWD_DP_WAIT
:
1077 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
1079 case STLINK_JTAG_GET_IDCODE_ERROR
:
1080 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
1082 case STLINK_JTAG_WRITE_ERROR
:
1083 LOG_DEBUG("Write error");
1085 case STLINK_JTAG_WRITE_VERIF_ERROR
:
1086 LOG_DEBUG("Write verify error, ignoring");
1088 case STLINK_SWD_AP_FAULT
:
1089 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
1090 * returns ERROR_OK with the comment:
1091 * Change in error status when reading outside RAM.
1092 * This fix allows CDT plugin to visualize memory.
1094 LOG_DEBUG("STLINK_SWD_AP_FAULT");
1096 case STLINK_SWD_AP_ERROR
:
1097 LOG_DEBUG("STLINK_SWD_AP_ERROR");
1099 case STLINK_SWD_AP_PARITY_ERROR
:
1100 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
1102 case STLINK_SWD_DP_FAULT
:
1103 LOG_DEBUG("STLINK_SWD_DP_FAULT");
1105 case STLINK_SWD_DP_ERROR
:
1106 LOG_DEBUG("STLINK_SWD_DP_ERROR");
1108 case STLINK_SWD_DP_PARITY_ERROR
:
1109 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
1111 case STLINK_SWD_AP_WDATA_ERROR
:
1112 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
1114 case STLINK_SWD_AP_STICKY_ERROR
:
1115 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
1117 case STLINK_SWD_AP_STICKYORUN_ERROR
:
1118 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
1120 case STLINK_BAD_AP_ERROR
:
1121 LOG_DEBUG("STLINK_BAD_AP_ERROR");
1124 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1130 * Wrapper around stlink_usb_xfer_noerrcheck()
1131 * to check the error code in the received packet
1133 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
1139 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1140 if (retval
!= ERROR_OK
)
1143 return stlink_usb_error_check(handle
);
1146 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
1148 Works for commands where the STLINK_DEBUG status is returned in the first
1149 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
1151 Returns an openocd result code.
1153 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
1157 struct stlink_usb_handle_s
*h
= handle
;
1160 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
1161 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1162 if (res
!= ERROR_OK
)
1166 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1167 res
= stlink_swim_status(handle
);
1168 if (res
!= ERROR_OK
)
1172 res
= stlink_usb_error_check(handle
);
1173 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1174 unsigned int delay_us
= (1<<retries
++) * 1000;
1175 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
1184 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
1186 struct stlink_usb_handle_s
*h
= handle
;
1190 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1192 return h
->backend
->read_trace(handle
, buf
, size
);
1196 this function writes transfer length in
1197 the right place in the cb
1199 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
1201 struct stlink_usb_handle_s
*h
= handle
;
1203 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
1206 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
1208 struct stlink_usb_handle_s
*h
= handle
;
1210 /* fill the send buffer */
1211 strcpy((char *)h
->cmdbuf
, "USBC");
1213 /* csw tag not used */
1214 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
1216 /* cbw data transfer length (in the following data phase in or out) */
1217 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
1220 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
1221 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
1222 /* cdb clength (is filled in at xfer) */
1223 h
->cmdbuf
[h
->cmdidx
++] = 0;
1227 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
1229 struct stlink_usb_handle_s
*h
= handle
;
1231 h
->direction
= direction
;
1235 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
1236 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
1238 if (h
->version
.stlink
== 1)
1239 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
1243 static int stlink_usb_version(void *handle
)
1248 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
1249 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
1251 struct stlink_usb_handle_s
*h
= handle
;
1255 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
1257 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
1259 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
1261 if (res
!= ERROR_OK
)
1264 version
= be_to_h_u16(h
->databuf
);
1265 v
= (version
>> 12) & 0x0f;
1266 x
= (version
>> 6) & 0x3f;
1269 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
1270 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
1273 case STLINK_V2_1_PID
:
1274 case STLINK_V2_1_NO_MSD_PID
:
1275 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
1276 /* MxSy : STM8 V2.1 - SWIM only */
1281 /* JxMy : STM32 V2.1 - JTAG/SWD only */
1294 /* STLINK-V3 requires a specific command */
1295 if (v
== 3 && x
== 0 && y
== 0) {
1296 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
1298 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
1300 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
1301 if (res
!= ERROR_OK
)
1305 swim
= h
->databuf
[1];
1306 jtag
= h
->databuf
[2];
1307 msd
= h
->databuf
[3];
1308 bridge
= h
->databuf
[4];
1309 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
1310 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
1313 h
->version
.stlink
= v
;
1314 h
->version
.jtag
= jtag
;
1315 h
->version
.swim
= swim
;
1318 switch (h
->version
.stlink
) {
1320 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
1321 if (h
->version
.jtag
>= 11)
1322 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1324 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
1328 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1329 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1331 /* API for trace from J13 */
1332 /* API for target voltage from J13 */
1333 if (h
->version
.jtag
>= 13)
1334 flags
|= STLINK_F_HAS_TRACE
;
1336 /* preferred API to get last R/W status from J15 */
1337 if (h
->version
.jtag
>= 15)
1338 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1340 /* API to set SWD frequency from J22 */
1341 if (h
->version
.jtag
>= 22)
1342 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1344 /* API to set JTAG frequency from J24 */
1345 /* API to access DAP registers from J24 */
1346 if (h
->version
.jtag
>= 24) {
1347 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1348 flags
|= STLINK_F_HAS_DAP_REG
;
1351 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1352 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1353 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1355 /* API to read/write memory at 16 bit from J26 */
1356 /* API to write memory without address increment from J26 */
1357 if (h
->version
.jtag
>= 26)
1358 flags
|= STLINK_F_HAS_MEM_16BIT
;
1360 /* API required to init AP before any AP access from J28 */
1361 if (h
->version
.jtag
>= 28)
1362 flags
|= STLINK_F_HAS_AP_INIT
;
1364 /* API required to return proper error code on close AP from J29 */
1365 if (h
->version
.jtag
>= 29)
1366 flags
|= STLINK_F_FIX_CLOSE_AP
;
1368 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1369 /* API to read memory without address increment from V2J32 */
1370 /* Memory R/W supports CSW from V2J32 */
1371 if (h
->version
.jtag
>= 32)
1372 flags
|= STLINK_F_HAS_DPBANKSEL
;
1376 /* all STLINK-V3 use api-v3 */
1377 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1379 /* STLINK-V3 is a superset of ST-LINK/V2 */
1382 /* API for target voltage */
1383 flags
|= STLINK_F_HAS_TRACE
;
1385 /* preferred API to get last R/W status */
1386 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1388 /* API to access DAP registers */
1389 flags
|= STLINK_F_HAS_DAP_REG
;
1391 /* API to read/write memory at 16 bit */
1392 /* API to write memory without address increment */
1393 flags
|= STLINK_F_HAS_MEM_16BIT
;
1395 /* API required to init AP before any AP access */
1396 flags
|= STLINK_F_HAS_AP_INIT
;
1398 /* API required to return proper error code on close AP */
1399 flags
|= STLINK_F_FIX_CLOSE_AP
;
1401 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1402 /* API to read memory without address increment from V3J2 */
1403 /* Memory R/W supports CSW from V3J2 */
1404 if (h
->version
.jtag
>= 2)
1405 flags
|= STLINK_F_HAS_DPBANKSEL
;
1407 /* 8bit read/write max packet size 512 bytes from V3J6 */
1408 if (h
->version
.jtag
>= 6)
1409 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1415 h
->version
.flags
= flags
;
1418 p
+= sprintf(p
, "V%d", v
);
1420 p
+= sprintf(p
, "J%d", jtag
);
1422 p
+= sprintf(p
, "M%d", msd
);
1424 p
+= sprintf(p
, "B%d", bridge
);
1426 sprintf(p
, "S%d", swim
);
1428 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1430 h
->version
.jtag_api
,
1437 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1439 struct stlink_usb_handle_s
*h
= handle
;
1440 uint32_t adc_results
[2];
1442 /* no error message, simply quit with error */
1443 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1444 return ERROR_COMMAND_NOTFOUND
;
1446 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1448 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1450 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1452 if (result
!= ERROR_OK
)
1455 /* convert result */
1456 adc_results
[0] = le_to_h_u32(h
->databuf
);
1457 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1459 *target_voltage
= 0;
1462 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1464 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1469 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1471 struct stlink_usb_handle_s
*h
= handle
;
1475 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1476 return ERROR_COMMAND_NOTFOUND
;
1478 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1480 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1481 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1482 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1485 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1487 if (result
!= ERROR_OK
)
1493 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1495 struct stlink_usb_handle_s
*h
= handle
;
1499 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1500 return ERROR_COMMAND_NOTFOUND
;
1502 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1504 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1505 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1506 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1509 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1511 if (result
!= ERROR_OK
)
1518 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1521 struct stlink_usb_handle_s
*h
= handle
;
1525 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1527 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1529 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1531 if (res
!= ERROR_OK
)
1534 *mode
= h
->databuf
[0];
1540 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1543 struct stlink_usb_handle_s
*h
= handle
;
1547 /* on api V2 we are able the read the latest command
1549 * TODO: we need the test on api V1 too
1551 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1554 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1557 case STLINK_MODE_DEBUG_JTAG
:
1558 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1559 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1560 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1562 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1563 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1565 case STLINK_MODE_DEBUG_SWD
:
1566 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1567 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1568 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1570 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1571 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1573 case STLINK_MODE_DEBUG_SWIM
:
1574 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1575 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1576 /* swim enter does not return any response or status */
1577 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1578 case STLINK_MODE_DFU
:
1579 case STLINK_MODE_MASS
:
1584 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1588 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1591 struct stlink_usb_handle_s
*h
= handle
;
1595 /* command with no reply, use a valid endpoint but zero size */
1596 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1599 case STLINK_MODE_DEBUG_JTAG
:
1600 case STLINK_MODE_DEBUG_SWD
:
1601 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1602 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1604 case STLINK_MODE_DEBUG_SWIM
:
1605 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1606 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1608 case STLINK_MODE_DFU
:
1609 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1610 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1612 case STLINK_MODE_MASS
:
1617 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1619 if (res
!= ERROR_OK
)
1625 static int stlink_usb_assert_srst(void *handle
, int srst
);
1627 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1630 case HL_TRANSPORT_SWD
:
1631 return STLINK_MODE_DEBUG_SWD
;
1632 case HL_TRANSPORT_JTAG
:
1633 return STLINK_MODE_DEBUG_JTAG
;
1635 return STLINK_MODE_UNKNOWN
;
1640 static int stlink_usb_exit_mode(void *handle
)
1644 enum stlink_mode emode
;
1648 res
= stlink_usb_current_mode(handle
, &mode
);
1650 if (res
!= ERROR_OK
)
1653 LOG_DEBUG("MODE: 0x%02X", mode
);
1655 /* try to exit current mode */
1657 case STLINK_DEV_DFU_MODE
:
1658 emode
= STLINK_MODE_DFU
;
1660 case STLINK_DEV_DEBUG_MODE
:
1661 emode
= STLINK_MODE_DEBUG_SWD
;
1663 case STLINK_DEV_SWIM_MODE
:
1664 emode
= STLINK_MODE_DEBUG_SWIM
;
1666 case STLINK_DEV_BOOTLOADER_MODE
:
1667 case STLINK_DEV_MASS_MODE
:
1669 emode
= STLINK_MODE_UNKNOWN
;
1673 if (emode
!= STLINK_MODE_UNKNOWN
)
1674 return stlink_usb_mode_leave(handle
, emode
);
1680 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1684 enum stlink_mode emode
;
1685 struct stlink_usb_handle_s
*h
= handle
;
1689 res
= stlink_usb_exit_mode(handle
);
1690 if (res
!= ERROR_OK
)
1693 res
= stlink_usb_current_mode(handle
, &mode
);
1695 if (res
!= ERROR_OK
)
1698 /* we check the target voltage here as an aid to debugging connection problems.
1699 * the stlink requires the target Vdd to be connected for reliable debugging.
1700 * this cmd is supported in all modes except DFU
1702 if (mode
!= STLINK_DEV_DFU_MODE
) {
1704 float target_voltage
;
1706 /* check target voltage (if supported) */
1707 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1709 if (res
!= ERROR_OK
) {
1710 if (res
!= ERROR_COMMAND_NOTFOUND
)
1711 LOG_ERROR("voltage check failed");
1712 /* attempt to continue as it is not a catastrophic failure */
1714 /* check for a sensible target voltage, operating range is 1.65-5.5v
1715 * according to datasheet */
1716 if (target_voltage
< 1.5)
1717 LOG_ERROR("target voltage may be too low for reliable debugging");
1721 LOG_DEBUG("MODE: 0x%02X", mode
);
1723 /* set selected mode */
1726 if (emode
== STLINK_MODE_UNKNOWN
) {
1727 LOG_ERROR("selected mode (transport) not supported");
1731 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1732 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1733 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1734 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1735 stlink_speed(h
, initial_interface_speed
, false);
1737 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1738 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1739 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1740 stlink_speed(h
, initial_interface_speed
, false);
1744 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
&&
1745 (emode
== STLINK_MODE_DEBUG_JTAG
|| emode
== STLINK_MODE_DEBUG_SWD
)) {
1746 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1748 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1749 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1750 stlink_speed(h
, initial_interface_speed
, false);
1753 /* preliminary SRST assert:
1754 * We want SRST is asserted before activating debug signals (mode_enter).
1755 * As the required mode has not been set, the adapter may not know what pin to use.
1756 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1757 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1758 * after power on, SWIM_RST stays unchanged */
1759 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1760 stlink_usb_assert_srst(handle
, 0);
1761 /* do not check the return status here, we will
1762 proceed and enter the desired mode below
1763 and try asserting srst again. */
1765 res
= stlink_usb_mode_enter(handle
, emode
);
1766 if (res
!= ERROR_OK
)
1769 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1770 if (connect_under_reset
) {
1771 res
= stlink_usb_assert_srst(handle
, 0);
1772 if (res
!= ERROR_OK
)
1776 res
= stlink_usb_current_mode(handle
, &mode
);
1778 if (res
!= ERROR_OK
)
1781 LOG_DEBUG("MODE: 0x%02X", mode
);
1786 /* request status from last swim request */
1787 static int stlink_swim_status(void *handle
)
1789 struct stlink_usb_handle_s
*h
= handle
;
1792 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1793 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1794 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1795 /* error is checked by the caller */
1796 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1797 if (res
!= ERROR_OK
)
1802 the purpose of this function is unknown...
1803 capabilities? anyway for swim v6 it returns
1806 __attribute__((unused
))
1807 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1809 struct stlink_usb_handle_s
*h
= handle
;
1812 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1813 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1814 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1815 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1816 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1817 if (res
!= ERROR_OK
)
1819 memcpy(cap
, h
->databuf
, 8);
1823 /* debug dongle assert/deassert sreset line */
1824 static int stlink_swim_assert_reset(void *handle
, int reset
)
1826 struct stlink_usb_handle_s
*h
= handle
;
1829 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1830 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1832 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1835 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1836 if (res
!= ERROR_OK
)
1843 1.3ms low then 750Hz then 1.5kHz
1845 static int stlink_swim_enter(void *handle
)
1847 struct stlink_usb_handle_s
*h
= handle
;
1850 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1851 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1852 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1853 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1854 if (res
!= ERROR_OK
)
1859 /* switch high/low speed swim */
1860 static int stlink_swim_speed(void *handle
, int speed
)
1862 struct stlink_usb_handle_s
*h
= handle
;
1865 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1866 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1867 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1869 h
->cmdbuf
[h
->cmdidx
++] = 1;
1871 h
->cmdbuf
[h
->cmdidx
++] = 0;
1872 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1873 if (res
!= ERROR_OK
)
1879 initiate srst from swim.
1880 nrst is pulled low for 50us.
1882 static int stlink_swim_generate_rst(void *handle
)
1884 struct stlink_usb_handle_s
*h
= handle
;
1887 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1888 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1889 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1890 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1891 if (res
!= ERROR_OK
)
1897 send resynchronize sequence
1898 swim is pulled low for 16us
1899 reply is 64 clks low
1901 static int stlink_swim_resync(void *handle
)
1903 struct stlink_usb_handle_s
*h
= handle
;
1906 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1907 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1908 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1909 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1910 if (res
!= ERROR_OK
)
1915 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1917 struct stlink_usb_handle_s
*h
= handle
;
1920 unsigned int datalen
= 0;
1921 int cmdsize
= STLINK_CMD_SIZE_V2
;
1923 if (len
> STLINK_SWIM_DATA_SIZE
)
1926 if (h
->version
.stlink
== 1)
1927 cmdsize
= STLINK_SG_SIZE
;
1929 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1930 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1931 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1932 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1934 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1936 for (i
= 0; i
< len
; i
++) {
1937 if (h
->cmdidx
== cmdsize
)
1938 h
->databuf
[datalen
++] = *(data
++);
1940 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1942 if (h
->version
.stlink
== 1)
1943 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1945 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1946 if (res
!= ERROR_OK
)
1951 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1953 struct stlink_usb_handle_s
*h
= handle
;
1956 if (len
> STLINK_SWIM_DATA_SIZE
)
1959 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1960 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1961 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1962 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1964 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1966 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1967 if (res
!= ERROR_OK
)
1970 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1971 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1972 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1973 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1974 if (res
!= ERROR_OK
)
1981 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1984 struct stlink_usb_handle_s
*h
= handle
;
1988 /* there is no swim read core id cmd */
1989 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1994 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1996 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1997 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1998 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
2000 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2003 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
2005 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2009 if (res
!= ERROR_OK
)
2012 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
2014 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
2019 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
2021 struct stlink_usb_handle_s
*h
= handle
;
2026 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
2028 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2029 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
2030 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2033 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2034 if (res
!= ERROR_OK
)
2037 *val
= le_to_h_u32(h
->databuf
+ 4);
2041 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
2043 struct stlink_usb_handle_s
*h
= handle
;
2047 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2049 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2050 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2051 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
2053 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
2054 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2056 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2059 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2063 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
2065 struct stlink_usb_handle_s
*h
= handle
;
2069 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
2072 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2074 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2075 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
2077 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2078 if (res
!= ERROR_OK
)
2081 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
2082 *size
= bytes_avail
< *size
? bytes_avail
: *size
;
2085 res
= stlink_usb_read_trace(handle
, buf
, *size
);
2086 if (res
!= ERROR_OK
)
2095 static enum target_state
stlink_usb_v2_get_status(void *handle
)
2100 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
2101 if (result
!= ERROR_OK
)
2102 return TARGET_UNKNOWN
;
2104 if (status
& S_HALT
)
2105 return TARGET_HALTED
;
2106 else if (status
& S_RESET_ST
)
2107 return TARGET_RESET
;
2109 return TARGET_RUNNING
;
2113 static enum target_state
stlink_usb_state(void *handle
)
2116 struct stlink_usb_handle_s
*h
= handle
;
2120 if (h
->reconnect_pending
) {
2121 LOG_INFO("Previous state query failed, trying to reconnect");
2122 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
2123 if (res
!= ERROR_OK
)
2124 return TARGET_UNKNOWN
;
2126 h
->reconnect_pending
= false;
2129 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2130 res
= stlink_usb_v2_get_status(handle
);
2131 if (res
== TARGET_UNKNOWN
)
2132 h
->reconnect_pending
= true;
2136 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2138 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2139 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
2141 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2143 if (res
!= ERROR_OK
)
2144 return TARGET_UNKNOWN
;
2146 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
2147 return TARGET_RUNNING
;
2148 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
2149 return TARGET_HALTED
;
2151 h
->reconnect_pending
= true;
2153 return TARGET_UNKNOWN
;
2156 static int stlink_usb_assert_srst(void *handle
, int srst
)
2158 struct stlink_usb_handle_s
*h
= handle
;
2162 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
2163 return stlink_swim_assert_reset(handle
, srst
);
2165 if (h
->version
.stlink
== 1)
2166 return ERROR_COMMAND_NOTFOUND
;
2168 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2171 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
2172 h
->cmdbuf
[h
->cmdidx
++] = srst
;
2174 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2178 static void stlink_usb_trace_disable(void *handle
)
2181 struct stlink_usb_handle_s
*h
= handle
;
2185 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
2187 LOG_DEBUG("Tracing: disable");
2189 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2190 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2191 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
2192 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2194 if (res
== ERROR_OK
)
2195 h
->trace
.enabled
= false;
2200 static int stlink_usb_trace_enable(void *handle
)
2203 struct stlink_usb_handle_s
*h
= handle
;
2207 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
2208 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2211 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
2212 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
2214 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
2217 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2219 if (res
== ERROR_OK
) {
2220 h
->trace
.enabled
= true;
2221 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
2224 LOG_ERROR("Tracing is not supported by this version.");
2232 static int stlink_usb_reset(void *handle
)
2234 struct stlink_usb_handle_s
*h
= handle
;
2239 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2241 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2243 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2244 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
2246 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
2248 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2249 if (retval
!= ERROR_OK
)
2252 if (h
->trace
.enabled
) {
2253 stlink_usb_trace_disable(h
);
2254 return stlink_usb_trace_enable(h
);
2261 static int stlink_usb_run(void *handle
)
2264 struct stlink_usb_handle_s
*h
= handle
;
2268 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2269 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
2274 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2276 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2277 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
2279 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2283 static int stlink_usb_halt(void *handle
)
2286 struct stlink_usb_handle_s
*h
= handle
;
2290 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2291 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2296 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2298 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2299 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
2301 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2305 static int stlink_usb_step(void *handle
)
2307 struct stlink_usb_handle_s
*h
= handle
;
2311 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2312 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
2313 * that the Cortex-M3 currently does. */
2314 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
2315 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
2316 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2319 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2321 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2322 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
2324 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2328 static int stlink_usb_read_regs(void *handle
)
2331 struct stlink_usb_handle_s
*h
= handle
;
2335 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
2337 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2338 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2340 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
2341 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
2342 /* regs data from offset 0 */
2344 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
2345 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
2346 /* status at offset 0, regs data from offset 4 */
2353 static int stlink_usb_read_reg(void *handle
, unsigned int regsel
, uint32_t *val
)
2356 struct stlink_usb_handle_s
*h
= handle
;
2360 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2361 res
= stlink_usb_write_debug_reg(h
, DCB_DCRSR
, regsel
& 0x7f);
2362 if (res
!= ERROR_OK
)
2365 /* FIXME: poll DHCSR.S_REGRDY before read DCRDR */
2366 return stlink_usb_v2_read_debug_reg(h
, DCB_DCRDR
, val
);
2369 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2371 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2372 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2373 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2375 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2376 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2378 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2379 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2380 if (res
!= ERROR_OK
)
2382 *val
= le_to_h_u32(h
->databuf
);
2385 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2386 if (res
!= ERROR_OK
)
2388 *val
= le_to_h_u32(h
->databuf
+ 4);
2394 static int stlink_usb_write_reg(void *handle
, unsigned int regsel
, uint32_t val
)
2396 struct stlink_usb_handle_s
*h
= handle
;
2400 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2401 int res
= stlink_usb_write_debug_reg(h
, DCB_DCRDR
, val
);
2402 if (res
!= ERROR_OK
)
2405 return stlink_usb_write_debug_reg(h
, DCB_DCRSR
, DCRSR_WNR
| (regsel
& 0x7f));
2406 /* FIXME: poll DHCSR.S_REGRDY after write DCRSR */
2409 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2411 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2412 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2413 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2415 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2416 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2417 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2420 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2423 static int stlink_usb_get_rw_status(void *handle
)
2425 struct stlink_usb_handle_s
*h
= handle
;
2429 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2432 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2434 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2435 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2436 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2437 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2439 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2440 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2445 static int stlink_usb_read_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2446 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2449 uint16_t read_len
= len
;
2450 struct stlink_usb_handle_s
*h
= handle
;
2454 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2455 return ERROR_COMMAND_NOTFOUND
;
2457 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2458 if (len
> stlink_usb_block(h
)) {
2459 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2463 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2465 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2466 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2467 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2469 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2471 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2472 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2475 /* we need to fix read length for single bytes */
2479 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2481 if (res
!= ERROR_OK
)
2484 memcpy(buffer
, h
->databuf
, len
);
2486 return stlink_usb_get_rw_status(handle
);
2490 static int stlink_usb_write_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2491 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2494 struct stlink_usb_handle_s
*h
= handle
;
2498 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2499 return ERROR_COMMAND_NOTFOUND
;
2501 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2502 if (len
> stlink_usb_block(h
)) {
2503 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2507 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2509 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2510 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2511 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2513 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2515 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2516 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2519 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2521 if (res
!= ERROR_OK
)
2524 return stlink_usb_get_rw_status(handle
);
2528 static int stlink_usb_read_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2529 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2532 struct stlink_usb_handle_s
*h
= handle
;
2536 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2537 return ERROR_COMMAND_NOTFOUND
;
2539 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2540 return ERROR_COMMAND_NOTFOUND
;
2542 if (len
> STLINK_MAX_RW16_32
) {
2543 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2547 /* data must be a multiple of 2 and half-word aligned */
2548 if (len
% 2 || addr
% 2) {
2549 LOG_DEBUG("Invalid data alignment");
2550 return ERROR_TARGET_UNALIGNED_ACCESS
;
2553 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2555 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2556 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2557 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2559 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2561 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2562 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2565 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2567 if (res
!= ERROR_OK
)
2570 memcpy(buffer
, h
->databuf
, len
);
2572 return stlink_usb_get_rw_status(handle
);
2576 static int stlink_usb_write_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2577 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2580 struct stlink_usb_handle_s
*h
= handle
;
2584 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2585 return ERROR_COMMAND_NOTFOUND
;
2587 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2588 return ERROR_COMMAND_NOTFOUND
;
2590 if (len
> STLINK_MAX_RW16_32
) {
2591 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2595 /* data must be a multiple of 2 and half-word aligned */
2596 if (len
% 2 || addr
% 2) {
2597 LOG_DEBUG("Invalid data alignment");
2598 return ERROR_TARGET_UNALIGNED_ACCESS
;
2601 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2603 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2604 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2605 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2607 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2609 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2610 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2613 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2615 if (res
!= ERROR_OK
)
2618 return stlink_usb_get_rw_status(handle
);
2622 static int stlink_usb_read_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2623 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2626 struct stlink_usb_handle_s
*h
= handle
;
2630 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2631 return ERROR_COMMAND_NOTFOUND
;
2633 if (len
> STLINK_MAX_RW16_32
) {
2634 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2638 /* data must be a multiple of 4 and word aligned */
2639 if (len
% 4 || addr
% 4) {
2640 LOG_DEBUG("Invalid data alignment");
2641 return ERROR_TARGET_UNALIGNED_ACCESS
;
2644 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2646 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2647 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2648 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2650 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2652 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2653 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2656 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2658 if (res
!= ERROR_OK
)
2661 memcpy(buffer
, h
->databuf
, len
);
2663 return stlink_usb_get_rw_status(handle
);
2667 static int stlink_usb_write_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2668 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2671 struct stlink_usb_handle_s
*h
= handle
;
2675 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2676 return ERROR_COMMAND_NOTFOUND
;
2678 if (len
> STLINK_MAX_RW16_32
) {
2679 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2683 /* data must be a multiple of 4 and word aligned */
2684 if (len
% 4 || addr
% 4) {
2685 LOG_DEBUG("Invalid data alignment");
2686 return ERROR_TARGET_UNALIGNED_ACCESS
;
2689 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2691 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2692 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2693 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2695 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2697 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2698 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2701 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2703 if (res
!= ERROR_OK
)
2706 return stlink_usb_get_rw_status(handle
);
2709 static int stlink_usb_read_mem32_noaddrinc(void *handle
, uint8_t ap_num
, uint32_t csw
,
2710 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2712 struct stlink_usb_handle_s
*h
= handle
;
2714 assert(handle
!= NULL
);
2716 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_RD_NO_INC
))
2717 return ERROR_COMMAND_NOTFOUND
;
2719 if (len
> STLINK_MAX_RW16_32
) {
2720 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2724 /* data must be a multiple of 4 and word aligned */
2725 if (len
% 4 || addr
% 4) {
2726 LOG_DEBUG("Invalid data alignment");
2727 return ERROR_TARGET_UNALIGNED_ACCESS
;
2730 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2732 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2733 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC
;
2734 h_u32_to_le(h
->cmdbuf
+ h
->cmdidx
, addr
);
2736 h_u16_to_le(h
->cmdbuf
+ h
->cmdidx
, len
);
2738 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2739 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2742 int retval
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2743 if (retval
!= ERROR_OK
)
2746 memcpy(buffer
, h
->databuf
, len
);
2748 return stlink_usb_get_rw_status(handle
);
2751 static int stlink_usb_write_mem32_noaddrinc(void *handle
, uint8_t ap_num
, uint32_t csw
,
2752 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2754 struct stlink_usb_handle_s
*h
= handle
;
2756 assert(handle
!= NULL
);
2758 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_WR_NO_INC
))
2759 return ERROR_COMMAND_NOTFOUND
;
2761 if (len
> STLINK_MAX_RW16_32
) {
2762 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2766 /* data must be a multiple of 4 and word aligned */
2767 if (len
% 4 || addr
% 4) {
2768 LOG_DEBUG("Invalid data alignment");
2769 return ERROR_TARGET_UNALIGNED_ACCESS
;
2772 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2774 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2775 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC
;
2776 h_u32_to_le(h
->cmdbuf
+ h
->cmdidx
, addr
);
2778 h_u16_to_le(h
->cmdbuf
+ h
->cmdidx
, len
);
2780 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2781 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2784 int retval
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2785 if (retval
!= ERROR_OK
)
2788 return stlink_usb_get_rw_status(handle
);
2791 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2793 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2794 if (max_tar_block
== 0)
2796 return max_tar_block
;
2799 static int stlink_usb_read_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2800 uint32_t addr
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2802 int retval
= ERROR_OK
;
2803 uint32_t bytes_remaining
;
2805 struct stlink_usb_handle_s
*h
= handle
;
2807 /* calculate byte count */
2810 /* switch to 8 bit if stlink does not support 16 bit memory read */
2811 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2815 bytes_remaining
= (size
!= 1) ?
2816 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2818 if (count
< bytes_remaining
)
2819 bytes_remaining
= count
;
2822 * all stlink support 8/32bit memory read/writes and only from
2823 * stlink V2J26 there is support for 16 bit memory read/write.
2824 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2828 /* When in jtag mode the stlink uses the auto-increment functionality.
2829 * However it expects us to pass the data correctly, this includes
2830 * alignment and any page boundaries. We already do this as part of the
2831 * adi_v5 implementation, but the stlink is a hla adapter and so this
2832 * needs implementing manually.
2833 * currently this only affects jtag mode, according to ST they do single
2834 * access in SWD mode - but this may change and so we do it for both modes */
2836 /* we first need to check for any unaligned bytes */
2837 if (addr
& (size
- 1)) {
2838 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2839 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2840 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2841 usleep((1 << retries
++) * 1000);
2844 if (retval
!= ERROR_OK
)
2846 buffer
+= head_bytes
;
2848 count
-= head_bytes
;
2849 bytes_remaining
-= head_bytes
;
2852 if (bytes_remaining
& (size
- 1))
2853 retval
= stlink_usb_read_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2855 retval
= stlink_usb_read_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2857 retval
= stlink_usb_read_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2859 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2862 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2863 usleep((1 << retries
++) * 1000);
2866 if (retval
!= ERROR_OK
)
2869 buffer
+= bytes_remaining
;
2870 addr
+= bytes_remaining
;
2871 count
-= bytes_remaining
;
2877 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2878 uint32_t count
, uint8_t *buffer
)
2880 return stlink_usb_read_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2881 addr
, size
, count
, buffer
);
2884 static int stlink_usb_write_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2885 uint32_t addr
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2887 int retval
= ERROR_OK
;
2888 uint32_t bytes_remaining
;
2890 struct stlink_usb_handle_s
*h
= handle
;
2892 /* calculate byte count */
2895 /* switch to 8 bit if stlink does not support 16 bit memory read */
2896 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2901 bytes_remaining
= (size
!= 1) ?
2902 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2904 if (count
< bytes_remaining
)
2905 bytes_remaining
= count
;
2908 * all stlink support 8/32bit memory read/writes and only from
2909 * stlink V2J26 there is support for 16 bit memory read/write.
2910 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2915 /* When in jtag mode the stlink uses the auto-increment functionality.
2916 * However it expects us to pass the data correctly, this includes
2917 * alignment and any page boundaries. We already do this as part of the
2918 * adi_v5 implementation, but the stlink is a hla adapter and so this
2919 * needs implementing manually.
2920 * currently this only affects jtag mode, according to ST they do single
2921 * access in SWD mode - but this may change and so we do it for both modes */
2923 /* we first need to check for any unaligned bytes */
2924 if (addr
& (size
- 1)) {
2926 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2927 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2928 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2929 usleep((1<<retries
++) * 1000);
2932 if (retval
!= ERROR_OK
)
2934 buffer
+= head_bytes
;
2936 count
-= head_bytes
;
2937 bytes_remaining
-= head_bytes
;
2940 if (bytes_remaining
& (size
- 1))
2941 retval
= stlink_usb_write_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2943 retval
= stlink_usb_write_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2945 retval
= stlink_usb_write_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2948 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2949 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2950 usleep((1<<retries
++) * 1000);
2953 if (retval
!= ERROR_OK
)
2956 buffer
+= bytes_remaining
;
2957 addr
+= bytes_remaining
;
2958 count
-= bytes_remaining
;
2964 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2965 uint32_t count
, const uint8_t *buffer
)
2967 return stlink_usb_write_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2968 addr
, size
, count
, buffer
);
2972 static int stlink_usb_override_target(const char *targetname
)
2974 return !strcmp(targetname
, "cortex_m");
2977 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2982 we only have low and high speed...
2983 before changing speed the SWIM_CSR HS bit
2987 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2988 if (retval
!= ERROR_OK
)
2989 LOG_ERROR("Unable to set adapter speed");
2992 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2995 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2998 int speed_index
= -1;
2999 int speed_diff
= INT_MAX
;
3000 int last_valid_speed
= -1;
3003 for (i
= 0; i
< map_size
; i
++) {
3006 last_valid_speed
= i
;
3007 if (khz
== map
[i
].speed
) {
3011 int current_diff
= khz
- map
[i
].speed
;
3012 /* get abs value for comparison */
3013 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
3014 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
3015 speed_diff
= current_diff
;
3021 if (speed_index
== -1) {
3022 /* this will only be here if we cannot match the slow speed.
3023 * use the slowest speed we support.*/
3024 speed_index
= last_valid_speed
;
3026 } else if (i
== map_size
)
3029 if (!match
&& query
) {
3030 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
3031 khz
, map
[speed_index
].speed
);
3037 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
3040 struct stlink_usb_handle_s
*h
= handle
;
3042 /* old firmware cannot change it */
3043 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
3046 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
3047 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
3050 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
3051 if (result
!= ERROR_OK
) {
3052 LOG_ERROR("Unable to set adapter speed");
3057 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
3060 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
3063 struct stlink_usb_handle_s
*h
= handle
;
3065 /* old firmware cannot change it */
3066 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
3069 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
3070 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
3073 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
3074 if (result
!= ERROR_OK
) {
3075 LOG_ERROR("Unable to set adapter speed");
3080 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
3083 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
3087 LOG_DEBUG("Supported clock speeds are:");
3088 for (i
= 0; i
< map_size
; i
++)
3090 LOG_DEBUG("%d kHz", map
[i
].speed
);
3093 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
3095 struct stlink_usb_handle_s
*h
= handle
;
3098 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3099 LOG_ERROR("Unknown command");
3103 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3105 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3106 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
3107 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3109 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
3111 int size
= h
->databuf
[8];
3113 if (size
> STLINK_V3_MAX_FREQ_NB
)
3114 size
= STLINK_V3_MAX_FREQ_NB
;
3116 for (i
= 0; i
< size
; i
++) {
3117 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
3118 map
[i
].speed_divisor
= i
;
3121 /* set to zero all the next entries */
3122 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
3128 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
3130 struct stlink_usb_handle_s
*h
= handle
;
3132 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3133 LOG_ERROR("Unknown command");
3137 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3139 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3140 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
3141 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3142 h
->cmdbuf
[h
->cmdidx
++] = 0;
3144 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
3146 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3149 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
3151 struct stlink_usb_handle_s
*h
= handle
;
3153 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
3155 stlink_get_com_freq(h
, is_jtag
, map
);
3157 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
3160 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
3161 if (result
!= ERROR_OK
) {
3162 LOG_ERROR("Unable to set adapter speed");
3166 return map
[speed_index
].speed
;
3169 static int stlink_speed(void *handle
, int khz
, bool query
)
3171 struct stlink_usb_handle_s
*h
= handle
;
3176 switch (h
->st_mode
) {
3177 case STLINK_MODE_DEBUG_SWIM
:
3178 return stlink_speed_swim(handle
, khz
, query
);
3179 case STLINK_MODE_DEBUG_SWD
:
3180 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
3181 return stlink_speed_v3(handle
, false, khz
, query
);
3183 return stlink_speed_swd(handle
, khz
, query
);
3185 case STLINK_MODE_DEBUG_JTAG
:
3186 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
3187 return stlink_speed_v3(handle
, true, khz
, query
);
3189 return stlink_speed_jtag(handle
, khz
, query
);
3199 static int stlink_usb_usb_close(void *handle
)
3201 struct stlink_usb_handle_s
*h
= handle
;
3206 if (h
->usb_backend_priv
.fd
) {
3207 stlink_usb_exit_mode(h
);
3208 /* do not check return code, it prevent
3209 us from closing jtag_libusb */
3210 jtag_libusb_close(h
->usb_backend_priv
.fd
);
3220 static int stlink_tcp_close(void *handle
)
3222 struct stlink_usb_handle_s
*h
= handle
;
3228 if (h
->tcp_backend_priv
.connected
) {
3229 if (h
->tcp_backend_priv
.connect_id
) {
3230 stlink_usb_exit_mode(h
);
3232 /* close the stlink */
3233 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_CLOSE_DEV
;
3234 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 4); /* reserved */
3235 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
3236 ret
= stlink_tcp_send_cmd(h
, 8, 4, true);
3237 if (ret
!= ERROR_OK
)
3238 LOG_ERROR("cannot close the STLINK");
3241 if (close_socket(h
->tcp_backend_priv
.fd
) != 0)
3242 LOG_ERROR("error closing the socket, errno: %s", strerror(errno
));
3245 free(h
->tcp_backend_priv
.send_buf
);
3246 free(h
->tcp_backend_priv
.recv_buf
);
3252 static int stlink_close(void *handle
)
3255 struct stlink_usb_handle_s
*h
= handle
;
3257 stlink_usb_close(handle
);
3265 /* Compute ST-Link serial number from the device descriptor
3266 * this function will help to work-around a bug in old ST-Link/V2 DFU
3267 * the buggy DFU returns an incorrect serial in the USB descriptor
3268 * example for the following serial "57FF72067265575742132067"
3269 * - the correct descriptor serial is:
3270 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
3271 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
3272 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
3273 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
3274 * so this case is managed by libusb_helper::string_descriptor_equal
3275 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
3276 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
3278 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
3279 * and then we have just to convert the raw data into printable characters using sprintf
3281 static char *stlink_usb_get_alternate_serial(struct libusb_device_handle
*device
,
3282 struct libusb_device_descriptor
*dev_desc
)
3285 unsigned char desc_serial
[(STLINK_SERIAL_LEN
+ 1) * 2];
3287 if (dev_desc
->iSerialNumber
== 0)
3290 /* get the LANGID from String Descriptor Zero */
3291 usb_retval
= libusb_get_string_descriptor(device
, 0, 0, desc_serial
,
3292 sizeof(desc_serial
));
3294 if (usb_retval
< LIBUSB_SUCCESS
) {
3295 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
3296 libusb_error_name(usb_retval
), usb_retval
);
3298 } else if (usb_retval
< 4) {
3299 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
3300 LOG_ERROR("could not get the LANGID");
3304 uint32_t langid
= desc_serial
[2] | (desc_serial
[3] << 8);
3306 /* get the serial */
3307 usb_retval
= libusb_get_string_descriptor(device
, dev_desc
->iSerialNumber
,
3308 langid
, desc_serial
, sizeof(desc_serial
));
3310 unsigned char len
= desc_serial
[0];
3312 if (usb_retval
< LIBUSB_SUCCESS
) {
3313 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
3314 libusb_error_name(usb_retval
), usb_retval
);
3316 } else if (desc_serial
[1] != LIBUSB_DT_STRING
|| len
> usb_retval
) {
3317 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
3321 if (len
== ((STLINK_SERIAL_LEN
+ 1) * 2)) {
3322 /* good ST-Link adapter, this case is managed by
3323 * libusb::libusb_get_string_descriptor_ascii */
3325 } else if (len
!= ((STLINK_SERIAL_LEN
/ 2 + 1) * 2)) {
3326 LOG_ERROR("unexpected serial length (%d) in descriptor", len
);
3330 /* else (len == 26) => buggy ST-Link */
3332 char *alternate_serial
= malloc((STLINK_SERIAL_LEN
+ 1) * sizeof(char));
3333 if (!alternate_serial
)
3336 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
3337 sprintf(alternate_serial
+ i
, "%02X", desc_serial
[i
+ 2]);
3339 alternate_serial
[STLINK_SERIAL_LEN
] = '\0';
3341 return alternate_serial
;
3345 static int stlink_usb_usb_open(void *handle
, struct hl_interface_param_s
*param
)
3347 struct stlink_usb_handle_s
*h
= handle
;
3348 int err
, retry_count
= 1;
3350 h
->cmdbuf
= malloc(STLINK_SG_SIZE
);
3351 h
->databuf
= malloc(STLINK_DATA_SIZE
);
3353 if (!h
->cmdbuf
|| !h
->databuf
)
3357 On certain host USB configurations(e.g. MacBook Air)
3358 STLINKv2 dongle seems to have its FW in a funky state if,
3359 after plugging it in, you try to use openocd with it more
3360 then once (by launching and closing openocd). In cases like
3361 that initial attempt to read the FW info via
3362 stlink_usb_version will fail and the device has to be reset
3363 in order to become operational.
3366 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
,
3367 &h
->usb_backend_priv
.fd
, stlink_usb_get_alternate_serial
) != ERROR_OK
) {
3368 LOG_ERROR("open failed");
3372 jtag_libusb_set_configuration(h
->usb_backend_priv
.fd
, 0);
3374 if (libusb_claim_interface(h
->usb_backend_priv
.fd
, 0) != ERROR_OK
) {
3375 LOG_DEBUG("claim interface failed");
3379 /* RX EP is common for all versions */
3380 h
->rx_ep
= STLINK_RX_EP
;
3383 if (jtag_libusb_get_pid(libusb_get_device(h
->usb_backend_priv
.fd
), &pid
) != ERROR_OK
) {
3384 LOG_DEBUG("libusb_get_pid failed");
3388 /* wrap version for first read */
3391 h
->version
.stlink
= 1;
3392 h
->tx_ep
= STLINK_TX_EP
;
3394 case STLINK_V3_USBLOADER_PID
:
3395 case STLINK_V3E_PID
:
3396 case STLINK_V3S_PID
:
3397 case STLINK_V3_2VCP_PID
:
3398 case STLINK_V3E_NO_MSD_PID
:
3399 h
->version
.stlink
= 3;
3400 h
->tx_ep
= STLINK_V2_1_TX_EP
;
3401 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
3403 case STLINK_V2_1_PID
:
3404 case STLINK_V2_1_NO_MSD_PID
:
3405 h
->version
.stlink
= 2;
3406 h
->tx_ep
= STLINK_V2_1_TX_EP
;
3407 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
3410 /* fall through - we assume V2 to be the default version*/
3412 h
->version
.stlink
= 2;
3413 h
->tx_ep
= STLINK_TX_EP
;
3414 h
->trace_ep
= STLINK_TRACE_EP
;
3418 /* get the device version */
3419 err
= stlink_usb_version(h
);
3421 if (err
== ERROR_OK
) {
3423 } else if (h
->version
.stlink
== 1 ||
3425 LOG_ERROR("read version failed");
3428 err
= libusb_release_interface(h
->usb_backend_priv
.fd
, 0);
3429 if (err
!= ERROR_OK
) {
3430 LOG_ERROR("release interface failed");
3434 err
= libusb_reset_device(h
->usb_backend_priv
.fd
);
3435 if (err
!= ERROR_OK
) {
3436 LOG_ERROR("reset device failed");
3440 jtag_libusb_close(h
->usb_backend_priv
.fd
);
3442 Give the device one second to settle down and
3445 usleep(1 * 1000 * 1000);
3454 static int stlink_tcp_open(void *handle
, struct hl_interface_param_s
*param
)
3456 struct stlink_usb_handle_s
*h
= handle
;
3459 /* SWIM is not supported using stlink-server */
3460 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
3461 LOG_ERROR("stlink-server does not support SWIM mode");
3465 h
->tcp_backend_priv
.send_buf
= malloc(STLINK_TCP_SEND_BUFFER_SIZE
);
3466 h
->tcp_backend_priv
.recv_buf
= malloc(STLINK_TCP_RECV_BUFFER_SIZE
);
3468 if (!h
->tcp_backend_priv
.send_buf
|| !h
->tcp_backend_priv
.recv_buf
)
3471 h
->cmdbuf
= &h
->tcp_backend_priv
.send_buf
[8];
3472 h
->databuf
= &h
->tcp_backend_priv
.recv_buf
[4];
3474 /* configure directions */
3475 h
->rx_ep
= STLINK_TCP_REQUEST_READ
;
3476 h
->tx_ep
= STLINK_TCP_REQUEST_WRITE
;
3477 h
->trace_ep
= STLINK_TCP_REQUEST_READ_SWO
;
3479 h
->tcp_backend_priv
.fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
3480 h
->tcp_backend_priv
.connected
= false;
3481 h
->tcp_backend_priv
.device_id
= 0;
3482 h
->tcp_backend_priv
.connect_id
= 0;
3484 if (h
->tcp_backend_priv
.fd
== -1) {
3485 LOG_ERROR("error creating the socket, errno: %s", strerror(errno
));
3489 struct sockaddr_in serv
;
3490 memset(&serv
, 0, sizeof(struct sockaddr_in
));
3491 serv
.sin_family
= AF_INET
;
3492 serv
.sin_port
= htons(param
->stlink_tcp_port
);
3493 serv
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3495 LOG_DEBUG("socket : %x", h
->tcp_backend_priv
.fd
);
3498 if (setsockopt(h
->tcp_backend_priv
.fd
, IPPROTO_TCP
, TCP_NODELAY
, (const void *)&optval
, sizeof(int)) == -1) {
3499 LOG_ERROR("cannot set sock option 'TCP_NODELAY', errno: %s", strerror(errno
));
3503 optval
= STLINK_TCP_RECV_BUFFER_SIZE
;
3504 if (setsockopt(h
->tcp_backend_priv
.fd
, SOL_SOCKET
, SO_RCVBUF
, (const void *)&optval
, sizeof(int)) == -1) {
3505 LOG_ERROR("cannot set sock option 'SO_RCVBUF', errno: %s", strerror(errno
));
3509 optval
= STLINK_TCP_SEND_BUFFER_SIZE
;
3510 if (setsockopt(h
->tcp_backend_priv
.fd
, SOL_SOCKET
, SO_SNDBUF
, (const void *)&optval
, sizeof(int)) == -1) {
3511 LOG_ERROR("cannot set sock option 'SO_SNDBUF', errno: %s", strerror(errno
));
3515 if (connect(h
->tcp_backend_priv
.fd
, (const struct sockaddr
*)&serv
, sizeof(serv
)) == -1) {
3516 LOG_ERROR("cannot connect to stlink server, errno: %s", strerror(errno
));
3520 h
->tcp_backend_priv
.connected
= true;
3522 LOG_INFO("connected to stlink-server");
3524 /* print stlink-server version */
3525 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_GET_SERVER_VERSION
;
3526 h
->tcp_backend_priv
.send_buf
[1] = OPENOCD_STLINK_TCP_API_VERSION
;
3527 memset(&h
->tcp_backend_priv
.send_buf
[2], 0, 2); /* reserved */
3528 ret
= stlink_tcp_send_cmd(h
, 4, 16, false);
3529 if (ret
!= ERROR_OK
) {
3530 LOG_ERROR("cannot get the stlink-server version");
3534 uint32_t api_ver
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[0]);
3535 uint32_t ver_major
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[4]);
3536 uint32_t ver_minor
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[8]);
3537 uint32_t ver_build
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[12]);
3538 LOG_INFO("stlink-server API v%d, version %d.%d.%d",
3539 api_ver
, ver_major
, ver_minor
, ver_build
);
3541 /* in stlink-server API v1 sending more than 1428 bytes will cause stlink-server
3542 * to crash in windows: select a safe default value (1K) */
3544 h
->max_mem_packet
= (1 << 10);
3546 /* refresh stlink list (re-enumerate) */
3547 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_REFRESH_DEVICE_LIST
;
3548 h
->tcp_backend_priv
.send_buf
[1] = 0; /* don't clear the list, just refresh it */
3549 ret
= stlink_tcp_send_cmd(h
, 2, 4, true);
3550 if (ret
!= ERROR_OK
)
3553 /* get the number of connected stlinks */
3554 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_GET_NB_DEV
;
3555 ret
= stlink_tcp_send_cmd(h
, 1, 4, false);
3556 if (ret
!= ERROR_OK
)
3559 uint32_t connected_stlinks
= le_to_h_u32(h
->tcp_backend_priv
.recv_buf
);
3561 if (connected_stlinks
== 0) {
3562 LOG_ERROR("no ST-LINK detected");
3566 LOG_DEBUG("%d ST-LINK detected", connected_stlinks
);
3568 if (connected_stlinks
> 255) {
3569 LOG_WARNING("STLink server cannot handle more than 255 ST-LINK connected");
3570 connected_stlinks
= 255;
3573 /* list all connected ST-Link and seek for the requested vid:pid and serial */
3574 char serial
[STLINK_TCP_SERIAL_SIZE
+ 1] = {0};
3575 uint8_t stlink_used
;
3576 bool stlink_id_matched
= false;
3577 bool stlink_serial_matched
= (!param
->serial
);
3579 for (uint32_t stlink_id
= 0; stlink_id
< connected_stlinks
; stlink_id
++) {
3580 /* get the stlink info */
3581 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_GET_DEV_INFO
;
3582 h
->tcp_backend_priv
.send_buf
[1] = (uint8_t)stlink_id
;
3583 memset(&h
->tcp_backend_priv
.send_buf
[2], 0, 2); /* reserved */
3584 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], 41); /* size of TDeviceInfo2 */
3585 ret
= stlink_tcp_send_cmd(h
, 8, 45, true);
3586 if (ret
!= ERROR_OK
)
3589 h
->tcp_backend_priv
.device_id
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[4]);
3590 memcpy(serial
, &h
->tcp_backend_priv
.recv_buf
[8], STLINK_TCP_SERIAL_SIZE
);
3591 h
->vid
= le_to_h_u16(&h
->tcp_backend_priv
.recv_buf
[40]);
3592 h
->pid
= le_to_h_u16(&h
->tcp_backend_priv
.recv_buf
[42]);
3593 stlink_used
= h
->tcp_backend_priv
.recv_buf
[44];
3595 /* check the vid:pid */
3596 for (int i
= 0; param
->vid
[i
]; i
++) {
3597 if (param
->vid
[i
] == h
->vid
&& param
->pid
[i
] == h
->pid
) {
3598 stlink_id_matched
= true;
3603 if (!stlink_id_matched
)
3606 /* check the serial if specified */
3607 if (param
->serial
) {
3608 /* ST-Link server fixes the buggy serial returned by old ST-Link DFU
3609 * for further details refer to stlink_usb_get_alternate_serial
3610 * so if the user passes the buggy serial, we need to fix it before
3611 * comparing with the serial returned by ST-Link server */
3612 if (strlen(param
->serial
) == STLINK_SERIAL_LEN
/ 2) {
3613 char fixed_serial
[STLINK_SERIAL_LEN
+ 1];
3615 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
3616 sprintf(fixed_serial
+ i
, "%02X", param
->serial
[i
/ 2]);
3618 fixed_serial
[STLINK_SERIAL_LEN
] = '\0';
3620 stlink_serial_matched
= strcmp(fixed_serial
, serial
) == 0;
3622 stlink_serial_matched
= strcmp(param
->serial
, serial
) == 0;
3625 if (!stlink_serial_matched
)
3626 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
3627 serial
, param
->serial
);
3628 else /* exit the search loop if there is match */
3632 if (!stlink_id_matched
) {
3633 LOG_ERROR("ST-LINK open failed (vid/pid mismatch)");
3637 if (!stlink_serial_matched
) {
3638 LOG_ERROR("ST-LINK open failed (serial mismatch)");
3642 /* check if device is 'exclusively' used by another application */
3644 LOG_ERROR("the selected device is already used");
3648 LOG_DEBUG("transport: vid: 0x%04x pid: 0x%04x serial: %s", h
->vid
, h
->pid
, serial
);
3650 /* now let's open the stlink */
3651 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_OPEN_DEV
;
3652 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 4); /* reserved */
3653 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.device_id
);
3654 ret
= stlink_tcp_send_cmd(h
, 8, 8, true);
3655 if (ret
!= ERROR_OK
)
3658 h
->tcp_backend_priv
.connect_id
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[4]);
3660 /* get stlink version */
3661 return stlink_usb_version(h
);
3664 static struct stlink_backend_s stlink_usb_backend
= {
3665 .open
= stlink_usb_usb_open
,
3666 .close
= stlink_usb_usb_close
,
3667 .xfer_noerrcheck
= stlink_usb_usb_xfer_noerrcheck
,
3668 .read_trace
= stlink_usb_usb_read_trace
,
3671 static struct stlink_backend_s stlink_tcp_backend
= {
3672 .open
= stlink_tcp_open
,
3673 .close
= stlink_tcp_close
,
3674 .xfer_noerrcheck
= stlink_tcp_xfer_noerrcheck
,
3675 .read_trace
= stlink_tcp_read_trace
,
3678 static int stlink_open(struct hl_interface_param_s
*param
, enum stlink_mode mode
, void **fd
)
3680 struct stlink_usb_handle_s
*h
;
3682 LOG_DEBUG("stlink_open");
3684 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
3687 LOG_DEBUG("malloc failed");
3693 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
3694 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
3695 h
->st_mode
, param
->vid
[i
], param
->pid
[i
],
3696 param
->serial
? param
->serial
: "");
3699 if (param
->use_stlink_tcp
)
3700 h
->backend
= &stlink_tcp_backend
;
3702 h
->backend
= &stlink_usb_backend
;
3704 if (stlink_usb_open(h
, param
) != ERROR_OK
)
3707 /* check if mode is supported */
3710 switch (h
->st_mode
) {
3711 case STLINK_MODE_DEBUG_SWD
:
3712 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
3715 case STLINK_MODE_DEBUG_JTAG
:
3716 if (h
->version
.jtag
== 0)
3719 case STLINK_MODE_DEBUG_SWIM
:
3720 if (h
->version
.swim
== 0)
3728 if (err
!= ERROR_OK
) {
3729 LOG_ERROR("mode (transport) not supported by device");
3733 /* initialize the debug hardware */
3734 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
3736 if (err
!= ERROR_OK
) {
3737 LOG_ERROR("init mode failed (unable to connect to the target)");
3741 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
3742 err
= stlink_swim_enter(h
);
3743 if (err
!= ERROR_OK
) {
3744 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
3748 h
->max_mem_packet
= STLINK_SWIM_DATA_SIZE
;
3752 /* set max_mem_packet if it was not set by the low-level interface */
3753 if (h
->max_mem_packet
== 0) {
3754 /* get cpuid, so we can determine the max page size
3755 * start with a safe default */
3756 h
->max_mem_packet
= (1 << 10);
3759 stlink_usb_open_ap(h
, STLINK_HLA_AP_NUM
);
3760 err
= stlink_usb_read_mem32(h
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
, CPUID
, 4, buffer
);
3761 if (err
== ERROR_OK
) {
3762 uint32_t cpuid
= le_to_h_u32(buffer
);
3763 int i
= (cpuid
>> 4) & 0xf;
3764 if (i
== 4 || i
== 3) {
3765 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
3766 h
->max_mem_packet
= (1 << 12);
3770 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
3782 static int stlink_usb_hl_open(struct hl_interface_param_s
*param
, void **fd
)
3784 return stlink_open(param
, stlink_get_mode(param
->transport
), fd
);
3787 static int stlink_config_trace(void *handle
, bool enabled
,
3788 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3789 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3790 uint16_t *prescaler
)
3792 struct stlink_usb_handle_s
*h
= handle
;
3794 if (!(h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
3795 LOG_ERROR("The attached ST-LINK version doesn't support trace");
3800 stlink_usb_trace_disable(h
);
3807 if (pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
) {
3808 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3812 unsigned int max_trace_freq
= (h
->version
.stlink
== 3) ?
3813 STLINK_V3_TRACE_MAX_HZ
: STLINK_TRACE_MAX_HZ
;
3815 /* Only concern ourselves with the frequency if the STlink is processing it. */
3816 if (*trace_freq
> max_trace_freq
) {
3817 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3823 *trace_freq
= max_trace_freq
;
3825 unsigned int presc
= (traceclkin_freq
+ *trace_freq
/ 2) / *trace_freq
;
3826 if (presc
== 0 || presc
> TPIU_ACPR_MAX_SWOSCALER
+ 1) {
3827 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3832 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
3833 unsigned int max_deviation
= (traceclkin_freq
* 3) / 100;
3834 if (presc
* *trace_freq
< traceclkin_freq
- max_deviation
||
3835 presc
* *trace_freq
> traceclkin_freq
+ max_deviation
) {
3836 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3843 stlink_usb_trace_disable(h
);
3845 h
->trace
.source_hz
= *trace_freq
;
3847 return stlink_usb_trace_enable(h
);
3851 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
3853 struct stlink_usb_handle_s
*h
= handle
;
3857 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3858 return ERROR_COMMAND_NOTFOUND
;
3860 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
3861 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3862 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3863 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
3864 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3866 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3870 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
3872 struct stlink_usb_handle_s
*h
= handle
;
3876 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3877 return ERROR_COMMAND_NOTFOUND
;
3879 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
3880 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3881 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3882 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG
;
3883 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3885 /* ignore incorrectly returned error on bogus FW */
3886 if (h
->version
.flags
& STLINK_F_FIX_CLOSE_AP
)
3887 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3889 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
3893 static int stlink_usb_rw_misc_out(void *handle
, uint32_t items
, const uint8_t *buffer
)
3895 struct stlink_usb_handle_s
*h
= handle
;
3896 unsigned int buflen
= ALIGN_UP(items
, 4) + 4 * items
;
3898 LOG_DEBUG_IO("%s(%" PRIu32
")", __func__
, items
);
3900 assert(handle
!= NULL
);
3902 if (!(h
->version
.flags
& STLINK_F_HAS_RW_MISC
))
3903 return ERROR_COMMAND_NOTFOUND
;
3905 stlink_usb_init_buffer(handle
, h
->tx_ep
, buflen
);
3907 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3908 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RW_MISC_OUT
;
3909 h_u32_to_le(&h
->cmdbuf
[2], items
);
3911 return stlink_usb_xfer_noerrcheck(handle
, buffer
, buflen
);
3914 static int stlink_usb_rw_misc_in(void *handle
, uint32_t items
, uint8_t *buffer
)
3916 struct stlink_usb_handle_s
*h
= handle
;
3917 unsigned int buflen
= 2 * 4 * items
;
3919 LOG_DEBUG_IO("%s(%" PRIu32
")", __func__
, items
);
3921 assert(handle
!= NULL
);
3923 if (!(h
->version
.flags
& STLINK_F_HAS_RW_MISC
))
3924 return ERROR_COMMAND_NOTFOUND
;
3926 stlink_usb_init_buffer(handle
, h
->rx_ep
, buflen
);
3928 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3929 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RW_MISC_IN
;
3931 int res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, buflen
);
3932 if (res
!= ERROR_OK
)
3935 memcpy(buffer
, h
->databuf
, buflen
);
3941 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3942 unsigned short addr
, uint32_t *val
)
3944 struct stlink_usb_handle_s
*h
= handle
;
3949 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3950 return ERROR_COMMAND_NOTFOUND
;
3952 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3953 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3954 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3955 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3956 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3958 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3959 *val
= le_to_h_u32(h
->databuf
+ 4);
3960 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%" PRIx32
, dap_port
, addr
, *val
);
3965 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3966 unsigned short addr
, uint32_t val
)
3968 struct stlink_usb_handle_s
*h
= handle
;
3972 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3973 return ERROR_COMMAND_NOTFOUND
;
3975 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%" PRIx32
, dap_port
, addr
, val
);
3976 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3977 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3978 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3979 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3980 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3981 h_u32_to_le(&h
->cmdbuf
[6], val
);
3982 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3986 struct hl_layout_api_s stlink_usb_layout_api
= {
3988 .open
= stlink_usb_hl_open
,
3990 .close
= stlink_close
,
3992 .idcode
= stlink_usb_idcode
,
3994 .state
= stlink_usb_state
,
3996 .reset
= stlink_usb_reset
,
3998 .assert_srst
= stlink_usb_assert_srst
,
4000 .run
= stlink_usb_run
,
4002 .halt
= stlink_usb_halt
,
4004 .step
= stlink_usb_step
,
4006 .read_regs
= stlink_usb_read_regs
,
4008 .read_reg
= stlink_usb_read_reg
,
4010 .write_reg
= stlink_usb_write_reg
,
4012 .read_mem
= stlink_usb_read_mem
,
4014 .write_mem
= stlink_usb_write_mem
,
4016 .write_debug_reg
= stlink_usb_write_debug_reg
,
4018 .override_target
= stlink_usb_override_target
,
4020 .speed
= stlink_speed
,
4022 .config_trace
= stlink_config_trace
,
4024 .poll_trace
= stlink_usb_trace_read
,
4027 /*****************************************************************************
4028 * DAP direct interface
4031 static struct stlink_usb_handle_s
*stlink_dap_handle
;
4032 static struct hl_interface_param_s stlink_dap_param
;
4033 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
4034 static uint32_t last_csw_default
[DP_APSEL_MAX
+ 1];
4035 static int stlink_dap_error
= ERROR_OK
;
4038 static int stlink_dap_record_error(int error
)
4040 if (stlink_dap_error
== ERROR_OK
)
4041 stlink_dap_error
= error
;
4046 static int stlink_dap_get_and_clear_error(void)
4048 int retval
= stlink_dap_error
;
4049 stlink_dap_error
= ERROR_OK
;
4053 static int stlink_dap_get_error(void)
4055 return stlink_dap_error
;
4058 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
)
4060 struct stlink_usb_handle_s
*h
= handle
;
4063 /* nothing to do on old versions */
4064 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
4067 if (apsel
> DP_APSEL_MAX
)
4070 if (test_bit(apsel
, opened_ap
))
4073 retval
= stlink_usb_init_access_port(h
, apsel
);
4074 if (retval
!= ERROR_OK
)
4077 LOG_DEBUG("AP %d enabled", apsel
);
4078 set_bit(apsel
, opened_ap
);
4079 last_csw_default
[apsel
] = 0;
4083 static int stlink_dap_open_ap(unsigned short apsel
)
4085 return stlink_usb_open_ap(stlink_dap_handle
, apsel
);
4089 static int stlink_dap_closeall_ap(void)
4093 /* nothing to do on old versions */
4094 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
4097 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
4098 if (!test_bit(apsel
, opened_ap
))
4100 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
4101 if (retval
!= ERROR_OK
)
4103 clear_bit(apsel
, opened_ap
);
4109 static int stlink_dap_reinit_interface(void)
4114 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
4115 * some firmware version it does not work as expected, and there is no
4116 * equivalent for SWD.
4117 * At least for now, to reset the interface quit from JTAG/SWD mode then
4118 * select the mode again.
4121 if (!stlink_dap_handle
->reconnect_pending
) {
4122 stlink_dap_handle
->reconnect_pending
= true;
4123 stlink_usb_mode_leave(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
4126 retval
= stlink_usb_mode_enter(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
4127 if (retval
!= ERROR_OK
)
4130 stlink_dap_handle
->reconnect_pending
= false;
4131 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
4132 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
4133 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
4134 if (test_bit(apsel
, opened_ap
)) {
4135 clear_bit(apsel
, opened_ap
);
4136 stlink_dap_open_ap(apsel
);
4142 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
4147 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
4149 /* Check if we should reset srst already when connecting, but not if reconnecting. */
4150 if (!dap
->do_reconnect
) {
4151 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4153 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
4154 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
4155 adapter_assert_reset();
4157 LOG_WARNING("\'srst_nogate\' reset_config option is required");
4161 dap
->do_reconnect
= false;
4162 dap_invalidate_cache(dap
);
4163 for (unsigned int i
= 0; i
<= DP_APSEL_MAX
; i
++)
4164 last_csw_default
[i
] = 0;
4166 retval
= dap_dp_init(dap
);
4167 if (retval
!= ERROR_OK
) {
4168 dap
->do_reconnect
= true;
4172 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
4173 if (retval
== ERROR_OK
)
4174 LOG_INFO("%s %#8.8" PRIx32
,
4175 (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
4178 dap
->do_reconnect
= true;
4184 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
4188 if (!dap
->do_reconnect
)
4191 retval
= stlink_dap_reinit_interface();
4192 if (retval
!= ERROR_OK
)
4195 return stlink_dap_op_connect(dap
);
4199 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
4201 /* Ignore the request */
4206 static int stlink_dap_dp_read(struct adiv5_dap
*dap
, unsigned int reg
, uint32_t *data
)
4211 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
4212 if (reg
& 0x000000F0) {
4213 LOG_ERROR("Banked DP registers not supported in current STLink FW");
4214 return ERROR_COMMAND_NOTFOUND
;
4217 data
= data
? data
: &dummy
;
4218 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
4219 && stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) {
4220 /* Quirk required in JTAG. Read RDBUFF to get the data */
4221 retval
= stlink_read_dap_register(stlink_dap_handle
,
4222 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
4223 if (retval
== ERROR_OK
)
4224 retval
= stlink_read_dap_register(stlink_dap_handle
,
4225 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
4227 retval
= stlink_read_dap_register(stlink_dap_handle
,
4228 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
4235 static int stlink_dap_dp_write(struct adiv5_dap
*dap
, unsigned int reg
, uint32_t data
)
4239 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
4240 if (reg
& 0x000000F0) {
4241 LOG_ERROR("Banked DP registers not supported in current STLink FW");
4242 return ERROR_COMMAND_NOTFOUND
;
4245 if (reg
== DP_SELECT
&& (data
& DP_SELECT_DPBANK
) != 0) {
4246 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
4247 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
4248 data
&= ~DP_SELECT_DPBANK
;
4251 /* ST-Link does not like that we set CORUNDETECT */
4252 if (reg
== DP_CTRL_STAT
)
4253 data
&= ~CORUNDETECT
;
4255 retval
= stlink_write_dap_register(stlink_dap_handle
,
4256 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
4261 static int stlink_dap_ap_read(struct adiv5_ap
*ap
, unsigned int reg
, uint32_t *data
)
4263 struct adiv5_dap
*dap
= ap
->dap
;
4267 if (reg
!= AP_REG_IDR
) {
4268 retval
= stlink_dap_open_ap(ap
->ap_num
);
4269 if (retval
!= ERROR_OK
)
4272 data
= data
? data
: &dummy
;
4273 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
4275 dap
->stlink_flush_ap_write
= false;
4280 static int stlink_dap_ap_write(struct adiv5_ap
*ap
, unsigned int reg
, uint32_t data
)
4282 struct adiv5_dap
*dap
= ap
->dap
;
4285 retval
= stlink_dap_open_ap(ap
->ap_num
);
4286 if (retval
!= ERROR_OK
)
4289 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
4291 dap
->stlink_flush_ap_write
= true;
4296 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
4298 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
4302 #define RW_MISC_CMD_ADDRESS 1
4303 #define RW_MISC_CMD_WRITE 2
4304 #define RW_MISC_CMD_READ 3
4305 #define RW_MISC_CMD_APNUM 5
4307 static int stlink_usb_misc_rw_segment(void *handle
, const struct dap_queue
*q
, unsigned int len
, unsigned int items
)
4309 uint8_t buf
[2 * 4 * items
];
4311 LOG_DEBUG("Queue: %u commands in %u items", len
, items
);
4313 int ap_num
= DP_APSEL_INVALID
;
4314 unsigned int cmd_index
= 0;
4315 unsigned int val_index
= ALIGN_UP(items
, 4);
4316 for (unsigned int i
= 0; i
< len
; i
++) {
4317 if (ap_num
!= q
[i
].mem_ap
.ap
->ap_num
) {
4318 ap_num
= q
[i
].mem_ap
.ap
->ap_num
;
4319 buf
[cmd_index
++] = RW_MISC_CMD_APNUM
;
4320 h_u32_to_le(&buf
[val_index
], ap_num
);
4325 case CMD_MEM_AP_READ32
:
4326 buf
[cmd_index
++] = RW_MISC_CMD_READ
;
4327 h_u32_to_le(&buf
[val_index
], q
[i
].mem_ap
.addr
);
4330 case CMD_MEM_AP_WRITE32
:
4331 buf
[cmd_index
++] = RW_MISC_CMD_ADDRESS
;
4332 h_u32_to_le(&buf
[val_index
], q
[i
].mem_ap
.addr
);
4334 buf
[cmd_index
++] = RW_MISC_CMD_WRITE
;
4335 h_u32_to_le(&buf
[val_index
], q
[i
].mem_ap
.data
);
4339 /* Not supposed to happen */
4343 /* pad after last command */
4344 while (!IS_ALIGNED(cmd_index
, 4))
4345 buf
[cmd_index
++] = 0;
4347 int retval
= stlink_usb_rw_misc_out(handle
, items
, buf
);
4348 if (retval
!= ERROR_OK
)
4351 retval
= stlink_usb_rw_misc_in(handle
, items
, buf
);
4352 if (retval
!= ERROR_OK
)
4355 ap_num
= DP_APSEL_INVALID
;
4357 unsigned int err_index
= 4 * items
;
4358 for (unsigned int i
= 0; i
< len
; i
++) {
4359 uint32_t errcode
= le_to_h_u32(&buf
[err_index
]);
4360 if (errcode
!= STLINK_DEBUG_ERR_OK
) {
4361 LOG_ERROR("unknown/unexpected STLINK status code 0x%x", errcode
);
4364 if (ap_num
!= q
[i
].mem_ap
.ap
->ap_num
) {
4365 ap_num
= q
[i
].mem_ap
.ap
->ap_num
;
4368 errcode
= le_to_h_u32(&buf
[err_index
]);
4369 if (errcode
!= STLINK_DEBUG_ERR_OK
) {
4370 LOG_ERROR("unknown/unexpected STLINK status code 0x%x", errcode
);
4375 if (q
[i
].cmd
== CMD_MEM_AP_READ32
) {
4376 *q
[i
].mem_ap
.p_data
= le_to_h_u32(&buf
[val_index
]);
4377 } else { /* q[i]->cmd == CMD_MEM_AP_WRITE32 */
4380 errcode
= le_to_h_u32(&buf
[err_index
]);
4381 if (errcode
!= STLINK_DEBUG_ERR_OK
) {
4382 LOG_ERROR("unknown/unexpected STLINK status code 0x%x", errcode
);
4393 static int stlink_usb_buf_rw_segment(void *handle
, const struct dap_queue
*q
, unsigned int count
)
4395 uint32_t bufsize
= count
* CMD_MEM_AP_2_SIZE(q
[0].cmd
);
4396 uint8_t buf
[bufsize
];
4397 uint8_t ap_num
= q
[0].mem_ap
.ap
->ap_num
;
4398 uint32_t addr
= q
[0].mem_ap
.addr
;
4399 uint32_t csw
= q
[0].mem_ap
.csw
;
4401 int retval
= stlink_dap_open_ap(ap_num
);
4402 if (retval
!= ERROR_OK
)
4406 case CMD_MEM_AP_WRITE8
:
4407 for (unsigned int i
= 0; i
< count
; i
++)
4408 buf
[i
] = q
[i
].mem_ap
.data
>> 8 * (q
[i
].mem_ap
.addr
& 3);
4409 return stlink_usb_write_mem8(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4411 case CMD_MEM_AP_WRITE16
:
4412 for (unsigned int i
= 0; i
< count
; i
++)
4413 h_u16_to_le(&buf
[2 * i
], q
[i
].mem_ap
.data
>> 8 * (q
[i
].mem_ap
.addr
& 2));
4414 return stlink_usb_write_mem16(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4416 case CMD_MEM_AP_WRITE32
:
4417 for (unsigned int i
= 0; i
< count
; i
++)
4418 h_u32_to_le(&buf
[4 * i
], q
[i
].mem_ap
.data
);
4419 if (count
> 1 && q
[0].mem_ap
.addr
== q
[1].mem_ap
.addr
)
4420 return stlink_usb_write_mem32_noaddrinc(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4422 return stlink_usb_write_mem32(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4424 case CMD_MEM_AP_READ8
:
4425 retval
= stlink_usb_read_mem8(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4426 if (retval
== ERROR_OK
)
4427 for (unsigned int i
= 0; i
< count
; i
++)
4428 *q
[i
].mem_ap
.p_data
= buf
[i
] << 8 * (q
[i
].mem_ap
.addr
& 3);
4431 case CMD_MEM_AP_READ16
:
4432 retval
= stlink_usb_read_mem16(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4433 if (retval
== ERROR_OK
)
4434 for (unsigned int i
= 0; i
< count
; i
++)
4435 *q
[i
].mem_ap
.p_data
= le_to_h_u16(&buf
[2 * i
]) << 8 * (q
[i
].mem_ap
.addr
& 2);
4438 case CMD_MEM_AP_READ32
:
4439 if (count
> 1 && q
[0].mem_ap
.addr
== q
[1].mem_ap
.addr
)
4440 retval
= stlink_usb_read_mem32_noaddrinc(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4442 retval
= stlink_usb_read_mem32(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4443 if (retval
== ERROR_OK
)
4444 for (unsigned int i
= 0; i
< count
; i
++)
4445 *q
[i
].mem_ap
.p_data
= le_to_h_u32(&buf
[4 * i
]);
4453 /* TODO: recover these values with cmd STLINK_DEBUG_APIV2_RW_MISC_GET_MAX (0x53) */
4454 #define STLINK_V2_RW_MISC_SIZE (64)
4455 #define STLINK_V3_RW_MISC_SIZE (1227)
4457 static int stlink_usb_count_misc_rw_queue(void *handle
, const struct dap_queue
*q
, unsigned int len
,
4458 unsigned int *pkt_items
)
4460 struct stlink_usb_handle_s
*h
= handle
;
4461 unsigned int i
, items
= 0;
4462 int ap_num
= DP_APSEL_INVALID
;
4463 unsigned int misc_max_items
= (h
->version
.stlink
== 2) ? STLINK_V2_RW_MISC_SIZE
: STLINK_V3_RW_MISC_SIZE
;
4465 if (!(h
->version
.flags
& STLINK_F_HAS_RW_MISC
))
4468 * RW_MISC sequence doesn't lock the st-link, so are not safe in shared mode.
4469 * Don't use it with TCP backend to prevent any issue in case of sharing.
4470 * This further degrades the performance, on top of TCP server overhead.
4472 if (h
->backend
== &stlink_tcp_backend
)
4475 for (i
= 0; i
< len
; i
++) {
4476 if (q
[i
].cmd
!= CMD_MEM_AP_READ32
&& q
[i
].cmd
!= CMD_MEM_AP_WRITE32
)
4478 unsigned int count
= 1;
4479 if (ap_num
!= q
[i
].mem_ap
.ap
->ap_num
) {
4481 ap_num
= q
[i
].mem_ap
.ap
->ap_num
;
4483 if (q
[i
].cmd
== CMD_MEM_AP_WRITE32
)
4485 if (items
+ count
> misc_max_items
)
4495 static int stlink_usb_count_buf_rw_queue(const struct dap_queue
*q
, unsigned int len
)
4497 uint32_t incr
= CMD_MEM_AP_2_SIZE(q
[0].cmd
);
4498 unsigned int len_max
;
4501 len_max
= stlink_usb_block(stlink_dap_handle
);
4503 len_max
= STLINK_MAX_RW16_32
/ incr
;
4505 /* check for no address increment, 32 bits only */
4506 if (len
> 1 && incr
== 4 && q
[0].mem_ap
.addr
== q
[1].mem_ap
.addr
)
4512 for (unsigned int i
= 1; i
< len
; i
++)
4513 if (q
[i
].cmd
!= q
[0].cmd
||
4514 q
[i
].mem_ap
.ap
!= q
[0].mem_ap
.ap
||
4515 q
[i
].mem_ap
.csw
!= q
[0].mem_ap
.csw
||
4516 q
[i
].mem_ap
.addr
!= q
[i
- 1].mem_ap
.addr
+ incr
)
4522 static int stlink_usb_mem_rw_queue(void *handle
, const struct dap_queue
*q
, unsigned int len
, unsigned int *skip
)
4524 unsigned int count
, misc_items
= 0;
4527 unsigned int count_misc
= stlink_usb_count_misc_rw_queue(handle
, q
, len
, &misc_items
);
4528 unsigned int count_buf
= stlink_usb_count_buf_rw_queue(q
, len
);
4530 if (count_misc
> count_buf
) {
4532 retval
= stlink_usb_misc_rw_segment(handle
, q
, count
, misc_items
);
4535 retval
= stlink_usb_buf_rw_segment(handle
, q
, count_buf
);
4537 if (retval
!= ERROR_OK
)
4544 static void stlink_dap_run_internal(struct adiv5_dap
*dap
)
4546 int retval
= stlink_dap_check_reconnect(dap
);
4547 if (retval
!= ERROR_OK
) {
4548 stlink_dap_handle
->queue_index
= 0;
4549 stlink_dap_record_error(retval
);
4553 unsigned int i
= stlink_dap_handle
->queue_index
;
4554 struct dap_queue
*q
= &stlink_dap_handle
->queue
[0];
4556 while (i
&& stlink_dap_get_error() == ERROR_OK
) {
4557 unsigned int skip
= 1;
4561 retval
= stlink_dap_dp_read(q
->dp_r
.dap
, q
->dp_r
.reg
, q
->dp_r
.p_data
);
4564 retval
= stlink_dap_dp_write(q
->dp_w
.dap
, q
->dp_w
.reg
, q
->dp_w
.data
);
4567 retval
= stlink_dap_ap_read(q
->ap_r
.ap
, q
->ap_r
.reg
, q
->ap_r
.p_data
);
4570 /* ignore increment packed, not supported */
4571 if (q
->ap_w
.reg
== MEM_AP_REG_CSW
)
4572 q
->ap_w
.data
&= ~CSW_ADDRINC_PACKED
;
4573 retval
= stlink_dap_ap_write(q
->ap_w
.ap
, q
->ap_w
.reg
, q
->ap_w
.data
);
4576 case CMD_MEM_AP_READ8
:
4577 case CMD_MEM_AP_READ16
:
4578 case CMD_MEM_AP_READ32
:
4579 case CMD_MEM_AP_WRITE8
:
4580 case CMD_MEM_AP_WRITE16
:
4581 case CMD_MEM_AP_WRITE32
:
4582 retval
= stlink_usb_mem_rw_queue(stlink_dap_handle
, q
, i
, &skip
);
4586 LOG_ERROR("ST-Link: Unknown queue command %d", q
->cmd
);
4587 retval
= ERROR_FAIL
;
4590 stlink_dap_record_error(retval
);
4595 stlink_dap_handle
->queue_index
= 0;
4599 static int stlink_dap_run_finalize(struct adiv5_dap
*dap
)
4601 uint32_t ctrlstat
, pwrmask
;
4602 int retval
, saved_retval
;
4604 /* Here no LOG_DEBUG. This is called continuously! */
4607 * ST-Link returns immediately after a DAP write, without waiting for it
4609 * Run a dummy read to DP_RDBUFF, as suggested in
4610 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
4612 if (dap
->stlink_flush_ap_write
) {
4613 dap
->stlink_flush_ap_write
= false;
4614 retval
= stlink_dap_dp_read(dap
, DP_RDBUFF
, NULL
);
4615 if (retval
!= ERROR_OK
) {
4616 dap
->do_reconnect
= true;
4621 saved_retval
= stlink_dap_get_and_clear_error();
4623 retval
= stlink_dap_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
4624 if (retval
!= ERROR_OK
) {
4625 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
4626 dap
->do_reconnect
= true;
4630 if (ctrlstat
& SSTICKYERR
) {
4631 if (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
)
4632 retval
= stlink_dap_dp_write(dap
, DP_CTRL_STAT
,
4633 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
4635 retval
= stlink_dap_dp_write(dap
, DP_ABORT
, STKERRCLR
);
4636 if (retval
!= ERROR_OK
) {
4637 dap
->do_reconnect
= true;
4642 /* check for power lost */
4643 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
4644 if ((ctrlstat
& pwrmask
) != pwrmask
)
4645 dap
->do_reconnect
= true;
4647 return saved_retval
;
4650 static int stlink_dap_op_queue_run(struct adiv5_dap
*dap
)
4652 stlink_dap_run_internal(dap
);
4653 return stlink_dap_run_finalize(dap
);
4657 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
4661 retval
= stlink_dap_closeall_ap();
4662 if (retval
!= ERROR_OK
)
4663 LOG_ERROR("Error closing APs");
4666 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned int reg
,
4669 if (stlink_dap_get_error() != ERROR_OK
)
4672 unsigned int i
= stlink_dap_handle
->queue_index
++;
4673 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4674 q
->cmd
= CMD_DP_READ
;
4677 q
->dp_r
.p_data
= data
;
4679 if (i
== MAX_QUEUE_DEPTH
- 1)
4680 stlink_dap_run_internal(dap
);
4685 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned int reg
,
4688 if (stlink_dap_get_error() != ERROR_OK
)
4691 unsigned int i
= stlink_dap_handle
->queue_index
++;
4692 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4693 q
->cmd
= CMD_DP_WRITE
;
4696 q
->dp_w
.data
= data
;
4698 if (i
== MAX_QUEUE_DEPTH
- 1)
4699 stlink_dap_run_internal(dap
);
4704 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned int reg
,
4707 if (stlink_dap_get_error() != ERROR_OK
)
4710 unsigned int i
= stlink_dap_handle
->queue_index
++;
4711 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4713 /* test STLINK_F_HAS_CSW implicitly tests STLINK_F_HAS_MEM_16BIT, STLINK_F_HAS_MEM_RD_NO_INC
4714 * and STLINK_F_HAS_RW_MISC */
4715 if ((stlink_dap_handle
->version
.flags
& STLINK_F_HAS_CSW
) &&
4716 (reg
== MEM_AP_REG_DRW
|| reg
== MEM_AP_REG_BD0
|| reg
== MEM_AP_REG_BD1
||
4717 reg
== MEM_AP_REG_BD2
|| reg
== MEM_AP_REG_BD3
)) {
4718 /* de-queue previous write-TAR */
4719 struct dap_queue
*prev_q
= q
- 1;
4720 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_TAR
) {
4721 stlink_dap_handle
->queue_index
= i
;
4726 /* de-queue previous write-CSW if it didn't changed ap->csw_default */
4727 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_CSW
&&
4728 !prev_q
->ap_w
.changes_csw_default
) {
4729 stlink_dap_handle
->queue_index
= i
;
4733 switch (ap
->csw_value
& CSW_SIZE_MASK
) {
4735 q
->cmd
= CMD_MEM_AP_READ8
;
4738 q
->cmd
= CMD_MEM_AP_READ16
;
4741 q
->cmd
= CMD_MEM_AP_READ32
;
4744 LOG_ERROR("ST-Link: Unsupported CSW size %d", ap
->csw_value
& CSW_SIZE_MASK
);
4745 stlink_dap_record_error(ERROR_FAIL
);
4749 q
->mem_ap
.addr
= (reg
== MEM_AP_REG_DRW
) ? ap
->tar_value
: ((ap
->tar_value
& ~0x0f) | (reg
& 0x0c));
4751 q
->mem_ap
.p_data
= data
;
4752 q
->mem_ap
.csw
= ap
->csw_default
;
4754 /* force TAR and CSW update */
4755 ap
->tar_valid
= false;
4758 q
->cmd
= CMD_AP_READ
;
4761 q
->ap_r
.p_data
= data
;
4764 if (i
== MAX_QUEUE_DEPTH
- 1)
4765 stlink_dap_run_internal(ap
->dap
);
4770 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned int reg
,
4773 if (stlink_dap_get_error() != ERROR_OK
)
4776 unsigned int i
= stlink_dap_handle
->queue_index
++;
4777 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4779 /* test STLINK_F_HAS_CSW implicitly tests STLINK_F_HAS_MEM_16BIT, STLINK_F_HAS_MEM_WR_NO_INC
4780 * and STLINK_F_HAS_RW_MISC */
4781 if ((stlink_dap_handle
->version
.flags
& STLINK_F_HAS_CSW
) &&
4782 (reg
== MEM_AP_REG_DRW
|| reg
== MEM_AP_REG_BD0
|| reg
== MEM_AP_REG_BD1
||
4783 reg
== MEM_AP_REG_BD2
|| reg
== MEM_AP_REG_BD3
)) {
4784 /* de-queue previous write-TAR */
4785 struct dap_queue
*prev_q
= q
- 1;
4786 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_TAR
) {
4787 stlink_dap_handle
->queue_index
= i
;
4792 /* de-queue previous write-CSW if it didn't changed ap->csw_default */
4793 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_CSW
&&
4794 !prev_q
->ap_w
.changes_csw_default
) {
4795 stlink_dap_handle
->queue_index
= i
;
4799 switch (ap
->csw_value
& CSW_SIZE_MASK
) {
4801 q
->cmd
= CMD_MEM_AP_WRITE8
;
4804 q
->cmd
= CMD_MEM_AP_WRITE16
;
4807 q
->cmd
= CMD_MEM_AP_WRITE32
;
4810 LOG_ERROR("ST-Link: Unsupported CSW size %d", ap
->csw_value
& CSW_SIZE_MASK
);
4811 stlink_dap_record_error(ERROR_FAIL
);
4815 q
->mem_ap
.addr
= (reg
== MEM_AP_REG_DRW
) ? ap
->tar_value
: ((ap
->tar_value
& ~0x0f) | (reg
& 0x0c));
4817 q
->mem_ap
.data
= data
;
4818 q
->mem_ap
.csw
= ap
->csw_default
;
4820 /* force TAR and CSW update */
4821 ap
->tar_valid
= false;
4824 q
->cmd
= CMD_AP_WRITE
;
4827 q
->ap_w
.data
= data
;
4828 if (reg
== MEM_AP_REG_CSW
&& ap
->csw_default
!= last_csw_default
[ap
->ap_num
]) {
4829 q
->ap_w
.changes_csw_default
= true;
4830 last_csw_default
[ap
->ap_num
] = ap
->csw_default
;
4832 q
->ap_w
.changes_csw_default
= false;
4836 if (i
== MAX_QUEUE_DEPTH
- 1)
4837 stlink_dap_run_internal(ap
->dap
);
4842 static int stlink_swim_op_srst(void)
4844 return stlink_swim_generate_rst(stlink_dap_handle
);
4847 static int stlink_swim_op_read_mem(uint32_t addr
, uint32_t size
,
4848 uint32_t count
, uint8_t *buffer
)
4851 uint32_t bytes_remaining
;
4853 LOG_DEBUG_IO("read at 0x%08" PRIx32
" len %" PRIu32
"*0x%08" PRIx32
, addr
, size
, count
);
4857 bytes_remaining
= (count
> STLINK_SWIM_DATA_SIZE
) ? STLINK_SWIM_DATA_SIZE
: count
;
4858 retval
= stlink_swim_readbytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
4859 if (retval
!= ERROR_OK
)
4862 buffer
+= bytes_remaining
;
4863 addr
+= bytes_remaining
;
4864 count
-= bytes_remaining
;
4870 static int stlink_swim_op_write_mem(uint32_t addr
, uint32_t size
,
4871 uint32_t count
, const uint8_t *buffer
)
4874 uint32_t bytes_remaining
;
4876 LOG_DEBUG_IO("write at 0x%08" PRIx32
" len %" PRIu32
"*0x%08" PRIx32
, addr
, size
, count
);
4880 bytes_remaining
= (count
> STLINK_SWIM_DATA_SIZE
) ? STLINK_SWIM_DATA_SIZE
: count
;
4881 retval
= stlink_swim_writebytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
4882 if (retval
!= ERROR_OK
)
4885 buffer
+= bytes_remaining
;
4886 addr
+= bytes_remaining
;
4887 count
-= bytes_remaining
;
4893 static int stlink_swim_op_reconnect(void)
4897 retval
= stlink_usb_mode_enter(stlink_dap_handle
, STLINK_MODE_DEBUG_SWIM
);
4898 if (retval
!= ERROR_OK
)
4901 return stlink_swim_resync(stlink_dap_handle
);
4904 static int stlink_dap_config_trace(bool enabled
,
4905 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
4906 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
4907 uint16_t *prescaler
)
4909 return stlink_config_trace(stlink_dap_handle
, enabled
, pin_protocol
,
4910 port_size
, trace_freq
, traceclkin_freq
,
4914 static int stlink_dap_trace_read(uint8_t *buf
, size_t *size
)
4916 return stlink_usb_trace_read(stlink_dap_handle
, buf
, size
);
4920 COMMAND_HANDLER(stlink_dap_serial_command
)
4922 LOG_DEBUG("stlink_dap_serial_command");
4924 if (CMD_ARGC
!= 1) {
4925 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
4926 return ERROR_COMMAND_SYNTAX_ERROR
;
4929 if (stlink_dap_param
.serial
) {
4930 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
4931 free((void *)stlink_dap_param
.serial
);
4934 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
4939 COMMAND_HANDLER(stlink_dap_vid_pid
)
4941 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
4943 if (CMD_ARGC
> max_usb_ids
* 2) {
4944 LOG_WARNING("ignoring extra IDs in vid_pid "
4945 "(maximum is %d pairs)", max_usb_ids
);
4946 CMD_ARGC
= max_usb_ids
* 2;
4948 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
4949 LOG_WARNING("incomplete vid_pid configuration directive");
4950 return ERROR_COMMAND_SYNTAX_ERROR
;
4952 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
4953 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
4954 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
4957 /* null termination */
4958 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
4964 COMMAND_HANDLER(stlink_dap_backend_command
)
4966 /* default values */
4967 bool use_stlink_tcp
= false;
4968 uint16_t stlink_tcp_port
= 7184;
4970 if (CMD_ARGC
== 0 || CMD_ARGC
> 2)
4971 return ERROR_COMMAND_SYNTAX_ERROR
;
4972 else if (strcmp(CMD_ARGV
[0], "usb") == 0) {
4974 return ERROR_COMMAND_SYNTAX_ERROR
;
4975 /* else use_stlink_tcp = false (already the case ) */
4976 } else if (strcmp(CMD_ARGV
[0], "tcp") == 0) {
4977 use_stlink_tcp
= true;
4979 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[1], stlink_tcp_port
);
4981 return ERROR_COMMAND_SYNTAX_ERROR
;
4983 stlink_dap_param
.use_stlink_tcp
= use_stlink_tcp
;
4984 stlink_dap_param
.stlink_tcp_port
= stlink_tcp_port
;
4989 #define BYTES_PER_LINE 16
4990 COMMAND_HANDLER(stlink_dap_cmd_command
)
4992 unsigned int rx_n
, tx_n
;
4993 struct stlink_usb_handle_s
*h
= stlink_dap_handle
;
4996 return ERROR_COMMAND_SYNTAX_ERROR
;
4998 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], rx_n
);
4999 tx_n
= CMD_ARGC
- 1;
5000 if (tx_n
> STLINK_SG_SIZE
|| rx_n
> STLINK_DATA_SIZE
) {
5001 LOG_ERROR("max %x byte sent and %d received", STLINK_SG_SIZE
, STLINK_DATA_SIZE
);
5002 return ERROR_COMMAND_SYNTAX_ERROR
;
5005 stlink_usb_init_buffer(h
, h
->rx_ep
, rx_n
);
5007 for (unsigned int i
= 0; i
< tx_n
; i
++) {
5009 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[i
+ 1], byte
);
5010 h
->cmdbuf
[h
->cmdidx
++] = byte
;
5013 int retval
= stlink_usb_xfer_noerrcheck(h
, h
->databuf
, rx_n
);
5014 if (retval
!= ERROR_OK
) {
5015 LOG_ERROR("Error %d", retval
);
5019 for (unsigned int i
= 0; i
< rx_n
; i
++)
5020 command_print_sameline(CMD
, "0x%02x%c", h
->databuf
[i
],
5021 ((i
== (rx_n
- 1)) || ((i
% BYTES_PER_LINE
) == (BYTES_PER_LINE
- 1))) ? '\n' : ' ');
5027 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
5030 .handler
= stlink_dap_serial_command
,
5031 .mode
= COMMAND_CONFIG
,
5032 .help
= "set the serial number of the adapter",
5033 .usage
= "<serial_number>",
5037 .handler
= stlink_dap_vid_pid
,
5038 .mode
= COMMAND_CONFIG
,
5039 .help
= "USB VID and PID of the adapter",
5040 .usage
= "(vid pid)+",
5044 .handler
= &stlink_dap_backend_command
,
5045 .mode
= COMMAND_CONFIG
,
5046 .help
= "select which ST-Link backend to use",
5047 .usage
= "usb | tcp [port]",
5051 .handler
= stlink_dap_cmd_command
,
5052 .mode
= COMMAND_EXEC
,
5053 .help
= "send arbitrary command",
5054 .usage
= "rx_n (tx_byte)+",
5056 COMMAND_REGISTRATION_DONE
5060 static const struct command_registration stlink_dap_command_handlers
[] = {
5063 .mode
= COMMAND_ANY
,
5064 .help
= "perform st-link management",
5065 .chain
= stlink_dap_subcommand_handlers
,
5068 COMMAND_REGISTRATION_DONE
5072 static int stlink_dap_init(void)
5074 enum reset_types jtag_reset_config
= jtag_get_reset_config();
5075 enum stlink_mode mode
;
5078 LOG_DEBUG("stlink_dap_init()");
5080 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
5081 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
5082 stlink_dap_param
.connect_under_reset
= true;
5084 LOG_WARNING("\'srst_nogate\' reset_config option is required");
5087 if (transport_is_dapdirect_swd())
5088 mode
= STLINK_MODE_DEBUG_SWD
;
5089 else if (transport_is_dapdirect_jtag())
5090 mode
= STLINK_MODE_DEBUG_JTAG
;
5091 else if (transport_is_swim())
5092 mode
= STLINK_MODE_DEBUG_SWIM
;
5094 LOG_ERROR("Unsupported transport");
5098 retval
= stlink_open(&stlink_dap_param
, mode
, (void **)&stlink_dap_handle
);
5099 if (retval
!= ERROR_OK
)
5102 if ((mode
!= STLINK_MODE_DEBUG_SWIM
) &&
5103 !(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
5104 LOG_ERROR("ST-Link version does not support DAP direct transport");
5111 static int stlink_dap_quit(void)
5113 LOG_DEBUG("stlink_dap_quit()");
5115 free((void *)stlink_dap_param
.serial
);
5116 stlink_dap_param
.serial
= NULL
;
5118 return stlink_close(stlink_dap_handle
);
5122 static int stlink_dap_reset(int req_trst
, int req_srst
)
5124 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
5125 return stlink_usb_assert_srst(stlink_dap_handle
,
5126 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
5127 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
5131 static int stlink_dap_speed(int speed
)
5134 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
5135 return ERROR_JTAG_NOT_IMPLEMENTED
;
5138 stlink_dap_param
.initial_interface_speed
= speed
;
5139 stlink_speed(stlink_dap_handle
, speed
, false);
5144 static int stlink_dap_khz(int khz
, int *jtag_speed
)
5147 LOG_ERROR("RCLK not supported");
5151 *jtag_speed
= stlink_speed(stlink_dap_handle
, khz
, true);
5156 static int stlink_dap_speed_div(int speed
, int *khz
)
5162 static const struct dap_ops stlink_dap_ops
= {
5163 .connect
= stlink_dap_op_connect
,
5164 .send_sequence
= stlink_dap_op_send_sequence
,
5165 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
5166 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
5167 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
5168 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
5169 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
5170 .run
= stlink_dap_op_queue_run
,
5171 .sync
= NULL
, /* optional */
5172 .quit
= stlink_dap_op_quit
, /* optional */
5175 static const struct swim_driver stlink_swim_ops
= {
5176 .srst
= stlink_swim_op_srst
,
5177 .read_mem
= stlink_swim_op_read_mem
,
5178 .write_mem
= stlink_swim_op_write_mem
,
5179 .reconnect
= stlink_swim_op_reconnect
,
5182 static const char *const stlink_dap_transport
[] = { "dapdirect_swd", "dapdirect_jtag", "swim", NULL
};
5184 struct adapter_driver stlink_dap_adapter_driver
= {
5186 .transports
= stlink_dap_transport
,
5187 .commands
= stlink_dap_command_handlers
,
5189 .init
= stlink_dap_init
,
5190 .quit
= stlink_dap_quit
,
5191 .reset
= stlink_dap_reset
,
5192 .speed
= stlink_dap_speed
,
5193 .khz
= stlink_dap_khz
,
5194 .speed_div
= stlink_dap_speed_div
,
5195 .config_trace
= stlink_dap_config_trace
,
5196 .poll_trace
= stlink_dap_trace_read
,
5198 .dap_jtag_ops
= &stlink_dap_ops
,
5199 .dap_swd_ops
= &stlink_dap_ops
,
5200 .swim_ops
= &stlink_swim_ops
,