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)
289 /* SWD clock speed */
290 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
292 {1800, 1}, /* default */
305 /* JTAG clock speed */
306 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
311 {1125, 32}, /* default */
317 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
318 static int stlink_swim_status(void *handle
);
321 static int stlink_usb_xfer_v1_get_status(void *handle
)
323 struct stlink_usb_handle_s
*h
= handle
;
325 assert(handle
!= NULL
);
328 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
330 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
331 13, STLINK_READ_TIMEOUT
) != 13)
336 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
339 if (t1
!= 0x53425355)
347 if (h
->cmdbuf
[12] != 0)
354 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
356 struct stlink_usb_handle_s
*h
= handle
;
358 assert(handle
!= NULL
);
360 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
361 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
365 if (h
->direction
== h
->tx_ep
&& size
) {
366 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
367 size
, STLINK_WRITE_TIMEOUT
) != size
) {
368 LOG_DEBUG("bulk write failed");
371 } else if (h
->direction
== h
->rx_ep
&& size
) {
372 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
373 size
, STLINK_READ_TIMEOUT
) != size
) {
374 LOG_DEBUG("bulk read failed");
383 static int stlink_usb_xfer_v1_get_sense(void *handle
)
386 struct stlink_usb_handle_s
*h
= handle
;
388 assert(handle
!= NULL
);
390 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
392 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
393 h
->cmdbuf
[h
->cmdidx
++] = 0;
394 h
->cmdbuf
[h
->cmdidx
++] = 0;
395 h
->cmdbuf
[h
->cmdidx
++] = 0;
396 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
398 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
403 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
410 transfers block in cmdbuf
411 <size> indicates number of bytes in the following
414 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
416 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
417 struct stlink_usb_handle_s
*h
= handle
;
419 assert(handle
!= NULL
);
421 if (h
->version
.stlink
== 1) {
422 cmdsize
= STLINK_SG_SIZE
;
423 /* put length in bCBWCBLength */
424 h
->cmdbuf
[14] = h
->cmdidx
-15;
427 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
432 if (h
->version
.stlink
== 1) {
433 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
434 /* check csw status */
435 if (h
->cmdbuf
[12] == 1) {
436 LOG_DEBUG("get sense");
437 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
448 Converts an STLINK status code held in the first byte of a response
449 to an openocd error, logs any error/wait status as debug output.
451 static int stlink_usb_error_check(void *handle
)
453 struct stlink_usb_handle_s
*h
= handle
;
455 assert(handle
!= NULL
);
457 if (h
->transport
== HL_TRANSPORT_SWIM
) {
458 switch (h
->databuf
[0]) {
459 case STLINK_SWIM_ERR_OK
:
461 case STLINK_SWIM_BUSY
:
464 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
469 /* TODO: no error checking yet on api V1 */
470 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
471 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
473 switch (h
->databuf
[0]) {
474 case STLINK_DEBUG_ERR_OK
:
476 case STLINK_DEBUG_ERR_FAULT
:
477 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
479 case STLINK_SWD_AP_WAIT
:
480 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
482 case STLINK_SWD_DP_WAIT
:
483 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
485 case STLINK_JTAG_WRITE_ERROR
:
486 LOG_DEBUG("Write error");
488 case STLINK_JTAG_WRITE_VERIF_ERROR
:
489 LOG_DEBUG("Write verify error, ignoring");
491 case STLINK_SWD_AP_FAULT
:
492 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
493 * returns ERROR_OK with the comment:
494 * Change in error status when reading outside RAM.
495 * This fix allows CDT plugin to visualize memory.
497 LOG_DEBUG("STLINK_SWD_AP_FAULT");
499 case STLINK_SWD_AP_ERROR
:
500 LOG_DEBUG("STLINK_SWD_AP_ERROR");
502 case STLINK_SWD_AP_PARITY_ERROR
:
503 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
505 case STLINK_SWD_DP_FAULT
:
506 LOG_DEBUG("STLINK_SWD_DP_FAULT");
508 case STLINK_SWD_DP_ERROR
:
509 LOG_DEBUG("STLINK_SWD_DP_ERROR");
511 case STLINK_SWD_DP_PARITY_ERROR
:
512 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
514 case STLINK_SWD_AP_WDATA_ERROR
:
515 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
517 case STLINK_SWD_AP_STICKY_ERROR
:
518 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
520 case STLINK_SWD_AP_STICKYORUN_ERROR
:
521 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
524 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
530 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
532 Works for commands where the STLINK_DEBUG status is returned in the first
533 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
535 Returns an openocd result code.
537 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
541 struct stlink_usb_handle_s
*h
= handle
;
544 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
545 res
= stlink_usb_xfer(handle
, buf
, size
);
550 if (h
->transport
== HL_TRANSPORT_SWIM
) {
551 res
= stlink_swim_status(handle
);
556 res
= stlink_usb_error_check(handle
);
557 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
558 usleep((1<<retries
++) * 1000);
566 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
568 struct stlink_usb_handle_s
*h
= handle
;
570 assert(handle
!= NULL
);
572 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
574 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
575 size
, STLINK_READ_TIMEOUT
) != size
) {
576 LOG_ERROR("bulk trace read failed");
584 this function writes transfer length in
585 the right place in the cb
587 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
589 struct stlink_usb_handle_s
*h
= handle
;
591 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
594 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
596 struct stlink_usb_handle_s
*h
= handle
;
598 /* fill the send buffer */
599 strcpy((char *)h
->cmdbuf
, "USBC");
601 /* csw tag not used */
602 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
604 /* cbw data transfer length (in the following data phase in or out) */
605 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
608 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
609 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
610 /* cdb clength (is filled in at xfer) */
611 h
->cmdbuf
[h
->cmdidx
++] = 0;
615 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
617 struct stlink_usb_handle_s
*h
= handle
;
619 h
->direction
= direction
;
623 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
624 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
626 if (h
->version
.stlink
== 1)
627 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
631 static int stlink_usb_version(void *handle
)
636 struct stlink_usb_handle_s
*h
= handle
;
638 assert(handle
!= NULL
);
640 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
642 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
644 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
649 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
651 h
->version
.stlink
= (v
>> 12) & 0x0f;
652 h
->version
.jtag
= (v
>> 6) & 0x3f;
653 h
->version
.swim
= v
& 0x3f;
654 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
655 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
658 switch (h
->version
.stlink
) {
660 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
661 if (h
->version
.jtag
>= 11)
662 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
664 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
668 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
669 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
671 /* API for trace from J13 */
672 if (h
->version
.jtag
>= 13)
673 flags
|= STLINK_F_HAS_TRACE
;
679 h
->version
.flags
= flags
;
681 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
684 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
692 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
694 struct stlink_usb_handle_s
*h
= handle
;
695 uint32_t adc_results
[2];
697 /* only supported by stlink/v2 and for firmware >= 13 */
698 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 13)
699 return ERROR_COMMAND_NOTFOUND
;
701 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
703 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
705 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
707 if (result
!= ERROR_OK
)
711 adc_results
[0] = le_to_h_u32(h
->databuf
);
712 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
717 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
719 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
724 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
726 struct stlink_usb_handle_s
*h
= handle
;
728 assert(handle
!= NULL
);
730 /* only supported by stlink/v2 and for firmware >= 22 */
731 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 22)
732 return ERROR_COMMAND_NOTFOUND
;
734 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
736 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
737 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
738 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
741 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
743 if (result
!= ERROR_OK
)
749 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
751 struct stlink_usb_handle_s
*h
= handle
;
753 assert(handle
!= NULL
);
755 /* only supported by stlink/v2 and for firmware >= 24 */
756 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
757 return ERROR_COMMAND_NOTFOUND
;
759 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
761 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
762 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
763 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
766 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
768 if (result
!= ERROR_OK
)
775 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
778 struct stlink_usb_handle_s
*h
= handle
;
780 assert(handle
!= NULL
);
782 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
784 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
786 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
791 *mode
= h
->databuf
[0];
797 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
800 struct stlink_usb_handle_s
*h
= handle
;
802 assert(handle
!= NULL
);
804 /* on api V2 we are able the read the latest command
806 * TODO: we need the test on api V1 too
808 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
811 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
814 case STLINK_MODE_DEBUG_JTAG
:
815 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
816 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
817 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
819 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
820 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
822 case STLINK_MODE_DEBUG_SWD
:
823 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
824 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
825 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
827 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
828 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
830 case STLINK_MODE_DEBUG_SWIM
:
831 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
832 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
833 /* no answer for this function... */
836 case STLINK_MODE_DFU
:
837 case STLINK_MODE_MASS
:
842 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
846 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
849 struct stlink_usb_handle_s
*h
= handle
;
851 assert(handle
!= NULL
);
853 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
856 case STLINK_MODE_DEBUG_JTAG
:
857 case STLINK_MODE_DEBUG_SWD
:
858 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
859 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
861 case STLINK_MODE_DEBUG_SWIM
:
862 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
863 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
865 case STLINK_MODE_DFU
:
866 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
867 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
869 case STLINK_MODE_MASS
:
874 res
= stlink_usb_xfer(handle
, 0, 0);
882 static int stlink_usb_assert_srst(void *handle
, int srst
);
884 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
887 case HL_TRANSPORT_SWD
:
888 return STLINK_MODE_DEBUG_SWD
;
889 case HL_TRANSPORT_JTAG
:
890 return STLINK_MODE_DEBUG_JTAG
;
891 case HL_TRANSPORT_SWIM
:
892 return STLINK_MODE_DEBUG_SWIM
;
894 return STLINK_MODE_UNKNOWN
;
899 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
903 enum stlink_mode emode
;
904 struct stlink_usb_handle_s
*h
= handle
;
906 assert(handle
!= NULL
);
908 res
= stlink_usb_current_mode(handle
, &mode
);
913 LOG_DEBUG("MODE: 0x%02X", mode
);
915 /* try to exit current mode */
917 case STLINK_DEV_DFU_MODE
:
918 emode
= STLINK_MODE_DFU
;
920 case STLINK_DEV_DEBUG_MODE
:
921 emode
= STLINK_MODE_DEBUG_SWD
;
923 case STLINK_DEV_SWIM_MODE
:
924 emode
= STLINK_MODE_DEBUG_SWIM
;
926 case STLINK_DEV_BOOTLOADER_MODE
:
927 case STLINK_DEV_MASS_MODE
:
929 emode
= STLINK_MODE_UNKNOWN
;
933 if (emode
!= STLINK_MODE_UNKNOWN
) {
934 res
= stlink_usb_mode_leave(handle
, emode
);
940 res
= stlink_usb_current_mode(handle
, &mode
);
945 /* we check the target voltage here as an aid to debugging connection problems.
946 * the stlink requires the target Vdd to be connected for reliable debugging.
947 * this cmd is supported in all modes except DFU
949 if (mode
!= STLINK_DEV_DFU_MODE
) {
951 float target_voltage
;
953 /* check target voltage (if supported) */
954 res
= stlink_usb_check_voltage(h
, &target_voltage
);
956 if (res
!= ERROR_OK
) {
957 if (res
!= ERROR_COMMAND_NOTFOUND
)
958 LOG_ERROR("voltage check failed");
959 /* attempt to continue as it is not a catastrophic failure */
961 /* check for a sensible target voltage, operating range is 1.65-5.5v
962 * according to datasheet */
963 if (target_voltage
< 1.5)
964 LOG_ERROR("target voltage may be too low for reliable debugging");
968 LOG_DEBUG("MODE: 0x%02X", mode
);
970 /* set selected mode */
971 emode
= stlink_get_mode(h
->transport
);
973 if (emode
== STLINK_MODE_UNKNOWN
) {
974 LOG_ERROR("selected mode (transport) not supported");
978 /* preliminary SRST assert:
979 * We want SRST is asserted before activating debug signals (mode_enter).
980 * As the required mode has not been set, the adapter may not know what pin to use.
981 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
982 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
983 * after power on, SWIM_RST stays unchanged */
984 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
985 stlink_usb_assert_srst(handle
, 0);
986 /* do not check the return status here, we will
987 proceed and enter the desired mode below
988 and try asserting srst again. */
990 res
= stlink_usb_mode_enter(handle
, emode
);
994 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
995 if (connect_under_reset
) {
996 res
= stlink_usb_assert_srst(handle
, 0);
1001 res
= stlink_usb_current_mode(handle
, &mode
);
1003 if (res
!= ERROR_OK
)
1006 LOG_DEBUG("MODE: 0x%02X", mode
);
1011 /* request status from last swim request */
1012 static int stlink_swim_status(void *handle
)
1014 struct stlink_usb_handle_s
*h
= handle
;
1017 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1018 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1019 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1020 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1021 if (res
!= ERROR_OK
)
1026 the purpose of this function is unknown...
1027 capabilites? anyway for swim v6 it returns
1030 __attribute__((unused
))
1031 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1033 struct stlink_usb_handle_s
*h
= handle
;
1036 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1037 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1038 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1039 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1040 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
1041 if (res
!= ERROR_OK
)
1043 memcpy(cap
, h
->databuf
, 8);
1047 /* debug dongle assert/deassert sreset line */
1048 static int stlink_swim_assert_reset(void *handle
, int reset
)
1050 struct stlink_usb_handle_s
*h
= handle
;
1053 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1054 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1056 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1058 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1059 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1060 if (res
!= ERROR_OK
)
1067 1.3ms low then 750Hz then 1.5kHz
1069 static int stlink_swim_enter(void *handle
)
1071 struct stlink_usb_handle_s
*h
= handle
;
1074 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1075 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1076 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1077 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1078 if (res
!= ERROR_OK
)
1083 /* switch high/low speed swim */
1084 static int stlink_swim_speed(void *handle
, int speed
)
1086 struct stlink_usb_handle_s
*h
= handle
;
1089 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1090 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1091 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1093 h
->cmdbuf
[h
->cmdidx
++] = 1;
1095 h
->cmdbuf
[h
->cmdidx
++] = 0;
1096 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1097 if (res
!= ERROR_OK
)
1103 initiate srst from swim.
1104 nrst is pulled low for 50us.
1106 static int stlink_swim_generate_rst(void *handle
)
1108 struct stlink_usb_handle_s
*h
= handle
;
1111 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1112 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1113 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1114 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1115 if (res
!= ERROR_OK
)
1121 send resyncronize sequence
1122 swim is pulled low for 16us
1123 reply is 64 clks low
1125 static int stlink_swim_resync(void *handle
)
1127 struct stlink_usb_handle_s
*h
= handle
;
1130 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1131 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1132 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1133 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1134 if (res
!= ERROR_OK
)
1139 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1141 struct stlink_usb_handle_s
*h
= handle
;
1144 unsigned int datalen
= 0;
1145 int cmdsize
= STLINK_CMD_SIZE_V2
;
1147 if (len
> STLINK_DATA_SIZE
)
1150 if (h
->version
.stlink
== 1)
1151 cmdsize
= STLINK_SG_SIZE
;
1153 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1154 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1155 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1156 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1158 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1160 for (i
= 0; i
< len
; i
++) {
1161 if (h
->cmdidx
== cmdsize
)
1162 h
->databuf
[datalen
++] = *(data
++);
1164 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1166 if (h
->version
.stlink
== 1)
1167 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1169 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1170 if (res
!= ERROR_OK
)
1175 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1177 struct stlink_usb_handle_s
*h
= handle
;
1180 if (len
> STLINK_DATA_SIZE
)
1183 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1184 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1185 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1186 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1188 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1190 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1191 if (res
!= ERROR_OK
)
1194 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1195 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1196 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1197 res
= stlink_usb_xfer(handle
, data
, len
);
1198 if (res
!= ERROR_OK
)
1205 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1208 struct stlink_usb_handle_s
*h
= handle
;
1210 assert(handle
!= NULL
);
1212 /* there is no swim read core id cmd */
1213 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1218 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1220 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1221 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1223 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1225 if (res
!= ERROR_OK
)
1228 *idcode
= le_to_h_u32(h
->databuf
);
1230 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1235 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1237 struct stlink_usb_handle_s
*h
= handle
;
1240 assert(handle
!= NULL
);
1242 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1244 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1245 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1246 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1249 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1250 if (res
!= ERROR_OK
)
1253 *val
= le_to_h_u32(h
->databuf
+ 4);
1257 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1259 struct stlink_usb_handle_s
*h
= handle
;
1261 assert(handle
!= NULL
);
1263 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1265 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1266 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1267 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1269 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1270 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1272 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1275 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1279 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1281 struct stlink_usb_handle_s
*h
= handle
;
1283 assert(handle
!= NULL
);
1285 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1288 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1290 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1291 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1293 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1294 if (res
!= ERROR_OK
)
1297 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1298 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1301 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1302 if (res
!= ERROR_OK
)
1311 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1316 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1317 if (result
!= ERROR_OK
)
1318 return TARGET_UNKNOWN
;
1320 if (status
& S_HALT
)
1321 return TARGET_HALTED
;
1322 else if (status
& S_RESET_ST
)
1323 return TARGET_RESET
;
1325 return TARGET_RUNNING
;
1329 static enum target_state
stlink_usb_state(void *handle
)
1332 struct stlink_usb_handle_s
*h
= handle
;
1334 assert(handle
!= NULL
);
1336 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1337 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1338 if (res
!= ERROR_OK
)
1339 return TARGET_UNKNOWN
;
1341 res
= stlink_swim_resync(handle
);
1342 if (res
!= ERROR_OK
)
1343 return TARGET_UNKNOWN
;
1348 if (h
->reconnect_pending
) {
1349 LOG_INFO("Previous state query failed, trying to reconnect");
1350 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1352 if (res
!= ERROR_OK
)
1353 return TARGET_UNKNOWN
;
1355 h
->reconnect_pending
= false;
1358 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1359 res
= stlink_usb_v2_get_status(handle
);
1360 if (res
== TARGET_UNKNOWN
)
1361 h
->reconnect_pending
= true;
1365 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1367 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1368 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1370 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1372 if (res
!= ERROR_OK
)
1373 return TARGET_UNKNOWN
;
1375 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1376 return TARGET_RUNNING
;
1377 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1378 return TARGET_HALTED
;
1380 h
->reconnect_pending
= true;
1382 return TARGET_UNKNOWN
;
1385 static int stlink_usb_assert_srst(void *handle
, int srst
)
1387 struct stlink_usb_handle_s
*h
= handle
;
1389 assert(handle
!= NULL
);
1391 if (h
->transport
== HL_TRANSPORT_SWIM
)
1392 return stlink_swim_assert_reset(handle
, srst
);
1394 if (h
->version
.stlink
== 1)
1395 return ERROR_COMMAND_NOTFOUND
;
1397 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1399 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1400 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1401 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1403 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1407 static void stlink_usb_trace_disable(void *handle
)
1410 struct stlink_usb_handle_s
*h
= handle
;
1412 assert(handle
!= NULL
);
1414 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1416 LOG_DEBUG("Tracing: disable");
1418 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1419 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1420 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1421 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1423 if (res
== ERROR_OK
)
1424 h
->trace
.enabled
= false;
1429 static int stlink_usb_trace_enable(void *handle
)
1432 struct stlink_usb_handle_s
*h
= handle
;
1434 assert(handle
!= NULL
);
1436 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1437 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1439 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1440 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1441 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1443 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1446 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1448 if (res
== ERROR_OK
) {
1449 h
->trace
.enabled
= true;
1450 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1453 LOG_ERROR("Tracing is not supported by this version.");
1461 static int stlink_usb_reset(void *handle
)
1463 struct stlink_usb_handle_s
*h
= handle
;
1466 assert(handle
!= NULL
);
1468 if (h
->transport
== HL_TRANSPORT_SWIM
)
1469 return stlink_swim_generate_rst(handle
);
1471 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1473 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1475 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1476 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1478 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1480 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1481 if (retval
!= ERROR_OK
)
1484 if (h
->trace
.enabled
) {
1485 stlink_usb_trace_disable(h
);
1486 return stlink_usb_trace_enable(h
);
1493 static int stlink_usb_run(void *handle
)
1496 struct stlink_usb_handle_s
*h
= handle
;
1498 assert(handle
!= NULL
);
1500 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1501 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1506 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1508 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1509 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1511 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1515 static int stlink_usb_halt(void *handle
)
1518 struct stlink_usb_handle_s
*h
= handle
;
1520 assert(handle
!= NULL
);
1522 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1523 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1528 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1530 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1531 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1533 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1537 static int stlink_usb_step(void *handle
)
1539 struct stlink_usb_handle_s
*h
= handle
;
1541 assert(handle
!= NULL
);
1543 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1544 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1545 * that the Cortex-M3 currently does. */
1546 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1547 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1548 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1551 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1553 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1554 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1556 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1560 static int stlink_usb_read_regs(void *handle
)
1563 struct stlink_usb_handle_s
*h
= handle
;
1565 assert(handle
!= NULL
);
1567 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1569 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1570 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1571 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1573 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1575 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1577 if (res
!= ERROR_OK
)
1584 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1587 struct stlink_usb_handle_s
*h
= handle
;
1589 assert(handle
!= NULL
);
1591 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1593 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1594 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1595 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1597 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1598 h
->cmdbuf
[h
->cmdidx
++] = num
;
1600 if (h
->jtag_api
== STLINK_JTAG_API_V1
) {
1601 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1602 if (res
!= ERROR_OK
)
1604 *val
= le_to_h_u32(h
->databuf
);
1607 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1608 if (res
!= ERROR_OK
)
1610 *val
= le_to_h_u32(h
->databuf
+ 4);
1616 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1618 struct stlink_usb_handle_s
*h
= handle
;
1620 assert(handle
!= NULL
);
1622 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1625 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1626 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1628 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1629 h
->cmdbuf
[h
->cmdidx
++] = num
;
1630 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1633 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1636 static int stlink_usb_get_rw_status(void *handle
)
1639 struct stlink_usb_handle_s
*h
= handle
;
1641 assert(handle
!= NULL
);
1643 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1646 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1648 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1649 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1651 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1653 if (res
!= ERROR_OK
)
1656 return stlink_usb_error_check(h
);
1660 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1664 uint16_t read_len
= len
;
1665 struct stlink_usb_handle_s
*h
= handle
;
1667 assert(handle
!= NULL
);
1669 /* max 8bit read/write is 64bytes */
1670 if (len
> STLINK_MAX_RW8
) {
1671 LOG_DEBUG("max buffer length exceeded");
1675 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1677 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1678 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1679 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1681 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1684 /* we need to fix read length for single bytes */
1688 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1690 if (res
!= ERROR_OK
)
1693 memcpy(buffer
, h
->databuf
, len
);
1695 return stlink_usb_get_rw_status(handle
);
1699 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1700 const uint8_t *buffer
)
1703 struct stlink_usb_handle_s
*h
= handle
;
1705 assert(handle
!= NULL
);
1707 /* max 8bit read/write is 64bytes */
1708 if (len
> STLINK_MAX_RW8
) {
1709 LOG_DEBUG("max buffer length exceeded");
1713 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1715 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1716 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1717 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1719 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1722 res
= stlink_usb_xfer(handle
, buffer
, len
);
1724 if (res
!= ERROR_OK
)
1727 return stlink_usb_get_rw_status(handle
);
1731 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1735 struct stlink_usb_handle_s
*h
= handle
;
1737 assert(handle
!= NULL
);
1739 /* only supported by stlink/v2 and for firmware >= 26 */
1740 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1741 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1742 return ERROR_COMMAND_NOTFOUND
;
1744 /* data must be a multiple of 2 and half-word aligned */
1745 if (len
% 2 || addr
% 2) {
1746 LOG_DEBUG("Invalid data alignment");
1747 return ERROR_TARGET_UNALIGNED_ACCESS
;
1750 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1752 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1753 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
1754 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1756 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1759 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1761 if (res
!= ERROR_OK
)
1764 memcpy(buffer
, h
->databuf
, len
);
1766 return stlink_usb_get_rw_status(handle
);
1770 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1771 const uint8_t *buffer
)
1774 struct stlink_usb_handle_s
*h
= handle
;
1776 assert(handle
!= NULL
);
1778 /* only supported by stlink/v2 and for firmware >= 26 */
1779 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1780 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1781 return ERROR_COMMAND_NOTFOUND
;
1783 /* data must be a multiple of 2 and half-word aligned */
1784 if (len
% 2 || addr
% 2) {
1785 LOG_DEBUG("Invalid data alignment");
1786 return ERROR_TARGET_UNALIGNED_ACCESS
;
1789 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1791 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1792 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
1793 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1795 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1798 res
= stlink_usb_xfer(handle
, buffer
, len
);
1800 if (res
!= ERROR_OK
)
1803 return stlink_usb_get_rw_status(handle
);
1807 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1811 struct stlink_usb_handle_s
*h
= handle
;
1813 assert(handle
!= NULL
);
1815 /* data must be a multiple of 4 and word aligned */
1816 if (len
% 4 || addr
% 4) {
1817 LOG_DEBUG("Invalid data alignment");
1818 return ERROR_TARGET_UNALIGNED_ACCESS
;
1821 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1823 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1824 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1825 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1827 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1830 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1832 if (res
!= ERROR_OK
)
1835 memcpy(buffer
, h
->databuf
, len
);
1837 return stlink_usb_get_rw_status(handle
);
1841 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1842 const uint8_t *buffer
)
1845 struct stlink_usb_handle_s
*h
= handle
;
1847 assert(handle
!= NULL
);
1849 /* data must be a multiple of 4 and word aligned */
1850 if (len
% 4 || addr
% 4) {
1851 LOG_DEBUG("Invalid data alignment");
1852 return ERROR_TARGET_UNALIGNED_ACCESS
;
1855 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1857 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1858 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1859 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1861 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1864 res
= stlink_usb_xfer(handle
, buffer
, len
);
1866 if (res
!= ERROR_OK
)
1869 return stlink_usb_get_rw_status(handle
);
1872 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1874 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1875 if (max_tar_block
== 0)
1877 return max_tar_block
;
1880 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1881 uint32_t count
, uint8_t *buffer
)
1883 int retval
= ERROR_OK
;
1884 uint32_t bytes_remaining
;
1886 struct stlink_usb_handle_s
*h
= handle
;
1888 /* calculate byte count */
1891 /* switch to 8 bit if stlink does not support 16 bit memory read */
1892 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1893 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1898 bytes_remaining
= (size
!= 1) ? \
1899 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1901 if (count
< bytes_remaining
)
1902 bytes_remaining
= count
;
1904 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1905 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
1906 if (retval
!= ERROR_OK
)
1910 * all stlink support 8/32bit memory read/writes and only from
1911 * stlink V2J26 there is support for 16 bit memory read/write.
1912 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1917 /* When in jtag mode the stlink uses the auto-increment functionality.
1918 * However it expects us to pass the data correctly, this includes
1919 * alignment and any page boundaries. We already do this as part of the
1920 * adi_v5 implementation, but the stlink is a hla adapter and so this
1921 * needs implementing manually.
1922 * currently this only affects jtag mode, according to ST they do single
1923 * access in SWD mode - but this may change and so we do it for both modes */
1925 /* we first need to check for any unaligned bytes */
1926 if (addr
& (size
- 1)) {
1928 uint32_t head_bytes
= size
- (addr
& (size
- 1));
1929 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
1930 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1931 usleep((1<<retries
++) * 1000);
1934 if (retval
!= ERROR_OK
)
1936 buffer
+= head_bytes
;
1938 count
-= head_bytes
;
1939 bytes_remaining
-= head_bytes
;
1942 if (bytes_remaining
& (size
- 1))
1943 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1945 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
1947 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
1949 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
1951 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1952 usleep((1<<retries
++) * 1000);
1955 if (retval
!= ERROR_OK
)
1958 buffer
+= bytes_remaining
;
1959 addr
+= bytes_remaining
;
1960 count
-= bytes_remaining
;
1966 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
1967 uint32_t count
, const uint8_t *buffer
)
1969 int retval
= ERROR_OK
;
1970 uint32_t bytes_remaining
;
1972 struct stlink_usb_handle_s
*h
= handle
;
1974 /* calculate byte count */
1977 /* switch to 8 bit if stlink does not support 16 bit memory read */
1978 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1979 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1984 bytes_remaining
= (size
!= 1) ? \
1985 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1987 if (count
< bytes_remaining
)
1988 bytes_remaining
= count
;
1990 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1991 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
1992 if (retval
!= ERROR_OK
)
1996 * all stlink support 8/32bit memory read/writes and only from
1997 * stlink V2J26 there is support for 16 bit memory read/write.
1998 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2003 /* When in jtag mode the stlink uses the auto-increment functionality.
2004 * However it expects us to pass the data correctly, this includes
2005 * alignment and any page boundaries. We already do this as part of the
2006 * adi_v5 implementation, but the stlink is a hla adapter and so this
2007 * needs implementing manually.
2008 * currently this only affects jtag mode, according to ST they do single
2009 * access in SWD mode - but this may change and so we do it for both modes */
2011 /* we first need to check for any unaligned bytes */
2012 if (addr
& (size
- 1)) {
2014 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2015 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2016 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2017 usleep((1<<retries
++) * 1000);
2020 if (retval
!= ERROR_OK
)
2022 buffer
+= head_bytes
;
2024 count
-= head_bytes
;
2025 bytes_remaining
-= head_bytes
;
2028 if (bytes_remaining
& (size
- 1))
2029 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2031 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2033 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2036 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2037 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2038 usleep((1<<retries
++) * 1000);
2041 if (retval
!= ERROR_OK
)
2044 buffer
+= bytes_remaining
;
2045 addr
+= bytes_remaining
;
2046 count
-= bytes_remaining
;
2053 static int stlink_usb_override_target(const char *targetname
)
2055 return !strcmp(targetname
, "cortex_m");
2058 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2061 we dont care what the khz rate is
2062 we only have low and high speed...
2063 before changing speed the SWIM_CSR HS bit
2067 stlink_swim_speed(handle
, 0);
2069 stlink_swim_speed(handle
, 1);
2073 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2076 int speed_index
= -1;
2077 int speed_diff
= INT_MAX
;
2080 for (i
= 0; i
< map_size
; i
++) {
2081 if (khz
== map
[i
].speed
) {
2085 int current_diff
= khz
- map
[i
].speed
;
2086 /* get abs value for comparison */
2087 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2088 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2089 speed_diff
= current_diff
;
2095 if (speed_index
== -1) {
2096 /* this will only be here if we cannot match the slow speed.
2097 * use the slowest speed we support.*/
2098 speed_index
= map_size
- 1;
2100 } else if (i
== map_size
)
2103 if (!match
&& query
) {
2104 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2105 khz
, map
[speed_index
].speed
);
2111 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2114 struct stlink_usb_handle_s
*h
= handle
;
2116 /* only supported by stlink/v2 and for firmware >= 22 */
2117 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 22)
2120 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2121 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2124 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2125 if (result
!= ERROR_OK
) {
2126 LOG_ERROR("Unable to set adapter speed");
2131 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2134 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2137 struct stlink_usb_handle_s
*h
= handle
;
2139 /* only supported by stlink/v2 and for firmware >= 24 */
2140 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
2143 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2144 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2147 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2148 if (result
!= ERROR_OK
) {
2149 LOG_ERROR("Unable to set adapter speed");
2154 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2157 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2161 LOG_DEBUG("Supported clock speeds are:");
2162 for (i
= 0; i
< map_size
; i
++)
2163 LOG_DEBUG("%d kHz", map
[i
].speed
);
2166 static int stlink_speed(void *handle
, int khz
, bool query
)
2168 struct stlink_usb_handle_s
*h
= handle
;
2173 if (h
->transport
== HL_TRANSPORT_SWIM
)
2174 return stlink_speed_swim(handle
, khz
, query
);
2175 else if (h
->transport
== HL_TRANSPORT_SWD
)
2176 return stlink_speed_swd(handle
, khz
, query
);
2177 else if (h
->transport
== HL_TRANSPORT_JTAG
)
2178 return stlink_speed_jtag(handle
, khz
, query
);
2184 static int stlink_usb_close(void *handle
)
2188 enum stlink_mode emode
;
2189 struct stlink_usb_handle_s
*h
= handle
;
2192 res
= stlink_usb_current_mode(handle
, &mode
);
2195 /* do not exit if return code != ERROR_OK,
2196 it prevents us from closing jtag_libusb */
2198 if (res
== ERROR_OK
) {
2199 /* try to exit current mode */
2201 case STLINK_DEV_DFU_MODE
:
2202 emode
= STLINK_MODE_DFU
;
2204 case STLINK_DEV_DEBUG_MODE
:
2205 emode
= STLINK_MODE_DEBUG_SWD
;
2207 case STLINK_DEV_SWIM_MODE
:
2208 emode
= STLINK_MODE_DEBUG_SWIM
;
2210 case STLINK_DEV_BOOTLOADER_MODE
:
2211 case STLINK_DEV_MASS_MODE
:
2213 emode
= STLINK_MODE_UNKNOWN
;
2217 if (emode
!= STLINK_MODE_UNKNOWN
)
2218 stlink_usb_mode_leave(handle
, emode
);
2219 /* do not check return code, it prevent
2220 us from closing jtag_libusb */
2224 jtag_libusb_close(h
->fd
);
2232 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2234 int err
, retry_count
= 1;
2235 struct stlink_usb_handle_s
*h
;
2236 enum stlink_jtag_api_version api
;
2238 LOG_DEBUG("stlink_usb_open");
2240 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2243 LOG_DEBUG("malloc failed");
2247 h
->transport
= param
->transport
;
2249 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2250 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2251 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2252 param
->serial
? param
->serial
: "");
2256 On certain host USB configurations(e.g. MacBook Air)
2257 STLINKv2 dongle seems to have its FW in a funky state if,
2258 after plugging it in, you try to use openocd with it more
2259 then once (by launching and closing openocd). In cases like
2260 that initial attempt to read the FW info via
2261 stlink_usb_version will fail and the device has to be reset
2262 in order to become operational.
2265 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2266 LOG_ERROR("open failed");
2270 jtag_libusb_set_configuration(h
->fd
, 0);
2272 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2273 LOG_DEBUG("claim interface failed");
2277 /* RX EP is common for all versions */
2278 h
->rx_ep
= STLINK_RX_EP
;
2281 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2282 LOG_DEBUG("libusb_get_pid failed");
2286 /* wrap version for first read */
2289 h
->version
.stlink
= 1;
2290 h
->tx_ep
= STLINK_TX_EP
;
2292 case STLINK_V2_1_PID
:
2293 case STLINK_V2_1_NO_MSD_PID
:
2294 h
->version
.stlink
= 2;
2295 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2296 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2299 /* fall through - we assume V2 to be the default version*/
2301 h
->version
.stlink
= 2;
2302 h
->tx_ep
= STLINK_TX_EP
;
2303 h
->trace_ep
= STLINK_TRACE_EP
;
2307 /* get the device version */
2308 err
= stlink_usb_version(h
);
2310 if (err
== ERROR_OK
) {
2312 } else if (h
->version
.stlink
== 1 ||
2314 LOG_ERROR("read version failed");
2317 err
= jtag_libusb_release_interface(h
->fd
, 0);
2318 if (err
!= ERROR_OK
) {
2319 LOG_ERROR("release interface failed");
2323 err
= jtag_libusb_reset_device(h
->fd
);
2324 if (err
!= ERROR_OK
) {
2325 LOG_ERROR("reset device failed");
2329 jtag_libusb_close(h
->fd
);
2331 Give the device one second to settle down and
2334 usleep(1 * 1000 * 1000);
2339 /* check if mode is supported */
2342 switch (h
->transport
) {
2343 case HL_TRANSPORT_SWD
:
2344 if (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
)
2347 case HL_TRANSPORT_JTAG
:
2348 if (h
->version
.jtag
== 0)
2351 case HL_TRANSPORT_SWIM
:
2352 if (h
->version
.swim
== 0)
2360 if (err
!= ERROR_OK
) {
2361 LOG_ERROR("mode (transport) not supported by device");
2365 api
= h
->version
.jtag_api_max
;
2367 LOG_INFO("using stlink api v%d", api
);
2369 /* set the used jtag api, this will default to the newest supported version */
2372 /* initialize the debug hardware */
2373 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2375 if (err
!= ERROR_OK
) {
2376 LOG_ERROR("init mode failed (unable to connect to the target)");
2380 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2381 err
= stlink_swim_enter(h
);
2382 if (err
!= ERROR_OK
) {
2383 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2387 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2391 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2392 /* jtag clock speed only supported by stlink/v2 and for firmware >= 24 */
2393 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 24) {
2394 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2395 stlink_speed(h
, param
->initial_interface_speed
, false);
2397 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2398 /* clock speed only supported by stlink/v2 and for firmware >= 22 */
2399 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 22) {
2400 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2401 stlink_speed(h
, param
->initial_interface_speed
, false);
2405 /* get cpuid, so we can determine the max page size
2406 * start with a safe default */
2407 h
->max_mem_packet
= (1 << 10);
2410 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2411 if (err
== ERROR_OK
) {
2412 uint32_t cpuid
= le_to_h_u32(buffer
);
2413 int i
= (cpuid
>> 4) & 0xf;
2414 if (i
== 4 || i
== 3) {
2415 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2416 h
->max_mem_packet
= (1 << 12);
2420 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2427 stlink_usb_close(h
);
2432 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2433 uint32_t port_size
, unsigned int *trace_freq
)
2435 struct stlink_usb_handle_s
*h
= handle
;
2437 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2438 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2439 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2444 stlink_usb_trace_disable(h
);
2448 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2449 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2450 STLINK_TRACE_MAX_HZ
);
2454 stlink_usb_trace_disable(h
);
2457 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2458 h
->trace
.source_hz
= *trace_freq
;
2460 return stlink_usb_trace_enable(h
);
2464 struct hl_layout_api_s stlink_usb_layout_api
= {
2466 .open
= stlink_usb_open
,
2468 .close
= stlink_usb_close
,
2470 .idcode
= stlink_usb_idcode
,
2472 .state
= stlink_usb_state
,
2474 .reset
= stlink_usb_reset
,
2476 .assert_srst
= stlink_usb_assert_srst
,
2478 .run
= stlink_usb_run
,
2480 .halt
= stlink_usb_halt
,
2482 .step
= stlink_usb_step
,
2484 .read_regs
= stlink_usb_read_regs
,
2486 .read_reg
= stlink_usb_read_reg
,
2488 .write_reg
= stlink_usb_write_reg
,
2490 .read_mem
= stlink_usb_read_mem
,
2492 .write_mem
= stlink_usb_write_mem
,
2494 .write_debug_reg
= stlink_usb_write_debug_reg
,
2496 .override_target
= stlink_usb_override_target
,
2498 .speed
= stlink_speed
,
2500 .config_trace
= stlink_config_trace
,
2502 .poll_trace
= stlink_usb_trace_read
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)