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 <target/target.h>
40 #include <transport/transport.h>
42 #include <target/cortex_m.h>
44 #include "libusb_helper.h"
47 #define USE_LIBUSB_ASYNCIO
50 #define STLINK_SERIAL_LEN 24
52 #define ENDPOINT_IN 0x80
53 #define ENDPOINT_OUT 0x00
55 #define STLINK_WRITE_TIMEOUT 1000
56 #define STLINK_READ_TIMEOUT 1000
58 #define STLINK_RX_EP (1|ENDPOINT_IN)
59 #define STLINK_TX_EP (2|ENDPOINT_OUT)
60 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
62 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
63 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
65 #define STLINK_SG_SIZE (31)
66 #define STLINK_DATA_SIZE (4096)
67 #define STLINK_CMD_SIZE_V2 (16)
68 #define STLINK_CMD_SIZE_V1 (10)
70 #define STLINK_V1_PID (0x3744)
71 #define STLINK_V2_PID (0x3748)
72 #define STLINK_V2_1_PID (0x374B)
73 #define STLINK_V2_1_NO_MSD_PID (0x3752)
74 #define STLINK_V3_USBLOADER_PID (0x374D)
75 #define STLINK_V3E_PID (0x374E)
76 #define STLINK_V3S_PID (0x374F)
77 #define STLINK_V3_2VCP_PID (0x3753)
80 * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
81 * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
82 * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
84 #define STLINK_MAX_RW8 (64)
85 #define STLINKV3_MAX_RW8 (512)
87 /* "WAIT" responses will be retried (with exponential backoff) at
88 * most this many times before failing to caller.
90 #define MAX_WAIT_RETRIES 8
92 enum stlink_jtag_api_version
{
93 STLINK_JTAG_API_V1
= 1,
99 struct stlink_usb_version
{
106 /** jtag api version supported */
107 enum stlink_jtag_api_version jtag_api
;
108 /** one bit for each feature supported. See macros STLINK_F_* */
113 struct stlink_usb_handle_s
{
115 struct libusb_device_handle
*fd
;
117 struct libusb_transfer
*trans
;
125 uint8_t cmdbuf
[STLINK_SG_SIZE
];
131 uint8_t databuf
[STLINK_DATA_SIZE
];
133 uint32_t max_mem_packet
;
135 enum hl_transports transport
;
137 struct stlink_usb_version version
;
144 /** whether SWO tracing is enabled or not */
146 /** trace module source clock */
149 /** reconnect is needed next time we try to query the
151 bool reconnect_pending
;
154 #define STLINK_SWIM_ERR_OK 0x00
155 #define STLINK_SWIM_BUSY 0x01
156 #define STLINK_DEBUG_ERR_OK 0x80
157 #define STLINK_DEBUG_ERR_FAULT 0x81
158 #define STLINK_SWD_AP_WAIT 0x10
159 #define STLINK_SWD_AP_FAULT 0x11
160 #define STLINK_SWD_AP_ERROR 0x12
161 #define STLINK_SWD_AP_PARITY_ERROR 0x13
162 #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
163 #define STLINK_JTAG_WRITE_ERROR 0x0c
164 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
165 #define STLINK_SWD_DP_WAIT 0x14
166 #define STLINK_SWD_DP_FAULT 0x15
167 #define STLINK_SWD_DP_ERROR 0x16
168 #define STLINK_SWD_DP_PARITY_ERROR 0x17
170 #define STLINK_SWD_AP_WDATA_ERROR 0x18
171 #define STLINK_SWD_AP_STICKY_ERROR 0x19
172 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
174 #define STLINK_BAD_AP_ERROR 0x1d
176 #define STLINK_CORE_RUNNING 0x80
177 #define STLINK_CORE_HALTED 0x81
178 #define STLINK_CORE_STAT_UNKNOWN -1
180 #define STLINK_GET_VERSION 0xF1
181 #define STLINK_DEBUG_COMMAND 0xF2
182 #define STLINK_DFU_COMMAND 0xF3
183 #define STLINK_SWIM_COMMAND 0xF4
184 #define STLINK_GET_CURRENT_MODE 0xF5
185 #define STLINK_GET_TARGET_VOLTAGE 0xF7
187 #define STLINK_DEV_DFU_MODE 0x00
188 #define STLINK_DEV_MASS_MODE 0x01
189 #define STLINK_DEV_DEBUG_MODE 0x02
190 #define STLINK_DEV_SWIM_MODE 0x03
191 #define STLINK_DEV_BOOTLOADER_MODE 0x04
192 #define STLINK_DEV_UNKNOWN_MODE -1
194 #define STLINK_DFU_EXIT 0x07
197 STLINK_SWIM_ENTER_SEQ
198 1.3ms low then 750Hz then 1.5kHz
201 STM8 DM pulls reset pin low 50us
204 uint8_t (0=low|1=high)
211 send syncronization seq (16us low, response 64 clocks low)
213 #define STLINK_SWIM_ENTER 0x00
214 #define STLINK_SWIM_EXIT 0x01
215 #define STLINK_SWIM_READ_CAP 0x02
216 #define STLINK_SWIM_SPEED 0x03
217 #define STLINK_SWIM_ENTER_SEQ 0x04
218 #define STLINK_SWIM_GEN_RST 0x05
219 #define STLINK_SWIM_RESET 0x06
220 #define STLINK_SWIM_ASSERT_RESET 0x07
221 #define STLINK_SWIM_DEASSERT_RESET 0x08
222 #define STLINK_SWIM_READSTATUS 0x09
223 #define STLINK_SWIM_WRITEMEM 0x0a
224 #define STLINK_SWIM_READMEM 0x0b
225 #define STLINK_SWIM_READBUF 0x0c
227 #define STLINK_DEBUG_GETSTATUS 0x01
228 #define STLINK_DEBUG_FORCEDEBUG 0x02
229 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
230 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
231 #define STLINK_DEBUG_APIV1_READREG 0x05
232 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
233 #define STLINK_DEBUG_READMEM_32BIT 0x07
234 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
235 #define STLINK_DEBUG_RUNCORE 0x09
236 #define STLINK_DEBUG_STEPCORE 0x0a
237 #define STLINK_DEBUG_APIV1_SETFP 0x0b
238 #define STLINK_DEBUG_READMEM_8BIT 0x0c
239 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
240 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
241 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
242 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
244 #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
245 #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
246 #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
248 #define STLINK_DEBUG_APIV1_ENTER 0x20
249 #define STLINK_DEBUG_EXIT 0x21
250 #define STLINK_DEBUG_READCOREID 0x22
252 #define STLINK_DEBUG_APIV2_ENTER 0x30
253 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
254 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
255 #define STLINK_DEBUG_APIV2_READREG 0x33
256 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
257 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
258 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
260 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
261 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
262 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
264 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
266 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
267 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
268 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
269 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
270 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
271 #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
272 #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
273 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
274 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
276 #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
277 #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
279 #define STLINK_APIV3_SET_COM_FREQ 0x61
280 #define STLINK_APIV3_GET_COM_FREQ 0x62
282 #define STLINK_APIV3_GET_VERSION_EX 0xFB
284 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
285 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
286 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
288 #define STLINK_DEBUG_PORT_ACCESS 0xffff
290 #define STLINK_TRACE_SIZE 4096
291 #define STLINK_TRACE_MAX_HZ 2000000
293 #define STLINK_V3_MAX_FREQ_NB 10
297 STLINK_MODE_UNKNOWN
= 0,
300 STLINK_MODE_DEBUG_JTAG
,
301 STLINK_MODE_DEBUG_SWD
,
302 STLINK_MODE_DEBUG_SWIM
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
->transport
== HL_TRANSPORT_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
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
823 res
= stlink_usb_xfer_noerrcheck(handle
, buf
, size
);
828 if (h
->transport
== HL_TRANSPORT_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
;
1291 case HL_TRANSPORT_SWIM
:
1292 return STLINK_MODE_DEBUG_SWIM
;
1294 return STLINK_MODE_UNKNOWN
;
1299 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
, int initial_interface_speed
)
1303 enum stlink_mode emode
;
1304 struct stlink_usb_handle_s
*h
= handle
;
1306 assert(handle
!= NULL
);
1308 res
= stlink_usb_current_mode(handle
, &mode
);
1310 if (res
!= ERROR_OK
)
1313 LOG_DEBUG("MODE: 0x%02X", mode
);
1315 /* try to exit current mode */
1317 case STLINK_DEV_DFU_MODE
:
1318 emode
= STLINK_MODE_DFU
;
1320 case STLINK_DEV_DEBUG_MODE
:
1321 emode
= STLINK_MODE_DEBUG_SWD
;
1323 case STLINK_DEV_SWIM_MODE
:
1324 emode
= STLINK_MODE_DEBUG_SWIM
;
1326 case STLINK_DEV_BOOTLOADER_MODE
:
1327 case STLINK_DEV_MASS_MODE
:
1329 emode
= STLINK_MODE_UNKNOWN
;
1333 if (emode
!= STLINK_MODE_UNKNOWN
) {
1334 res
= stlink_usb_mode_leave(handle
, emode
);
1336 if (res
!= ERROR_OK
)
1340 res
= stlink_usb_current_mode(handle
, &mode
);
1342 if (res
!= ERROR_OK
)
1345 /* we check the target voltage here as an aid to debugging connection problems.
1346 * the stlink requires the target Vdd to be connected for reliable debugging.
1347 * this cmd is supported in all modes except DFU
1349 if (mode
!= STLINK_DEV_DFU_MODE
) {
1351 float target_voltage
;
1353 /* check target voltage (if supported) */
1354 res
= stlink_usb_check_voltage(h
, &target_voltage
);
1356 if (res
!= ERROR_OK
) {
1357 if (res
!= ERROR_COMMAND_NOTFOUND
)
1358 LOG_ERROR("voltage check failed");
1359 /* attempt to continue as it is not a catastrophic failure */
1361 /* check for a sensible target voltage, operating range is 1.65-5.5v
1362 * according to datasheet */
1363 if (target_voltage
< 1.5)
1364 LOG_ERROR("target voltage may be too low for reliable debugging");
1368 LOG_DEBUG("MODE: 0x%02X", mode
);
1370 /* set selected mode */
1371 emode
= stlink_get_mode(h
->transport
);
1373 if (emode
== STLINK_MODE_UNKNOWN
) {
1374 LOG_ERROR("selected mode (transport) not supported");
1378 /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
1379 if (h
->transport
== HL_TRANSPORT_JTAG
) {
1380 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
1381 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
1382 stlink_speed(h
, initial_interface_speed
, false);
1384 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
1385 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
1386 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
1387 stlink_speed(h
, initial_interface_speed
, false);
1391 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
) {
1392 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
1394 stlink_get_com_freq(h
, (h
->transport
== HL_TRANSPORT_JTAG
), map
);
1395 stlink_dump_speed_map(map
, ARRAY_SIZE(map
));
1396 stlink_speed(h
, initial_interface_speed
, false);
1399 /* preliminary SRST assert:
1400 * We want SRST is asserted before activating debug signals (mode_enter).
1401 * As the required mode has not been set, the adapter may not know what pin to use.
1402 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
1403 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
1404 * after power on, SWIM_RST stays unchanged */
1405 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1406 stlink_usb_assert_srst(handle
, 0);
1407 /* do not check the return status here, we will
1408 proceed and enter the desired mode below
1409 and try asserting srst again. */
1411 res
= stlink_usb_mode_enter(handle
, emode
);
1412 if (res
!= ERROR_OK
)
1415 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1416 if (connect_under_reset
) {
1417 res
= stlink_usb_assert_srst(handle
, 0);
1418 if (res
!= ERROR_OK
)
1422 res
= stlink_usb_current_mode(handle
, &mode
);
1424 if (res
!= ERROR_OK
)
1427 LOG_DEBUG("MODE: 0x%02X", mode
);
1432 /* request status from last swim request */
1433 static int stlink_swim_status(void *handle
)
1435 struct stlink_usb_handle_s
*h
= handle
;
1438 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1439 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1440 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1441 /* error is checked by the caller */
1442 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1443 if (res
!= ERROR_OK
)
1448 the purpose of this function is unknown...
1449 capabilites? anyway for swim v6 it returns
1452 __attribute__((unused
))
1453 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1455 struct stlink_usb_handle_s
*h
= handle
;
1458 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1459 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1460 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1461 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1462 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 8);
1463 if (res
!= ERROR_OK
)
1465 memcpy(cap
, h
->databuf
, 8);
1469 /* debug dongle assert/deassert sreset line */
1470 static int stlink_swim_assert_reset(void *handle
, int reset
)
1472 struct stlink_usb_handle_s
*h
= handle
;
1475 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1476 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1478 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1480 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1481 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1482 if (res
!= ERROR_OK
)
1489 1.3ms low then 750Hz then 1.5kHz
1491 static int stlink_swim_enter(void *handle
)
1493 struct stlink_usb_handle_s
*h
= handle
;
1496 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1497 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1498 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1499 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1500 if (res
!= ERROR_OK
)
1505 /* switch high/low speed swim */
1506 static int stlink_swim_speed(void *handle
, int speed
)
1508 struct stlink_usb_handle_s
*h
= handle
;
1511 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1512 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1513 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1515 h
->cmdbuf
[h
->cmdidx
++] = 1;
1517 h
->cmdbuf
[h
->cmdidx
++] = 0;
1518 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1519 if (res
!= ERROR_OK
)
1525 initiate srst from swim.
1526 nrst is pulled low for 50us.
1528 static int stlink_swim_generate_rst(void *handle
)
1530 struct stlink_usb_handle_s
*h
= handle
;
1533 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1534 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1535 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1536 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1537 if (res
!= ERROR_OK
)
1543 send resyncronize sequence
1544 swim is pulled low for 16us
1545 reply is 64 clks low
1547 static int stlink_swim_resync(void *handle
)
1549 struct stlink_usb_handle_s
*h
= handle
;
1552 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1553 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1554 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1555 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1556 if (res
!= ERROR_OK
)
1561 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1563 struct stlink_usb_handle_s
*h
= handle
;
1566 unsigned int datalen
= 0;
1567 int cmdsize
= STLINK_CMD_SIZE_V2
;
1569 if (len
> STLINK_DATA_SIZE
)
1572 if (h
->version
.stlink
== 1)
1573 cmdsize
= STLINK_SG_SIZE
;
1575 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1576 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1577 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1578 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1580 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1582 for (i
= 0; i
< len
; i
++) {
1583 if (h
->cmdidx
== cmdsize
)
1584 h
->databuf
[datalen
++] = *(data
++);
1586 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1588 if (h
->version
.stlink
== 1)
1589 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1591 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1592 if (res
!= ERROR_OK
)
1597 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1599 struct stlink_usb_handle_s
*h
= handle
;
1602 if (len
> STLINK_DATA_SIZE
)
1605 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1606 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1607 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1608 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1610 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1612 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1613 if (res
!= ERROR_OK
)
1616 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1617 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1618 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1619 res
= stlink_usb_xfer_noerrcheck(handle
, data
, len
);
1620 if (res
!= ERROR_OK
)
1627 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1630 struct stlink_usb_handle_s
*h
= handle
;
1632 assert(handle
!= NULL
);
1634 /* there is no swim read core id cmd */
1635 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1640 stlink_usb_init_buffer(handle
, h
->rx_ep
, 12);
1642 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1643 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
1644 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1646 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
1649 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_IDCODES
;
1651 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
1655 if (res
!= ERROR_OK
)
1658 *idcode
= le_to_h_u32(h
->databuf
+ offset
);
1660 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1665 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1667 struct stlink_usb_handle_s
*h
= handle
;
1670 assert(handle
!= NULL
);
1672 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1674 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1675 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1676 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1679 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1680 if (res
!= ERROR_OK
)
1683 *val
= le_to_h_u32(h
->databuf
+ 4);
1687 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1689 struct stlink_usb_handle_s
*h
= handle
;
1691 assert(handle
!= NULL
);
1693 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1695 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1696 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1697 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1699 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1700 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1702 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1705 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1709 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1711 struct stlink_usb_handle_s
*h
= handle
;
1713 assert(handle
!= NULL
);
1715 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1718 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1720 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1721 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1723 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1724 if (res
!= ERROR_OK
)
1727 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1728 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1731 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1732 if (res
!= ERROR_OK
)
1741 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1746 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1747 if (result
!= ERROR_OK
)
1748 return TARGET_UNKNOWN
;
1750 if (status
& S_HALT
)
1751 return TARGET_HALTED
;
1752 else if (status
& S_RESET_ST
)
1753 return TARGET_RESET
;
1755 return TARGET_RUNNING
;
1759 static enum target_state
stlink_usb_state(void *handle
)
1762 struct stlink_usb_handle_s
*h
= handle
;
1764 assert(handle
!= NULL
);
1766 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1767 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1768 if (res
!= ERROR_OK
)
1769 return TARGET_UNKNOWN
;
1771 res
= stlink_swim_resync(handle
);
1772 if (res
!= ERROR_OK
)
1773 return TARGET_UNKNOWN
;
1778 if (h
->reconnect_pending
) {
1779 LOG_INFO("Previous state query failed, trying to reconnect");
1780 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1782 if (res
!= ERROR_OK
)
1783 return TARGET_UNKNOWN
;
1785 h
->reconnect_pending
= false;
1788 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1789 res
= stlink_usb_v2_get_status(handle
);
1790 if (res
== TARGET_UNKNOWN
)
1791 h
->reconnect_pending
= true;
1795 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1797 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1798 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1800 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 2);
1802 if (res
!= ERROR_OK
)
1803 return TARGET_UNKNOWN
;
1805 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1806 return TARGET_RUNNING
;
1807 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1808 return TARGET_HALTED
;
1810 h
->reconnect_pending
= true;
1812 return TARGET_UNKNOWN
;
1815 static int stlink_usb_assert_srst(void *handle
, int srst
)
1817 struct stlink_usb_handle_s
*h
= handle
;
1819 assert(handle
!= NULL
);
1821 if (h
->transport
== HL_TRANSPORT_SWIM
)
1822 return stlink_swim_assert_reset(handle
, srst
);
1824 if (h
->version
.stlink
== 1)
1825 return ERROR_COMMAND_NOTFOUND
;
1827 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1830 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1831 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1833 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1837 static void stlink_usb_trace_disable(void *handle
)
1840 struct stlink_usb_handle_s
*h
= handle
;
1842 assert(handle
!= NULL
);
1844 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1846 LOG_DEBUG("Tracing: disable");
1848 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1849 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1850 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1851 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1853 if (res
== ERROR_OK
)
1854 h
->trace
.enabled
= false;
1859 static int stlink_usb_trace_enable(void *handle
)
1862 struct stlink_usb_handle_s
*h
= handle
;
1864 assert(handle
!= NULL
);
1866 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1867 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1869 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1870 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1871 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1873 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1876 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
1878 if (res
== ERROR_OK
) {
1879 h
->trace
.enabled
= true;
1880 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1883 LOG_ERROR("Tracing is not supported by this version.");
1891 static int stlink_usb_reset(void *handle
)
1893 struct stlink_usb_handle_s
*h
= handle
;
1896 assert(handle
!= NULL
);
1898 if (h
->transport
== HL_TRANSPORT_SWIM
)
1899 return stlink_swim_generate_rst(handle
);
1901 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1903 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1905 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
1906 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1908 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1910 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1911 if (retval
!= ERROR_OK
)
1914 if (h
->trace
.enabled
) {
1915 stlink_usb_trace_disable(h
);
1916 return stlink_usb_trace_enable(h
);
1923 static int stlink_usb_run(void *handle
)
1926 struct stlink_usb_handle_s
*h
= handle
;
1928 assert(handle
!= NULL
);
1930 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1931 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1936 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1938 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1939 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1941 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1945 static int stlink_usb_halt(void *handle
)
1948 struct stlink_usb_handle_s
*h
= handle
;
1950 assert(handle
!= NULL
);
1952 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1953 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1958 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1960 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1961 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1963 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1967 static int stlink_usb_step(void *handle
)
1969 struct stlink_usb_handle_s
*h
= handle
;
1971 assert(handle
!= NULL
);
1973 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V1
) {
1974 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1975 * that the Cortex-M3 currently does. */
1976 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1977 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1978 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1981 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1983 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1984 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1986 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1990 static int stlink_usb_read_regs(void *handle
)
1993 struct stlink_usb_handle_s
*h
= handle
;
1995 assert(handle
!= NULL
);
1997 stlink_usb_init_buffer(handle
, h
->rx_ep
, 88);
1999 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2000 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2002 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
2003 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 84);
2004 /* regs data from offset 0 */
2006 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
2007 res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 88);
2008 /* status at offset 0, regs data from offset 4 */
2015 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
2018 struct stlink_usb_handle_s
*h
= handle
;
2020 assert(handle
!= NULL
);
2022 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->version
.jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
2024 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2025 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2026 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
2028 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
2029 h
->cmdbuf
[h
->cmdidx
++] = num
;
2031 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
) {
2032 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, 4);
2033 if (res
!= ERROR_OK
)
2035 *val
= le_to_h_u32(h
->databuf
);
2038 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
2039 if (res
!= ERROR_OK
)
2041 *val
= le_to_h_u32(h
->databuf
+ 4);
2047 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
2049 struct stlink_usb_handle_s
*h
= handle
;
2051 assert(handle
!= NULL
);
2053 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2055 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2056 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2057 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
2059 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
2060 h
->cmdbuf
[h
->cmdidx
++] = num
;
2061 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
2064 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
2067 static int stlink_usb_get_rw_status(void *handle
)
2069 struct stlink_usb_handle_s
*h
= handle
;
2071 assert(handle
!= NULL
);
2073 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2076 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
2078 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2079 if (h
->version
.flags
& STLINK_F_HAS_GETLASTRWSTATUS2
) {
2080 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2
;
2081 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 12);
2083 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
2084 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
2089 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2093 uint16_t read_len
= len
;
2094 struct stlink_usb_handle_s
*h
= handle
;
2096 assert(handle
!= NULL
);
2098 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2099 if (len
> stlink_usb_block(h
)) {
2100 LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h
));
2104 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
2106 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2107 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
2108 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2110 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2113 /* we need to fix read length for single bytes */
2117 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, read_len
);
2119 if (res
!= ERROR_OK
)
2122 memcpy(buffer
, h
->databuf
, len
);
2124 return stlink_usb_get_rw_status(handle
);
2128 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
2129 const uint8_t *buffer
)
2132 struct stlink_usb_handle_s
*h
= handle
;
2134 assert(handle
!= NULL
);
2136 /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
2137 if (len
> stlink_usb_block(h
)) {
2138 LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h
));
2142 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2144 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2145 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
2146 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2148 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2151 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2153 if (res
!= ERROR_OK
)
2156 return stlink_usb_get_rw_status(handle
);
2160 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2164 struct stlink_usb_handle_s
*h
= handle
;
2166 assert(handle
!= NULL
);
2168 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2169 return ERROR_COMMAND_NOTFOUND
;
2171 /* data must be a multiple of 2 and half-word aligned */
2172 if (len
% 2 || addr
% 2) {
2173 LOG_DEBUG("Invalid data alignment");
2174 return ERROR_TARGET_UNALIGNED_ACCESS
;
2177 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2179 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2180 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
2181 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2183 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2186 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2188 if (res
!= ERROR_OK
)
2191 memcpy(buffer
, h
->databuf
, len
);
2193 return stlink_usb_get_rw_status(handle
);
2197 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
2198 const uint8_t *buffer
)
2201 struct stlink_usb_handle_s
*h
= handle
;
2203 assert(handle
!= NULL
);
2205 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2206 return ERROR_COMMAND_NOTFOUND
;
2208 /* data must be a multiple of 2 and half-word aligned */
2209 if (len
% 2 || addr
% 2) {
2210 LOG_DEBUG("Invalid data alignment");
2211 return ERROR_TARGET_UNALIGNED_ACCESS
;
2214 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2216 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2217 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
2218 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2220 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2223 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2225 if (res
!= ERROR_OK
)
2228 return stlink_usb_get_rw_status(handle
);
2232 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2236 struct stlink_usb_handle_s
*h
= handle
;
2238 assert(handle
!= NULL
);
2240 /* data must be a multiple of 4 and word aligned */
2241 if (len
% 4 || addr
% 4) {
2242 LOG_DEBUG("Invalid data alignment");
2243 return ERROR_TARGET_UNALIGNED_ACCESS
;
2246 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
2248 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2249 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
2250 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2252 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2255 res
= stlink_usb_xfer_noerrcheck(handle
, h
->databuf
, len
);
2257 if (res
!= ERROR_OK
)
2260 memcpy(buffer
, h
->databuf
, len
);
2262 return stlink_usb_get_rw_status(handle
);
2266 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
2267 const uint8_t *buffer
)
2270 struct stlink_usb_handle_s
*h
= handle
;
2272 assert(handle
!= NULL
);
2274 /* data must be a multiple of 4 and word aligned */
2275 if (len
% 4 || addr
% 4) {
2276 LOG_DEBUG("Invalid data alignment");
2277 return ERROR_TARGET_UNALIGNED_ACCESS
;
2280 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
2282 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2283 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
2284 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
2286 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
2289 res
= stlink_usb_xfer_noerrcheck(handle
, buffer
, len
);
2291 if (res
!= ERROR_OK
)
2294 return stlink_usb_get_rw_status(handle
);
2297 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
2299 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
2300 if (max_tar_block
== 0)
2302 return max_tar_block
;
2305 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
2306 uint32_t count
, uint8_t *buffer
)
2308 int retval
= ERROR_OK
;
2309 uint32_t bytes_remaining
;
2311 struct stlink_usb_handle_s
*h
= handle
;
2313 /* calculate byte count */
2316 /* switch to 8 bit if stlink does not support 16 bit memory read */
2317 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2322 bytes_remaining
= (size
!= 1) ? \
2323 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2325 if (count
< bytes_remaining
)
2326 bytes_remaining
= count
;
2328 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2329 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
2330 if (retval
!= ERROR_OK
)
2334 * all stlink support 8/32bit memory read/writes and only from
2335 * stlink V2J26 there is support for 16 bit memory read/write.
2336 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2341 /* When in jtag mode the stlink uses the auto-increment functionality.
2342 * However it expects us to pass the data correctly, this includes
2343 * alignment and any page boundaries. We already do this as part of the
2344 * adi_v5 implementation, but the stlink is a hla adapter and so this
2345 * needs implementing manually.
2346 * currently this only affects jtag mode, according to ST they do single
2347 * access in SWD mode - but this may change and so we do it for both modes */
2349 /* we first need to check for any unaligned bytes */
2350 if (addr
& (size
- 1)) {
2352 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2353 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
2354 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2355 usleep((1<<retries
++) * 1000);
2358 if (retval
!= ERROR_OK
)
2360 buffer
+= head_bytes
;
2362 count
-= head_bytes
;
2363 bytes_remaining
-= head_bytes
;
2366 if (bytes_remaining
& (size
- 1))
2367 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2369 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
2371 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
2373 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
2375 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2376 usleep((1<<retries
++) * 1000);
2379 if (retval
!= ERROR_OK
)
2382 buffer
+= bytes_remaining
;
2383 addr
+= bytes_remaining
;
2384 count
-= bytes_remaining
;
2390 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
2391 uint32_t count
, const uint8_t *buffer
)
2393 int retval
= ERROR_OK
;
2394 uint32_t bytes_remaining
;
2396 struct stlink_usb_handle_s
*h
= handle
;
2398 /* calculate byte count */
2401 /* switch to 8 bit if stlink does not support 16 bit memory read */
2402 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
2407 bytes_remaining
= (size
!= 1) ? \
2408 stlink_max_block_size(h
->max_mem_packet
, addr
) : stlink_usb_block(h
);
2410 if (count
< bytes_remaining
)
2411 bytes_remaining
= count
;
2413 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2414 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
2415 if (retval
!= ERROR_OK
)
2419 * all stlink support 8/32bit memory read/writes and only from
2420 * stlink V2J26 there is support for 16 bit memory read/write.
2421 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2426 /* When in jtag mode the stlink uses the auto-increment functionality.
2427 * However it expects us to pass the data correctly, this includes
2428 * alignment and any page boundaries. We already do this as part of the
2429 * adi_v5 implementation, but the stlink is a hla adapter and so this
2430 * needs implementing manually.
2431 * currently this only affects jtag mode, according to ST they do single
2432 * access in SWD mode - but this may change and so we do it for both modes */
2434 /* we first need to check for any unaligned bytes */
2435 if (addr
& (size
- 1)) {
2437 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2438 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2439 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2440 usleep((1<<retries
++) * 1000);
2443 if (retval
!= ERROR_OK
)
2445 buffer
+= head_bytes
;
2447 count
-= head_bytes
;
2448 bytes_remaining
-= head_bytes
;
2451 if (bytes_remaining
& (size
- 1))
2452 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2454 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2456 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2459 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2460 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2461 usleep((1<<retries
++) * 1000);
2464 if (retval
!= ERROR_OK
)
2467 buffer
+= bytes_remaining
;
2468 addr
+= bytes_remaining
;
2469 count
-= bytes_remaining
;
2476 static int stlink_usb_override_target(const char *targetname
)
2478 return !strcmp(targetname
, "cortex_m");
2481 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2484 we dont care what the khz rate is
2485 we only have low and high speed...
2486 before changing speed the SWIM_CSR HS bit
2490 stlink_swim_speed(handle
, 0);
2492 stlink_swim_speed(handle
, 1);
2496 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2499 int speed_index
= -1;
2500 int speed_diff
= INT_MAX
;
2501 int last_valid_speed
= -1;
2504 for (i
= 0; i
< map_size
; i
++) {
2507 last_valid_speed
= i
;
2508 if (khz
== map
[i
].speed
) {
2512 int current_diff
= khz
- map
[i
].speed
;
2513 /* get abs value for comparison */
2514 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2515 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2516 speed_diff
= current_diff
;
2522 if (speed_index
== -1) {
2523 /* this will only be here if we cannot match the slow speed.
2524 * use the slowest speed we support.*/
2525 speed_index
= last_valid_speed
;
2527 } else if (i
== map_size
)
2530 if (!match
&& query
) {
2531 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2532 khz
, map
[speed_index
].speed
);
2538 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2541 struct stlink_usb_handle_s
*h
= handle
;
2543 /* old firmware cannot change it */
2544 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2547 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2548 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2551 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2552 if (result
!= ERROR_OK
) {
2553 LOG_ERROR("Unable to set adapter speed");
2558 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2561 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2564 struct stlink_usb_handle_s
*h
= handle
;
2566 /* old firmware cannot change it */
2567 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2570 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2571 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2574 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2575 if (result
!= ERROR_OK
) {
2576 LOG_ERROR("Unable to set adapter speed");
2581 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2584 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2588 LOG_DEBUG("Supported clock speeds are:");
2589 for (i
= 0; i
< map_size
; i
++)
2591 LOG_DEBUG("%d kHz", map
[i
].speed
);
2594 static int stlink_get_com_freq(void *handle
, bool is_jtag
, struct speed_map
*map
)
2596 struct stlink_usb_handle_s
*h
= handle
;
2599 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2600 LOG_ERROR("Unknown command");
2604 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2606 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2607 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_GET_COM_FREQ
;
2608 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2610 int res
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 52);
2612 int size
= h
->databuf
[8];
2614 if (size
> STLINK_V3_MAX_FREQ_NB
)
2615 size
= STLINK_V3_MAX_FREQ_NB
;
2617 for (i
= 0; i
< size
; i
++) {
2618 map
[i
].speed
= le_to_h_u32(&h
->databuf
[12 + 4 * i
]);
2619 map
[i
].speed_divisor
= i
;
2622 /* set to zero all the next entries */
2623 for (i
= size
; i
< STLINK_V3_MAX_FREQ_NB
; i
++)
2629 static int stlink_set_com_freq(void *handle
, bool is_jtag
, unsigned int frequency
)
2631 struct stlink_usb_handle_s
*h
= handle
;
2633 if (h
->version
.jtag_api
!= STLINK_JTAG_API_V3
) {
2634 LOG_ERROR("Unknown command");
2638 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
2640 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
2641 h
->cmdbuf
[h
->cmdidx
++] = STLINK_APIV3_SET_COM_FREQ
;
2642 h
->cmdbuf
[h
->cmdidx
++] = is_jtag
? 1 : 0;
2643 h
->cmdbuf
[h
->cmdidx
++] = 0;
2645 h_u32_to_le(&h
->cmdbuf
[4], frequency
);
2647 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
2650 static int stlink_speed_v3(void *handle
, bool is_jtag
, int khz
, bool query
)
2652 struct stlink_usb_handle_s
*h
= handle
;
2654 struct speed_map map
[STLINK_V3_MAX_FREQ_NB
];
2656 stlink_get_com_freq(h
, is_jtag
, map
);
2658 speed_index
= stlink_match_speed_map(map
, ARRAY_SIZE(map
), khz
, query
);
2661 int result
= stlink_set_com_freq(h
, is_jtag
, map
[speed_index
].speed
);
2662 if (result
!= ERROR_OK
) {
2663 LOG_ERROR("Unable to set adapter speed");
2667 return map
[speed_index
].speed
;
2670 static int stlink_speed(void *handle
, int khz
, bool query
)
2672 struct stlink_usb_handle_s
*h
= handle
;
2677 switch (h
->transport
) {
2678 case HL_TRANSPORT_SWIM
:
2679 return stlink_speed_swim(handle
, khz
, query
);
2680 case HL_TRANSPORT_SWD
:
2681 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2682 return stlink_speed_v3(handle
, false, khz
, query
);
2684 return stlink_speed_swd(handle
, khz
, query
);
2686 case HL_TRANSPORT_JTAG
:
2687 if (h
->version
.jtag_api
== STLINK_JTAG_API_V3
)
2688 return stlink_speed_v3(handle
, true, khz
, query
);
2690 return stlink_speed_jtag(handle
, khz
, query
);
2700 static int stlink_usb_close(void *handle
)
2704 enum stlink_mode emode
;
2705 struct stlink_usb_handle_s
*h
= handle
;
2708 res
= stlink_usb_current_mode(handle
, &mode
);
2711 /* do not exit if return code != ERROR_OK,
2712 it prevents us from closing jtag_libusb */
2714 if (res
== ERROR_OK
) {
2715 /* try to exit current mode */
2717 case STLINK_DEV_DFU_MODE
:
2718 emode
= STLINK_MODE_DFU
;
2720 case STLINK_DEV_DEBUG_MODE
:
2721 emode
= STLINK_MODE_DEBUG_SWD
;
2723 case STLINK_DEV_SWIM_MODE
:
2724 emode
= STLINK_MODE_DEBUG_SWIM
;
2726 case STLINK_DEV_BOOTLOADER_MODE
:
2727 case STLINK_DEV_MASS_MODE
:
2729 emode
= STLINK_MODE_UNKNOWN
;
2733 if (emode
!= STLINK_MODE_UNKNOWN
)
2734 stlink_usb_mode_leave(handle
, emode
);
2735 /* do not check return code, it prevent
2736 us from closing jtag_libusb */
2740 jtag_libusb_close(h
->fd
);
2747 /* Compute ST-Link serial number from the device descriptor
2748 * this function will help to work-around a bug in old ST-Link/V2 DFU
2749 * the buggy DFU returns an incorrect serial in the USB descriptor
2750 * example for the following serial "57FF72067265575742132067"
2751 * - the correct descriptor serial is:
2752 * 0x32, 0x03, 0x35, 0x00, 0x37, 0x00, 0x46, 0x00, 0x46, 0x00, 0x37, 0x00, 0x32, 0x00 ...
2753 * this contains the length (0x32 = 50), the type (0x3 = DT_STRING) and the serial in unicode format
2754 * the serial part is: 0x0035, 0x0037, 0x0046, 0x0046, 0x0037, 0x0032 ... >> 57FF72 ...
2755 * this format could be read correctly by 'libusb_get_string_descriptor_ascii'
2756 * so this case is managed by libusb_helper::string_descriptor_equal
2757 * - the buggy DFU is not doing any unicode conversion and returns a raw serial data in the descriptor
2758 * 0x1a, 0x03, 0x57, 0x00, 0xFF, 0x00, 0x72, 0x00 ...
2760 * based on the length (0x1a = 26) we could easily decide if we have to fixup the serial
2761 * and then we have just to convert the raw data into printable characters using sprintf
2763 char *stlink_usb_get_alternate_serial(libusb_device_handle
*device
,
2764 struct libusb_device_descriptor
*dev_desc
)
2767 unsigned char desc_serial
[(STLINK_SERIAL_LEN
+ 1) * 2];
2769 if (dev_desc
->iSerialNumber
== 0)
2772 /* get the LANGID from String Descriptor Zero */
2773 usb_retval
= libusb_get_string_descriptor(device
, 0, 0, desc_serial
,
2774 sizeof(desc_serial
));
2776 if (usb_retval
< LIBUSB_SUCCESS
) {
2777 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2778 libusb_error_name(usb_retval
), usb_retval
);
2780 } else if (usb_retval
< 4) {
2781 /* the size should be least 4 bytes to contain a minimum of 1 supported LANGID */
2782 LOG_ERROR("could not get the LANGID");
2786 uint32_t langid
= desc_serial
[2] | (desc_serial
[3] << 8);
2788 /* get the serial */
2789 usb_retval
= libusb_get_string_descriptor(device
, dev_desc
->iSerialNumber
,
2790 langid
, desc_serial
, sizeof(desc_serial
));
2792 unsigned char len
= desc_serial
[0];
2794 if (usb_retval
< LIBUSB_SUCCESS
) {
2795 LOG_ERROR("libusb_get_string_descriptor() failed: %s(%d)",
2796 libusb_error_name(usb_retval
), usb_retval
);
2798 } else if (desc_serial
[1] != LIBUSB_DT_STRING
|| len
> usb_retval
) {
2799 LOG_ERROR("invalid string in ST-LINK USB serial descriptor");
2803 if (len
== ((STLINK_SERIAL_LEN
+ 1) * 2)) {
2804 /* good ST-Link adapter, this case is managed by
2805 * libusb::libusb_get_string_descriptor_ascii */
2807 } else if (len
!= ((STLINK_SERIAL_LEN
/ 2 + 1) * 2)) {
2808 LOG_ERROR("unexpected serial length (%d) in descriptor", len
);
2812 /* else (len == 26) => buggy ST-Link */
2814 char *alternate_serial
= malloc((STLINK_SERIAL_LEN
+ 1) * sizeof(char));
2815 if (alternate_serial
== NULL
)
2818 for (unsigned int i
= 0; i
< STLINK_SERIAL_LEN
; i
+= 2)
2819 sprintf(alternate_serial
+ i
, "%02X", desc_serial
[i
+ 2]);
2821 alternate_serial
[STLINK_SERIAL_LEN
] = '\0';
2823 return alternate_serial
;
2827 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2829 int err
, retry_count
= 1;
2830 struct stlink_usb_handle_s
*h
;
2832 LOG_DEBUG("stlink_usb_open");
2834 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2837 LOG_DEBUG("malloc failed");
2841 h
->transport
= param
->transport
;
2843 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2844 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2845 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2846 param
->serial
? param
->serial
: "");
2850 On certain host USB configurations(e.g. MacBook Air)
2851 STLINKv2 dongle seems to have its FW in a funky state if,
2852 after plugging it in, you try to use openocd with it more
2853 then once (by launching and closing openocd). In cases like
2854 that initial attempt to read the FW info via
2855 stlink_usb_version will fail and the device has to be reset
2856 in order to become operational.
2859 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
,
2860 &h
->fd
, stlink_usb_get_alternate_serial
) != ERROR_OK
) {
2861 LOG_ERROR("open failed");
2865 jtag_libusb_set_configuration(h
->fd
, 0);
2867 if (libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2868 LOG_DEBUG("claim interface failed");
2872 /* RX EP is common for all versions */
2873 h
->rx_ep
= STLINK_RX_EP
;
2876 if (jtag_libusb_get_pid(libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2877 LOG_DEBUG("libusb_get_pid failed");
2881 /* wrap version for first read */
2884 h
->version
.stlink
= 1;
2885 h
->tx_ep
= STLINK_TX_EP
;
2887 case STLINK_V3_USBLOADER_PID
:
2888 case STLINK_V3E_PID
:
2889 case STLINK_V3S_PID
:
2890 case STLINK_V3_2VCP_PID
:
2891 h
->version
.stlink
= 3;
2892 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2893 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2895 case STLINK_V2_1_PID
:
2896 case STLINK_V2_1_NO_MSD_PID
:
2897 h
->version
.stlink
= 2;
2898 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2899 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2902 /* fall through - we assume V2 to be the default version*/
2904 h
->version
.stlink
= 2;
2905 h
->tx_ep
= STLINK_TX_EP
;
2906 h
->trace_ep
= STLINK_TRACE_EP
;
2910 /* get the device version */
2911 err
= stlink_usb_version(h
);
2913 if (err
== ERROR_OK
) {
2915 } else if (h
->version
.stlink
== 1 ||
2917 LOG_ERROR("read version failed");
2920 err
= libusb_release_interface(h
->fd
, 0);
2921 if (err
!= ERROR_OK
) {
2922 LOG_ERROR("release interface failed");
2926 err
= libusb_reset_device(h
->fd
);
2927 if (err
!= ERROR_OK
) {
2928 LOG_ERROR("reset device failed");
2932 jtag_libusb_close(h
->fd
);
2934 Give the device one second to settle down and
2937 usleep(1 * 1000 * 1000);
2942 /* check if mode is supported */
2945 switch (h
->transport
) {
2946 case HL_TRANSPORT_SWD
:
2947 if (h
->version
.jtag_api
== STLINK_JTAG_API_V1
)
2950 case HL_TRANSPORT_JTAG
:
2951 if (h
->version
.jtag
== 0)
2954 case HL_TRANSPORT_SWIM
:
2955 if (h
->version
.swim
== 0)
2963 if (err
!= ERROR_OK
) {
2964 LOG_ERROR("mode (transport) not supported by device");
2968 /* initialize the debug hardware */
2969 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
, param
->initial_interface_speed
);
2971 if (err
!= ERROR_OK
) {
2972 LOG_ERROR("init mode failed (unable to connect to the target)");
2976 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2977 err
= stlink_swim_enter(h
);
2978 if (err
!= ERROR_OK
) {
2979 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2983 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2987 /* get cpuid, so we can determine the max page size
2988 * start with a safe default */
2989 h
->max_mem_packet
= (1 << 10);
2992 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2993 if (err
== ERROR_OK
) {
2994 uint32_t cpuid
= le_to_h_u32(buffer
);
2995 int i
= (cpuid
>> 4) & 0xf;
2996 if (i
== 4 || i
== 3) {
2997 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2998 h
->max_mem_packet
= (1 << 12);
3002 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
3009 stlink_usb_close(h
);
3014 int stlink_config_trace(void *handle
, bool enabled
,
3015 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3016 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3017 uint16_t *prescaler
)
3019 struct stlink_usb_handle_s
*h
= handle
;
3022 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
3023 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
3024 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
3029 stlink_usb_trace_disable(h
);
3033 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
3034 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
3035 STLINK_TRACE_MAX_HZ
);
3039 stlink_usb_trace_disable(h
);
3042 *trace_freq
= STLINK_TRACE_MAX_HZ
;
3044 presc
= traceclkin_freq
/ *trace_freq
;
3046 if (traceclkin_freq
% *trace_freq
> 0)
3049 if (presc
> TPIU_ACPR_MAX_SWOSCALER
) {
3050 LOG_ERROR("SWO frequency is not suitable. Please choose a different "
3056 h
->trace
.source_hz
= *trace_freq
;
3058 return stlink_usb_trace_enable(h
);
3062 static int stlink_usb_init_access_port(void *handle
, unsigned char ap_num
)
3064 struct stlink_usb_handle_s
*h
= handle
;
3066 assert(handle
!= NULL
);
3068 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3069 return ERROR_COMMAND_NOTFOUND
;
3071 LOG_DEBUG_IO("init ap_num = %d", ap_num
);
3072 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3073 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3074 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_INIT_AP
;
3075 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3077 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3081 static int stlink_usb_close_access_port(void *handle
, unsigned char ap_num
)
3083 struct stlink_usb_handle_s
*h
= handle
;
3085 assert(handle
!= NULL
);
3087 if (!(h
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3088 return ERROR_COMMAND_NOTFOUND
;
3090 LOG_DEBUG_IO("close ap_num = %d", ap_num
);
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_CLOSE_AP_DBG
;
3094 h
->cmdbuf
[h
->cmdidx
++] = ap_num
;
3096 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3100 static int stlink_read_dap_register(void *handle
, unsigned short dap_port
,
3101 unsigned short addr
, uint32_t *val
)
3103 struct stlink_usb_handle_s
*h
= handle
;
3106 assert(handle
!= NULL
);
3108 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3109 return ERROR_COMMAND_NOTFOUND
;
3111 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3112 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3113 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READ_DAP_REG
;
3114 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3115 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3117 retval
= stlink_usb_xfer_errcheck(handle
, h
->databuf
, 8);
3118 *val
= le_to_h_u32(h
->databuf
+ 4);
3119 LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, *val
);
3124 static int stlink_write_dap_register(void *handle
, unsigned short dap_port
,
3125 unsigned short addr
, uint32_t val
)
3127 struct stlink_usb_handle_s
*h
= handle
;
3129 assert(handle
!= NULL
);
3131 if (!(h
->version
.flags
& STLINK_F_HAS_DAP_REG
))
3132 return ERROR_COMMAND_NOTFOUND
;
3134 LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port
, addr
, val
);
3135 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
3136 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
3137 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG
;
3138 h_u16_to_le(&h
->cmdbuf
[2], dap_port
);
3139 h_u16_to_le(&h
->cmdbuf
[4], addr
);
3140 h_u32_to_le(&h
->cmdbuf
[6], val
);
3141 return stlink_usb_xfer_errcheck(handle
, h
->databuf
, 2);
3145 struct hl_layout_api_s stlink_usb_layout_api
= {
3147 .open
= stlink_usb_open
,
3149 .close
= stlink_usb_close
,
3151 .idcode
= stlink_usb_idcode
,
3153 .state
= stlink_usb_state
,
3155 .reset
= stlink_usb_reset
,
3157 .assert_srst
= stlink_usb_assert_srst
,
3159 .run
= stlink_usb_run
,
3161 .halt
= stlink_usb_halt
,
3163 .step
= stlink_usb_step
,
3165 .read_regs
= stlink_usb_read_regs
,
3167 .read_reg
= stlink_usb_read_reg
,
3169 .write_reg
= stlink_usb_write_reg
,
3171 .read_mem
= stlink_usb_read_mem
,
3173 .write_mem
= stlink_usb_write_mem
,
3175 .write_debug_reg
= stlink_usb_write_debug_reg
,
3177 .override_target
= stlink_usb_override_target
,
3179 .speed
= stlink_speed
,
3181 .config_trace
= stlink_config_trace
,
3183 .poll_trace
= stlink_usb_trace_read
,
3186 /*****************************************************************************
3187 * DAP direct interface
3190 static struct stlink_usb_handle_s
*stlink_dap_handle
;
3191 static struct hl_interface_param_s stlink_dap_param
;
3192 static DECLARE_BITMAP(opened_ap
, DP_APSEL_MAX
+ 1);
3193 static int stlink_dap_error
= ERROR_OK
;
3195 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3199 static int stlink_dap_record_error(int error
)
3201 if (stlink_dap_error
== ERROR_OK
)
3202 stlink_dap_error
= error
;
3207 static int stlink_dap_get_and_clear_error(void)
3209 int retval
= stlink_dap_error
;
3210 stlink_dap_error
= ERROR_OK
;
3215 static int stlink_dap_open_ap(unsigned short apsel
)
3219 /* nothing to do on old versions */
3220 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3223 if (apsel
> DP_APSEL_MAX
)
3226 if (test_bit(apsel
, opened_ap
))
3229 retval
= stlink_usb_init_access_port(stlink_dap_handle
, apsel
);
3230 if (retval
!= ERROR_OK
)
3233 LOG_DEBUG("AP %d enabled", apsel
);
3234 set_bit(apsel
, opened_ap
);
3239 static int stlink_dap_closeall_ap(void)
3243 /* nothing to do on old versions */
3244 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
))
3247 for (apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++) {
3248 if (!test_bit(apsel
, opened_ap
))
3250 retval
= stlink_usb_close_access_port(stlink_dap_handle
, apsel
);
3251 if (retval
!= ERROR_OK
)
3253 clear_bit(apsel
, opened_ap
);
3259 static int stlink_dap_reinit_interface(void)
3262 enum stlink_mode mode
;
3265 * On JTAG only, it should be enough to call stlink_usb_reset(). But on
3266 * some firmware version it does not work as expected, and there is no
3267 * equivalent for SWD.
3268 * At least for now, to reset the interface quit from JTAG/SWD mode then
3269 * select the mode again.
3272 mode
= stlink_get_mode(stlink_dap_param
.transport
);
3273 if (!stlink_dap_handle
->reconnect_pending
) {
3274 stlink_dap_handle
->reconnect_pending
= true;
3275 stlink_usb_mode_leave(stlink_dap_handle
, mode
);
3278 retval
= stlink_usb_mode_enter(stlink_dap_handle
, mode
);
3279 if (retval
!= ERROR_OK
)
3282 stlink_dap_handle
->reconnect_pending
= false;
3283 /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
3284 if (stlink_dap_handle
->version
.flags
& STLINK_F_HAS_AP_INIT
)
3285 for (int apsel
= 0; apsel
<= DP_APSEL_MAX
; apsel
++)
3286 if (test_bit(apsel
, opened_ap
)) {
3287 clear_bit(apsel
, opened_ap
);
3288 stlink_dap_open_ap(apsel
);
3294 static int stlink_dap_op_connect(struct adiv5_dap
*dap
)
3299 LOG_INFO("stlink_dap_op_connect(%sconnect)", dap
->do_reconnect
? "re" : "");
3301 /* Check if we should reset srst already when connecting, but not if reconnecting. */
3302 if (!dap
->do_reconnect
) {
3303 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3305 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3306 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3307 adapter_assert_reset();
3309 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3313 dap
->do_reconnect
= false;
3314 dap_invalidate_cache(dap
);
3316 retval
= dap_dp_init(dap
);
3317 if (retval
!= ERROR_OK
) {
3318 dap
->do_reconnect
= true;
3322 retval
= stlink_usb_idcode(stlink_dap_handle
, &idcode
);
3323 if (retval
== ERROR_OK
)
3324 LOG_INFO("%s %#8.8" PRIx32
,
3325 (stlink_dap_handle
->transport
== HL_TRANSPORT_JTAG
) ? "JTAG IDCODE" : "SWD DPIDR",
3328 dap
->do_reconnect
= true;
3334 static int stlink_dap_check_reconnect(struct adiv5_dap
*dap
)
3338 if (!dap
->do_reconnect
)
3341 retval
= stlink_dap_reinit_interface();
3342 if (retval
!= ERROR_OK
)
3345 return stlink_dap_op_connect(dap
);
3349 static int stlink_dap_op_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
3351 /* Ignore the request */
3356 static int stlink_dap_op_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
,
3362 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3363 if (reg
& 0x000000F0) {
3364 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3365 return ERROR_COMMAND_NOTFOUND
;
3368 retval
= stlink_dap_check_reconnect(dap
);
3369 if (retval
!= ERROR_OK
)
3372 data
= data
? : &dummy
;
3373 if (stlink_dap_handle
->version
.flags
& STLINK_F_QUIRK_JTAG_DP_READ
3374 && stlink_dap_handle
->transport
== HL_TRANSPORT_JTAG
) {
3375 /* Quirk required in JTAG. Read RDBUFF to get the data */
3376 retval
= stlink_read_dap_register(stlink_dap_handle
,
3377 STLINK_DEBUG_PORT_ACCESS
, reg
, &dummy
);
3378 if (retval
== ERROR_OK
)
3379 retval
= stlink_read_dap_register(stlink_dap_handle
,
3380 STLINK_DEBUG_PORT_ACCESS
, DP_RDBUFF
, data
);
3382 retval
= stlink_read_dap_register(stlink_dap_handle
,
3383 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3386 return stlink_dap_record_error(retval
);
3390 static int stlink_dap_op_queue_dp_write(struct adiv5_dap
*dap
, unsigned reg
,
3395 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DPBANKSEL
))
3396 if (reg
& 0x000000F0) {
3397 LOG_ERROR("Banked DP registers not supported in current STLink FW");
3398 return ERROR_COMMAND_NOTFOUND
;
3401 if (reg
== DP_SELECT
&& (data
& DP_SELECT_DPBANK
) != 0) {
3402 /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
3403 LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
3404 data
&= ~DP_SELECT_DPBANK
;
3407 retval
= stlink_dap_check_reconnect(dap
);
3408 if (retval
!= ERROR_OK
)
3411 /* ST-Link does not like that we set CORUNDETECT */
3412 if (reg
== DP_CTRL_STAT
)
3413 data
&= ~CORUNDETECT
;
3415 retval
= stlink_write_dap_register(stlink_dap_handle
,
3416 STLINK_DEBUG_PORT_ACCESS
, reg
, data
);
3417 return stlink_dap_record_error(retval
);
3421 static int stlink_dap_op_queue_ap_read(struct adiv5_ap
*ap
, unsigned reg
,
3424 struct adiv5_dap
*dap
= ap
->dap
;
3428 retval
= stlink_dap_check_reconnect(dap
);
3429 if (retval
!= ERROR_OK
)
3432 if (reg
!= AP_REG_IDR
) {
3433 retval
= stlink_dap_open_ap(ap
->ap_num
);
3434 if (retval
!= ERROR_OK
)
3437 data
= data
? : &dummy
;
3438 retval
= stlink_read_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3440 dap
->stlink_flush_ap_write
= false;
3441 return stlink_dap_record_error(retval
);
3445 static int stlink_dap_op_queue_ap_write(struct adiv5_ap
*ap
, unsigned reg
,
3448 struct adiv5_dap
*dap
= ap
->dap
;
3451 retval
= stlink_dap_check_reconnect(dap
);
3452 if (retval
!= ERROR_OK
)
3455 retval
= stlink_dap_open_ap(ap
->ap_num
);
3456 if (retval
!= ERROR_OK
)
3459 retval
= stlink_write_dap_register(stlink_dap_handle
, ap
->ap_num
, reg
,
3461 dap
->stlink_flush_ap_write
= true;
3462 return stlink_dap_record_error(retval
);
3466 static int stlink_dap_op_queue_ap_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
3468 LOG_WARNING("stlink_dap_op_queue_ap_abort()");
3473 static int stlink_dap_op_run(struct adiv5_dap
*dap
)
3475 uint32_t ctrlstat
, pwrmask
;
3476 int retval
, saved_retval
;
3478 /* Here no LOG_DEBUG. This is called continuously! */
3481 * ST-Link returns immediately after a DAP write, without waiting for it
3483 * Run a dummy read to DP_RDBUFF, as suggested in
3484 * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
3486 if (dap
->stlink_flush_ap_write
) {
3487 dap
->stlink_flush_ap_write
= false;
3488 retval
= stlink_dap_op_queue_dp_read(dap
, DP_RDBUFF
, NULL
);
3489 if (retval
!= ERROR_OK
) {
3490 dap
->do_reconnect
= true;
3495 saved_retval
= stlink_dap_get_and_clear_error();
3497 retval
= stlink_dap_op_queue_dp_read(dap
, DP_CTRL_STAT
, &ctrlstat
);
3498 if (retval
!= ERROR_OK
) {
3499 dap
->do_reconnect
= true;
3502 retval
= stlink_dap_get_and_clear_error();
3503 if (retval
!= ERROR_OK
) {
3504 LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
3505 dap
->do_reconnect
= true;
3509 if (ctrlstat
& SSTICKYERR
) {
3510 if (stlink_dap_param
.transport
== HL_TRANSPORT_JTAG
)
3511 retval
= stlink_dap_op_queue_dp_write(dap
, DP_CTRL_STAT
,
3512 ctrlstat
& (dap
->dp_ctrl_stat
| SSTICKYERR
));
3514 retval
= stlink_dap_op_queue_dp_write(dap
, DP_ABORT
, STKERRCLR
);
3515 if (retval
!= ERROR_OK
) {
3516 dap
->do_reconnect
= true;
3519 retval
= stlink_dap_get_and_clear_error();
3520 if (retval
!= ERROR_OK
) {
3521 dap
->do_reconnect
= true;
3526 /* check for power lost */
3527 pwrmask
= dap
->dp_ctrl_stat
& (CDBGPWRUPREQ
| CSYSPWRUPREQ
);
3528 if ((ctrlstat
& pwrmask
) != pwrmask
)
3529 dap
->do_reconnect
= true;
3531 return saved_retval
;
3535 static void stlink_dap_op_quit(struct adiv5_dap
*dap
)
3539 retval
= stlink_dap_closeall_ap();
3540 if (retval
!= ERROR_OK
)
3541 LOG_ERROR("Error closing APs");
3544 static int stlink_dap_config_trace(bool enabled
,
3545 enum tpiu_pin_protocol pin_protocol
, uint32_t port_size
,
3546 unsigned int *trace_freq
, unsigned int traceclkin_freq
,
3547 uint16_t *prescaler
)
3549 return stlink_config_trace(stlink_dap_handle
, enabled
, pin_protocol
,
3550 port_size
, trace_freq
, traceclkin_freq
,
3554 static int stlink_dap_trace_read(uint8_t *buf
, size_t *size
)
3556 return stlink_usb_trace_read(stlink_dap_handle
, buf
, size
);
3560 COMMAND_HANDLER(stlink_dap_serial_command
)
3562 LOG_DEBUG("stlink_dap_serial_command");
3564 if (CMD_ARGC
!= 1) {
3565 LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
3566 return ERROR_COMMAND_SYNTAX_ERROR
;
3569 if (stlink_dap_param
.serial
) {
3570 LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
3571 free((void *)stlink_dap_param
.serial
);
3574 stlink_dap_param
.serial
= strdup(CMD_ARGV
[0]);
3579 COMMAND_HANDLER(stlink_dap_vid_pid
)
3581 unsigned int i
, max_usb_ids
= HLA_MAX_USB_IDS
;
3583 if (CMD_ARGC
> max_usb_ids
* 2) {
3584 LOG_WARNING("ignoring extra IDs in vid_pid "
3585 "(maximum is %d pairs)", max_usb_ids
);
3586 CMD_ARGC
= max_usb_ids
* 2;
3588 if (CMD_ARGC
< 2 || (CMD_ARGC
& 1)) {
3589 LOG_WARNING("incomplete vid_pid configuration directive");
3590 return ERROR_COMMAND_SYNTAX_ERROR
;
3592 for (i
= 0; i
< CMD_ARGC
; i
+= 2) {
3593 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
], stlink_dap_param
.vid
[i
/ 2]);
3594 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[i
+ 1], stlink_dap_param
.pid
[i
/ 2]);
3597 /* null termination */
3598 stlink_dap_param
.vid
[i
/ 2] = stlink_dap_param
.pid
[i
/ 2] = 0;
3604 static const struct command_registration stlink_dap_subcommand_handlers
[] = {
3607 .handler
= stlink_dap_serial_command
,
3608 .mode
= COMMAND_CONFIG
,
3609 .help
= "set the serial number of the adapter",
3610 .usage
= "<serial_number>",
3614 .handler
= stlink_dap_vid_pid
,
3615 .mode
= COMMAND_CONFIG
,
3616 .help
= "USB VID and PID of the adapter",
3617 .usage
= "(vid pid)+",
3619 COMMAND_REGISTRATION_DONE
3623 static const struct command_registration stlink_dap_command_handlers
[] = {
3626 .mode
= COMMAND_ANY
,
3627 .help
= "perform st-link management",
3628 .chain
= stlink_dap_subcommand_handlers
,
3631 COMMAND_REGISTRATION_DONE
3635 static int stlink_dap_init(void)
3637 enum reset_types jtag_reset_config
= jtag_get_reset_config();
3640 LOG_DEBUG("stlink_dap_init()");
3642 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
3643 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
3644 stlink_dap_param
.connect_under_reset
= true;
3646 LOG_WARNING("\'srst_nogate\' reset_config option is required");
3649 if (transport_is_dapdirect_swd())
3650 stlink_dap_param
.transport
= HL_TRANSPORT_SWD
;
3651 else if (transport_is_dapdirect_jtag())
3652 stlink_dap_param
.transport
= HL_TRANSPORT_JTAG
;
3654 LOG_ERROR("Unsupported transport");
3658 retval
= stlink_usb_open(&stlink_dap_param
, (void **)&stlink_dap_handle
);
3659 if (retval
!= ERROR_OK
)
3662 if (!(stlink_dap_handle
->version
.flags
& STLINK_F_HAS_DAP_REG
)) {
3663 LOG_ERROR("ST-Link version does not support DAP direct transport");
3670 static int stlink_dap_quit(void)
3672 LOG_DEBUG("stlink_dap_quit()");
3674 free((void *)stlink_dap_param
.serial
);
3675 stlink_dap_param
.serial
= NULL
;
3677 return stlink_usb_close(stlink_dap_handle
);
3681 static int stlink_dap_reset(int req_trst
, int req_srst
)
3683 LOG_DEBUG("stlink_dap_reset(%d)", req_srst
);
3684 return stlink_usb_assert_srst(stlink_dap_handle
,
3685 req_srst
? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
3686 : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH
);
3690 static int stlink_dap_speed(int speed
)
3693 LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
3694 return ERROR_JTAG_NOT_IMPLEMENTED
;
3697 stlink_dap_param
.initial_interface_speed
= speed
;
3698 stlink_speed(stlink_dap_handle
, speed
, false);
3703 static int stlink_dap_khz(int khz
, int *jtag_speed
)
3706 LOG_ERROR("RCLK not supported");
3710 *jtag_speed
= stlink_speed(stlink_dap_handle
, khz
, true);
3715 static int stlink_dap_speed_div(int speed
, int *khz
)
3721 static const struct dap_ops stlink_dap_ops
= {
3722 .connect
= stlink_dap_op_connect
,
3723 .send_sequence
= stlink_dap_op_send_sequence
,
3724 .queue_dp_read
= stlink_dap_op_queue_dp_read
,
3725 .queue_dp_write
= stlink_dap_op_queue_dp_write
,
3726 .queue_ap_read
= stlink_dap_op_queue_ap_read
,
3727 .queue_ap_write
= stlink_dap_op_queue_ap_write
,
3728 .queue_ap_abort
= stlink_dap_op_queue_ap_abort
,
3729 .run
= stlink_dap_op_run
,
3730 .sync
= NULL
, /* optional */
3731 .quit
= stlink_dap_op_quit
, /* optional */
3734 static const char *const stlink_dap_transport
[] = { "dapdirect_jtag", "dapdirect_swd", NULL
};
3736 struct adapter_driver stlink_dap_adapter_driver
= {
3738 .transports
= stlink_dap_transport
,
3739 .commands
= stlink_dap_command_handlers
,
3741 .init
= stlink_dap_init
,
3742 .quit
= stlink_dap_quit
,
3743 .reset
= stlink_dap_reset
,
3744 .speed
= stlink_dap_speed
,
3745 .khz
= stlink_dap_khz
,
3746 .speed_div
= stlink_dap_speed_div
,
3747 .config_trace
= stlink_dap_config_trace
,
3748 .poll_trace
= stlink_dap_trace_read
,
3750 .dap_jtag_ops
= &stlink_dap_ops
,
3751 .dap_swd_ops
= &stlink_dap_ops
,