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/binarybuffer.h>
37 #include <helper/bits.h>
38 #include <helper/system.h>
39 #include <jtag/interface.h>
40 #include <jtag/hla/hla_layout.h>
41 #include <jtag/hla/hla_transport.h>
42 #include <jtag/hla/hla_interface.h>
43 #include <jtag/swim.h>
44 #include <target/arm_adi_v5.h>
45 #include <target/target.h>
46 #include <transport/transport.h>
48 #include <target/cortex_m.h>
50 #include <helper/system.h>
52 #ifdef HAVE_ARPA_INET_H
53 #include <arpa/inet.h>
56 #ifdef HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
60 #include "libusb_helper.h"
63 #define USE_LIBUSB_ASYNCIO
66 #define STLINK_SERIAL_LEN 24
68 #define ENDPOINT_IN 0x80
69 #define ENDPOINT_OUT 0x00
71 #define STLINK_WRITE_TIMEOUT 1000
72 #define STLINK_READ_TIMEOUT 1000
74 #define STLINK_RX_EP (1|ENDPOINT_IN)
75 #define STLINK_TX_EP (2|ENDPOINT_OUT)
76 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
78 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
79 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
81 #define STLINK_SG_SIZE (31)
82 #define STLINK_DATA_SIZE (6144)
83 #define STLINK_CMD_SIZE_V2 (16)
84 #define STLINK_CMD_SIZE_V1 (10)
86 #define STLINK_V1_PID (0x3744)
87 #define STLINK_V2_PID (0x3748)
88 #define STLINK_V2_1_PID (0x374B)
89 #define STLINK_V2_1_NO_MSD_PID (0x3752)
90 #define STLINK_V3_USBLOADER_PID (0x374D)
91 #define STLINK_V3E_PID (0x374E)
92 #define STLINK_V3S_PID (0x374F)
93 #define STLINK_V3_2VCP_PID (0x3753)
94 #define STLINK_V3E_NO_MSD_PID (0x3754)
97 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
98 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
99 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
101 * For 16 and 32bit read/writes stlink handles USB packet split and the limit
102 * is the internal buffer size of 6144 bytes.
103 * TODO: override ADIv5 layer's tar_autoincr_block that limits the transfer
104 * to 1024 or 4096 bytes
106 #define STLINK_MAX_RW8 (64)
107 #define STLINKV3_MAX_RW8 (512)
108 #define STLINK_MAX_RW16_32 STLINK_DATA_SIZE
109 #define STLINK_SWIM_DATA_SIZE STLINK_DATA_SIZE
111 /* "WAIT" responses will be retried (with exponential backoff) at
112 * most this many times before failing to caller.
114 #define MAX_WAIT_RETRIES 8
116 /* HLA is currently limited at AP#0 and no control on CSW */
117 #define STLINK_HLA_AP_NUM 0
118 #define STLINK_HLA_CSW 0
120 enum stlink_jtag_api_version
{
121 STLINK_JTAG_API_V1
= 1,
127 STLINK_MODE_UNKNOWN
= 0,
130 STLINK_MODE_DEBUG_JTAG
,
131 STLINK_MODE_DEBUG_SWD
,
132 STLINK_MODE_DEBUG_SWIM
136 struct stlink_usb_version
{
143 /** jtag api version supported */
144 enum stlink_jtag_api_version jtag_api
;
145 /** one bit for each feature supported. See macros STLINK_F_* */
149 struct stlink_usb_priv_s
{
151 struct libusb_device_handle
*fd
;
153 struct libusb_transfer
*trans
;
156 struct stlink_tcp_priv_s
{
171 struct stlink_backend_s
{
173 int (*open
)(void *handle
, struct hl_interface_param_s
*param
);
175 int (*close
)(void *handle
);
177 int (*xfer_noerrcheck
)(void *handle
, const uint8_t *buf
, int size
);
179 int (*read_trace
)(void *handle
, const uint8_t *buf
, int size
);
182 /* TODO: make queue size dynamic */
183 /* TODO: don't allocate queue for HLA */
184 #define MAX_QUEUE_DEPTH (4096)
194 * encode the bytes size in the enum's value. This makes easy to extract it
195 * with a simple logic AND, by using the macro CMD_MEM_AP_2_SIZE() below
197 CMD_MEM_AP_READ8
= 0x10 + 1,
198 CMD_MEM_AP_READ16
= 0x10 + 2,
199 CMD_MEM_AP_READ32
= 0x10 + 4,
201 CMD_MEM_AP_WRITE8
= 0x20 + 1,
202 CMD_MEM_AP_WRITE16
= 0x20 + 2,
203 CMD_MEM_AP_WRITE32
= 0x20 + 4,
206 #define CMD_MEM_AP_2_SIZE(cmd) ((cmd) & 7)
213 struct adiv5_dap
*dap
;
218 struct adiv5_dap
*dap
;
230 bool changes_csw_default
;
245 struct stlink_usb_handle_s
{
247 struct stlink_backend_s
*backend
;
250 struct stlink_usb_priv_s usb_backend_priv
;
251 struct stlink_tcp_priv_s tcp_backend_priv
;
268 uint32_t max_mem_packet
;
270 enum stlink_mode st_mode
;
272 struct stlink_usb_version version
;
279 /** whether SWO tracing is enabled or not */
281 /** trace module source clock */
284 /** reconnect is needed next time we try to query the
286 bool reconnect_pending
;
287 /** queue of dap_direct operations */
288 struct dap_queue queue
[MAX_QUEUE_DEPTH
];
289 /** first element available in the queue */
290 unsigned int queue_index
;
294 static inline int stlink_usb_open(void *handle
, struct hl_interface_param_s
*param
)
296 struct stlink_usb_handle_s
*h
= handle
;
297 return h
->backend
->open(handle
, param
);
301 static inline int stlink_usb_close(void *handle
)
303 struct stlink_usb_handle_s
*h
= handle
;
304 return h
->backend
->close(handle
);
307 static inline int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
309 struct stlink_usb_handle_s
*h
= handle
;
310 return h
->backend
->xfer_noerrcheck(handle
, buf
, size
);
313 #define STLINK_SWIM_ERR_OK 0x00
314 #define STLINK_SWIM_BUSY 0x01
315 #define STLINK_DEBUG_ERR_OK 0x80
316 #define STLINK_DEBUG_ERR_FAULT 0x81
317 #define STLINK_SWD_AP_WAIT 0x10
318 #define STLINK_SWD_AP_FAULT 0x11
319 #define STLINK_SWD_AP_ERROR 0x12
320 #define STLINK_SWD_AP_PARITY_ERROR 0x13
321 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
322 #define STLINK_JTAG_WRITE_ERROR 0x0c
323 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
324 #define STLINK_SWD_DP_WAIT 0x14
325 #define STLINK_SWD_DP_FAULT 0x15
326 #define STLINK_SWD_DP_ERROR 0x16
327 #define STLINK_SWD_DP_PARITY_ERROR 0x17
329 #define STLINK_SWD_AP_WDATA_ERROR 0x18
330 #define STLINK_SWD_AP_STICKY_ERROR 0x19
331 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
333 #define STLINK_BAD_AP_ERROR 0x1d
335 #define STLINK_CORE_RUNNING 0x80
336 #define STLINK_CORE_HALTED 0x81
337 #define STLINK_CORE_STAT_UNKNOWN -1
339 #define STLINK_GET_VERSION 0xF1
340 #define STLINK_DEBUG_COMMAND 0xF2
341 #define STLINK_DFU_COMMAND 0xF3
342 #define STLINK_SWIM_COMMAND 0xF4
343 #define STLINK_GET_CURRENT_MODE 0xF5
344 #define STLINK_GET_TARGET_VOLTAGE 0xF7
346 #define STLINK_DEV_DFU_MODE 0x00
347 #define STLINK_DEV_MASS_MODE 0x01
348 #define STLINK_DEV_DEBUG_MODE 0x02
349 #define STLINK_DEV_SWIM_MODE 0x03
350 #define STLINK_DEV_BOOTLOADER_MODE 0x04
351 #define STLINK_DEV_UNKNOWN_MODE -1
353 #define STLINK_DFU_EXIT 0x07
356 STLINK_SWIM_ENTER_SEQ
357 1.3ms low then 750Hz then 1.5kHz
360 STM8 DM pulls reset pin low 50us
363 uint8_t (0=low|1=high)
370 send synchronization seq (16us low, response 64 clocks low)
372 #define STLINK_SWIM_ENTER 0x00
373 #define STLINK_SWIM_EXIT 0x01
374 #define STLINK_SWIM_READ_CAP 0x02
375 #define STLINK_SWIM_SPEED 0x03
376 #define STLINK_SWIM_ENTER_SEQ 0x04
377 #define STLINK_SWIM_GEN_RST 0x05
378 #define STLINK_SWIM_RESET 0x06
379 #define STLINK_SWIM_ASSERT_RESET 0x07
380 #define STLINK_SWIM_DEASSERT_RESET 0x08
381 #define STLINK_SWIM_READSTATUS 0x09
382 #define STLINK_SWIM_WRITEMEM 0x0a
383 #define STLINK_SWIM_READMEM 0x0b
384 #define STLINK_SWIM_READBUF 0x0c
386 #define STLINK_DEBUG_GETSTATUS 0x01
387 #define STLINK_DEBUG_FORCEDEBUG 0x02
388 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
389 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
390 #define STLINK_DEBUG_APIV1_READREG 0x05
391 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
392 #define STLINK_DEBUG_READMEM_32BIT 0x07
393 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
394 #define STLINK_DEBUG_RUNCORE 0x09
395 #define STLINK_DEBUG_STEPCORE 0x0a
396 #define STLINK_DEBUG_APIV1_SETFP 0x0b
397 #define STLINK_DEBUG_READMEM_8BIT 0x0c
398 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
399 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
400 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
401 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
403 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
404 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
405 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
407 #define STLINK_DEBUG_APIV1_ENTER 0x20
408 #define STLINK_DEBUG_EXIT 0x21
409 #define STLINK_DEBUG_READCOREID 0x22
411 #define STLINK_DEBUG_APIV2_ENTER 0x30
412 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
413 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
414 #define STLINK_DEBUG_APIV2_READREG 0x33
415 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
416 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
417 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
419 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
420 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
421 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
423 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
425 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
426 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
427 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
428 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
429 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
430 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
431 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
432 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
433 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
435 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
436 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
438 #define STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC 0x50
440 #define STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC 0x54
442 #define STLINK_APIV3_SET_COM_FREQ 0x61
443 #define STLINK_APIV3_GET_COM_FREQ 0x62
445 #define STLINK_APIV3_GET_VERSION_EX 0xFB
447 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
448 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
449 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
451 #define STLINK_DEBUG_PORT_ACCESS 0xffff
453 #define STLINK_TRACE_SIZE 4096
454 #define STLINK_TRACE_MAX_HZ 2000000
455 #define STLINK_V3_TRACE_MAX_HZ 24000000
457 #define STLINK_V3_MAX_FREQ_NB 10
459 #define REQUEST_SENSE 0x03
460 #define REQUEST_SENSE_LENGTH 18
462 /* STLINK TCP commands */
463 #define STLINK_TCP_CMD_REFRESH_DEVICE_LIST 0x00
464 #define STLINK_TCP_CMD_GET_NB_DEV 0x01
465 #define STLINK_TCP_CMD_GET_DEV_INFO 0x02
466 #define STLINK_TCP_CMD_OPEN_DEV 0x03
467 #define STLINK_TCP_CMD_CLOSE_DEV 0x04
468 #define STLINK_TCP_CMD_SEND_USB_CMD 0x05
469 #define STLINK_TCP_CMD_GET_SERVER_VERSION 0x06
470 #define STLINK_TCP_CMD_GET_NB_OF_DEV_CLIENTS 0x07
472 /* STLINK TCP constants */
473 #define OPENOCD_STLINK_TCP_API_VERSION 1
474 #define STLINK_TCP_REQUEST_WRITE 0
475 #define STLINK_TCP_REQUEST_READ 1
476 #define STLINK_TCP_REQUEST_READ_SWO 3
477 #define STLINK_TCP_SS_SIZE 4
478 #define STLINK_TCP_USB_CMD_SIZE 32
479 #define STLINK_TCP_SERIAL_SIZE 32
480 #define STLINK_TCP_SEND_BUFFER_SIZE 10240
481 #define STLINK_TCP_RECV_BUFFER_SIZE 10240
483 /* STLINK TCP command status */
484 #define STLINK_TCP_SS_OK 0x00000001
485 #define STLINK_TCP_SS_MEMORY_PROBLEM 0x00001000
486 #define STLINK_TCP_SS_TIMEOUT 0x00001001
487 #define STLINK_TCP_SS_BAD_PARAMETER 0x00001002
488 #define STLINK_TCP_SS_OPEN_ERR 0x00001003
489 #define STLINK_TCP_SS_TRUNCATED_DATA 0x00001052
490 #define STLINK_TCP_SS_CMD_NOT_AVAILABLE 0x00001053
491 #define STLINK_TCP_SS_TCP_ERROR 0x00002001
492 #define STLINK_TCP_SS_TCP_CANT_CONNECT 0x00002002
493 #define STLINK_TCP_SS_WIN32_ERROR 0x00010000
496 * Map the relevant features, quirks and workaround for specific firmware
499 #define STLINK_F_HAS_TRACE BIT(0) /* v2>=j13 || v3 */
500 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(1) /* v2>=j15 || v3 */
501 #define STLINK_F_HAS_SWD_SET_FREQ BIT(2) /* v2>=j22 */
502 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(3) /* v2>=j24 */
503 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(4) /* v2>=j24 && v2<j32 */
504 #define STLINK_F_HAS_DAP_REG BIT(5) /* v2>=j24 || v3 */
505 #define STLINK_F_HAS_MEM_16BIT BIT(6) /* v2>=j26 || v3 */
506 #define STLINK_F_HAS_AP_INIT BIT(7) /* v2>=j28 || v3 */
507 #define STLINK_F_FIX_CLOSE_AP BIT(8) /* v2>=j29 || v3 */
508 #define STLINK_F_HAS_DPBANKSEL BIT(9) /* v2>=j32 || v3>=j2 */
509 #define STLINK_F_HAS_RW8_512BYTES BIT(10) /* v3>=j6 */
512 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
513 #define STLINK_F_HAS_FPU_REG STLINK_F_HAS_GETLASTRWSTATUS2
514 #define STLINK_F_HAS_MEM_WR_NO_INC STLINK_F_HAS_MEM_16BIT
515 #define STLINK_F_HAS_MEM_RD_NO_INC STLINK_F_HAS_DPBANKSEL
516 #define STLINK_F_HAS_CSW STLINK_F_HAS_DPBANKSEL
518 #define STLINK_REGSEL_IS_FPU(x) ((x) > 0x1F)
525 /* SWD clock speed */
526 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
528 {1800, 1}, /* default */
541 /* JTAG clock speed */
542 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
546 {1125, 32}, /* default */
552 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
553 static int stlink_swim_status(void *handle
);
554 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
555 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
556 static int stlink_speed(void *handle
, int khz
, bool query
);
557 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
);
560 static unsigned int stlink_usb_block(void *handle
)
562 struct stlink_usb_handle_s
*h
= handle
;
566 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
567 return STLINKV3_MAX_RW8
;
569 return STLINK_MAX_RW8
;
572 #ifdef USE_LIBUSB_ASYNCIO
574 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
576 int *completed
= transfer
->user_data
;
578 /* caller interprets result and frees transfer */
582 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
584 int r
, *completed
= transfer
->user_data
;
586 while (!*completed
) {
587 r
= jtag_libusb_handle_events_completed(completed
);
589 if (r
== LIBUSB_ERROR_INTERRUPTED
)
591 libusb_cancel_transfer(transfer
);
598 static int transfer_error_status(const struct libusb_transfer
*transfer
)
602 switch (transfer
->status
) {
603 case LIBUSB_TRANSFER_COMPLETED
:
606 case LIBUSB_TRANSFER_TIMED_OUT
:
607 r
= LIBUSB_ERROR_TIMEOUT
;
609 case LIBUSB_TRANSFER_STALL
:
610 r
= LIBUSB_ERROR_PIPE
;
612 case LIBUSB_TRANSFER_OVERFLOW
:
613 r
= LIBUSB_ERROR_OVERFLOW
;
615 case LIBUSB_TRANSFER_NO_DEVICE
:
616 r
= LIBUSB_ERROR_NO_DEVICE
;
618 case LIBUSB_TRANSFER_ERROR
:
619 case LIBUSB_TRANSFER_CANCELLED
:
623 r
= LIBUSB_ERROR_OTHER
;
637 size_t transfer_size
;
638 struct libusb_transfer
*transfer
;
641 static int jtag_libusb_bulk_transfer_n(
642 struct libusb_device_handle
*dev_handle
,
643 struct jtag_xfer
*transfers
,
648 int returnval
= ERROR_OK
;
651 for (size_t i
= 0; i
< n_transfers
; ++i
) {
652 transfers
[i
].retval
= 0;
653 transfers
[i
].completed
= 0;
654 transfers
[i
].transfer_size
= 0;
655 transfers
[i
].transfer
= libusb_alloc_transfer(0);
657 if (!transfers
[i
].transfer
) {
658 for (size_t j
= 0; j
< i
; ++j
)
659 libusb_free_transfer(transfers
[j
].transfer
);
661 LOG_DEBUG("ERROR, failed to alloc usb transfers");
662 for (size_t k
= 0; k
< n_transfers
; ++k
)
663 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
668 for (size_t i
= 0; i
< n_transfers
; ++i
) {
669 libusb_fill_bulk_transfer(
670 transfers
[i
].transfer
,
672 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
673 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
674 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
676 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
678 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
680 /* Probably no point continuing to submit transfers once a submission fails.
681 * As a result, tag all remaining transfers as errors.
683 for (size_t j
= i
; j
< n_transfers
; ++j
)
684 transfers
[j
].retval
= retval
;
686 returnval
= ERROR_FAIL
;
691 /* Wait for every submitted USB transfer to complete.
693 for (size_t i
= 0; i
< n_transfers
; ++i
) {
694 if (transfers
[i
].retval
== 0) {
695 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
697 retval
= transfer_error_status(transfers
[i
].transfer
);
699 returnval
= ERROR_FAIL
;
700 transfers
[i
].retval
= retval
;
701 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
703 /* Assuming actual_length is only valid if there is no transfer error.
705 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
709 libusb_free_transfer(transfers
[i
].transfer
);
710 transfers
[i
].transfer
= NULL
;
720 static int stlink_usb_xfer_v1_get_status(void *handle
)
722 struct stlink_usb_handle_s
*h
= handle
;
728 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
730 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
731 STLINK_READ_TIMEOUT
, &tr
);
737 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
740 if (t1
!= 0x53425355)
748 if (h
->cmdbuf
[12] != 0)
754 #ifdef USE_LIBUSB_ASYNCIO
755 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
757 struct stlink_usb_handle_s
*h
= handle
;
761 size_t n_transfers
= 0;
762 struct jtag_xfer transfers
[2];
764 memset(transfers
, 0, sizeof(transfers
));
766 transfers
[0].ep
= h
->tx_ep
;
767 transfers
[0].buf
= h
->cmdbuf
;
768 transfers
[0].size
= cmdsize
;
772 if (h
->direction
== h
->tx_ep
&& size
) {
773 transfers
[1].ep
= h
->tx_ep
;
774 transfers
[1].buf
= (uint8_t *)buf
;
775 transfers
[1].size
= size
;
778 } else if (h
->direction
== h
->rx_ep
&& size
) {
779 transfers
[1].ep
= h
->rx_ep
;
780 transfers
[1].buf
= (uint8_t *)buf
;
781 transfers
[1].size
= size
;
786 return jtag_libusb_bulk_transfer_n(
787 h
->usb_backend_priv
.fd
,
790 STLINK_WRITE_TIMEOUT
);
793 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
795 struct stlink_usb_handle_s
*h
= handle
;
800 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
801 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
802 if (ret
|| tr
!= cmdsize
)
805 if (h
->direction
== h
->tx_ep
&& size
) {
806 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)buf
,
807 size
, STLINK_WRITE_TIMEOUT
, &tr
);
808 if (ret
|| tr
!= size
) {
809 LOG_DEBUG("bulk write failed");
812 } else if (h
->direction
== h
->rx_ep
&& size
) {
813 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)buf
,
814 size
, STLINK_READ_TIMEOUT
, &tr
);
815 if (ret
|| tr
!= size
) {
816 LOG_DEBUG("bulk read failed");
826 static int stlink_usb_xfer_v1_get_sense(void *handle
)
829 struct stlink_usb_handle_s
*h
= handle
;
833 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
835 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
836 h
->cmdbuf
[h
->cmdidx
++] = 0;
837 h
->cmdbuf
[h
->cmdidx
++] = 0;
838 h
->cmdbuf
[h
->cmdidx
++] = 0;
839 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
841 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
846 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
853 static int stlink_usb_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
855 struct stlink_usb_handle_s
*h
= handle
;
858 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->trace_ep
, (char *)buf
, size
,
859 STLINK_READ_TIMEOUT
, &tr
);
860 if (ret
|| tr
!= size
) {
861 LOG_ERROR("bulk trace read failed");
869 transfers block in cmdbuf
870 <size> indicates number of bytes in the following
872 Ignore the (eventual) error code in the received packet.
874 static int stlink_usb_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
876 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
877 struct stlink_usb_handle_s
*h
= handle
;
881 if (h
->version
.stlink
== 1) {
882 cmdsize
= STLINK_SG_SIZE
;
883 /* put length in bCBWCBLength */
884 h
->cmdbuf
[14] = h
->cmdidx
-15;
887 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
892 if (h
->version
.stlink
== 1) {
893 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
894 /* check csw status */
895 if (h
->cmdbuf
[12] == 1) {
896 LOG_DEBUG("get sense");
897 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
908 static int stlink_tcp_send_cmd(void *handle
, int send_size
, int recv_size
, bool check_tcp_status
)
910 struct stlink_usb_handle_s
*h
= handle
;
914 /* send the TCP command */
915 int sent_size
= send(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.send_buf
, send_size
, 0);
916 if (sent_size
!= send_size
) {
917 LOG_ERROR("failed to send USB CMD");
919 LOG_DEBUG("socket send error: %s (errno %d)", strerror(errno
), errno
);
921 LOG_DEBUG("sent size %d (expected %d)", sent_size
, send_size
);
927 /* read the TCP response */
928 int received_size
= recv(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.recv_buf
, recv_size
, 0);
929 if (received_size
!= recv_size
) {
930 LOG_ERROR("failed to receive USB CMD response");
931 if (received_size
== -1)
932 LOG_DEBUG("socket recv error: %s (errno %d)", strerror(errno
), errno
);
934 LOG_DEBUG("received size %d (expected %d)", received_size
, recv_size
);
938 if (check_tcp_status
) {
939 uint32_t tcp_ss
= le_to_h_u32(h
->tcp_backend_priv
.recv_buf
);
940 if (tcp_ss
!= STLINK_TCP_SS_OK
) {
941 LOG_ERROR("TCP error status 0x%X", tcp_ss
);
950 static int stlink_tcp_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
952 struct stlink_usb_handle_s
*h
= handle
;
954 int send_size
= STLINK_TCP_USB_CMD_SIZE
;
955 int recv_size
= STLINK_TCP_SS_SIZE
;
959 /* prepare the TCP command */
960 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_SEND_USB_CMD
;
961 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 3); /* reserved for alignment and future use, must be zero */
962 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
963 /* tcp_backend_priv.send_buf[8..23] already contains the constructed stlink command */
964 h
->tcp_backend_priv
.send_buf
[24] = h
->direction
;
965 memset(&h
->tcp_backend_priv
.send_buf
[25], 0, 3); /* reserved for alignment and future use, must be zero */
967 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[28], size
);
970 * if the xfer is a write request (tx_ep)
971 * > then buf content will be copied
973 * else : the xfer is a read or trace read request (rx_ep or trace_ep)
974 * > the buf content will be filled from &databuf[4].
976 * note : if h->direction is trace_ep, h->cmdbuf is zeros.
979 if (h
->direction
== h
->tx_ep
) { /* STLINK_TCP_REQUEST_WRITE */
981 if (send_size
> STLINK_TCP_SEND_BUFFER_SIZE
) {
982 LOG_ERROR("STLINK_TCP command buffer overflow");
985 memcpy(&h
->tcp_backend_priv
.send_buf
[32], buf
, size
);
986 } else { /* STLINK_TCP_REQUEST_READ or STLINK_TCP_REQUEST_READ_SWO */
988 if (recv_size
> STLINK_TCP_RECV_BUFFER_SIZE
) {
989 LOG_ERROR("STLINK_TCP data buffer overflow");
994 int ret
= stlink_tcp_send_cmd(h
, send_size
, recv_size
, true);
998 if (h
->direction
!= h
->tx_ep
) {
999 /* the read data is located in tcp_backend_priv.recv_buf[4] */
1000 /* most of the case it will be copying the data from tcp_backend_priv.recv_buf[4]
1001 * to handle->cmd_buff which are the same, so let's avoid unnecessary copying */
1002 if (buf
!= &h
->tcp_backend_priv
.recv_buf
[4])
1003 memcpy((uint8_t *)buf
, &h
->tcp_backend_priv
.recv_buf
[4], size
);
1010 static int stlink_tcp_read_trace(void *handle
, const uint8_t *buf
, int size
)
1012 struct stlink_usb_handle_s
*h
= handle
;
1014 stlink_usb_init_buffer(h
, h
->trace_ep
, 0);
1015 return stlink_tcp_xfer_noerrcheck(handle
, buf
, size
);
1019 Converts an STLINK status code held in the first byte of a response
1020 to an openocd error, logs any error/wait status as debug output.
1022 static int stlink_usb_error_check(void *handle
)
1024 struct stlink_usb_handle_s
*h
= handle
;
1028 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1029 switch (h
->databuf
[0]) {
1030 case STLINK_SWIM_ERR_OK
:
1032 case STLINK_SWIM_BUSY
:
1035 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1040 /* TODO: no error checking yet on api V1 */
1041 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1042 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
1044 switch (h
->databuf
[0]) {
1045 case STLINK_DEBUG_ERR_OK
:
1047 case STLINK_DEBUG_ERR_FAULT
:
1048 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
1050 case STLINK_SWD_AP_WAIT
:
1051 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
1053 case STLINK_SWD_DP_WAIT
:
1054 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
1056 case STLINK_JTAG_GET_IDCODE_ERROR
:
1057 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
1059 case STLINK_JTAG_WRITE_ERROR
:
1060 LOG_DEBUG("Write error");
1062 case STLINK_JTAG_WRITE_VERIF_ERROR
:
1063 LOG_DEBUG("Write verify error, ignoring");
1065 case STLINK_SWD_AP_FAULT
:
1066 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
1067 * returns ERROR_OK with the comment:
1068 * Change in error status when reading outside RAM.
1069 * This fix allows CDT plugin to visualize memory.
1071 LOG_DEBUG("STLINK_SWD_AP_FAULT");
1073 case STLINK_SWD_AP_ERROR
:
1074 LOG_DEBUG("STLINK_SWD_AP_ERROR");
1076 case STLINK_SWD_AP_PARITY_ERROR
:
1077 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
1079 case STLINK_SWD_DP_FAULT
:
1080 LOG_DEBUG("STLINK_SWD_DP_FAULT");
1082 case STLINK_SWD_DP_ERROR
:
1083 LOG_DEBUG("STLINK_SWD_DP_ERROR");
1085 case STLINK_SWD_DP_PARITY_ERROR
:
1086 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
1088 case STLINK_SWD_AP_WDATA_ERROR
:
1089 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
1091 case STLINK_SWD_AP_STICKY_ERROR
:
1092 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
1094 case STLINK_SWD_AP_STICKYORUN_ERROR
:
1095 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
1097 case STLINK_BAD_AP_ERROR
:
1098 LOG_DEBUG("STLINK_BAD_AP_ERROR");
1101 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1107 * Wrapper around stlink_usb_xfer_noerrcheck()
1108 * to check the error code in the received packet
1110 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
1116 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1117 if (retval
!= ERROR_OK
)
1120 return stlink_usb_error_check(handle
);
1123 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
1125 Works for commands where the STLINK_DEBUG status is returned in the first
1126 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
1128 Returns an openocd result code.
1130 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
1134 struct stlink_usb_handle_s
*h
= handle
;
1137 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
1138 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1139 if (res
!= ERROR_OK
)
1143 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1144 res
= stlink_swim_status(handle
);
1145 if (res
!= ERROR_OK
)
1149 res
= stlink_usb_error_check(handle
);
1150 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1151 unsigned int delay_us
= (1<<retries
++) * 1000;
1152 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
1161 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
1163 struct stlink_usb_handle_s
*h
= handle
;
1167 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1169 return h
->backend
->read_trace(handle
, buf
, size
);
1173 this function writes transfer length in
1174 the right place in the cb
1176 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
1178 struct stlink_usb_handle_s
*h
= handle
;
1180 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
1183 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
1185 struct stlink_usb_handle_s
*h
= handle
;
1187 /* fill the send buffer */
1188 strcpy((char *)h
->cmdbuf
, "USBC");
1190 /* csw tag not used */
1191 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
1193 /* cbw data transfer length (in the following data phase in or out) */
1194 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
1197 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
1198 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
1199 /* cdb clength (is filled in at xfer) */
1200 h
->cmdbuf
[h
->cmdidx
++] = 0;
1204 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
1206 struct stlink_usb_handle_s
*h
= handle
;
1208 h
->direction
= direction
;
1212 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
1213 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
1215 if (h
->version
.stlink
== 1)
1216 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
1220 static int stlink_usb_version(void *handle
)
1225 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
1226 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
1228 struct stlink_usb_handle_s
*h
= handle
;
1232 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
1234 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
1236 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
1238 if (res
!= ERROR_OK
)
1241 version
= be_to_h_u16(h
->databuf
);
1242 v
= (version
>> 12) & 0x0f;
1243 x
= (version
>> 6) & 0x3f;
1246 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
1247 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
1250 case STLINK_V2_1_PID
:
1251 case STLINK_V2_1_NO_MSD_PID
:
1252 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
1253 /* MxSy : STM8 V2.1 - SWIM only */
1258 /* JxMy : STM32 V2.1 - JTAG/SWD only */
1271 /* STLINK-V3 requires a specific command */
1272 if (v
== 3 && x
== 0 && y
== 0) {
1273 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
1275 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
1277 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
1278 if (res
!= ERROR_OK
)
1282 swim
= h
->databuf
[1];
1283 jtag
= h
->databuf
[2];
1284 msd
= h
->databuf
[3];
1285 bridge
= h
->databuf
[4];
1286 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
1287 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
1290 h
->version
.stlink
= v
;
1291 h
->version
.jtag
= jtag
;
1292 h
->version
.swim
= swim
;
1295 switch (h
->version
.stlink
) {
1297 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
1298 if (h
->version
.jtag
>= 11)
1299 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1301 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
1305 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1306 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1308 /* API for trace from J13 */
1309 /* API for target voltage from J13 */
1310 if (h
->version
.jtag
>= 13)
1311 flags
|= STLINK_F_HAS_TRACE
;
1313 /* preferred API to get last R/W status from J15 */
1314 if (h
->version
.jtag
>= 15)
1315 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1317 /* API to set SWD frequency from J22 */
1318 if (h
->version
.jtag
>= 22)
1319 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1321 /* API to set JTAG frequency from J24 */
1322 /* API to access DAP registers from J24 */
1323 if (h
->version
.jtag
>= 24) {
1324 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1325 flags
|= STLINK_F_HAS_DAP_REG
;
1328 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1329 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1330 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1332 /* API to read/write memory at 16 bit from J26 */
1333 /* API to write memory without address increment from J26 */
1334 if (h
->version
.jtag
>= 26)
1335 flags
|= STLINK_F_HAS_MEM_16BIT
;
1337 /* API required to init AP before any AP access from J28 */
1338 if (h
->version
.jtag
>= 28)
1339 flags
|= STLINK_F_HAS_AP_INIT
;
1341 /* API required to return proper error code on close AP from J29 */
1342 if (h
->version
.jtag
>= 29)
1343 flags
|= STLINK_F_FIX_CLOSE_AP
;
1345 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1346 /* API to read memory without address increment from V2J32 */
1347 /* Memory R/W supports CSW from V2J32 */
1348 if (h
->version
.jtag
>= 32)
1349 flags
|= STLINK_F_HAS_DPBANKSEL
;
1353 /* all STLINK-V3 use api-v3 */
1354 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1356 /* STLINK-V3 is a superset of ST-LINK/V2 */
1359 /* API for target voltage */
1360 flags
|= STLINK_F_HAS_TRACE
;
1362 /* preferred API to get last R/W status */
1363 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1365 /* API to access DAP registers */
1366 flags
|= STLINK_F_HAS_DAP_REG
;
1368 /* API to read/write memory at 16 bit */
1369 /* API to write memory without address increment */
1370 flags
|= STLINK_F_HAS_MEM_16BIT
;
1372 /* API required to init AP before any AP access */
1373 flags
|= STLINK_F_HAS_AP_INIT
;
1375 /* API required to return proper error code on close AP */
1376 flags
|= STLINK_F_FIX_CLOSE_AP
;
1378 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1379 /* API to read memory without address increment from V3J2 */
1380 /* Memory R/W supports CSW from V3J2 */
1381 if (h
->version
.jtag
>= 2)
1382 flags
|= STLINK_F_HAS_DPBANKSEL
;
1384 /* 8bit read/write max packet size 512 bytes from V3J6 */
1385 if (h
->version
.jtag
>= 6)
1386 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1392 h
->version
.flags
= flags
;
1395 p
+= sprintf(p
, "V%d", v
);
1397 p
+= sprintf(p
, "J%d", jtag
);
1399 p
+= sprintf(p
, "M%d", msd
);
1401 p
+= sprintf(p
, "B%d", bridge
);
1403 sprintf(p
, "S%d", swim
);
1405 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1407 h
->version
.jtag_api
,
1414 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1416 struct stlink_usb_handle_s
*h
= handle
;
1417 uint32_t adc_results
[2];
1419 /* no error message, simply quit with error */
1420 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1421 return ERROR_COMMAND_NOTFOUND
;
1423 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1425 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1427 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1429 if (result
!= ERROR_OK
)
1432 /* convert result */
1433 adc_results
[0] = le_to_h_u32(h
->databuf
);
1434 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1436 *target_voltage
= 0;
1439 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1441 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1446 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1448 struct stlink_usb_handle_s
*h
= handle
;
1452 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1453 return ERROR_COMMAND_NOTFOUND
;
1455 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1457 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1458 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1459 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1462 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1464 if (result
!= ERROR_OK
)
1470 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1472 struct stlink_usb_handle_s
*h
= handle
;
1476 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1477 return ERROR_COMMAND_NOTFOUND
;
1479 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1481 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1482 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1483 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1486 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1488 if (result
!= ERROR_OK
)
1495 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1498 struct stlink_usb_handle_s
*h
= handle
;
1502 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1504 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1506 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1508 if (res
!= ERROR_OK
)
1511 *mode
= h
->databuf
[0];
1517 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1520 struct stlink_usb_handle_s
*h
= handle
;
1524 /* on api V2 we are able the read the latest command
1526 * TODO: we need the test on api V1 too
1528 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1531 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1534 case STLINK_MODE_DEBUG_JTAG
:
1535 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1536 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1537 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1539 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1540 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1542 case STLINK_MODE_DEBUG_SWD
:
1543 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1544 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1545 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1547 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1548 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1550 case STLINK_MODE_DEBUG_SWIM
:
1551 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1552 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1553 /* swim enter does not return any response or status */
1554 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1555 case STLINK_MODE_DFU
:
1556 case STLINK_MODE_MASS
:
1561 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1565 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1568 struct stlink_usb_handle_s
*h
= handle
;
1572 /* command with no reply, use a valid endpoint but zero size */
1573 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1576 case STLINK_MODE_DEBUG_JTAG
:
1577 case STLINK_MODE_DEBUG_SWD
:
1578 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1579 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1581 case STLINK_MODE_DEBUG_SWIM
:
1582 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1583 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1585 case STLINK_MODE_DFU
:
1586 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1587 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1589 case STLINK_MODE_MASS
:
1594 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1596 if (res
!= ERROR_OK
)
1602 static int stlink_usb_assert_srst(void *handle
, int srst
);
1604 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1607 case HL_TRANSPORT_SWD
:
1608 return STLINK_MODE_DEBUG_SWD
;
1609 case HL_TRANSPORT_JTAG
:
1610 return STLINK_MODE_DEBUG_JTAG
;
1612 return STLINK_MODE_UNKNOWN
;
1617 static int stlink_usb_exit_mode(void *handle
)
1621 enum stlink_mode emode
;
1625 res
= stlink_usb_current_mode(handle
, &mode
);
1627 if (res
!= ERROR_OK
)
1630 LOG_DEBUG("MODE: 0x%02X", mode
);
1632 /* try to exit current mode */
1634 case STLINK_DEV_DFU_MODE
:
1635 emode
= STLINK_MODE_DFU
;
1637 case STLINK_DEV_DEBUG_MODE
:
1638 emode
= STLINK_MODE_DEBUG_SWD
;
1640 case STLINK_DEV_SWIM_MODE
:
1641 emode
= STLINK_MODE_DEBUG_SWIM
;
1643 case STLINK_DEV_BOOTLOADER_MODE
:
1644 case STLINK_DEV_MASS_MODE
:
1646 emode
= STLINK_MODE_UNKNOWN
;
1650 if (emode
!= STLINK_MODE_UNKNOWN
)
1651 return stlink_usb_mode_leave(handle
, emode
);
1657 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1661 enum stlink_mode emode
;
1662 struct stlink_usb_handle_s
*h
= handle
;
1666 res
= stlink_usb_exit_mode(handle
);
1667 if (res
!= ERROR_OK
)
1670 res
= stlink_usb_current_mode(handle
, &mode
);
1672 if (res
!= ERROR_OK
)
1675 /* we check the target voltage here as an aid to debugging connection problems.
1676 * the stlink requires the target Vdd to be connected for reliable debugging.
1677 * this cmd is supported in all modes except DFU
1679 if (mode
!= STLINK_DEV_DFU_MODE
) {
1681 float target_voltage
;
1683 /* check target voltage (if supported) */
1684 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1686 if (res
!= ERROR_OK
) {
1687 if (res
!= ERROR_COMMAND_NOTFOUND
)
1688 LOG_ERROR("voltage check failed");
1689 /* attempt to continue as it is not a catastrophic failure */
1691 /* check for a sensible target voltage, operating range is 1.65-5.5v
1692 * according to datasheet */
1693 if (target_voltage
< 1.5)
1694 LOG_ERROR("target voltage may be too low for reliable debugging");
1698 LOG_DEBUG("MODE: 0x%02X", mode
);
1700 /* set selected mode */
1703 if (emode
== STLINK_MODE_UNKNOWN
) {
1704 LOG_ERROR("selected mode (transport) not supported");
1708 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1709 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1710 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1711 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1712 stlink_speed(h
, initial_interface_speed
, false);
1714 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1715 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1716 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1717 stlink_speed(h
, initial_interface_speed
, false);
1721 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
&&
1722 (emode
== STLINK_MODE_DEBUG_JTAG
|| emode
== STLINK_MODE_DEBUG_SWD
)) {
1723 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1725 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1726 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1727 stlink_speed(h
, initial_interface_speed
, false);
1730 /* preliminary SRST assert:
1731 * We want SRST is asserted before activating debug signals (mode_enter).
1732 * As the required mode has not been set, the adapter may not know what pin to use.
1733 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1734 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1735 * after power on, SWIM_RST stays unchanged */
1736 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1737 stlink_usb_assert_srst(handle
, 0);
1738 /* do not check the return status here, we will
1739 proceed and enter the desired mode below
1740 and try asserting srst again. */
1742 res
= stlink_usb_mode_enter(handle
, emode
);
1743 if (res
!= ERROR_OK
)
1746 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1747 if (connect_under_reset
) {
1748 res
= stlink_usb_assert_srst(handle
, 0);
1749 if (res
!= ERROR_OK
)
1753 res
= stlink_usb_current_mode(handle
, &mode
);
1755 if (res
!= ERROR_OK
)
1758 LOG_DEBUG("MODE: 0x%02X", mode
);
1763 /* request status from last swim request */
1764 static int stlink_swim_status(void *handle
)
1766 struct stlink_usb_handle_s
*h
= handle
;
1769 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1770 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1771 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1772 /* error is checked by the caller */
1773 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1774 if (res
!= ERROR_OK
)
1779 the purpose of this function is unknown...
1780 capabilities? anyway for swim v6 it returns
1783 __attribute__((unused
))
1784 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1786 struct stlink_usb_handle_s
*h
= handle
;
1789 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1790 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1791 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1792 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1793 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1794 if (res
!= ERROR_OK
)
1796 memcpy(cap
, h
->databuf
, 8);
1800 /* debug dongle assert/deassert sreset line */
1801 static int stlink_swim_assert_reset(void *handle
, int reset
)
1803 struct stlink_usb_handle_s
*h
= handle
;
1806 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1807 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1809 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1811 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1812 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1813 if (res
!= ERROR_OK
)
1820 1.3ms low then 750Hz then 1.5kHz
1822 static int stlink_swim_enter(void *handle
)
1824 struct stlink_usb_handle_s
*h
= handle
;
1827 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1828 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1830 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1831 if (res
!= ERROR_OK
)
1836 /* switch high/low speed swim */
1837 static int stlink_swim_speed(void *handle
, int speed
)
1839 struct stlink_usb_handle_s
*h
= handle
;
1842 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1843 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1844 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1846 h
->cmdbuf
[h
->cmdidx
++] = 1;
1848 h
->cmdbuf
[h
->cmdidx
++] = 0;
1849 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1850 if (res
!= ERROR_OK
)
1856 initiate srst from swim.
1857 nrst is pulled low for 50us.
1859 static int stlink_swim_generate_rst(void *handle
)
1861 struct stlink_usb_handle_s
*h
= handle
;
1864 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1865 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1866 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1867 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1868 if (res
!= ERROR_OK
)
1874 send resynchronize sequence
1875 swim is pulled low for 16us
1876 reply is 64 clks low
1878 static int stlink_swim_resync(void *handle
)
1880 struct stlink_usb_handle_s
*h
= handle
;
1883 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1884 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1885 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1886 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1887 if (res
!= ERROR_OK
)
1892 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1894 struct stlink_usb_handle_s
*h
= handle
;
1897 unsigned int datalen
= 0;
1898 int cmdsize
= STLINK_CMD_SIZE_V2
;
1900 if (len
> STLINK_SWIM_DATA_SIZE
)
1903 if (h
->version
.stlink
== 1)
1904 cmdsize
= STLINK_SG_SIZE
;
1906 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1907 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1908 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1909 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1911 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1913 for (i
= 0; i
< len
; i
++) {
1914 if (h
->cmdidx
== cmdsize
)
1915 h
->databuf
[datalen
++] = *(data
++);
1917 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1919 if (h
->version
.stlink
== 1)
1920 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1922 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1923 if (res
!= ERROR_OK
)
1928 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1930 struct stlink_usb_handle_s
*h
= handle
;
1933 if (len
> STLINK_SWIM_DATA_SIZE
)
1936 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1937 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1938 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1939 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1941 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1943 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1944 if (res
!= ERROR_OK
)
1947 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1948 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1949 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1950 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1951 if (res
!= ERROR_OK
)
1958 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1961 struct stlink_usb_handle_s
*h
= handle
;
1965 /* there is no swim read core id cmd */
1966 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1971 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1973 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1974 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1975 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1977 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1980 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1982 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1986 if (res
!= ERROR_OK
)
1989 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1991 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1996 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1998 struct stlink_usb_handle_s
*h
= handle
;
2003 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
2005 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2006 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
2007 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2010 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2011 if (res
!= ERROR_OK
)
2014 *val
= le_to_h_u32(h
->databuf
+ 4);
2018 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
2020 struct stlink_usb_handle_s
*h
= handle
;
2024 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2026 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2027 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2028 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
2030 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
2031 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2033 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2036 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2040 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
2042 struct stlink_usb_handle_s
*h
= handle
;
2046 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
2049 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2051 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2052 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
2054 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2055 if (res
!= ERROR_OK
)
2058 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
2059 *size
= bytes_avail
< *size
? bytes_avail
: *size
;
2062 res
= stlink_usb_read_trace(handle
, buf
, *size
);
2063 if (res
!= ERROR_OK
)
2072 static enum target_state
stlink_usb_v2_get_status(void *handle
)
2077 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
2078 if (result
!= ERROR_OK
)
2079 return TARGET_UNKNOWN
;
2081 if (status
& S_HALT
)
2082 return TARGET_HALTED
;
2083 else if (status
& S_RESET_ST
)
2084 return TARGET_RESET
;
2086 return TARGET_RUNNING
;
2090 static enum target_state
stlink_usb_state(void *handle
)
2093 struct stlink_usb_handle_s
*h
= handle
;
2097 if (h
->reconnect_pending
) {
2098 LOG_INFO("Previous state query failed, trying to reconnect");
2099 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
2100 if (res
!= ERROR_OK
)
2101 return TARGET_UNKNOWN
;
2103 h
->reconnect_pending
= false;
2106 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2107 res
= stlink_usb_v2_get_status(handle
);
2108 if (res
== TARGET_UNKNOWN
)
2109 h
->reconnect_pending
= true;
2113 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2115 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2116 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
2118 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2120 if (res
!= ERROR_OK
)
2121 return TARGET_UNKNOWN
;
2123 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
2124 return TARGET_RUNNING
;
2125 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
2126 return TARGET_HALTED
;
2128 h
->reconnect_pending
= true;
2130 return TARGET_UNKNOWN
;
2133 static int stlink_usb_assert_srst(void *handle
, int srst
)
2135 struct stlink_usb_handle_s
*h
= handle
;
2139 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
2140 return stlink_swim_assert_reset(handle
, srst
);
2142 if (h
->version
.stlink
== 1)
2143 return ERROR_COMMAND_NOTFOUND
;
2145 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2147 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2148 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
2149 h
->cmdbuf
[h
->cmdidx
++] = srst
;
2151 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2155 static void stlink_usb_trace_disable(void *handle
)
2158 struct stlink_usb_handle_s
*h
= handle
;
2162 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
2164 LOG_DEBUG("Tracing: disable");
2166 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2167 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2168 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
2169 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2171 if (res
== ERROR_OK
)
2172 h
->trace
.enabled
= false;
2177 static int stlink_usb_trace_enable(void *handle
)
2180 struct stlink_usb_handle_s
*h
= handle
;
2184 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
2185 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2187 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2188 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
2189 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
2191 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
2194 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2196 if (res
== ERROR_OK
) {
2197 h
->trace
.enabled
= true;
2198 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
2201 LOG_ERROR("Tracing is not supported by this version.");
2209 static int stlink_usb_reset(void *handle
)
2211 struct stlink_usb_handle_s
*h
= handle
;
2216 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2218 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2220 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2221 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
2223 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
2225 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2226 if (retval
!= ERROR_OK
)
2229 if (h
->trace
.enabled
) {
2230 stlink_usb_trace_disable(h
);
2231 return stlink_usb_trace_enable(h
);
2238 static int stlink_usb_run(void *handle
)
2241 struct stlink_usb_handle_s
*h
= handle
;
2245 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2246 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
2251 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2253 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2254 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
2256 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2260 static int stlink_usb_halt(void *handle
)
2263 struct stlink_usb_handle_s
*h
= handle
;
2267 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2268 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2273 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2275 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2276 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
2278 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2282 static int stlink_usb_step(void *handle
)
2284 struct stlink_usb_handle_s
*h
= handle
;
2288 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2289 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
2290 * that the Cortex-M3 currently does. */
2291 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
2292 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
2293 return 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_STEPCORE
;
2301 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2305 static int stlink_usb_read_regs(void *handle
)
2308 struct stlink_usb_handle_s
*h
= handle
;
2312 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
2314 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2315 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2317 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
2318 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
2319 /* regs data from offset 0 */
2321 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
2322 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
2323 /* status at offset 0, regs data from offset 4 */
2330 static int stlink_usb_read_reg(void *handle
, unsigned int regsel
, uint32_t *val
)
2333 struct stlink_usb_handle_s
*h
= handle
;
2337 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2338 res
= stlink_usb_write_debug_reg(h
, DCB_DCRSR
, regsel
& 0x7f);
2339 if (res
!= ERROR_OK
)
2342 /* FIXME: poll DHCSR.S_REGRDY before read DCRDR */
2343 return stlink_usb_v2_read_debug_reg(h
, DCB_DCRDR
, val
);
2346 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2348 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2349 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2350 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2352 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2353 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2355 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2356 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2357 if (res
!= ERROR_OK
)
2359 *val
= le_to_h_u32(h
->databuf
);
2362 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2363 if (res
!= ERROR_OK
)
2365 *val
= le_to_h_u32(h
->databuf
+ 4);
2371 static int stlink_usb_write_reg(void *handle
, unsigned int regsel
, uint32_t val
)
2373 struct stlink_usb_handle_s
*h
= handle
;
2377 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2378 int res
= stlink_usb_write_debug_reg(h
, DCB_DCRDR
, val
);
2379 if (res
!= ERROR_OK
)
2382 return stlink_usb_write_debug_reg(h
, DCB_DCRSR
, DCRSR_WNR
| (regsel
& 0x7f));
2383 /* FIXME: poll DHCSR.S_REGRDY after write DCRSR */
2386 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2388 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2389 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2390 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2392 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2393 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2394 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2397 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2400 static int stlink_usb_get_rw_status(void *handle
)
2402 struct stlink_usb_handle_s
*h
= handle
;
2406 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2409 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2411 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2412 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2413 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2414 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2416 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2417 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2422 static int stlink_usb_read_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2423 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2426 uint16_t read_len
= len
;
2427 struct stlink_usb_handle_s
*h
= handle
;
2431 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2432 return ERROR_COMMAND_NOTFOUND
;
2434 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2435 if (len
> stlink_usb_block(h
)) {
2436 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2440 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2442 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2443 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2444 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2446 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2448 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2449 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2452 /* we need to fix read length for single bytes */
2456 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2458 if (res
!= ERROR_OK
)
2461 memcpy(buffer
, h
->databuf
, len
);
2463 return stlink_usb_get_rw_status(handle
);
2467 static int stlink_usb_write_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2468 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2471 struct stlink_usb_handle_s
*h
= handle
;
2475 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2476 return ERROR_COMMAND_NOTFOUND
;
2478 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2479 if (len
> stlink_usb_block(h
)) {
2480 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2484 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2487 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2488 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2490 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2492 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2493 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2496 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2498 if (res
!= ERROR_OK
)
2501 return stlink_usb_get_rw_status(handle
);
2505 static int stlink_usb_read_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2506 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2509 struct stlink_usb_handle_s
*h
= handle
;
2513 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2514 return ERROR_COMMAND_NOTFOUND
;
2516 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2517 return ERROR_COMMAND_NOTFOUND
;
2519 if (len
> STLINK_MAX_RW16_32
) {
2520 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2524 /* data must be a multiple of 2 and half-word aligned */
2525 if (len
% 2 || addr
% 2) {
2526 LOG_DEBUG("Invalid data alignment");
2527 return ERROR_TARGET_UNALIGNED_ACCESS
;
2530 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2532 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2533 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2534 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2536 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2538 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2539 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2542 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2544 if (res
!= ERROR_OK
)
2547 memcpy(buffer
, h
->databuf
, len
);
2549 return stlink_usb_get_rw_status(handle
);
2553 static int stlink_usb_write_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2554 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2557 struct stlink_usb_handle_s
*h
= handle
;
2561 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2562 return ERROR_COMMAND_NOTFOUND
;
2564 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2565 return ERROR_COMMAND_NOTFOUND
;
2567 if (len
> STLINK_MAX_RW16_32
) {
2568 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2572 /* data must be a multiple of 2 and half-word aligned */
2573 if (len
% 2 || addr
% 2) {
2574 LOG_DEBUG("Invalid data alignment");
2575 return ERROR_TARGET_UNALIGNED_ACCESS
;
2578 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2580 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2581 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2582 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2584 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2586 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2587 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2590 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2592 if (res
!= ERROR_OK
)
2595 return stlink_usb_get_rw_status(handle
);
2599 static int stlink_usb_read_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2600 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2603 struct stlink_usb_handle_s
*h
= handle
;
2607 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2608 return ERROR_COMMAND_NOTFOUND
;
2610 if (len
> STLINK_MAX_RW16_32
) {
2611 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2615 /* data must be a multiple of 4 and word aligned */
2616 if (len
% 4 || addr
% 4) {
2617 LOG_DEBUG("Invalid data alignment");
2618 return ERROR_TARGET_UNALIGNED_ACCESS
;
2621 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2623 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2625 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2627 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2629 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2630 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2633 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2635 if (res
!= ERROR_OK
)
2638 memcpy(buffer
, h
->databuf
, len
);
2640 return stlink_usb_get_rw_status(handle
);
2644 static int stlink_usb_write_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2645 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2648 struct stlink_usb_handle_s
*h
= handle
;
2652 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2653 return ERROR_COMMAND_NOTFOUND
;
2655 if (len
> STLINK_MAX_RW16_32
) {
2656 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2660 /* data must be a multiple of 4 and word aligned */
2661 if (len
% 4 || addr
% 4) {
2662 LOG_DEBUG("Invalid data alignment");
2663 return ERROR_TARGET_UNALIGNED_ACCESS
;
2666 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2668 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2669 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2670 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2672 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2674 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2675 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2678 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2680 if (res
!= ERROR_OK
)
2683 return stlink_usb_get_rw_status(handle
);
2686 static int stlink_usb_read_mem32_noaddrinc(void *handle
, uint8_t ap_num
, uint32_t csw
,
2687 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2689 struct stlink_usb_handle_s
*h
= handle
;
2691 assert(handle
!= NULL
);
2693 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_RD_NO_INC
))
2694 return ERROR_COMMAND_NOTFOUND
;
2696 if (len
> STLINK_MAX_RW16_32
) {
2697 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2701 /* data must be a multiple of 4 and word aligned */
2702 if (len
% 4 || addr
% 4) {
2703 LOG_DEBUG("Invalid data alignment");
2704 return ERROR_TARGET_UNALIGNED_ACCESS
;
2707 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2709 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2710 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC
;
2711 h_u32_to_le(h
->cmdbuf
+ h
->cmdidx
, addr
);
2713 h_u16_to_le(h
->cmdbuf
+ h
->cmdidx
, len
);
2715 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2716 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2719 int retval
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2720 if (retval
!= ERROR_OK
)
2723 memcpy(buffer
, h
->databuf
, len
);
2725 return stlink_usb_get_rw_status(handle
);
2728 static int stlink_usb_write_mem32_noaddrinc(void *handle
, uint8_t ap_num
, uint32_t csw
,
2729 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2731 struct stlink_usb_handle_s
*h
= handle
;
2733 assert(handle
!= NULL
);
2735 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_WR_NO_INC
))
2736 return ERROR_COMMAND_NOTFOUND
;
2738 if (len
> STLINK_MAX_RW16_32
) {
2739 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2743 /* data must be a multiple of 4 and word aligned */
2744 if (len
% 4 || addr
% 4) {
2745 LOG_DEBUG("Invalid data alignment");
2746 return ERROR_TARGET_UNALIGNED_ACCESS
;
2749 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2751 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2752 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC
;
2753 h_u32_to_le(h
->cmdbuf
+ h
->cmdidx
, addr
);
2755 h_u16_to_le(h
->cmdbuf
+ h
->cmdidx
, len
);
2757 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2758 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2761 int retval
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2762 if (retval
!= ERROR_OK
)
2765 return stlink_usb_get_rw_status(handle
);
2768 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2770 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2771 if (max_tar_block
== 0)
2773 return max_tar_block
;
2776 static int stlink_usb_read_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2777 uint32_t addr
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2779 int retval
= ERROR_OK
;
2780 uint32_t bytes_remaining
;
2782 struct stlink_usb_handle_s
*h
= handle
;
2784 /* calculate byte count */
2787 /* switch to 8 bit if stlink does not support 16 bit memory read */
2788 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2792 bytes_remaining
= (size
!= 1) ?
2793 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2795 if (count
< bytes_remaining
)
2796 bytes_remaining
= count
;
2799 * all stlink support 8/32bit memory read/writes and only from
2800 * stlink V2J26 there is support for 16 bit memory read/write.
2801 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2805 /* When in jtag mode the stlink uses the auto-increment functionality.
2806 * However it expects us to pass the data correctly, this includes
2807 * alignment and any page boundaries. We already do this as part of the
2808 * adi_v5 implementation, but the stlink is a hla adapter and so this
2809 * needs implementing manually.
2810 * currently this only affects jtag mode, according to ST they do single
2811 * access in SWD mode - but this may change and so we do it for both modes */
2813 /* we first need to check for any unaligned bytes */
2814 if (addr
& (size
- 1)) {
2815 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2816 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2817 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2818 usleep((1 << retries
++) * 1000);
2821 if (retval
!= ERROR_OK
)
2823 buffer
+= head_bytes
;
2825 count
-= head_bytes
;
2826 bytes_remaining
-= head_bytes
;
2829 if (bytes_remaining
& (size
- 1))
2830 retval
= stlink_usb_read_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2832 retval
= stlink_usb_read_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2834 retval
= stlink_usb_read_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2836 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2839 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2840 usleep((1 << retries
++) * 1000);
2843 if (retval
!= ERROR_OK
)
2846 buffer
+= bytes_remaining
;
2847 addr
+= bytes_remaining
;
2848 count
-= bytes_remaining
;
2854 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2855 uint32_t count
, uint8_t *buffer
)
2857 return stlink_usb_read_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2858 addr
, size
, count
, buffer
);
2861 static int stlink_usb_write_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2862 uint32_t addr
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2864 int retval
= ERROR_OK
;
2865 uint32_t bytes_remaining
;
2867 struct stlink_usb_handle_s
*h
= handle
;
2869 /* calculate byte count */
2872 /* switch to 8 bit if stlink does not support 16 bit memory read */
2873 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2878 bytes_remaining
= (size
!= 1) ?
2879 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2881 if (count
< bytes_remaining
)
2882 bytes_remaining
= count
;
2885 * all stlink support 8/32bit memory read/writes and only from
2886 * stlink V2J26 there is support for 16 bit memory read/write.
2887 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2892 /* When in jtag mode the stlink uses the auto-increment functionality.
2893 * However it expects us to pass the data correctly, this includes
2894 * alignment and any page boundaries. We already do this as part of the
2895 * adi_v5 implementation, but the stlink is a hla adapter and so this
2896 * needs implementing manually.
2897 * currently this only affects jtag mode, according to ST they do single
2898 * access in SWD mode - but this may change and so we do it for both modes */
2900 /* we first need to check for any unaligned bytes */
2901 if (addr
& (size
- 1)) {
2903 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2904 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2905 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2906 usleep((1<<retries
++) * 1000);
2909 if (retval
!= ERROR_OK
)
2911 buffer
+= head_bytes
;
2913 count
-= head_bytes
;
2914 bytes_remaining
-= head_bytes
;
2917 if (bytes_remaining
& (size
- 1))
2918 retval
= stlink_usb_write_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2920 retval
= stlink_usb_write_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2922 retval
= stlink_usb_write_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2925 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2926 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2927 usleep((1<<retries
++) * 1000);
2930 if (retval
!= ERROR_OK
)
2933 buffer
+= bytes_remaining
;
2934 addr
+= bytes_remaining
;
2935 count
-= bytes_remaining
;
2941 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2942 uint32_t count
, const uint8_t *buffer
)
2944 return stlink_usb_write_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2945 addr
, size
, count
, buffer
);
2949 static int stlink_usb_override_target(const char *targetname
)
2951 return !strcmp(targetname
, "cortex_m");
2954 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2959 we only have low and high speed...
2960 before changing speed the SWIM_CSR HS bit
2964 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2965 if (retval
!= ERROR_OK
)
2966 LOG_ERROR("Unable to set adapter speed");
2969 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2972 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2975 int speed_index
= -1;
2976 int speed_diff
= INT_MAX
;
2977 int last_valid_speed
= -1;
2980 for (i
= 0; i
< map_size
; i
++) {
2983 last_valid_speed
= i
;
2984 if (khz
== map
[i
].speed
) {
2988 int current_diff
= khz
- map
[i
].speed
;
2989 /* get abs value for comparison */
2990 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2991 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2992 speed_diff
= current_diff
;
2998 if (speed_index
== -1) {
2999 /* this will only be here if we cannot match the slow speed.
3000 * use the slowest speed we support.*/
3001 speed_index
= last_valid_speed
;
3003 } else if (i
== map_size
)
3006 if (!match
&& query
) {
3007 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
3008 khz
, map
[speed_index
].speed
);
3014 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
3017 struct stlink_usb_handle_s
*h
= handle
;
3019 /* old firmware cannot change it */
3020 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
3023 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
3024 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
3027 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
3028 if (result
!= ERROR_OK
) {
3029 LOG_ERROR("Unable to set adapter speed");
3034 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
3037 static int stlink_speed_jtag(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_JTAG_SET_FREQ
))
3046 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
3047 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
3050 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
3051 if (result
!= ERROR_OK
) {
3052 LOG_ERROR("Unable to set adapter speed");
3057 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
3060 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
3064 LOG_DEBUG("Supported clock speeds are:");
3065 for (i
= 0; i
< map_size
; i
++)
3067 LOG_DEBUG("%d kHz", map
[i
].speed
);
3070 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
3072 struct stlink_usb_handle_s
*h
= handle
;
3075 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3076 LOG_ERROR("Unknown command");
3080 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3082 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3083 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
3084 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3086 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
3088 int size
= h
->databuf
[8];
3090 if (size
> STLINK_V3_MAX_FREQ_NB
)
3091 size
= STLINK_V3_MAX_FREQ_NB
;
3093 for (i
= 0; i
< size
; i
++) {
3094 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
3095 map
[i
].speed_divisor
= i
;
3098 /* set to zero all the next entries */
3099 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
3105 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
3107 struct stlink_usb_handle_s
*h
= handle
;
3109 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3110 LOG_ERROR("Unknown command");
3114 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3116 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3117 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
3118 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3119 h
->cmdbuf
[h
->cmdidx
++] = 0;
3121 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
3123 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3126 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
3128 struct stlink_usb_handle_s
*h
= handle
;
3130 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
3132 stlink_get_com_freq(h
, is_jtag
, map
);
3134 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
3137 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
3138 if (result
!= ERROR_OK
) {
3139 LOG_ERROR("Unable to set adapter speed");
3143 return map
[speed_index
].speed
;
3146 static int stlink_speed(void *handle
, int khz
, bool query
)
3148 struct stlink_usb_handle_s
*h
= handle
;
3153 switch (h
->st_mode
) {
3154 case STLINK_MODE_DEBUG_SWIM
:
3155 return stlink_speed_swim(handle
, khz
, query
);
3156 case STLINK_MODE_DEBUG_SWD
:
3157 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
3158 return stlink_speed_v3(handle
, false, khz
, query
);
3160 return stlink_speed_swd(handle
, khz
, query
);
3162 case STLINK_MODE_DEBUG_JTAG
:
3163 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
3164 return stlink_speed_v3(handle
, true, khz
, query
);
3166 return stlink_speed_jtag(handle
, khz
, query
);
3176 static int stlink_usb_usb_close(void *handle
)
3178 struct stlink_usb_handle_s
*h
= handle
;
3183 if (h
->usb_backend_priv
.fd
) {
3184 stlink_usb_exit_mode(h
);
3185 /* do not check return code, it prevent
3186 us from closing jtag_libusb */
3187 jtag_libusb_close(h
->usb_backend_priv
.fd
);
3197 static int stlink_tcp_close(void *handle
)
3199 struct stlink_usb_handle_s
*h
= handle
;
3205 if (h
->tcp_backend_priv
.connected
) {
3206 if (h
->tcp_backend_priv
.connect_id
) {
3207 stlink_usb_exit_mode(h
);
3209 /* close the stlink */
3210 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_CLOSE_DEV
;
3211 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 4); /* reserved */
3212 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
3213 ret
= stlink_tcp_send_cmd(h
, 8, 4, true);
3214 if (ret
!= ERROR_OK
)
3215 LOG_ERROR("cannot close the STLINK");
3218 if (close_socket(h
->tcp_backend_priv
.fd
) != 0)
3219 LOG_ERROR("error closing the socket, errno: %s", strerror(errno
));
3222 free(h
->tcp_backend_priv
.send_buf
);
3223 free(h
->tcp_backend_priv
.recv_buf
);
3229 static int stlink_close(void *handle
)
3232 struct stlink_usb_handle_s
*h
= handle
;
3234 stlink_usb_close(handle
);
3242 /* Compute ST-Link serial number from the device descriptor
3243 * this function will help to work-around a bug in old ST-Link/V2 DFU
3244 * the buggy DFU returns an incorrect serial in the USB descriptor
3245 * example for the following serial "57FF72067265575742132067"
3246 * - the correct descriptor serial is:
3247 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
3248 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
3249 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
3250 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
3251 * so this case is managed by libusb_helper::string_descriptor_equal
3252 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
3253 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
3255 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
3256 * and then we have just to convert the raw data into printable characters using sprintf
3258 static char *stlink_usb_get_alternate_serial(struct libusb_device_handle
*device
,
3259 struct libusb_device_descriptor
*dev_desc
)
3262 unsigned char desc_serial
[(STLINK_SERIAL_LEN
+ 1) * 2];
3264 if (dev_desc
->iSerialNumber
== 0)
3267 /* get the LANGID from String Descriptor Zero */
3268 usb_retval
= libusb_get_string_descriptor(device
, 0, 0, desc_serial
,
3269 sizeof(desc_serial
));
3271 if (usb_retval
< LIBUSB_SUCCESS
) {
3272 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
3273 libusb_error_name(usb_retval
), usb_retval
);
3275 } else if (usb_retval
< 4) {
3276 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
3277 LOG_ERROR("could not get the LANGID");
3281 uint32_t langid
= desc_serial
[2] | (desc_serial
[3] << 8);
3283 /* get the serial */
3284 usb_retval
= libusb_get_string_descriptor(device
, dev_desc
->iSerialNumber
,
3285 langid
, desc_serial
, sizeof(desc_serial
));
3287 unsigned char len
= desc_serial
[0];
3289 if (usb_retval
< LIBUSB_SUCCESS
) {
3290 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
3291 libusb_error_name(usb_retval
), usb_retval
);
3293 } else if (desc_serial
[1] != LIBUSB_DT_STRING
|| len
> usb_retval
) {
3294 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
3298 if (len
== ((STLINK_SERIAL_LEN
+ 1) * 2)) {
3299 /* good ST-Link adapter, this case is managed by
3300 * libusb::libusb_get_string_descriptor_ascii */
3302 } else if (len
!= ((STLINK_SERIAL_LEN
/ 2 + 1) * 2)) {
3303 LOG_ERROR("unexpected serial length (%d) in descriptor", len
);
3307 /* else (len == 26) => buggy ST-Link */
3309 char *alternate_serial
= malloc((STLINK_SERIAL_LEN
+ 1) * sizeof(char));
3310 if (!alternate_serial
)
3313 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
3314 sprintf(alternate_serial
+ i
, "%02X", desc_serial
[i
+ 2]);
3316 alternate_serial
[STLINK_SERIAL_LEN
] = '\0';
3318 return alternate_serial
;
3322 static int stlink_usb_usb_open(void *handle
, struct hl_interface_param_s
*param
)
3324 struct stlink_usb_handle_s
*h
= handle
;
3325 int err
, retry_count
= 1;
3327 h
->cmdbuf
= malloc(STLINK_SG_SIZE
);
3328 h
->databuf
= malloc(STLINK_DATA_SIZE
);
3330 if (!h
->cmdbuf
|| !h
->databuf
)
3334 On certain host USB configurations(e.g. MacBook Air)
3335 STLINKv2 dongle seems to have its FW in a funky state if,
3336 after plugging it in, you try to use openocd with it more
3337 then once (by launching and closing openocd). In cases like
3338 that initial attempt to read the FW info via
3339 stlink_usb_version will fail and the device has to be reset
3340 in order to become operational.
3343 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
,
3344 &h
->usb_backend_priv
.fd
, stlink_usb_get_alternate_serial
) != ERROR_OK
) {
3345 LOG_ERROR("open failed");
3349 jtag_libusb_set_configuration(h
->usb_backend_priv
.fd
, 0);
3351 if (libusb_claim_interface(h
->usb_backend_priv
.fd
, 0) != ERROR_OK
) {
3352 LOG_DEBUG("claim interface failed");
3356 /* RX EP is common for all versions */
3357 h
->rx_ep
= STLINK_RX_EP
;
3360 if (jtag_libusb_get_pid(libusb_get_device(h
->usb_backend_priv
.fd
), &pid
) != ERROR_OK
) {
3361 LOG_DEBUG("libusb_get_pid failed");
3365 /* wrap version for first read */
3368 h
->version
.stlink
= 1;
3369 h
->tx_ep
= STLINK_TX_EP
;
3371 case STLINK_V3_USBLOADER_PID
:
3372 case STLINK_V3E_PID
:
3373 case STLINK_V3S_PID
:
3374 case STLINK_V3_2VCP_PID
:
3375 case STLINK_V3E_NO_MSD_PID
:
3376 h
->version
.stlink
= 3;
3377 h
->tx_ep
= STLINK_V2_1_TX_EP
;
3378 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
3380 case STLINK_V2_1_PID
:
3381 case STLINK_V2_1_NO_MSD_PID
:
3382 h
->version
.stlink
= 2;
3383 h
->tx_ep
= STLINK_V2_1_TX_EP
;
3384 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
3387 /* fall through - we assume V2 to be the default version*/
3389 h
->version
.stlink
= 2;
3390 h
->tx_ep
= STLINK_TX_EP
;
3391 h
->trace_ep
= STLINK_TRACE_EP
;
3395 /* get the device version */
3396 err
= stlink_usb_version(h
);
3398 if (err
== ERROR_OK
) {
3400 } else if (h
->version
.stlink
== 1 ||
3402 LOG_ERROR("read version failed");
3405 err
= libusb_release_interface(h
->usb_backend_priv
.fd
, 0);
3406 if (err
!= ERROR_OK
) {
3407 LOG_ERROR("release interface failed");
3411 err
= libusb_reset_device(h
->usb_backend_priv
.fd
);
3412 if (err
!= ERROR_OK
) {
3413 LOG_ERROR("reset device failed");
3417 jtag_libusb_close(h
->usb_backend_priv
.fd
);
3419 Give the device one second to settle down and
3422 usleep(1 * 1000 * 1000);
3431 static int stlink_tcp_open(void *handle
, struct hl_interface_param_s
*param
)
3433 struct stlink_usb_handle_s
*h
= handle
;
3436 /* SWIM is not supported using stlink-server */
3437 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
3438 LOG_ERROR("stlink-server does not support SWIM mode");
3442 h
->tcp_backend_priv
.send_buf
= malloc(STLINK_TCP_SEND_BUFFER_SIZE
);
3443 h
->tcp_backend_priv
.recv_buf
= malloc(STLINK_TCP_RECV_BUFFER_SIZE
);
3445 if (!h
->tcp_backend_priv
.send_buf
|| !h
->tcp_backend_priv
.recv_buf
)
3448 h
->cmdbuf
= &h
->tcp_backend_priv
.send_buf
[8];
3449 h
->databuf
= &h
->tcp_backend_priv
.recv_buf
[4];
3451 /* configure directions */
3452 h
->rx_ep
= STLINK_TCP_REQUEST_READ
;
3453 h
->tx_ep
= STLINK_TCP_REQUEST_WRITE
;
3454 h
->trace_ep
= STLINK_TCP_REQUEST_READ_SWO
;
3456 h
->tcp_backend_priv
.fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
3457 h
->tcp_backend_priv
.connected
= false;
3458 h
->tcp_backend_priv
.device_id
= 0;
3459 h
->tcp_backend_priv
.connect_id
= 0;
3461 if (h
->tcp_backend_priv
.fd
== -1) {
3462 LOG_ERROR("error creating the socket, errno: %s", strerror(errno
));
3466 struct sockaddr_in serv
;
3467 memset(&serv
, 0, sizeof(struct sockaddr_in
));
3468 serv
.sin_family
= AF_INET
;
3469 serv
.sin_port
= htons(param
->stlink_tcp_port
);
3470 serv
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
3472 LOG_DEBUG("socket : %x", h
->tcp_backend_priv
.fd
);
3475 if (setsockopt(h
->tcp_backend_priv
.fd
, IPPROTO_TCP
, TCP_NODELAY
, (const void *)&optval
, sizeof(int)) == -1) {
3476 LOG_ERROR("cannot set sock option 'TCP_NODELAY', errno: %s", strerror(errno
));
3480 optval
= STLINK_TCP_RECV_BUFFER_SIZE
;
3481 if (setsockopt(h
->tcp_backend_priv
.fd
, SOL_SOCKET
, SO_RCVBUF
, (const void *)&optval
, sizeof(int)) == -1) {
3482 LOG_ERROR("cannot set sock option 'SO_RCVBUF', errno: %s", strerror(errno
));
3486 optval
= STLINK_TCP_SEND_BUFFER_SIZE
;
3487 if (setsockopt(h
->tcp_backend_priv
.fd
, SOL_SOCKET
, SO_SNDBUF
, (const void *)&optval
, sizeof(int)) == -1) {
3488 LOG_ERROR("cannot set sock option 'SO_SNDBUF', errno: %s", strerror(errno
));
3492 if (connect(h
->tcp_backend_priv
.fd
, (const struct sockaddr
*)&serv
, sizeof(serv
)) == -1) {
3493 LOG_ERROR("cannot connect to stlink server, errno: %s", strerror(errno
));
3497 h
->tcp_backend_priv
.connected
= true;
3499 LOG_INFO("connected to stlink-server");
3501 /* print stlink-server version */
3502 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_GET_SERVER_VERSION
;
3503 h
->tcp_backend_priv
.send_buf
[1] = OPENOCD_STLINK_TCP_API_VERSION
;
3504 memset(&h
->tcp_backend_priv
.send_buf
[2], 0, 2); /* reserved */
3505 ret
= stlink_tcp_send_cmd(h
, 4, 16, false);
3506 if (ret
!= ERROR_OK
) {
3507 LOG_ERROR("cannot get the stlink-server version");
3511 uint32_t api_ver
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[0]);
3512 uint32_t ver_major
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[4]);
3513 uint32_t ver_minor
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[8]);
3514 uint32_t ver_build
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[12]);
3515 LOG_INFO("stlink-server API v%d, version %d.%d.%d",
3516 api_ver
, ver_major
, ver_minor
, ver_build
);
3518 /* in stlink-server API v1 sending more than 1428 bytes will cause stlink-server
3519 * to crash in windows: select a safe default value (1K) */
3521 h
->max_mem_packet
= (1 << 10);
3523 /* refresh stlink list (re-enumerate) */
3524 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_REFRESH_DEVICE_LIST
;
3525 h
->tcp_backend_priv
.send_buf
[1] = 0; /* don't clear the list, just refresh it */
3526 ret
= stlink_tcp_send_cmd(h
, 2, 4, true);
3527 if (ret
!= ERROR_OK
)
3530 /* get the number of connected stlinks */
3531 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_GET_NB_DEV
;
3532 ret
= stlink_tcp_send_cmd(h
, 1, 4, false);
3533 if (ret
!= ERROR_OK
)
3536 uint32_t connected_stlinks
= le_to_h_u32(h
->tcp_backend_priv
.recv_buf
);
3538 if (connected_stlinks
== 0) {
3539 LOG_ERROR("no ST-LINK detected");
3543 LOG_DEBUG("%d ST-LINK detected", connected_stlinks
);
3545 if (connected_stlinks
> 255) {
3546 LOG_WARNING("STLink server cannot handle more than 255 ST-LINK connected");
3547 connected_stlinks
= 255;
3550 /* list all connected ST-Link and seek for the requested vid:pid and serial */
3551 char serial
[STLINK_TCP_SERIAL_SIZE
+ 1] = {0};
3552 uint8_t stlink_used
;
3553 bool stlink_id_matched
= false;
3554 bool stlink_serial_matched
= (!param
->serial
);
3556 for (uint32_t stlink_id
= 0; stlink_id
< connected_stlinks
; stlink_id
++) {
3557 /* get the stlink info */
3558 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_GET_DEV_INFO
;
3559 h
->tcp_backend_priv
.send_buf
[1] = (uint8_t)stlink_id
;
3560 memset(&h
->tcp_backend_priv
.send_buf
[2], 0, 2); /* reserved */
3561 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], 41); /* size of TDeviceInfo2 */
3562 ret
= stlink_tcp_send_cmd(h
, 8, 45, true);
3563 if (ret
!= ERROR_OK
)
3566 h
->tcp_backend_priv
.device_id
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[4]);
3567 memcpy(serial
, &h
->tcp_backend_priv
.recv_buf
[8], STLINK_TCP_SERIAL_SIZE
);
3568 h
->vid
= le_to_h_u16(&h
->tcp_backend_priv
.recv_buf
[40]);
3569 h
->pid
= le_to_h_u16(&h
->tcp_backend_priv
.recv_buf
[42]);
3570 stlink_used
= h
->tcp_backend_priv
.recv_buf
[44];
3572 /* check the vid:pid */
3573 for (int i
= 0; param
->vid
[i
]; i
++) {
3574 if (param
->vid
[i
] == h
->vid
&& param
->pid
[i
] == h
->pid
) {
3575 stlink_id_matched
= true;
3580 if (!stlink_id_matched
)
3583 /* check the serial if specified */
3584 if (param
->serial
) {
3585 /* ST-Link server fixes the buggy serial returned by old ST-Link DFU
3586 * for further details refer to stlink_usb_get_alternate_serial
3587 * so if the user passes the buggy serial, we need to fix it before
3588 * comparing with the serial returned by ST-Link server */
3589 if (strlen(param
->serial
) == STLINK_SERIAL_LEN
/ 2) {
3590 char fixed_serial
[STLINK_SERIAL_LEN
+ 1];
3592 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
3593 sprintf(fixed_serial
+ i
, "%02X", param
->serial
[i
/ 2]);
3595 fixed_serial
[STLINK_SERIAL_LEN
] = '\0';
3597 stlink_serial_matched
= strcmp(fixed_serial
, serial
) == 0;
3599 stlink_serial_matched
= strcmp(param
->serial
, serial
) == 0;
3602 if (!stlink_serial_matched
)
3603 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
3604 serial
, param
->serial
);
3605 else /* exit the search loop if there is match */
3609 if (!stlink_id_matched
) {
3610 LOG_ERROR("ST-LINK open failed (vid/pid mismatch)");
3614 if (!stlink_serial_matched
) {
3615 LOG_ERROR("ST-LINK open failed (serial mismatch)");
3619 /* check if device is 'exclusively' used by another application */
3621 LOG_ERROR("the selected device is already used");
3625 LOG_DEBUG("transport: vid: 0x%04x pid: 0x%04x serial: %s", h
->vid
, h
->pid
, serial
);
3627 /* now let's open the stlink */
3628 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_OPEN_DEV
;
3629 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 4); /* reserved */
3630 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.device_id
);
3631 ret
= stlink_tcp_send_cmd(h
, 8, 8, true);
3632 if (ret
!= ERROR_OK
)
3635 h
->tcp_backend_priv
.connect_id
= le_to_h_u32(&h
->tcp_backend_priv
.recv_buf
[4]);
3637 /* get stlink version */
3638 return stlink_usb_version(h
);
3641 static struct stlink_backend_s stlink_usb_backend
= {
3642 .open
= stlink_usb_usb_open
,
3643 .close
= stlink_usb_usb_close
,
3644 .xfer_noerrcheck
= stlink_usb_usb_xfer_noerrcheck
,
3645 .read_trace
= stlink_usb_usb_read_trace
,
3648 static struct stlink_backend_s stlink_tcp_backend
= {
3649 .open
= stlink_tcp_open
,
3650 .close
= stlink_tcp_close
,
3651 .xfer_noerrcheck
= stlink_tcp_xfer_noerrcheck
,
3652 .read_trace
= stlink_tcp_read_trace
,
3655 static int stlink_open(struct hl_interface_param_s
*param
, enum stlink_mode mode
, void **fd
)
3657 struct stlink_usb_handle_s
*h
;
3659 LOG_DEBUG("stlink_open");
3661 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
3664 LOG_DEBUG("malloc failed");
3670 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
3671 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
3672 h
->st_mode
, param
->vid
[i
], param
->pid
[i
],
3673 param
->serial
? param
->serial
: "");
3676 if (param
->use_stlink_tcp
)
3677 h
->backend
= &stlink_tcp_backend
;
3679 h
->backend
= &stlink_usb_backend
;
3681 if (stlink_usb_open(h
, param
) != ERROR_OK
)
3684 /* check if mode is supported */
3687 switch (h
->st_mode
) {
3688 case STLINK_MODE_DEBUG_SWD
:
3689 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
3692 case STLINK_MODE_DEBUG_JTAG
:
3693 if (h
->version
.jtag
== 0)
3696 case STLINK_MODE_DEBUG_SWIM
:
3697 if (h
->version
.swim
== 0)
3705 if (err
!= ERROR_OK
) {
3706 LOG_ERROR("mode (transport) not supported by device");
3710 /* initialize the debug hardware */
3711 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
3713 if (err
!= ERROR_OK
) {
3714 LOG_ERROR("init mode failed (unable to connect to the target)");
3718 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
3719 err
= stlink_swim_enter(h
);
3720 if (err
!= ERROR_OK
) {
3721 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
3725 h
->max_mem_packet
= STLINK_SWIM_DATA_SIZE
;
3729 /* set max_mem_packet if it was not set by the low-level interface */
3730 if (h
->max_mem_packet
== 0) {
3731 /* get cpuid, so we can determine the max page size
3732 * start with a safe default */
3733 h
->max_mem_packet
= (1 << 10);
3736 stlink_usb_open_ap(h
, STLINK_HLA_AP_NUM
);
3737 err
= stlink_usb_read_mem32(h
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
, CPUID
, 4, buffer
);
3738 if (err
== ERROR_OK
) {
3739 uint32_t cpuid
= le_to_h_u32(buffer
);
3740 int i
= (cpuid
>> 4) & 0xf;
3741 if (i
== 4 || i
== 3) {
3742 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
3743 h
->max_mem_packet
= (1 << 12);
3747 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
3759 static int stlink_usb_hl_open(struct hl_interface_param_s
*param
, void **fd
)
3761 return stlink_open(param
, stlink_get_mode(param
->transport
), fd
);
3764 static int stlink_config_trace(void *handle
, bool enabled
,
3765 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3766 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3767 uint16_t *prescaler
)
3769 struct stlink_usb_handle_s
*h
= handle
;
3771 if (!(h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
3772 LOG_ERROR("The attached ST-LINK version doesn't support trace");
3777 stlink_usb_trace_disable(h
);
3784 if (pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
) {
3785 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3789 unsigned int max_trace_freq
= (h
->version
.stlink
== 3) ?
3790 STLINK_V3_TRACE_MAX_HZ
: STLINK_TRACE_MAX_HZ
;
3792 /* Only concern ourselves with the frequency if the STlink is processing it. */
3793 if (*trace_freq
> max_trace_freq
) {
3794 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3800 *trace_freq
= max_trace_freq
;
3802 unsigned int presc
= (traceclkin_freq
+ *trace_freq
/ 2) / *trace_freq
;
3803 if (presc
== 0 || presc
> TPIU_ACPR_MAX_SWOSCALER
+ 1) {
3804 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3809 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
3810 unsigned int max_deviation
= (traceclkin_freq
* 3) / 100;
3811 if (presc
* *trace_freq
< traceclkin_freq
- max_deviation
||
3812 presc
* *trace_freq
> traceclkin_freq
+ max_deviation
) {
3813 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3820 stlink_usb_trace_disable(h
);
3822 h
->trace
.source_hz
= *trace_freq
;
3824 return stlink_usb_trace_enable(h
);
3828 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
3830 struct stlink_usb_handle_s
*h
= handle
;
3834 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3835 return ERROR_COMMAND_NOTFOUND
;
3837 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
3838 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3839 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3840 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
3841 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3843 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3847 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
3849 struct stlink_usb_handle_s
*h
= handle
;
3853 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3854 return ERROR_COMMAND_NOTFOUND
;
3856 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
3857 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3858 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3859 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG
;
3860 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3862 /* ignore incorrectly returned error on bogus FW */
3863 if (h
->version
.flags
& STLINK_F_FIX_CLOSE_AP
)
3864 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3866 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
3871 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3872 unsigned short addr
, uint32_t *val
)
3874 struct stlink_usb_handle_s
*h
= handle
;
3879 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3880 return ERROR_COMMAND_NOTFOUND
;
3882 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3883 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3884 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3885 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3886 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3888 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3889 *val
= le_to_h_u32(h
->databuf
+ 4);
3890 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%" PRIx32
, dap_port
, addr
, *val
);
3895 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3896 unsigned short addr
, uint32_t val
)
3898 struct stlink_usb_handle_s
*h
= handle
;
3902 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3903 return ERROR_COMMAND_NOTFOUND
;
3905 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%" PRIx32
, dap_port
, addr
, val
);
3906 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3907 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3908 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3909 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3910 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3911 h_u32_to_le(&h
->cmdbuf
[6], val
);
3912 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3916 struct hl_layout_api_s stlink_usb_layout_api
= {
3918 .open
= stlink_usb_hl_open
,
3920 .close
= stlink_close
,
3922 .idcode
= stlink_usb_idcode
,
3924 .state
= stlink_usb_state
,
3926 .reset
= stlink_usb_reset
,
3928 .assert_srst
= stlink_usb_assert_srst
,
3930 .run
= stlink_usb_run
,
3932 .halt
= stlink_usb_halt
,
3934 .step
= stlink_usb_step
,
3936 .read_regs
= stlink_usb_read_regs
,
3938 .read_reg
= stlink_usb_read_reg
,
3940 .write_reg
= stlink_usb_write_reg
,
3942 .read_mem
= stlink_usb_read_mem
,
3944 .write_mem
= stlink_usb_write_mem
,
3946 .write_debug_reg
= stlink_usb_write_debug_reg
,
3948 .override_target
= stlink_usb_override_target
,
3950 .speed
= stlink_speed
,
3952 .config_trace
= stlink_config_trace
,
3954 .poll_trace
= stlink_usb_trace_read
,
3957 /*****************************************************************************
3958 * DAP direct interface
3961 static struct stlink_usb_handle_s
*stlink_dap_handle
;
3962 static struct hl_interface_param_s stlink_dap_param
;
3963 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
3964 static uint32_t last_csw_default
[DP_APSEL_MAX
+ 1];
3965 static int stlink_dap_error
= ERROR_OK
;
3968 static int stlink_dap_record_error(int error
)
3970 if (stlink_dap_error
== ERROR_OK
)
3971 stlink_dap_error
= error
;
3976 static int stlink_dap_get_and_clear_error(void)
3978 int retval
= stlink_dap_error
;
3979 stlink_dap_error
= ERROR_OK
;
3983 static int stlink_dap_get_error(void)
3985 return stlink_dap_error
;
3988 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
)
3990 struct stlink_usb_handle_s
*h
= handle
;
3993 /* nothing to do on old versions */
3994 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3997 if (apsel
> DP_APSEL_MAX
)
4000 if (test_bit(apsel
, opened_ap
))
4003 retval
= stlink_usb_init_access_port(h
, apsel
);
4004 if (retval
!= ERROR_OK
)
4007 LOG_DEBUG("AP %d enabled", apsel
);
4008 set_bit(apsel
, opened_ap
);
4009 last_csw_default
[apsel
] = 0;
4013 static int stlink_dap_open_ap(unsigned short apsel
)
4015 return stlink_usb_open_ap(stlink_dap_handle
, apsel
);
4019 static int stlink_dap_closeall_ap(void)
4023 /* nothing to do on old versions */
4024 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
4027 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
4028 if (!test_bit(apsel
, opened_ap
))
4030 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
4031 if (retval
!= ERROR_OK
)
4033 clear_bit(apsel
, opened_ap
);
4039 static int stlink_dap_reinit_interface(void)
4044 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
4045 * some firmware version it does not work as expected, and there is no
4046 * equivalent for SWD.
4047 * At least for now, to reset the interface quit from JTAG/SWD mode then
4048 * select the mode again.
4051 if (!stlink_dap_handle
->reconnect_pending
) {
4052 stlink_dap_handle
->reconnect_pending
= true;
4053 stlink_usb_mode_leave(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
4056 retval
= stlink_usb_mode_enter(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
4057 if (retval
!= ERROR_OK
)
4060 stlink_dap_handle
->reconnect_pending
= false;
4061 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
4062 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
4063 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
4064 if (test_bit(apsel
, opened_ap
)) {
4065 clear_bit(apsel
, opened_ap
);
4066 stlink_dap_open_ap(apsel
);
4072 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
4077 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
4079 /* Check if we should reset srst already when connecting, but not if reconnecting. */
4080 if (!dap
->do_reconnect
) {
4081 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4083 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
4084 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
4085 adapter_assert_reset();
4087 LOG_WARNING("\'srst_nogate\' reset_config option is required");
4091 dap
->do_reconnect
= false;
4092 dap_invalidate_cache(dap
);
4093 for (unsigned int i
= 0; i
<= DP_APSEL_MAX
; i
++)
4094 last_csw_default
[i
] = 0;
4096 retval
= dap_dp_init(dap
);
4097 if (retval
!= ERROR_OK
) {
4098 dap
->do_reconnect
= true;
4102 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
4103 if (retval
== ERROR_OK
)
4104 LOG_INFO("%s %#8.8" PRIx32
,
4105 (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
4108 dap
->do_reconnect
= true;
4114 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
4118 if (!dap
->do_reconnect
)
4121 retval
= stlink_dap_reinit_interface();
4122 if (retval
!= ERROR_OK
)
4125 return stlink_dap_op_connect(dap
);
4129 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
4131 /* Ignore the request */
4136 static int stlink_dap_dp_read(struct adiv5_dap
*dap
, unsigned int reg
, uint32_t *data
)
4141 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
4142 if (reg
& 0x000000F0) {
4143 LOG_ERROR("Banked DP registers not supported in current STLink FW");
4144 return ERROR_COMMAND_NOTFOUND
;
4147 data
= data
? data
: &dummy
;
4148 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
4149 && stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) {
4150 /* Quirk required in JTAG. Read RDBUFF to get the data */
4151 retval
= stlink_read_dap_register(stlink_dap_handle
,
4152 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
4153 if (retval
== ERROR_OK
)
4154 retval
= stlink_read_dap_register(stlink_dap_handle
,
4155 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
4157 retval
= stlink_read_dap_register(stlink_dap_handle
,
4158 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
4165 static int stlink_dap_dp_write(struct adiv5_dap
*dap
, unsigned int reg
, uint32_t data
)
4169 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
4170 if (reg
& 0x000000F0) {
4171 LOG_ERROR("Banked DP registers not supported in current STLink FW");
4172 return ERROR_COMMAND_NOTFOUND
;
4175 if (reg
== DP_SELECT
&& (data
& DP_SELECT_DPBANK
) != 0) {
4176 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
4177 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
4178 data
&= ~DP_SELECT_DPBANK
;
4181 /* ST-Link does not like that we set CORUNDETECT */
4182 if (reg
== DP_CTRL_STAT
)
4183 data
&= ~CORUNDETECT
;
4185 retval
= stlink_write_dap_register(stlink_dap_handle
,
4186 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
4191 static int stlink_dap_ap_read(struct adiv5_ap
*ap
, unsigned int reg
, uint32_t *data
)
4193 struct adiv5_dap
*dap
= ap
->dap
;
4197 if (reg
!= AP_REG_IDR
) {
4198 retval
= stlink_dap_open_ap(ap
->ap_num
);
4199 if (retval
!= ERROR_OK
)
4202 data
= data
? data
: &dummy
;
4203 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
4205 dap
->stlink_flush_ap_write
= false;
4210 static int stlink_dap_ap_write(struct adiv5_ap
*ap
, unsigned int reg
, uint32_t data
)
4212 struct adiv5_dap
*dap
= ap
->dap
;
4215 retval
= stlink_dap_open_ap(ap
->ap_num
);
4216 if (retval
!= ERROR_OK
)
4219 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
4221 dap
->stlink_flush_ap_write
= true;
4226 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
4228 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
4232 static int stlink_usb_buf_rw_segment(void *handle
, const struct dap_queue
*q
, unsigned int count
)
4234 uint32_t bufsize
= count
* CMD_MEM_AP_2_SIZE(q
[0].cmd
);
4235 uint8_t buf
[bufsize
];
4236 uint8_t ap_num
= q
[0].mem_ap
.ap
->ap_num
;
4237 uint32_t addr
= q
[0].mem_ap
.addr
;
4238 uint32_t csw
= q
[0].mem_ap
.csw
;
4240 int retval
= stlink_dap_open_ap(ap_num
);
4241 if (retval
!= ERROR_OK
)
4245 case CMD_MEM_AP_WRITE8
:
4246 for (unsigned int i
= 0; i
< count
; i
++)
4247 buf
[i
] = q
[i
].mem_ap
.data
>> 8 * (q
[i
].mem_ap
.addr
& 3);
4248 return stlink_usb_write_mem8(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4250 case CMD_MEM_AP_WRITE16
:
4251 for (unsigned int i
= 0; i
< count
; i
++)
4252 h_u16_to_le(&buf
[2 * i
], q
[i
].mem_ap
.data
>> 8 * (q
[i
].mem_ap
.addr
& 2));
4253 return stlink_usb_write_mem16(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4255 case CMD_MEM_AP_WRITE32
:
4256 for (unsigned int i
= 0; i
< count
; i
++)
4257 h_u32_to_le(&buf
[4 * i
], q
[i
].mem_ap
.data
);
4258 if (count
> 1 && q
[0].mem_ap
.addr
== q
[1].mem_ap
.addr
)
4259 return stlink_usb_write_mem32_noaddrinc(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4261 return stlink_usb_write_mem32(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4263 case CMD_MEM_AP_READ8
:
4264 retval
= stlink_usb_read_mem8(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4265 if (retval
== ERROR_OK
)
4266 for (unsigned int i
= 0; i
< count
; i
++)
4267 *q
[i
].mem_ap
.p_data
= buf
[i
] << 8 * (q
[i
].mem_ap
.addr
& 3);
4270 case CMD_MEM_AP_READ16
:
4271 retval
= stlink_usb_read_mem16(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4272 if (retval
== ERROR_OK
)
4273 for (unsigned int i
= 0; i
< count
; i
++)
4274 *q
[i
].mem_ap
.p_data
= le_to_h_u16(&buf
[2 * i
]) << 8 * (q
[i
].mem_ap
.addr
& 2);
4277 case CMD_MEM_AP_READ32
:
4278 if (count
> 1 && q
[0].mem_ap
.addr
== q
[1].mem_ap
.addr
)
4279 retval
= stlink_usb_read_mem32_noaddrinc(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4281 retval
= stlink_usb_read_mem32(stlink_dap_handle
, ap_num
, csw
, addr
, bufsize
, buf
);
4282 if (retval
== ERROR_OK
)
4283 for (unsigned int i
= 0; i
< count
; i
++)
4284 *q
[i
].mem_ap
.p_data
= le_to_h_u32(&buf
[4 * i
]);
4292 static int stlink_usb_count_buf_rw_queue(const struct dap_queue
*q
, unsigned int len
)
4294 uint32_t incr
= CMD_MEM_AP_2_SIZE(q
[0].cmd
);
4295 unsigned int len_max
;
4298 len_max
= stlink_usb_block(stlink_dap_handle
);
4300 len_max
= STLINK_MAX_RW16_32
/ incr
;
4302 /* check for no address increment, 32 bits only */
4303 if (len
> 1 && incr
== 4 && q
[0].mem_ap
.addr
== q
[1].mem_ap
.addr
)
4309 for (unsigned int i
= 1; i
< len
; i
++)
4310 if (q
[i
].cmd
!= q
[0].cmd
||
4311 q
[i
].mem_ap
.ap
!= q
[0].mem_ap
.ap
||
4312 q
[i
].mem_ap
.csw
!= q
[0].mem_ap
.csw
||
4313 q
[i
].mem_ap
.addr
!= q
[i
- 1].mem_ap
.addr
+ incr
)
4319 static int stlink_usb_mem_rw_queue(void *handle
, const struct dap_queue
*q
, unsigned int len
, unsigned int *skip
)
4321 unsigned int count
= stlink_usb_count_buf_rw_queue(q
, len
);
4323 int retval
= stlink_usb_buf_rw_segment(handle
, q
, count
);
4324 if (retval
!= ERROR_OK
)
4331 static void stlink_dap_run_internal(struct adiv5_dap
*dap
)
4333 int retval
= stlink_dap_check_reconnect(dap
);
4334 if (retval
!= ERROR_OK
) {
4335 stlink_dap_handle
->queue_index
= 0;
4336 stlink_dap_record_error(retval
);
4340 unsigned int i
= stlink_dap_handle
->queue_index
;
4341 struct dap_queue
*q
= &stlink_dap_handle
->queue
[0];
4343 while (i
&& stlink_dap_get_error() == ERROR_OK
) {
4344 unsigned int skip
= 1;
4348 retval
= stlink_dap_dp_read(q
->dp_r
.dap
, q
->dp_r
.reg
, q
->dp_r
.p_data
);
4351 retval
= stlink_dap_dp_write(q
->dp_w
.dap
, q
->dp_w
.reg
, q
->dp_w
.data
);
4354 retval
= stlink_dap_ap_read(q
->ap_r
.ap
, q
->ap_r
.reg
, q
->ap_r
.p_data
);
4357 /* ignore increment packed, not supported */
4358 if (q
->ap_w
.reg
== MEM_AP_REG_CSW
)
4359 q
->ap_w
.data
&= ~CSW_ADDRINC_PACKED
;
4360 retval
= stlink_dap_ap_write(q
->ap_w
.ap
, q
->ap_w
.reg
, q
->ap_w
.data
);
4363 case CMD_MEM_AP_READ8
:
4364 case CMD_MEM_AP_READ16
:
4365 case CMD_MEM_AP_READ32
:
4366 case CMD_MEM_AP_WRITE8
:
4367 case CMD_MEM_AP_WRITE16
:
4368 case CMD_MEM_AP_WRITE32
:
4369 retval
= stlink_usb_mem_rw_queue(stlink_dap_handle
, q
, i
, &skip
);
4373 LOG_ERROR("ST-Link: Unknown queue command %d", q
->cmd
);
4374 retval
= ERROR_FAIL
;
4377 stlink_dap_record_error(retval
);
4382 stlink_dap_handle
->queue_index
= 0;
4386 static int stlink_dap_run_finalize(struct adiv5_dap
*dap
)
4388 uint32_t ctrlstat
, pwrmask
;
4389 int retval
, saved_retval
;
4391 /* Here no LOG_DEBUG. This is called continuously! */
4394 * ST-Link returns immediately after a DAP write, without waiting for it
4396 * Run a dummy read to DP_RDBUFF, as suggested in
4397 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
4399 if (dap
->stlink_flush_ap_write
) {
4400 dap
->stlink_flush_ap_write
= false;
4401 retval
= stlink_dap_dp_read(dap
, DP_RDBUFF
, NULL
);
4402 if (retval
!= ERROR_OK
) {
4403 dap
->do_reconnect
= true;
4408 saved_retval
= stlink_dap_get_and_clear_error();
4410 retval
= stlink_dap_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
4411 if (retval
!= ERROR_OK
) {
4412 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
4413 dap
->do_reconnect
= true;
4417 if (ctrlstat
& SSTICKYERR
) {
4418 if (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
)
4419 retval
= stlink_dap_dp_write(dap
, DP_CTRL_STAT
,
4420 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
4422 retval
= stlink_dap_dp_write(dap
, DP_ABORT
, STKERRCLR
);
4423 if (retval
!= ERROR_OK
) {
4424 dap
->do_reconnect
= true;
4429 /* check for power lost */
4430 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
4431 if ((ctrlstat
& pwrmask
) != pwrmask
)
4432 dap
->do_reconnect
= true;
4434 return saved_retval
;
4437 static int stlink_dap_op_queue_run(struct adiv5_dap
*dap
)
4439 stlink_dap_run_internal(dap
);
4440 return stlink_dap_run_finalize(dap
);
4444 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
4448 retval
= stlink_dap_closeall_ap();
4449 if (retval
!= ERROR_OK
)
4450 LOG_ERROR("Error closing APs");
4453 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned int reg
,
4456 if (stlink_dap_get_error() != ERROR_OK
)
4459 unsigned int i
= stlink_dap_handle
->queue_index
++;
4460 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4461 q
->cmd
= CMD_DP_READ
;
4464 q
->dp_r
.p_data
= data
;
4466 if (i
== MAX_QUEUE_DEPTH
- 1)
4467 stlink_dap_run_internal(dap
);
4472 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned int reg
,
4475 if (stlink_dap_get_error() != ERROR_OK
)
4478 unsigned int i
= stlink_dap_handle
->queue_index
++;
4479 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4480 q
->cmd
= CMD_DP_WRITE
;
4483 q
->dp_w
.data
= data
;
4485 if (i
== MAX_QUEUE_DEPTH
- 1)
4486 stlink_dap_run_internal(dap
);
4491 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned int reg
,
4494 if (stlink_dap_get_error() != ERROR_OK
)
4497 unsigned int i
= stlink_dap_handle
->queue_index
++;
4498 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4500 /* test STLINK_F_HAS_CSW implicitly tests STLINK_F_HAS_MEM_16BIT, STLINK_F_HAS_MEM_RD_NO_INC */
4501 if ((stlink_dap_handle
->version
.flags
& STLINK_F_HAS_CSW
) &&
4502 (reg
== MEM_AP_REG_DRW
|| reg
== MEM_AP_REG_BD0
|| reg
== MEM_AP_REG_BD1
||
4503 reg
== MEM_AP_REG_BD2
|| reg
== MEM_AP_REG_BD3
)) {
4504 /* de-queue previous write-TAR */
4505 struct dap_queue
*prev_q
= q
- 1;
4506 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_TAR
) {
4507 stlink_dap_handle
->queue_index
= i
;
4512 /* de-queue previous write-CSW if it didn't changed ap->csw_default */
4513 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_CSW
&&
4514 !prev_q
->ap_w
.changes_csw_default
) {
4515 stlink_dap_handle
->queue_index
= i
;
4519 switch (ap
->csw_value
& CSW_SIZE_MASK
) {
4521 q
->cmd
= CMD_MEM_AP_READ8
;
4524 q
->cmd
= CMD_MEM_AP_READ16
;
4527 q
->cmd
= CMD_MEM_AP_READ32
;
4530 LOG_ERROR("ST-Link: Unsupported CSW size %d", ap
->csw_value
& CSW_SIZE_MASK
);
4531 stlink_dap_record_error(ERROR_FAIL
);
4535 q
->mem_ap
.addr
= (reg
== MEM_AP_REG_DRW
) ? ap
->tar_value
: ((ap
->tar_value
& ~0x0f) | (reg
& 0x0c));
4537 q
->mem_ap
.p_data
= data
;
4538 q
->mem_ap
.csw
= ap
->csw_default
;
4540 /* force TAR and CSW update */
4541 ap
->tar_valid
= false;
4544 q
->cmd
= CMD_AP_READ
;
4547 q
->ap_r
.p_data
= data
;
4550 if (i
== MAX_QUEUE_DEPTH
- 1)
4551 stlink_dap_run_internal(ap
->dap
);
4556 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned int reg
,
4559 if (stlink_dap_get_error() != ERROR_OK
)
4562 unsigned int i
= stlink_dap_handle
->queue_index
++;
4563 struct dap_queue
*q
= &stlink_dap_handle
->queue
[i
];
4565 /* test STLINK_F_HAS_CSW implicitly tests STLINK_F_HAS_MEM_16BIT, STLINK_F_HAS_MEM_WR_NO_INC */
4566 if ((stlink_dap_handle
->version
.flags
& STLINK_F_HAS_CSW
) &&
4567 (reg
== MEM_AP_REG_DRW
|| reg
== MEM_AP_REG_BD0
|| reg
== MEM_AP_REG_BD1
||
4568 reg
== MEM_AP_REG_BD2
|| reg
== MEM_AP_REG_BD3
)) {
4569 /* de-queue previous write-TAR */
4570 struct dap_queue
*prev_q
= q
- 1;
4571 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_TAR
) {
4572 stlink_dap_handle
->queue_index
= i
;
4577 /* de-queue previous write-CSW if it didn't changed ap->csw_default */
4578 if (i
&& prev_q
->cmd
== CMD_AP_WRITE
&& prev_q
->ap_w
.ap
== ap
&& prev_q
->ap_w
.reg
== MEM_AP_REG_CSW
&&
4579 !prev_q
->ap_w
.changes_csw_default
) {
4580 stlink_dap_handle
->queue_index
= i
;
4584 switch (ap
->csw_value
& CSW_SIZE_MASK
) {
4586 q
->cmd
= CMD_MEM_AP_WRITE8
;
4589 q
->cmd
= CMD_MEM_AP_WRITE16
;
4592 q
->cmd
= CMD_MEM_AP_WRITE32
;
4595 LOG_ERROR("ST-Link: Unsupported CSW size %d", ap
->csw_value
& CSW_SIZE_MASK
);
4596 stlink_dap_record_error(ERROR_FAIL
);
4600 q
->mem_ap
.addr
= (reg
== MEM_AP_REG_DRW
) ? ap
->tar_value
: ((ap
->tar_value
& ~0x0f) | (reg
& 0x0c));
4602 q
->mem_ap
.data
= data
;
4603 q
->mem_ap
.csw
= ap
->csw_default
;
4605 /* force TAR and CSW update */
4606 ap
->tar_valid
= false;
4609 q
->cmd
= CMD_AP_WRITE
;
4612 q
->ap_w
.data
= data
;
4613 if (reg
== MEM_AP_REG_CSW
&& ap
->csw_default
!= last_csw_default
[ap
->ap_num
]) {
4614 q
->ap_w
.changes_csw_default
= true;
4615 last_csw_default
[ap
->ap_num
] = ap
->csw_default
;
4617 q
->ap_w
.changes_csw_default
= false;
4621 if (i
== MAX_QUEUE_DEPTH
- 1)
4622 stlink_dap_run_internal(ap
->dap
);
4627 static int stlink_swim_op_srst(void)
4629 return stlink_swim_generate_rst(stlink_dap_handle
);
4632 static int stlink_swim_op_read_mem(uint32_t addr
, uint32_t size
,
4633 uint32_t count
, uint8_t *buffer
)
4636 uint32_t bytes_remaining
;
4638 LOG_DEBUG_IO("read at 0x%08" PRIx32
" len %" PRIu32
"*0x%08" PRIx32
, addr
, size
, count
);
4642 bytes_remaining
= (count
> STLINK_SWIM_DATA_SIZE
) ? STLINK_SWIM_DATA_SIZE
: count
;
4643 retval
= stlink_swim_readbytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
4644 if (retval
!= ERROR_OK
)
4647 buffer
+= bytes_remaining
;
4648 addr
+= bytes_remaining
;
4649 count
-= bytes_remaining
;
4655 static int stlink_swim_op_write_mem(uint32_t addr
, uint32_t size
,
4656 uint32_t count
, const uint8_t *buffer
)
4659 uint32_t bytes_remaining
;
4661 LOG_DEBUG_IO("write at 0x%08" PRIx32
" len %" PRIu32
"*0x%08" PRIx32
, addr
, size
, count
);
4665 bytes_remaining
= (count
> STLINK_SWIM_DATA_SIZE
) ? STLINK_SWIM_DATA_SIZE
: count
;
4666 retval
= stlink_swim_writebytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
4667 if (retval
!= ERROR_OK
)
4670 buffer
+= bytes_remaining
;
4671 addr
+= bytes_remaining
;
4672 count
-= bytes_remaining
;
4678 static int stlink_swim_op_reconnect(void)
4682 retval
= stlink_usb_mode_enter(stlink_dap_handle
, STLINK_MODE_DEBUG_SWIM
);
4683 if (retval
!= ERROR_OK
)
4686 return stlink_swim_resync(stlink_dap_handle
);
4689 static int stlink_dap_config_trace(bool enabled
,
4690 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
4691 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
4692 uint16_t *prescaler
)
4694 return stlink_config_trace(stlink_dap_handle
, enabled
, pin_protocol
,
4695 port_size
, trace_freq
, traceclkin_freq
,
4699 static int stlink_dap_trace_read(uint8_t *buf
, size_t *size
)
4701 return stlink_usb_trace_read(stlink_dap_handle
, buf
, size
);
4705 COMMAND_HANDLER(stlink_dap_serial_command
)
4707 LOG_DEBUG("stlink_dap_serial_command");
4709 if (CMD_ARGC
!= 1) {
4710 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
4711 return ERROR_COMMAND_SYNTAX_ERROR
;
4714 if (stlink_dap_param
.serial
) {
4715 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
4716 free((void *)stlink_dap_param
.serial
);
4719 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
4724 COMMAND_HANDLER(stlink_dap_vid_pid
)
4726 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
4728 if (CMD_ARGC
> max_usb_ids
* 2) {
4729 LOG_WARNING("ignoring extra IDs in vid_pid "
4730 "(maximum is %d pairs)", max_usb_ids
);
4731 CMD_ARGC
= max_usb_ids
* 2;
4733 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
4734 LOG_WARNING("incomplete vid_pid configuration directive");
4735 return ERROR_COMMAND_SYNTAX_ERROR
;
4737 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
4738 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
4739 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
4742 /* null termination */
4743 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
4749 COMMAND_HANDLER(stlink_dap_backend_command
)
4751 /* default values */
4752 bool use_stlink_tcp
= false;
4753 uint16_t stlink_tcp_port
= 7184;
4755 if (CMD_ARGC
== 0 || CMD_ARGC
> 2)
4756 return ERROR_COMMAND_SYNTAX_ERROR
;
4757 else if (strcmp(CMD_ARGV
[0], "usb") == 0) {
4759 return ERROR_COMMAND_SYNTAX_ERROR
;
4760 /* else use_stlink_tcp = false (already the case ) */
4761 } else if (strcmp(CMD_ARGV
[0], "tcp") == 0) {
4762 use_stlink_tcp
= true;
4764 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[1], stlink_tcp_port
);
4766 return ERROR_COMMAND_SYNTAX_ERROR
;
4768 stlink_dap_param
.use_stlink_tcp
= use_stlink_tcp
;
4769 stlink_dap_param
.stlink_tcp_port
= stlink_tcp_port
;
4774 #define BYTES_PER_LINE 16
4775 COMMAND_HANDLER(stlink_dap_cmd_command
)
4777 unsigned int rx_n
, tx_n
;
4778 struct stlink_usb_handle_s
*h
= stlink_dap_handle
;
4781 return ERROR_COMMAND_SYNTAX_ERROR
;
4783 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], rx_n
);
4784 tx_n
= CMD_ARGC
- 1;
4785 if (tx_n
> STLINK_SG_SIZE
|| rx_n
> STLINK_DATA_SIZE
) {
4786 LOG_ERROR("max %x byte sent and %d received", STLINK_SG_SIZE
, STLINK_DATA_SIZE
);
4787 return ERROR_COMMAND_SYNTAX_ERROR
;
4790 stlink_usb_init_buffer(h
, h
->rx_ep
, rx_n
);
4792 for (unsigned int i
= 0; i
< tx_n
; i
++) {
4794 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[i
+ 1], byte
);
4795 h
->cmdbuf
[h
->cmdidx
++] = byte
;
4798 int retval
= stlink_usb_xfer_noerrcheck(h
, h
->databuf
, rx_n
);
4799 if (retval
!= ERROR_OK
) {
4800 LOG_ERROR("Error %d", retval
);
4804 for (unsigned int i
= 0; i
< rx_n
; i
++)
4805 command_print_sameline(CMD
, "0x%02x%c", h
->databuf
[i
],
4806 ((i
== (rx_n
- 1)) || ((i
% BYTES_PER_LINE
) == (BYTES_PER_LINE
- 1))) ? '\n' : ' ');
4812 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
4815 .handler
= stlink_dap_serial_command
,
4816 .mode
= COMMAND_CONFIG
,
4817 .help
= "set the serial number of the adapter",
4818 .usage
= "<serial_number>",
4822 .handler
= stlink_dap_vid_pid
,
4823 .mode
= COMMAND_CONFIG
,
4824 .help
= "USB VID and PID of the adapter",
4825 .usage
= "(vid pid)+",
4829 .handler
= &stlink_dap_backend_command
,
4830 .mode
= COMMAND_CONFIG
,
4831 .help
= "select which ST-Link backend to use",
4832 .usage
= "usb | tcp [port]",
4836 .handler
= stlink_dap_cmd_command
,
4837 .mode
= COMMAND_EXEC
,
4838 .help
= "send arbitrary command",
4839 .usage
= "rx_n (tx_byte)+",
4841 COMMAND_REGISTRATION_DONE
4845 static const struct command_registration stlink_dap_command_handlers
[] = {
4848 .mode
= COMMAND_ANY
,
4849 .help
= "perform st-link management",
4850 .chain
= stlink_dap_subcommand_handlers
,
4853 COMMAND_REGISTRATION_DONE
4857 static int stlink_dap_init(void)
4859 enum reset_types jtag_reset_config
= jtag_get_reset_config();
4860 enum stlink_mode mode
;
4863 LOG_DEBUG("stlink_dap_init()");
4865 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
4866 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
4867 stlink_dap_param
.connect_under_reset
= true;
4869 LOG_WARNING("\'srst_nogate\' reset_config option is required");
4872 if (transport_is_dapdirect_swd())
4873 mode
= STLINK_MODE_DEBUG_SWD
;
4874 else if (transport_is_dapdirect_jtag())
4875 mode
= STLINK_MODE_DEBUG_JTAG
;
4876 else if (transport_is_swim())
4877 mode
= STLINK_MODE_DEBUG_SWIM
;
4879 LOG_ERROR("Unsupported transport");
4883 retval
= stlink_open(&stlink_dap_param
, mode
, (void **)&stlink_dap_handle
);
4884 if (retval
!= ERROR_OK
)
4887 if ((mode
!= STLINK_MODE_DEBUG_SWIM
) &&
4888 !(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
4889 LOG_ERROR("ST-Link version does not support DAP direct transport");
4896 static int stlink_dap_quit(void)
4898 LOG_DEBUG("stlink_dap_quit()");
4900 free((void *)stlink_dap_param
.serial
);
4901 stlink_dap_param
.serial
= NULL
;
4903 return stlink_close(stlink_dap_handle
);
4907 static int stlink_dap_reset(int req_trst
, int req_srst
)
4909 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
4910 return stlink_usb_assert_srst(stlink_dap_handle
,
4911 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
4912 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
4916 static int stlink_dap_speed(int speed
)
4919 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
4920 return ERROR_JTAG_NOT_IMPLEMENTED
;
4923 stlink_dap_param
.initial_interface_speed
= speed
;
4924 stlink_speed(stlink_dap_handle
, speed
, false);
4929 static int stlink_dap_khz(int khz
, int *jtag_speed
)
4932 LOG_ERROR("RCLK not supported");
4936 *jtag_speed
= stlink_speed(stlink_dap_handle
, khz
, true);
4941 static int stlink_dap_speed_div(int speed
, int *khz
)
4947 static const struct dap_ops stlink_dap_ops
= {
4948 .connect
= stlink_dap_op_connect
,
4949 .send_sequence
= stlink_dap_op_send_sequence
,
4950 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
4951 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
4952 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
4953 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
4954 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
4955 .run
= stlink_dap_op_queue_run
,
4956 .sync
= NULL
, /* optional */
4957 .quit
= stlink_dap_op_quit
, /* optional */
4960 static const struct swim_driver stlink_swim_ops
= {
4961 .srst
= stlink_swim_op_srst
,
4962 .read_mem
= stlink_swim_op_read_mem
,
4963 .write_mem
= stlink_swim_op_write_mem
,
4964 .reconnect
= stlink_swim_op_reconnect
,
4967 static const char *const stlink_dap_transport
[] = { "dapdirect_swd", "dapdirect_jtag", "swim", NULL
};
4969 struct adapter_driver stlink_dap_adapter_driver
= {
4971 .transports
= stlink_dap_transport
,
4972 .commands
= stlink_dap_command_handlers
,
4974 .init
= stlink_dap_init
,
4975 .quit
= stlink_dap_quit
,
4976 .reset
= stlink_dap_reset
,
4977 .speed
= stlink_dap_speed
,
4978 .khz
= stlink_dap_khz
,
4979 .speed_div
= stlink_dap_speed_div
,
4980 .config_trace
= stlink_dap_config_trace
,
4981 .poll_trace
= stlink_dap_trace_read
,
4983 .dap_jtag_ops
= &stlink_dap_ops
,
4984 .dap_swd_ops
= &stlink_dap_ops
,
4985 .swim_ops
= &stlink_swim_ops
,