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
262 #define STLINK_TRACE_MIN_VERSION 13
266 STLINK_MODE_UNKNOWN
= 0,
269 STLINK_MODE_DEBUG_JTAG
,
270 STLINK_MODE_DEBUG_SWD
,
271 STLINK_MODE_DEBUG_SWIM
274 #define REQUEST_SENSE 0x03
275 #define REQUEST_SENSE_LENGTH 18
278 * Map the relevant features, quirks and workaround for specific firmware
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
.stlink
>= 2);
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
;
675 h
->version
.flags
= flags
;
677 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
680 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
688 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
690 struct stlink_usb_handle_s
*h
= handle
;
691 uint32_t adc_results
[2];
693 /* only supported by stlink/v2 and for firmware >= 13 */
694 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 13)
695 return ERROR_COMMAND_NOTFOUND
;
697 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
699 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
701 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
703 if (result
!= ERROR_OK
)
707 adc_results
[0] = le_to_h_u32(h
->databuf
);
708 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
713 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
715 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
720 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
722 struct stlink_usb_handle_s
*h
= handle
;
724 assert(handle
!= NULL
);
726 /* only supported by stlink/v2 and for firmware >= 22 */
727 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 22)
728 return ERROR_COMMAND_NOTFOUND
;
730 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
732 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
733 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
734 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
737 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
739 if (result
!= ERROR_OK
)
745 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
747 struct stlink_usb_handle_s
*h
= handle
;
749 assert(handle
!= NULL
);
751 /* only supported by stlink/v2 and for firmware >= 24 */
752 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
753 return ERROR_COMMAND_NOTFOUND
;
755 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
757 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
758 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
759 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
762 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
764 if (result
!= ERROR_OK
)
771 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
774 struct stlink_usb_handle_s
*h
= handle
;
776 assert(handle
!= NULL
);
778 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
780 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
782 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
787 *mode
= h
->databuf
[0];
793 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
796 struct stlink_usb_handle_s
*h
= handle
;
798 assert(handle
!= NULL
);
800 /* on api V2 we are able the read the latest command
802 * TODO: we need the test on api V1 too
804 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
807 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
810 case STLINK_MODE_DEBUG_JTAG
:
811 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
812 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
813 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
815 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
816 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
818 case STLINK_MODE_DEBUG_SWD
:
819 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
820 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
821 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
823 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
824 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
826 case STLINK_MODE_DEBUG_SWIM
:
827 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
828 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
829 /* no answer for this function... */
832 case STLINK_MODE_DFU
:
833 case STLINK_MODE_MASS
:
838 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
842 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
845 struct stlink_usb_handle_s
*h
= handle
;
847 assert(handle
!= NULL
);
849 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
852 case STLINK_MODE_DEBUG_JTAG
:
853 case STLINK_MODE_DEBUG_SWD
:
854 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
855 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
857 case STLINK_MODE_DEBUG_SWIM
:
858 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
859 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
861 case STLINK_MODE_DFU
:
862 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
863 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
865 case STLINK_MODE_MASS
:
870 res
= stlink_usb_xfer(handle
, 0, 0);
878 static int stlink_usb_assert_srst(void *handle
, int srst
);
880 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
883 case HL_TRANSPORT_SWD
:
884 return STLINK_MODE_DEBUG_SWD
;
885 case HL_TRANSPORT_JTAG
:
886 return STLINK_MODE_DEBUG_JTAG
;
887 case HL_TRANSPORT_SWIM
:
888 return STLINK_MODE_DEBUG_SWIM
;
890 return STLINK_MODE_UNKNOWN
;
895 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
899 enum stlink_mode emode
;
900 struct stlink_usb_handle_s
*h
= handle
;
902 assert(handle
!= NULL
);
904 res
= stlink_usb_current_mode(handle
, &mode
);
909 LOG_DEBUG("MODE: 0x%02X", mode
);
911 /* try to exit current mode */
913 case STLINK_DEV_DFU_MODE
:
914 emode
= STLINK_MODE_DFU
;
916 case STLINK_DEV_DEBUG_MODE
:
917 emode
= STLINK_MODE_DEBUG_SWD
;
919 case STLINK_DEV_SWIM_MODE
:
920 emode
= STLINK_MODE_DEBUG_SWIM
;
922 case STLINK_DEV_BOOTLOADER_MODE
:
923 case STLINK_DEV_MASS_MODE
:
925 emode
= STLINK_MODE_UNKNOWN
;
929 if (emode
!= STLINK_MODE_UNKNOWN
) {
930 res
= stlink_usb_mode_leave(handle
, emode
);
936 res
= stlink_usb_current_mode(handle
, &mode
);
941 /* we check the target voltage here as an aid to debugging connection problems.
942 * the stlink requires the target Vdd to be connected for reliable debugging.
943 * this cmd is supported in all modes except DFU
945 if (mode
!= STLINK_DEV_DFU_MODE
) {
947 float target_voltage
;
949 /* check target voltage (if supported) */
950 res
= stlink_usb_check_voltage(h
, &target_voltage
);
952 if (res
!= ERROR_OK
) {
953 if (res
!= ERROR_COMMAND_NOTFOUND
)
954 LOG_ERROR("voltage check failed");
955 /* attempt to continue as it is not a catastrophic failure */
957 /* check for a sensible target voltage, operating range is 1.65-5.5v
958 * according to datasheet */
959 if (target_voltage
< 1.5)
960 LOG_ERROR("target voltage may be too low for reliable debugging");
964 LOG_DEBUG("MODE: 0x%02X", mode
);
966 /* set selected mode */
967 emode
= stlink_get_mode(h
->transport
);
969 if (emode
== STLINK_MODE_UNKNOWN
) {
970 LOG_ERROR("selected mode (transport) not supported");
974 /* preliminary SRST assert:
975 * We want SRST is asserted before activating debug signals (mode_enter).
976 * As the required mode has not been set, the adapter may not know what pin to use.
977 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
978 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
979 * after power on, SWIM_RST stays unchanged */
980 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
981 stlink_usb_assert_srst(handle
, 0);
982 /* do not check the return status here, we will
983 proceed and enter the desired mode below
984 and try asserting srst again. */
986 res
= stlink_usb_mode_enter(handle
, emode
);
990 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
991 if (connect_under_reset
) {
992 res
= stlink_usb_assert_srst(handle
, 0);
997 res
= stlink_usb_current_mode(handle
, &mode
);
1002 LOG_DEBUG("MODE: 0x%02X", mode
);
1007 /* request status from last swim request */
1008 static int stlink_swim_status(void *handle
)
1010 struct stlink_usb_handle_s
*h
= handle
;
1013 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1014 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1015 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1016 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1017 if (res
!= ERROR_OK
)
1022 the purpose of this function is unknown...
1023 capabilites? anyway for swim v6 it returns
1026 __attribute__((unused
))
1027 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1029 struct stlink_usb_handle_s
*h
= handle
;
1032 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1033 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1034 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1035 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1036 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
1037 if (res
!= ERROR_OK
)
1039 memcpy(cap
, h
->databuf
, 8);
1043 /* debug dongle assert/deassert sreset line */
1044 static int stlink_swim_assert_reset(void *handle
, int reset
)
1046 struct stlink_usb_handle_s
*h
= handle
;
1049 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1050 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1052 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1054 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1055 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1056 if (res
!= ERROR_OK
)
1063 1.3ms low then 750Hz then 1.5kHz
1065 static int stlink_swim_enter(void *handle
)
1067 struct stlink_usb_handle_s
*h
= handle
;
1070 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1071 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1072 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1073 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1074 if (res
!= ERROR_OK
)
1079 /* switch high/low speed swim */
1080 static int stlink_swim_speed(void *handle
, int speed
)
1082 struct stlink_usb_handle_s
*h
= handle
;
1085 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1086 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1087 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1089 h
->cmdbuf
[h
->cmdidx
++] = 1;
1091 h
->cmdbuf
[h
->cmdidx
++] = 0;
1092 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1093 if (res
!= ERROR_OK
)
1099 initiate srst from swim.
1100 nrst is pulled low for 50us.
1102 static int stlink_swim_generate_rst(void *handle
)
1104 struct stlink_usb_handle_s
*h
= handle
;
1107 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1108 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1109 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1110 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1111 if (res
!= ERROR_OK
)
1117 send resyncronize sequence
1118 swim is pulled low for 16us
1119 reply is 64 clks low
1121 static int stlink_swim_resync(void *handle
)
1123 struct stlink_usb_handle_s
*h
= handle
;
1126 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1127 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1128 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1129 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1130 if (res
!= ERROR_OK
)
1135 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1137 struct stlink_usb_handle_s
*h
= handle
;
1140 unsigned int datalen
= 0;
1141 int cmdsize
= STLINK_CMD_SIZE_V2
;
1143 if (len
> STLINK_DATA_SIZE
)
1146 if (h
->version
.stlink
== 1)
1147 cmdsize
= STLINK_SG_SIZE
;
1149 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1150 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1151 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1152 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1154 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1156 for (i
= 0; i
< len
; i
++) {
1157 if (h
->cmdidx
== cmdsize
)
1158 h
->databuf
[datalen
++] = *(data
++);
1160 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1162 if (h
->version
.stlink
== 1)
1163 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1165 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1166 if (res
!= ERROR_OK
)
1171 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1173 struct stlink_usb_handle_s
*h
= handle
;
1176 if (len
> STLINK_DATA_SIZE
)
1179 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1180 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1181 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1182 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1184 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1186 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1187 if (res
!= ERROR_OK
)
1190 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1191 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1192 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1193 res
= stlink_usb_xfer(handle
, data
, len
);
1194 if (res
!= ERROR_OK
)
1201 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1204 struct stlink_usb_handle_s
*h
= handle
;
1206 assert(handle
!= NULL
);
1208 /* there is no swim read core id cmd */
1209 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1214 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1216 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1217 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1219 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1221 if (res
!= ERROR_OK
)
1224 *idcode
= le_to_h_u32(h
->databuf
);
1226 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1231 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1233 struct stlink_usb_handle_s
*h
= handle
;
1236 assert(handle
!= NULL
);
1238 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1240 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1241 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1242 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1245 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1246 if (res
!= ERROR_OK
)
1249 *val
= le_to_h_u32(h
->databuf
+ 4);
1253 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1255 struct stlink_usb_handle_s
*h
= handle
;
1257 assert(handle
!= NULL
);
1259 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1261 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1262 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1263 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1265 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1266 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1268 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1271 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1275 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1277 struct stlink_usb_handle_s
*h
= handle
;
1279 assert(handle
!= NULL
);
1281 if (h
->trace
.enabled
&& h
->version
.jtag
>= STLINK_TRACE_MIN_VERSION
) {
1284 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1286 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1287 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1289 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1290 if (res
!= ERROR_OK
)
1293 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1294 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1297 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1298 if (res
!= ERROR_OK
)
1307 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1312 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1313 if (result
!= ERROR_OK
)
1314 return TARGET_UNKNOWN
;
1316 if (status
& S_HALT
)
1317 return TARGET_HALTED
;
1318 else if (status
& S_RESET_ST
)
1319 return TARGET_RESET
;
1321 return TARGET_RUNNING
;
1325 static enum target_state
stlink_usb_state(void *handle
)
1328 struct stlink_usb_handle_s
*h
= handle
;
1330 assert(handle
!= NULL
);
1332 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1333 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1334 if (res
!= ERROR_OK
)
1335 return TARGET_UNKNOWN
;
1337 res
= stlink_swim_resync(handle
);
1338 if (res
!= ERROR_OK
)
1339 return TARGET_UNKNOWN
;
1344 if (h
->reconnect_pending
) {
1345 LOG_INFO("Previous state query failed, trying to reconnect");
1346 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1348 if (res
!= ERROR_OK
)
1349 return TARGET_UNKNOWN
;
1351 h
->reconnect_pending
= false;
1354 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1355 res
= stlink_usb_v2_get_status(handle
);
1356 if (res
== TARGET_UNKNOWN
)
1357 h
->reconnect_pending
= true;
1361 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1363 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1364 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1366 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1368 if (res
!= ERROR_OK
)
1369 return TARGET_UNKNOWN
;
1371 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1372 return TARGET_RUNNING
;
1373 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1374 return TARGET_HALTED
;
1376 h
->reconnect_pending
= true;
1378 return TARGET_UNKNOWN
;
1381 static int stlink_usb_assert_srst(void *handle
, int srst
)
1383 struct stlink_usb_handle_s
*h
= handle
;
1385 assert(handle
!= NULL
);
1387 if (h
->transport
== HL_TRANSPORT_SWIM
)
1388 return stlink_swim_assert_reset(handle
, srst
);
1390 if (h
->version
.stlink
== 1)
1391 return ERROR_COMMAND_NOTFOUND
;
1393 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1395 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1396 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1397 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1399 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1403 static void stlink_usb_trace_disable(void *handle
)
1406 struct stlink_usb_handle_s
*h
= handle
;
1408 assert(handle
!= NULL
);
1410 assert(h
->version
.jtag
>= STLINK_TRACE_MIN_VERSION
);
1412 LOG_DEBUG("Tracing: disable");
1414 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1415 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1416 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1417 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1419 if (res
== ERROR_OK
)
1420 h
->trace
.enabled
= false;
1425 static int stlink_usb_trace_enable(void *handle
)
1428 struct stlink_usb_handle_s
*h
= handle
;
1430 assert(handle
!= NULL
);
1432 if (h
->version
.jtag
>= STLINK_TRACE_MIN_VERSION
) {
1433 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1435 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1436 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1437 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1439 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1442 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1444 if (res
== ERROR_OK
) {
1445 h
->trace
.enabled
= true;
1446 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1449 LOG_ERROR("Tracing is not supported by this version.");
1457 static int stlink_usb_reset(void *handle
)
1459 struct stlink_usb_handle_s
*h
= handle
;
1462 assert(handle
!= NULL
);
1464 if (h
->transport
== HL_TRANSPORT_SWIM
)
1465 return stlink_swim_generate_rst(handle
);
1467 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1469 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1471 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1472 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1474 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1476 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1477 if (retval
!= ERROR_OK
)
1480 if (h
->trace
.enabled
) {
1481 stlink_usb_trace_disable(h
);
1482 return stlink_usb_trace_enable(h
);
1489 static int stlink_usb_run(void *handle
)
1492 struct stlink_usb_handle_s
*h
= handle
;
1494 assert(handle
!= NULL
);
1496 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1497 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1502 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1504 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1505 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1507 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1511 static int stlink_usb_halt(void *handle
)
1514 struct stlink_usb_handle_s
*h
= handle
;
1516 assert(handle
!= NULL
);
1518 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1519 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1524 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1526 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1527 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1529 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1533 static int stlink_usb_step(void *handle
)
1535 struct stlink_usb_handle_s
*h
= handle
;
1537 assert(handle
!= NULL
);
1539 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1540 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1541 * that the Cortex-M3 currently does. */
1542 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1543 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1544 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1547 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1549 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1550 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1552 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1556 static int stlink_usb_read_regs(void *handle
)
1559 struct stlink_usb_handle_s
*h
= handle
;
1561 assert(handle
!= NULL
);
1563 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1565 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1566 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1567 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1569 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1571 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1573 if (res
!= ERROR_OK
)
1580 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1583 struct stlink_usb_handle_s
*h
= handle
;
1585 assert(handle
!= NULL
);
1587 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1589 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1590 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1591 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1593 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1594 h
->cmdbuf
[h
->cmdidx
++] = num
;
1596 if (h
->jtag_api
== STLINK_JTAG_API_V1
) {
1597 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1598 if (res
!= ERROR_OK
)
1600 *val
= le_to_h_u32(h
->databuf
);
1603 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1604 if (res
!= ERROR_OK
)
1606 *val
= le_to_h_u32(h
->databuf
+ 4);
1612 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1614 struct stlink_usb_handle_s
*h
= handle
;
1616 assert(handle
!= NULL
);
1618 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1620 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1621 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1622 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1624 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1625 h
->cmdbuf
[h
->cmdidx
++] = num
;
1626 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1629 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1632 static int stlink_usb_get_rw_status(void *handle
)
1635 struct stlink_usb_handle_s
*h
= handle
;
1637 assert(handle
!= NULL
);
1639 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1642 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1644 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1645 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1647 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1649 if (res
!= ERROR_OK
)
1652 return stlink_usb_error_check(h
);
1656 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1660 uint16_t read_len
= len
;
1661 struct stlink_usb_handle_s
*h
= handle
;
1663 assert(handle
!= NULL
);
1665 /* max 8bit read/write is 64bytes */
1666 if (len
> STLINK_MAX_RW8
) {
1667 LOG_DEBUG("max buffer length exceeded");
1671 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1673 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1674 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1675 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1677 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1680 /* we need to fix read length for single bytes */
1684 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1686 if (res
!= ERROR_OK
)
1689 memcpy(buffer
, h
->databuf
, len
);
1691 return stlink_usb_get_rw_status(handle
);
1695 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1696 const uint8_t *buffer
)
1699 struct stlink_usb_handle_s
*h
= handle
;
1701 assert(handle
!= NULL
);
1703 /* max 8bit read/write is 64bytes */
1704 if (len
> STLINK_MAX_RW8
) {
1705 LOG_DEBUG("max buffer length exceeded");
1709 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1711 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1712 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1713 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1715 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1718 res
= stlink_usb_xfer(handle
, buffer
, len
);
1720 if (res
!= ERROR_OK
)
1723 return stlink_usb_get_rw_status(handle
);
1727 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1731 struct stlink_usb_handle_s
*h
= handle
;
1733 assert(handle
!= NULL
);
1735 /* only supported by stlink/v2 and for firmware >= 26 */
1736 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1737 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1738 return ERROR_COMMAND_NOTFOUND
;
1740 /* data must be a multiple of 2 and half-word aligned */
1741 if (len
% 2 || addr
% 2) {
1742 LOG_DEBUG("Invalid data alignment");
1743 return ERROR_TARGET_UNALIGNED_ACCESS
;
1746 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1748 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1749 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
1750 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1752 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1755 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1757 if (res
!= ERROR_OK
)
1760 memcpy(buffer
, h
->databuf
, len
);
1762 return stlink_usb_get_rw_status(handle
);
1766 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1767 const uint8_t *buffer
)
1770 struct stlink_usb_handle_s
*h
= handle
;
1772 assert(handle
!= NULL
);
1774 /* only supported by stlink/v2 and for firmware >= 26 */
1775 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1776 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1777 return ERROR_COMMAND_NOTFOUND
;
1779 /* data must be a multiple of 2 and half-word aligned */
1780 if (len
% 2 || addr
% 2) {
1781 LOG_DEBUG("Invalid data alignment");
1782 return ERROR_TARGET_UNALIGNED_ACCESS
;
1785 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1787 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1788 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
1789 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1791 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1794 res
= stlink_usb_xfer(handle
, buffer
, len
);
1796 if (res
!= ERROR_OK
)
1799 return stlink_usb_get_rw_status(handle
);
1803 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1807 struct stlink_usb_handle_s
*h
= handle
;
1809 assert(handle
!= NULL
);
1811 /* data must be a multiple of 4 and word aligned */
1812 if (len
% 4 || addr
% 4) {
1813 LOG_DEBUG("Invalid data alignment");
1814 return ERROR_TARGET_UNALIGNED_ACCESS
;
1817 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1819 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1820 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1821 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1823 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1826 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1828 if (res
!= ERROR_OK
)
1831 memcpy(buffer
, h
->databuf
, len
);
1833 return stlink_usb_get_rw_status(handle
);
1837 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1838 const uint8_t *buffer
)
1841 struct stlink_usb_handle_s
*h
= handle
;
1843 assert(handle
!= NULL
);
1845 /* data must be a multiple of 4 and word aligned */
1846 if (len
% 4 || addr
% 4) {
1847 LOG_DEBUG("Invalid data alignment");
1848 return ERROR_TARGET_UNALIGNED_ACCESS
;
1851 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1853 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1854 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1855 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1857 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1860 res
= stlink_usb_xfer(handle
, buffer
, len
);
1862 if (res
!= ERROR_OK
)
1865 return stlink_usb_get_rw_status(handle
);
1868 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1870 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1871 if (max_tar_block
== 0)
1873 return max_tar_block
;
1876 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1877 uint32_t count
, uint8_t *buffer
)
1879 int retval
= ERROR_OK
;
1880 uint32_t bytes_remaining
;
1882 struct stlink_usb_handle_s
*h
= handle
;
1884 /* calculate byte count */
1887 /* switch to 8 bit if stlink does not support 16 bit memory read */
1888 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1889 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1894 bytes_remaining
= (size
!= 1) ? \
1895 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1897 if (count
< bytes_remaining
)
1898 bytes_remaining
= count
;
1900 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1901 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
1902 if (retval
!= ERROR_OK
)
1906 * all stlink support 8/32bit memory read/writes and only from
1907 * stlink V2J26 there is support for 16 bit memory read/write.
1908 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1913 /* When in jtag mode the stlink uses the auto-increment functionality.
1914 * However it expects us to pass the data correctly, this includes
1915 * alignment and any page boundaries. We already do this as part of the
1916 * adi_v5 implementation, but the stlink is a hla adapter and so this
1917 * needs implementing manually.
1918 * currently this only affects jtag mode, according to ST they do single
1919 * access in SWD mode - but this may change and so we do it for both modes */
1921 /* we first need to check for any unaligned bytes */
1922 if (addr
& (size
- 1)) {
1924 uint32_t head_bytes
= size
- (addr
& (size
- 1));
1925 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
1926 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1927 usleep((1<<retries
++) * 1000);
1930 if (retval
!= ERROR_OK
)
1932 buffer
+= head_bytes
;
1934 count
-= head_bytes
;
1935 bytes_remaining
-= head_bytes
;
1938 if (bytes_remaining
& (size
- 1))
1939 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1941 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
1943 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
1945 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
1947 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1948 usleep((1<<retries
++) * 1000);
1951 if (retval
!= ERROR_OK
)
1954 buffer
+= bytes_remaining
;
1955 addr
+= bytes_remaining
;
1956 count
-= bytes_remaining
;
1962 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
1963 uint32_t count
, const uint8_t *buffer
)
1965 int retval
= ERROR_OK
;
1966 uint32_t bytes_remaining
;
1968 struct stlink_usb_handle_s
*h
= handle
;
1970 /* calculate byte count */
1973 /* switch to 8 bit if stlink does not support 16 bit memory read */
1974 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1975 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1980 bytes_remaining
= (size
!= 1) ? \
1981 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1983 if (count
< bytes_remaining
)
1984 bytes_remaining
= count
;
1986 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1987 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
1988 if (retval
!= ERROR_OK
)
1992 * all stlink support 8/32bit memory read/writes and only from
1993 * stlink V2J26 there is support for 16 bit memory read/write.
1994 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1999 /* When in jtag mode the stlink uses the auto-increment functionality.
2000 * However it expects us to pass the data correctly, this includes
2001 * alignment and any page boundaries. We already do this as part of the
2002 * adi_v5 implementation, but the stlink is a hla adapter and so this
2003 * needs implementing manually.
2004 * currently this only affects jtag mode, according to ST they do single
2005 * access in SWD mode - but this may change and so we do it for both modes */
2007 /* we first need to check for any unaligned bytes */
2008 if (addr
& (size
- 1)) {
2010 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2011 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2012 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2013 usleep((1<<retries
++) * 1000);
2016 if (retval
!= ERROR_OK
)
2018 buffer
+= head_bytes
;
2020 count
-= head_bytes
;
2021 bytes_remaining
-= head_bytes
;
2024 if (bytes_remaining
& (size
- 1))
2025 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2027 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2029 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2032 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2033 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2034 usleep((1<<retries
++) * 1000);
2037 if (retval
!= ERROR_OK
)
2040 buffer
+= bytes_remaining
;
2041 addr
+= bytes_remaining
;
2042 count
-= bytes_remaining
;
2049 static int stlink_usb_override_target(const char *targetname
)
2051 return !strcmp(targetname
, "cortex_m");
2054 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2057 we dont care what the khz rate is
2058 we only have low and high speed...
2059 before changing speed the SWIM_CSR HS bit
2063 stlink_swim_speed(handle
, 0);
2065 stlink_swim_speed(handle
, 1);
2069 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2072 int speed_index
= -1;
2073 int speed_diff
= INT_MAX
;
2076 for (i
= 0; i
< map_size
; i
++) {
2077 if (khz
== map
[i
].speed
) {
2081 int current_diff
= khz
- map
[i
].speed
;
2082 /* get abs value for comparison */
2083 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2084 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2085 speed_diff
= current_diff
;
2091 if (speed_index
== -1) {
2092 /* this will only be here if we cannot match the slow speed.
2093 * use the slowest speed we support.*/
2094 speed_index
= map_size
- 1;
2096 } else if (i
== map_size
)
2099 if (!match
&& query
) {
2100 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2101 khz
, map
[speed_index
].speed
);
2107 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2110 struct stlink_usb_handle_s
*h
= handle
;
2112 /* only supported by stlink/v2 and for firmware >= 22 */
2113 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 22)
2116 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2117 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2120 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2121 if (result
!= ERROR_OK
) {
2122 LOG_ERROR("Unable to set adapter speed");
2127 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2130 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2133 struct stlink_usb_handle_s
*h
= handle
;
2135 /* only supported by stlink/v2 and for firmware >= 24 */
2136 if (h
->version
.stlink
== 1 || h
->version
.jtag
< 24)
2139 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2140 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2143 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2144 if (result
!= ERROR_OK
) {
2145 LOG_ERROR("Unable to set adapter speed");
2150 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2153 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2157 LOG_DEBUG("Supported clock speeds are:");
2158 for (i
= 0; i
< map_size
; i
++)
2159 LOG_DEBUG("%d kHz", map
[i
].speed
);
2162 static int stlink_speed(void *handle
, int khz
, bool query
)
2164 struct stlink_usb_handle_s
*h
= handle
;
2169 if (h
->transport
== HL_TRANSPORT_SWIM
)
2170 return stlink_speed_swim(handle
, khz
, query
);
2171 else if (h
->transport
== HL_TRANSPORT_SWD
)
2172 return stlink_speed_swd(handle
, khz
, query
);
2173 else if (h
->transport
== HL_TRANSPORT_JTAG
)
2174 return stlink_speed_jtag(handle
, khz
, query
);
2180 static int stlink_usb_close(void *handle
)
2184 enum stlink_mode emode
;
2185 struct stlink_usb_handle_s
*h
= handle
;
2188 res
= stlink_usb_current_mode(handle
, &mode
);
2191 /* do not exit if return code != ERROR_OK,
2192 it prevents us from closing jtag_libusb */
2194 if (res
== ERROR_OK
) {
2195 /* try to exit current mode */
2197 case STLINK_DEV_DFU_MODE
:
2198 emode
= STLINK_MODE_DFU
;
2200 case STLINK_DEV_DEBUG_MODE
:
2201 emode
= STLINK_MODE_DEBUG_SWD
;
2203 case STLINK_DEV_SWIM_MODE
:
2204 emode
= STLINK_MODE_DEBUG_SWIM
;
2206 case STLINK_DEV_BOOTLOADER_MODE
:
2207 case STLINK_DEV_MASS_MODE
:
2209 emode
= STLINK_MODE_UNKNOWN
;
2213 if (emode
!= STLINK_MODE_UNKNOWN
)
2214 stlink_usb_mode_leave(handle
, emode
);
2215 /* do not check return code, it prevent
2216 us from closing jtag_libusb */
2220 jtag_libusb_close(h
->fd
);
2228 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2230 int err
, retry_count
= 1;
2231 struct stlink_usb_handle_s
*h
;
2232 enum stlink_jtag_api_version api
;
2234 LOG_DEBUG("stlink_usb_open");
2236 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2239 LOG_DEBUG("malloc failed");
2243 h
->transport
= param
->transport
;
2245 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2246 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2247 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2248 param
->serial
? param
->serial
: "");
2252 On certain host USB configurations(e.g. MacBook Air)
2253 STLINKv2 dongle seems to have its FW in a funky state if,
2254 after plugging it in, you try to use openocd with it more
2255 then once (by launching and closing openocd). In cases like
2256 that initial attempt to read the FW info via
2257 stlink_usb_version will fail and the device has to be reset
2258 in order to become operational.
2261 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2262 LOG_ERROR("open failed");
2266 jtag_libusb_set_configuration(h
->fd
, 0);
2268 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2269 LOG_DEBUG("claim interface failed");
2273 /* RX EP is common for all versions */
2274 h
->rx_ep
= STLINK_RX_EP
;
2277 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2278 LOG_DEBUG("libusb_get_pid failed");
2282 /* wrap version for first read */
2285 h
->version
.stlink
= 1;
2286 h
->tx_ep
= STLINK_TX_EP
;
2287 h
->trace_ep
= STLINK_TRACE_EP
;
2289 case STLINK_V2_1_PID
:
2290 case STLINK_V2_1_NO_MSD_PID
:
2291 h
->version
.stlink
= 2;
2292 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2293 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2296 /* fall through - we assume V2 to be the default version*/
2298 h
->version
.stlink
= 2;
2299 h
->tx_ep
= STLINK_TX_EP
;
2300 h
->trace_ep
= STLINK_TRACE_EP
;
2304 /* get the device version */
2305 err
= stlink_usb_version(h
);
2307 if (err
== ERROR_OK
) {
2309 } else if (h
->version
.stlink
== 1 ||
2311 LOG_ERROR("read version failed");
2314 err
= jtag_libusb_release_interface(h
->fd
, 0);
2315 if (err
!= ERROR_OK
) {
2316 LOG_ERROR("release interface failed");
2320 err
= jtag_libusb_reset_device(h
->fd
);
2321 if (err
!= ERROR_OK
) {
2322 LOG_ERROR("reset device failed");
2326 jtag_libusb_close(h
->fd
);
2328 Give the device one second to settle down and
2331 usleep(1 * 1000 * 1000);
2336 /* check if mode is supported */
2339 switch (h
->transport
) {
2340 case HL_TRANSPORT_SWD
:
2341 if (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
)
2344 case HL_TRANSPORT_JTAG
:
2345 if (h
->version
.jtag
== 0)
2348 case HL_TRANSPORT_SWIM
:
2349 if (h
->version
.swim
== 0)
2357 if (err
!= ERROR_OK
) {
2358 LOG_ERROR("mode (transport) not supported by device");
2362 api
= h
->version
.jtag_api_max
;
2364 LOG_INFO("using stlink api v%d", api
);
2366 /* set the used jtag api, this will default to the newest supported version */
2369 /* initialize the debug hardware */
2370 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2372 if (err
!= ERROR_OK
) {
2373 LOG_ERROR("init mode failed (unable to connect to the target)");
2377 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2378 err
= stlink_swim_enter(h
);
2379 if (err
!= ERROR_OK
) {
2380 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2384 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2388 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2389 /* jtag clock speed only supported by stlink/v2 and for firmware >= 24 */
2390 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 24) {
2391 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2392 stlink_speed(h
, param
->initial_interface_speed
, false);
2394 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2395 /* clock speed only supported by stlink/v2 and for firmware >= 22 */
2396 if (h
->version
.stlink
>= 2 && h
->version
.jtag
>= 22) {
2397 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2398 stlink_speed(h
, param
->initial_interface_speed
, false);
2402 /* get cpuid, so we can determine the max page size
2403 * start with a safe default */
2404 h
->max_mem_packet
= (1 << 10);
2407 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2408 if (err
== ERROR_OK
) {
2409 uint32_t cpuid
= le_to_h_u32(buffer
);
2410 int i
= (cpuid
>> 4) & 0xf;
2411 if (i
== 4 || i
== 3) {
2412 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2413 h
->max_mem_packet
= (1 << 12);
2417 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2424 stlink_usb_close(h
);
2429 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2430 uint32_t port_size
, unsigned int *trace_freq
)
2432 struct stlink_usb_handle_s
*h
= handle
;
2434 if (enabled
&& (h
->jtag_api
< 2 ||
2435 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2436 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2441 stlink_usb_trace_disable(h
);
2445 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2446 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2447 STLINK_TRACE_MAX_HZ
);
2451 stlink_usb_trace_disable(h
);
2454 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2455 h
->trace
.source_hz
= *trace_freq
;
2457 return stlink_usb_trace_enable(h
);
2461 struct hl_layout_api_s stlink_usb_layout_api
= {
2463 .open
= stlink_usb_open
,
2465 .close
= stlink_usb_close
,
2467 .idcode
= stlink_usb_idcode
,
2469 .state
= stlink_usb_state
,
2471 .reset
= stlink_usb_reset
,
2473 .assert_srst
= stlink_usb_assert_srst
,
2475 .run
= stlink_usb_run
,
2477 .halt
= stlink_usb_halt
,
2479 .step
= stlink_usb_step
,
2481 .read_regs
= stlink_usb_read_regs
,
2483 .read_reg
= stlink_usb_read_reg
,
2485 .write_reg
= stlink_usb_write_reg
,
2487 .read_mem
= stlink_usb_read_mem
,
2489 .write_mem
= stlink_usb_write_mem
,
2491 .write_debug_reg
= stlink_usb_write_debug_reg
,
2493 .override_target
= stlink_usb_override_target
,
2495 .speed
= stlink_speed
,
2497 .config_trace
= stlink_config_trace
,
2499 .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)