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
;
244 struct stlink_usb_handle_s
{
246 struct stlink_backend_s
*backend
;
249 struct stlink_usb_priv_s usb_backend_priv
;
250 struct stlink_tcp_priv_s tcp_backend_priv
;
267 uint32_t max_mem_packet
;
269 enum stlink_mode st_mode
;
271 struct stlink_usb_version version
;
278 /** whether SWO tracing is enabled or not */
280 /** trace module source clock */
283 /** reconnect is needed next time we try to query the
285 bool reconnect_pending
;
286 /** queue of dap_direct operations */
287 struct dap_queue queue
[MAX_QUEUE_DEPTH
];
288 /** first element available in the queue */
289 unsigned int queue_index
;
293 static inline int stlink_usb_open(void *handle
, struct hl_interface_param_s
*param
)
295 struct stlink_usb_handle_s
*h
= handle
;
296 return h
->backend
->open(handle
, param
);
300 static inline int stlink_usb_close(void *handle
)
302 struct stlink_usb_handle_s
*h
= handle
;
303 return h
->backend
->close(handle
);
306 static inline int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
308 struct stlink_usb_handle_s
*h
= handle
;
309 return h
->backend
->xfer_noerrcheck(handle
, buf
, size
);
312 #define STLINK_SWIM_ERR_OK 0x00
313 #define STLINK_SWIM_BUSY 0x01
314 #define STLINK_DEBUG_ERR_OK 0x80
315 #define STLINK_DEBUG_ERR_FAULT 0x81
316 #define STLINK_SWD_AP_WAIT 0x10
317 #define STLINK_SWD_AP_FAULT 0x11
318 #define STLINK_SWD_AP_ERROR 0x12
319 #define STLINK_SWD_AP_PARITY_ERROR 0x13
320 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
321 #define STLINK_JTAG_WRITE_ERROR 0x0c
322 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
323 #define STLINK_SWD_DP_WAIT 0x14
324 #define STLINK_SWD_DP_FAULT 0x15
325 #define STLINK_SWD_DP_ERROR 0x16
326 #define STLINK_SWD_DP_PARITY_ERROR 0x17
328 #define STLINK_SWD_AP_WDATA_ERROR 0x18
329 #define STLINK_SWD_AP_STICKY_ERROR 0x19
330 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
332 #define STLINK_BAD_AP_ERROR 0x1d
334 #define STLINK_CORE_RUNNING 0x80
335 #define STLINK_CORE_HALTED 0x81
336 #define STLINK_CORE_STAT_UNKNOWN -1
338 #define STLINK_GET_VERSION 0xF1
339 #define STLINK_DEBUG_COMMAND 0xF2
340 #define STLINK_DFU_COMMAND 0xF3
341 #define STLINK_SWIM_COMMAND 0xF4
342 #define STLINK_GET_CURRENT_MODE 0xF5
343 #define STLINK_GET_TARGET_VOLTAGE 0xF7
345 #define STLINK_DEV_DFU_MODE 0x00
346 #define STLINK_DEV_MASS_MODE 0x01
347 #define STLINK_DEV_DEBUG_MODE 0x02
348 #define STLINK_DEV_SWIM_MODE 0x03
349 #define STLINK_DEV_BOOTLOADER_MODE 0x04
350 #define STLINK_DEV_UNKNOWN_MODE -1
352 #define STLINK_DFU_EXIT 0x07
355 STLINK_SWIM_ENTER_SEQ
356 1.3ms low then 750Hz then 1.5kHz
359 STM8 DM pulls reset pin low 50us
362 uint8_t (0=low|1=high)
369 send synchronization seq (16us low, response 64 clocks low)
371 #define STLINK_SWIM_ENTER 0x00
372 #define STLINK_SWIM_EXIT 0x01
373 #define STLINK_SWIM_READ_CAP 0x02
374 #define STLINK_SWIM_SPEED 0x03
375 #define STLINK_SWIM_ENTER_SEQ 0x04
376 #define STLINK_SWIM_GEN_RST 0x05
377 #define STLINK_SWIM_RESET 0x06
378 #define STLINK_SWIM_ASSERT_RESET 0x07
379 #define STLINK_SWIM_DEASSERT_RESET 0x08
380 #define STLINK_SWIM_READSTATUS 0x09
381 #define STLINK_SWIM_WRITEMEM 0x0a
382 #define STLINK_SWIM_READMEM 0x0b
383 #define STLINK_SWIM_READBUF 0x0c
385 #define STLINK_DEBUG_GETSTATUS 0x01
386 #define STLINK_DEBUG_FORCEDEBUG 0x02
387 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
388 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
389 #define STLINK_DEBUG_APIV1_READREG 0x05
390 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
391 #define STLINK_DEBUG_READMEM_32BIT 0x07
392 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
393 #define STLINK_DEBUG_RUNCORE 0x09
394 #define STLINK_DEBUG_STEPCORE 0x0a
395 #define STLINK_DEBUG_APIV1_SETFP 0x0b
396 #define STLINK_DEBUG_READMEM_8BIT 0x0c
397 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
398 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
399 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
400 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
402 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
403 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
404 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
406 #define STLINK_DEBUG_APIV1_ENTER 0x20
407 #define STLINK_DEBUG_EXIT 0x21
408 #define STLINK_DEBUG_READCOREID 0x22
410 #define STLINK_DEBUG_APIV2_ENTER 0x30
411 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
412 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
413 #define STLINK_DEBUG_APIV2_READREG 0x33
414 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
415 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
416 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
418 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
419 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
420 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
422 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
424 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
425 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
426 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
427 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
428 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
429 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
430 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
431 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
432 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
434 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
435 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
437 #define STLINK_APIV3_SET_COM_FREQ 0x61
438 #define STLINK_APIV3_GET_COM_FREQ 0x62
440 #define STLINK_APIV3_GET_VERSION_EX 0xFB
442 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
443 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
444 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
446 #define STLINK_DEBUG_PORT_ACCESS 0xffff
448 #define STLINK_TRACE_SIZE 4096
449 #define STLINK_TRACE_MAX_HZ 2000000
450 #define STLINK_V3_TRACE_MAX_HZ 24000000
452 #define STLINK_V3_MAX_FREQ_NB 10
454 #define REQUEST_SENSE 0x03
455 #define REQUEST_SENSE_LENGTH 18
457 /* STLINK TCP commands */
458 #define STLINK_TCP_CMD_REFRESH_DEVICE_LIST 0x00
459 #define STLINK_TCP_CMD_GET_NB_DEV 0x01
460 #define STLINK_TCP_CMD_GET_DEV_INFO 0x02
461 #define STLINK_TCP_CMD_OPEN_DEV 0x03
462 #define STLINK_TCP_CMD_CLOSE_DEV 0x04
463 #define STLINK_TCP_CMD_SEND_USB_CMD 0x05
464 #define STLINK_TCP_CMD_GET_SERVER_VERSION 0x06
465 #define STLINK_TCP_CMD_GET_NB_OF_DEV_CLIENTS 0x07
467 /* STLINK TCP constants */
468 #define OPENOCD_STLINK_TCP_API_VERSION 1
469 #define STLINK_TCP_REQUEST_WRITE 0
470 #define STLINK_TCP_REQUEST_READ 1
471 #define STLINK_TCP_REQUEST_READ_SWO 3
472 #define STLINK_TCP_SS_SIZE 4
473 #define STLINK_TCP_USB_CMD_SIZE 32
474 #define STLINK_TCP_SERIAL_SIZE 32
475 #define STLINK_TCP_SEND_BUFFER_SIZE 10240
476 #define STLINK_TCP_RECV_BUFFER_SIZE 10240
478 /* STLINK TCP command status */
479 #define STLINK_TCP_SS_OK 0x00000001
480 #define STLINK_TCP_SS_MEMORY_PROBLEM 0x00001000
481 #define STLINK_TCP_SS_TIMEOUT 0x00001001
482 #define STLINK_TCP_SS_BAD_PARAMETER 0x00001002
483 #define STLINK_TCP_SS_OPEN_ERR 0x00001003
484 #define STLINK_TCP_SS_TRUNCATED_DATA 0x00001052
485 #define STLINK_TCP_SS_CMD_NOT_AVAILABLE 0x00001053
486 #define STLINK_TCP_SS_TCP_ERROR 0x00002001
487 #define STLINK_TCP_SS_TCP_CANT_CONNECT 0x00002002
488 #define STLINK_TCP_SS_WIN32_ERROR 0x00010000
491 * Map the relevant features, quirks and workaround for specific firmware
494 #define STLINK_F_HAS_TRACE BIT(0) /* v2>=j13 || v3 */
495 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(1) /* v2>=j15 || v3 */
496 #define STLINK_F_HAS_SWD_SET_FREQ BIT(2) /* v2>=j22 */
497 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(3) /* v2>=j24 */
498 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(4) /* v2>=j24 && v2<j32 */
499 #define STLINK_F_HAS_DAP_REG BIT(5) /* v2>=j24 || v3 */
500 #define STLINK_F_HAS_MEM_16BIT BIT(6) /* v2>=j26 || v3 */
501 #define STLINK_F_HAS_AP_INIT BIT(7) /* v2>=j28 || v3 */
502 #define STLINK_F_FIX_CLOSE_AP BIT(8) /* v2>=j29 || v3 */
503 #define STLINK_F_HAS_DPBANKSEL BIT(9) /* v2>=j32 || v3>=j2 */
504 #define STLINK_F_HAS_RW8_512BYTES BIT(10) /* v3>=j6 */
507 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
508 #define STLINK_F_HAS_FPU_REG STLINK_F_HAS_GETLASTRWSTATUS2
509 #define STLINK_F_HAS_CSW STLINK_F_HAS_DPBANKSEL
511 #define STLINK_REGSEL_IS_FPU(x) ((x) > 0x1F)
518 /* SWD clock speed */
519 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
521 {1800, 1}, /* default */
534 /* JTAG clock speed */
535 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
539 {1125, 32}, /* default */
545 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
546 static int stlink_swim_status(void *handle
);
547 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
548 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
549 static int stlink_speed(void *handle
, int khz
, bool query
);
550 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
);
553 static unsigned int stlink_usb_block(void *handle
)
555 struct stlink_usb_handle_s
*h
= handle
;
559 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
560 return STLINKV3_MAX_RW8
;
562 return STLINK_MAX_RW8
;
565 #ifdef USE_LIBUSB_ASYNCIO
567 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
569 int *completed
= transfer
->user_data
;
571 /* caller interprets result and frees transfer */
575 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
577 int r
, *completed
= transfer
->user_data
;
579 while (!*completed
) {
580 r
= jtag_libusb_handle_events_completed(completed
);
582 if (r
== LIBUSB_ERROR_INTERRUPTED
)
584 libusb_cancel_transfer(transfer
);
591 static int transfer_error_status(const struct libusb_transfer
*transfer
)
595 switch (transfer
->status
) {
596 case LIBUSB_TRANSFER_COMPLETED
:
599 case LIBUSB_TRANSFER_TIMED_OUT
:
600 r
= LIBUSB_ERROR_TIMEOUT
;
602 case LIBUSB_TRANSFER_STALL
:
603 r
= LIBUSB_ERROR_PIPE
;
605 case LIBUSB_TRANSFER_OVERFLOW
:
606 r
= LIBUSB_ERROR_OVERFLOW
;
608 case LIBUSB_TRANSFER_NO_DEVICE
:
609 r
= LIBUSB_ERROR_NO_DEVICE
;
611 case LIBUSB_TRANSFER_ERROR
:
612 case LIBUSB_TRANSFER_CANCELLED
:
616 r
= LIBUSB_ERROR_OTHER
;
630 size_t transfer_size
;
631 struct libusb_transfer
*transfer
;
634 static int jtag_libusb_bulk_transfer_n(
635 struct libusb_device_handle
*dev_handle
,
636 struct jtag_xfer
*transfers
,
641 int returnval
= ERROR_OK
;
644 for (size_t i
= 0; i
< n_transfers
; ++i
) {
645 transfers
[i
].retval
= 0;
646 transfers
[i
].completed
= 0;
647 transfers
[i
].transfer_size
= 0;
648 transfers
[i
].transfer
= libusb_alloc_transfer(0);
650 if (!transfers
[i
].transfer
) {
651 for (size_t j
= 0; j
< i
; ++j
)
652 libusb_free_transfer(transfers
[j
].transfer
);
654 LOG_DEBUG("ERROR, failed to alloc usb transfers");
655 for (size_t k
= 0; k
< n_transfers
; ++k
)
656 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
661 for (size_t i
= 0; i
< n_transfers
; ++i
) {
662 libusb_fill_bulk_transfer(
663 transfers
[i
].transfer
,
665 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
666 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
667 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
669 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
671 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
673 /* Probably no point continuing to submit transfers once a submission fails.
674 * As a result, tag all remaining transfers as errors.
676 for (size_t j
= i
; j
< n_transfers
; ++j
)
677 transfers
[j
].retval
= retval
;
679 returnval
= ERROR_FAIL
;
684 /* Wait for every submitted USB transfer to complete.
686 for (size_t i
= 0; i
< n_transfers
; ++i
) {
687 if (transfers
[i
].retval
== 0) {
688 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
690 retval
= transfer_error_status(transfers
[i
].transfer
);
692 returnval
= ERROR_FAIL
;
693 transfers
[i
].retval
= retval
;
694 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
696 /* Assuming actual_length is only valid if there is no transfer error.
698 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
702 libusb_free_transfer(transfers
[i
].transfer
);
703 transfers
[i
].transfer
= NULL
;
713 static int stlink_usb_xfer_v1_get_status(void *handle
)
715 struct stlink_usb_handle_s
*h
= handle
;
721 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
723 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
724 STLINK_READ_TIMEOUT
, &tr
);
730 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
733 if (t1
!= 0x53425355)
741 if (h
->cmdbuf
[12] != 0)
747 #ifdef USE_LIBUSB_ASYNCIO
748 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
750 struct stlink_usb_handle_s
*h
= handle
;
754 size_t n_transfers
= 0;
755 struct jtag_xfer transfers
[2];
757 memset(transfers
, 0, sizeof(transfers
));
759 transfers
[0].ep
= h
->tx_ep
;
760 transfers
[0].buf
= h
->cmdbuf
;
761 transfers
[0].size
= cmdsize
;
765 if (h
->direction
== h
->tx_ep
&& size
) {
766 transfers
[1].ep
= h
->tx_ep
;
767 transfers
[1].buf
= (uint8_t *)buf
;
768 transfers
[1].size
= size
;
771 } else if (h
->direction
== h
->rx_ep
&& size
) {
772 transfers
[1].ep
= h
->rx_ep
;
773 transfers
[1].buf
= (uint8_t *)buf
;
774 transfers
[1].size
= size
;
779 return jtag_libusb_bulk_transfer_n(
780 h
->usb_backend_priv
.fd
,
783 STLINK_WRITE_TIMEOUT
);
786 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
788 struct stlink_usb_handle_s
*h
= handle
;
793 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
794 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
795 if (ret
|| tr
!= cmdsize
)
798 if (h
->direction
== h
->tx_ep
&& size
) {
799 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)buf
,
800 size
, STLINK_WRITE_TIMEOUT
, &tr
);
801 if (ret
|| tr
!= size
) {
802 LOG_DEBUG("bulk write failed");
805 } else if (h
->direction
== h
->rx_ep
&& size
) {
806 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)buf
,
807 size
, STLINK_READ_TIMEOUT
, &tr
);
808 if (ret
|| tr
!= size
) {
809 LOG_DEBUG("bulk read failed");
819 static int stlink_usb_xfer_v1_get_sense(void *handle
)
822 struct stlink_usb_handle_s
*h
= handle
;
826 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
828 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
829 h
->cmdbuf
[h
->cmdidx
++] = 0;
830 h
->cmdbuf
[h
->cmdidx
++] = 0;
831 h
->cmdbuf
[h
->cmdidx
++] = 0;
832 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
834 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
839 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
846 static int stlink_usb_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
848 struct stlink_usb_handle_s
*h
= handle
;
851 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->trace_ep
, (char *)buf
, size
,
852 STLINK_READ_TIMEOUT
, &tr
);
853 if (ret
|| tr
!= size
) {
854 LOG_ERROR("bulk trace read failed");
862 transfers block in cmdbuf
863 <size> indicates number of bytes in the following
865 Ignore the (eventual) error code in the received packet.
867 static int stlink_usb_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
869 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
870 struct stlink_usb_handle_s
*h
= handle
;
874 if (h
->version
.stlink
== 1) {
875 cmdsize
= STLINK_SG_SIZE
;
876 /* put length in bCBWCBLength */
877 h
->cmdbuf
[14] = h
->cmdidx
-15;
880 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
885 if (h
->version
.stlink
== 1) {
886 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
887 /* check csw status */
888 if (h
->cmdbuf
[12] == 1) {
889 LOG_DEBUG("get sense");
890 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
901 static int stlink_tcp_send_cmd(void *handle
, int send_size
, int recv_size
, bool check_tcp_status
)
903 struct stlink_usb_handle_s
*h
= handle
;
907 /* send the TCP command */
908 int sent_size
= send(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.send_buf
, send_size
, 0);
909 if (sent_size
!= send_size
) {
910 LOG_ERROR("failed to send USB CMD");
912 LOG_DEBUG("socket send error: %s (errno %d)", strerror(errno
), errno
);
914 LOG_DEBUG("sent size %d (expected %d)", sent_size
, send_size
);
920 /* read the TCP response */
921 int received_size
= recv(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.recv_buf
, recv_size
, 0);
922 if (received_size
!= recv_size
) {
923 LOG_ERROR("failed to receive USB CMD response");
924 if (received_size
== -1)
925 LOG_DEBUG("socket recv error: %s (errno %d)", strerror(errno
), errno
);
927 LOG_DEBUG("received size %d (expected %d)", received_size
, recv_size
);
931 if (check_tcp_status
) {
932 uint32_t tcp_ss
= le_to_h_u32(h
->tcp_backend_priv
.recv_buf
);
933 if (tcp_ss
!= STLINK_TCP_SS_OK
) {
934 LOG_ERROR("TCP error status 0x%X", tcp_ss
);
943 static int stlink_tcp_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
945 struct stlink_usb_handle_s
*h
= handle
;
947 int send_size
= STLINK_TCP_USB_CMD_SIZE
;
948 int recv_size
= STLINK_TCP_SS_SIZE
;
952 /* prepare the TCP command */
953 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_SEND_USB_CMD
;
954 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 3); /* reserved for alignment and future use, must be zero */
955 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
956 /* tcp_backend_priv.send_buf[8..23] already contains the constructed stlink command */
957 h
->tcp_backend_priv
.send_buf
[24] = h
->direction
;
958 memset(&h
->tcp_backend_priv
.send_buf
[25], 0, 3); /* reserved for alignment and future use, must be zero */
960 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[28], size
);
963 * if the xfer is a write request (tx_ep)
964 * > then buf content will be copied
966 * else : the xfer is a read or trace read request (rx_ep or trace_ep)
967 * > the buf content will be filled from &databuf[4].
969 * note : if h->direction is trace_ep, h->cmdbuf is zeros.
972 if (h
->direction
== h
->tx_ep
) { /* STLINK_TCP_REQUEST_WRITE */
974 if (send_size
> STLINK_TCP_SEND_BUFFER_SIZE
) {
975 LOG_ERROR("STLINK_TCP command buffer overflow");
978 memcpy(&h
->tcp_backend_priv
.send_buf
[32], buf
, size
);
979 } else { /* STLINK_TCP_REQUEST_READ or STLINK_TCP_REQUEST_READ_SWO */
981 if (recv_size
> STLINK_TCP_RECV_BUFFER_SIZE
) {
982 LOG_ERROR("STLINK_TCP data buffer overflow");
987 int ret
= stlink_tcp_send_cmd(h
, send_size
, recv_size
, true);
991 if (h
->direction
!= h
->tx_ep
) {
992 /* the read data is located in tcp_backend_priv.recv_buf[4] */
993 /* most of the case it will be copying the data from tcp_backend_priv.recv_buf[4]
994 * to handle->cmd_buff which are the same, so let's avoid unnecessary copying */
995 if (buf
!= &h
->tcp_backend_priv
.recv_buf
[4])
996 memcpy((uint8_t *)buf
, &h
->tcp_backend_priv
.recv_buf
[4], size
);
1003 static int stlink_tcp_read_trace(void *handle
, const uint8_t *buf
, int size
)
1005 struct stlink_usb_handle_s
*h
= handle
;
1007 stlink_usb_init_buffer(h
, h
->trace_ep
, 0);
1008 return stlink_tcp_xfer_noerrcheck(handle
, buf
, size
);
1012 Converts an STLINK status code held in the first byte of a response
1013 to an openocd error, logs any error/wait status as debug output.
1015 static int stlink_usb_error_check(void *handle
)
1017 struct stlink_usb_handle_s
*h
= handle
;
1021 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1022 switch (h
->databuf
[0]) {
1023 case STLINK_SWIM_ERR_OK
:
1025 case STLINK_SWIM_BUSY
:
1028 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1033 /* TODO: no error checking yet on api V1 */
1034 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1035 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
1037 switch (h
->databuf
[0]) {
1038 case STLINK_DEBUG_ERR_OK
:
1040 case STLINK_DEBUG_ERR_FAULT
:
1041 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
1043 case STLINK_SWD_AP_WAIT
:
1044 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
1046 case STLINK_SWD_DP_WAIT
:
1047 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
1049 case STLINK_JTAG_GET_IDCODE_ERROR
:
1050 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
1052 case STLINK_JTAG_WRITE_ERROR
:
1053 LOG_DEBUG("Write error");
1055 case STLINK_JTAG_WRITE_VERIF_ERROR
:
1056 LOG_DEBUG("Write verify error, ignoring");
1058 case STLINK_SWD_AP_FAULT
:
1059 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
1060 * returns ERROR_OK with the comment:
1061 * Change in error status when reading outside RAM.
1062 * This fix allows CDT plugin to visualize memory.
1064 LOG_DEBUG("STLINK_SWD_AP_FAULT");
1066 case STLINK_SWD_AP_ERROR
:
1067 LOG_DEBUG("STLINK_SWD_AP_ERROR");
1069 case STLINK_SWD_AP_PARITY_ERROR
:
1070 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
1072 case STLINK_SWD_DP_FAULT
:
1073 LOG_DEBUG("STLINK_SWD_DP_FAULT");
1075 case STLINK_SWD_DP_ERROR
:
1076 LOG_DEBUG("STLINK_SWD_DP_ERROR");
1078 case STLINK_SWD_DP_PARITY_ERROR
:
1079 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
1081 case STLINK_SWD_AP_WDATA_ERROR
:
1082 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
1084 case STLINK_SWD_AP_STICKY_ERROR
:
1085 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
1087 case STLINK_SWD_AP_STICKYORUN_ERROR
:
1088 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
1090 case STLINK_BAD_AP_ERROR
:
1091 LOG_DEBUG("STLINK_BAD_AP_ERROR");
1094 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1100 * Wrapper around stlink_usb_xfer_noerrcheck()
1101 * to check the error code in the received packet
1103 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
1109 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1110 if (retval
!= ERROR_OK
)
1113 return stlink_usb_error_check(handle
);
1116 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
1118 Works for commands where the STLINK_DEBUG status is returned in the first
1119 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
1121 Returns an openocd result code.
1123 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
1127 struct stlink_usb_handle_s
*h
= handle
;
1130 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
1131 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1132 if (res
!= ERROR_OK
)
1136 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1137 res
= stlink_swim_status(handle
);
1138 if (res
!= ERROR_OK
)
1142 res
= stlink_usb_error_check(handle
);
1143 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1144 unsigned int delay_us
= (1<<retries
++) * 1000;
1145 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
1154 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
1156 struct stlink_usb_handle_s
*h
= handle
;
1160 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1162 return h
->backend
->read_trace(handle
, buf
, size
);
1166 this function writes transfer length in
1167 the right place in the cb
1169 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
1171 struct stlink_usb_handle_s
*h
= handle
;
1173 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
1176 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
1178 struct stlink_usb_handle_s
*h
= handle
;
1180 /* fill the send buffer */
1181 strcpy((char *)h
->cmdbuf
, "USBC");
1183 /* csw tag not used */
1184 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
1186 /* cbw data transfer length (in the following data phase in or out) */
1187 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
1190 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
1191 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
1192 /* cdb clength (is filled in at xfer) */
1193 h
->cmdbuf
[h
->cmdidx
++] = 0;
1197 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
1199 struct stlink_usb_handle_s
*h
= handle
;
1201 h
->direction
= direction
;
1205 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
1206 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
1208 if (h
->version
.stlink
== 1)
1209 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
1213 static int stlink_usb_version(void *handle
)
1218 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
1219 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
1221 struct stlink_usb_handle_s
*h
= handle
;
1225 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
1227 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
1229 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
1231 if (res
!= ERROR_OK
)
1234 version
= be_to_h_u16(h
->databuf
);
1235 v
= (version
>> 12) & 0x0f;
1236 x
= (version
>> 6) & 0x3f;
1239 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
1240 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
1243 case STLINK_V2_1_PID
:
1244 case STLINK_V2_1_NO_MSD_PID
:
1245 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
1246 /* MxSy : STM8 V2.1 - SWIM only */
1251 /* JxMy : STM32 V2.1 - JTAG/SWD only */
1264 /* STLINK-V3 requires a specific command */
1265 if (v
== 3 && x
== 0 && y
== 0) {
1266 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
1268 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
1270 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
1271 if (res
!= ERROR_OK
)
1275 swim
= h
->databuf
[1];
1276 jtag
= h
->databuf
[2];
1277 msd
= h
->databuf
[3];
1278 bridge
= h
->databuf
[4];
1279 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
1280 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
1283 h
->version
.stlink
= v
;
1284 h
->version
.jtag
= jtag
;
1285 h
->version
.swim
= swim
;
1288 switch (h
->version
.stlink
) {
1290 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
1291 if (h
->version
.jtag
>= 11)
1292 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1294 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
1298 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1299 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1301 /* API for trace from J13 */
1302 /* API for target voltage from J13 */
1303 if (h
->version
.jtag
>= 13)
1304 flags
|= STLINK_F_HAS_TRACE
;
1306 /* preferred API to get last R/W status from J15 */
1307 if (h
->version
.jtag
>= 15)
1308 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1310 /* API to set SWD frequency from J22 */
1311 if (h
->version
.jtag
>= 22)
1312 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1314 /* API to set JTAG frequency from J24 */
1315 /* API to access DAP registers from J24 */
1316 if (h
->version
.jtag
>= 24) {
1317 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1318 flags
|= STLINK_F_HAS_DAP_REG
;
1321 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1322 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1323 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1325 /* API to read/write memory at 16 bit from J26 */
1326 if (h
->version
.jtag
>= 26)
1327 flags
|= STLINK_F_HAS_MEM_16BIT
;
1329 /* API required to init AP before any AP access from J28 */
1330 if (h
->version
.jtag
>= 28)
1331 flags
|= STLINK_F_HAS_AP_INIT
;
1333 /* API required to return proper error code on close AP from J29 */
1334 if (h
->version
.jtag
>= 29)
1335 flags
|= STLINK_F_FIX_CLOSE_AP
;
1337 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1338 /* Memory R/W supports CSW from V2J32 */
1339 if (h
->version
.jtag
>= 32)
1340 flags
|= STLINK_F_HAS_DPBANKSEL
;
1344 /* all STLINK-V3 use api-v3 */
1345 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1347 /* STLINK-V3 is a superset of ST-LINK/V2 */
1350 /* API for target voltage */
1351 flags
|= STLINK_F_HAS_TRACE
;
1353 /* preferred API to get last R/W status */
1354 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1356 /* API to access DAP registers */
1357 flags
|= STLINK_F_HAS_DAP_REG
;
1359 /* API to read/write memory at 16 bit */
1360 flags
|= STLINK_F_HAS_MEM_16BIT
;
1362 /* API required to init AP before any AP access */
1363 flags
|= STLINK_F_HAS_AP_INIT
;
1365 /* API required to return proper error code on close AP */
1366 flags
|= STLINK_F_FIX_CLOSE_AP
;
1368 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1369 /* Memory R/W supports CSW from V3J2 */
1370 if (h
->version
.jtag
>= 2)
1371 flags
|= STLINK_F_HAS_DPBANKSEL
;
1373 /* 8bit read/write max packet size 512 bytes from V3J6 */
1374 if (h
->version
.jtag
>= 6)
1375 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1381 h
->version
.flags
= flags
;
1384 p
+= sprintf(p
, "V%d", v
);
1386 p
+= sprintf(p
, "J%d", jtag
);
1388 p
+= sprintf(p
, "M%d", msd
);
1390 p
+= sprintf(p
, "B%d", bridge
);
1392 sprintf(p
, "S%d", swim
);
1394 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1396 h
->version
.jtag_api
,
1403 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1405 struct stlink_usb_handle_s
*h
= handle
;
1406 uint32_t adc_results
[2];
1408 /* no error message, simply quit with error */
1409 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1410 return ERROR_COMMAND_NOTFOUND
;
1412 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1414 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1416 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1418 if (result
!= ERROR_OK
)
1421 /* convert result */
1422 adc_results
[0] = le_to_h_u32(h
->databuf
);
1423 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1425 *target_voltage
= 0;
1428 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1430 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1435 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1437 struct stlink_usb_handle_s
*h
= handle
;
1441 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1442 return ERROR_COMMAND_NOTFOUND
;
1444 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1446 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1447 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1448 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1451 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1453 if (result
!= ERROR_OK
)
1459 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1461 struct stlink_usb_handle_s
*h
= handle
;
1465 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1466 return ERROR_COMMAND_NOTFOUND
;
1468 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1470 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1471 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1472 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1475 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1477 if (result
!= ERROR_OK
)
1484 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1487 struct stlink_usb_handle_s
*h
= handle
;
1491 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1493 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1495 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1497 if (res
!= ERROR_OK
)
1500 *mode
= h
->databuf
[0];
1506 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1509 struct stlink_usb_handle_s
*h
= handle
;
1513 /* on api V2 we are able the read the latest command
1515 * TODO: we need the test on api V1 too
1517 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1520 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1523 case STLINK_MODE_DEBUG_JTAG
:
1524 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1525 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1526 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1528 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1529 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1531 case STLINK_MODE_DEBUG_SWD
:
1532 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1533 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1534 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1536 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1537 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1539 case STLINK_MODE_DEBUG_SWIM
:
1540 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1541 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1542 /* swim enter does not return any response or status */
1543 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1544 case STLINK_MODE_DFU
:
1545 case STLINK_MODE_MASS
:
1550 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1554 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1557 struct stlink_usb_handle_s
*h
= handle
;
1561 /* command with no reply, use a valid endpoint but zero size */
1562 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1565 case STLINK_MODE_DEBUG_JTAG
:
1566 case STLINK_MODE_DEBUG_SWD
:
1567 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1568 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1570 case STLINK_MODE_DEBUG_SWIM
:
1571 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1572 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1574 case STLINK_MODE_DFU
:
1575 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1576 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1578 case STLINK_MODE_MASS
:
1583 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1585 if (res
!= ERROR_OK
)
1591 static int stlink_usb_assert_srst(void *handle
, int srst
);
1593 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1596 case HL_TRANSPORT_SWD
:
1597 return STLINK_MODE_DEBUG_SWD
;
1598 case HL_TRANSPORT_JTAG
:
1599 return STLINK_MODE_DEBUG_JTAG
;
1601 return STLINK_MODE_UNKNOWN
;
1606 static int stlink_usb_exit_mode(void *handle
)
1610 enum stlink_mode emode
;
1614 res
= stlink_usb_current_mode(handle
, &mode
);
1616 if (res
!= ERROR_OK
)
1619 LOG_DEBUG("MODE: 0x%02X", mode
);
1621 /* try to exit current mode */
1623 case STLINK_DEV_DFU_MODE
:
1624 emode
= STLINK_MODE_DFU
;
1626 case STLINK_DEV_DEBUG_MODE
:
1627 emode
= STLINK_MODE_DEBUG_SWD
;
1629 case STLINK_DEV_SWIM_MODE
:
1630 emode
= STLINK_MODE_DEBUG_SWIM
;
1632 case STLINK_DEV_BOOTLOADER_MODE
:
1633 case STLINK_DEV_MASS_MODE
:
1635 emode
= STLINK_MODE_UNKNOWN
;
1639 if (emode
!= STLINK_MODE_UNKNOWN
)
1640 return stlink_usb_mode_leave(handle
, emode
);
1646 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1650 enum stlink_mode emode
;
1651 struct stlink_usb_handle_s
*h
= handle
;
1655 res
= stlink_usb_exit_mode(handle
);
1656 if (res
!= ERROR_OK
)
1659 res
= stlink_usb_current_mode(handle
, &mode
);
1661 if (res
!= ERROR_OK
)
1664 /* we check the target voltage here as an aid to debugging connection problems.
1665 * the stlink requires the target Vdd to be connected for reliable debugging.
1666 * this cmd is supported in all modes except DFU
1668 if (mode
!= STLINK_DEV_DFU_MODE
) {
1670 float target_voltage
;
1672 /* check target voltage (if supported) */
1673 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1675 if (res
!= ERROR_OK
) {
1676 if (res
!= ERROR_COMMAND_NOTFOUND
)
1677 LOG_ERROR("voltage check failed");
1678 /* attempt to continue as it is not a catastrophic failure */
1680 /* check for a sensible target voltage, operating range is 1.65-5.5v
1681 * according to datasheet */
1682 if (target_voltage
< 1.5)
1683 LOG_ERROR("target voltage may be too low for reliable debugging");
1687 LOG_DEBUG("MODE: 0x%02X", mode
);
1689 /* set selected mode */
1692 if (emode
== STLINK_MODE_UNKNOWN
) {
1693 LOG_ERROR("selected mode (transport) not supported");
1697 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1698 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1699 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1700 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1701 stlink_speed(h
, initial_interface_speed
, false);
1703 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1704 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1705 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1706 stlink_speed(h
, initial_interface_speed
, false);
1710 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
&&
1711 (emode
== STLINK_MODE_DEBUG_JTAG
|| emode
== STLINK_MODE_DEBUG_SWD
)) {
1712 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1714 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1715 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1716 stlink_speed(h
, initial_interface_speed
, false);
1719 /* preliminary SRST assert:
1720 * We want SRST is asserted before activating debug signals (mode_enter).
1721 * As the required mode has not been set, the adapter may not know what pin to use.
1722 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1723 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1724 * after power on, SWIM_RST stays unchanged */
1725 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1726 stlink_usb_assert_srst(handle
, 0);
1727 /* do not check the return status here, we will
1728 proceed and enter the desired mode below
1729 and try asserting srst again. */
1731 res
= stlink_usb_mode_enter(handle
, emode
);
1732 if (res
!= ERROR_OK
)
1735 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1736 if (connect_under_reset
) {
1737 res
= stlink_usb_assert_srst(handle
, 0);
1738 if (res
!= ERROR_OK
)
1742 res
= stlink_usb_current_mode(handle
, &mode
);
1744 if (res
!= ERROR_OK
)
1747 LOG_DEBUG("MODE: 0x%02X", mode
);
1752 /* request status from last swim request */
1753 static int stlink_swim_status(void *handle
)
1755 struct stlink_usb_handle_s
*h
= handle
;
1758 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1759 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1760 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1761 /* error is checked by the caller */
1762 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1763 if (res
!= ERROR_OK
)
1768 the purpose of this function is unknown...
1769 capabilities? anyway for swim v6 it returns
1772 __attribute__((unused
))
1773 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1775 struct stlink_usb_handle_s
*h
= handle
;
1778 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1779 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1780 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1781 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1782 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1783 if (res
!= ERROR_OK
)
1785 memcpy(cap
, h
->databuf
, 8);
1789 /* debug dongle assert/deassert sreset line */
1790 static int stlink_swim_assert_reset(void *handle
, int reset
)
1792 struct stlink_usb_handle_s
*h
= handle
;
1795 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1796 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1798 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1800 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1801 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1802 if (res
!= ERROR_OK
)
1809 1.3ms low then 750Hz then 1.5kHz
1811 static int stlink_swim_enter(void *handle
)
1813 struct stlink_usb_handle_s
*h
= handle
;
1816 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1817 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1818 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1819 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1820 if (res
!= ERROR_OK
)
1825 /* switch high/low speed swim */
1826 static int stlink_swim_speed(void *handle
, int speed
)
1828 struct stlink_usb_handle_s
*h
= handle
;
1831 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1832 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1833 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1835 h
->cmdbuf
[h
->cmdidx
++] = 1;
1837 h
->cmdbuf
[h
->cmdidx
++] = 0;
1838 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1839 if (res
!= ERROR_OK
)
1845 initiate srst from swim.
1846 nrst is pulled low for 50us.
1848 static int stlink_swim_generate_rst(void *handle
)
1850 struct stlink_usb_handle_s
*h
= handle
;
1853 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1854 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1855 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1856 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1857 if (res
!= ERROR_OK
)
1863 send resynchronize sequence
1864 swim is pulled low for 16us
1865 reply is 64 clks low
1867 static int stlink_swim_resync(void *handle
)
1869 struct stlink_usb_handle_s
*h
= handle
;
1872 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1873 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1874 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1875 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1876 if (res
!= ERROR_OK
)
1881 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1883 struct stlink_usb_handle_s
*h
= handle
;
1886 unsigned int datalen
= 0;
1887 int cmdsize
= STLINK_CMD_SIZE_V2
;
1889 if (len
> STLINK_SWIM_DATA_SIZE
)
1892 if (h
->version
.stlink
== 1)
1893 cmdsize
= STLINK_SG_SIZE
;
1895 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1896 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1897 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1898 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1900 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1902 for (i
= 0; i
< len
; i
++) {
1903 if (h
->cmdidx
== cmdsize
)
1904 h
->databuf
[datalen
++] = *(data
++);
1906 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1908 if (h
->version
.stlink
== 1)
1909 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1911 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1912 if (res
!= ERROR_OK
)
1917 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1919 struct stlink_usb_handle_s
*h
= handle
;
1922 if (len
> STLINK_SWIM_DATA_SIZE
)
1925 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1926 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1927 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1928 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1930 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1932 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1933 if (res
!= ERROR_OK
)
1936 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1937 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1938 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1939 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1940 if (res
!= ERROR_OK
)
1947 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1950 struct stlink_usb_handle_s
*h
= handle
;
1954 /* there is no swim read core id cmd */
1955 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1960 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1962 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1963 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1964 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1966 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1969 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1971 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1975 if (res
!= ERROR_OK
)
1978 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1980 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1985 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1987 struct stlink_usb_handle_s
*h
= handle
;
1992 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1994 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1995 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1996 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1999 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2000 if (res
!= ERROR_OK
)
2003 *val
= le_to_h_u32(h
->databuf
+ 4);
2007 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
2009 struct stlink_usb_handle_s
*h
= handle
;
2013 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2015 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2016 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2017 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
2019 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
2020 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2022 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2025 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2029 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
2031 struct stlink_usb_handle_s
*h
= handle
;
2035 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
2038 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2040 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2041 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
2043 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2044 if (res
!= ERROR_OK
)
2047 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
2048 *size
= bytes_avail
< *size
? bytes_avail
: *size
;
2051 res
= stlink_usb_read_trace(handle
, buf
, *size
);
2052 if (res
!= ERROR_OK
)
2061 static enum target_state
stlink_usb_v2_get_status(void *handle
)
2066 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
2067 if (result
!= ERROR_OK
)
2068 return TARGET_UNKNOWN
;
2070 if (status
& S_HALT
)
2071 return TARGET_HALTED
;
2072 else if (status
& S_RESET_ST
)
2073 return TARGET_RESET
;
2075 return TARGET_RUNNING
;
2079 static enum target_state
stlink_usb_state(void *handle
)
2082 struct stlink_usb_handle_s
*h
= handle
;
2086 if (h
->reconnect_pending
) {
2087 LOG_INFO("Previous state query failed, trying to reconnect");
2088 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
2089 if (res
!= ERROR_OK
)
2090 return TARGET_UNKNOWN
;
2092 h
->reconnect_pending
= false;
2095 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2096 res
= stlink_usb_v2_get_status(handle
);
2097 if (res
== TARGET_UNKNOWN
)
2098 h
->reconnect_pending
= true;
2102 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2104 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2105 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
2107 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2109 if (res
!= ERROR_OK
)
2110 return TARGET_UNKNOWN
;
2112 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
2113 return TARGET_RUNNING
;
2114 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
2115 return TARGET_HALTED
;
2117 h
->reconnect_pending
= true;
2119 return TARGET_UNKNOWN
;
2122 static int stlink_usb_assert_srst(void *handle
, int srst
)
2124 struct stlink_usb_handle_s
*h
= handle
;
2128 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
2129 return stlink_swim_assert_reset(handle
, srst
);
2131 if (h
->version
.stlink
== 1)
2132 return ERROR_COMMAND_NOTFOUND
;
2134 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2136 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2137 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
2138 h
->cmdbuf
[h
->cmdidx
++] = srst
;
2140 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2144 static void stlink_usb_trace_disable(void *handle
)
2147 struct stlink_usb_handle_s
*h
= handle
;
2151 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
2153 LOG_DEBUG("Tracing: disable");
2155 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2156 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2157 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
2158 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2160 if (res
== ERROR_OK
)
2161 h
->trace
.enabled
= false;
2166 static int stlink_usb_trace_enable(void *handle
)
2169 struct stlink_usb_handle_s
*h
= handle
;
2173 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
2174 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2176 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2177 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
2178 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
2180 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
2183 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2185 if (res
== ERROR_OK
) {
2186 h
->trace
.enabled
= true;
2187 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
2190 LOG_ERROR("Tracing is not supported by this version.");
2198 static int stlink_usb_reset(void *handle
)
2200 struct stlink_usb_handle_s
*h
= handle
;
2205 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2207 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2209 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
2212 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
2214 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2215 if (retval
!= ERROR_OK
)
2218 if (h
->trace
.enabled
) {
2219 stlink_usb_trace_disable(h
);
2220 return stlink_usb_trace_enable(h
);
2227 static int stlink_usb_run(void *handle
)
2230 struct stlink_usb_handle_s
*h
= handle
;
2234 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2235 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
2240 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2242 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2243 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
2245 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2249 static int stlink_usb_halt(void *handle
)
2252 struct stlink_usb_handle_s
*h
= handle
;
2256 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2257 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2262 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2264 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2265 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
2267 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2271 static int stlink_usb_step(void *handle
)
2273 struct stlink_usb_handle_s
*h
= handle
;
2277 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2278 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
2279 * that the Cortex-M3 currently does. */
2280 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
2281 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
2282 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2285 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2287 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2288 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
2290 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2294 static int stlink_usb_read_regs(void *handle
)
2297 struct stlink_usb_handle_s
*h
= handle
;
2301 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
2303 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2304 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2306 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
2307 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
2308 /* regs data from offset 0 */
2310 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
2311 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
2312 /* status at offset 0, regs data from offset 4 */
2319 static int stlink_usb_read_reg(void *handle
, unsigned int regsel
, uint32_t *val
)
2322 struct stlink_usb_handle_s
*h
= handle
;
2326 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2327 res
= stlink_usb_write_debug_reg(h
, DCB_DCRSR
, regsel
& 0x7f);
2328 if (res
!= ERROR_OK
)
2331 /* FIXME: poll DHCSR.S_REGRDY before read DCRDR */
2332 return stlink_usb_v2_read_debug_reg(h
, DCB_DCRDR
, val
);
2335 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2337 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2338 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2339 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2341 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2342 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2344 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2345 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2346 if (res
!= ERROR_OK
)
2348 *val
= le_to_h_u32(h
->databuf
);
2351 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2352 if (res
!= ERROR_OK
)
2354 *val
= le_to_h_u32(h
->databuf
+ 4);
2360 static int stlink_usb_write_reg(void *handle
, unsigned int regsel
, uint32_t val
)
2362 struct stlink_usb_handle_s
*h
= handle
;
2366 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2367 int res
= stlink_usb_write_debug_reg(h
, DCB_DCRDR
, val
);
2368 if (res
!= ERROR_OK
)
2371 return stlink_usb_write_debug_reg(h
, DCB_DCRSR
, DCRSR_WNR
| (regsel
& 0x7f));
2372 /* FIXME: poll DHCSR.S_REGRDY after write DCRSR */
2375 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2377 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2378 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2379 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2381 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2382 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2383 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2386 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2389 static int stlink_usb_get_rw_status(void *handle
)
2391 struct stlink_usb_handle_s
*h
= handle
;
2395 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2398 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2400 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2401 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2402 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2403 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2405 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2406 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2411 static int stlink_usb_read_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2412 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2415 uint16_t read_len
= len
;
2416 struct stlink_usb_handle_s
*h
= handle
;
2420 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2421 return ERROR_COMMAND_NOTFOUND
;
2423 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2424 if (len
> stlink_usb_block(h
)) {
2425 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2429 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2431 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2432 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2433 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2435 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2437 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2438 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2441 /* we need to fix read length for single bytes */
2445 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2447 if (res
!= ERROR_OK
)
2450 memcpy(buffer
, h
->databuf
, len
);
2452 return stlink_usb_get_rw_status(handle
);
2456 static int stlink_usb_write_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2457 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2460 struct stlink_usb_handle_s
*h
= handle
;
2464 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2465 return ERROR_COMMAND_NOTFOUND
;
2467 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2468 if (len
> stlink_usb_block(h
)) {
2469 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2473 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2475 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2476 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2477 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2479 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2481 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2482 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2485 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2487 if (res
!= ERROR_OK
)
2490 return stlink_usb_get_rw_status(handle
);
2494 static int stlink_usb_read_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2495 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2498 struct stlink_usb_handle_s
*h
= handle
;
2502 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2503 return ERROR_COMMAND_NOTFOUND
;
2505 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2506 return ERROR_COMMAND_NOTFOUND
;
2508 if (len
> STLINK_MAX_RW16_32
) {
2509 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2513 /* data must be a multiple of 2 and half-word aligned */
2514 if (len
% 2 || addr
% 2) {
2515 LOG_DEBUG("Invalid data alignment");
2516 return ERROR_TARGET_UNALIGNED_ACCESS
;
2519 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2521 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2522 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2523 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2525 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2527 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2528 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2531 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2533 if (res
!= ERROR_OK
)
2536 memcpy(buffer
, h
->databuf
, len
);
2538 return stlink_usb_get_rw_status(handle
);
2542 static int stlink_usb_write_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2543 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2546 struct stlink_usb_handle_s
*h
= handle
;
2550 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2551 return ERROR_COMMAND_NOTFOUND
;
2553 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2554 return ERROR_COMMAND_NOTFOUND
;
2556 if (len
> STLINK_MAX_RW16_32
) {
2557 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2561 /* data must be a multiple of 2 and half-word aligned */
2562 if (len
% 2 || addr
% 2) {
2563 LOG_DEBUG("Invalid data alignment");
2564 return ERROR_TARGET_UNALIGNED_ACCESS
;
2567 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2569 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2570 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2571 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2573 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2575 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2576 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2579 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2581 if (res
!= ERROR_OK
)
2584 return stlink_usb_get_rw_status(handle
);
2588 static int stlink_usb_read_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2589 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2592 struct stlink_usb_handle_s
*h
= handle
;
2596 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2597 return ERROR_COMMAND_NOTFOUND
;
2599 if (len
> STLINK_MAX_RW16_32
) {
2600 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2604 /* data must be a multiple of 4 and word aligned */
2605 if (len
% 4 || addr
% 4) {
2606 LOG_DEBUG("Invalid data alignment");
2607 return ERROR_TARGET_UNALIGNED_ACCESS
;
2610 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2612 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2613 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2614 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2616 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2618 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2619 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2622 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2624 if (res
!= ERROR_OK
)
2627 memcpy(buffer
, h
->databuf
, len
);
2629 return stlink_usb_get_rw_status(handle
);
2633 static int stlink_usb_write_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2634 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2637 struct stlink_usb_handle_s
*h
= handle
;
2641 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2642 return ERROR_COMMAND_NOTFOUND
;
2644 if (len
> STLINK_MAX_RW16_32
) {
2645 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2649 /* data must be a multiple of 4 and word aligned */
2650 if (len
% 4 || addr
% 4) {
2651 LOG_DEBUG("Invalid data alignment");
2652 return ERROR_TARGET_UNALIGNED_ACCESS
;
2655 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2657 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2658 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2659 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2661 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2663 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2664 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2667 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2669 if (res
!= ERROR_OK
)
2672 return stlink_usb_get_rw_status(handle
);
2675 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2677 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2678 if (max_tar_block
== 0)
2680 return max_tar_block
;
2683 static int stlink_usb_read_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2684 uint32_t addr
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2686 int retval
= ERROR_OK
;
2687 uint32_t bytes_remaining
;
2689 struct stlink_usb_handle_s
*h
= handle
;
2691 /* calculate byte count */
2694 /* switch to 8 bit if stlink does not support 16 bit memory read */
2695 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2699 bytes_remaining
= (size
!= 1) ?
2700 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2702 if (count
< bytes_remaining
)
2703 bytes_remaining
= count
;
2706 * all stlink support 8/32bit memory read/writes and only from
2707 * stlink V2J26 there is support for 16 bit memory read/write.
2708 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2712 /* When in jtag mode the stlink uses the auto-increment functionality.
2713 * However it expects us to pass the data correctly, this includes
2714 * alignment and any page boundaries. We already do this as part of the
2715 * adi_v5 implementation, but the stlink is a hla adapter and so this
2716 * needs implementing manually.
2717 * currently this only affects jtag mode, according to ST they do single
2718 * access in SWD mode - but this may change and so we do it for both modes */
2720 /* we first need to check for any unaligned bytes */
2721 if (addr
& (size
- 1)) {
2722 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2723 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2724 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2725 usleep((1 << retries
++) * 1000);
2728 if (retval
!= ERROR_OK
)
2730 buffer
+= head_bytes
;
2732 count
-= head_bytes
;
2733 bytes_remaining
-= head_bytes
;
2736 if (bytes_remaining
& (size
- 1))
2737 retval
= stlink_usb_read_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2739 retval
= stlink_usb_read_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2741 retval
= stlink_usb_read_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2743 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2746 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2747 usleep((1 << retries
++) * 1000);
2750 if (retval
!= ERROR_OK
)
2753 buffer
+= bytes_remaining
;
2754 addr
+= bytes_remaining
;
2755 count
-= bytes_remaining
;
2761 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2762 uint32_t count
, uint8_t *buffer
)
2764 return stlink_usb_read_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2765 addr
, size
, count
, buffer
);
2768 static int stlink_usb_write_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2769 uint32_t addr
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2771 int retval
= ERROR_OK
;
2772 uint32_t bytes_remaining
;
2774 struct stlink_usb_handle_s
*h
= handle
;
2776 /* calculate byte count */
2779 /* switch to 8 bit if stlink does not support 16 bit memory read */
2780 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2785 bytes_remaining
= (size
!= 1) ?
2786 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2788 if (count
< bytes_remaining
)
2789 bytes_remaining
= count
;
2792 * all stlink support 8/32bit memory read/writes and only from
2793 * stlink V2J26 there is support for 16 bit memory read/write.
2794 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2799 /* When in jtag mode the stlink uses the auto-increment functionality.
2800 * However it expects us to pass the data correctly, this includes
2801 * alignment and any page boundaries. We already do this as part of the
2802 * adi_v5 implementation, but the stlink is a hla adapter and so this
2803 * needs implementing manually.
2804 * currently this only affects jtag mode, according to ST they do single
2805 * access in SWD mode - but this may change and so we do it for both modes */
2807 /* we first need to check for any unaligned bytes */
2808 if (addr
& (size
- 1)) {
2810 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2811 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2812 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2813 usleep((1<<retries
++) * 1000);
2816 if (retval
!= ERROR_OK
)
2818 buffer
+= head_bytes
;
2820 count
-= head_bytes
;
2821 bytes_remaining
-= head_bytes
;
2824 if (bytes_remaining
& (size
- 1))
2825 retval
= stlink_usb_write_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2827 retval
= stlink_usb_write_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2829 retval
= stlink_usb_write_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2832 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2833 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2834 usleep((1<<retries
++) * 1000);
2837 if (retval
!= ERROR_OK
)
2840 buffer
+= bytes_remaining
;
2841 addr
+= bytes_remaining
;
2842 count
-= bytes_remaining
;
2848 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2849 uint32_t count
, const uint8_t *buffer
)
2851 return stlink_usb_write_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2852 addr
, size
, count
, buffer
);
2856 static int stlink_usb_override_target(const char *targetname
)
2858 return !strcmp(targetname
, "cortex_m");
2861 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2866 we only have low and high speed...
2867 before changing speed the SWIM_CSR HS bit
2871 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2872 if (retval
!= ERROR_OK
)
2873 LOG_ERROR("Unable to set adapter speed");
2876 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2879 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2882 int speed_index
= -1;
2883 int speed_diff
= INT_MAX
;
2884 int last_valid_speed
= -1;
2887 for (i
= 0; i
< map_size
; i
++) {
2890 last_valid_speed
= i
;
2891 if (khz
== map
[i
].speed
) {
2895 int current_diff
= khz
- map
[i
].speed
;
2896 /* get abs value for comparison */
2897 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2898 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2899 speed_diff
= current_diff
;
2905 if (speed_index
== -1) {
2906 /* this will only be here if we cannot match the slow speed.
2907 * use the slowest speed we support.*/
2908 speed_index
= last_valid_speed
;
2910 } else if (i
== map_size
)
2913 if (!match
&& query
) {
2914 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
2915 khz
, map
[speed_index
].speed
);
2921 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2924 struct stlink_usb_handle_s
*h
= handle
;
2926 /* old firmware cannot change it */
2927 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2930 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2931 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2934 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2935 if (result
!= ERROR_OK
) {
2936 LOG_ERROR("Unable to set adapter speed");
2941 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2944 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2947 struct stlink_usb_handle_s
*h
= handle
;
2949 /* old firmware cannot change it */
2950 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2953 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2954 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2957 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2958 if (result
!= ERROR_OK
) {
2959 LOG_ERROR("Unable to set adapter speed");
2964 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2967 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2971 LOG_DEBUG("Supported clock speeds are:");
2972 for (i
= 0; i
< map_size
; i
++)
2974 LOG_DEBUG("%d kHz", map
[i
].speed
);
2977 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2979 struct stlink_usb_handle_s
*h
= handle
;
2982 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2983 LOG_ERROR("Unknown command");
2987 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2989 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2990 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2991 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2993 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2995 int size
= h
->databuf
[8];
2997 if (size
> STLINK_V3_MAX_FREQ_NB
)
2998 size
= STLINK_V3_MAX_FREQ_NB
;
3000 for (i
= 0; i
< size
; i
++) {
3001 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
3002 map
[i
].speed_divisor
= i
;
3005 /* set to zero all the next entries */
3006 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
3012 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
3014 struct stlink_usb_handle_s
*h
= handle
;
3016 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3017 LOG_ERROR("Unknown command");
3021 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3023 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3024 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
3025 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3026 h
->cmdbuf
[h
->cmdidx
++] = 0;
3028 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
3030 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3033 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
3035 struct stlink_usb_handle_s
*h
= handle
;
3037 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
3039 stlink_get_com_freq(h
, is_jtag
, map
);
3041 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
3044 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
3045 if (result
!= ERROR_OK
) {
3046 LOG_ERROR("Unable to set adapter speed");
3050 return map
[speed_index
].speed
;
3053 static int stlink_speed(void *handle
, int khz
, bool query
)
3055 struct stlink_usb_handle_s
*h
= handle
;
3060 switch (h
->st_mode
) {
3061 case STLINK_MODE_DEBUG_SWIM
:
3062 return stlink_speed_swim(handle
, khz
, query
);
3063 case STLINK_MODE_DEBUG_SWD
:
3064 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
3065 return stlink_speed_v3(handle
, false, khz
, query
);
3067 return stlink_speed_swd(handle
, khz
, query
);
3069 case STLINK_MODE_DEBUG_JTAG
:
3070 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
3071 return stlink_speed_v3(handle
, true, khz
, query
);
3073 return stlink_speed_jtag(handle
, khz
, query
);
3083 static int stlink_usb_usb_close(void *handle
)
3085 struct stlink_usb_handle_s
*h
= handle
;
3090 if (h
->usb_backend_priv
.fd
) {
3091 stlink_usb_exit_mode(h
);
3092 /* do not check return code, it prevent
3093 us from closing jtag_libusb */
3094 jtag_libusb_close(h
->usb_backend_priv
.fd
);
3104 static int stlink_tcp_close(void *handle
)
3106 struct stlink_usb_handle_s
*h
= handle
;
3112 if (h
->tcp_backend_priv
.connected
) {
3113 if (h
->tcp_backend_priv
.connect_id
) {
3114 stlink_usb_exit_mode(h
);
3116 /* close the stlink */
3117 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_CLOSE_DEV
;
3118 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 4); /* reserved */
3119 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
3120 ret
= stlink_tcp_send_cmd(h
, 8, 4, true);
3121 if (ret
!= ERROR_OK
)
3122 LOG_ERROR("cannot close the STLINK");
3125 if (close_socket(h
->tcp_backend_priv
.fd
) != 0)
3126 LOG_ERROR("error closing the socket, errno: %s", strerror(errno
));
3129 free(h
->tcp_backend_priv
.send_buf
);
3130 free(h
->tcp_backend_priv
.recv_buf
);
3136 static int stlink_close(void *handle
)
3139 struct stlink_usb_handle_s
*h
= handle
;
3141 stlink_usb_close(handle
);