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)
283 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
290 /* SWD clock speed */
291 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
293 {1800, 1}, /* default */
306 /* JTAG clock speed */
307 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
312 {1125, 32}, /* default */
318 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
319 static int stlink_swim_status(void *handle
);
322 static int stlink_usb_xfer_v1_get_status(void *handle
)
324 struct stlink_usb_handle_s
*h
= handle
;
326 assert(handle
!= NULL
);
329 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
331 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
332 13, STLINK_READ_TIMEOUT
) != 13)
337 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
340 if (t1
!= 0x53425355)
348 if (h
->cmdbuf
[12] != 0)
355 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
357 struct stlink_usb_handle_s
*h
= handle
;
359 assert(handle
!= NULL
);
361 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
362 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
366 if (h
->direction
== h
->tx_ep
&& size
) {
367 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
368 size
, STLINK_WRITE_TIMEOUT
) != size
) {
369 LOG_DEBUG("bulk write failed");
372 } else if (h
->direction
== h
->rx_ep
&& size
) {
373 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
374 size
, STLINK_READ_TIMEOUT
) != size
) {
375 LOG_DEBUG("bulk read failed");
384 static int stlink_usb_xfer_v1_get_sense(void *handle
)
387 struct stlink_usb_handle_s
*h
= handle
;
389 assert(handle
!= NULL
);
391 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
393 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
394 h
->cmdbuf
[h
->cmdidx
++] = 0;
395 h
->cmdbuf
[h
->cmdidx
++] = 0;
396 h
->cmdbuf
[h
->cmdidx
++] = 0;
397 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
399 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
404 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
411 transfers block in cmdbuf
412 <size> indicates number of bytes in the following
415 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
417 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
418 struct stlink_usb_handle_s
*h
= handle
;
420 assert(handle
!= NULL
);
422 if (h
->version
.stlink
== 1) {
423 cmdsize
= STLINK_SG_SIZE
;
424 /* put length in bCBWCBLength */
425 h
->cmdbuf
[14] = h
->cmdidx
-15;
428 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
433 if (h
->version
.stlink
== 1) {
434 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
435 /* check csw status */
436 if (h
->cmdbuf
[12] == 1) {
437 LOG_DEBUG("get sense");
438 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
449 Converts an STLINK status code held in the first byte of a response
450 to an openocd error, logs any error/wait status as debug output.
452 static int stlink_usb_error_check(void *handle
)
454 struct stlink_usb_handle_s
*h
= handle
;
456 assert(handle
!= NULL
);
458 if (h
->transport
== HL_TRANSPORT_SWIM
) {
459 switch (h
->databuf
[0]) {
460 case STLINK_SWIM_ERR_OK
:
462 case STLINK_SWIM_BUSY
:
465 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
470 /* TODO: no error checking yet on api V1 */
471 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
472 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
474 switch (h
->databuf
[0]) {
475 case STLINK_DEBUG_ERR_OK
:
477 case STLINK_DEBUG_ERR_FAULT
:
478 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
480 case STLINK_SWD_AP_WAIT
:
481 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
483 case STLINK_SWD_DP_WAIT
:
484 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
486 case STLINK_JTAG_WRITE_ERROR
:
487 LOG_DEBUG("Write error");
489 case STLINK_JTAG_WRITE_VERIF_ERROR
:
490 LOG_DEBUG("Write verify error, ignoring");
492 case STLINK_SWD_AP_FAULT
:
493 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
494 * returns ERROR_OK with the comment:
495 * Change in error status when reading outside RAM.
496 * This fix allows CDT plugin to visualize memory.
498 LOG_DEBUG("STLINK_SWD_AP_FAULT");
500 case STLINK_SWD_AP_ERROR
:
501 LOG_DEBUG("STLINK_SWD_AP_ERROR");
503 case STLINK_SWD_AP_PARITY_ERROR
:
504 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
506 case STLINK_SWD_DP_FAULT
:
507 LOG_DEBUG("STLINK_SWD_DP_FAULT");
509 case STLINK_SWD_DP_ERROR
:
510 LOG_DEBUG("STLINK_SWD_DP_ERROR");
512 case STLINK_SWD_DP_PARITY_ERROR
:
513 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
515 case STLINK_SWD_AP_WDATA_ERROR
:
516 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
518 case STLINK_SWD_AP_STICKY_ERROR
:
519 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
521 case STLINK_SWD_AP_STICKYORUN_ERROR
:
522 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
525 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
531 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
533 Works for commands where the STLINK_DEBUG status is returned in the first
534 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
536 Returns an openocd result code.
538 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
542 struct stlink_usb_handle_s
*h
= handle
;
545 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
546 res
= stlink_usb_xfer(handle
, buf
, size
);
551 if (h
->transport
== HL_TRANSPORT_SWIM
) {
552 res
= stlink_swim_status(handle
);
557 res
= stlink_usb_error_check(handle
);
558 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
559 usleep((1<<retries
++) * 1000);
567 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
569 struct stlink_usb_handle_s
*h
= handle
;
571 assert(handle
!= NULL
);
573 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
575 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
576 size
, STLINK_READ_TIMEOUT
) != size
) {
577 LOG_ERROR("bulk trace read failed");
585 this function writes transfer length in
586 the right place in the cb
588 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
590 struct stlink_usb_handle_s
*h
= handle
;
592 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
595 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
597 struct stlink_usb_handle_s
*h
= handle
;
599 /* fill the send buffer */
600 strcpy((char *)h
->cmdbuf
, "USBC");
602 /* csw tag not used */
603 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
605 /* cbw data transfer length (in the following data phase in or out) */
606 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
609 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
610 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
611 /* cdb clength (is filled in at xfer) */
612 h
->cmdbuf
[h
->cmdidx
++] = 0;
616 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
618 struct stlink_usb_handle_s
*h
= handle
;
620 h
->direction
= direction
;
624 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
625 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
627 if (h
->version
.stlink
== 1)
628 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
632 static int stlink_usb_version(void *handle
)
637 struct stlink_usb_handle_s
*h
= handle
;
639 assert(handle
!= NULL
);
641 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
643 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
645 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
650 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
652 h
->version
.stlink
= (v
>> 12) & 0x0f;
653 h
->version
.jtag
= (v
>> 6) & 0x3f;
654 h
->version
.swim
= v
& 0x3f;
655 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
656 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
659 switch (h
->version
.stlink
) {
661 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
662 if (h
->version
.jtag
>= 11)
663 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
665 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
669 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
670 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
672 /* API for trace from J13 */
673 /* API for target voltage from J13 */
674 if (h
->version
.jtag
>= 13)
675 flags
|= STLINK_F_HAS_TRACE
;
681 h
->version
.flags
= flags
;
683 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
686 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
694 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
696 struct stlink_usb_handle_s
*h
= handle
;
697 uint32_t adc_results
[2];
699 /* no error message, simply quit with error */
700 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
701 return ERROR_COMMAND_NOTFOUND
;
703 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
705 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
707 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
709 if (result
!= ERROR_OK
)
713 adc_results
[0] = le_to_h_u32(h
->databuf
);
714 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
719 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
721 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
726 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
728 struct stlink_usb_handle_s
*h
= handle
;
730 assert(handle
!= NULL
);
732 /* only supported by stlink/v2 and for firmware >= 22 */
733 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 22)
734 return ERROR_COMMAND_NOTFOUND
;
736 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
738 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
739 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
740 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
743 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
745 if (result
!= ERROR_OK
)
751 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
753 struct stlink_usb_handle_s
*h
= handle
;
755 assert(handle
!= NULL
);
757 /* only supported by stlink/v2 and for firmware >= 24 */
758 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
759 return ERROR_COMMAND_NOTFOUND
;
761 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
763 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
764 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
765 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
768 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
770 if (result
!= ERROR_OK
)
777 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
780 struct stlink_usb_handle_s
*h
= handle
;
782 assert(handle
!= NULL
);
784 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
786 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
788 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
793 *mode
= h
->databuf
[0];
799 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
802 struct stlink_usb_handle_s
*h
= handle
;
804 assert(handle
!= NULL
);
806 /* on api V2 we are able the read the latest command
808 * TODO: we need the test on api V1 too
810 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
813 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
816 case STLINK_MODE_DEBUG_JTAG
:
817 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
818 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
819 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
821 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
822 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
824 case STLINK_MODE_DEBUG_SWD
:
825 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
826 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
827 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
830 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
832 case STLINK_MODE_DEBUG_SWIM
:
833 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
835 /* no answer for this function... */
838 case STLINK_MODE_DFU
:
839 case STLINK_MODE_MASS
:
844 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
848 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
851 struct stlink_usb_handle_s
*h
= handle
;
853 assert(handle
!= NULL
);
855 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
858 case STLINK_MODE_DEBUG_JTAG
:
859 case STLINK_MODE_DEBUG_SWD
:
860 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
861 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
863 case STLINK_MODE_DEBUG_SWIM
:
864 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
865 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
867 case STLINK_MODE_DFU
:
868 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
869 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
871 case STLINK_MODE_MASS
:
876 res
= stlink_usb_xfer(handle
, 0, 0);
884 static int stlink_usb_assert_srst(void *handle
, int srst
);
886 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
889 case HL_TRANSPORT_SWD
:
890 return STLINK_MODE_DEBUG_SWD
;
891 case HL_TRANSPORT_JTAG
:
892 return STLINK_MODE_DEBUG_JTAG
;
893 case HL_TRANSPORT_SWIM
:
894 return STLINK_MODE_DEBUG_SWIM
;
896 return STLINK_MODE_UNKNOWN
;
901 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
905 enum stlink_mode emode
;
906 struct stlink_usb_handle_s
*h
= handle
;
908 assert(handle
!= NULL
);
910 res
= stlink_usb_current_mode(handle
, &mode
);
915 LOG_DEBUG("MODE: 0x%02X", mode
);
917 /* try to exit current mode */
919 case STLINK_DEV_DFU_MODE
:
920 emode
= STLINK_MODE_DFU
;
922 case STLINK_DEV_DEBUG_MODE
:
923 emode
= STLINK_MODE_DEBUG_SWD
;
925 case STLINK_DEV_SWIM_MODE
:
926 emode
= STLINK_MODE_DEBUG_SWIM
;
928 case STLINK_DEV_BOOTLOADER_MODE
:
929 case STLINK_DEV_MASS_MODE
:
931 emode
= STLINK_MODE_UNKNOWN
;
935 if (emode
!= STLINK_MODE_UNKNOWN
) {
936 res
= stlink_usb_mode_leave(handle
, emode
);
942 res
= stlink_usb_current_mode(handle
, &mode
);
947 /* we check the target voltage here as an aid to debugging connection problems.
948 * the stlink requires the target Vdd to be connected for reliable debugging.
949 * this cmd is supported in all modes except DFU
951 if (mode
!= STLINK_DEV_DFU_MODE
) {
953 float target_voltage
;
955 /* check target voltage (if supported) */
956 res
= stlink_usb_check_voltage(h
, &target_voltage
);
958 if (res
!= ERROR_OK
) {
959 if (res
!= ERROR_COMMAND_NOTFOUND
)
960 LOG_ERROR("voltage check failed");
961 /* attempt to continue as it is not a catastrophic failure */
963 /* check for a sensible target voltage, operating range is 1.65-5.5v
964 * according to datasheet */
965 if (target_voltage
< 1.5)
966 LOG_ERROR("target voltage may be too low for reliable debugging");
970 LOG_DEBUG("MODE: 0x%02X", mode
);
972 /* set selected mode */
973 emode
= stlink_get_mode(h
->transport
);
975 if (emode
== STLINK_MODE_UNKNOWN
) {
976 LOG_ERROR("selected mode (transport) not supported");
980 /* preliminary SRST assert:
981 * We want SRST is asserted before activating debug signals (mode_enter).
982 * As the required mode has not been set, the adapter may not know what pin to use.
983 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
984 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
985 * after power on, SWIM_RST stays unchanged */
986 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
987 stlink_usb_assert_srst(handle
, 0);
988 /* do not check the return status here, we will
989 proceed and enter the desired mode below
990 and try asserting srst again. */
992 res
= stlink_usb_mode_enter(handle
, emode
);
996 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
997 if (connect_under_reset
) {
998 res
= stlink_usb_assert_srst(handle
, 0);
1003 res
= stlink_usb_current_mode(handle
, &mode
);
1005 if (res
!= ERROR_OK
)
1008 LOG_DEBUG("MODE: 0x%02X", mode
);
1013 /* request status from last swim request */
1014 static int stlink_swim_status(void *handle
)
1016 struct stlink_usb_handle_s
*h
= handle
;
1019 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1020 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1021 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1022 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1023 if (res
!= ERROR_OK
)
1028 the purpose of this function is unknown...
1029 capabilites? anyway for swim v6 it returns
1032 __attribute__((unused
))
1033 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1035 struct stlink_usb_handle_s
*h
= handle
;
1038 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1039 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1040 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1041 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1042 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
1043 if (res
!= ERROR_OK
)
1045 memcpy(cap
, h
->databuf
, 8);
1049 /* debug dongle assert/deassert sreset line */
1050 static int stlink_swim_assert_reset(void *handle
, int reset
)
1052 struct stlink_usb_handle_s
*h
= handle
;
1055 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1056 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1058 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1060 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1061 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1062 if (res
!= ERROR_OK
)
1069 1.3ms low then 750Hz then 1.5kHz
1071 static int stlink_swim_enter(void *handle
)
1073 struct stlink_usb_handle_s
*h
= handle
;
1076 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1077 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1078 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1079 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1080 if (res
!= ERROR_OK
)
1085 /* switch high/low speed swim */
1086 static int stlink_swim_speed(void *handle
, int speed
)
1088 struct stlink_usb_handle_s
*h
= handle
;
1091 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1092 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1093 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1095 h
->cmdbuf
[h
->cmdidx
++] = 1;
1097 h
->cmdbuf
[h
->cmdidx
++] = 0;
1098 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1099 if (res
!= ERROR_OK
)
1105 initiate srst from swim.
1106 nrst is pulled low for 50us.
1108 static int stlink_swim_generate_rst(void *handle
)
1110 struct stlink_usb_handle_s
*h
= handle
;
1113 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1114 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1115 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1116 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1117 if (res
!= ERROR_OK
)
1123 send resyncronize sequence
1124 swim is pulled low for 16us
1125 reply is 64 clks low
1127 static int stlink_swim_resync(void *handle
)
1129 struct stlink_usb_handle_s
*h
= handle
;
1132 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1133 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1134 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1135 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1136 if (res
!= ERROR_OK
)
1141 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1143 struct stlink_usb_handle_s
*h
= handle
;
1146 unsigned int datalen
= 0;
1147 int cmdsize
= STLINK_CMD_SIZE_V2
;
1149 if (len
> STLINK_DATA_SIZE
)
1152 if (h
->version
.stlink
== 1)
1153 cmdsize
= STLINK_SG_SIZE
;
1155 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1156 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1157 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1158 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1160 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1162 for (i
= 0; i
< len
; i
++) {
1163 if (h
->cmdidx
== cmdsize
)
1164 h
->databuf
[datalen
++] = *(data
++);
1166 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1168 if (h
->version
.stlink
== 1)
1169 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1171 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1172 if (res
!= ERROR_OK
)
1177 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1179 struct stlink_usb_handle_s
*h
= handle
;
1182 if (len
> STLINK_DATA_SIZE
)
1185 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1186 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1187 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1188 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1190 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1192 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1193 if (res
!= ERROR_OK
)
1196 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1197 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1198 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1199 res
= stlink_usb_xfer(handle
, data
, len
);
1200 if (res
!= ERROR_OK
)
1207 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1210 struct stlink_usb_handle_s
*h
= handle
;
1212 assert(handle
!= NULL
);
1214 /* there is no swim read core id cmd */
1215 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1220 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1222 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1223 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1225 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1227 if (res
!= ERROR_OK
)
1230 *idcode
= le_to_h_u32(h
->databuf
);
1232 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1237 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1239 struct stlink_usb_handle_s
*h
= handle
;
1242 assert(handle
!= NULL
);
1244 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1246 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1247 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1248 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1251 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1252 if (res
!= ERROR_OK
)
1255 *val
= le_to_h_u32(h
->databuf
+ 4);
1259 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1261 struct stlink_usb_handle_s
*h
= handle
;
1263 assert(handle
!= NULL
);
1265 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1267 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1268 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1269 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1271 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1272 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1274 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1277 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1281 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1283 struct stlink_usb_handle_s
*h
= handle
;
1285 assert(handle
!= NULL
);
1287 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1290 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1292 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1293 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1295 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1296 if (res
!= ERROR_OK
)
1299 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1300 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1303 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1304 if (res
!= ERROR_OK
)
1313 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1318 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1319 if (result
!= ERROR_OK
)
1320 return TARGET_UNKNOWN
;
1322 if (status
& S_HALT
)
1323 return TARGET_HALTED
;
1324 else if (status
& S_RESET_ST
)
1325 return TARGET_RESET
;
1327 return TARGET_RUNNING
;
1331 static enum target_state
stlink_usb_state(void *handle
)
1334 struct stlink_usb_handle_s
*h
= handle
;
1336 assert(handle
!= NULL
);
1338 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1339 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1340 if (res
!= ERROR_OK
)
1341 return TARGET_UNKNOWN
;
1343 res
= stlink_swim_resync(handle
);
1344 if (res
!= ERROR_OK
)
1345 return TARGET_UNKNOWN
;
1350 if (h
->reconnect_pending
) {
1351 LOG_INFO("Previous state query failed, trying to reconnect");
1352 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1354 if (res
!= ERROR_OK
)
1355 return TARGET_UNKNOWN
;
1357 h
->reconnect_pending
= false;
1360 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1361 res
= stlink_usb_v2_get_status(handle
);
1362 if (res
== TARGET_UNKNOWN
)
1363 h
->reconnect_pending
= true;
1367 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1369 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1370 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1372 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1374 if (res
!= ERROR_OK
)
1375 return TARGET_UNKNOWN
;
1377 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1378 return TARGET_RUNNING
;
1379 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1380 return TARGET_HALTED
;
1382 h
->reconnect_pending
= true;
1384 return TARGET_UNKNOWN
;
1387 static int stlink_usb_assert_srst(void *handle
, int srst
)
1389 struct stlink_usb_handle_s
*h
= handle
;
1391 assert(handle
!= NULL
);
1393 if (h
->transport
== HL_TRANSPORT_SWIM
)
1394 return stlink_swim_assert_reset(handle
, srst
);
1396 if (h
->version
.stlink
== 1)
1397 return ERROR_COMMAND_NOTFOUND
;
1399 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1401 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1402 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1403 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1405 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1409 static void stlink_usb_trace_disable(void *handle
)
1412 struct stlink_usb_handle_s
*h
= handle
;
1414 assert(handle
!= NULL
);
1416 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1418 LOG_DEBUG("Tracing: disable");
1420 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1421 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1422 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1423 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1425 if (res
== ERROR_OK
)
1426 h
->trace
.enabled
= false;
1431 static int stlink_usb_trace_enable(void *handle
)
1434 struct stlink_usb_handle_s
*h
= handle
;
1436 assert(handle
!= NULL
);
1438 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1439 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1441 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1442 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1443 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1445 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1448 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1450 if (res
== ERROR_OK
) {
1451 h
->trace
.enabled
= true;
1452 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1455 LOG_ERROR("Tracing is not supported by this version.");
1463 static int stlink_usb_reset(void *handle
)
1465 struct stlink_usb_handle_s
*h
= handle
;
1468 assert(handle
!= NULL
);
1470 if (h
->transport
== HL_TRANSPORT_SWIM
)
1471 return stlink_swim_generate_rst(handle
);
1473 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1475 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1477 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1478 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1480 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1482 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1483 if (retval
!= ERROR_OK
)
1486 if (h
->trace
.enabled
) {
1487 stlink_usb_trace_disable(h
);
1488 return stlink_usb_trace_enable(h
);
1495 static int stlink_usb_run(void *handle
)
1498 struct stlink_usb_handle_s
*h
= handle
;
1500 assert(handle
!= NULL
);
1502 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1503 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1508 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1510 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1511 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1513 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1517 static int stlink_usb_halt(void *handle
)
1520 struct stlink_usb_handle_s
*h
= handle
;
1522 assert(handle
!= NULL
);
1524 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1525 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1530 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1532 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1533 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1535 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1539 static int stlink_usb_step(void *handle
)
1541 struct stlink_usb_handle_s
*h
= handle
;
1543 assert(handle
!= NULL
);
1545 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1546 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1547 * that the Cortex-M3 currently does. */
1548 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1549 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1550 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1553 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1555 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1556 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1558 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1562 static int stlink_usb_read_regs(void *handle
)
1565 struct stlink_usb_handle_s
*h
= handle
;
1567 assert(handle
!= NULL
);
1569 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1571 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1572 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1573 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1575 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1577 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1579 if (res
!= ERROR_OK
)
1586 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1589 struct stlink_usb_handle_s
*h
= handle
;
1591 assert(handle
!= NULL
);
1593 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1595 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1596 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1597 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1599 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1600 h
->cmdbuf
[h
->cmdidx
++] = num
;
1602 if (h
->jtag_api
== STLINK_JTAG_API_V1
) {
1603 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1604 if (res
!= ERROR_OK
)
1606 *val
= le_to_h_u32(h
->databuf
);
1609 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1610 if (res
!= ERROR_OK
)
1612 *val
= le_to_h_u32(h
->databuf
+ 4);
1618 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1620 struct stlink_usb_handle_s
*h
= handle
;
1622 assert(handle
!= NULL
);
1624 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1626 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1627 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1628 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1630 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1631 h
->cmdbuf
[h
->cmdidx
++] = num
;
1632 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1635 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1638 static int stlink_usb_get_rw_status(void *handle
)
1641 struct stlink_usb_handle_s
*h
= handle
;
1643 assert(handle
!= NULL
);
1645 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1648 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1650 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1651 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1653 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1655 if (res
!= ERROR_OK
)
1658 return stlink_usb_error_check(h
);
1662 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1666 uint16_t read_len
= len
;
1667 struct stlink_usb_handle_s
*h
= handle
;
1669 assert(handle
!= NULL
);
1671 /* max 8bit read/write is 64bytes */
1672 if (len
> STLINK_MAX_RW8
) {
1673 LOG_DEBUG("max buffer length exceeded");
1677 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1679 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1680 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1681 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1683 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1686 /* we need to fix read length for single bytes */
1690 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1692 if (res
!= ERROR_OK
)
1695 memcpy(buffer
, h
->databuf
, len
);
1697 return stlink_usb_get_rw_status(handle
);
1701 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1702 const uint8_t *buffer
)
1705 struct stlink_usb_handle_s
*h
= handle
;
1707 assert(handle
!= NULL
);
1709 /* max 8bit read/write is 64bytes */
1710 if (len
> STLINK_MAX_RW8
) {
1711 LOG_DEBUG("max buffer length exceeded");
1715 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1717 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1718 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1719 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1721 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1724 res
= stlink_usb_xfer(handle
, buffer
, len
);
1726 if (res
!= ERROR_OK
)
1729 return stlink_usb_get_rw_status(handle
);
1733 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1737 struct stlink_usb_handle_s
*h
= handle
;
1739 assert(handle
!= NULL
);
1741 /* only supported by stlink/v2 and for firmware >= 26 */
1742 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1743 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1744 return ERROR_COMMAND_NOTFOUND
;
1746 /* data must be a multiple of 2 and half-word aligned */
1747 if (len
% 2 || addr
% 2) {
1748 LOG_DEBUG("Invalid data alignment");
1749 return ERROR_TARGET_UNALIGNED_ACCESS
;
1752 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1754 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1755 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
1756 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1758 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1761 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1763 if (res
!= ERROR_OK
)
1766 memcpy(buffer
, h
->databuf
, len
);
1768 return stlink_usb_get_rw_status(handle
);
1772 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1773 const uint8_t *buffer
)
1776 struct stlink_usb_handle_s
*h
= handle
;
1778 assert(handle
!= NULL
);
1780 /* only supported by stlink/v2 and for firmware >= 26 */
1781 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1782 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1783 return ERROR_COMMAND_NOTFOUND
;
1785 /* data must be a multiple of 2 and half-word aligned */
1786 if (len
% 2 || addr
% 2) {
1787 LOG_DEBUG("Invalid data alignment");
1788 return ERROR_TARGET_UNALIGNED_ACCESS
;
1791 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1793 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1794 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
1795 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1797 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1800 res
= stlink_usb_xfer(handle
, buffer
, len
);
1802 if (res
!= ERROR_OK
)
1805 return stlink_usb_get_rw_status(handle
);
1809 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1813 struct stlink_usb_handle_s
*h
= handle
;
1815 assert(handle
!= NULL
);
1817 /* data must be a multiple of 4 and word aligned */
1818 if (len
% 4 || addr
% 4) {
1819 LOG_DEBUG("Invalid data alignment");
1820 return ERROR_TARGET_UNALIGNED_ACCESS
;
1823 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1825 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1826 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1827 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1829 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1832 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1834 if (res
!= ERROR_OK
)
1837 memcpy(buffer
, h
->databuf
, len
);
1839 return stlink_usb_get_rw_status(handle
);
1843 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1844 const uint8_t *buffer
)
1847 struct stlink_usb_handle_s
*h
= handle
;
1849 assert(handle
!= NULL
);
1851 /* data must be a multiple of 4 and word aligned */
1852 if (len
% 4 || addr
% 4) {
1853 LOG_DEBUG("Invalid data alignment");
1854 return ERROR_TARGET_UNALIGNED_ACCESS
;
1857 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1859 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1860 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1861 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1863 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1866 res
= stlink_usb_xfer(handle
, buffer
, len
);
1868 if (res
!= ERROR_OK
)
1871 return stlink_usb_get_rw_status(handle
);
1874 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1876 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1877 if (max_tar_block
== 0)
1879 return max_tar_block
;
1882 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1883 uint32_t count
, uint8_t *buffer
)
1885 int retval
= ERROR_OK
;
1886 uint32_t bytes_remaining
;
1888 struct stlink_usb_handle_s
*h
= handle
;
1890 /* calculate byte count */
1893 /* switch to 8 bit if stlink does not support 16 bit memory read */
1894 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1895 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1900 bytes_remaining
= (size
!= 1) ? \
1901 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1903 if (count
< bytes_remaining
)
1904 bytes_remaining
= count
;
1906 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1907 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
1908 if (retval
!= ERROR_OK
)
1912 * all stlink support 8/32bit memory read/writes and only from
1913 * stlink V2J26 there is support for 16 bit memory read/write.
1914 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1919 /* When in jtag mode the stlink uses the auto-increment functionality.
1920 * However it expects us to pass the data correctly, this includes
1921 * alignment and any page boundaries. We already do this as part of the
1922 * adi_v5 implementation, but the stlink is a hla adapter and so this
1923 * needs implementing manually.
1924 * currently this only affects jtag mode, according to ST they do single
1925 * access in SWD mode - but this may change and so we do it for both modes */
1927 /* we first need to check for any unaligned bytes */
1928 if (addr
& (size
- 1)) {
1930 uint32_t head_bytes
= size
- (addr
& (size
- 1));
1931 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
1932 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1933 usleep((1<<retries
++) * 1000);
1936 if (retval
!= ERROR_OK
)
1938 buffer
+= head_bytes
;
1940 count
-= head_bytes
;
1941 bytes_remaining
-= head_bytes
;
1944 if (bytes_remaining
& (size
- 1))
1945 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1947 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
1949 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
1951 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
1953 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1954 usleep((1<<retries
++) * 1000);
1957 if (retval
!= ERROR_OK
)
1960 buffer
+= bytes_remaining
;
1961 addr
+= bytes_remaining
;
1962 count
-= bytes_remaining
;
1968 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
1969 uint32_t count
, const uint8_t *buffer
)
1971 int retval
= ERROR_OK
;
1972 uint32_t bytes_remaining
;
1974 struct stlink_usb_handle_s
*h
= handle
;
1976 /* calculate byte count */
1979 /* switch to 8 bit if stlink does not support 16 bit memory read */
1980 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1981 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1986 bytes_remaining
= (size
!= 1) ? \
1987 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1989 if (count
< bytes_remaining
)
1990 bytes_remaining
= count
;
1992 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1993 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
1994 if (retval
!= ERROR_OK
)
1998 * all stlink support 8/32bit memory read/writes and only from
1999 * stlink V2J26 there is support for 16 bit memory read/write.
2000 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2005 /* When in jtag mode the stlink uses the auto-increment functionality.
2006 * However it expects us to pass the data correctly, this includes
2007 * alignment and any page boundaries. We already do this as part of the
2008 * adi_v5 implementation, but the stlink is a hla adapter and so this
2009 * needs implementing manually.
2010 * currently this only affects jtag mode, according to ST they do single
2011 * access in SWD mode - but this may change and so we do it for both modes */
2013 /* we first need to check for any unaligned bytes */
2014 if (addr
& (size
- 1)) {
2016 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2017 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2018 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2019 usleep((1<<retries
++) * 1000);
2022 if (retval
!= ERROR_OK
)
2024 buffer
+= head_bytes
;
2026 count
-= head_bytes
;
2027 bytes_remaining
-= head_bytes
;
2030 if (bytes_remaining
& (size
- 1))
2031 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2033 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2035 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2038 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2039 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2040 usleep((1<<retries
++) * 1000);
2043 if (retval
!= ERROR_OK
)
2046 buffer
+= bytes_remaining
;
2047 addr
+= bytes_remaining
;
2048 count
-= bytes_remaining
;
2055 static int stlink_usb_override_target(const char *targetname
)
2057 return !strcmp(targetname
, "cortex_m");
2060 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2063 we dont care what the khz rate is
2064 we only have low and high speed...
2065 before changing speed the SWIM_CSR HS bit
2069 stlink_swim_speed(handle
, 0);
2071 stlink_swim_speed(handle
, 1);
2075 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2078 int speed_index
= -1;
2079 int speed_diff
= INT_MAX
;
2082 for (i
= 0; i
< map_size
; i
++) {
2083 if (khz
== map
[i
].speed
) {
2087 int current_diff
= khz
- map
[i
].speed
;
2088 /* get abs value for comparison */
2089 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2090 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2091 speed_diff
= current_diff
;
2097 if (speed_index
== -1) {
2098 /* this will only be here if we cannot match the slow speed.
2099 * use the slowest speed we support.*/
2100 speed_index
= map_size
- 1;
2102 } else if (i
== map_size
)
2105 if (!match
&& query
) {
2106 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2107 khz
, map
[speed_index
].speed
);
2113 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2116 struct stlink_usb_handle_s
*h
= handle
;
2118 /* only supported by stlink/v2 and for firmware >= 22 */
2119 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 22)
2122 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2123 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2126 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2127 if (result
!= ERROR_OK
) {
2128 LOG_ERROR("Unable to set adapter speed");
2133 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2136 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2139 struct stlink_usb_handle_s
*h
= handle
;
2141 /* only supported by stlink/v2 and for firmware >= 24 */
2142 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
2145 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2146 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2149 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2150 if (result
!= ERROR_OK
) {
2151 LOG_ERROR("Unable to set adapter speed");
2156 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2159 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2163 LOG_DEBUG("Supported clock speeds are:");
2164 for (i
= 0; i
< map_size
; i
++)
2165 LOG_DEBUG("%d kHz", map
[i
].speed
);
2168 static int stlink_speed(void *handle
, int khz
, bool query
)
2170 struct stlink_usb_handle_s
*h
= handle
;
2175 if (h
->transport
== HL_TRANSPORT_SWIM
)
2176 return stlink_speed_swim(handle
, khz
, query
);
2177 else if (h
->transport
== HL_TRANSPORT_SWD
)
2178 return stlink_speed_swd(handle
, khz
, query
);
2179 else if (h
->transport
== HL_TRANSPORT_JTAG
)
2180 return stlink_speed_jtag(handle
, khz
, query
);
2186 static int stlink_usb_close(void *handle
)
2190 enum stlink_mode emode
;
2191 struct stlink_usb_handle_s
*h
= handle
;
2194 res
= stlink_usb_current_mode(handle
, &mode
);
2197 /* do not exit if return code != ERROR_OK,
2198 it prevents us from closing jtag_libusb */
2200 if (res
== ERROR_OK
) {
2201 /* try to exit current mode */
2203 case STLINK_DEV_DFU_MODE
:
2204 emode
= STLINK_MODE_DFU
;
2206 case STLINK_DEV_DEBUG_MODE
:
2207 emode
= STLINK_MODE_DEBUG_SWD
;
2209 case STLINK_DEV_SWIM_MODE
:
2210 emode
= STLINK_MODE_DEBUG_SWIM
;
2212 case STLINK_DEV_BOOTLOADER_MODE
:
2213 case STLINK_DEV_MASS_MODE
:
2215 emode
= STLINK_MODE_UNKNOWN
;
2219 if (emode
!= STLINK_MODE_UNKNOWN
)
2220 stlink_usb_mode_leave(handle
, emode
);
2221 /* do not check return code, it prevent
2222 us from closing jtag_libusb */
2226 jtag_libusb_close(h
->fd
);
2234 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2236 int err
, retry_count
= 1;
2237 struct stlink_usb_handle_s
*h
;
2238 enum stlink_jtag_api_version api
;
2240 LOG_DEBUG("stlink_usb_open");
2242 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2245 LOG_DEBUG("malloc failed");
2249 h
->transport
= param
->transport
;
2251 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2252 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2253 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2254 param
->serial
? param
->serial
: "");
2258 On certain host USB configurations(e.g. MacBook Air)
2259 STLINKv2 dongle seems to have its FW in a funky state if,
2260 after plugging it in, you try to use openocd with it more
2261 then once (by launching and closing openocd). In cases like
2262 that initial attempt to read the FW info via
2263 stlink_usb_version will fail and the device has to be reset
2264 in order to become operational.
2267 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2268 LOG_ERROR("open failed");
2272 jtag_libusb_set_configuration(h
->fd
, 0);
2274 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2275 LOG_DEBUG("claim interface failed");
2279 /* RX EP is common for all versions */
2280 h
->rx_ep
= STLINK_RX_EP
;
2283 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2284 LOG_DEBUG("libusb_get_pid failed");
2288 /* wrap version for first read */
2291 h
->version
.stlink
= 1;
2292 h
->tx_ep
= STLINK_TX_EP
;
2294 case STLINK_V2_1_PID
:
2295 case STLINK_V2_1_NO_MSD_PID
:
2296 h
->version
.stlink
= 2;
2297 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2298 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2301 /* fall through - we assume V2 to be the default version*/
2303 h
->version
.stlink
= 2;
2304 h
->tx_ep
= STLINK_TX_EP
;
2305 h
->trace_ep
= STLINK_TRACE_EP
;
2309 /* get the device version */
2310 err
= stlink_usb_version(h
);
2312 if (err
== ERROR_OK
) {
2314 } else if (h
->version
.stlink
== 1 ||
2316 LOG_ERROR("read version failed");
2319 err
= jtag_libusb_release_interface(h
->fd
, 0);
2320 if (err
!= ERROR_OK
) {
2321 LOG_ERROR("release interface failed");
2325 err
= jtag_libusb_reset_device(h
->fd
);
2326 if (err
!= ERROR_OK
) {
2327 LOG_ERROR("reset device failed");
2331 jtag_libusb_close(h
->fd
);
2333 Give the device one second to settle down and
2336 usleep(1 * 1000 * 1000);
2341 /* check if mode is supported */
2344 switch (h
->transport
) {
2345 case HL_TRANSPORT_SWD
:
2346 if (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
)
2349 case HL_TRANSPORT_JTAG
:
2350 if (h
->version
.jtag
== 0)
2353 case HL_TRANSPORT_SWIM
:
2354 if (h
->version
.swim
== 0)
2362 if (err
!= ERROR_OK
) {
2363 LOG_ERROR("mode (transport) not supported by device");
2367 api
= h
->version
.jtag_api_max
;
2369 LOG_INFO("using stlink api v%d", api
);
2371 /* set the used jtag api, this will default to the newest supported version */
2374 /* initialize the debug hardware */
2375 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2377 if (err
!= ERROR_OK
) {
2378 LOG_ERROR("init mode failed (unable to connect to the target)");
2382 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2383 err
= stlink_swim_enter(h
);
2384 if (err
!= ERROR_OK
) {
2385 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2389 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2393 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2394 /* jtag clock speed only supported by stlink/v2 and for firmware >= 24 */
2395 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 24) {
2396 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2397 stlink_speed(h
, param
->initial_interface_speed
, false);
2399 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2400 /* clock speed only supported by stlink/v2 and for firmware >= 22 */
2401 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 22) {
2402 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2403 stlink_speed(h
, param
->initial_interface_speed
, false);
2407 /* get cpuid, so we can determine the max page size
2408 * start with a safe default */
2409 h
->max_mem_packet
= (1 << 10);
2412 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2413 if (err
== ERROR_OK
) {
2414 uint32_t cpuid
= le_to_h_u32(buffer
);
2415 int i
= (cpuid
>> 4) & 0xf;
2416 if (i
== 4 || i
== 3) {
2417 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2418 h
->max_mem_packet
= (1 << 12);
2422 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2429 stlink_usb_close(h
);
2434 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2435 uint32_t port_size
, unsigned int *trace_freq
)
2437 struct stlink_usb_handle_s
*h
= handle
;
2439 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2440 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2441 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2446 stlink_usb_trace_disable(h
);
2450 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2451 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2452 STLINK_TRACE_MAX_HZ
);
2456 stlink_usb_trace_disable(h
);
2459 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2460 h
->trace
.source_hz
= *trace_freq
;
2462 return stlink_usb_trace_enable(h
);
2466 struct hl_layout_api_s stlink_usb_layout_api
= {
2468 .open
= stlink_usb_open
,
2470 .close
= stlink_usb_close
,
2472 .idcode
= stlink_usb_idcode
,
2474 .state
= stlink_usb_state
,
2476 .reset
= stlink_usb_reset
,
2478 .assert_srst
= stlink_usb_assert_srst
,
2480 .run
= stlink_usb_run
,
2482 .halt
= stlink_usb_halt
,
2484 .step
= stlink_usb_step
,
2486 .read_regs
= stlink_usb_read_regs
,
2488 .read_reg
= stlink_usb_read_reg
,
2490 .write_reg
= stlink_usb_write_reg
,
2492 .read_mem
= stlink_usb_read_mem
,
2494 .write_mem
= stlink_usb_write_mem
,
2496 .write_debug_reg
= stlink_usb_write_debug_reg
,
2498 .override_target
= stlink_usb_override_target
,
2500 .speed
= stlink_speed
,
2502 .config_trace
= stlink_config_trace
,
2504 .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)