1 /***************************************************************************
2 * SWIM contributions by Ake Rehnman *
3 * Copyright (C) 2017 Ake Rehnman *
4 * ake.rehnman(at)gmail.com *
6 * Copyright (C) 2011-2012 by Mathias Kuester *
7 * Mathias Kuester <kesmtp@freenet.de> *
9 * Copyright (C) 2012 by Spencer Oliver *
10 * spen@spen-soft.co.uk *
12 * This code is based on https://github.com/texane/stlink *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
32 /* project specific includes */
33 #include <helper/binarybuffer.h>
34 #include <helper/bits.h>
35 #include <jtag/interface.h>
36 #include <jtag/hla/hla_layout.h>
37 #include <jtag/hla/hla_transport.h>
38 #include <jtag/hla/hla_interface.h>
39 #include <jtag/swim.h>
40 #include <target/target.h>
41 #include <transport/transport.h>
43 #include <target/cortex_m.h>
45 #include "libusb_helper.h"
48 #define USE_LIBUSB_ASYNCIO
51 #define STLINK_SERIAL_LEN 24
53 #define ENDPOINT_IN 0x80
54 #define ENDPOINT_OUT 0x00
56 #define STLINK_WRITE_TIMEOUT 1000
57 #define STLINK_READ_TIMEOUT 1000
59 #define STLINK_RX_EP (1|ENDPOINT_IN)
60 #define STLINK_TX_EP (2|ENDPOINT_OUT)
61 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
63 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
64 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
66 #define STLINK_SG_SIZE (31)
67 #define STLINK_DATA_SIZE (4096)
68 #define STLINK_CMD_SIZE_V2 (16)
69 #define STLINK_CMD_SIZE_V1 (10)
71 #define STLINK_V1_PID (0x3744)
72 #define STLINK_V2_PID (0x3748)
73 #define STLINK_V2_1_PID (0x374B)
74 #define STLINK_V2_1_NO_MSD_PID (0x3752)
75 #define STLINK_V3_USBLOADER_PID (0x374D)
76 #define STLINK_V3E_PID (0x374E)
77 #define STLINK_V3S_PID (0x374F)
78 #define STLINK_V3_2VCP_PID (0x3753)
81 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
82 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
83 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
85 #define STLINK_MAX_RW8 (64)
86 #define STLINKV3_MAX_RW8 (512)
88 /* "WAIT" responses will be retried (with exponential backoff) at
89 * most this many times before failing to caller.
91 #define MAX_WAIT_RETRIES 8
93 enum stlink_jtag_api_version
{
94 STLINK_JTAG_API_V1
= 1,
100 STLINK_MODE_UNKNOWN
= 0,
103 STLINK_MODE_DEBUG_JTAG
,
104 STLINK_MODE_DEBUG_SWD
,
105 STLINK_MODE_DEBUG_SWIM
109 struct stlink_usb_version
{
116 /** jtag api version supported */
117 enum stlink_jtag_api_version jtag_api
;
118 /** one bit for each feature supported. See macros STLINK_F_* */
123 struct stlink_usb_handle_s
{
125 struct libusb_device_handle
*fd
;
127 struct libusb_transfer
*trans
;
135 uint8_t cmdbuf
[STLINK_SG_SIZE
];
141 uint8_t databuf
[STLINK_DATA_SIZE
];
143 uint32_t max_mem_packet
;
145 enum stlink_mode st_mode
;
147 struct stlink_usb_version version
;
154 /** whether SWO tracing is enabled or not */
156 /** trace module source clock */
159 /** reconnect is needed next time we try to query the
161 bool reconnect_pending
;
164 #define STLINK_SWIM_ERR_OK 0x00
165 #define STLINK_SWIM_BUSY 0x01
166 #define STLINK_DEBUG_ERR_OK 0x80
167 #define STLINK_DEBUG_ERR_FAULT 0x81
168 #define STLINK_SWD_AP_WAIT 0x10
169 #define STLINK_SWD_AP_FAULT 0x11
170 #define STLINK_SWD_AP_ERROR 0x12
171 #define STLINK_SWD_AP_PARITY_ERROR 0x13
172 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
173 #define STLINK_JTAG_WRITE_ERROR 0x0c
174 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
175 #define STLINK_SWD_DP_WAIT 0x14
176 #define STLINK_SWD_DP_FAULT 0x15
177 #define STLINK_SWD_DP_ERROR 0x16
178 #define STLINK_SWD_DP_PARITY_ERROR 0x17
180 #define STLINK_SWD_AP_WDATA_ERROR 0x18
181 #define STLINK_SWD_AP_STICKY_ERROR 0x19
182 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
184 #define STLINK_BAD_AP_ERROR 0x1d
186 #define STLINK_CORE_RUNNING 0x80
187 #define STLINK_CORE_HALTED 0x81
188 #define STLINK_CORE_STAT_UNKNOWN -1
190 #define STLINK_GET_VERSION 0xF1
191 #define STLINK_DEBUG_COMMAND 0xF2
192 #define STLINK_DFU_COMMAND 0xF3
193 #define STLINK_SWIM_COMMAND 0xF4
194 #define STLINK_GET_CURRENT_MODE 0xF5
195 #define STLINK_GET_TARGET_VOLTAGE 0xF7
197 #define STLINK_DEV_DFU_MODE 0x00
198 #define STLINK_DEV_MASS_MODE 0x01
199 #define STLINK_DEV_DEBUG_MODE 0x02
200 #define STLINK_DEV_SWIM_MODE 0x03
201 #define STLINK_DEV_BOOTLOADER_MODE 0x04
202 #define STLINK_DEV_UNKNOWN_MODE -1
204 #define STLINK_DFU_EXIT 0x07
207 STLINK_SWIM_ENTER_SEQ
208 1.3ms low then 750Hz then 1.5kHz
211 STM8 DM pulls reset pin low 50us
214 uint8_t (0=low|1=high)
221 send syncronization seq (16us low, response 64 clocks low)
223 #define STLINK_SWIM_ENTER 0x00
224 #define STLINK_SWIM_EXIT 0x01
225 #define STLINK_SWIM_READ_CAP 0x02
226 #define STLINK_SWIM_SPEED 0x03
227 #define STLINK_SWIM_ENTER_SEQ 0x04
228 #define STLINK_SWIM_GEN_RST 0x05
229 #define STLINK_SWIM_RESET 0x06
230 #define STLINK_SWIM_ASSERT_RESET 0x07
231 #define STLINK_SWIM_DEASSERT_RESET 0x08
232 #define STLINK_SWIM_READSTATUS 0x09
233 #define STLINK_SWIM_WRITEMEM 0x0a
234 #define STLINK_SWIM_READMEM 0x0b
235 #define STLINK_SWIM_READBUF 0x0c
237 #define STLINK_DEBUG_GETSTATUS 0x01
238 #define STLINK_DEBUG_FORCEDEBUG 0x02
239 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
240 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
241 #define STLINK_DEBUG_APIV1_READREG 0x05
242 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
243 #define STLINK_DEBUG_READMEM_32BIT 0x07
244 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
245 #define STLINK_DEBUG_RUNCORE 0x09
246 #define STLINK_DEBUG_STEPCORE 0x0a
247 #define STLINK_DEBUG_APIV1_SETFP 0x0b
248 #define STLINK_DEBUG_READMEM_8BIT 0x0c
249 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
250 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
251 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
252 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
254 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
255 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
256 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
258 #define STLINK_DEBUG_APIV1_ENTER 0x20
259 #define STLINK_DEBUG_EXIT 0x21
260 #define STLINK_DEBUG_READCOREID 0x22
262 #define STLINK_DEBUG_APIV2_ENTER 0x30
263 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
264 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
265 #define STLINK_DEBUG_APIV2_READREG 0x33
266 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
267 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
268 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
270 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
271 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
272 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
274 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
276 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
277 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
278 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
279 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
280 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
281 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
282 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
283 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
284 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
286 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
287 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
289 #define STLINK_APIV3_SET_COM_FREQ 0x61
290 #define STLINK_APIV3_GET_COM_FREQ 0x62
292 #define STLINK_APIV3_GET_VERSION_EX 0xFB
294 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
295 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
296 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
298 #define STLINK_DEBUG_PORT_ACCESS 0xffff
300 #define STLINK_TRACE_SIZE 4096
301 #define STLINK_TRACE_MAX_HZ 2000000
303 #define STLINK_V3_MAX_FREQ_NB 10
305 #define REQUEST_SENSE 0x03
306 #define REQUEST_SENSE_LENGTH 18
309 * Map the relevant features, quirks and workaround for specific firmware
312 #define STLINK_F_HAS_TRACE BIT(0)
313 #define STLINK_F_HAS_SWD_SET_FREQ BIT(1)
314 #define STLINK_F_HAS_JTAG_SET_FREQ BIT(2)
315 #define STLINK_F_HAS_MEM_16BIT BIT(3)
316 #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(4)
317 #define STLINK_F_HAS_DAP_REG BIT(5)
318 #define STLINK_F_QUIRK_JTAG_DP_READ BIT(6)
319 #define STLINK_F_HAS_AP_INIT BIT(7)
320 #define STLINK_F_HAS_DPBANKSEL BIT(8)
321 #define STLINK_F_HAS_RW8_512BYTES BIT(9)
324 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
331 /* SWD clock speed */
332 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
334 {1800, 1}, /* default */
347 /* JTAG clock speed */
348 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
352 {1125, 32}, /* default */
358 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
359 static int stlink_swim_status(void *handle
);
360 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
);
361 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
);
362 static int stlink_speed(void *handle
, int khz
, bool query
);
365 static unsigned int stlink_usb_block(void *handle
)
367 struct stlink_usb_handle_s
*h
= handle
;
369 assert(handle
!= NULL
);
371 if (h
->version
.flags
& STLINK_F_HAS_RW8_512BYTES
)
372 return STLINKV3_MAX_RW8
;
374 return STLINK_MAX_RW8
;
379 #ifdef USE_LIBUSB_ASYNCIO
381 static LIBUSB_CALL
void sync_transfer_cb(struct libusb_transfer
*transfer
)
383 int *completed
= transfer
->user_data
;
385 /* caller interprets result and frees transfer */
389 static void sync_transfer_wait_for_completion(struct libusb_transfer
*transfer
)
391 int r
, *completed
= transfer
->user_data
;
393 /* Assuming a single libusb context exists. There no existing interface into this
394 * module to pass a libusb context.
396 struct libusb_context
*ctx
= NULL
;
398 while (!*completed
) {
399 r
= libusb_handle_events_completed(ctx
, completed
);
401 if (r
== LIBUSB_ERROR_INTERRUPTED
)
403 libusb_cancel_transfer(transfer
);
410 static int transfer_error_status(const struct libusb_transfer
*transfer
)
414 switch (transfer
->status
) {
415 case LIBUSB_TRANSFER_COMPLETED
:
418 case LIBUSB_TRANSFER_TIMED_OUT
:
419 r
= LIBUSB_ERROR_TIMEOUT
;
421 case LIBUSB_TRANSFER_STALL
:
422 r
= LIBUSB_ERROR_PIPE
;
424 case LIBUSB_TRANSFER_OVERFLOW
:
425 r
= LIBUSB_ERROR_OVERFLOW
;
427 case LIBUSB_TRANSFER_NO_DEVICE
:
428 r
= LIBUSB_ERROR_NO_DEVICE
;
430 case LIBUSB_TRANSFER_ERROR
:
431 case LIBUSB_TRANSFER_CANCELLED
:
435 r
= LIBUSB_ERROR_OTHER
;
449 size_t transfer_size
;
450 struct libusb_transfer
*transfer
;
453 static int jtag_libusb_bulk_transfer_n(
454 struct libusb_device_handle
*dev_handle
,
455 struct jtag_xfer
*transfers
,
460 int returnval
= ERROR_OK
;
463 for (size_t i
= 0; i
< n_transfers
; ++i
) {
464 transfers
[i
].retval
= 0;
465 transfers
[i
].completed
= 0;
466 transfers
[i
].transfer_size
= 0;
467 transfers
[i
].transfer
= libusb_alloc_transfer(0);
469 if (transfers
[i
].transfer
== NULL
) {
470 for (size_t j
= 0; j
< i
; ++j
)
471 libusb_free_transfer(transfers
[j
].transfer
);
473 LOG_DEBUG("ERROR, failed to alloc usb transfers");
474 for (size_t k
= 0; k
< n_transfers
; ++k
)
475 transfers
[k
].retval
= LIBUSB_ERROR_NO_MEM
;
480 for (size_t i
= 0; i
< n_transfers
; ++i
) {
481 libusb_fill_bulk_transfer(
482 transfers
[i
].transfer
,
484 transfers
[i
].ep
, transfers
[i
].buf
, transfers
[i
].size
,
485 sync_transfer_cb
, &transfers
[i
].completed
, timeout
);
486 transfers
[i
].transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
488 retval
= libusb_submit_transfer(transfers
[i
].transfer
);
490 LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i
, retval
);
492 /* Probably no point continuing to submit transfers once a submission fails.
493 * As a result, tag all remaining transfers as errors.
495 for (size_t j
= i
; j
< n_transfers
; ++j
)
496 transfers
[j
].retval
= retval
;
498 returnval
= ERROR_FAIL
;
503 /* Wait for every submitted USB transfer to complete.
505 for (size_t i
= 0; i
< n_transfers
; ++i
) {
506 if (transfers
[i
].retval
== 0) {
507 sync_transfer_wait_for_completion(transfers
[i
].transfer
);
509 retval
= transfer_error_status(transfers
[i
].transfer
);
511 returnval
= ERROR_FAIL
;
512 transfers
[i
].retval
= retval
;
513 LOG_DEBUG("ERROR, transfer %zu failed, error %d", i
, retval
);
515 /* Assuming actual_length is only valid if there is no transfer error.
517 transfers
[i
].transfer_size
= transfers
[i
].transfer
->actual_length
;
521 libusb_free_transfer(transfers
[i
].transfer
);
522 transfers
[i
].transfer
= NULL
;
532 static int stlink_usb_xfer_v1_get_status(void *handle
)
534 struct stlink_usb_handle_s
*h
= handle
;
537 assert(handle
!= NULL
);
540 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
542 ret
= jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
, 13,
543 STLINK_READ_TIMEOUT
, &tr
);
549 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
552 if (t1
!= 0x53425355)
560 if (h
->cmdbuf
[12] != 0)
566 #ifdef USE_LIBUSB_ASYNCIO
567 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
569 struct stlink_usb_handle_s
*h
= handle
;
571 assert(handle
!= NULL
);
573 size_t n_transfers
= 0;
574 struct jtag_xfer transfers
[2];
576 memset(transfers
, 0, sizeof(transfers
));
578 transfers
[0].ep
= h
->tx_ep
;
579 transfers
[0].buf
= h
->cmdbuf
;
580 transfers
[0].size
= cmdsize
;
584 if (h
->direction
== h
->tx_ep
&& size
) {
585 transfers
[1].ep
= h
->tx_ep
;
586 transfers
[1].buf
= (uint8_t *)buf
;
587 transfers
[1].size
= size
;
590 } else if (h
->direction
== h
->rx_ep
&& size
) {
591 transfers
[1].ep
= h
->rx_ep
;
592 transfers
[1].buf
= (uint8_t *)buf
;
593 transfers
[1].size
= size
;
598 return jtag_libusb_bulk_transfer_n(
602 STLINK_WRITE_TIMEOUT
);
605 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
607 struct stlink_usb_handle_s
*h
= handle
;
610 assert(handle
!= NULL
);
612 ret
= jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
,
613 cmdsize
, STLINK_WRITE_TIMEOUT
, &tr
);
614 if (ret
|| tr
!= cmdsize
)
617 if (h
->direction
== h
->tx_ep
&& size
) {
618 ret
= jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
619 size
, STLINK_WRITE_TIMEOUT
, &tr
);
620 if (ret
|| tr
!= size
) {
621 LOG_DEBUG("bulk write failed");
624 } else if (h
->direction
== h
->rx_ep
&& size
) {
625 ret
= jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
626 size
, STLINK_READ_TIMEOUT
, &tr
);
627 if (ret
|| tr
!= size
) {
628 LOG_DEBUG("bulk read failed");
638 static int stlink_usb_xfer_v1_get_sense(void *handle
)
641 struct stlink_usb_handle_s
*h
= handle
;
643 assert(handle
!= NULL
);
645 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
647 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
648 h
->cmdbuf
[h
->cmdidx
++] = 0;
649 h
->cmdbuf
[h
->cmdidx
++] = 0;
650 h
->cmdbuf
[h
->cmdidx
++] = 0;
651 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
653 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
658 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
665 transfers block in cmdbuf
666 <size> indicates number of bytes in the following
668 Ignore the (eventual) error code in the received packet.
670 static int stlink_usb_xfer_noerrcheck(void *handle
, const uint8_t *buf
, int size
)
672 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
673 struct stlink_usb_handle_s
*h
= handle
;
675 assert(handle
!= NULL
);
677 if (h
->version
.stlink
== 1) {
678 cmdsize
= STLINK_SG_SIZE
;
679 /* put length in bCBWCBLength */
680 h
->cmdbuf
[14] = h
->cmdidx
-15;
683 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
688 if (h
->version
.stlink
== 1) {
689 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
690 /* check csw status */
691 if (h
->cmdbuf
[12] == 1) {
692 LOG_DEBUG("get sense");
693 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
704 Converts an STLINK status code held in the first byte of a response
705 to an openocd error, logs any error/wait status as debug output.
707 static int stlink_usb_error_check(void *handle
)
709 struct stlink_usb_handle_s
*h
= handle
;
711 assert(handle
!= NULL
);
713 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
714 switch (h
->databuf
[0]) {
715 case STLINK_SWIM_ERR_OK
:
717 case STLINK_SWIM_BUSY
:
720 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
725 /* TODO: no error checking yet on api V1 */
726 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
727 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
729 switch (h
->databuf
[0]) {
730 case STLINK_DEBUG_ERR_OK
:
732 case STLINK_DEBUG_ERR_FAULT
:
733 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
735 case STLINK_SWD_AP_WAIT
:
736 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
738 case STLINK_SWD_DP_WAIT
:
739 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
741 case STLINK_JTAG_GET_IDCODE_ERROR
:
742 LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
744 case STLINK_JTAG_WRITE_ERROR
:
745 LOG_DEBUG("Write error");
747 case STLINK_JTAG_WRITE_VERIF_ERROR
:
748 LOG_DEBUG("Write verify error, ignoring");
750 case STLINK_SWD_AP_FAULT
:
751 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
752 * returns ERROR_OK with the comment:
753 * Change in error status when reading outside RAM.
754 * This fix allows CDT plugin to visualize memory.
756 LOG_DEBUG("STLINK_SWD_AP_FAULT");
758 case STLINK_SWD_AP_ERROR
:
759 LOG_DEBUG("STLINK_SWD_AP_ERROR");
761 case STLINK_SWD_AP_PARITY_ERROR
:
762 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
764 case STLINK_SWD_DP_FAULT
:
765 LOG_DEBUG("STLINK_SWD_DP_FAULT");
767 case STLINK_SWD_DP_ERROR
:
768 LOG_DEBUG("STLINK_SWD_DP_ERROR");
770 case STLINK_SWD_DP_PARITY_ERROR
:
771 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
773 case STLINK_SWD_AP_WDATA_ERROR
:
774 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
776 case STLINK_SWD_AP_STICKY_ERROR
:
777 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
779 case STLINK_SWD_AP_STICKYORUN_ERROR
:
780 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
782 case STLINK_BAD_AP_ERROR
:
783 LOG_DEBUG("STLINK_BAD_AP_ERROR");
786 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
792 * Wrapper around stlink_usb_xfer_noerrcheck()
793 * to check the error code in the received packet
795 static int stlink_usb_xfer_errcheck(void *handle
, const uint8_t *buf
, int size
)
801 retval
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
802 if (retval
!= ERROR_OK
)
805 return stlink_usb_error_check(handle
);
808 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
810 Works for commands where the STLINK_DEBUG status is returned in the first
811 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
813 Returns an openocd result code.
815 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
819 struct stlink_usb_handle_s
*h
= handle
;
822 if ((h
->st_mode
!= STLINK_MODE_DEBUG_SWIM
) || !retries
) {
823 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
828 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
829 res
= stlink_swim_status(handle
);
834 res
= stlink_usb_error_check(handle
);
835 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
836 unsigned int delay_us
= (1<<retries
++) * 1000;
837 LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries
, delay_us
);
846 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
848 struct stlink_usb_handle_s
*h
= handle
;
851 assert(handle
!= NULL
);
853 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
855 ret
= jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
, size
,
856 STLINK_READ_TIMEOUT
, &tr
);
857 if (ret
|| tr
!= size
) {
858 LOG_ERROR("bulk trace read failed");
866 this function writes transfer length in
867 the right place in the cb
869 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
871 struct stlink_usb_handle_s
*h
= handle
;
873 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
876 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
878 struct stlink_usb_handle_s
*h
= handle
;
880 /* fill the send buffer */
881 strcpy((char *)h
->cmdbuf
, "USBC");
883 /* csw tag not used */
884 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
886 /* cbw data transfer length (in the following data phase in or out) */
887 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
890 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
891 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
892 /* cdb clength (is filled in at xfer) */
893 h
->cmdbuf
[h
->cmdidx
++] = 0;
897 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
899 struct stlink_usb_handle_s
*h
= handle
;
901 h
->direction
= direction
;
905 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
906 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
908 if (h
->version
.stlink
== 1)
909 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
913 static int stlink_usb_version(void *handle
)
918 uint8_t v
, x
, y
, jtag
, swim
, msd
, bridge
= 0;
919 char v_str
[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
921 struct stlink_usb_handle_s
*h
= handle
;
923 assert(handle
!= NULL
);
925 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
927 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
929 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 6);
934 version
= be_to_h_u16(h
->databuf
);
935 v
= (version
>> 12) & 0x0f;
936 x
= (version
>> 6) & 0x3f;
939 h
->vid
= le_to_h_u16(h
->databuf
+ 2);
940 h
->pid
= le_to_h_u16(h
->databuf
+ 4);
943 case STLINK_V2_1_PID
:
944 case STLINK_V2_1_NO_MSD_PID
:
945 if ((x
<= 22 && y
== 7) || (x
>= 25 && y
>= 7 && y
<= 12)) {
946 /* MxSy : STM8 V2.1 - SWIM only */
951 /* JxMy : STM32 V2.1 - JTAG/SWD only */
964 /* STLINK-V3 requires a specific command */
965 if (v
== 3 && x
== 0 && y
== 0) {
966 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
968 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_VERSION_EX
;
970 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 12);
975 swim
= h
->databuf
[1];
976 jtag
= h
->databuf
[2];
978 bridge
= h
->databuf
[4];
979 h
->vid
= le_to_h_u16(h
->databuf
+ 8);
980 h
->pid
= le_to_h_u16(h
->databuf
+ 10);
983 h
->version
.stlink
= v
;
984 h
->version
.jtag
= jtag
;
985 h
->version
.swim
= swim
;
988 switch (h
->version
.stlink
) {
990 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
991 if (h
->version
.jtag
>= 11)
992 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
994 h
->version
.jtag_api
= STLINK_JTAG_API_V1
;
998 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
999 h
->version
.jtag_api
= STLINK_JTAG_API_V2
;
1001 /* API for trace from J13 */
1002 /* API for target voltage from J13 */
1003 if (h
->version
.jtag
>= 13)
1004 flags
|= STLINK_F_HAS_TRACE
;
1006 /* preferred API to get last R/W status from J15 */
1007 if (h
->version
.jtag
>= 15)
1008 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1010 /* API to set SWD frequency from J22 */
1011 if (h
->version
.jtag
>= 22)
1012 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
1014 /* API to set JTAG frequency from J24 */
1015 /* API to access DAP registers from J24 */
1016 if (h
->version
.jtag
>= 24) {
1017 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
1018 flags
|= STLINK_F_HAS_DAP_REG
;
1021 /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
1022 if (h
->version
.jtag
>= 24 && h
->version
.jtag
< 32)
1023 flags
|= STLINK_F_QUIRK_JTAG_DP_READ
;
1025 /* API to read/write memory at 16 bit from J26 */
1026 if (h
->version
.jtag
>= 26)
1027 flags
|= STLINK_F_HAS_MEM_16BIT
;
1029 /* API required to init AP before any AP access from J28 */
1030 if (h
->version
.jtag
>= 28)
1031 flags
|= STLINK_F_HAS_AP_INIT
;
1033 /* Banked regs (DPv1 & DPv2) support from V2J32 */
1034 if (h
->version
.jtag
>= 32)
1035 flags
|= STLINK_F_HAS_DPBANKSEL
;
1039 /* all STLINK-V3 use api-v3 */
1040 h
->version
.jtag_api
= STLINK_JTAG_API_V3
;
1042 /* STLINK-V3 is a superset of ST-LINK/V2 */
1045 /* API for target voltage */
1046 flags
|= STLINK_F_HAS_TRACE
;
1048 /* preferred API to get last R/W status */
1049 flags
|= STLINK_F_HAS_GETLASTRWSTATUS2
;
1051 /* API to access DAP registers */
1052 flags
|= STLINK_F_HAS_DAP_REG
;
1054 /* API to read/write memory at 16 bit */
1055 flags
|= STLINK_F_HAS_MEM_16BIT
;
1057 /* API required to init AP before any AP access */
1058 flags
|= STLINK_F_HAS_AP_INIT
;
1060 /* Banked regs (DPv1 & DPv2) support from V3J2 */
1061 if (h
->version
.jtag
>= 2)
1062 flags
|= STLINK_F_HAS_DPBANKSEL
;
1064 /* 8bit read/write max packet size 512 bytes from V3J6 */
1065 if (h
->version
.jtag
>= 6)
1066 flags
|= STLINK_F_HAS_RW8_512BYTES
;
1072 h
->version
.flags
= flags
;
1075 p
+= sprintf(p
, "V%d", v
);
1077 p
+= sprintf(p
, "J%d", jtag
);
1079 p
+= sprintf(p
, "M%d", msd
);
1081 p
+= sprintf(p
, "B%d", bridge
);
1083 sprintf(p
, "S%d", swim
);
1085 LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
1087 h
->version
.jtag_api
,
1094 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
1096 struct stlink_usb_handle_s
*h
= handle
;
1097 uint32_t adc_results
[2];
1099 /* no error message, simply quit with error */
1100 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
1101 return ERROR_COMMAND_NOTFOUND
;
1103 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1105 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
1107 int result
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1109 if (result
!= ERROR_OK
)
1112 /* convert result */
1113 adc_results
[0] = le_to_h_u32(h
->databuf
);
1114 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
1116 *target_voltage
= 0;
1119 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
1121 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
1126 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
1128 struct stlink_usb_handle_s
*h
= handle
;
1130 assert(handle
!= NULL
);
1132 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
1133 return ERROR_COMMAND_NOTFOUND
;
1135 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1137 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1138 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
1139 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1142 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1144 if (result
!= ERROR_OK
)
1150 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
1152 struct stlink_usb_handle_s
*h
= handle
;
1154 assert(handle
!= NULL
);
1156 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
1157 return ERROR_COMMAND_NOTFOUND
;
1159 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1161 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1162 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
1163 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
1166 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1168 if (result
!= ERROR_OK
)
1175 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
1178 struct stlink_usb_handle_s
*h
= handle
;
1180 assert(handle
!= NULL
);
1182 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1184 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
1186 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1188 if (res
!= ERROR_OK
)
1191 *mode
= h
->databuf
[0];
1197 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
1200 struct stlink_usb_handle_s
*h
= handle
;
1202 assert(handle
!= NULL
);
1204 /* on api V2 we are able the read the latest command
1206 * TODO: we need the test on api V1 too
1208 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
)
1211 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
1214 case STLINK_MODE_DEBUG_JTAG
:
1215 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1216 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1217 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1219 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1220 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET
;
1222 case STLINK_MODE_DEBUG_SWD
:
1223 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1224 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1225 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
1227 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
1228 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD_NO_RESET
;
1230 case STLINK_MODE_DEBUG_SWIM
:
1231 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1232 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
1233 /* swim enter does not return any response or status */
1234 return stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1235 case STLINK_MODE_DFU
:
1236 case STLINK_MODE_MASS
:
1241 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
1245 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
1248 struct stlink_usb_handle_s
*h
= handle
;
1250 assert(handle
!= NULL
);
1252 /* command with no reply, use a valid endpoint but zero size */
1253 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1256 case STLINK_MODE_DEBUG_JTAG
:
1257 case STLINK_MODE_DEBUG_SWD
:
1258 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1259 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
1261 case STLINK_MODE_DEBUG_SWIM
:
1262 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1263 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
1265 case STLINK_MODE_DFU
:
1266 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
1267 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
1269 case STLINK_MODE_MASS
:
1274 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 0);
1276 if (res
!= ERROR_OK
)
1282 static int stlink_usb_assert_srst(void *handle
, int srst
);
1284 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
1287 case HL_TRANSPORT_SWD
:
1288 return STLINK_MODE_DEBUG_SWD
;
1289 case HL_TRANSPORT_JTAG
:
1290 return STLINK_MODE_DEBUG_JTAG
;
1292 return STLINK_MODE_UNKNOWN
;
1297 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1301 enum stlink_mode emode
;
1302 struct stlink_usb_handle_s
*h
= handle
;
1304 assert(handle
!= NULL
);
1306 res
= stlink_usb_current_mode(handle
, &mode
);
1308 if (res
!= ERROR_OK
)
1311 LOG_DEBUG("MODE: 0x%02X", mode
);
1313 /* try to exit current mode */
1315 case STLINK_DEV_DFU_MODE
:
1316 emode
= STLINK_MODE_DFU
;
1318 case STLINK_DEV_DEBUG_MODE
:
1319 emode
= STLINK_MODE_DEBUG_SWD
;
1321 case STLINK_DEV_SWIM_MODE
:
1322 emode
= STLINK_MODE_DEBUG_SWIM
;
1324 case STLINK_DEV_BOOTLOADER_MODE
:
1325 case STLINK_DEV_MASS_MODE
:
1327 emode
= STLINK_MODE_UNKNOWN
;
1331 if (emode
!= STLINK_MODE_UNKNOWN
) {
1332 res
= stlink_usb_mode_leave(handle
, emode
);
1334 if (res
!= ERROR_OK
)
1338 res
= stlink_usb_current_mode(handle
, &mode
);
1340 if (res
!= ERROR_OK
)
1343 /* we check the target voltage here as an aid to debugging connection problems.
1344 * the stlink requires the target Vdd to be connected for reliable debugging.
1345 * this cmd is supported in all modes except DFU
1347 if (mode
!= STLINK_DEV_DFU_MODE
) {
1349 float target_voltage
;
1351 /* check target voltage (if supported) */
1352 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1354 if (res
!= ERROR_OK
) {
1355 if (res
!= ERROR_COMMAND_NOTFOUND
)
1356 LOG_ERROR("voltage check failed");
1357 /* attempt to continue as it is not a catastrophic failure */
1359 /* check for a sensible target voltage, operating range is 1.65-5.5v
1360 * according to datasheet */
1361 if (target_voltage
< 1.5)
1362 LOG_ERROR("target voltage may be too low for reliable debugging");
1366 LOG_DEBUG("MODE: 0x%02X", mode
);
1368 /* set selected mode */
1371 if (emode
== STLINK_MODE_UNKNOWN
) {
1372 LOG_ERROR("selected mode (transport) not supported");
1376 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1377 if (emode
== STLINK_MODE_DEBUG_JTAG
) {
1378 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1379 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1380 stlink_speed(h
, initial_interface_speed
, false);
1382 } else if (emode
== STLINK_MODE_DEBUG_SWD
) {
1383 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1384 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1385 stlink_speed(h
, initial_interface_speed
, false);
1389 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
1390 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1392 stlink_get_com_freq(h
, (emode
== STLINK_MODE_DEBUG_JTAG
), map
);
1393 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1394 stlink_speed(h
, initial_interface_speed
, false);
1397 /* preliminary SRST assert:
1398 * We want SRST is asserted before activating debug signals (mode_enter).
1399 * As the required mode has not been set, the adapter may not know what pin to use.
1400 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1401 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1402 * after power on, SWIM_RST stays unchanged */
1403 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1404 stlink_usb_assert_srst(handle
, 0);
1405 /* do not check the return status here, we will
1406 proceed and enter the desired mode below
1407 and try asserting srst again. */
1409 res
= stlink_usb_mode_enter(handle
, emode
);
1410 if (res
!= ERROR_OK
)
1413 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1414 if (connect_under_reset
) {
1415 res
= stlink_usb_assert_srst(handle
, 0);
1416 if (res
!= ERROR_OK
)
1420 res
= stlink_usb_current_mode(handle
, &mode
);
1422 if (res
!= ERROR_OK
)
1425 LOG_DEBUG("MODE: 0x%02X", mode
);
1430 /* request status from last swim request */
1431 static int stlink_swim_status(void *handle
)
1433 struct stlink_usb_handle_s
*h
= handle
;
1436 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1437 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1438 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1439 /* error is checked by the caller */
1440 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1441 if (res
!= ERROR_OK
)
1446 the purpose of this function is unknown...
1447 capabilites? anyway for swim v6 it returns
1450 __attribute__((unused
))
1451 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1453 struct stlink_usb_handle_s
*h
= handle
;
1456 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1457 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1458 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1459 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1460 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1461 if (res
!= ERROR_OK
)
1463 memcpy(cap
, h
->databuf
, 8);
1467 /* debug dongle assert/deassert sreset line */
1468 static int stlink_swim_assert_reset(void *handle
, int reset
)
1470 struct stlink_usb_handle_s
*h
= handle
;
1473 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1474 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1476 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1478 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1479 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1480 if (res
!= ERROR_OK
)
1487 1.3ms low then 750Hz then 1.5kHz
1489 static int stlink_swim_enter(void *handle
)
1491 struct stlink_usb_handle_s
*h
= handle
;
1494 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1495 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1496 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1497 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1498 if (res
!= ERROR_OK
)
1503 /* switch high/low speed swim */
1504 static int stlink_swim_speed(void *handle
, int speed
)
1506 struct stlink_usb_handle_s
*h
= handle
;
1509 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1510 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1511 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1513 h
->cmdbuf
[h
->cmdidx
++] = 1;
1515 h
->cmdbuf
[h
->cmdidx
++] = 0;
1516 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1517 if (res
!= ERROR_OK
)
1523 initiate srst from swim.
1524 nrst is pulled low for 50us.
1526 static int stlink_swim_generate_rst(void *handle
)
1528 struct stlink_usb_handle_s
*h
= handle
;
1531 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1532 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1533 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1534 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1535 if (res
!= ERROR_OK
)
1541 send resyncronize sequence
1542 swim is pulled low for 16us
1543 reply is 64 clks low
1545 static int stlink_swim_resync(void *handle
)
1547 struct stlink_usb_handle_s
*h
= handle
;
1550 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1551 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1552 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1553 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1554 if (res
!= ERROR_OK
)
1559 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1561 struct stlink_usb_handle_s
*h
= handle
;
1564 unsigned int datalen
= 0;
1565 int cmdsize
= STLINK_CMD_SIZE_V2
;
1567 if (len
> STLINK_DATA_SIZE
)
1570 if (h
->version
.stlink
== 1)
1571 cmdsize
= STLINK_SG_SIZE
;
1573 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1574 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1575 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1576 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1578 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1580 for (i
= 0; i
< len
; i
++) {
1581 if (h
->cmdidx
== cmdsize
)
1582 h
->databuf
[datalen
++] = *(data
++);
1584 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1586 if (h
->version
.stlink
== 1)
1587 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1589 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1590 if (res
!= ERROR_OK
)
1595 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1597 struct stlink_usb_handle_s
*h
= handle
;
1600 if (len
> STLINK_DATA_SIZE
)
1603 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1604 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1605 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1606 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1608 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1610 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1611 if (res
!= ERROR_OK
)
1614 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1615 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1616 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1617 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1618 if (res
!= ERROR_OK
)
1625 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1628 struct stlink_usb_handle_s
*h
= handle
;
1630 assert(handle
!= NULL
);
1632 /* there is no swim read core id cmd */
1633 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
1638 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1640 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1641 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1642 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1644 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1647 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1649 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1653 if (res
!= ERROR_OK
)
1656 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1658 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1663 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1665 struct stlink_usb_handle_s
*h
= handle
;
1668 assert(handle
!= NULL
);
1670 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1672 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1673 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1674 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1677 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1678 if (res
!= ERROR_OK
)
1681 *val
= le_to_h_u32(h
->databuf
+ 4);
1685 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1687 struct stlink_usb_handle_s
*h
= handle
;
1689 assert(handle
!= NULL
);
1691 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1693 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1694 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1695 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1697 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1698 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1700 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1703 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1707 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1709 struct stlink_usb_handle_s
*h
= handle
;
1711 assert(handle
!= NULL
);
1713 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1716 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1718 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1719 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1721 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1722 if (res
!= ERROR_OK
)
1725 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1726 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1729 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1730 if (res
!= ERROR_OK
)
1739 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1744 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1745 if (result
!= ERROR_OK
)
1746 return TARGET_UNKNOWN
;
1748 if (status
& S_HALT
)
1749 return TARGET_HALTED
;
1750 else if (status
& S_RESET_ST
)
1751 return TARGET_RESET
;
1753 return TARGET_RUNNING
;
1757 static enum target_state
stlink_usb_state(void *handle
)
1760 struct stlink_usb_handle_s
*h
= handle
;
1762 assert(handle
!= NULL
);
1764 if (h
->reconnect_pending
) {
1765 LOG_INFO("Previous state query failed, trying to reconnect");
1766 res
= stlink_usb_mode_enter(handle
, h
->st_mode
);
1767 if (res
!= ERROR_OK
)
1768 return TARGET_UNKNOWN
;
1770 h
->reconnect_pending
= false;
1773 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1774 res
= stlink_usb_v2_get_status(handle
);
1775 if (res
== TARGET_UNKNOWN
)
1776 h
->reconnect_pending
= true;
1780 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1782 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1783 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1785 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1787 if (res
!= ERROR_OK
)
1788 return TARGET_UNKNOWN
;
1790 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1791 return TARGET_RUNNING
;
1792 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1793 return TARGET_HALTED
;
1795 h
->reconnect_pending
= true;
1797 return TARGET_UNKNOWN
;
1800 static int stlink_usb_assert_srst(void *handle
, int srst
)
1802 struct stlink_usb_handle_s
*h
= handle
;
1804 assert(handle
!= NULL
);
1806 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
)
1807 return stlink_swim_assert_reset(handle
, srst
);
1809 if (h
->version
.stlink
== 1)
1810 return ERROR_COMMAND_NOTFOUND
;
1812 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1814 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1815 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1816 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1818 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1822 static void stlink_usb_trace_disable(void *handle
)
1825 struct stlink_usb_handle_s
*h
= handle
;
1827 assert(handle
!= NULL
);
1829 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1831 LOG_DEBUG("Tracing: disable");
1833 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1835 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1836 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1838 if (res
== ERROR_OK
)
1839 h
->trace
.enabled
= false;
1844 static int stlink_usb_trace_enable(void *handle
)
1847 struct stlink_usb_handle_s
*h
= handle
;
1849 assert(handle
!= NULL
);
1851 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1852 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1854 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1855 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1856 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1858 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1861 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1863 if (res
== ERROR_OK
) {
1864 h
->trace
.enabled
= true;
1865 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1868 LOG_ERROR("Tracing is not supported by this version.");
1876 static int stlink_usb_reset(void *handle
)
1878 struct stlink_usb_handle_s
*h
= handle
;
1881 assert(handle
!= NULL
);
1883 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1885 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1887 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1888 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1890 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1892 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1893 if (retval
!= ERROR_OK
)
1896 if (h
->trace
.enabled
) {
1897 stlink_usb_trace_disable(h
);
1898 return stlink_usb_trace_enable(h
);
1905 static int stlink_usb_run(void *handle
)
1908 struct stlink_usb_handle_s
*h
= handle
;
1910 assert(handle
!= NULL
);
1912 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1913 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1918 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1920 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1921 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1923 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1927 static int stlink_usb_halt(void *handle
)
1930 struct stlink_usb_handle_s
*h
= handle
;
1932 assert(handle
!= NULL
);
1934 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1935 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1940 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1942 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1943 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1945 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1949 static int stlink_usb_step(void *handle
)
1951 struct stlink_usb_handle_s
*h
= handle
;
1953 assert(handle
!= NULL
);
1955 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1956 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1957 * that the Cortex-M3 currently does. */
1958 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1959 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1960 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1963 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1965 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1966 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1968 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1972 static int stlink_usb_read_regs(void *handle
)
1975 struct stlink_usb_handle_s
*h
= handle
;
1977 assert(handle
!= NULL
);
1979 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1981 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1982 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1984 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1985 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
1986 /* regs data from offset 0 */
1988 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1989 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
1990 /* status at offset 0, regs data from offset 4 */
1997 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
2000 struct stlink_usb_handle_s
*h
= handle
;
2002 assert(handle
!= NULL
);
2004 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2006 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2007 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2008 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2010 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2011 h
->cmdbuf
[h
->cmdidx
++] = num
;
2013 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2014 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2015 if (res
!= ERROR_OK
)
2017 *val
= le_to_h_u32(h
->databuf
);
2020 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2021 if (res
!= ERROR_OK
)
2023 *val
= le_to_h_u32(h
->databuf
+ 4);
2029 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
2031 struct stlink_usb_handle_s
*h
= handle
;
2033 assert(handle
!= NULL
);
2035 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2037 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2038 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2039 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2041 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2042 h
->cmdbuf
[h
->cmdidx
++] = num
;
2043 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2046 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2049 static int stlink_usb_get_rw_status(void *handle
)
2051 struct stlink_usb_handle_s
*h
= handle
;
2053 assert(handle
!= NULL
);
2055 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2058 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2060 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2061 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2062 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2063 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2065 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2066 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2071 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2075 uint16_t read_len
= len
;
2076 struct stlink_usb_handle_s
*h
= handle
;
2078 assert(handle
!= NULL
);
2080 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2081 if (len
> stlink_usb_block(h
)) {
2082 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2086 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2088 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2089 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2090 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2092 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2095 /* we need to fix read length for single bytes */
2099 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2101 if (res
!= ERROR_OK
)
2104 memcpy(buffer
, h
->databuf
, len
);
2106 return stlink_usb_get_rw_status(handle
);
2110 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2111 const uint8_t *buffer
)
2114 struct stlink_usb_handle_s
*h
= handle
;
2116 assert(handle
!= NULL
);
2118 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2119 if (len
> stlink_usb_block(h
)) {
2120 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2124 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2126 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2127 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2128 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2130 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2133 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2135 if (res
!= ERROR_OK
)
2138 return stlink_usb_get_rw_status(handle
);
2142 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2146 struct stlink_usb_handle_s
*h
= handle
;
2148 assert(handle
!= NULL
);
2150 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2151 return ERROR_COMMAND_NOTFOUND
;
2153 /* data must be a multiple of 2 and half-word aligned */
2154 if (len
% 2 || addr
% 2) {
2155 LOG_DEBUG("Invalid data alignment");
2156 return ERROR_TARGET_UNALIGNED_ACCESS
;
2159 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2161 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2162 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2163 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2165 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2168 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2170 if (res
!= ERROR_OK
)
2173 memcpy(buffer
, h
->databuf
, len
);
2175 return stlink_usb_get_rw_status(handle
);
2179 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2180 const uint8_t *buffer
)
2183 struct stlink_usb_handle_s
*h
= handle
;
2185 assert(handle
!= NULL
);
2187 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2188 return ERROR_COMMAND_NOTFOUND
;
2190 /* data must be a multiple of 2 and half-word aligned */
2191 if (len
% 2 || addr
% 2) {
2192 LOG_DEBUG("Invalid data alignment");
2193 return ERROR_TARGET_UNALIGNED_ACCESS
;
2196 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2198 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2199 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2200 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2202 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2205 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2207 if (res
!= ERROR_OK
)
2210 return stlink_usb_get_rw_status(handle
);
2214 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2218 struct stlink_usb_handle_s
*h
= handle
;
2220 assert(handle
!= NULL
);
2222 /* data must be a multiple of 4 and word aligned */
2223 if (len
% 4 || addr
% 4) {
2224 LOG_DEBUG("Invalid data alignment");
2225 return ERROR_TARGET_UNALIGNED_ACCESS
;
2228 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2230 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2231 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2232 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2234 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2237 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2239 if (res
!= ERROR_OK
)
2242 memcpy(buffer
, h
->databuf
, len
);
2244 return stlink_usb_get_rw_status(handle
);
2248 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2249 const uint8_t *buffer
)
2252 struct stlink_usb_handle_s
*h
= handle
;
2254 assert(handle
!= NULL
);
2256 /* data must be a multiple of 4 and word aligned */
2257 if (len
% 4 || addr
% 4) {
2258 LOG_DEBUG("Invalid data alignment");
2259 return ERROR_TARGET_UNALIGNED_ACCESS
;
2262 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2264 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2265 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2266 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2268 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2271 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2273 if (res
!= ERROR_OK
)
2276 return stlink_usb_get_rw_status(handle
);
2279 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2281 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2282 if (max_tar_block
== 0)
2284 return max_tar_block
;
2287 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2288 uint32_t count
, uint8_t *buffer
)
2290 int retval
= ERROR_OK
;
2291 uint32_t bytes_remaining
;
2293 struct stlink_usb_handle_s
*h
= handle
;
2295 /* calculate byte count */
2298 /* switch to 8 bit if stlink does not support 16 bit memory read */
2299 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2304 bytes_remaining
= (size
!= 1) ?
2305 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2307 if (count
< bytes_remaining
)
2308 bytes_remaining
= count
;
2311 * all stlink support 8/32bit memory read/writes and only from
2312 * stlink V2J26 there is support for 16 bit memory read/write.
2313 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2318 /* When in jtag mode the stlink uses the auto-increment functionality.
2319 * However it expects us to pass the data correctly, this includes
2320 * alignment and any page boundaries. We already do this as part of the
2321 * adi_v5 implementation, but the stlink is a hla adapter and so this
2322 * needs implementing manually.
2323 * currently this only affects jtag mode, according to ST they do single
2324 * access in SWD mode - but this may change and so we do it for both modes */
2326 /* we first need to check for any unaligned bytes */
2327 if (addr
& (size
- 1)) {
2329 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2330 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2331 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2332 usleep((1<<retries
++) * 1000);
2335 if (retval
!= ERROR_OK
)
2337 buffer
+= head_bytes
;
2339 count
-= head_bytes
;
2340 bytes_remaining
-= head_bytes
;
2343 if (bytes_remaining
& (size
- 1))
2344 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2346 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2348 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2350 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2352 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2353 usleep((1<<retries
++) * 1000);
2356 if (retval
!= ERROR_OK
)
2359 buffer
+= bytes_remaining
;
2360 addr
+= bytes_remaining
;
2361 count
-= bytes_remaining
;
2367 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2368 uint32_t count
, const uint8_t *buffer
)
2370 int retval
= ERROR_OK
;
2371 uint32_t bytes_remaining
;
2373 struct stlink_usb_handle_s
*h
= handle
;
2375 /* calculate byte count */
2378 /* switch to 8 bit if stlink does not support 16 bit memory read */
2379 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2384 bytes_remaining
= (size
!= 1) ?
2385 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2387 if (count
< bytes_remaining
)
2388 bytes_remaining
= count
;
2391 * all stlink support 8/32bit memory read/writes and only from
2392 * stlink V2J26 there is support for 16 bit memory read/write.
2393 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2398 /* When in jtag mode the stlink uses the auto-increment functionality.
2399 * However it expects us to pass the data correctly, this includes
2400 * alignment and any page boundaries. We already do this as part of the
2401 * adi_v5 implementation, but the stlink is a hla adapter and so this
2402 * needs implementing manually.
2403 * currently this only affects jtag mode, according to ST they do single
2404 * access in SWD mode - but this may change and so we do it for both modes */
2406 /* we first need to check for any unaligned bytes */
2407 if (addr
& (size
- 1)) {
2409 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2410 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2411 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2412 usleep((1<<retries
++) * 1000);
2415 if (retval
!= ERROR_OK
)
2417 buffer
+= head_bytes
;
2419 count
-= head_bytes
;
2420 bytes_remaining
-= head_bytes
;
2423 if (bytes_remaining
& (size
- 1))
2424 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2426 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2428 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2431 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2432 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2433 usleep((1<<retries
++) * 1000);
2436 if (retval
!= ERROR_OK
)
2439 buffer
+= bytes_remaining
;
2440 addr
+= bytes_remaining
;
2441 count
-= bytes_remaining
;
2448 static int stlink_usb_override_target(const char *targetname
)
2450 return !strcmp(targetname
, "cortex_m");
2453 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2458 we only have low and high speed...
2459 before changing speed the SWIM_CSR HS bit
2463 retval
= stlink_swim_speed(handle
, (khz
< SWIM_FREQ_HIGH
) ? 0 : 1);
2464 if (retval
!= ERROR_OK
)
2465 LOG_ERROR("Unable to set adapter speed");
2468 return (khz
< SWIM_FREQ_HIGH
) ? SWIM_FREQ_LOW
: SWIM_FREQ_HIGH
;
2471 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2474 int speed_index
= -1;
2475 int speed_diff
= INT_MAX
;
2476 int last_valid_speed
= -1;
2479 for (i
= 0; i
< map_size
; i
++) {
2482 last_valid_speed
= i
;
2483 if (khz
== map
[i
].speed
) {
2487 int current_diff
= khz
- map
[i
].speed
;
2488 /* get abs value for comparison */
2489 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2490 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2491 speed_diff
= current_diff
;
2497 if (speed_index
== -1) {
2498 /* this will only be here if we cannot match the slow speed.
2499 * use the slowest speed we support.*/
2500 speed_index
= last_valid_speed
;
2502 } else if (i
== map_size
)
2505 if (!match
&& query
) {
2506 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz",
2507 khz
, map
[speed_index
].speed
);
2513 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2516 struct stlink_usb_handle_s
*h
= handle
;
2518 /* old firmware cannot change it */
2519 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2522 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2523 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2526 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2527 if (result
!= ERROR_OK
) {
2528 LOG_ERROR("Unable to set adapter speed");
2533 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2536 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2539 struct stlink_usb_handle_s
*h
= handle
;
2541 /* old firmware cannot change it */
2542 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2545 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2546 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2549 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2550 if (result
!= ERROR_OK
) {
2551 LOG_ERROR("Unable to set adapter speed");
2556 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2559 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2563 LOG_DEBUG("Supported clock speeds are:");
2564 for (i
= 0; i
< map_size
; i
++)
2566 LOG_DEBUG("%d kHz", map
[i
].speed
);
2569 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2571 struct stlink_usb_handle_s
*h
= handle
;
2574 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2575 LOG_ERROR("Unknown command");
2579 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2581 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2582 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2583 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2585 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2587 int size
= h
->databuf
[8];
2589 if (size
> STLINK_V3_MAX_FREQ_NB
)
2590 size
= STLINK_V3_MAX_FREQ_NB
;
2592 for (i
= 0; i
< size
; i
++) {
2593 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2594 map
[i
].speed_divisor
= i
;
2597 /* set to zero all the next entries */
2598 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2604 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2606 struct stlink_usb_handle_s
*h
= handle
;
2608 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2609 LOG_ERROR("Unknown command");
2613 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2615 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2616 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2617 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2618 h
->cmdbuf
[h
->cmdidx
++] = 0;
2620 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2622 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2625 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2627 struct stlink_usb_handle_s
*h
= handle
;
2629 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2631 stlink_get_com_freq(h
, is_jtag
, map
);
2633 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2636 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2637 if (result
!= ERROR_OK
) {
2638 LOG_ERROR("Unable to set adapter speed");
2642 return map
[speed_index
].speed
;
2645 static int stlink_speed(void *handle
, int khz
, bool query
)
2647 struct stlink_usb_handle_s
*h
= handle
;
2652 switch (h
->st_mode
) {
2653 case STLINK_MODE_DEBUG_SWIM
:
2654 return stlink_speed_swim(handle
, khz
, query
);
2655 case STLINK_MODE_DEBUG_SWD
:
2656 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2657 return stlink_speed_v3(handle
, false, khz
, query
);
2659 return stlink_speed_swd(handle
, khz
, query
);
2661 case STLINK_MODE_DEBUG_JTAG
:
2662 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2663 return stlink_speed_v3(handle
, true, khz
, query
);
2665 return stlink_speed_jtag(handle
, khz
, query
);
2675 static int stlink_usb_close(void *handle
)
2679 enum stlink_mode emode
;
2680 struct stlink_usb_handle_s
*h
= handle
;
2683 res
= stlink_usb_current_mode(handle
, &mode
);
2686 /* do not exit if return code != ERROR_OK,
2687 it prevents us from closing jtag_libusb */
2689 if (res
== ERROR_OK
) {
2690 /* try to exit current mode */
2692 case STLINK_DEV_DFU_MODE
:
2693 emode
= STLINK_MODE_DFU
;
2695 case STLINK_DEV_DEBUG_MODE
:
2696 emode
= STLINK_MODE_DEBUG_SWD
;
2698 case STLINK_DEV_SWIM_MODE
:
2699 emode
= STLINK_MODE_DEBUG_SWIM
;
2701 case STLINK_DEV_BOOTLOADER_MODE
:
2702 case STLINK_DEV_MASS_MODE
:
2704 emode
= STLINK_MODE_UNKNOWN
;
2708 if (emode
!= STLINK_MODE_UNKNOWN
)
2709 stlink_usb_mode_leave(handle
, emode
);
2710 /* do not check return code, it prevent
2711 us from closing jtag_libusb */
2715 jtag_libusb_close(h
->fd
);
2722 /* Compute ST-Link serial number from the device descriptor
2723 * this function will help to work-around a bug in old ST-Link/V2 DFU
2724 * the buggy DFU returns an incorrect serial in the USB descriptor
2725 * example for the following serial "57FF72067265575742132067"
2726 * - the correct descriptor serial is:
2727 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
2728 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
2729 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
2730 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
2731 * so this case is managed by libusb_helper::string_descriptor_equal
2732 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
2733 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
2735 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
2736 * and then we have just to convert the raw data into printable characters using sprintf
2738 char *stlink_usb_get_alternate_serial(libusb_device_handle
*device
,
2739 struct libusb_device_descriptor
*dev_desc
)
2742 unsigned char desc_serial
[(STLINK_SERIAL_LEN
+ 1) * 2];
2744 if (dev_desc
->iSerialNumber
== 0)
2747 /* get the LANGID from String Descriptor Zero */
2748 usb_retval
= libusb_get_string_descriptor(device
, 0, 0, desc_serial
,
2749 sizeof(desc_serial
));
2751 if (usb_retval
< LIBUSB_SUCCESS
) {
2752 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2753 libusb_error_name(usb_retval
), usb_retval
);
2755 } else if (usb_retval
< 4) {
2756 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
2757 LOG_ERROR("could not get the LANGID");
2761 uint32_t langid
= desc_serial
[2] | (desc_serial
[3] << 8);
2763 /* get the serial */
2764 usb_retval
= libusb_get_string_descriptor(device
, dev_desc
->iSerialNumber
,
2765 langid
, desc_serial
, sizeof(desc_serial
));
2767 unsigned char len
= desc_serial
[0];
2769 if (usb_retval
< LIBUSB_SUCCESS
) {
2770 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2771 libusb_error_name(usb_retval
), usb_retval
);
2773 } else if (desc_serial
[1] != LIBUSB_DT_STRING
|| len
> usb_retval
) {
2774 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
2778 if (len
== ((STLINK_SERIAL_LEN
+ 1) * 2)) {
2779 /* good ST-Link adapter, this case is managed by
2780 * libusb::libusb_get_string_descriptor_ascii */
2782 } else if (len
!= ((STLINK_SERIAL_LEN
/ 2 + 1) * 2)) {
2783 LOG_ERROR("unexpected serial length (%d) in descriptor", len
);
2787 /* else (len == 26) => buggy ST-Link */
2789 char *alternate_serial
= malloc((STLINK_SERIAL_LEN
+ 1) * sizeof(char));
2790 if (alternate_serial
== NULL
)
2793 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
2794 sprintf(alternate_serial
+ i
, "%02X", desc_serial
[i
+ 2]);
2796 alternate_serial
[STLINK_SERIAL_LEN
] = '\0';
2798 return alternate_serial
;
2802 static int stlink_usb_open(struct hl_interface_param_s
*param
, enum stlink_mode mode
, void **fd
)
2804 int err
, retry_count
= 1;
2805 struct stlink_usb_handle_s
*h
;
2807 LOG_DEBUG("stlink_usb_open");
2809 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2812 LOG_DEBUG("malloc failed");
2818 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2819 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2820 h
->st_mode
, param
->vid
[i
], param
->pid
[i
],
2821 param
->serial
? param
->serial
: "");
2825 On certain host USB configurations(e.g. MacBook Air)
2826 STLINKv2 dongle seems to have its FW in a funky state if,
2827 after plugging it in, you try to use openocd with it more
2828 then once (by launching and closing openocd). In cases like
2829 that initial attempt to read the FW info via
2830 stlink_usb_version will fail and the device has to be reset
2831 in order to become operational.
2834 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
,
2835 &h
->fd
, stlink_usb_get_alternate_serial
) != ERROR_OK
) {
2836 LOG_ERROR("open failed");
2840 jtag_libusb_set_configuration(h
->fd
, 0);
2842 if (libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2843 LOG_DEBUG("claim interface failed");
2847 /* RX EP is common for all versions */
2848 h
->rx_ep
= STLINK_RX_EP
;
2851 if (jtag_libusb_get_pid(libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2852 LOG_DEBUG("libusb_get_pid failed");
2856 /* wrap version for first read */
2859 h
->version
.stlink
= 1;
2860 h
->tx_ep
= STLINK_TX_EP
;
2862 case STLINK_V3_USBLOADER_PID
:
2863 case STLINK_V3E_PID
:
2864 case STLINK_V3S_PID
:
2865 case STLINK_V3_2VCP_PID
:
2866 h
->version
.stlink
= 3;
2867 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2868 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2870 case STLINK_V2_1_PID
:
2871 case STLINK_V2_1_NO_MSD_PID
:
2872 h
->version
.stlink
= 2;
2873 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2874 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2877 /* fall through - we assume V2 to be the default version*/
2879 h
->version
.stlink
= 2;
2880 h
->tx_ep
= STLINK_TX_EP
;
2881 h
->trace_ep
= STLINK_TRACE_EP
;
2885 /* get the device version */
2886 err
= stlink_usb_version(h
);
2888 if (err
== ERROR_OK
) {
2890 } else if (h
->version
.stlink
== 1 ||
2892 LOG_ERROR("read version failed");
2895 err
= libusb_release_interface(h
->fd
, 0);
2896 if (err
!= ERROR_OK
) {
2897 LOG_ERROR("release interface failed");
2901 err
= libusb_reset_device(h
->fd
);
2902 if (err
!= ERROR_OK
) {
2903 LOG_ERROR("reset device failed");
2907 jtag_libusb_close(h
->fd
);
2909 Give the device one second to settle down and
2912 usleep(1 * 1000 * 1000);
2917 /* check if mode is supported */
2920 switch (h
->st_mode
) {
2921 case STLINK_MODE_DEBUG_SWD
:
2922 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2925 case STLINK_MODE_DEBUG_JTAG
:
2926 if (h
->version
.jtag
== 0)
2929 case STLINK_MODE_DEBUG_SWIM
:
2930 if (h
->version
.swim
== 0)
2938 if (err
!= ERROR_OK
) {
2939 LOG_ERROR("mode (transport) not supported by device");
2943 /* initialize the debug hardware */
2944 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
2946 if (err
!= ERROR_OK
) {
2947 LOG_ERROR("init mode failed (unable to connect to the target)");
2951 if (h
->st_mode
== STLINK_MODE_DEBUG_SWIM
) {
2952 err
= stlink_swim_enter(h
);
2953 if (err
!= ERROR_OK
) {
2954 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2958 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2962 /* get cpuid, so we can determine the max page size
2963 * start with a safe default */
2964 h
->max_mem_packet
= (1 << 10);
2967 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2968 if (err
== ERROR_OK
) {
2969 uint32_t cpuid
= le_to_h_u32(buffer
);
2970 int i
= (cpuid
>> 4) & 0xf;
2971 if (i
== 4 || i
== 3) {
2972 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2973 h
->max_mem_packet
= (1 << 12);
2977 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2984 stlink_usb_close(h
);
2989 static int stlink_usb_hl_open(struct hl_interface_param_s
*param
, void **fd
)
2991 return stlink_usb_open(param
, stlink_get_mode(param
->transport
), fd
);
2994 int stlink_config_trace(void *handle
, bool enabled
,
2995 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
2996 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
2997 uint16_t *prescaler
)
2999 struct stlink_usb_handle_s
*h
= handle
;
3002 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
3003 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
3004 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3009 stlink_usb_trace_disable(h
);
3013 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
3014 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3015 STLINK_TRACE_MAX_HZ
);
3019 stlink_usb_trace_disable(h
);
3022 *trace_freq
= STLINK_TRACE_MAX_HZ
;
3024 presc
= traceclkin_freq
/ *trace_freq
;
3026 if (traceclkin_freq
% *trace_freq
> 0)
3029 if (presc
> TPIU_ACPR_MAX_SWOSCALER
) {
3030 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3036 h
->trace
.source_hz
= *trace_freq
;
3038 return stlink_usb_trace_enable(h
);
3042 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
3044 struct stlink_usb_handle_s
*h
= handle
;
3046 assert(handle
!= NULL
);
3048 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3049 return ERROR_COMMAND_NOTFOUND
;
3051 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
3052 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3053 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3054 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
3055 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3057 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3061 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
3063 struct stlink_usb_handle_s
*h
= handle
;
3065 assert(handle
!= NULL
);
3067 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3068 return ERROR_COMMAND_NOTFOUND
;
3070 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
3071 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3072 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3073 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG
;
3074 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3076 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3080 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3081 unsigned short addr
, uint32_t *val
)
3083 struct stlink_usb_handle_s
*h
= handle
;
3086 assert(handle
!= NULL
);
3088 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3089 return ERROR_COMMAND_NOTFOUND
;
3091 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3092 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3093 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3094 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3095 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3097 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3098 *val
= le_to_h_u32(h
->databuf
+ 4);
3099 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, *val
);
3104 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3105 unsigned short addr
, uint32_t val
)
3107 struct stlink_usb_handle_s
*h
= handle
;
3109 assert(handle
!= NULL
);
3111 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3112 return ERROR_COMMAND_NOTFOUND
;
3114 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, val
);
3115 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3116 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3117 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3118 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3119 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3120 h_u32_to_le(&h
->cmdbuf
[6], val
);
3121 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3125 struct hl_layout_api_s stlink_usb_layout_api
= {
3127 .open
= stlink_usb_hl_open
,
3129 .close
= stlink_usb_close
,
3131 .idcode
= stlink_usb_idcode
,
3133 .state
= stlink_usb_state
,
3135 .reset
= stlink_usb_reset
,
3137 .assert_srst
= stlink_usb_assert_srst
,
3139 .run
= stlink_usb_run
,
3141 .halt
= stlink_usb_halt
,
3143 .step
= stlink_usb_step
,
3145 .read_regs
= stlink_usb_read_regs
,
3147 .read_reg
= stlink_usb_read_reg
,
3149 .write_reg
= stlink_usb_write_reg
,
3151 .read_mem
= stlink_usb_read_mem
,
3153 .write_mem
= stlink_usb_write_mem
,
3155 .write_debug_reg
= stlink_usb_write_debug_reg
,
3157 .override_target
= stlink_usb_override_target
,
3159 .speed
= stlink_speed
,
3161 .config_trace
= stlink_config_trace
,
3163 .poll_trace
= stlink_usb_trace_read
,
3166 /*****************************************************************************
3167 * DAP direct interface
3170 static struct stlink_usb_handle_s
*stlink_dap_handle
;
3171 static struct hl_interface_param_s stlink_dap_param
;
3172 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
3173 static int stlink_dap_error
= ERROR_OK
;
3175 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3179 static int stlink_dap_record_error(int error
)
3181 if (stlink_dap_error
== ERROR_OK
)
3182 stlink_dap_error
= error
;
3187 static int stlink_dap_get_and_clear_error(void)
3189 int retval
= stlink_dap_error
;
3190 stlink_dap_error
= ERROR_OK
;
3195 static int stlink_dap_open_ap(unsigned short apsel
)
3199 /* nothing to do on old versions */
3200 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3203 if (apsel
> DP_APSEL_MAX
)
3206 if (test_bit(apsel
, opened_ap
))
3209 retval
= stlink_usb_init_access_port(stlink_dap_handle
, apsel
);
3210 if (retval
!= ERROR_OK
)
3213 LOG_DEBUG("AP %d enabled", apsel
);
3214 set_bit(apsel
, opened_ap
);
3219 static int stlink_dap_closeall_ap(void)
3223 /* nothing to do on old versions */
3224 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3227 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
3228 if (!test_bit(apsel
, opened_ap
))
3230 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
3231 if (retval
!= ERROR_OK
)
3233 clear_bit(apsel
, opened_ap
);
3239 static int stlink_dap_reinit_interface(void)
3244 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
3245 * some firmware version it does not work as expected, and there is no
3246 * equivalent for SWD.
3247 * At least for now, to reset the interface quit from JTAG/SWD mode then
3248 * select the mode again.
3251 if (!stlink_dap_handle
->reconnect_pending
) {
3252 stlink_dap_handle
->reconnect_pending
= true;
3253 stlink_usb_mode_leave(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
3256 retval
= stlink_usb_mode_enter(stlink_dap_handle
, stlink_dap_handle
->st_mode
);
3257 if (retval
!= ERROR_OK
)
3260 stlink_dap_handle
->reconnect_pending
= false;
3261 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
3262 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
3263 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
3264 if (test_bit(apsel
, opened_ap
)) {
3265 clear_bit(apsel
, opened_ap
);
3266 stlink_dap_open_ap(apsel
);
3272 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
3277 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
3279 /* Check if we should reset srst already when connecting, but not if reconnecting. */
3280 if (!dap
->do_reconnect
) {
3281 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3283 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3284 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3285 adapter_assert_reset();
3287 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3291 dap
->do_reconnect
= false;
3292 dap_invalidate_cache(dap
);
3294 retval
= dap_dp_init(dap
);
3295 if (retval
!= ERROR_OK
) {
3296 dap
->do_reconnect
= true;
3300 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
3301 if (retval
== ERROR_OK
)
3302 LOG_INFO("%s %#8.8" PRIx32
,
3303 (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
3306 dap
->do_reconnect
= true;
3312 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
3316 if (!dap
->do_reconnect
)
3319 retval
= stlink_dap_reinit_interface();
3320 if (retval
!= ERROR_OK
)
3323 return stlink_dap_op_connect(dap
);
3327 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
3329 /* Ignore the request */
3334 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3340 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3341 if (reg
& 0x000000F0) {
3342 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3343 return ERROR_COMMAND_NOTFOUND
;
3346 retval
= stlink_dap_check_reconnect(dap
);
3347 if (retval
!= ERROR_OK
)
3350 data
= data
? : &dummy
;
3351 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
3352 && stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
) {
3353 /* Quirk required in JTAG. Read RDBUFF to get the data */
3354 retval
= stlink_read_dap_register(stlink_dap_handle
,
3355 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
3356 if (retval
== ERROR_OK
)
3357 retval
= stlink_read_dap_register(stlink_dap_handle
,
3358 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
3360 retval
= stlink_read_dap_register(stlink_dap_handle
,
3361 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3364 return stlink_dap_record_error(retval
);
3368 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned reg
,
3373 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3374 if (reg
& 0x000000F0) {
3375 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3376 return ERROR_COMMAND_NOTFOUND
;
3379 if (reg
== DP_SELECT
&& (data
& DP_SELECT_DPBANK
) != 0) {
3380 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
3381 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
3382 data
&= ~DP_SELECT_DPBANK
;
3385 retval
= stlink_dap_check_reconnect(dap
);
3386 if (retval
!= ERROR_OK
)
3389 /* ST-Link does not like that we set CORUNDETECT */
3390 if (reg
== DP_CTRL_STAT
)
3391 data
&= ~CORUNDETECT
;
3393 retval
= stlink_write_dap_register(stlink_dap_handle
,
3394 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3395 return stlink_dap_record_error(retval
);
3399 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned reg
,
3402 struct adiv5_dap
*dap
= ap
->dap
;
3406 retval
= stlink_dap_check_reconnect(dap
);
3407 if (retval
!= ERROR_OK
)
3410 if (reg
!= AP_REG_IDR
) {
3411 retval
= stlink_dap_open_ap(ap
->ap_num
);
3412 if (retval
!= ERROR_OK
)
3415 data
= data
? : &dummy
;
3416 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3418 dap
->stlink_flush_ap_write
= false;
3419 return stlink_dap_record_error(retval
);
3423 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned reg
,
3426 struct adiv5_dap
*dap
= ap
->dap
;
3429 retval
= stlink_dap_check_reconnect(dap
);
3430 if (retval
!= ERROR_OK
)
3433 retval
= stlink_dap_open_ap(ap
->ap_num
);
3434 if (retval
!= ERROR_OK
)
3437 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3439 dap
->stlink_flush_ap_write
= true;
3440 return stlink_dap_record_error(retval
);
3444 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
3446 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
3451 static int stlink_dap_op_run(struct adiv5_dap
*dap
)
3453 uint32_t ctrlstat
, pwrmask
;
3454 int retval
, saved_retval
;
3456 /* Here no LOG_DEBUG. This is called continuously! */
3459 * ST-Link returns immediately after a DAP write, without waiting for it
3461 * Run a dummy read to DP_RDBUFF, as suggested in
3462 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
3464 if (dap
->stlink_flush_ap_write
) {
3465 dap
->stlink_flush_ap_write
= false;
3466 retval
= stlink_dap_op_queue_dp_read(dap
, DP_RDBUFF
, NULL
);
3467 if (retval
!= ERROR_OK
) {
3468 dap
->do_reconnect
= true;
3473 saved_retval
= stlink_dap_get_and_clear_error();
3475 retval
= stlink_dap_op_queue_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
3476 if (retval
!= ERROR_OK
) {
3477 dap
->do_reconnect
= true;
3480 retval
= stlink_dap_get_and_clear_error();
3481 if (retval
!= ERROR_OK
) {
3482 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
3483 dap
->do_reconnect
= true;
3487 if (ctrlstat
& SSTICKYERR
) {
3488 if (stlink_dap_handle
->st_mode
== STLINK_MODE_DEBUG_JTAG
)
3489 retval
= stlink_dap_op_queue_dp_write(dap
, DP_CTRL_STAT
,
3490 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
3492 retval
= stlink_dap_op_queue_dp_write(dap
, DP_ABORT
, STKERRCLR
);
3493 if (retval
!= ERROR_OK
) {
3494 dap
->do_reconnect
= true;
3497 retval
= stlink_dap_get_and_clear_error();
3498 if (retval
!= ERROR_OK
) {
3499 dap
->do_reconnect
= true;
3504 /* check for power lost */
3505 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
3506 if ((ctrlstat
& pwrmask
) != pwrmask
)
3507 dap
->do_reconnect
= true;
3509 return saved_retval
;
3513 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
3517 retval
= stlink_dap_closeall_ap();
3518 if (retval
!= ERROR_OK
)
3519 LOG_ERROR("Error closing APs");
3522 static int stlink_swim_op_srst(void)
3524 return stlink_swim_generate_rst(stlink_dap_handle
);
3527 static int stlink_swim_op_read_mem(uint32_t addr
, uint32_t size
,
3528 uint32_t count
, uint8_t *buffer
)
3531 uint32_t bytes_remaining
;
3533 LOG_DEBUG_IO("read at 0x%08x len %d*0x%08x", addr
, size
, count
);
3537 bytes_remaining
= (count
> STLINK_DATA_SIZE
) ? STLINK_DATA_SIZE
: count
;
3538 retval
= stlink_swim_readbytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
3539 if (retval
!= ERROR_OK
)
3542 buffer
+= bytes_remaining
;
3543 addr
+= bytes_remaining
;
3544 count
-= bytes_remaining
;
3550 static int stlink_swim_op_write_mem(uint32_t addr
, uint32_t size
,
3551 uint32_t count
, const uint8_t *buffer
)
3554 uint32_t bytes_remaining
;
3556 LOG_DEBUG_IO("write at 0x%08x len %d*0x%08x", addr
, size
, count
);
3560 bytes_remaining
= (count
> STLINK_DATA_SIZE
) ? STLINK_DATA_SIZE
: count
;
3561 retval
= stlink_swim_writebytes(stlink_dap_handle
, addr
, bytes_remaining
, buffer
);
3562 if (retval
!= ERROR_OK
)
3565 buffer
+= bytes_remaining
;
3566 addr
+= bytes_remaining
;
3567 count
-= bytes_remaining
;
3573 static int stlink_swim_op_reconnect(void)
3577 retval
= stlink_usb_mode_enter(stlink_dap_handle
, STLINK_MODE_DEBUG_SWIM
);
3578 if (retval
!= ERROR_OK
)
3581 return stlink_swim_resync(stlink_dap_handle
);
3584 static int stlink_dap_config_trace(bool enabled
,
3585 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3586 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3587 uint16_t *prescaler
)
3589 return stlink_config_trace(stlink_dap_handle
, enabled
, pin_protocol
,
3590 port_size
, trace_freq
, traceclkin_freq
,
3594 static int stlink_dap_trace_read(uint8_t *buf
, size_t *size
)
3596 return stlink_usb_trace_read(stlink_dap_handle
, buf
, size
);
3600 COMMAND_HANDLER(stlink_dap_serial_command
)
3602 LOG_DEBUG("stlink_dap_serial_command");
3604 if (CMD_ARGC
!= 1) {
3605 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
3606 return ERROR_COMMAND_SYNTAX_ERROR
;
3609 if (stlink_dap_param
.serial
) {
3610 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
3611 free((void *)stlink_dap_param
.serial
);
3614 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
3619 COMMAND_HANDLER(stlink_dap_vid_pid
)
3621 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
3623 if (CMD_ARGC
> max_usb_ids
* 2) {
3624 LOG_WARNING("ignoring extra IDs in vid_pid "
3625 "(maximum is %d pairs)", max_usb_ids
);
3626 CMD_ARGC
= max_usb_ids
* 2;
3628 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3629 LOG_WARNING("incomplete vid_pid configuration directive");
3630 return ERROR_COMMAND_SYNTAX_ERROR
;
3632 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3633 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
3634 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
3637 /* null termination */
3638 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
3644 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
3647 .handler
= stlink_dap_serial_command
,
3648 .mode
= COMMAND_CONFIG
,
3649 .help
= "set the serial number of the adapter",
3650 .usage
= "<serial_number>",
3654 .handler
= stlink_dap_vid_pid
,
3655 .mode
= COMMAND_CONFIG
,
3656 .help
= "USB VID and PID of the adapter",
3657 .usage
= "(vid pid)+",
3659 COMMAND_REGISTRATION_DONE
3663 static const struct command_registration stlink_dap_command_handlers
[] = {
3666 .mode
= COMMAND_ANY
,
3667 .help
= "perform st-link management",
3668 .chain
= stlink_dap_subcommand_handlers
,
3671 COMMAND_REGISTRATION_DONE
3675 static int stlink_dap_init(void)
3677 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3678 enum stlink_mode mode
;
3681 LOG_DEBUG("stlink_dap_init()");
3683 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3684 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3685 stlink_dap_param
.connect_under_reset
= true;
3687 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3690 if (transport_is_dapdirect_swd())
3691 mode
= STLINK_MODE_DEBUG_SWD
;
3692 else if (transport_is_dapdirect_jtag())
3693 mode
= STLINK_MODE_DEBUG_JTAG
;
3694 else if (transport_is_swim())
3695 mode
= STLINK_MODE_DEBUG_SWIM
;
3697 LOG_ERROR("Unsupported transport");
3701 retval
= stlink_usb_open(&stlink_dap_param
, mode
, (void **)&stlink_dap_handle
);
3702 if (retval
!= ERROR_OK
)
3705 if ((mode
!= STLINK_MODE_DEBUG_SWIM
) &&
3706 !(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
3707 LOG_ERROR("ST-Link version does not support DAP direct transport");
3714 static int stlink_dap_quit(void)
3716 LOG_DEBUG("stlink_dap_quit()");
3718 free((void *)stlink_dap_param
.serial
);
3719 stlink_dap_param
.serial
= NULL
;
3721 return stlink_usb_close(stlink_dap_handle
);
3725 static int stlink_dap_reset(int req_trst
, int req_srst
)
3727 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
3728 return stlink_usb_assert_srst(stlink_dap_handle
,
3729 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
3730 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
3734 static int stlink_dap_speed(int speed
)
3737 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
3738 return ERROR_JTAG_NOT_IMPLEMENTED
;
3741 stlink_dap_param
.initial_interface_speed
= speed
;
3742 stlink_speed(stlink_dap_handle
, speed
, false);
3747 static int stlink_dap_khz(int khz
, int *jtag_speed
)
3750 LOG_ERROR("RCLK not supported");
3754 *jtag_speed
= stlink_speed(stlink_dap_handle
, khz
, true);
3759 static int stlink_dap_speed_div(int speed
, int *khz
)
3765 static const struct dap_ops stlink_dap_ops
= {
3766 .connect
= stlink_dap_op_connect
,
3767 .send_sequence
= stlink_dap_op_send_sequence
,
3768 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
3769 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
3770 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
3771 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
3772 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
3773 .run
= stlink_dap_op_run
,
3774 .sync
= NULL
, /* optional */
3775 .quit
= stlink_dap_op_quit
, /* optional */
3778 static const struct swim_driver stlink_swim_ops
= {
3779 .srst
= stlink_swim_op_srst
,
3780 .read_mem
= stlink_swim_op_read_mem
,
3781 .write_mem
= stlink_swim_op_write_mem
,
3782 .reconnect
= stlink_swim_op_reconnect
,
3785 static const char *const stlink_dap_transport
[] = { "dapdirect_jtag", "dapdirect_swd", "swim", NULL
};
3787 struct adapter_driver stlink_dap_adapter_driver
= {
3789 .transports
= stlink_dap_transport
,
3790 .commands
= stlink_dap_command_handlers
,
3792 .init
= stlink_dap_init
,
3793 .quit
= stlink_dap_quit
,
3794 .reset
= stlink_dap_reset
,
3795 .speed
= stlink_dap_speed
,
3796 .khz
= stlink_dap_khz
,
3797 .speed_div
= stlink_dap_speed_div
,
3798 .config_trace
= stlink_dap_config_trace
,
3799 .poll_trace
= stlink_dap_trace_read
,
3801 .dap_jtag_ops
= &stlink_dap_ops
,
3802 .dap_swd_ops
= &stlink_dap_ops
,
3803 .swim_ops
= &stlink_swim_ops
,