1 /***************************************************************************
2 * SWIM contributions by Ake Rehnman *
3 * Copyright (C) 2017 Ake Rehnman *
4 * ake.rehnman(at)gmail.com *
6 * Copyright (C) 2011-2012 by Mathias Kuester *
7 * Mathias Kuester <kesmtp@freenet.de> *
9 * Copyright (C) 2012 by Spencer Oliver *
10 * spen@spen-soft.co.uk *
12 * This code is based on https://github.com/texane/stlink *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
26 ***************************************************************************/
32 /* project specific includes */
33 #include <helper/binarybuffer.h>
34 #include <jtag/interface.h>
35 #include <jtag/hla/hla_layout.h>
36 #include <jtag/hla/hla_transport.h>
37 #include <jtag/hla/hla_interface.h>
38 #include <target/target.h>
40 #include <target/cortex_m.h>
42 #include "libusb_common.h"
44 #define ENDPOINT_IN 0x80
45 #define ENDPOINT_OUT 0x00
47 #define STLINK_WRITE_TIMEOUT 1000
48 #define STLINK_READ_TIMEOUT 1000
50 #define STLINK_NULL_EP 0
51 #define STLINK_RX_EP (1|ENDPOINT_IN)
52 #define STLINK_TX_EP (2|ENDPOINT_OUT)
53 #define STLINK_TRACE_EP (3|ENDPOINT_IN)
55 #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
56 #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
58 #define STLINK_SG_SIZE (31)
59 #define STLINK_DATA_SIZE (4096)
60 #define STLINK_CMD_SIZE_V2 (16)
61 #define STLINK_CMD_SIZE_V1 (10)
63 #define STLINK_V1_PID (0x3744)
64 #define STLINK_V2_PID (0x3748)
65 #define STLINK_V2_1_PID (0x374B)
66 #define STLINK_V2_1_NO_MSD_PID (0x3752)
68 /* the current implementation of the stlink limits
69 * 8bit read/writes to max 64 bytes. */
70 #define STLINK_MAX_RW8 (64)
72 /* "WAIT" responses will be retried (with exponential backoff) at
73 * most this many times before failing to caller.
75 #define MAX_WAIT_RETRIES 8
77 enum stlink_jtag_api_version
{
78 STLINK_JTAG_API_V1
= 1,
83 struct stlink_usb_version
{
90 /** highest supported jtag api version */
91 enum stlink_jtag_api_version jtag_api_max
;
92 /** one bit for each feature supported. See macros STLINK_F_* */
97 struct stlink_usb_handle_s
{
99 struct jtag_libusb_device_handle
*fd
;
101 struct libusb_transfer
*trans
;
109 uint8_t cmdbuf
[STLINK_SG_SIZE
];
115 uint8_t databuf
[STLINK_DATA_SIZE
];
117 uint32_t max_mem_packet
;
119 enum hl_transports transport
;
121 struct stlink_usb_version version
;
126 /** this is the currently used jtag api */
127 enum stlink_jtag_api_version jtag_api
;
130 /** whether SWO tracing is enabled or not */
132 /** trace module source clock */
135 /** reconnect is needed next time we try to query the
137 bool reconnect_pending
;
140 #define STLINK_SWIM_ERR_OK 0x00
141 #define STLINK_SWIM_BUSY 0x01
142 #define STLINK_DEBUG_ERR_OK 0x80
143 #define STLINK_DEBUG_ERR_FAULT 0x81
144 #define STLINK_SWD_AP_WAIT 0x10
145 #define STLINK_SWD_AP_FAULT 0x11
146 #define STLINK_SWD_AP_ERROR 0x12
147 #define STLINK_SWD_AP_PARITY_ERROR 0x13
148 #define STLINK_JTAG_WRITE_ERROR 0x0c
149 #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
150 #define STLINK_SWD_DP_WAIT 0x14
151 #define STLINK_SWD_DP_FAULT 0x15
152 #define STLINK_SWD_DP_ERROR 0x16
153 #define STLINK_SWD_DP_PARITY_ERROR 0x17
155 #define STLINK_SWD_AP_WDATA_ERROR 0x18
156 #define STLINK_SWD_AP_STICKY_ERROR 0x19
157 #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
159 #define STLINK_CORE_RUNNING 0x80
160 #define STLINK_CORE_HALTED 0x81
161 #define STLINK_CORE_STAT_UNKNOWN -1
163 #define STLINK_GET_VERSION 0xF1
164 #define STLINK_DEBUG_COMMAND 0xF2
165 #define STLINK_DFU_COMMAND 0xF3
166 #define STLINK_SWIM_COMMAND 0xF4
167 #define STLINK_GET_CURRENT_MODE 0xF5
168 #define STLINK_GET_TARGET_VOLTAGE 0xF7
170 #define STLINK_DEV_DFU_MODE 0x00
171 #define STLINK_DEV_MASS_MODE 0x01
172 #define STLINK_DEV_DEBUG_MODE 0x02
173 #define STLINK_DEV_SWIM_MODE 0x03
174 #define STLINK_DEV_BOOTLOADER_MODE 0x04
175 #define STLINK_DEV_UNKNOWN_MODE -1
177 #define STLINK_DFU_EXIT 0x07
180 STLINK_SWIM_ENTER_SEQ
181 1.3ms low then 750Hz then 1.5kHz
184 STM8 DM pulls reset pin low 50us
187 uint8_t (0=low|1=high)
194 send syncronization seq (16us low, response 64 clocks low)
196 #define STLINK_SWIM_ENTER 0x00
197 #define STLINK_SWIM_EXIT 0x01
198 #define STLINK_SWIM_READ_CAP 0x02
199 #define STLINK_SWIM_SPEED 0x03
200 #define STLINK_SWIM_ENTER_SEQ 0x04
201 #define STLINK_SWIM_GEN_RST 0x05
202 #define STLINK_SWIM_RESET 0x06
203 #define STLINK_SWIM_ASSERT_RESET 0x07
204 #define STLINK_SWIM_DEASSERT_RESET 0x08
205 #define STLINK_SWIM_READSTATUS 0x09
206 #define STLINK_SWIM_WRITEMEM 0x0a
207 #define STLINK_SWIM_READMEM 0x0b
208 #define STLINK_SWIM_READBUF 0x0c
210 #define STLINK_DEBUG_ENTER_JTAG 0x00
211 #define STLINK_DEBUG_GETSTATUS 0x01
212 #define STLINK_DEBUG_FORCEDEBUG 0x02
213 #define STLINK_DEBUG_APIV1_RESETSYS 0x03
214 #define STLINK_DEBUG_APIV1_READALLREGS 0x04
215 #define STLINK_DEBUG_APIV1_READREG 0x05
216 #define STLINK_DEBUG_APIV1_WRITEREG 0x06
217 #define STLINK_DEBUG_READMEM_32BIT 0x07
218 #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
219 #define STLINK_DEBUG_RUNCORE 0x09
220 #define STLINK_DEBUG_STEPCORE 0x0a
221 #define STLINK_DEBUG_APIV1_SETFP 0x0b
222 #define STLINK_DEBUG_READMEM_8BIT 0x0c
223 #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
224 #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
225 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
226 #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
228 #define STLINK_DEBUG_ENTER_JTAG 0x00
229 #define STLINK_DEBUG_ENTER_SWD 0xa3
231 #define STLINK_DEBUG_APIV1_ENTER 0x20
232 #define STLINK_DEBUG_EXIT 0x21
233 #define STLINK_DEBUG_READCOREID 0x22
235 #define STLINK_DEBUG_APIV2_ENTER 0x30
236 #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
237 #define STLINK_DEBUG_APIV2_RESETSYS 0x32
238 #define STLINK_DEBUG_APIV2_READREG 0x33
239 #define STLINK_DEBUG_APIV2_WRITEREG 0x34
240 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
241 #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
243 #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
244 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
245 #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
247 #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
248 #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
249 #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
250 #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
251 #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
253 #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
254 #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
256 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
257 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
258 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
260 #define STLINK_TRACE_SIZE 4096
261 #define STLINK_TRACE_MAX_HZ 2000000
265 STLINK_MODE_UNKNOWN
= 0,
268 STLINK_MODE_DEBUG_JTAG
,
269 STLINK_MODE_DEBUG_SWD
,
270 STLINK_MODE_DEBUG_SWIM
273 #define REQUEST_SENSE 0x03
274 #define REQUEST_SENSE_LENGTH 18
277 * Map the relevant features, quirks and workaround for specific firmware
280 #define STLINK_F_HAS_TRACE (1UL << 0)
281 #define STLINK_F_HAS_SWD_SET_FREQ (1UL << 1)
282 #define STLINK_F_HAS_JTAG_SET_FREQ (1UL << 2)
283 #define STLINK_F_HAS_MEM_16BIT (1UL << 3)
286 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
293 /* SWD clock speed */
294 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
296 {1800, 1}, /* default */
309 /* JTAG clock speed */
310 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
315 {1125, 32}, /* default */
321 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
322 static int stlink_swim_status(void *handle
);
325 static int stlink_usb_xfer_v1_get_status(void *handle
)
327 struct stlink_usb_handle_s
*h
= handle
;
329 assert(handle
!= NULL
);
332 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
334 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
335 13, STLINK_READ_TIMEOUT
) != 13)
340 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
343 if (t1
!= 0x53425355)
351 if (h
->cmdbuf
[12] != 0)
358 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
360 struct stlink_usb_handle_s
*h
= handle
;
362 assert(handle
!= NULL
);
364 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
365 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
369 if (h
->direction
== h
->tx_ep
&& size
) {
370 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
371 size
, STLINK_WRITE_TIMEOUT
) != size
) {
372 LOG_DEBUG("bulk write failed");
375 } else if (h
->direction
== h
->rx_ep
&& size
) {
376 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
377 size
, STLINK_READ_TIMEOUT
) != size
) {
378 LOG_DEBUG("bulk read failed");
387 static int stlink_usb_xfer_v1_get_sense(void *handle
)
390 struct stlink_usb_handle_s
*h
= handle
;
392 assert(handle
!= NULL
);
394 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
396 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
397 h
->cmdbuf
[h
->cmdidx
++] = 0;
398 h
->cmdbuf
[h
->cmdidx
++] = 0;
399 h
->cmdbuf
[h
->cmdidx
++] = 0;
400 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
402 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
407 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
414 transfers block in cmdbuf
415 <size> indicates number of bytes in the following
418 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
420 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
421 struct stlink_usb_handle_s
*h
= handle
;
423 assert(handle
!= NULL
);
425 if (h
->version
.stlink
== 1) {
426 cmdsize
= STLINK_SG_SIZE
;
427 /* put length in bCBWCBLength */
428 h
->cmdbuf
[14] = h
->cmdidx
-15;
431 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
436 if (h
->version
.stlink
== 1) {
437 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
438 /* check csw status */
439 if (h
->cmdbuf
[12] == 1) {
440 LOG_DEBUG("get sense");
441 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
452 Converts an STLINK status code held in the first byte of a response
453 to an openocd error, logs any error/wait status as debug output.
455 static int stlink_usb_error_check(void *handle
)
457 struct stlink_usb_handle_s
*h
= handle
;
459 assert(handle
!= NULL
);
461 if (h
->transport
== HL_TRANSPORT_SWIM
) {
462 switch (h
->databuf
[0]) {
463 case STLINK_SWIM_ERR_OK
:
465 case STLINK_SWIM_BUSY
:
468 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
473 /* TODO: no error checking yet on api V1 */
474 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
475 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
477 switch (h
->databuf
[0]) {
478 case STLINK_DEBUG_ERR_OK
:
480 case STLINK_DEBUG_ERR_FAULT
:
481 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
483 case STLINK_SWD_AP_WAIT
:
484 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
486 case STLINK_SWD_DP_WAIT
:
487 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
489 case STLINK_JTAG_WRITE_ERROR
:
490 LOG_DEBUG("Write error");
492 case STLINK_JTAG_WRITE_VERIF_ERROR
:
493 LOG_DEBUG("Write verify error, ignoring");
495 case STLINK_SWD_AP_FAULT
:
496 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
497 * returns ERROR_OK with the comment:
498 * Change in error status when reading outside RAM.
499 * This fix allows CDT plugin to visualize memory.
501 LOG_DEBUG("STLINK_SWD_AP_FAULT");
503 case STLINK_SWD_AP_ERROR
:
504 LOG_DEBUG("STLINK_SWD_AP_ERROR");
506 case STLINK_SWD_AP_PARITY_ERROR
:
507 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
509 case STLINK_SWD_DP_FAULT
:
510 LOG_DEBUG("STLINK_SWD_DP_FAULT");
512 case STLINK_SWD_DP_ERROR
:
513 LOG_DEBUG("STLINK_SWD_DP_ERROR");
515 case STLINK_SWD_DP_PARITY_ERROR
:
516 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
518 case STLINK_SWD_AP_WDATA_ERROR
:
519 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
521 case STLINK_SWD_AP_STICKY_ERROR
:
522 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
524 case STLINK_SWD_AP_STICKYORUN_ERROR
:
525 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
528 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
534 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
536 Works for commands where the STLINK_DEBUG status is returned in the first
537 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
539 Returns an openocd result code.
541 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
545 struct stlink_usb_handle_s
*h
= handle
;
548 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
549 res
= stlink_usb_xfer(handle
, buf
, size
);
554 if (h
->transport
== HL_TRANSPORT_SWIM
) {
555 res
= stlink_swim_status(handle
);
560 res
= stlink_usb_error_check(handle
);
561 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
562 usleep((1<<retries
++) * 1000);
570 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
572 struct stlink_usb_handle_s
*h
= handle
;
574 assert(handle
!= NULL
);
576 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
578 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
579 size
, STLINK_READ_TIMEOUT
) != size
) {
580 LOG_ERROR("bulk trace read failed");
588 this function writes transfer length in
589 the right place in the cb
591 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
593 struct stlink_usb_handle_s
*h
= handle
;
595 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
598 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
600 struct stlink_usb_handle_s
*h
= handle
;
602 /* fill the send buffer */
603 strcpy((char *)h
->cmdbuf
, "USBC");
605 /* csw tag not used */
606 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
608 /* cbw data transfer length (in the following data phase in or out) */
609 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
612 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
613 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
614 /* cdb clength (is filled in at xfer) */
615 h
->cmdbuf
[h
->cmdidx
++] = 0;
619 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
621 struct stlink_usb_handle_s
*h
= handle
;
623 h
->direction
= direction
;
627 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
628 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
630 if (h
->version
.stlink
== 1)
631 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
635 static int stlink_usb_version(void *handle
)
640 struct stlink_usb_handle_s
*h
= handle
;
642 assert(handle
!= NULL
);
644 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
646 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
648 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
653 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
655 h
->version
.stlink
= (v
>> 12) & 0x0f;
656 h
->version
.jtag
= (v
>> 6) & 0x3f;
657 h
->version
.swim
= v
& 0x3f;
658 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
659 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
662 switch (h
->version
.stlink
) {
664 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
665 if (h
->version
.jtag
>= 11)
666 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
668 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
672 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
673 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
675 /* API for trace from J13 */
676 /* API for target voltage from J13 */
677 if (h
->version
.jtag
>= 13)
678 flags
|= STLINK_F_HAS_TRACE
;
680 /* API to set SWD frequency from J22 */
681 if (h
->version
.jtag
>= 22)
682 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
684 /* API to set JTAG frequency from J24 */
685 if (h
->version
.jtag
>= 24)
686 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
688 /* API to read/write memory at 16 bit from J26 */
689 if (h
->version
.jtag
>= 26)
690 flags
|= STLINK_F_HAS_MEM_16BIT
;
696 h
->version
.flags
= flags
;
698 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
701 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
709 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
711 struct stlink_usb_handle_s
*h
= handle
;
712 uint32_t adc_results
[2];
714 /* no error message, simply quit with error */
715 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
716 return ERROR_COMMAND_NOTFOUND
;
718 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
720 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
722 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
724 if (result
!= ERROR_OK
)
728 adc_results
[0] = le_to_h_u32(h
->databuf
);
729 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
734 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
736 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
741 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
743 struct stlink_usb_handle_s
*h
= handle
;
745 assert(handle
!= NULL
);
747 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
748 return ERROR_COMMAND_NOTFOUND
;
750 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
752 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
753 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
754 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
757 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
759 if (result
!= ERROR_OK
)
765 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
767 struct stlink_usb_handle_s
*h
= handle
;
769 assert(handle
!= NULL
);
771 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
772 return ERROR_COMMAND_NOTFOUND
;
774 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
776 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
777 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
778 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
781 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
783 if (result
!= ERROR_OK
)
790 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
793 struct stlink_usb_handle_s
*h
= handle
;
795 assert(handle
!= NULL
);
797 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
799 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
801 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
806 *mode
= h
->databuf
[0];
812 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
815 struct stlink_usb_handle_s
*h
= handle
;
817 assert(handle
!= NULL
);
819 /* on api V2 we are able the read the latest command
821 * TODO: we need the test on api V1 too
823 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
826 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
829 case STLINK_MODE_DEBUG_JTAG
:
830 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
831 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
832 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
835 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
837 case STLINK_MODE_DEBUG_SWD
:
838 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
839 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
840 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
842 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
843 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
845 case STLINK_MODE_DEBUG_SWIM
:
846 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
847 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
848 /* no answer for this function... */
851 case STLINK_MODE_DFU
:
852 case STLINK_MODE_MASS
:
857 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
861 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
864 struct stlink_usb_handle_s
*h
= handle
;
866 assert(handle
!= NULL
);
868 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
871 case STLINK_MODE_DEBUG_JTAG
:
872 case STLINK_MODE_DEBUG_SWD
:
873 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
874 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
876 case STLINK_MODE_DEBUG_SWIM
:
877 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
878 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
880 case STLINK_MODE_DFU
:
881 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
882 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
884 case STLINK_MODE_MASS
:
889 res
= stlink_usb_xfer(handle
, 0, 0);
897 static int stlink_usb_assert_srst(void *handle
, int srst
);
899 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
902 case HL_TRANSPORT_SWD
:
903 return STLINK_MODE_DEBUG_SWD
;
904 case HL_TRANSPORT_JTAG
:
905 return STLINK_MODE_DEBUG_JTAG
;
906 case HL_TRANSPORT_SWIM
:
907 return STLINK_MODE_DEBUG_SWIM
;
909 return STLINK_MODE_UNKNOWN
;
914 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
918 enum stlink_mode emode
;
919 struct stlink_usb_handle_s
*h
= handle
;
921 assert(handle
!= NULL
);
923 res
= stlink_usb_current_mode(handle
, &mode
);
928 LOG_DEBUG("MODE: 0x%02X", mode
);
930 /* try to exit current mode */
932 case STLINK_DEV_DFU_MODE
:
933 emode
= STLINK_MODE_DFU
;
935 case STLINK_DEV_DEBUG_MODE
:
936 emode
= STLINK_MODE_DEBUG_SWD
;
938 case STLINK_DEV_SWIM_MODE
:
939 emode
= STLINK_MODE_DEBUG_SWIM
;
941 case STLINK_DEV_BOOTLOADER_MODE
:
942 case STLINK_DEV_MASS_MODE
:
944 emode
= STLINK_MODE_UNKNOWN
;
948 if (emode
!= STLINK_MODE_UNKNOWN
) {
949 res
= stlink_usb_mode_leave(handle
, emode
);
955 res
= stlink_usb_current_mode(handle
, &mode
);
960 /* we check the target voltage here as an aid to debugging connection problems.
961 * the stlink requires the target Vdd to be connected for reliable debugging.
962 * this cmd is supported in all modes except DFU
964 if (mode
!= STLINK_DEV_DFU_MODE
) {
966 float target_voltage
;
968 /* check target voltage (if supported) */
969 res
= stlink_usb_check_voltage(h
, &target_voltage
);
971 if (res
!= ERROR_OK
) {
972 if (res
!= ERROR_COMMAND_NOTFOUND
)
973 LOG_ERROR("voltage check failed");
974 /* attempt to continue as it is not a catastrophic failure */
976 /* check for a sensible target voltage, operating range is 1.65-5.5v
977 * according to datasheet */
978 if (target_voltage
< 1.5)
979 LOG_ERROR("target voltage may be too low for reliable debugging");
983 LOG_DEBUG("MODE: 0x%02X", mode
);
985 /* set selected mode */
986 emode
= stlink_get_mode(h
->transport
);
988 if (emode
== STLINK_MODE_UNKNOWN
) {
989 LOG_ERROR("selected mode (transport) not supported");
993 /* preliminary SRST assert:
994 * We want SRST is asserted before activating debug signals (mode_enter).
995 * As the required mode has not been set, the adapter may not know what pin to use.
996 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
997 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
998 * after power on, SWIM_RST stays unchanged */
999 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
1000 stlink_usb_assert_srst(handle
, 0);
1001 /* do not check the return status here, we will
1002 proceed and enter the desired mode below
1003 and try asserting srst again. */
1005 res
= stlink_usb_mode_enter(handle
, emode
);
1006 if (res
!= ERROR_OK
)
1009 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1010 if (connect_under_reset
) {
1011 res
= stlink_usb_assert_srst(handle
, 0);
1012 if (res
!= ERROR_OK
)
1016 res
= stlink_usb_current_mode(handle
, &mode
);
1018 if (res
!= ERROR_OK
)
1021 LOG_DEBUG("MODE: 0x%02X", mode
);
1026 /* request status from last swim request */
1027 static int stlink_swim_status(void *handle
)
1029 struct stlink_usb_handle_s
*h
= handle
;
1032 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1033 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1034 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1035 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1036 if (res
!= ERROR_OK
)
1041 the purpose of this function is unknown...
1042 capabilites? anyway for swim v6 it returns
1045 __attribute__((unused
))
1046 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1048 struct stlink_usb_handle_s
*h
= handle
;
1051 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1052 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1053 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1054 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1055 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
1056 if (res
!= ERROR_OK
)
1058 memcpy(cap
, h
->databuf
, 8);
1062 /* debug dongle assert/deassert sreset line */
1063 static int stlink_swim_assert_reset(void *handle
, int reset
)
1065 struct stlink_usb_handle_s
*h
= handle
;
1068 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1069 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1071 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1073 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1074 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1075 if (res
!= ERROR_OK
)
1082 1.3ms low then 750Hz then 1.5kHz
1084 static int stlink_swim_enter(void *handle
)
1086 struct stlink_usb_handle_s
*h
= handle
;
1089 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1090 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1091 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1092 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1093 if (res
!= ERROR_OK
)
1098 /* switch high/low speed swim */
1099 static int stlink_swim_speed(void *handle
, int speed
)
1101 struct stlink_usb_handle_s
*h
= handle
;
1104 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1105 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1106 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1108 h
->cmdbuf
[h
->cmdidx
++] = 1;
1110 h
->cmdbuf
[h
->cmdidx
++] = 0;
1111 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1112 if (res
!= ERROR_OK
)
1118 initiate srst from swim.
1119 nrst is pulled low for 50us.
1121 static int stlink_swim_generate_rst(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_GEN_RST
;
1129 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1130 if (res
!= ERROR_OK
)
1136 send resyncronize sequence
1137 swim is pulled low for 16us
1138 reply is 64 clks low
1140 static int stlink_swim_resync(void *handle
)
1142 struct stlink_usb_handle_s
*h
= handle
;
1145 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1146 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1147 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1148 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1149 if (res
!= ERROR_OK
)
1154 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1156 struct stlink_usb_handle_s
*h
= handle
;
1159 unsigned int datalen
= 0;
1160 int cmdsize
= STLINK_CMD_SIZE_V2
;
1162 if (len
> STLINK_DATA_SIZE
)
1165 if (h
->version
.stlink
== 1)
1166 cmdsize
= STLINK_SG_SIZE
;
1168 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1169 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1170 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1171 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1173 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1175 for (i
= 0; i
< len
; i
++) {
1176 if (h
->cmdidx
== cmdsize
)
1177 h
->databuf
[datalen
++] = *(data
++);
1179 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1181 if (h
->version
.stlink
== 1)
1182 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1184 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1185 if (res
!= ERROR_OK
)
1190 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1192 struct stlink_usb_handle_s
*h
= handle
;
1195 if (len
> STLINK_DATA_SIZE
)
1198 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1199 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1200 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1201 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1203 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1205 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1206 if (res
!= ERROR_OK
)
1209 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1210 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1211 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1212 res
= stlink_usb_xfer(handle
, data
, len
);
1213 if (res
!= ERROR_OK
)
1220 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1223 struct stlink_usb_handle_s
*h
= handle
;
1225 assert(handle
!= NULL
);
1227 /* there is no swim read core id cmd */
1228 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1233 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1235 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1236 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1238 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1240 if (res
!= ERROR_OK
)
1243 *idcode
= le_to_h_u32(h
->databuf
);
1245 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1250 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1252 struct stlink_usb_handle_s
*h
= handle
;
1255 assert(handle
!= NULL
);
1257 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1259 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1260 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1261 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1264 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1265 if (res
!= ERROR_OK
)
1268 *val
= le_to_h_u32(h
->databuf
+ 4);
1272 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1274 struct stlink_usb_handle_s
*h
= handle
;
1276 assert(handle
!= NULL
);
1278 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1280 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1281 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1282 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1284 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1285 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1287 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1290 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1294 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1296 struct stlink_usb_handle_s
*h
= handle
;
1298 assert(handle
!= NULL
);
1300 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1303 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1305 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1306 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1308 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1309 if (res
!= ERROR_OK
)
1312 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1313 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1316 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1317 if (res
!= ERROR_OK
)
1326 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1331 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1332 if (result
!= ERROR_OK
)
1333 return TARGET_UNKNOWN
;
1335 if (status
& S_HALT
)
1336 return TARGET_HALTED
;
1337 else if (status
& S_RESET_ST
)
1338 return TARGET_RESET
;
1340 return TARGET_RUNNING
;
1344 static enum target_state
stlink_usb_state(void *handle
)
1347 struct stlink_usb_handle_s
*h
= handle
;
1349 assert(handle
!= NULL
);
1351 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1352 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1353 if (res
!= ERROR_OK
)
1354 return TARGET_UNKNOWN
;
1356 res
= stlink_swim_resync(handle
);
1357 if (res
!= ERROR_OK
)
1358 return TARGET_UNKNOWN
;
1363 if (h
->reconnect_pending
) {
1364 LOG_INFO("Previous state query failed, trying to reconnect");
1365 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1367 if (res
!= ERROR_OK
)
1368 return TARGET_UNKNOWN
;
1370 h
->reconnect_pending
= false;
1373 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1374 res
= stlink_usb_v2_get_status(handle
);
1375 if (res
== TARGET_UNKNOWN
)
1376 h
->reconnect_pending
= true;
1380 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1382 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1383 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1385 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1387 if (res
!= ERROR_OK
)
1388 return TARGET_UNKNOWN
;
1390 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1391 return TARGET_RUNNING
;
1392 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1393 return TARGET_HALTED
;
1395 h
->reconnect_pending
= true;
1397 return TARGET_UNKNOWN
;
1400 static int stlink_usb_assert_srst(void *handle
, int srst
)
1402 struct stlink_usb_handle_s
*h
= handle
;
1404 assert(handle
!= NULL
);
1406 if (h
->transport
== HL_TRANSPORT_SWIM
)
1407 return stlink_swim_assert_reset(handle
, srst
);
1409 if (h
->version
.stlink
== 1)
1410 return ERROR_COMMAND_NOTFOUND
;
1412 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1414 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1415 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1416 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1418 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1422 static void stlink_usb_trace_disable(void *handle
)
1425 struct stlink_usb_handle_s
*h
= handle
;
1427 assert(handle
!= NULL
);
1429 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1431 LOG_DEBUG("Tracing: disable");
1433 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1434 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1435 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1436 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1438 if (res
== ERROR_OK
)
1439 h
->trace
.enabled
= false;
1444 static int stlink_usb_trace_enable(void *handle
)
1447 struct stlink_usb_handle_s
*h
= handle
;
1449 assert(handle
!= NULL
);
1451 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1452 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1454 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1455 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1456 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1458 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1461 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1463 if (res
== ERROR_OK
) {
1464 h
->trace
.enabled
= true;
1465 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1468 LOG_ERROR("Tracing is not supported by this version.");
1476 static int stlink_usb_reset(void *handle
)
1478 struct stlink_usb_handle_s
*h
= handle
;
1481 assert(handle
!= NULL
);
1483 if (h
->transport
== HL_TRANSPORT_SWIM
)
1484 return stlink_swim_generate_rst(handle
);
1486 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1488 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1490 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1491 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1493 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1495 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1496 if (retval
!= ERROR_OK
)
1499 if (h
->trace
.enabled
) {
1500 stlink_usb_trace_disable(h
);
1501 return stlink_usb_trace_enable(h
);
1508 static int stlink_usb_run(void *handle
)
1511 struct stlink_usb_handle_s
*h
= handle
;
1513 assert(handle
!= NULL
);
1515 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1516 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1521 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1523 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1524 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1526 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1530 static int stlink_usb_halt(void *handle
)
1533 struct stlink_usb_handle_s
*h
= handle
;
1535 assert(handle
!= NULL
);
1537 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1538 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1543 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1545 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1546 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1548 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1552 static int stlink_usb_step(void *handle
)
1554 struct stlink_usb_handle_s
*h
= handle
;
1556 assert(handle
!= NULL
);
1558 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1559 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1560 * that the Cortex-M3 currently does. */
1561 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1562 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1563 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1566 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1568 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1569 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1571 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1575 static int stlink_usb_read_regs(void *handle
)
1578 struct stlink_usb_handle_s
*h
= handle
;
1580 assert(handle
!= NULL
);
1582 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1584 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1585 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1586 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1588 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1590 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1592 if (res
!= ERROR_OK
)
1599 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1602 struct stlink_usb_handle_s
*h
= handle
;
1604 assert(handle
!= NULL
);
1606 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1608 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1609 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1610 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1612 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1613 h
->cmdbuf
[h
->cmdidx
++] = num
;
1615 if (h
->jtag_api
== STLINK_JTAG_API_V1
) {
1616 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1617 if (res
!= ERROR_OK
)
1619 *val
= le_to_h_u32(h
->databuf
);
1622 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1623 if (res
!= ERROR_OK
)
1625 *val
= le_to_h_u32(h
->databuf
+ 4);
1631 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1633 struct stlink_usb_handle_s
*h
= handle
;
1635 assert(handle
!= NULL
);
1637 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1639 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1640 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1641 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1643 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1644 h
->cmdbuf
[h
->cmdidx
++] = num
;
1645 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1648 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1651 static int stlink_usb_get_rw_status(void *handle
)
1654 struct stlink_usb_handle_s
*h
= handle
;
1656 assert(handle
!= NULL
);
1658 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1661 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1663 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1664 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1666 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1668 if (res
!= ERROR_OK
)
1671 return stlink_usb_error_check(h
);
1675 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1679 uint16_t read_len
= len
;
1680 struct stlink_usb_handle_s
*h
= handle
;
1682 assert(handle
!= NULL
);
1684 /* max 8bit read/write is 64bytes */
1685 if (len
> STLINK_MAX_RW8
) {
1686 LOG_DEBUG("max buffer length exceeded");
1690 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1692 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1693 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1694 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1696 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1699 /* we need to fix read length for single bytes */
1703 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1705 if (res
!= ERROR_OK
)
1708 memcpy(buffer
, h
->databuf
, len
);
1710 return stlink_usb_get_rw_status(handle
);
1714 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1715 const uint8_t *buffer
)
1718 struct stlink_usb_handle_s
*h
= handle
;
1720 assert(handle
!= NULL
);
1722 /* max 8bit read/write is 64bytes */
1723 if (len
> STLINK_MAX_RW8
) {
1724 LOG_DEBUG("max buffer length exceeded");
1728 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1730 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1731 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1732 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1734 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1737 res
= stlink_usb_xfer(handle
, buffer
, len
);
1739 if (res
!= ERROR_OK
)
1742 return stlink_usb_get_rw_status(handle
);
1746 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1750 struct stlink_usb_handle_s
*h
= handle
;
1752 assert(handle
!= NULL
);
1754 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
1755 return ERROR_COMMAND_NOTFOUND
;
1757 /* data must be a multiple of 2 and half-word aligned */
1758 if (len
% 2 || addr
% 2) {
1759 LOG_DEBUG("Invalid data alignment");
1760 return ERROR_TARGET_UNALIGNED_ACCESS
;
1763 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1765 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1766 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
1767 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1769 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1772 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1774 if (res
!= ERROR_OK
)
1777 memcpy(buffer
, h
->databuf
, len
);
1779 return stlink_usb_get_rw_status(handle
);
1783 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1784 const uint8_t *buffer
)
1787 struct stlink_usb_handle_s
*h
= handle
;
1789 assert(handle
!= NULL
);
1791 if (!(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
1792 return ERROR_COMMAND_NOTFOUND
;
1794 /* data must be a multiple of 2 and half-word aligned */
1795 if (len
% 2 || addr
% 2) {
1796 LOG_DEBUG("Invalid data alignment");
1797 return ERROR_TARGET_UNALIGNED_ACCESS
;
1800 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1802 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1803 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
1804 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1806 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1809 res
= stlink_usb_xfer(handle
, buffer
, len
);
1811 if (res
!= ERROR_OK
)
1814 return stlink_usb_get_rw_status(handle
);
1818 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1822 struct stlink_usb_handle_s
*h
= handle
;
1824 assert(handle
!= NULL
);
1826 /* data must be a multiple of 4 and word aligned */
1827 if (len
% 4 || addr
% 4) {
1828 LOG_DEBUG("Invalid data alignment");
1829 return ERROR_TARGET_UNALIGNED_ACCESS
;
1832 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1835 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1836 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1838 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1841 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1843 if (res
!= ERROR_OK
)
1846 memcpy(buffer
, h
->databuf
, len
);
1848 return stlink_usb_get_rw_status(handle
);
1852 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1853 const uint8_t *buffer
)
1856 struct stlink_usb_handle_s
*h
= handle
;
1858 assert(handle
!= NULL
);
1860 /* data must be a multiple of 4 and word aligned */
1861 if (len
% 4 || addr
% 4) {
1862 LOG_DEBUG("Invalid data alignment");
1863 return ERROR_TARGET_UNALIGNED_ACCESS
;
1866 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1868 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1869 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1870 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1872 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1875 res
= stlink_usb_xfer(handle
, buffer
, len
);
1877 if (res
!= ERROR_OK
)
1880 return stlink_usb_get_rw_status(handle
);
1883 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1885 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1886 if (max_tar_block
== 0)
1888 return max_tar_block
;
1891 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1892 uint32_t count
, uint8_t *buffer
)
1894 int retval
= ERROR_OK
;
1895 uint32_t bytes_remaining
;
1897 struct stlink_usb_handle_s
*h
= handle
;
1899 /* calculate byte count */
1902 /* switch to 8 bit if stlink does not support 16 bit memory read */
1903 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
1908 bytes_remaining
= (size
!= 1) ? \
1909 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1911 if (count
< bytes_remaining
)
1912 bytes_remaining
= count
;
1914 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1915 retval
= stlink_swim_readbytes(handle
, addr
, bytes_remaining
, buffer
);
1916 if (retval
!= ERROR_OK
)
1920 * all stlink support 8/32bit memory read/writes and only from
1921 * stlink V2J26 there is support for 16 bit memory read/write.
1922 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
1927 /* When in jtag mode the stlink uses the auto-increment functionality.
1928 * However it expects us to pass the data correctly, this includes
1929 * alignment and any page boundaries. We already do this as part of the
1930 * adi_v5 implementation, but the stlink is a hla adapter and so this
1931 * needs implementing manually.
1932 * currently this only affects jtag mode, according to ST they do single
1933 * access in SWD mode - but this may change and so we do it for both modes */
1935 /* we first need to check for any unaligned bytes */
1936 if (addr
& (size
- 1)) {
1938 uint32_t head_bytes
= size
- (addr
& (size
- 1));
1939 retval
= stlink_usb_read_mem8(handle
, addr
, head_bytes
, buffer
);
1940 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1941 usleep((1<<retries
++) * 1000);
1944 if (retval
!= ERROR_OK
)
1946 buffer
+= head_bytes
;
1948 count
-= head_bytes
;
1949 bytes_remaining
-= head_bytes
;
1952 if (bytes_remaining
& (size
- 1))
1953 retval
= stlink_usb_read_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
1955 retval
= stlink_usb_read_mem16(handle
, addr
, bytes_remaining
, buffer
);
1957 retval
= stlink_usb_read_mem32(handle
, addr
, bytes_remaining
, buffer
);
1959 retval
= stlink_usb_read_mem8(handle
, addr
, bytes_remaining
, buffer
);
1961 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
1962 usleep((1<<retries
++) * 1000);
1965 if (retval
!= ERROR_OK
)
1968 buffer
+= bytes_remaining
;
1969 addr
+= bytes_remaining
;
1970 count
-= bytes_remaining
;
1976 static int stlink_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
1977 uint32_t count
, const uint8_t *buffer
)
1979 int retval
= ERROR_OK
;
1980 uint32_t bytes_remaining
;
1982 struct stlink_usb_handle_s
*h
= handle
;
1984 /* calculate byte count */
1987 /* switch to 8 bit if stlink does not support 16 bit memory read */
1988 if (size
== 2 && !(h
->version
.flags
& STLINK_F_HAS_MEM_16BIT
))
1993 bytes_remaining
= (size
!= 1) ? \
1994 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1996 if (count
< bytes_remaining
)
1997 bytes_remaining
= count
;
1999 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2000 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
2001 if (retval
!= ERROR_OK
)
2005 * all stlink support 8/32bit memory read/writes and only from
2006 * stlink V2J26 there is support for 16 bit memory read/write.
2007 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2012 /* When in jtag mode the stlink uses the auto-increment functionality.
2013 * However it expects us to pass the data correctly, this includes
2014 * alignment and any page boundaries. We already do this as part of the
2015 * adi_v5 implementation, but the stlink is a hla adapter and so this
2016 * needs implementing manually.
2017 * currently this only affects jtag mode, according to ST they do single
2018 * access in SWD mode - but this may change and so we do it for both modes */
2020 /* we first need to check for any unaligned bytes */
2021 if (addr
& (size
- 1)) {
2023 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2024 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2025 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2026 usleep((1<<retries
++) * 1000);
2029 if (retval
!= ERROR_OK
)
2031 buffer
+= head_bytes
;
2033 count
-= head_bytes
;
2034 bytes_remaining
-= head_bytes
;
2037 if (bytes_remaining
& (size
- 1))
2038 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2040 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2042 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2045 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2046 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2047 usleep((1<<retries
++) * 1000);
2050 if (retval
!= ERROR_OK
)
2053 buffer
+= bytes_remaining
;
2054 addr
+= bytes_remaining
;
2055 count
-= bytes_remaining
;
2062 static int stlink_usb_override_target(const char *targetname
)
2064 return !strcmp(targetname
, "cortex_m");
2067 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2070 we dont care what the khz rate is
2071 we only have low and high speed...
2072 before changing speed the SWIM_CSR HS bit
2076 stlink_swim_speed(handle
, 0);
2078 stlink_swim_speed(handle
, 1);
2082 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2085 int speed_index
= -1;
2086 int speed_diff
= INT_MAX
;
2089 for (i
= 0; i
< map_size
; i
++) {
2090 if (khz
== map
[i
].speed
) {
2094 int current_diff
= khz
- map
[i
].speed
;
2095 /* get abs value for comparison */
2096 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2097 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2098 speed_diff
= current_diff
;
2104 if (speed_index
== -1) {
2105 /* this will only be here if we cannot match the slow speed.
2106 * use the slowest speed we support.*/
2107 speed_index
= map_size
- 1;
2109 } else if (i
== map_size
)
2112 if (!match
&& query
) {
2113 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2114 khz
, map
[speed_index
].speed
);
2120 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2123 struct stlink_usb_handle_s
*h
= handle
;
2125 /* old firmware cannot change it */
2126 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2129 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2130 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2133 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2134 if (result
!= ERROR_OK
) {
2135 LOG_ERROR("Unable to set adapter speed");
2140 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2143 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2146 struct stlink_usb_handle_s
*h
= handle
;
2148 /* old firmware cannot change it */
2149 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2152 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2153 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2156 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2157 if (result
!= ERROR_OK
) {
2158 LOG_ERROR("Unable to set adapter speed");
2163 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2166 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2170 LOG_DEBUG("Supported clock speeds are:");
2171 for (i
= 0; i
< map_size
; i
++)
2172 LOG_DEBUG("%d kHz", map
[i
].speed
);
2175 static int stlink_speed(void *handle
, int khz
, bool query
)
2177 struct stlink_usb_handle_s
*h
= handle
;
2182 if (h
->transport
== HL_TRANSPORT_SWIM
)
2183 return stlink_speed_swim(handle
, khz
, query
);
2184 else if (h
->transport
== HL_TRANSPORT_SWD
)
2185 return stlink_speed_swd(handle
, khz
, query
);
2186 else if (h
->transport
== HL_TRANSPORT_JTAG
)
2187 return stlink_speed_jtag(handle
, khz
, query
);
2193 static int stlink_usb_close(void *handle
)
2197 enum stlink_mode emode
;
2198 struct stlink_usb_handle_s
*h
= handle
;
2201 res
= stlink_usb_current_mode(handle
, &mode
);
2204 /* do not exit if return code != ERROR_OK,
2205 it prevents us from closing jtag_libusb */
2207 if (res
== ERROR_OK
) {
2208 /* try to exit current mode */
2210 case STLINK_DEV_DFU_MODE
:
2211 emode
= STLINK_MODE_DFU
;
2213 case STLINK_DEV_DEBUG_MODE
:
2214 emode
= STLINK_MODE_DEBUG_SWD
;
2216 case STLINK_DEV_SWIM_MODE
:
2217 emode
= STLINK_MODE_DEBUG_SWIM
;
2219 case STLINK_DEV_BOOTLOADER_MODE
:
2220 case STLINK_DEV_MASS_MODE
:
2222 emode
= STLINK_MODE_UNKNOWN
;
2226 if (emode
!= STLINK_MODE_UNKNOWN
)
2227 stlink_usb_mode_leave(handle
, emode
);
2228 /* do not check return code, it prevent
2229 us from closing jtag_libusb */
2233 jtag_libusb_close(h
->fd
);
2241 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2243 int err
, retry_count
= 1;
2244 struct stlink_usb_handle_s
*h
;
2245 enum stlink_jtag_api_version api
;
2247 LOG_DEBUG("stlink_usb_open");
2249 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2252 LOG_DEBUG("malloc failed");
2256 h
->transport
= param
->transport
;
2258 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2259 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2260 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2261 param
->serial
? param
->serial
: "");
2265 On certain host USB configurations(e.g. MacBook Air)
2266 STLINKv2 dongle seems to have its FW in a funky state if,
2267 after plugging it in, you try to use openocd with it more
2268 then once (by launching and closing openocd). In cases like
2269 that initial attempt to read the FW info via
2270 stlink_usb_version will fail and the device has to be reset
2271 in order to become operational.
2274 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2275 LOG_ERROR("open failed");
2279 jtag_libusb_set_configuration(h
->fd
, 0);
2281 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2282 LOG_DEBUG("claim interface failed");
2286 /* RX EP is common for all versions */
2287 h
->rx_ep
= STLINK_RX_EP
;
2290 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2291 LOG_DEBUG("libusb_get_pid failed");
2295 /* wrap version for first read */
2298 h
->version
.stlink
= 1;
2299 h
->tx_ep
= STLINK_TX_EP
;
2301 case STLINK_V2_1_PID
:
2302 case STLINK_V2_1_NO_MSD_PID
:
2303 h
->version
.stlink
= 2;
2304 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2305 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2308 /* fall through - we assume V2 to be the default version*/
2310 h
->version
.stlink
= 2;
2311 h
->tx_ep
= STLINK_TX_EP
;
2312 h
->trace_ep
= STLINK_TRACE_EP
;
2316 /* get the device version */
2317 err
= stlink_usb_version(h
);
2319 if (err
== ERROR_OK
) {
2321 } else if (h
->version
.stlink
== 1 ||
2323 LOG_ERROR("read version failed");
2326 err
= jtag_libusb_release_interface(h
->fd
, 0);
2327 if (err
!= ERROR_OK
) {
2328 LOG_ERROR("release interface failed");
2332 err
= jtag_libusb_reset_device(h
->fd
);
2333 if (err
!= ERROR_OK
) {
2334 LOG_ERROR("reset device failed");
2338 jtag_libusb_close(h
->fd
);
2340 Give the device one second to settle down and
2343 usleep(1 * 1000 * 1000);
2348 /* check if mode is supported */
2351 switch (h
->transport
) {
2352 case HL_TRANSPORT_SWD
:
2353 if (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
)
2356 case HL_TRANSPORT_JTAG
:
2357 if (h
->version
.jtag
== 0)
2360 case HL_TRANSPORT_SWIM
:
2361 if (h
->version
.swim
== 0)
2369 if (err
!= ERROR_OK
) {
2370 LOG_ERROR("mode (transport) not supported by device");
2374 api
= h
->version
.jtag_api_max
;
2376 LOG_INFO("using stlink api v%d", api
);
2378 /* set the used jtag api, this will default to the newest supported version */
2381 /* initialize the debug hardware */
2382 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2384 if (err
!= ERROR_OK
) {
2385 LOG_ERROR("init mode failed (unable to connect to the target)");
2389 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2390 err
= stlink_swim_enter(h
);
2391 if (err
!= ERROR_OK
) {
2392 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2396 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2400 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2401 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
2402 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2403 stlink_speed(h
, param
->initial_interface_speed
, false);
2405 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2406 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
2407 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2408 stlink_speed(h
, param
->initial_interface_speed
, false);
2412 /* get cpuid, so we can determine the max page size
2413 * start with a safe default */
2414 h
->max_mem_packet
= (1 << 10);
2417 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2418 if (err
== ERROR_OK
) {
2419 uint32_t cpuid
= le_to_h_u32(buffer
);
2420 int i
= (cpuid
>> 4) & 0xf;
2421 if (i
== 4 || i
== 3) {
2422 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2423 h
->max_mem_packet
= (1 << 12);
2427 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2434 stlink_usb_close(h
);
2439 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2440 uint32_t port_size
, unsigned int *trace_freq
)
2442 struct stlink_usb_handle_s
*h
= handle
;
2444 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2445 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2446 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2451 stlink_usb_trace_disable(h
);
2455 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2456 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2457 STLINK_TRACE_MAX_HZ
);
2461 stlink_usb_trace_disable(h
);
2464 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2465 h
->trace
.source_hz
= *trace_freq
;
2467 return stlink_usb_trace_enable(h
);
2471 struct hl_layout_api_s stlink_usb_layout_api
= {
2473 .open
= stlink_usb_open
,
2475 .close
= stlink_usb_close
,
2477 .idcode
= stlink_usb_idcode
,
2479 .state
= stlink_usb_state
,
2481 .reset
= stlink_usb_reset
,
2483 .assert_srst
= stlink_usb_assert_srst
,
2485 .run
= stlink_usb_run
,
2487 .halt
= stlink_usb_halt
,
2489 .step
= stlink_usb_step
,
2491 .read_regs
= stlink_usb_read_regs
,
2493 .read_reg
= stlink_usb_read_reg
,
2495 .write_reg
= stlink_usb_write_reg
,
2497 .read_mem
= stlink_usb_read_mem
,
2499 .write_mem
= stlink_usb_write_mem
,
2501 .write_debug_reg
= stlink_usb_write_debug_reg
,
2503 .override_target
= stlink_usb_override_target
,
2505 .speed
= stlink_speed
,
2507 .config_trace
= stlink_config_trace
,
2509 .poll_trace
= stlink_usb_trace_read
,