1 /***************************************************************************
2 * Copyright (C) 2020 by Tarek Bochkati *
3 * Tarek Bochkati <tarek.bouchkati@gmail.com> *
5 * SWIM contributions by Ake Rehnman *
6 * Copyright (C) 2017 Ake Rehnman *
7 * ake.rehnman(at)gmail.com *
9 * Copyright (C) 2011-2012 by Mathias Kuester *
10 * Mathias Kuester <kesmtp@freenet.de> *
12 * Copyright (C) 2012 by Spencer Oliver *
13 * spen@spen-soft.co.uk *
15 * This code is based on https://github.com/texane/stlink *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
29 ***************************************************************************/
35 /* project specific includes */
36 #include <helper/align.h>
37 #include <helper/binarybuffer.h>
38 #include <helper/bits.h>
39 #include <helper/system.h>
40 #include <jtag/interface.h>
41 #include <jtag/hla/hla_layout.h>
42 #include <jtag/hla/hla_transport.h>
43 #include <jtag/hla/hla_interface.h>
44 #include <jtag/swim.h>
45 #include <target/arm_adi_v5.h>
46 #include <target/target.h>
47 #include <transport/transport.h>
49 #include <target/cortex_m.h>
51 #include <helper/system.h>
53 #ifdef HAVE_ARPA_INET_H
54 #include <arpa/inet.h>
57 #ifdef HAVE_NETINET_TCP_H
58 #include <netinet/tcp.h>
61 #include "libusb_helper.h"
64 #define USE_LIBUSB_ASYNCIO
67 #define STLINK_SERIAL_LEN 24
69 #define ENDPOINT_IN 0x80
70 #define ENDPOINT_OUT 0x00
72 #define STLINK_WRITE_TIMEOUT 1000
73 #define STLINK_READ_TIMEOUT 1000
75 #define STLINK_RX_EP (1|ENDPOINT_IN)
76 #define STLINK_TX_EP (2|ENDPOINT_OUT)
77 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
79 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
80 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
82 #define STLINK_SG_SIZE (31)
83 #define STLINK_DATA_SIZE (6144)
84 #define STLINK_CMD_SIZE_V2 (16)
85 #define STLINK_CMD_SIZE_V1 (10)
87 #define STLINK_V1_PID (0x3744)
88 #define STLINK_V2_PID (0x3748)
89 #define STLINK_V2_1_PID (0x374B)
90 #define STLINK_V2_1_NO_MSD_PID (0x3752)
91 #define STLINK_V3_USBLOADER_PID (0x374D)
92 #define STLINK_V3E_PID (0x374E)
93 #define STLINK_V3S_PID (0x374F)
94 #define STLINK_V3_2VCP_PID (0x3753)
95 #define STLINK_V3E_NO_MSD_PID (0x3754)
98 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
99 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
100 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
102 * For 16 and 32bit read/writes stlink handles USB packet split and the limit
103 * is the internal buffer size of 6144 bytes.
104 * TODO: override ADIv5 layer's tar_autoincr_block that limits the transfer
105 * to 1024 or 4096 bytes
107 #define STLINK_MAX_RW8 (64)
108 #define STLINKV3_MAX_RW8 (512)
109 #define STLINK_MAX_RW16_32 STLINK_DATA_SIZE
110 #define STLINK_SWIM_DATA_SIZE STLINK_DATA_SIZE
112 /* "WAIT" responses will be retried (with exponential backoff) at
113 * most this many times before failing to caller.
115 #define MAX_WAIT_RETRIES 8
117 /* HLA is currently limited at AP#0 and no control on CSW */
118 #define STLINK_HLA_AP_NUM 0
119 #define STLINK_HLA_CSW 0
121 enum stlink_jtag_api_version
{
122 STLINK_JTAG_API_V1
= 1,
128 STLINK_MODE_UNKNOWN
= 0,
131 STLINK_MODE_DEBUG_JTAG
,
132 STLINK_MODE_DEBUG_SWD
,
133 STLINK_MODE_DEBUG_SWIM
137 struct stlink_usb_version
{
144 /** jtag api version supported */
145 enum stlink_jtag_api_version jtag_api
;
146 /** one bit for each feature supported. See macros STLINK_F_* */
150 struct stlink_usb_priv_s
{
152 struct libusb_device_handle
*fd
;
154 struct libusb_transfer
*trans
;
157 struct stlink_tcp_priv_s
{
172 struct stlink_backend_s
{
174 int (*open
)(void *handle
, struct hl_interface_param_s
*param
);
176 int (*close
)(void *handle
);
178 int (*xfer_noerrcheck
)(void *handle
, const uint8_t *buf
, int size
);
180 int (*read_trace
)(void *handle
, const uint8_t *buf
, int size
);
183 /* TODO: make queue size dynamic */
184 /* TODO: don't allocate queue for HLA */
185 #define MAX_QUEUE_DEPTH (4096)
195 * encode the bytes size in the enum's value. This makes easy to extract it
196 * with a simple logic AND, by using the macro CMD_MEM_AP_2_SIZE() below
198 CMD_MEM_AP_READ8
= 0x10 + 1,
199 CMD_MEM_AP_READ16
= 0x10 + 2,
200 CMD_MEM_AP_READ32
= 0x10 + 4,
202 CMD_MEM_AP_WRITE8
= 0x20 + 1,
203 CMD_MEM_AP_WRITE16
= 0x20 + 2,
204 CMD_MEM_AP_WRITE32
= 0x20 + 4,
207 #define CMD_MEM_AP_2_SIZE(cmd) ((cmd) & 7)
214 struct adiv5_dap
*dap
;
219 struct adiv5_dap
*dap
;
231 bool changes_csw_default
;
246 struct stlink_usb_handle_s
{
248 struct stlink_backend_s
*backend
;
251 struct stlink_usb_priv_s usb_backend_priv
;
252 struct stlink_tcp_priv_s tcp_backend_priv
;
269 uint32_t max_mem_packet
;
271 enum stlink_mode st_mode
;
273 struct stlink_usb_version version
;
280 /** whether SWO tracing is enabled or not */
282 /** trace module source clock */
285 /** reconnect is needed next time we try to query the
287 bool reconnect_pending
;
288 /** queue of dap_direct operations */
289 struct dap_queue queue
[MAX_QUEUE_DEPTH
];
290 /** first element available in the queue */
291 unsigned int queue_index
;
295 static inline int stlink_usb_open(void *handle
, struct hl_interface_param_s
*param
)
297 struct stlink_usb_handle_s
*h
= handle
;
298 return h
->backend
->open(handle
, param
);
302 static inline int stlink_usb_close(void *handle
)
304 struct stlink_usb_handle_s
*h
= handle
;
305 return h
->backend
->close(handle
);
308 static inline int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
310 struct stlink_usb_handle_s
*h
= handle
;
311 return h
->backend
->xfer_noerrcheck(handle
, buf
, size
);
314 #define STLINK_SWIM_ERR_OK 0x00
315 #define STLINK_SWIM_BUSY 0x01
316 #define STLINK_DEBUG_ERR_OK 0x80
317 #define STLINK_DEBUG_ERR_FAULT 0x81
318 #define STLINK_SWD_AP_WAIT 0x10
319 #define STLINK_SWD_AP_FAULT 0x11
320 #define STLINK_SWD_AP_ERROR 0x12
321 #define STLINK_SWD_AP_PARITY_ERROR 0x13
322 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
323 #define STLINK_JTAG_WRITE_ERROR 0x0c
324 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
325 #define STLINK_SWD_DP_WAIT 0x14
326 #define STLINK_SWD_DP_FAULT 0x15
327 #define STLINK_SWD_DP_ERROR 0x16
328 #define STLINK_SWD_DP_PARITY_ERROR 0x17
330 #define STLINK_SWD_AP_WDATA_ERROR 0x18
331 #define STLINK_SWD_AP_STICKY_ERROR 0x19
332 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
334 #define STLINK_BAD_AP_ERROR 0x1d
336 #define STLINK_CORE_RUNNING 0x80
337 #define STLINK_CORE_HALTED 0x81
338 #define STLINK_CORE_STAT_UNKNOWN -1
340 #define STLINK_GET_VERSION 0xF1
341 #define STLINK_DEBUG_COMMAND 0xF2
342 #define STLINK_DFU_COMMAND 0xF3
343 #define STLINK_SWIM_COMMAND 0xF4
344 #define STLINK_GET_CURRENT_MODE 0xF5
345 #define STLINK_GET_TARGET_VOLTAGE 0xF7
347 #define STLINK_DEV_DFU_MODE 0x00
348 #define STLINK_DEV_MASS_MODE 0x01
349 #define STLINK_DEV_DEBUG_MODE 0x02
350 #define STLINK_DEV_SWIM_MODE 0x03
351 #define STLINK_DEV_BOOTLOADER_MODE 0x04
352 #define STLINK_DEV_UNKNOWN_MODE -1
354 #define STLINK_DFU_EXIT 0x07
357 STLINK_SWIM_ENTER_SEQ
358 1.3ms low then 750Hz then 1.5kHz
361 STM8 DM pulls reset pin low 50us
364 uint8_t (0=low|1=high)
371 send synchronization seq (16us low, response 64 clocks low)
373 #define STLINK_SWIM_ENTER 0x00
374 #define STLINK_SWIM_EXIT 0x01
375 #define STLINK_SWIM_READ_CAP 0x02
376 #define STLINK_SWIM_SPEED 0x03
377 #define STLINK_SWIM_ENTER_SEQ 0x04
378 #define STLINK_SWIM_GEN_RST 0x05
379 #define STLINK_SWIM_RESET 0x06
380 #define STLINK_SWIM_ASSERT_RESET 0x07
381 #define STLINK_SWIM_DEASSERT_RESET 0x08
382 #define STLINK_SWIM_READSTATUS 0x09
383 #define STLINK_SWIM_WRITEMEM 0x0a
384 #define STLINK_SWIM_READMEM 0x0b
385 #define STLINK_SWIM_READBUF 0x0c
387 #define STLINK_DEBUG_GETSTATUS 0x01
388 #define STLINK_DEBUG_FORCEDEBUG 0x02
389 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
390 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
391 #define STLINK_DEBUG_APIV1_READREG 0x05
392 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
393 #define STLINK_DEBUG_READMEM_32BIT 0x07
394 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
395 #define STLINK_DEBUG_RUNCORE 0x09
396 #define STLINK_DEBUG_STEPCORE 0x0a
397 #define STLINK_DEBUG_APIV1_SETFP 0x0b
398 #define STLINK_DEBUG_READMEM_8BIT 0x0c
399 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
400 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
401 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
402 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
404 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
405 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
406 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
408 #define STLINK_DEBUG_APIV1_ENTER 0x20
409 #define STLINK_DEBUG_EXIT 0x21
410 #define STLINK_DEBUG_READCOREID 0x22
412 #define STLINK_DEBUG_APIV2_ENTER 0x30
413 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
414 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
415 #define STLINK_DEBUG_APIV2_READREG 0x33
416 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
417 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
418 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
420 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
421 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
422 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
424 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
426 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
427 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
428 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
429 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
430 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
431 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
432 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
433 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
434 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
436 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
437 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
439 #define STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC 0x50
440 #define STLINK_DEBUG_APIV2_RW_MISC_OUT 0x51
441 #define STLINK_DEBUG_APIV2_RW_MISC_IN 0x52
443 #define STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC 0x54
445 #define STLINK_APIV3_SET_COM_FREQ 0x61
446 #define STLINK_APIV3_GET_COM_FREQ 0x62
448 #define STLINK_APIV3_GET_VERSION_EX 0xFB
450 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
451 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
452 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
454 #define STLINK_DEBUG_PORT_ACCESS 0xffff
456 #define STLINK_TRACE_SIZE 4096
457 #define STLINK_TRACE_MAX_HZ 2000000
458 #define STLINK_V3_TRACE_MAX_HZ 24000000
460 #define STLINK_V3_MAX_FREQ_NB 10
462 #define REQUEST_SENSE 0x03
463 #define REQUEST_SENSE_LENGTH 18
465 /* STLINK TCP commands */
466 #define STLINK_TCP_CMD_REFRESH_DEVICE_LIST 0x00
467 #define STLINK_TCP_CMD_GET_NB_DEV 0x01
468 #define STLINK_TCP_CMD_GET_DEV_INFO 0x02
469 #define STLINK_TCP_CMD_OPEN_DEV 0x03
470 #define STLINK_TCP_CMD_CLOSE_DEV 0x04
471 #define STLINK_TCP_CMD_SEND_USB_CMD 0x05
472 #define STLINK_TCP_CMD_GET_SERVER_VERSION 0x06
473 #define STLINK_TCP_CMD_GET_NB_OF_DEV_CLIENTS 0x07
475 /* STLINK TCP constants */
476 #define OPENOCD_STLINK_TCP_API_VERSION 1
477 #define STLINK_TCP_REQUEST_WRITE 0
478 #define STLINK_TCP_REQUEST_READ 1
479 #define STLINK_TCP_REQUEST_READ_SWO 3
480 #define STLINK_TCP_SS_SIZE 4
481 #define STLINK_TCP_USB_CMD_SIZE 32
482 #define STLINK_TCP_SERIAL_SIZE 32
483 #define STLINK_TCP_SEND_BUFFER_SIZE 10240
484 #define STLINK_TCP_RECV_BUFFER_SIZE 10240
486 /* STLINK TCP command status */
487 #define STLINK_TCP_SS_OK 0x00000001
488 #define STLINK_TCP_SS_MEMORY_PROBLEM 0x00001000
489 #define STLINK_TCP_SS_TIMEOUT 0x00001001
490 #define STLINK_TCP_SS_BAD_PARAMETER 0x00001002
491 #define STLINK_TCP_SS_OPEN_ERR 0x00001003
492 #define STLINK_TCP_SS_TRUNCATED_DATA 0x00001052
493 #define STLINK_TCP_SS_CMD_NOT_AVAILABLE 0x00001053
494 #define STLINK_TCP_SS_TCP_ERROR 0x00002001
495 #define STLINK_TCP_SS_TCP_CANT_CONNECT 0x00002002
496 #define STLINK_TCP_SS_WIN32_ERROR 0x00010000
499 * Map the relevant features, quirks and workaround for specific firmware
502 #define STLINK_F_HAS_TRACE BIT(0) /* v2>=j13 || v3 */
503 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(1) /* v2>=j15 || v3 */
504 #define STLINK_F_HAS_SWD_SET_FREQ BIT(2) /* v2>=j22 */
505 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(3) /* v2>=j24 */
506 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(4) /* v2>=j24 && v2<j32 */
507 #define STLINK_F_HAS_DAP_REG BIT(5) /* v2>=j24 || v3 */
508 #define STLINK_F_HAS_MEM_16BIT BIT(6) /* v2>=j26 || v3 */
509 #define STLINK_F_HAS_AP_INIT BIT(7) /* v2>=j28 || v3 */
510 #define STLINK_F_FIX_CLOSE_AP BIT(8) /* v2>=j29 || v3 */
511 #define STLINK_F_HAS_DPBANKSEL BIT(9) /* v2>=j32 || v3>=j2 */
512 #define STLINK_F_HAS_RW8_512BYTES BIT(10) /* v3>=j6 */
515 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
516 #define STLINK_F_HAS_FPU_REG STLINK_F_HAS_GETLASTRWSTATUS2
517 #define STLINK_F_HAS_MEM_WR_NO_INC STLINK_F_HAS_MEM_16BIT
518 #define STLINK_F_HAS_MEM_RD_NO_INC STLINK_F_HAS_DPBANKSEL
519 #define STLINK_F_HAS_RW_MISC STLINK_F_HAS_DPBANKSEL
520 #define STLINK_F_HAS_CSW STLINK_F_HAS_DPBANKSEL
522 #define STLINK_REGSEL_IS_FPU(x) ((x) > 0x1F)
529 /* SWD clock speed */
530 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
532 {1800, 1}, /* default */
545 /* JTAG clock speed */
546 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
550 {1125, 32}, /* default */
556 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
557 static int stlink_swim_status(void *handle
);
558 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
559 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
560 static int stlink_speed(void *handle
, int khz
, bool query
);
561 static int stlink_usb_open_ap(void *handle
, unsigned short apsel
);
564 static unsigned int stlink_usb_block(void *handle
)
566 struct stlink_usb_handle_s
*h
= handle
;
570 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
571 return STLINKV3_MAX_RW8
;
573 return STLINK_MAX_RW8
;
576 #ifdef USE_LIBUSB_ASYNCIO
578 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
580 int *completed
= transfer
->user_data
;
582 /* caller interprets result and frees transfer */
586 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
588 int r
, *completed
= transfer
->user_data
;
590 while (!*completed
) {
591 r
= jtag_libusb_handle_events_completed(completed
);
593 if (r
== LIBUSB_ERROR_INTERRUPTED
)
595 libusb_cancel_transfer(transfer
);
602 static int transfer_error_status(const struct libusb_transfer
*transfer
)
606 switch (transfer
->status
) {
607 case LIBUSB_TRANSFER_COMPLETED
:
610 case LIBUSB_TRANSFER_TIMED_OUT
:
611 r
= LIBUSB_ERROR_TIMEOUT
;
613 case LIBUSB_TRANSFER_STALL
:
614 r
= LIBUSB_ERROR_PIPE
;
616 case LIBUSB_TRANSFER_OVERFLOW
:
617 r
= LIBUSB_ERROR_OVERFLOW
;
619 case LIBUSB_TRANSFER_NO_DEVICE
:
620 r
= LIBUSB_ERROR_NO_DEVICE
;
622 case LIBUSB_TRANSFER_ERROR
:
623 case LIBUSB_TRANSFER_CANCELLED
:
627 r
= LIBUSB_ERROR_OTHER
;
641 size_t transfer_size
;
642 struct libusb_transfer
*transfer
;
645 static int jtag_libusb_bulk_transfer_n(
646 struct libusb_device_handle
*dev_handle
,
647 struct jtag_xfer
*transfers
,
652 int returnval
= ERROR_OK
;
655 for (size_t i
= 0; i
< n_transfers
; ++i
) {
656 transfers
[i
].retval
= 0;
657 transfers
[i
].completed
= 0;
658 transfers
[i
].transfer_size
= 0;
659 transfers
[i
].transfer
= libusb_alloc_transfer(0);
661 if (!transfers
[i
].transfer
) {
662 for (size_t j
= 0; j
< i
; ++j
)
663 libusb_free_transfer(transfers
[j
].transfer
);
665 LOG_DEBUG("ERROR, failed to alloc usb transfers");
666 for (size_t k
= 0; k
< n_transfers
; ++k
)
667 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
672 for (size_t i
= 0; i
< n_transfers
; ++i
) {
673 libusb_fill_bulk_transfer(
674 transfers
[i
].transfer
,
676 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
677 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
678 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
680 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
682 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
684 /* Probably no point continuing to submit transfers once a submission fails.
685 * As a result, tag all remaining transfers as errors.
687 for (size_t j
= i
; j
< n_transfers
; ++j
)
688 transfers
[j
].retval
= retval
;
690 returnval
= ERROR_FAIL
;
695 /* Wait for every submitted USB transfer to complete.
697 for (size_t i
= 0; i
< n_transfers
; ++i
) {
698 if (transfers
[i
].retval
== 0) {
699 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
701 retval
= transfer_error_status(transfers
[i
].transfer
);
703 returnval
= ERROR_FAIL
;
704 transfers
[i
].retval
= retval
;
705 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
707 /* Assuming actual_length is only valid if there is no transfer error.
709 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
713 libusb_free_transfer(transfers
[i
].transfer
);
714 transfers
[i
].transfer
= NULL
;
724 static int stlink_usb_xfer_v1_get_status(void *handle
)
726 struct stlink_usb_handle_s
*h
= handle
;
732 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
734 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
735 STLINK_READ_TIMEOUT
, &tr
);
741 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
744 if (t1
!= 0x53425355)
752 if (h
->cmdbuf
[12] != 0)
758 #ifdef USE_LIBUSB_ASYNCIO
759 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
761 struct stlink_usb_handle_s
*h
= handle
;
765 size_t n_transfers
= 0;
766 struct jtag_xfer transfers
[2];
768 memset(transfers
, 0, sizeof(transfers
));
770 transfers
[0].ep
= h
->tx_ep
;
771 transfers
[0].buf
= h
->cmdbuf
;
772 transfers
[0].size
= cmdsize
;
776 if (h
->direction
== h
->tx_ep
&& size
) {
777 transfers
[1].ep
= h
->tx_ep
;
778 transfers
[1].buf
= (uint8_t *)buf
;
779 transfers
[1].size
= size
;
782 } else if (h
->direction
== h
->rx_ep
&& size
) {
783 transfers
[1].ep
= h
->rx_ep
;
784 transfers
[1].buf
= (uint8_t *)buf
;
785 transfers
[1].size
= size
;
790 return jtag_libusb_bulk_transfer_n(
791 h
->usb_backend_priv
.fd
,
794 STLINK_WRITE_TIMEOUT
);
797 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
799 struct stlink_usb_handle_s
*h
= handle
;
804 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
805 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
806 if (ret
|| tr
!= cmdsize
)
809 if (h
->direction
== h
->tx_ep
&& size
) {
810 ret
= jtag_libusb_bulk_write(h
->usb_backend_priv
.fd
, h
->tx_ep
, (char *)buf
,
811 size
, STLINK_WRITE_TIMEOUT
, &tr
);
812 if (ret
|| tr
!= size
) {
813 LOG_DEBUG("bulk write failed");
816 } else if (h
->direction
== h
->rx_ep
&& size
) {
817 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->rx_ep
, (char *)buf
,
818 size
, STLINK_READ_TIMEOUT
, &tr
);
819 if (ret
|| tr
!= size
) {
820 LOG_DEBUG("bulk read failed");
830 static int stlink_usb_xfer_v1_get_sense(void *handle
)
833 struct stlink_usb_handle_s
*h
= handle
;
837 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
839 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
840 h
->cmdbuf
[h
->cmdidx
++] = 0;
841 h
->cmdbuf
[h
->cmdidx
++] = 0;
842 h
->cmdbuf
[h
->cmdidx
++] = 0;
843 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
845 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
850 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
857 static int stlink_usb_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
859 struct stlink_usb_handle_s
*h
= handle
;
862 ret
= jtag_libusb_bulk_read(h
->usb_backend_priv
.fd
, h
->trace_ep
, (char *)buf
, size
,
863 STLINK_READ_TIMEOUT
, &tr
);
864 if (ret
|| tr
!= size
) {
865 LOG_ERROR("bulk trace read failed");
873 transfers block in cmdbuf
874 <size> indicates number of bytes in the following
876 Ignore the (eventual) error code in the received packet.
878 static int stlink_usb_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
880 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
881 struct stlink_usb_handle_s
*h
= handle
;
885 if (h
->version
.stlink
== 1) {
886 cmdsize
= STLINK_SG_SIZE
;
887 /* put length in bCBWCBLength */
888 h
->cmdbuf
[14] = h
->cmdidx
-15;
891 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
896 if (h
->version
.stlink
== 1) {
897 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
898 /* check csw status */
899 if (h
->cmdbuf
[12] == 1) {
900 LOG_DEBUG("get sense");
901 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
912 static int stlink_tcp_send_cmd(void *handle
, int send_size
, int recv_size
, bool check_tcp_status
)
914 struct stlink_usb_handle_s
*h
= handle
;
918 /* send the TCP command */
919 int sent_size
= send(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.send_buf
, send_size
, 0);
920 if (sent_size
!= send_size
) {
921 LOG_ERROR("failed to send USB CMD");
923 LOG_DEBUG("socket send error: %s (errno %d)", strerror(errno
), errno
);
925 LOG_DEBUG("sent size %d (expected %d)", sent_size
, send_size
);
931 /* read the TCP response */
932 int received_size
= recv(h
->tcp_backend_priv
.fd
, (void *)h
->tcp_backend_priv
.recv_buf
, recv_size
, 0);
933 if (received_size
!= recv_size
) {
934 LOG_ERROR("failed to receive USB CMD response");
935 if (received_size
== -1)
936 LOG_DEBUG("socket recv error: %s (errno %d)", strerror(errno
), errno
);
938 LOG_DEBUG("received size %d (expected %d)", received_size
, recv_size
);
942 if (check_tcp_status
) {
943 uint32_t tcp_ss
= le_to_h_u32(h
->tcp_backend_priv
.recv_buf
);
944 if (tcp_ss
!= STLINK_TCP_SS_OK
) {
945 LOG_ERROR("TCP error status 0x%X", tcp_ss
);
954 static int stlink_tcp_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
956 struct stlink_usb_handle_s
*h
= handle
;
958 int send_size
= STLINK_TCP_USB_CMD_SIZE
;
959 int recv_size
= STLINK_TCP_SS_SIZE
;
963 /* prepare the TCP command */
964 h
->tcp_backend_priv
.send_buf
[0] = STLINK_TCP_CMD_SEND_USB_CMD
;
965 memset(&h
->tcp_backend_priv
.send_buf
[1], 0, 3); /* reserved for alignment and future use, must be zero */
966 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[4], h
->tcp_backend_priv
.connect_id
);
967 /* tcp_backend_priv.send_buf[8..23] already contains the constructed stlink command */
968 h
->tcp_backend_priv
.send_buf
[24] = h
->direction
;
969 memset(&h
->tcp_backend_priv
.send_buf
[25], 0, 3); /* reserved for alignment and future use, must be zero */
971 h_u32_to_le(&h
->tcp_backend_priv
.send_buf
[28], size
);
974 * if the xfer is a write request (tx_ep)
975 * > then buf content will be copied
977 * else : the xfer is a read or trace read request (rx_ep or trace_ep)
978 * > the buf content will be filled from &databuf[4].
980 * note : if h->direction is trace_ep, h->cmdbuf is zeros.
983 if (h
->direction
== h
->tx_ep
) { /* STLINK_TCP_REQUEST_WRITE */
985 if (send_size
> STLINK_TCP_SEND_BUFFER_SIZE
) {
986 LOG_ERROR("STLINK_TCP command buffer overflow");
989 memcpy(&h
->tcp_backend_priv
.send_buf
[32], buf
, size
);
990 } else { /* STLINK_TCP_REQUEST_READ or STLINK_TCP_REQUEST_READ_SWO */
992 if (recv_size
> STLINK_TCP_RECV_BUFFER_SIZE
) {
993 LOG_ERROR("STLINK_TCP data buffer overflow");
998 int ret
= stlink_tcp_send_cmd(h
, send_size
, recv_size
, true);
1002 if (h
->direction
!= h
->tx_ep
) {
1003 /* the read data is located in tcp_backend_priv.recv_buf[4] */
1004 /* most of the case it will be copying the data from tcp_backend_priv.recv_buf[4]
1005 * to handle->cmd_buff which are the same, so let's avoid unnecessary copying */
1006 if (buf
!= &h
->tcp_backend_priv
.recv_buf
[4])
1007 memcpy((uint8_t *)buf
, &h
->tcp_backend_priv
.recv_buf
[4], size
);
1014 static int stlink_tcp_read_trace(void *handle
, const uint8_t *buf
, int size
)
1016 struct stlink_usb_handle_s
*h
= handle
;
1018 stlink_usb_init_buffer(h
, h
->trace_ep
, 0);
1019 return stlink_tcp_xfer_noerrcheck(handle
, buf
, size
);
1023 Converts an STLINK status code held in the first byte of a response
1024 to an openocd error, logs any error/wait status as debug output.
1026 static int stlink_usb_error_check(void *handle
)
1028 struct stlink_usb_handle_s
*h
= handle
;
1032 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1033 switch (h
->databuf
[0]) {
1034 case STLINK_SWIM_ERR_OK
:
1036 case STLINK_SWIM_BUSY
:
1039 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1044 /* TODO: no error checking yet on api V1 */
1045 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1046 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
1048 switch (h
->databuf
[0]) {
1049 case STLINK_DEBUG_ERR_OK
:
1051 case STLINK_DEBUG_ERR_FAULT
:
1052 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
1054 case STLINK_SWD_AP_WAIT
:
1055 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
1057 case STLINK_SWD_DP_WAIT
:
1058 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
1060 case STLINK_JTAG_GET_IDCODE_ERROR
:
1061 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
1063 case STLINK_JTAG_WRITE_ERROR
:
1064 LOG_DEBUG("Write error");
1066 case STLINK_JTAG_WRITE_VERIF_ERROR
:
1067 LOG_DEBUG("Write verify error, ignoring");
1069 case STLINK_SWD_AP_FAULT
:
1070 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
1071 * returns ERROR_OK with the comment:
1072 * Change in error status when reading outside RAM.
1073 * This fix allows CDT plugin to visualize memory.
1075 LOG_DEBUG("STLINK_SWD_AP_FAULT");
1077 case STLINK_SWD_AP_ERROR
:
1078 LOG_DEBUG("STLINK_SWD_AP_ERROR");
1080 case STLINK_SWD_AP_PARITY_ERROR
:
1081 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
1083 case STLINK_SWD_DP_FAULT
:
1084 LOG_DEBUG("STLINK_SWD_DP_FAULT");
1086 case STLINK_SWD_DP_ERROR
:
1087 LOG_DEBUG("STLINK_SWD_DP_ERROR");
1089 case STLINK_SWD_DP_PARITY_ERROR
:
1090 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
1092 case STLINK_SWD_AP_WDATA_ERROR
:
1093 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
1095 case STLINK_SWD_AP_STICKY_ERROR
:
1096 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
1098 case STLINK_SWD_AP_STICKYORUN_ERROR
:
1099 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
1101 case STLINK_BAD_AP_ERROR
:
1102 LOG_DEBUG("STLINK_BAD_AP_ERROR");
1105 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
1111 * Wrapper around stlink_usb_xfer_noerrcheck()
1112 * to check the error code in the received packet
1114 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
1120 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1121 if (retval
!= ERROR_OK
)
1124 return stlink_usb_error_check(handle
);
1127 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
1129 Works for commands where the STLINK_DEBUG status is returned in the first
1130 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
1132 Returns an openocd result code.
1134 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
1138 struct stlink_usb_handle_s
*h
= handle
;
1141 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
1142 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
1143 if (res
!= ERROR_OK
)
1147 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1148 res
= stlink_swim_status(handle
);
1149 if (res
!= ERROR_OK
)
1153 res
= stlink_usb_error_check(handle
);
1154 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1155 unsigned int delay_us
= (1<<retries
++) * 1000;
1156 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
1165 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
1167 struct stlink_usb_handle_s
*h
= handle
;
1171 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1173 return h
->backend
->read_trace(handle
, buf
, size
);
1177 this function writes transfer length in
1178 the right place in the cb
1180 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
1182 struct stlink_usb_handle_s
*h
= handle
;
1184 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
1187 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
1189 struct stlink_usb_handle_s
*h
= handle
;
1191 /* fill the send buffer */
1192 strcpy((char *)h
->cmdbuf
, "USBC");
1194 /* csw tag not used */
1195 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
1197 /* cbw data transfer length (in the following data phase in or out) */
1198 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
1201 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
1202 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
1203 /* cdb clength (is filled in at xfer) */
1204 h
->cmdbuf
[h
->cmdidx
++] = 0;
1208 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
1210 struct stlink_usb_handle_s
*h
= handle
;
1212 h
->direction
= direction
;
1216 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
1217 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
1219 if (h
->version
.stlink
== 1)
1220 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
1224 static int stlink_usb_version(void *handle
)
1229 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
1230 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
1232 struct stlink_usb_handle_s
*h
= handle
;
1236 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
1238 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
1240 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
1242 if (res
!= ERROR_OK
)
1245 version
= be_to_h_u16(h
->databuf
);
1246 v
= (version
>> 12) & 0x0f;
1247 x
= (version
>> 6) & 0x3f;
1250 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
1251 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
1254 case STLINK_V2_1_PID
:
1255 case STLINK_V2_1_NO_MSD_PID
:
1256 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
1257 /* MxSy : STM8 V2.1 - SWIM only */
1262 /* JxMy : STM32 V2.1 - JTAG/SWD only */
1275 /* STLINK-V3 requires a specific command */
1276 if (v
== 3 && x
== 0 && y
== 0) {
1277 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
1279 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
1281 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
1282 if (res
!= ERROR_OK
)
1286 swim
= h
->databuf
[1];
1287 jtag
= h
->databuf
[2];
1288 msd
= h
->databuf
[3];
1289 bridge
= h
->databuf
[4];
1290 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
1291 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
1294 h
->version
.stlink
= v
;
1295 h
->version
.jtag
= jtag
;
1296 h
->version
.swim
= swim
;
1299 switch (h
->version
.stlink
) {
1301 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
1302 if (h
->version
.jtag
>= 11)
1303 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1305 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
1309 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
1310 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1312 /* API for trace from J13 */
1313 /* API for target voltage from J13 */
1314 if (h
->version
.jtag
>= 13)
1315 flags
|= STLINK_F_HAS_TRACE
;
1317 /* preferred API to get last R/W status from J15 */
1318 if (h
->version
.jtag
>= 15)
1319 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1321 /* API to set SWD frequency from J22 */
1322 if (h
->version
.jtag
>= 22)
1323 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1325 /* API to set JTAG frequency from J24 */
1326 /* API to access DAP registers from J24 */
1327 if (h
->version
.jtag
>= 24) {
1328 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1329 flags
|= STLINK_F_HAS_DAP_REG
;
1332 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1333 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1334 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1336 /* API to read/write memory at 16 bit from J26 */
1337 /* API to write memory without address increment from J26 */
1338 if (h
->version
.jtag
>= 26)
1339 flags
|= STLINK_F_HAS_MEM_16BIT
;
1341 /* API required to init AP before any AP access from J28 */
1342 if (h
->version
.jtag
>= 28)
1343 flags
|= STLINK_F_HAS_AP_INIT
;
1345 /* API required to return proper error code on close AP from J29 */
1346 if (h
->version
.jtag
>= 29)
1347 flags
|= STLINK_F_FIX_CLOSE_AP
;
1349 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1350 /* API to read memory without address increment from V2J32 */
1351 /* Memory R/W supports CSW from V2J32 */
1352 if (h
->version
.jtag
>= 32)
1353 flags
|= STLINK_F_HAS_DPBANKSEL
;
1357 /* all STLINK-V3 use api-v3 */
1358 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1360 /* STLINK-V3 is a superset of ST-LINK/V2 */
1363 /* API for target voltage */
1364 flags
|= STLINK_F_HAS_TRACE
;
1366 /* preferred API to get last R/W status */
1367 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1369 /* API to access DAP registers */
1370 flags
|= STLINK_F_HAS_DAP_REG
;
1372 /* API to read/write memory at 16 bit */
1373 /* API to write memory without address increment */
1374 flags
|= STLINK_F_HAS_MEM_16BIT
;
1376 /* API required to init AP before any AP access */
1377 flags
|= STLINK_F_HAS_AP_INIT
;
1379 /* API required to return proper error code on close AP */
1380 flags
|= STLINK_F_FIX_CLOSE_AP
;
1382 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1383 /* API to read memory without address increment from V3J2 */
1384 /* Memory R/W supports CSW from V3J2 */
1385 if (h
->version
.jtag
>= 2)
1386 flags
|= STLINK_F_HAS_DPBANKSEL
;
1388 /* 8bit read/write max packet size 512 bytes from V3J6 */
1389 if (h
->version
.jtag
>= 6)
1390 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1396 h
->version
.flags
= flags
;
1399 p
+= sprintf(p
, "V%d", v
);
1401 p
+= sprintf(p
, "J%d", jtag
);
1403 p
+= sprintf(p
, "M%d", msd
);
1405 p
+= sprintf(p
, "B%d", bridge
);
1407 sprintf(p
, "S%d", swim
);
1409 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1411 h
->version
.jtag_api
,
1418 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1420 struct stlink_usb_handle_s
*h
= handle
;
1421 uint32_t adc_results
[2];
1423 /* no error message, simply quit with error */
1424 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1425 return ERROR_COMMAND_NOTFOUND
;
1427 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1429 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1431 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1433 if (result
!= ERROR_OK
)
1436 /* convert result */
1437 adc_results
[0] = le_to_h_u32(h
->databuf
);
1438 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1440 *target_voltage
= 0;
1443 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1445 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1450 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1452 struct stlink_usb_handle_s
*h
= handle
;
1456 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1457 return ERROR_COMMAND_NOTFOUND
;
1459 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1461 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1462 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1463 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1466 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1468 if (result
!= ERROR_OK
)
1474 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1476 struct stlink_usb_handle_s
*h
= handle
;
1480 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1481 return ERROR_COMMAND_NOTFOUND
;
1483 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1485 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1487 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1490 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1492 if (result
!= ERROR_OK
)
1499 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1502 struct stlink_usb_handle_s
*h
= handle
;
1506 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1508 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1510 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1512 if (res
!= ERROR_OK
)
1515 *mode
= h
->databuf
[0];
1521 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1524 struct stlink_usb_handle_s
*h
= handle
;
1528 /* on api V2 we are able the read the latest command
1530 * TODO: we need the test on api V1 too
1532 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1535 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1538 case STLINK_MODE_DEBUG_JTAG
:
1539 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1540 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1541 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1543 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1544 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1546 case STLINK_MODE_DEBUG_SWD
:
1547 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1548 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1549 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1551 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1552 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1554 case STLINK_MODE_DEBUG_SWIM
:
1555 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1556 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1557 /* swim enter does not return any response or status */
1558 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1559 case STLINK_MODE_DFU
:
1560 case STLINK_MODE_MASS
:
1565 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1569 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1572 struct stlink_usb_handle_s
*h
= handle
;
1576 /* command with no reply, use a valid endpoint but zero size */
1577 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1580 case STLINK_MODE_DEBUG_JTAG
:
1581 case STLINK_MODE_DEBUG_SWD
:
1582 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1583 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1585 case STLINK_MODE_DEBUG_SWIM
:
1586 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1587 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1589 case STLINK_MODE_DFU
:
1590 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1591 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1593 case STLINK_MODE_MASS
:
1598 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1600 if (res
!= ERROR_OK
)
1606 static int stlink_usb_assert_srst(void *handle
, int srst
);
1608 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1611 case HL_TRANSPORT_SWD
:
1612 return STLINK_MODE_DEBUG_SWD
;
1613 case HL_TRANSPORT_JTAG
:
1614 return STLINK_MODE_DEBUG_JTAG
;
1616 return STLINK_MODE_UNKNOWN
;
1621 static int stlink_usb_exit_mode(void *handle
)
1625 enum stlink_mode emode
;
1629 res
= stlink_usb_current_mode(handle
, &mode
);
1631 if (res
!= ERROR_OK
)
1634 LOG_DEBUG("MODE: 0x%02X", mode
);
1636 /* try to exit current mode */
1638 case STLINK_DEV_DFU_MODE
:
1639 emode
= STLINK_MODE_DFU
;
1641 case STLINK_DEV_DEBUG_MODE
:
1642 emode
= STLINK_MODE_DEBUG_SWD
;
1644 case STLINK_DEV_SWIM_MODE
:
1645 emode
= STLINK_MODE_DEBUG_SWIM
;
1647 case STLINK_DEV_BOOTLOADER_MODE
:
1648 case STLINK_DEV_MASS_MODE
:
1650 emode
= STLINK_MODE_UNKNOWN
;
1654 if (emode
!= STLINK_MODE_UNKNOWN
)
1655 return stlink_usb_mode_leave(handle
, emode
);
1661 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1665 enum stlink_mode emode
;
1666 struct stlink_usb_handle_s
*h
= handle
;
1670 res
= stlink_usb_exit_mode(handle
);
1671 if (res
!= ERROR_OK
)
1674 res
= stlink_usb_current_mode(handle
, &mode
);
1676 if (res
!= ERROR_OK
)
1679 /* we check the target voltage here as an aid to debugging connection problems.
1680 * the stlink requires the target Vdd to be connected for reliable debugging.
1681 * this cmd is supported in all modes except DFU
1683 if (mode
!= STLINK_DEV_DFU_MODE
) {
1685 float target_voltage
;
1687 /* check target voltage (if supported) */
1688 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1690 if (res
!= ERROR_OK
) {
1691 if (res
!= ERROR_COMMAND_NOTFOUND
)
1692 LOG_ERROR("voltage check failed");
1693 /* attempt to continue as it is not a catastrophic failure */
1695 /* check for a sensible target voltage, operating range is 1.65-5.5v
1696 * according to datasheet */
1697 if (target_voltage
< 1.5)
1698 LOG_ERROR("target voltage may be too low for reliable debugging");
1702 LOG_DEBUG("MODE: 0x%02X", mode
);
1704 /* set selected mode */
1707 if (emode
== STLINK_MODE_UNKNOWN
) {
1708 LOG_ERROR("selected mode (transport) not supported");
1712 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1713 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1714 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1715 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1716 stlink_speed(h
, initial_interface_speed
, false);
1718 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1719 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1720 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1721 stlink_speed(h
, initial_interface_speed
, false);
1725 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
&&
1726 (emode
== STLINK_MODE_DEBUG_JTAG
|| emode
== STLINK_MODE_DEBUG_SWD
)) {
1727 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1729 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1730 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1731 stlink_speed(h
, initial_interface_speed
, false);
1734 /* preliminary SRST assert:
1735 * We want SRST is asserted before activating debug signals (mode_enter).
1736 * As the required mode has not been set, the adapter may not know what pin to use.
1737 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1738 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1739 * after power on, SWIM_RST stays unchanged */
1740 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1741 stlink_usb_assert_srst(handle
, 0);
1742 /* do not check the return status here, we will
1743 proceed and enter the desired mode below
1744 and try asserting srst again. */
1746 res
= stlink_usb_mode_enter(handle
, emode
);
1747 if (res
!= ERROR_OK
)
1750 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1751 if (connect_under_reset
) {
1752 res
= stlink_usb_assert_srst(handle
, 0);
1753 if (res
!= ERROR_OK
)
1757 res
= stlink_usb_current_mode(handle
, &mode
);
1759 if (res
!= ERROR_OK
)
1762 LOG_DEBUG("MODE: 0x%02X", mode
);
1767 /* request status from last swim request */
1768 static int stlink_swim_status(void *handle
)
1770 struct stlink_usb_handle_s
*h
= handle
;
1773 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1774 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1775 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1776 /* error is checked by the caller */
1777 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1778 if (res
!= ERROR_OK
)
1783 the purpose of this function is unknown...
1784 capabilities? anyway for swim v6 it returns
1787 __attribute__((unused
))
1788 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1790 struct stlink_usb_handle_s
*h
= handle
;
1793 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1794 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1795 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1796 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1797 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1798 if (res
!= ERROR_OK
)
1800 memcpy(cap
, h
->databuf
, 8);
1804 /* debug dongle assert/deassert sreset line */
1805 static int stlink_swim_assert_reset(void *handle
, int reset
)
1807 struct stlink_usb_handle_s
*h
= handle
;
1810 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1811 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1813 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1815 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1816 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1817 if (res
!= ERROR_OK
)
1824 1.3ms low then 750Hz then 1.5kHz
1826 static int stlink_swim_enter(void *handle
)
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_ENTER_SEQ
;
1834 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1835 if (res
!= ERROR_OK
)
1840 /* switch high/low speed swim */
1841 static int stlink_swim_speed(void *handle
, int speed
)
1843 struct stlink_usb_handle_s
*h
= handle
;
1846 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1847 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1848 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1850 h
->cmdbuf
[h
->cmdidx
++] = 1;
1852 h
->cmdbuf
[h
->cmdidx
++] = 0;
1853 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1854 if (res
!= ERROR_OK
)
1860 initiate srst from swim.
1861 nrst is pulled low for 50us.
1863 static int stlink_swim_generate_rst(void *handle
)
1865 struct stlink_usb_handle_s
*h
= handle
;
1868 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1869 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1870 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1871 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1872 if (res
!= ERROR_OK
)
1878 send resynchronize sequence
1879 swim is pulled low for 16us
1880 reply is 64 clks low
1882 static int stlink_swim_resync(void *handle
)
1884 struct stlink_usb_handle_s
*h
= handle
;
1887 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1888 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1889 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1890 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1891 if (res
!= ERROR_OK
)
1896 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1898 struct stlink_usb_handle_s
*h
= handle
;
1901 unsigned int datalen
= 0;
1902 int cmdsize
= STLINK_CMD_SIZE_V2
;
1904 if (len
> STLINK_SWIM_DATA_SIZE
)
1907 if (h
->version
.stlink
== 1)
1908 cmdsize
= STLINK_SG_SIZE
;
1910 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1911 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1912 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1913 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1915 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1917 for (i
= 0; i
< len
; i
++) {
1918 if (h
->cmdidx
== cmdsize
)
1919 h
->databuf
[datalen
++] = *(data
++);
1921 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1923 if (h
->version
.stlink
== 1)
1924 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1926 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1927 if (res
!= ERROR_OK
)
1932 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1934 struct stlink_usb_handle_s
*h
= handle
;
1937 if (len
> STLINK_SWIM_DATA_SIZE
)
1940 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1941 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1942 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1943 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1945 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1947 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1948 if (res
!= ERROR_OK
)
1951 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1952 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1953 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1954 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1955 if (res
!= ERROR_OK
)
1962 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1965 struct stlink_usb_handle_s
*h
= handle
;
1969 /* there is no swim read core id cmd */
1970 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1975 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1977 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1978 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1979 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1981 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1984 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1986 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1990 if (res
!= ERROR_OK
)
1993 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1995 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
2000 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
2002 struct stlink_usb_handle_s
*h
= handle
;
2007 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
2009 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2010 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
2011 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2014 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2015 if (res
!= ERROR_OK
)
2018 *val
= le_to_h_u32(h
->databuf
+ 4);
2022 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
2024 struct stlink_usb_handle_s
*h
= handle
;
2028 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2030 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2031 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2032 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
2034 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
2035 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2037 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2040 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2044 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
2046 struct stlink_usb_handle_s
*h
= handle
;
2050 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
2053 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2055 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2056 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
2058 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2059 if (res
!= ERROR_OK
)
2062 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
2063 *size
= bytes_avail
< *size
? bytes_avail
: *size
;
2066 res
= stlink_usb_read_trace(handle
, buf
, *size
);
2067 if (res
!= ERROR_OK
)
2076 static enum target_state
stlink_usb_v2_get_status(void *handle
)
2081 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
2082 if (result
!= ERROR_OK
)
2083 return TARGET_UNKNOWN
;
2085 if (status
& S_HALT
)
2086 return TARGET_HALTED
;
2087 else if (status
& S_RESET_ST
)
2088 return TARGET_RESET
;
2090 return TARGET_RUNNING
;
2094 static enum target_state
stlink_usb_state(void *handle
)
2097 struct stlink_usb_handle_s
*h
= handle
;
2101 if (h
->reconnect_pending
) {
2102 LOG_INFO("Previous state query failed, trying to reconnect");
2103 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
2104 if (res
!= ERROR_OK
)
2105 return TARGET_UNKNOWN
;
2107 h
->reconnect_pending
= false;
2110 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2111 res
= stlink_usb_v2_get_status(handle
);
2112 if (res
== TARGET_UNKNOWN
)
2113 h
->reconnect_pending
= true;
2117 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2119 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2120 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
2122 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
2124 if (res
!= ERROR_OK
)
2125 return TARGET_UNKNOWN
;
2127 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
2128 return TARGET_RUNNING
;
2129 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
2130 return TARGET_HALTED
;
2132 h
->reconnect_pending
= true;
2134 return TARGET_UNKNOWN
;
2137 static int stlink_usb_assert_srst(void *handle
, int srst
)
2139 struct stlink_usb_handle_s
*h
= handle
;
2143 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
2144 return stlink_swim_assert_reset(handle
, srst
);
2146 if (h
->version
.stlink
== 1)
2147 return ERROR_COMMAND_NOTFOUND
;
2149 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2151 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2152 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
2153 h
->cmdbuf
[h
->cmdidx
++] = srst
;
2155 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2159 static void stlink_usb_trace_disable(void *handle
)
2162 struct stlink_usb_handle_s
*h
= handle
;
2166 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
2168 LOG_DEBUG("Tracing: disable");
2170 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2171 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2172 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
2173 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2175 if (res
== ERROR_OK
)
2176 h
->trace
.enabled
= false;
2181 static int stlink_usb_trace_enable(void *handle
)
2184 struct stlink_usb_handle_s
*h
= handle
;
2188 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
2189 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
2191 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2192 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
2193 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
2195 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
2198 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2200 if (res
== ERROR_OK
) {
2201 h
->trace
.enabled
= true;
2202 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
2205 LOG_ERROR("Tracing is not supported by this version.");
2213 static int stlink_usb_reset(void *handle
)
2215 struct stlink_usb_handle_s
*h
= handle
;
2220 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2222 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2224 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2225 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
2227 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
2229 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2230 if (retval
!= ERROR_OK
)
2233 if (h
->trace
.enabled
) {
2234 stlink_usb_trace_disable(h
);
2235 return stlink_usb_trace_enable(h
);
2242 static int stlink_usb_run(void *handle
)
2245 struct stlink_usb_handle_s
*h
= handle
;
2249 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2250 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
2255 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2257 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2258 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
2260 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2264 static int stlink_usb_halt(void *handle
)
2267 struct stlink_usb_handle_s
*h
= handle
;
2271 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2272 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2277 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2279 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2280 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
2282 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2286 static int stlink_usb_step(void *handle
)
2288 struct stlink_usb_handle_s
*h
= handle
;
2292 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
2293 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
2294 * that the Cortex-M3 currently does. */
2295 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
2296 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
2297 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
2300 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2302 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2303 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
2305 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2309 static int stlink_usb_read_regs(void *handle
)
2312 struct stlink_usb_handle_s
*h
= handle
;
2316 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
2318 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2319 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2321 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
2322 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
2323 /* regs data from offset 0 */
2325 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
2326 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
2327 /* status at offset 0, regs data from offset 4 */
2334 static int stlink_usb_read_reg(void *handle
, unsigned int regsel
, uint32_t *val
)
2337 struct stlink_usb_handle_s
*h
= handle
;
2341 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2342 res
= stlink_usb_write_debug_reg(h
, DCB_DCRSR
, regsel
& 0x7f);
2343 if (res
!= ERROR_OK
)
2346 /* FIXME: poll DHCSR.S_REGRDY before read DCRDR */
2347 return stlink_usb_v2_read_debug_reg(h
, DCB_DCRDR
, val
);
2350 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2352 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2353 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2354 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2356 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2357 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2359 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2360 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2361 if (res
!= ERROR_OK
)
2363 *val
= le_to_h_u32(h
->databuf
);
2366 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2367 if (res
!= ERROR_OK
)
2369 *val
= le_to_h_u32(h
->databuf
+ 4);
2375 static int stlink_usb_write_reg(void *handle
, unsigned int regsel
, uint32_t val
)
2377 struct stlink_usb_handle_s
*h
= handle
;
2381 if (STLINK_REGSEL_IS_FPU(regsel
) && !(h
->version
.flags
& STLINK_F_HAS_FPU_REG
)) {
2382 int res
= stlink_usb_write_debug_reg(h
, DCB_DCRDR
, val
);
2383 if (res
!= ERROR_OK
)
2386 return stlink_usb_write_debug_reg(h
, DCB_DCRSR
, DCRSR_WNR
| (regsel
& 0x7f));
2387 /* FIXME: poll DHCSR.S_REGRDY after write DCRSR */
2390 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2392 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2393 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2394 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2396 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2397 h
->cmdbuf
[h
->cmdidx
++] = regsel
;
2398 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2401 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2404 static int stlink_usb_get_rw_status(void *handle
)
2406 struct stlink_usb_handle_s
*h
= handle
;
2410 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2413 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2415 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2416 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2417 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2418 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2420 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2421 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2426 static int stlink_usb_read_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2427 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2430 uint16_t read_len
= len
;
2431 struct stlink_usb_handle_s
*h
= handle
;
2435 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2436 return ERROR_COMMAND_NOTFOUND
;
2438 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2439 if (len
> stlink_usb_block(h
)) {
2440 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2444 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2446 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2447 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2448 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2450 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2452 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2453 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2456 /* we need to fix read length for single bytes */
2460 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2462 if (res
!= ERROR_OK
)
2465 memcpy(buffer
, h
->databuf
, len
);
2467 return stlink_usb_get_rw_status(handle
);
2471 static int stlink_usb_write_mem8(void *handle
, uint8_t ap_num
, uint32_t csw
,
2472 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2475 struct stlink_usb_handle_s
*h
= handle
;
2479 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2480 return ERROR_COMMAND_NOTFOUND
;
2482 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2483 if (len
> stlink_usb_block(h
)) {
2484 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2488 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2490 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2491 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2492 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2494 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2496 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2497 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2500 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2502 if (res
!= ERROR_OK
)
2505 return stlink_usb_get_rw_status(handle
);
2509 static int stlink_usb_read_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2510 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2513 struct stlink_usb_handle_s
*h
= handle
;
2517 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2518 return ERROR_COMMAND_NOTFOUND
;
2520 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2521 return ERROR_COMMAND_NOTFOUND
;
2523 if (len
> STLINK_MAX_RW16_32
) {
2524 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2528 /* data must be a multiple of 2 and half-word aligned */
2529 if (len
% 2 || addr
% 2) {
2530 LOG_DEBUG("Invalid data alignment");
2531 return ERROR_TARGET_UNALIGNED_ACCESS
;
2534 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2536 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2537 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2538 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2540 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2542 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2543 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2546 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2548 if (res
!= ERROR_OK
)
2551 memcpy(buffer
, h
->databuf
, len
);
2553 return stlink_usb_get_rw_status(handle
);
2557 static int stlink_usb_write_mem16(void *handle
, uint8_t ap_num
, uint32_t csw
,
2558 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2561 struct stlink_usb_handle_s
*h
= handle
;
2565 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2566 return ERROR_COMMAND_NOTFOUND
;
2568 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2569 return ERROR_COMMAND_NOTFOUND
;
2571 if (len
> STLINK_MAX_RW16_32
) {
2572 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2576 /* data must be a multiple of 2 and half-word aligned */
2577 if (len
% 2 || addr
% 2) {
2578 LOG_DEBUG("Invalid data alignment");
2579 return ERROR_TARGET_UNALIGNED_ACCESS
;
2582 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2584 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2585 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2586 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2588 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2590 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2591 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2594 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2596 if (res
!= ERROR_OK
)
2599 return stlink_usb_get_rw_status(handle
);
2603 static int stlink_usb_read_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2604 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2607 struct stlink_usb_handle_s
*h
= handle
;
2611 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2612 return ERROR_COMMAND_NOTFOUND
;
2614 if (len
> STLINK_MAX_RW16_32
) {
2615 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2619 /* data must be a multiple of 4 and word aligned */
2620 if (len
% 4 || addr
% 4) {
2621 LOG_DEBUG("Invalid data alignment");
2622 return ERROR_TARGET_UNALIGNED_ACCESS
;
2625 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2627 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2628 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2629 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2631 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2633 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2634 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2637 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2639 if (res
!= ERROR_OK
)
2642 memcpy(buffer
, h
->databuf
, len
);
2644 return stlink_usb_get_rw_status(handle
);
2648 static int stlink_usb_write_mem32(void *handle
, uint8_t ap_num
, uint32_t csw
,
2649 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2652 struct stlink_usb_handle_s
*h
= handle
;
2656 if ((ap_num
!= 0 || csw
!= 0) && !(h
->version
.flags
& STLINK_F_HAS_CSW
))
2657 return ERROR_COMMAND_NOTFOUND
;
2659 if (len
> STLINK_MAX_RW16_32
) {
2660 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2664 /* data must be a multiple of 4 and word aligned */
2665 if (len
% 4 || addr
% 4) {
2666 LOG_DEBUG("Invalid data alignment");
2667 return ERROR_TARGET_UNALIGNED_ACCESS
;
2670 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2672 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2673 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2674 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2676 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2678 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2679 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2682 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2684 if (res
!= ERROR_OK
)
2687 return stlink_usb_get_rw_status(handle
);
2690 static int stlink_usb_read_mem32_noaddrinc(void *handle
, uint8_t ap_num
, uint32_t csw
,
2691 uint32_t addr
, uint16_t len
, uint8_t *buffer
)
2693 struct stlink_usb_handle_s
*h
= handle
;
2695 assert(handle
!= NULL
);
2697 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_RD_NO_INC
))
2698 return ERROR_COMMAND_NOTFOUND
;
2700 if (len
> STLINK_MAX_RW16_32
) {
2701 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2705 /* data must be a multiple of 4 and word aligned */
2706 if (len
% 4 || addr
% 4) {
2707 LOG_DEBUG("Invalid data alignment");
2708 return ERROR_TARGET_UNALIGNED_ACCESS
;
2711 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2713 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2714 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT_NO_ADDR_INC
;
2715 h_u32_to_le(h
->cmdbuf
+ h
->cmdidx
, addr
);
2717 h_u16_to_le(h
->cmdbuf
+ h
->cmdidx
, len
);
2719 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2720 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2723 int retval
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2724 if (retval
!= ERROR_OK
)
2727 memcpy(buffer
, h
->databuf
, len
);
2729 return stlink_usb_get_rw_status(handle
);
2732 static int stlink_usb_write_mem32_noaddrinc(void *handle
, uint8_t ap_num
, uint32_t csw
,
2733 uint32_t addr
, uint16_t len
, const uint8_t *buffer
)
2735 struct stlink_usb_handle_s
*h
= handle
;
2737 assert(handle
!= NULL
);
2739 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_WR_NO_INC
))
2740 return ERROR_COMMAND_NOTFOUND
;
2742 if (len
> STLINK_MAX_RW16_32
) {
2743 LOG_DEBUG("max buffer (%d) length exceeded", STLINK_MAX_RW16_32
);
2747 /* data must be a multiple of 4 and word aligned */
2748 if (len
% 4 || addr
% 4) {
2749 LOG_DEBUG("Invalid data alignment");
2750 return ERROR_TARGET_UNALIGNED_ACCESS
;
2753 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2755 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2756 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT_NO_ADDR_INC
;
2757 h_u32_to_le(h
->cmdbuf
+ h
->cmdidx
, addr
);
2759 h_u16_to_le(h
->cmdbuf
+ h
->cmdidx
, len
);
2761 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
2762 h_u24_to_le(h
->cmdbuf
+ h
->cmdidx
, csw
>> 8);
2765 int retval
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2766 if (retval
!= ERROR_OK
)
2769 return stlink_usb_get_rw_status(handle
);
2772 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2774 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2775 if (max_tar_block
== 0)
2777 return max_tar_block
;
2780 static int stlink_usb_read_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2781 uint32_t addr
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2783 int retval
= ERROR_OK
;
2784 uint32_t bytes_remaining
;
2786 struct stlink_usb_handle_s
*h
= handle
;
2788 /* calculate byte count */
2791 /* switch to 8 bit if stlink does not support 16 bit memory read */
2792 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2796 bytes_remaining
= (size
!= 1) ?
2797 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2799 if (count
< bytes_remaining
)
2800 bytes_remaining
= count
;
2803 * all stlink support 8/32bit memory read/writes and only from
2804 * stlink V2J26 there is support for 16 bit memory read/write.
2805 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2809 /* When in jtag mode the stlink uses the auto-increment functionality.
2810 * However it expects us to pass the data correctly, this includes
2811 * alignment and any page boundaries. We already do this as part of the
2812 * adi_v5 implementation, but the stlink is a hla adapter and so this
2813 * needs implementing manually.
2814 * currently this only affects jtag mode, according to ST they do single
2815 * access in SWD mode - but this may change and so we do it for both modes */
2817 /* we first need to check for any unaligned bytes */
2818 if (addr
& (size
- 1)) {
2819 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2820 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2821 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2822 usleep((1 << retries
++) * 1000);
2825 if (retval
!= ERROR_OK
)
2827 buffer
+= head_bytes
;
2829 count
-= head_bytes
;
2830 bytes_remaining
-= head_bytes
;
2833 if (bytes_remaining
& (size
- 1))
2834 retval
= stlink_usb_read_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2836 retval
= stlink_usb_read_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2838 retval
= stlink_usb_read_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2840 retval
= stlink_usb_read_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2843 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2844 usleep((1 << retries
++) * 1000);
2847 if (retval
!= ERROR_OK
)
2850 buffer
+= bytes_remaining
;
2851 addr
+= bytes_remaining
;
2852 count
-= bytes_remaining
;
2858 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2859 uint32_t count
, uint8_t *buffer
)
2861 return stlink_usb_read_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2862 addr
, size
, count
, buffer
);
2865 static int stlink_usb_write_ap_mem(void *handle
, uint8_t ap_num
, uint32_t csw
,
2866 uint32_t addr
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2868 int retval
= ERROR_OK
;
2869 uint32_t bytes_remaining
;
2871 struct stlink_usb_handle_s
*h
= handle
;
2873 /* calculate byte count */
2876 /* switch to 8 bit if stlink does not support 16 bit memory read */
2877 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2882 bytes_remaining
= (size
!= 1) ?
2883 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2885 if (count
< bytes_remaining
)
2886 bytes_remaining
= count
;
2889 * all stlink support 8/32bit memory read/writes and only from
2890 * stlink V2J26 there is support for 16 bit memory read/write.
2891 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2896 /* When in jtag mode the stlink uses the auto-increment functionality.
2897 * However it expects us to pass the data correctly, this includes
2898 * alignment and any page boundaries. We already do this as part of the
2899 * adi_v5 implementation, but the stlink is a hla adapter and so this
2900 * needs implementing manually.
2901 * currently this only affects jtag mode, according to ST they do single
2902 * access in SWD mode - but this may change and so we do it for both modes */
2904 /* we first need to check for any unaligned bytes */
2905 if (addr
& (size
- 1)) {
2907 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2908 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, head_bytes
, buffer
);
2909 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2910 usleep((1<<retries
++) * 1000);
2913 if (retval
!= ERROR_OK
)
2915 buffer
+= head_bytes
;
2917 count
-= head_bytes
;
2918 bytes_remaining
-= head_bytes
;
2921 if (bytes_remaining
& (size
- 1))
2922 retval
= stlink_usb_write_ap_mem(handle
, ap_num
, csw
, addr
, 1, bytes_remaining
, buffer
);
2924 retval
= stlink_usb_write_mem16(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2926 retval
= stlink_usb_write_mem32(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2929 retval
= stlink_usb_write_mem8(handle
, ap_num
, csw
, addr
, bytes_remaining
, buffer
);
2930 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2931 usleep((1<<retries
++) * 1000);
2934 if (retval
!= ERROR_OK
)
2937 buffer
+= bytes_remaining
;
2938 addr
+= bytes_remaining
;
2939 count
-= bytes_remaining
;
2945 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2946 uint32_t count
, const uint8_t *buffer
)
2948 return stlink_usb_write_ap_mem(handle
, STLINK_HLA_AP_NUM
, STLINK_HLA_CSW
,
2949 addr
, size
, count
, buffer
);
2953 static int stlink_usb_override_target(const char *targetname
)
2955 return !strcmp(targetname
, "cortex_m");
2958 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2963 we only have low and high speed...
2964 before changing speed the SWIM_CSR HS bit
2968 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2969 if (retval
!= ERROR_OK
)
2970 LOG_ERROR("Unable to set adapter speed");
2973 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2976 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2979 int speed_index
= -1;
2980 int speed_diff
= INT_MAX
;
2981 int last_valid_speed
= -1;
2984 for (i
= 0; i
< map_size
; i
++) {
2987 last_valid_speed
= i
;
2988 if (khz
== map
[i
].speed
) {
2992 int current_diff
= khz
- map
[i
].speed
;
2993 /* get abs value for comparison */
2994 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2995 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2996 speed_diff
= current_diff
;
3002 if (speed_index
== -1) {
3003 /* this will only be here if we cannot match the slow speed.
3004 * use the slowest speed we support.*/
3005 speed_index
= last_valid_speed
;
3007 } else if (i
== map_size
)
3010 if (!match
&& query
) {
3011 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
3012 khz
, map
[speed_index
].speed
);
3018 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
3021 struct stlink_usb_handle_s
*h
= handle
;
3023 /* old firmware cannot change it */
3024 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
3027 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
3028 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
3031 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
3032 if (result
!= ERROR_OK
) {
3033 LOG_ERROR("Unable to set adapter speed");
3038 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
3041 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
3044 struct stlink_usb_handle_s
*h
= handle
;
3046 /* old firmware cannot change it */
3047 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
3050 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
3051 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
3054 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
3055 if (result
!= ERROR_OK
) {
3056 LOG_ERROR("Unable to set adapter speed");
3061 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
3064 static void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
3068 LOG_DEBUG("Supported clock speeds are:");
3069 for (i
= 0; i
< map_size
; i
++)
3071 LOG_DEBUG("%d kHz", map
[i
].speed
);
3074 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
3076 struct stlink_usb_handle_s
*h
= handle
;
3079 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3080 LOG_ERROR("Unknown command");
3084 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3086 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3087 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
3088 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3090 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
3092 int size
= h
->databuf
[8];
3094 if (size
> STLINK_V3_MAX_FREQ_NB
)
3095 size
= STLINK_V3_MAX_FREQ_NB
;
3097 for (i
= 0; i
< size
; i
++) {
3098 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
3099 map
[i
].speed_divisor
= i
;
3102 /* set to zero all the next entries */
3103 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
3109 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
3111 struct stlink_usb_handle_s
*h
= handle
;
3113 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
3114 LOG_ERROR("Unknown command");
3118 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3120 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3121 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
3122 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
3123 h
->cmdbuf
[h
->cmdidx
++] = 0;
3125 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
3127 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3130 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
3132 struct stlink_usb_handle_s
*h
= handle
;
3134 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
3136 stlink_get_com_freq(h
, is_jtag
, map
);
3138 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);