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 <jtag/interface.h>
35 #include <jtag/hla/hla_layout.h>
36 #include <jtag/hla/hla_transport.h>
37 #include <jtag/hla/hla_interface.h>
38 #include <target/target.h>
40 #include <target/cortex_m.h>
42 #include "libusb_common.h"
44 #define ENDPOINT_IN 0x80
45 #define ENDPOINT_OUT 0x00
47 #define STLINK_WRITE_TIMEOUT 1000
48 #define STLINK_READ_TIMEOUT 1000
50 #define STLINK_NULL_EP 0
51 #define STLINK_RX_EP (1|ENDPOINT_IN)
52 #define STLINK_TX_EP (2|ENDPOINT_OUT)
53 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
55 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
56 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
58 #define STLINK_SG_SIZE (31)
59 #define STLINK_DATA_SIZE (4096)
60 #define STLINK_CMD_SIZE_V2 (16)
61 #define STLINK_CMD_SIZE_V1 (10)
63 #define STLINK_V1_PID (0x3744)
64 #define STLINK_V2_PID (0x3748)
65 #define STLINK_V2_1_PID (0x374B)
66 #define STLINK_V2_1_NO_MSD_PID (0x3752)
68 /* the current implementation of the stlink limits
69 * 8bit read/writes to max 64 bytes. */
70 #define STLINK_MAX_RW8 (64)
72 /* "WAIT" responses will be retried (with exponential backoff) at
73 * most this many times before failing to caller.
75 #define MAX_WAIT_RETRIES 8
77 enum stlink_jtag_api_version
{
78 STLINK_JTAG_API_V1
= 1,
83 struct stlink_usb_version
{
90 /** highest supported jtag api version */
91 enum stlink_jtag_api_version jtag_api_max
;
92 /** one bit for each feature supported. See macros STLINK_F_* */
97 struct stlink_usb_handle_s
{
99 struct jtag_libusb_device_handle
*fd
;
101 struct libusb_transfer
*trans
;
109 uint8_t cmdbuf
[STLINK_SG_SIZE
];
115 uint8_t databuf
[STLINK_DATA_SIZE
];
117 uint32_t max_mem_packet
;
119 enum hl_transports transport
;
121 struct stlink_usb_version version
;
126 /** this is the currently used jtag api */
127 enum stlink_jtag_api_version jtag_api
;
130 /** whether SWO tracing is enabled or not */
132 /** trace module source clock */
135 /** reconnect is needed next time we try to query the
137 bool reconnect_pending
;
140 #define STLINK_SWIM_ERR_OK 0x00
141 #define STLINK_SWIM_BUSY 0x01
142 #define STLINK_DEBUG_ERR_OK 0x80
143 #define STLINK_DEBUG_ERR_FAULT 0x81
144 #define STLINK_SWD_AP_WAIT 0x10
145 #define STLINK_SWD_AP_FAULT 0x11
146 #define STLINK_SWD_AP_ERROR 0x12
147 #define STLINK_SWD_AP_PARITY_ERROR 0x13
148 #define STLINK_JTAG_WRITE_ERROR 0x0c
149 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
150 #define STLINK_SWD_DP_WAIT 0x14
151 #define STLINK_SWD_DP_FAULT 0x15
152 #define STLINK_SWD_DP_ERROR 0x16
153 #define STLINK_SWD_DP_PARITY_ERROR 0x17
155 #define STLINK_SWD_AP_WDATA_ERROR 0x18
156 #define STLINK_SWD_AP_STICKY_ERROR 0x19
157 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
159 #define STLINK_CORE_RUNNING 0x80
160 #define STLINK_CORE_HALTED 0x81
161 #define STLINK_CORE_STAT_UNKNOWN -1
163 #define STLINK_GET_VERSION 0xF1
164 #define STLINK_DEBUG_COMMAND 0xF2
165 #define STLINK_DFU_COMMAND 0xF3
166 #define STLINK_SWIM_COMMAND 0xF4
167 #define STLINK_GET_CURRENT_MODE 0xF5
168 #define STLINK_GET_TARGET_VOLTAGE 0xF7
170 #define STLINK_DEV_DFU_MODE 0x00
171 #define STLINK_DEV_MASS_MODE 0x01
172 #define STLINK_DEV_DEBUG_MODE 0x02
173 #define STLINK_DEV_SWIM_MODE 0x03
174 #define STLINK_DEV_BOOTLOADER_MODE 0x04
175 #define STLINK_DEV_UNKNOWN_MODE -1
177 #define STLINK_DFU_EXIT 0x07
180 STLINK_SWIM_ENTER_SEQ
181 1.3ms low then 750Hz then 1.5kHz
184 STM8 DM pulls reset pin low 50us
187 uint8_t (0=low|1=high)
194 send syncronization seq (16us low, response 64 clocks low)
196 #define STLINK_SWIM_ENTER 0x00
197 #define STLINK_SWIM_EXIT 0x01
198 #define STLINK_SWIM_READ_CAP 0x02
199 #define STLINK_SWIM_SPEED 0x03
200 #define STLINK_SWIM_ENTER_SEQ 0x04
201 #define STLINK_SWIM_GEN_RST 0x05
202 #define STLINK_SWIM_RESET 0x06
203 #define STLINK_SWIM_ASSERT_RESET 0x07
204 #define STLINK_SWIM_DEASSERT_RESET 0x08
205 #define STLINK_SWIM_READSTATUS 0x09
206 #define STLINK_SWIM_WRITEMEM 0x0a
207 #define STLINK_SWIM_READMEM 0x0b
208 #define STLINK_SWIM_READBUF 0x0c
210 #define STLINK_DEBUG_ENTER_JTAG 0x00
211 #define STLINK_DEBUG_GETSTATUS 0x01
212 #define STLINK_DEBUG_FORCEDEBUG 0x02
213 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
214 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
215 #define STLINK_DEBUG_APIV1_READREG 0x05
216 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
217 #define STLINK_DEBUG_READMEM_32BIT 0x07
218 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
219 #define STLINK_DEBUG_RUNCORE 0x09
220 #define STLINK_DEBUG_STEPCORE 0x0a
221 #define STLINK_DEBUG_APIV1_SETFP 0x0b
222 #define STLINK_DEBUG_READMEM_8BIT 0x0c
223 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
224 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
225 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
226 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
228 #define STLINK_DEBUG_ENTER_JTAG 0x00
229 #define STLINK_DEBUG_ENTER_SWD 0xa3
231 #define STLINK_DEBUG_APIV1_ENTER 0x20
232 #define STLINK_DEBUG_EXIT 0x21
233 #define STLINK_DEBUG_READCOREID 0x22
235 #define STLINK_DEBUG_APIV2_ENTER 0x30
236 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
237 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
238 #define STLINK_DEBUG_APIV2_READREG 0x33
239 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
240 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
241 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
243 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
244 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
245 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
247 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
248 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
249 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
250 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
251 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
253 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
254 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
256 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
257 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
258 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
260 #define STLINK_TRACE_SIZE 4096
261 #define STLINK_TRACE_MAX_HZ 2000000
265 STLINK_MODE_UNKNOWN
= 0,
268 STLINK_MODE_DEBUG_JTAG
,
269 STLINK_MODE_DEBUG_SWD
,
270 STLINK_MODE_DEBUG_SWIM
273 #define REQUEST_SENSE 0x03
274 #define REQUEST_SENSE_LENGTH 18
277 * Map the relevant features, quirks and workaround for specific firmware
280 #define STLINK_F_HAS_TRACE (1UL << 0)
281 #define STLINK_F_HAS_SWD_SET_FREQ (1UL << 1)
284 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
291 /* SWD clock speed */
292 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
294 {1800, 1}, /* default */
307 /* JTAG clock speed */
308 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
313 {1125, 32}, /* default */
319 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
320 static int stlink_swim_status(void *handle
);
323 static int stlink_usb_xfer_v1_get_status(void *handle
)
325 struct stlink_usb_handle_s
*h
= handle
;
327 assert(handle
!= NULL
);
330 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
332 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
333 13, STLINK_READ_TIMEOUT
) != 13)
338 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
341 if (t1
!= 0x53425355)
349 if (h
->cmdbuf
[12] != 0)
356 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
358 struct stlink_usb_handle_s
*h
= handle
;
360 assert(handle
!= NULL
);
362 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
363 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
367 if (h
->direction
== h
->tx_ep
&& size
) {
368 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
369 size
, STLINK_WRITE_TIMEOUT
) != size
) {
370 LOG_DEBUG("bulk write failed");
373 } else if (h
->direction
== h
->rx_ep
&& size
) {
374 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
375 size
, STLINK_READ_TIMEOUT
) != size
) {
376 LOG_DEBUG("bulk read failed");
385 static int stlink_usb_xfer_v1_get_sense(void *handle
)
388 struct stlink_usb_handle_s
*h
= handle
;
390 assert(handle
!= NULL
);
392 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
394 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
395 h
->cmdbuf
[h
->cmdidx
++] = 0;
396 h
->cmdbuf
[h
->cmdidx
++] = 0;
397 h
->cmdbuf
[h
->cmdidx
++] = 0;
398 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
400 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
405 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
412 transfers block in cmdbuf
413 <size> indicates number of bytes in the following
416 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
418 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
419 struct stlink_usb_handle_s
*h
= handle
;
421 assert(handle
!= NULL
);
423 if (h
->version
.stlink
== 1) {
424 cmdsize
= STLINK_SG_SIZE
;
425 /* put length in bCBWCBLength */
426 h
->cmdbuf
[14] = h
->cmdidx
-15;
429 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
434 if (h
->version
.stlink
== 1) {
435 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
436 /* check csw status */
437 if (h
->cmdbuf
[12] == 1) {
438 LOG_DEBUG("get sense");
439 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
450 Converts an STLINK status code held in the first byte of a response
451 to an openocd error, logs any error/wait status as debug output.
453 static int stlink_usb_error_check(void *handle
)
455 struct stlink_usb_handle_s
*h
= handle
;
457 assert(handle
!= NULL
);
459 if (h
->transport
== HL_TRANSPORT_SWIM
) {
460 switch (h
->databuf
[0]) {
461 case STLINK_SWIM_ERR_OK
:
463 case STLINK_SWIM_BUSY
:
466 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
471 /* TODO: no error checking yet on api V1 */
472 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
473 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
475 switch (h
->databuf
[0]) {
476 case STLINK_DEBUG_ERR_OK
:
478 case STLINK_DEBUG_ERR_FAULT
:
479 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
481 case STLINK_SWD_AP_WAIT
:
482 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
484 case STLINK_SWD_DP_WAIT
:
485 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
487 case STLINK_JTAG_WRITE_ERROR
:
488 LOG_DEBUG("Write error");
490 case STLINK_JTAG_WRITE_VERIF_ERROR
:
491 LOG_DEBUG("Write verify error, ignoring");
493 case STLINK_SWD_AP_FAULT
:
494 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
495 * returns ERROR_OK with the comment:
496 * Change in error status when reading outside RAM.
497 * This fix allows CDT plugin to visualize memory.
499 LOG_DEBUG("STLINK_SWD_AP_FAULT");
501 case STLINK_SWD_AP_ERROR
:
502 LOG_DEBUG("STLINK_SWD_AP_ERROR");
504 case STLINK_SWD_AP_PARITY_ERROR
:
505 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
507 case STLINK_SWD_DP_FAULT
:
508 LOG_DEBUG("STLINK_SWD_DP_FAULT");
510 case STLINK_SWD_DP_ERROR
:
511 LOG_DEBUG("STLINK_SWD_DP_ERROR");
513 case STLINK_SWD_DP_PARITY_ERROR
:
514 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
516 case STLINK_SWD_AP_WDATA_ERROR
:
517 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
519 case STLINK_SWD_AP_STICKY_ERROR
:
520 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
522 case STLINK_SWD_AP_STICKYORUN_ERROR
:
523 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
526 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
532 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
534 Works for commands where the STLINK_DEBUG status is returned in the first
535 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
537 Returns an openocd result code.
539 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
543 struct stlink_usb_handle_s
*h
= handle
;
546 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
547 res
= stlink_usb_xfer(handle
, buf
, size
);
552 if (h
->transport
== HL_TRANSPORT_SWIM
) {
553 res
= stlink_swim_status(handle
);
558 res
= stlink_usb_error_check(handle
);
559 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
560 usleep((1<<retries
++) * 1000);
568 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
570 struct stlink_usb_handle_s
*h
= handle
;
572 assert(handle
!= NULL
);
574 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
576 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
577 size
, STLINK_READ_TIMEOUT
) != size
) {
578 LOG_ERROR("bulk trace read failed");
586 this function writes transfer length in
587 the right place in the cb
589 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
591 struct stlink_usb_handle_s
*h
= handle
;
593 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
596 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
598 struct stlink_usb_handle_s
*h
= handle
;
600 /* fill the send buffer */
601 strcpy((char *)h
->cmdbuf
, "USBC");
603 /* csw tag not used */
604 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
606 /* cbw data transfer length (in the following data phase in or out) */
607 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
610 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
611 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
612 /* cdb clength (is filled in at xfer) */
613 h
->cmdbuf
[h
->cmdidx
++] = 0;
617 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
619 struct stlink_usb_handle_s
*h
= handle
;
621 h
->direction
= direction
;
625 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
626 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
628 if (h
->version
.stlink
== 1)
629 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
633 static int stlink_usb_version(void *handle
)
638 struct stlink_usb_handle_s
*h
= handle
;
640 assert(handle
!= NULL
);
642 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
644 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
646 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
651 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
653 h
->version
.stlink
= (v
>> 12) & 0x0f;
654 h
->version
.jtag
= (v
>> 6) & 0x3f;
655 h
->version
.swim
= v
& 0x3f;
656 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
657 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
660 switch (h
->version
.stlink
) {
662 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
663 if (h
->version
.jtag
>= 11)
664 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
666 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
670 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
671 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
673 /* API for trace from J13 */
674 /* API for target voltage from J13 */
675 if (h
->version
.jtag
>= 13)
676 flags
|= STLINK_F_HAS_TRACE
;
678 /* API to set SWD frequency from J22 */
679 if (h
->version
.jtag
>= 22)
680 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
686 h
->version
.flags
= flags
;
688 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
691 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
699 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
701 struct stlink_usb_handle_s
*h
= handle
;
702 uint32_t adc_results
[2];
704 /* no error message, simply quit with error */
705 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
706 return ERROR_COMMAND_NOTFOUND
;
708 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
710 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
712 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
714 if (result
!= ERROR_OK
)
718 adc_results
[0] = le_to_h_u32(h
->databuf
);
719 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
724 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
726 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
731 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
733 struct stlink_usb_handle_s
*h
= handle
;
735 assert(handle
!= NULL
);
737 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
738 return ERROR_COMMAND_NOTFOUND
;
740 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
742 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
743 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
744 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
747 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
749 if (result
!= ERROR_OK
)
755 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
757 struct stlink_usb_handle_s
*h
= handle
;
759 assert(handle
!= NULL
);
761 /* only supported by stlink/v2 and for firmware >= 24 */
762 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
763 return ERROR_COMMAND_NOTFOUND
;
765 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
767 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
768 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
769 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
772 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
774 if (result
!= ERROR_OK
)
781 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
784 struct stlink_usb_handle_s
*h
= handle
;
786 assert(handle
!= NULL
);
788 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
790 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
792 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
797 *mode
= h
->databuf
[0];
803 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
806 struct stlink_usb_handle_s
*h
= handle
;
808 assert(handle
!= NULL
);
810 /* on api V2 we are able the read the latest command
812 * TODO: we need the test on api V1 too
814 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
817 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
820 case STLINK_MODE_DEBUG_JTAG
:
821 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
822 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
823 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
825 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
826 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
828 case STLINK_MODE_DEBUG_SWD
:
829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
830 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
831 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
833 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
836 case STLINK_MODE_DEBUG_SWIM
:
837 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
838 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
839 /* no answer for this function... */
842 case STLINK_MODE_DFU
:
843 case STLINK_MODE_MASS
:
848 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
852 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
855 struct stlink_usb_handle_s
*h
= handle
;
857 assert(handle
!= NULL
);
859 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
862 case STLINK_MODE_DEBUG_JTAG
:
863 case STLINK_MODE_DEBUG_SWD
:
864 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
865 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
867 case STLINK_MODE_DEBUG_SWIM
:
868 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
869 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
871 case STLINK_MODE_DFU
:
872 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
873 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
875 case STLINK_MODE_MASS
:
880 res
= stlink_usb_xfer(handle
, 0, 0);
888 static int stlink_usb_assert_srst(void *handle
, int srst
);
890 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
893 case HL_TRANSPORT_SWD
:
894 return STLINK_MODE_DEBUG_SWD
;
895 case HL_TRANSPORT_JTAG
:
896 return STLINK_MODE_DEBUG_JTAG
;
897 case HL_TRANSPORT_SWIM
:
898 return STLINK_MODE_DEBUG_SWIM
;
900 return STLINK_MODE_UNKNOWN
;
905 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
909 enum stlink_mode emode
;
910 struct stlink_usb_handle_s
*h
= handle
;
912 assert(handle
!= NULL
);
914 res
= stlink_usb_current_mode(handle
, &mode
);
919 LOG_DEBUG("MODE: 0x%02X", mode
);
921 /* try to exit current mode */
923 case STLINK_DEV_DFU_MODE
:
924 emode
= STLINK_MODE_DFU
;
926 case STLINK_DEV_DEBUG_MODE
:
927 emode
= STLINK_MODE_DEBUG_SWD
;
929 case STLINK_DEV_SWIM_MODE
:
930 emode
= STLINK_MODE_DEBUG_SWIM
;
932 case STLINK_DEV_BOOTLOADER_MODE
:
933 case STLINK_DEV_MASS_MODE
:
935 emode
= STLINK_MODE_UNKNOWN
;
939 if (emode
!= STLINK_MODE_UNKNOWN
) {
940 res
= stlink_usb_mode_leave(handle
, emode
);
946 res
= stlink_usb_current_mode(handle
, &mode
);
951 /* we check the target voltage here as an aid to debugging connection problems.
952 * the stlink requires the target Vdd to be connected for reliable debugging.
953 * this cmd is supported in all modes except DFU
955 if (mode
!= STLINK_DEV_DFU_MODE
) {
957 float target_voltage
;
959 /* check target voltage (if supported) */
960 res
= stlink_usb_check_voltage(h
, &target_voltage
);
962 if (res
!= ERROR_OK
) {
963 if (res
!= ERROR_COMMAND_NOTFOUND
)
964 LOG_ERROR("voltage check failed");
965 /* attempt to continue as it is not a catastrophic failure */
967 /* check for a sensible target voltage, operating range is 1.65-5.5v
968 * according to datasheet */
969 if (target_voltage
< 1.5)
970 LOG_ERROR("target voltage may be too low for reliable debugging");
974 LOG_DEBUG("MODE: 0x%02X", mode
);
976 /* set selected mode */
977 emode
= stlink_get_mode(h
->transport
);
979 if (emode
== STLINK_MODE_UNKNOWN
) {
980 LOG_ERROR("selected mode (transport) not supported");
984 /* preliminary SRST assert:
985 * We want SRST is asserted before activating debug signals (mode_enter).
986 * As the required mode has not been set, the adapter may not know what pin to use.
987 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
988 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
989 * after power on, SWIM_RST stays unchanged */
990 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
991 stlink_usb_assert_srst(handle
, 0);
992 /* do not check the return status here, we will
993 proceed and enter the desired mode below
994 and try asserting srst again. */
996 res
= stlink_usb_mode_enter(handle
, emode
);
1000 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1001 if (connect_under_reset
) {
1002 res
= stlink_usb_assert_srst(handle
, 0);
1003 if (res
!= ERROR_OK
)
1007 res
= stlink_usb_current_mode(handle
, &mode
);
1009 if (res
!= ERROR_OK
)
1012 LOG_DEBUG("MODE: 0x%02X", mode
);
1017 /* request status from last swim request */
1018 static int stlink_swim_status(void *handle
)
1020 struct stlink_usb_handle_s
*h
= handle
;
1023 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1024 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1025 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1026 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1027 if (res
!= ERROR_OK
)
1032 the purpose of this function is unknown...
1033 capabilites? anyway for swim v6 it returns
1036 __attribute__((unused
))
1037 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1039 struct stlink_usb_handle_s
*h
= handle
;
1042 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1043 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1044 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1045 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1046 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
1047 if (res
!= ERROR_OK
)
1049 memcpy(cap
, h
->databuf
, 8);
1053 /* debug dongle assert/deassert sreset line */
1054 static int stlink_swim_assert_reset(void *handle
, int reset
)
1056 struct stlink_usb_handle_s
*h
= handle
;
1059 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1060 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1062 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1064 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1065 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1066 if (res
!= ERROR_OK
)
1073 1.3ms low then 750Hz then 1.5kHz
1075 static int stlink_swim_enter(void *handle
)
1077 struct stlink_usb_handle_s
*h
= handle
;
1080 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1081 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1082 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1083 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1084 if (res
!= ERROR_OK
)
1089 /* switch high/low speed swim */
1090 static int stlink_swim_speed(void *handle
, int speed
)
1092 struct stlink_usb_handle_s
*h
= handle
;
1095 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1096 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1097 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1099 h
->cmdbuf
[h
->cmdidx
++] = 1;
1101 h
->cmdbuf
[h
->cmdidx
++] = 0;
1102 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1103 if (res
!= ERROR_OK
)
1109 initiate srst from swim.
1110 nrst is pulled low for 50us.
1112 static int stlink_swim_generate_rst(void *handle
)
1114 struct stlink_usb_handle_s
*h
= handle
;
1117 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1118 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1119 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1120 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1121 if (res
!= ERROR_OK
)
1127 send resyncronize sequence
1128 swim is pulled low for 16us
1129 reply is 64 clks low
1131 static int stlink_swim_resync(void *handle
)
1133 struct stlink_usb_handle_s
*h
= handle
;
1136 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1137 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1138 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1139 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1140 if (res
!= ERROR_OK
)
1145 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1147 struct stlink_usb_handle_s
*h
= handle
;
1150 unsigned int datalen
= 0;
1151 int cmdsize
= STLINK_CMD_SIZE_V2
;
1153 if (len
> STLINK_DATA_SIZE
)
1156 if (h
->version
.stlink
== 1)
1157 cmdsize
= STLINK_SG_SIZE
;
1159 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1160 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1161 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1162 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1164 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1166 for (i
= 0; i
< len
; i
++) {
1167 if (h
->cmdidx
== cmdsize
)
1168 h
->databuf
[datalen
++] = *(data
++);
1170 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1172 if (h
->version
.stlink
== 1)
1173 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1175 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1176 if (res
!= ERROR_OK
)
1181 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1183 struct stlink_usb_handle_s
*h
= handle
;
1186 if (len
> STLINK_DATA_SIZE
)
1189 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1190 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1191 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1192 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1194 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1196 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1197 if (res
!= ERROR_OK
)
1200 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1201 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1202 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1203 res
= stlink_usb_xfer(handle
, data
, len
);
1204 if (res
!= ERROR_OK
)
1211 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1214 struct stlink_usb_handle_s
*h
= handle
;
1216 assert(handle
!= NULL
);
1218 /* there is no swim read core id cmd */
1219 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1224 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1226 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1227 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1229 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1231 if (res
!= ERROR_OK
)
1234 *idcode
= le_to_h_u32(h
->databuf
);
1236 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1241 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1243 struct stlink_usb_handle_s
*h
= handle
;
1246 assert(handle
!= NULL
);
1248 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1250 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1251 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1252 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1255 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1256 if (res
!= ERROR_OK
)
1259 *val
= le_to_h_u32(h
->databuf
+ 4);
1263 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1265 struct stlink_usb_handle_s
*h
= handle
;
1267 assert(handle
!= NULL
);
1269 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1271 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1272 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1273 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1275 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1276 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1278 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1281 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1285 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1287 struct stlink_usb_handle_s
*h
= handle
;
1289 assert(handle
!= NULL
);
1291 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1294 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1296 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1297 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1299 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1300 if (res
!= ERROR_OK
)
1303 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1304 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1307 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1308 if (res
!= ERROR_OK
)
1317 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1322 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1323 if (result
!= ERROR_OK
)
1324 return TARGET_UNKNOWN
;
1326 if (status
& S_HALT
)
1327 return TARGET_HALTED
;
1328 else if (status
& S_RESET_ST
)
1329 return TARGET_RESET
;
1331 return TARGET_RUNNING
;
1335 static enum target_state
stlink_usb_state(void *handle
)
1338 struct stlink_usb_handle_s
*h
= handle
;
1340 assert(handle
!= NULL
);
1342 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1343 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1344 if (res
!= ERROR_OK
)
1345 return TARGET_UNKNOWN
;
1347 res
= stlink_swim_resync(handle
);
1348 if (res
!= ERROR_OK
)
1349 return TARGET_UNKNOWN
;
1354 if (h
->reconnect_pending
) {
1355 LOG_INFO("Previous state query failed, trying to reconnect");
1356 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1358 if (res
!= ERROR_OK
)
1359 return TARGET_UNKNOWN
;
1361 h
->reconnect_pending
= false;
1364 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1365 res
= stlink_usb_v2_get_status(handle
);
1366 if (res
== TARGET_UNKNOWN
)
1367 h
->reconnect_pending
= true;
1371 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1373 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1374 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1376 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1378 if (res
!= ERROR_OK
)
1379 return TARGET_UNKNOWN
;
1381 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1382 return TARGET_RUNNING
;
1383 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1384 return TARGET_HALTED
;
1386 h
->reconnect_pending
= true;
1388 return TARGET_UNKNOWN
;
1391 static int stlink_usb_assert_srst(void *handle
, int srst
)
1393 struct stlink_usb_handle_s
*h
= handle
;
1395 assert(handle
!= NULL
);
1397 if (h
->transport
== HL_TRANSPORT_SWIM
)
1398 return stlink_swim_assert_reset(handle
, srst
);
1400 if (h
->version
.stlink
== 1)
1401 return ERROR_COMMAND_NOTFOUND
;
1403 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1405 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1406 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1407 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1409 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1413 static void stlink_usb_trace_disable(void *handle
)
1416 struct stlink_usb_handle_s
*h
= handle
;
1418 assert(handle
!= NULL
);
1420 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1422 LOG_DEBUG("Tracing: disable");
1424 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1425 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1426 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1427 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1429 if (res
== ERROR_OK
)
1430 h
->trace
.enabled
= false;
1435 static int stlink_usb_trace_enable(void *handle
)
1438 struct stlink_usb_handle_s
*h
= handle
;
1440 assert(handle
!= NULL
);
1442 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1443 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1445 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1446 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1447 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1449 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1452 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1454 if (res
== ERROR_OK
) {
1455 h
->trace
.enabled
= true;
1456 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1459 LOG_ERROR("Tracing is not supported by this version.");
1467 static int stlink_usb_reset(void *handle
)
1469 struct stlink_usb_handle_s
*h
= handle
;
1472 assert(handle
!= NULL
);
1474 if (h
->transport
== HL_TRANSPORT_SWIM
)
1475 return stlink_swim_generate_rst(handle
);
1477 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1479 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1481 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1482 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1484 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1486 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1487 if (retval
!= ERROR_OK
)
1490 if (h
->trace
.enabled
) {
1491 stlink_usb_trace_disable(h
);
1492 return stlink_usb_trace_enable(h
);
1499 static int stlink_usb_run(void *handle
)
1502 struct stlink_usb_handle_s
*h
= handle
;
1504 assert(handle
!= NULL
);
1506 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1507 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1512 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1514 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1515 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1517 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1521 static int stlink_usb_halt(void *handle
)
1524 struct stlink_usb_handle_s
*h
= handle
;
1526 assert(handle
!= NULL
);
1528 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1529 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1534 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1536 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1537 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1539 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1543 static int stlink_usb_step(void *handle
)
1545 struct stlink_usb_handle_s
*h
= handle
;
1547 assert(handle
!= NULL
);
1549 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1550 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1551 * that the Cortex-M3 currently does. */
1552 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1553 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1554 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1557 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1559 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1560 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1562 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1566 static int stlink_usb_read_regs(void *handle
)
1569 struct stlink_usb_handle_s
*h
= handle
;
1571 assert(handle
!= NULL
);
1573 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1575 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1576 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1577 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1579 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1581 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1583 if (res
!= ERROR_OK
)
1590 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1593 struct stlink_usb_handle_s
*h
= handle
;
1595 assert(handle
!= NULL
);
1597 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1599 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1600 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1601 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1603 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1604 h
->cmdbuf
[h
->cmdidx
++] = num
;
1606 if (h
->jtag_api
== STLINK_JTAG_API_V1
) {
1607 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1608 if (res
!= ERROR_OK
)
1610 *val
= le_to_h_u32(h
->databuf
);
1613 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1614 if (res
!= ERROR_OK
)
1616 *val
= le_to_h_u32(h
->databuf
+ 4);
1622 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1624 struct stlink_usb_handle_s
*h
= handle
;
1626 assert(handle
!= NULL
);
1628 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1630 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1631 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1632 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1634 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1635 h
->cmdbuf
[h
->cmdidx
++] = num
;
1636 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1639 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1642 static int stlink_usb_get_rw_status(void *handle
)
1645 struct stlink_usb_handle_s
*h
= handle
;
1647 assert(handle
!= NULL
);
1649 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1652 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1654 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1655 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1657 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1659 if (res
!= ERROR_OK
)
1662 return stlink_usb_error_check(h
);
1666 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1670 uint16_t read_len
= len
;
1671 struct stlink_usb_handle_s
*h
= handle
;
1673 assert(handle
!= NULL
);
1675 /* max 8bit read/write is 64bytes */
1676 if (len
> STLINK_MAX_RW8
) {
1677 LOG_DEBUG("max buffer length exceeded");
1681 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1683 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1684 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1685 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1687 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1690 /* we need to fix read length for single bytes */
1694 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1696 if (res
!= ERROR_OK
)
1699 memcpy(buffer
, h
->databuf
, len
);
1701 return stlink_usb_get_rw_status(handle
);
1705 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1706 const uint8_t *buffer
)
1709 struct stlink_usb_handle_s
*h
= handle
;
1711 assert(handle
!= NULL
);
1713 /* max 8bit read/write is 64bytes */
1714 if (len
> STLINK_MAX_RW8
) {
1715 LOG_DEBUG("max buffer length exceeded");
1719 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1721 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1722 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1723 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1725 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1728 res
= stlink_usb_xfer(handle
, buffer
, len
);
1730 if (res
!= ERROR_OK
)
1733 return stlink_usb_get_rw_status(handle
);
1737 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1741 struct stlink_usb_handle_s
*h
= handle
;
1743 assert(handle
!= NULL
);
1745 /* only supported by stlink/v2 and for firmware >= 26 */
1746 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1747 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1748 return ERROR_COMMAND_NOTFOUND
;
1750 /* data must be a multiple of 2 and half-word aligned */
1751 if (len
% 2 || addr
% 2) {
1752 LOG_DEBUG("Invalid data alignment");
1753 return ERROR_TARGET_UNALIGNED_ACCESS
;
1756 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1758 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1759 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
1760 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1762 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1765 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1767 if (res
!= ERROR_OK
)
1770 memcpy(buffer
, h
->databuf
, len
);
1772 return stlink_usb_get_rw_status(handle
);
1776 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1777 const uint8_t *buffer
)
1780 struct stlink_usb_handle_s
*h
= handle
;
1782 assert(handle
!= NULL
);
1784 /* only supported by stlink/v2 and for firmware >= 26 */
1785 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1786 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1787 return ERROR_COMMAND_NOTFOUND
;
1789 /* data must be a multiple of 2 and half-word aligned */
1790 if (len
% 2 || addr
% 2) {
1791 LOG_DEBUG("Invalid data alignment");
1792 return ERROR_TARGET_UNALIGNED_ACCESS
;
1795 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1797 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1798 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
1799 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1801 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1804 res
= stlink_usb_xfer(handle
, buffer
, len
);
1806 if (res
!= ERROR_OK
)
1809 return stlink_usb_get_rw_status(handle
);
1813 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1817 struct stlink_usb_handle_s
*h
= handle
;
1819 assert(handle
!= NULL
);
1821 /* data must be a multiple of 4 and word aligned */
1822 if (len
% 4 || addr
% 4) {
1823 LOG_DEBUG("Invalid data alignment");
1824 return ERROR_TARGET_UNALIGNED_ACCESS
;
1827 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1830 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1831 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1833 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1836 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1838 if (res
!= ERROR_OK
)
1841 memcpy(buffer
, h
->databuf
, len
);
1843 return stlink_usb_get_rw_status(handle
);
1847 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1848 const uint8_t *buffer
)
1851 struct stlink_usb_handle_s
*h
= handle
;
1853 assert(handle
!= NULL
);
1855 /* data must be a multiple of 4 and word aligned */
1856 if (len
% 4 || addr
% 4) {
1857 LOG_DEBUG("Invalid data alignment");
1858 return ERROR_TARGET_UNALIGNED_ACCESS
;
1861 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1863 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1864 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1865 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1867 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1870 res
= stlink_usb_xfer(handle
, buffer
, len
);
1872 if (res
!= ERROR_OK
)
1875 return stlink_usb_get_rw_status(handle
);
1878 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1880 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1881 if (max_tar_block
== 0)
1883 return max_tar_block
;
1886 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1887 uint32_t count
, uint8_t *buffer
)
1889 int retval
= ERROR_OK
;
1890 uint32_t bytes_remaining
;
1892 struct stlink_usb_handle_s
*h
= handle
;
1894 /* calculate byte count */
1897 /* switch to 8 bit if stlink does not support 16 bit memory read */
1898 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1899 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1904 bytes_remaining
= (size
!= 1) ? \
1905 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1907 if (count
< bytes_remaining
)
1908 bytes_remaining
= count
;
1910 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1911 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
1912 if (retval
!= ERROR_OK
)
1916 * all stlink support 8/32bit memory read/writes and only from
1917 * stlink V2J26 there is support for 16 bit memory read/write.
1918 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1923 /* When in jtag mode the stlink uses the auto-increment functionality.
1924 * However it expects us to pass the data correctly, this includes
1925 * alignment and any page boundaries. We already do this as part of the
1926 * adi_v5 implementation, but the stlink is a hla adapter and so this
1927 * needs implementing manually.
1928 * currently this only affects jtag mode, according to ST they do single
1929 * access in SWD mode - but this may change and so we do it for both modes */
1931 /* we first need to check for any unaligned bytes */
1932 if (addr
& (size
- 1)) {
1934 uint32_t head_bytes
= size
- (addr
& (size
- 1));
1935 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
1936 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1937 usleep((1<<retries
++) * 1000);
1940 if (retval
!= ERROR_OK
)
1942 buffer
+= head_bytes
;
1944 count
-= head_bytes
;
1945 bytes_remaining
-= head_bytes
;
1948 if (bytes_remaining
& (size
- 1))
1949 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1951 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
1953 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
1955 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
1957 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1958 usleep((1<<retries
++) * 1000);
1961 if (retval
!= ERROR_OK
)
1964 buffer
+= bytes_remaining
;
1965 addr
+= bytes_remaining
;
1966 count
-= bytes_remaining
;
1972 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
1973 uint32_t count
, const uint8_t *buffer
)
1975 int retval
= ERROR_OK
;
1976 uint32_t bytes_remaining
;
1978 struct stlink_usb_handle_s
*h
= handle
;
1980 /* calculate byte count */
1983 /* switch to 8 bit if stlink does not support 16 bit memory read */
1984 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1985 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1990 bytes_remaining
= (size
!= 1) ? \
1991 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1993 if (count
< bytes_remaining
)
1994 bytes_remaining
= count
;
1996 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1997 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
1998 if (retval
!= ERROR_OK
)
2002 * all stlink support 8/32bit memory read/writes and only from
2003 * stlink V2J26 there is support for 16 bit memory read/write.
2004 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2009 /* When in jtag mode the stlink uses the auto-increment functionality.
2010 * However it expects us to pass the data correctly, this includes
2011 * alignment and any page boundaries. We already do this as part of the
2012 * adi_v5 implementation, but the stlink is a hla adapter and so this
2013 * needs implementing manually.
2014 * currently this only affects jtag mode, according to ST they do single
2015 * access in SWD mode - but this may change and so we do it for both modes */
2017 /* we first need to check for any unaligned bytes */
2018 if (addr
& (size
- 1)) {
2020 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2021 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2022 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2023 usleep((1<<retries
++) * 1000);
2026 if (retval
!= ERROR_OK
)
2028 buffer
+= head_bytes
;
2030 count
-= head_bytes
;
2031 bytes_remaining
-= head_bytes
;
2034 if (bytes_remaining
& (size
- 1))
2035 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2037 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2039 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2042 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2043 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2044 usleep((1<<retries
++) * 1000);
2047 if (retval
!= ERROR_OK
)
2050 buffer
+= bytes_remaining
;
2051 addr
+= bytes_remaining
;
2052 count
-= bytes_remaining
;
2059 static int stlink_usb_override_target(const char *targetname
)
2061 return !strcmp(targetname
, "cortex_m");
2064 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2067 we dont care what the khz rate is
2068 we only have low and high speed...
2069 before changing speed the SWIM_CSR HS bit
2073 stlink_swim_speed(handle
, 0);
2075 stlink_swim_speed(handle
, 1);
2079 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2082 int speed_index
= -1;
2083 int speed_diff
= INT_MAX
;
2086 for (i
= 0; i
< map_size
; i
++) {
2087 if (khz
== map
[i
].speed
) {
2091 int current_diff
= khz
- map
[i
].speed
;
2092 /* get abs value for comparison */
2093 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2094 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2095 speed_diff
= current_diff
;
2101 if (speed_index
== -1) {
2102 /* this will only be here if we cannot match the slow speed.
2103 * use the slowest speed we support.*/
2104 speed_index
= map_size
- 1;
2106 } else if (i
== map_size
)
2109 if (!match
&& query
) {
2110 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2111 khz
, map
[speed_index
].speed
);
2117 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2120 struct stlink_usb_handle_s
*h
= handle
;
2122 /* old firmware cannot change it */
2123 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2126 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2127 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2130 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2131 if (result
!= ERROR_OK
) {
2132 LOG_ERROR("Unable to set adapter speed");
2137 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2140 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2143 struct stlink_usb_handle_s
*h
= handle
;
2145 /* only supported by stlink/v2 and for firmware >= 24 */
2146 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
2149 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2150 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2153 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2154 if (result
!= ERROR_OK
) {
2155 LOG_ERROR("Unable to set adapter speed");
2160 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2163 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2167 LOG_DEBUG("Supported clock speeds are:");
2168 for (i
= 0; i
< map_size
; i
++)
2169 LOG_DEBUG("%d kHz", map
[i
].speed
);
2172 static int stlink_speed(void *handle
, int khz
, bool query
)
2174 struct stlink_usb_handle_s
*h
= handle
;
2179 if (h
->transport
== HL_TRANSPORT_SWIM
)
2180 return stlink_speed_swim(handle
, khz
, query
);
2181 else if (h
->transport
== HL_TRANSPORT_SWD
)
2182 return stlink_speed_swd(handle
, khz
, query
);
2183 else if (h
->transport
== HL_TRANSPORT_JTAG
)
2184 return stlink_speed_jtag(handle
, khz
, query
);
2190 static int stlink_usb_close(void *handle
)
2194 enum stlink_mode emode
;
2195 struct stlink_usb_handle_s
*h
= handle
;
2198 res
= stlink_usb_current_mode(handle
, &mode
);
2201 /* do not exit if return code != ERROR_OK,
2202 it prevents us from closing jtag_libusb */
2204 if (res
== ERROR_OK
) {
2205 /* try to exit current mode */
2207 case STLINK_DEV_DFU_MODE
:
2208 emode
= STLINK_MODE_DFU
;
2210 case STLINK_DEV_DEBUG_MODE
:
2211 emode
= STLINK_MODE_DEBUG_SWD
;
2213 case STLINK_DEV_SWIM_MODE
:
2214 emode
= STLINK_MODE_DEBUG_SWIM
;
2216 case STLINK_DEV_BOOTLOADER_MODE
:
2217 case STLINK_DEV_MASS_MODE
:
2219 emode
= STLINK_MODE_UNKNOWN
;
2223 if (emode
!= STLINK_MODE_UNKNOWN
)
2224 stlink_usb_mode_leave(handle
, emode
);
2225 /* do not check return code, it prevent
2226 us from closing jtag_libusb */
2230 jtag_libusb_close(h
->fd
);
2238 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2240 int err
, retry_count
= 1;
2241 struct stlink_usb_handle_s
*h
;
2242 enum stlink_jtag_api_version api
;
2244 LOG_DEBUG("stlink_usb_open");
2246 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2249 LOG_DEBUG("malloc failed");
2253 h
->transport
= param
->transport
;
2255 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2256 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2257 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2258 param
->serial
? param
->serial
: "");
2262 On certain host USB configurations(e.g. MacBook Air)
2263 STLINKv2 dongle seems to have its FW in a funky state if,
2264 after plugging it in, you try to use openocd with it more
2265 then once (by launching and closing openocd). In cases like
2266 that initial attempt to read the FW info via
2267 stlink_usb_version will fail and the device has to be reset
2268 in order to become operational.
2271 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2272 LOG_ERROR("open failed");
2276 jtag_libusb_set_configuration(h
->fd
, 0);
2278 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2279 LOG_DEBUG("claim interface failed");
2283 /* RX EP is common for all versions */
2284 h
->rx_ep
= STLINK_RX_EP
;
2287 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2288 LOG_DEBUG("libusb_get_pid failed");
2292 /* wrap version for first read */
2295 h
->version
.stlink
= 1;
2296 h
->tx_ep
= STLINK_TX_EP
;
2298 case STLINK_V2_1_PID
:
2299 case STLINK_V2_1_NO_MSD_PID
:
2300 h
->version
.stlink
= 2;
2301 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2302 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2305 /* fall through - we assume V2 to be the default version*/
2307 h
->version
.stlink
= 2;
2308 h
->tx_ep
= STLINK_TX_EP
;
2309 h
->trace_ep
= STLINK_TRACE_EP
;
2313 /* get the device version */
2314 err
= stlink_usb_version(h
);
2316 if (err
== ERROR_OK
) {
2318 } else if (h
->version
.stlink
== 1 ||
2320 LOG_ERROR("read version failed");
2323 err
= jtag_libusb_release_interface(h
->fd
, 0);
2324 if (err
!= ERROR_OK
) {
2325 LOG_ERROR("release interface failed");
2329 err
= jtag_libusb_reset_device(h
->fd
);
2330 if (err
!= ERROR_OK
) {
2331 LOG_ERROR("reset device failed");
2335 jtag_libusb_close(h
->fd
);
2337 Give the device one second to settle down and
2340 usleep(1 * 1000 * 1000);
2345 /* check if mode is supported */
2348 switch (h
->transport
) {
2349 case HL_TRANSPORT_SWD
:
2350 if (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
)
2353 case HL_TRANSPORT_JTAG
:
2354 if (h
->version
.jtag
== 0)
2357 case HL_TRANSPORT_SWIM
:
2358 if (h
->version
.swim
== 0)
2366 if (err
!= ERROR_OK
) {
2367 LOG_ERROR("mode (transport) not supported by device");
2371 api
= h
->version
.jtag_api_max
;
2373 LOG_INFO("using stlink api v%d", api
);
2375 /* set the used jtag api, this will default to the newest supported version */
2378 /* initialize the debug hardware */
2379 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2381 if (err
!= ERROR_OK
) {
2382 LOG_ERROR("init mode failed (unable to connect to the target)");
2386 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2387 err
= stlink_swim_enter(h
);
2388 if (err
!= ERROR_OK
) {
2389 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2393 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2397 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2398 /* jtag clock speed only supported by stlink/v2 and for firmware >= 24 */
2399 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 24) {
2400 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2401 stlink_speed(h
, param
->initial_interface_speed
, false);
2403 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2404 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
2405 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2406 stlink_speed(h
, param
->initial_interface_speed
, false);
2410 /* get cpuid, so we can determine the max page size
2411 * start with a safe default */
2412 h
->max_mem_packet
= (1 << 10);
2415 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2416 if (err
== ERROR_OK
) {
2417 uint32_t cpuid
= le_to_h_u32(buffer
);
2418 int i
= (cpuid
>> 4) & 0xf;
2419 if (i
== 4 || i
== 3) {
2420 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2421 h
->max_mem_packet
= (1 << 12);
2425 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2432 stlink_usb_close(h
);
2437 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2438 uint32_t port_size
, unsigned int *trace_freq
)
2440 struct stlink_usb_handle_s
*h
= handle
;
2442 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2443 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2444 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2449 stlink_usb_trace_disable(h
);
2453 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2454 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2455 STLINK_TRACE_MAX_HZ
);
2459 stlink_usb_trace_disable(h
);
2462 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2463 h
->trace
.source_hz
= *trace_freq
;
2465 return stlink_usb_trace_enable(h
);
2469 struct hl_layout_api_s stlink_usb_layout_api
= {
2471 .open
= stlink_usb_open
,
2473 .close
= stlink_usb_close
,
2475 .idcode
= stlink_usb_idcode
,
2477 .state
= stlink_usb_state
,
2479 .reset
= stlink_usb_reset
,
2481 .assert_srst
= stlink_usb_assert_srst
,
2483 .run
= stlink_usb_run
,
2485 .halt
= stlink_usb_halt
,
2487 .step
= stlink_usb_step
,
2489 .read_regs
= stlink_usb_read_regs
,
2491 .read_reg
= stlink_usb_read_reg
,
2493 .write_reg
= stlink_usb_write_reg
,
2495 .read_mem
= stlink_usb_read_mem
,
2497 .write_mem
= stlink_usb_write_mem
,
2499 .write_debug_reg
= stlink_usb_write_debug_reg
,
2501 .override_target
= stlink_usb_override_target
,
2503 .speed
= stlink_speed
,
2505 .config_trace
= stlink_config_trace
,
2507 .poll_trace
= stlink_usb_trace_read
,