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)
285 #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
292 /* SWD clock speed */
293 static const struct speed_map stlink_khz_to_speed_map_swd
[] = {
295 {1800, 1}, /* default */
308 /* JTAG clock speed */
309 static const struct speed_map stlink_khz_to_speed_map_jtag
[] = {
314 {1125, 32}, /* default */
320 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
);
321 static int stlink_swim_status(void *handle
);
324 static int stlink_usb_xfer_v1_get_status(void *handle
)
326 struct stlink_usb_handle_s
*h
= handle
;
328 assert(handle
!= NULL
);
331 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
333 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)h
->cmdbuf
,
334 13, STLINK_READ_TIMEOUT
) != 13)
339 t1
= buf_get_u32(h
->cmdbuf
, 0, 32);
342 if (t1
!= 0x53425355)
350 if (h
->cmdbuf
[12] != 0)
357 static int stlink_usb_xfer_rw(void *handle
, int cmdsize
, const uint8_t *buf
, int size
)
359 struct stlink_usb_handle_s
*h
= handle
;
361 assert(handle
!= NULL
);
363 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)h
->cmdbuf
, cmdsize
,
364 STLINK_WRITE_TIMEOUT
) != cmdsize
) {
368 if (h
->direction
== h
->tx_ep
&& size
) {
369 if (jtag_libusb_bulk_write(h
->fd
, h
->tx_ep
, (char *)buf
,
370 size
, STLINK_WRITE_TIMEOUT
) != size
) {
371 LOG_DEBUG("bulk write failed");
374 } else if (h
->direction
== h
->rx_ep
&& size
) {
375 if (jtag_libusb_bulk_read(h
->fd
, h
->rx_ep
, (char *)buf
,
376 size
, STLINK_READ_TIMEOUT
) != size
) {
377 LOG_DEBUG("bulk read failed");
386 static int stlink_usb_xfer_v1_get_sense(void *handle
)
389 struct stlink_usb_handle_s
*h
= handle
;
391 assert(handle
!= NULL
);
393 stlink_usb_init_buffer(handle
, h
->rx_ep
, 16);
395 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE
;
396 h
->cmdbuf
[h
->cmdidx
++] = 0;
397 h
->cmdbuf
[h
->cmdidx
++] = 0;
398 h
->cmdbuf
[h
->cmdidx
++] = 0;
399 h
->cmdbuf
[h
->cmdidx
++] = REQUEST_SENSE_LENGTH
;
401 res
= stlink_usb_xfer_rw(handle
, REQUEST_SENSE_LENGTH
, h
->databuf
, 16);
406 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
)
413 transfers block in cmdbuf
414 <size> indicates number of bytes in the following
417 static int stlink_usb_xfer(void *handle
, const uint8_t *buf
, int size
)
419 int err
, cmdsize
= STLINK_CMD_SIZE_V2
;
420 struct stlink_usb_handle_s
*h
= handle
;
422 assert(handle
!= NULL
);
424 if (h
->version
.stlink
== 1) {
425 cmdsize
= STLINK_SG_SIZE
;
426 /* put length in bCBWCBLength */
427 h
->cmdbuf
[14] = h
->cmdidx
-15;
430 err
= stlink_usb_xfer_rw(handle
, cmdsize
, buf
, size
);
435 if (h
->version
.stlink
== 1) {
436 if (stlink_usb_xfer_v1_get_status(handle
) != ERROR_OK
) {
437 /* check csw status */
438 if (h
->cmdbuf
[12] == 1) {
439 LOG_DEBUG("get sense");
440 if (stlink_usb_xfer_v1_get_sense(handle
) != ERROR_OK
)
451 Converts an STLINK status code held in the first byte of a response
452 to an openocd error, logs any error/wait status as debug output.
454 static int stlink_usb_error_check(void *handle
)
456 struct stlink_usb_handle_s
*h
= handle
;
458 assert(handle
!= NULL
);
460 if (h
->transport
== HL_TRANSPORT_SWIM
) {
461 switch (h
->databuf
[0]) {
462 case STLINK_SWIM_ERR_OK
:
464 case STLINK_SWIM_BUSY
:
467 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
472 /* TODO: no error checking yet on api V1 */
473 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
474 h
->databuf
[0] = STLINK_DEBUG_ERR_OK
;
476 switch (h
->databuf
[0]) {
477 case STLINK_DEBUG_ERR_OK
:
479 case STLINK_DEBUG_ERR_FAULT
:
480 LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT
);
482 case STLINK_SWD_AP_WAIT
:
483 LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT
);
485 case STLINK_SWD_DP_WAIT
:
486 LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT
);
488 case STLINK_JTAG_WRITE_ERROR
:
489 LOG_DEBUG("Write error");
491 case STLINK_JTAG_WRITE_VERIF_ERROR
:
492 LOG_DEBUG("Write verify error, ignoring");
494 case STLINK_SWD_AP_FAULT
:
495 /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
496 * returns ERROR_OK with the comment:
497 * Change in error status when reading outside RAM.
498 * This fix allows CDT plugin to visualize memory.
500 LOG_DEBUG("STLINK_SWD_AP_FAULT");
502 case STLINK_SWD_AP_ERROR
:
503 LOG_DEBUG("STLINK_SWD_AP_ERROR");
505 case STLINK_SWD_AP_PARITY_ERROR
:
506 LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
508 case STLINK_SWD_DP_FAULT
:
509 LOG_DEBUG("STLINK_SWD_DP_FAULT");
511 case STLINK_SWD_DP_ERROR
:
512 LOG_DEBUG("STLINK_SWD_DP_ERROR");
514 case STLINK_SWD_DP_PARITY_ERROR
:
515 LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
517 case STLINK_SWD_AP_WDATA_ERROR
:
518 LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
520 case STLINK_SWD_AP_STICKY_ERROR
:
521 LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
523 case STLINK_SWD_AP_STICKYORUN_ERROR
:
524 LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
527 LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h
->databuf
[0]);
533 /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
535 Works for commands where the STLINK_DEBUG status is returned in the first
536 byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
538 Returns an openocd result code.
540 static int stlink_cmd_allow_retry(void *handle
, const uint8_t *buf
, int size
)
544 struct stlink_usb_handle_s
*h
= handle
;
547 if ((h
->transport
!= HL_TRANSPORT_SWIM
) || !retries
) {
548 res
= stlink_usb_xfer(handle
, buf
, size
);
553 if (h
->transport
== HL_TRANSPORT_SWIM
) {
554 res
= stlink_swim_status(handle
);
559 res
= stlink_usb_error_check(handle
);
560 if (res
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
561 usleep((1<<retries
++) * 1000);
569 static int stlink_usb_read_trace(void *handle
, const uint8_t *buf
, int size
)
571 struct stlink_usb_handle_s
*h
= handle
;
573 assert(handle
!= NULL
);
575 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
577 if (jtag_libusb_bulk_read(h
->fd
, h
->trace_ep
, (char *)buf
,
578 size
, STLINK_READ_TIMEOUT
) != size
) {
579 LOG_ERROR("bulk trace read failed");
587 this function writes transfer length in
588 the right place in the cb
590 static void stlink_usb_set_cbw_transfer_datalength(void *handle
, uint32_t size
)
592 struct stlink_usb_handle_s
*h
= handle
;
594 buf_set_u32(h
->cmdbuf
+8, 0, 32, size
);
597 static void stlink_usb_xfer_v1_create_cmd(void *handle
, uint8_t direction
, uint32_t size
)
599 struct stlink_usb_handle_s
*h
= handle
;
601 /* fill the send buffer */
602 strcpy((char *)h
->cmdbuf
, "USBC");
604 /* csw tag not used */
605 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, 0);
607 /* cbw data transfer length (in the following data phase in or out) */
608 buf_set_u32(h
->cmdbuf
+h
->cmdidx
, 0, 32, size
);
611 h
->cmdbuf
[h
->cmdidx
++] = (direction
== h
->rx_ep
? ENDPOINT_IN
: ENDPOINT_OUT
);
612 h
->cmdbuf
[h
->cmdidx
++] = 0; /* lun */
613 /* cdb clength (is filled in at xfer) */
614 h
->cmdbuf
[h
->cmdidx
++] = 0;
618 static void stlink_usb_init_buffer(void *handle
, uint8_t direction
, uint32_t size
)
620 struct stlink_usb_handle_s
*h
= handle
;
622 h
->direction
= direction
;
626 memset(h
->cmdbuf
, 0, STLINK_SG_SIZE
);
627 memset(h
->databuf
, 0, STLINK_DATA_SIZE
);
629 if (h
->version
.stlink
== 1)
630 stlink_usb_xfer_v1_create_cmd(handle
, direction
, size
);
634 static int stlink_usb_version(void *handle
)
639 struct stlink_usb_handle_s
*h
= handle
;
641 assert(handle
!= NULL
);
643 stlink_usb_init_buffer(handle
, h
->rx_ep
, 6);
645 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_VERSION
;
647 res
= stlink_usb_xfer(handle
, h
->databuf
, 6);
652 v
= (h
->databuf
[0] << 8) | h
->databuf
[1];
654 h
->version
.stlink
= (v
>> 12) & 0x0f;
655 h
->version
.jtag
= (v
>> 6) & 0x3f;
656 h
->version
.swim
= v
& 0x3f;
657 h
->vid
= buf_get_u32(h
->databuf
, 16, 16);
658 h
->pid
= buf_get_u32(h
->databuf
, 32, 16);
661 switch (h
->version
.stlink
) {
663 /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
664 if (h
->version
.jtag
>= 11)
665 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
667 h
->version
.jtag_api_max
= STLINK_JTAG_API_V1
;
671 /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
672 h
->version
.jtag_api_max
= STLINK_JTAG_API_V2
;
674 /* API for trace from J13 */
675 /* API for target voltage from J13 */
676 if (h
->version
.jtag
>= 13)
677 flags
|= STLINK_F_HAS_TRACE
;
679 /* API to set SWD frequency from J22 */
680 if (h
->version
.jtag
>= 22)
681 flags
|= STLINK_F_HAS_SWD_SET_FREQ
;
683 /* API to set JTAG frequency from J24 */
684 if (h
->version
.jtag
>= 24)
685 flags
|= STLINK_F_HAS_JTAG_SET_FREQ
;
691 h
->version
.flags
= flags
;
693 LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
696 (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
) ? 1 : 2,
704 static int stlink_usb_check_voltage(void *handle
, float *target_voltage
)
706 struct stlink_usb_handle_s
*h
= handle
;
707 uint32_t adc_results
[2];
709 /* no error message, simply quit with error */
710 if (!(h
->version
.flags
& STLINK_F_HAS_TARGET_VOLT
))
711 return ERROR_COMMAND_NOTFOUND
;
713 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
715 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_TARGET_VOLTAGE
;
717 int result
= stlink_usb_xfer(handle
, h
->databuf
, 8);
719 if (result
!= ERROR_OK
)
723 adc_results
[0] = le_to_h_u32(h
->databuf
);
724 adc_results
[1] = le_to_h_u32(h
->databuf
+ 4);
729 *target_voltage
= 2 * ((float)adc_results
[1]) * (float)(1.2 / adc_results
[0]);
731 LOG_INFO("Target voltage: %f", (double)*target_voltage
);
736 static int stlink_usb_set_swdclk(void *handle
, uint16_t clk_divisor
)
738 struct stlink_usb_handle_s
*h
= handle
;
740 assert(handle
!= NULL
);
742 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
743 return ERROR_COMMAND_NOTFOUND
;
745 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
747 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
748 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ
;
749 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
752 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
754 if (result
!= ERROR_OK
)
760 static int stlink_usb_set_jtagclk(void *handle
, uint16_t clk_divisor
)
762 struct stlink_usb_handle_s
*h
= handle
;
764 assert(handle
!= NULL
);
766 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
767 return ERROR_COMMAND_NOTFOUND
;
769 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
771 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
772 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ
;
773 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, clk_divisor
);
776 int result
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
778 if (result
!= ERROR_OK
)
785 static int stlink_usb_current_mode(void *handle
, uint8_t *mode
)
788 struct stlink_usb_handle_s
*h
= handle
;
790 assert(handle
!= NULL
);
792 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
794 h
->cmdbuf
[h
->cmdidx
++] = STLINK_GET_CURRENT_MODE
;
796 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
801 *mode
= h
->databuf
[0];
807 static int stlink_usb_mode_enter(void *handle
, enum stlink_mode type
)
810 struct stlink_usb_handle_s
*h
= handle
;
812 assert(handle
!= NULL
);
814 /* on api V2 we are able the read the latest command
816 * TODO: we need the test on api V1 too
818 if (h
->jtag_api
== STLINK_JTAG_API_V2
)
821 stlink_usb_init_buffer(handle
, h
->rx_ep
, rx_size
);
824 case STLINK_MODE_DEBUG_JTAG
:
825 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
826 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
827 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
829 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
830 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_JTAG
;
832 case STLINK_MODE_DEBUG_SWD
:
833 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
834 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
835 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_ENTER
;
837 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_ENTER
;
838 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_ENTER_SWD
;
840 case STLINK_MODE_DEBUG_SWIM
:
841 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
842 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER
;
843 /* no answer for this function... */
846 case STLINK_MODE_DFU
:
847 case STLINK_MODE_MASS
:
852 return stlink_cmd_allow_retry(handle
, h
->databuf
, rx_size
);
856 static int stlink_usb_mode_leave(void *handle
, enum stlink_mode type
)
859 struct stlink_usb_handle_s
*h
= handle
;
861 assert(handle
!= NULL
);
863 stlink_usb_init_buffer(handle
, STLINK_NULL_EP
, 0);
866 case STLINK_MODE_DEBUG_JTAG
:
867 case STLINK_MODE_DEBUG_SWD
:
868 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
869 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_EXIT
;
871 case STLINK_MODE_DEBUG_SWIM
:
872 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
873 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_EXIT
;
875 case STLINK_MODE_DFU
:
876 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_COMMAND
;
877 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DFU_EXIT
;
879 case STLINK_MODE_MASS
:
884 res
= stlink_usb_xfer(handle
, 0, 0);
892 static int stlink_usb_assert_srst(void *handle
, int srst
);
894 static enum stlink_mode
stlink_get_mode(enum hl_transports t
)
897 case HL_TRANSPORT_SWD
:
898 return STLINK_MODE_DEBUG_SWD
;
899 case HL_TRANSPORT_JTAG
:
900 return STLINK_MODE_DEBUG_JTAG
;
901 case HL_TRANSPORT_SWIM
:
902 return STLINK_MODE_DEBUG_SWIM
;
904 return STLINK_MODE_UNKNOWN
;
909 static int stlink_usb_init_mode(void *handle
, bool connect_under_reset
)
913 enum stlink_mode emode
;
914 struct stlink_usb_handle_s
*h
= handle
;
916 assert(handle
!= NULL
);
918 res
= stlink_usb_current_mode(handle
, &mode
);
923 LOG_DEBUG("MODE: 0x%02X", mode
);
925 /* try to exit current mode */
927 case STLINK_DEV_DFU_MODE
:
928 emode
= STLINK_MODE_DFU
;
930 case STLINK_DEV_DEBUG_MODE
:
931 emode
= STLINK_MODE_DEBUG_SWD
;
933 case STLINK_DEV_SWIM_MODE
:
934 emode
= STLINK_MODE_DEBUG_SWIM
;
936 case STLINK_DEV_BOOTLOADER_MODE
:
937 case STLINK_DEV_MASS_MODE
:
939 emode
= STLINK_MODE_UNKNOWN
;
943 if (emode
!= STLINK_MODE_UNKNOWN
) {
944 res
= stlink_usb_mode_leave(handle
, emode
);
950 res
= stlink_usb_current_mode(handle
, &mode
);
955 /* we check the target voltage here as an aid to debugging connection problems.
956 * the stlink requires the target Vdd to be connected for reliable debugging.
957 * this cmd is supported in all modes except DFU
959 if (mode
!= STLINK_DEV_DFU_MODE
) {
961 float target_voltage
;
963 /* check target voltage (if supported) */
964 res
= stlink_usb_check_voltage(h
, &target_voltage
);
966 if (res
!= ERROR_OK
) {
967 if (res
!= ERROR_COMMAND_NOTFOUND
)
968 LOG_ERROR("voltage check failed");
969 /* attempt to continue as it is not a catastrophic failure */
971 /* check for a sensible target voltage, operating range is 1.65-5.5v
972 * according to datasheet */
973 if (target_voltage
< 1.5)
974 LOG_ERROR("target voltage may be too low for reliable debugging");
978 LOG_DEBUG("MODE: 0x%02X", mode
);
980 /* set selected mode */
981 emode
= stlink_get_mode(h
->transport
);
983 if (emode
== STLINK_MODE_UNKNOWN
) {
984 LOG_ERROR("selected mode (transport) not supported");
988 /* preliminary SRST assert:
989 * We want SRST is asserted before activating debug signals (mode_enter).
990 * As the required mode has not been set, the adapter may not know what pin to use.
991 * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
992 * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
993 * after power on, SWIM_RST stays unchanged */
994 if (connect_under_reset
&& emode
!= STLINK_MODE_DEBUG_SWIM
)
995 stlink_usb_assert_srst(handle
, 0);
996 /* do not check the return status here, we will
997 proceed and enter the desired mode below
998 and try asserting srst again. */
1000 res
= stlink_usb_mode_enter(handle
, emode
);
1001 if (res
!= ERROR_OK
)
1004 /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
1005 if (connect_under_reset
) {
1006 res
= stlink_usb_assert_srst(handle
, 0);
1007 if (res
!= ERROR_OK
)
1011 res
= stlink_usb_current_mode(handle
, &mode
);
1013 if (res
!= ERROR_OK
)
1016 LOG_DEBUG("MODE: 0x%02X", mode
);
1021 /* request status from last swim request */
1022 static int stlink_swim_status(void *handle
)
1024 struct stlink_usb_handle_s
*h
= handle
;
1027 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1028 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1029 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READSTATUS
;
1030 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1031 if (res
!= ERROR_OK
)
1036 the purpose of this function is unknown...
1037 capabilites? anyway for swim v6 it returns
1040 __attribute__((unused
))
1041 static int stlink_swim_cap(void *handle
, uint8_t *cap
)
1043 struct stlink_usb_handle_s
*h
= handle
;
1046 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1047 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1048 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READ_CAP
;
1049 h
->cmdbuf
[h
->cmdidx
++] = 0x01;
1050 res
= stlink_usb_xfer(handle
, h
->databuf
, 8);
1051 if (res
!= ERROR_OK
)
1053 memcpy(cap
, h
->databuf
, 8);
1057 /* debug dongle assert/deassert sreset line */
1058 static int stlink_swim_assert_reset(void *handle
, int reset
)
1060 struct stlink_usb_handle_s
*h
= handle
;
1063 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1064 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1066 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ASSERT_RESET
;
1068 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_DEASSERT_RESET
;
1069 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1070 if (res
!= ERROR_OK
)
1077 1.3ms low then 750Hz then 1.5kHz
1079 static int stlink_swim_enter(void *handle
)
1081 struct stlink_usb_handle_s
*h
= handle
;
1084 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1085 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1086 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_ENTER_SEQ
;
1087 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1088 if (res
!= ERROR_OK
)
1093 /* switch high/low speed swim */
1094 static int stlink_swim_speed(void *handle
, int speed
)
1096 struct stlink_usb_handle_s
*h
= handle
;
1099 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1100 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1101 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_SPEED
;
1103 h
->cmdbuf
[h
->cmdidx
++] = 1;
1105 h
->cmdbuf
[h
->cmdidx
++] = 0;
1106 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1107 if (res
!= ERROR_OK
)
1113 initiate srst from swim.
1114 nrst is pulled low for 50us.
1116 static int stlink_swim_generate_rst(void *handle
)
1118 struct stlink_usb_handle_s
*h
= handle
;
1121 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1122 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1123 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_GEN_RST
;
1124 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1125 if (res
!= ERROR_OK
)
1131 send resyncronize sequence
1132 swim is pulled low for 16us
1133 reply is 64 clks low
1135 static int stlink_swim_resync(void *handle
)
1137 struct stlink_usb_handle_s
*h
= handle
;
1140 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1141 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1142 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_RESET
;
1143 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1144 if (res
!= ERROR_OK
)
1149 static int stlink_swim_writebytes(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *data
)
1151 struct stlink_usb_handle_s
*h
= handle
;
1154 unsigned int datalen
= 0;
1155 int cmdsize
= STLINK_CMD_SIZE_V2
;
1157 if (len
> STLINK_DATA_SIZE
)
1160 if (h
->version
.stlink
== 1)
1161 cmdsize
= STLINK_SG_SIZE
;
1163 stlink_usb_init_buffer(handle
, h
->tx_ep
, 0);
1164 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1165 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_WRITEMEM
;
1166 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1168 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1170 for (i
= 0; i
< len
; i
++) {
1171 if (h
->cmdidx
== cmdsize
)
1172 h
->databuf
[datalen
++] = *(data
++);
1174 h
->cmdbuf
[h
->cmdidx
++] = *(data
++);
1176 if (h
->version
.stlink
== 1)
1177 stlink_usb_set_cbw_transfer_datalength(handle
, datalen
);
1179 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, datalen
);
1180 if (res
!= ERROR_OK
)
1185 static int stlink_swim_readbytes(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *data
)
1187 struct stlink_usb_handle_s
*h
= handle
;
1190 if (len
> STLINK_DATA_SIZE
)
1193 stlink_usb_init_buffer(handle
, h
->rx_ep
, 0);
1194 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1195 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READMEM
;
1196 h_u16_to_be(h
->cmdbuf
+h
->cmdidx
, len
);
1198 h_u32_to_be(h
->cmdbuf
+h
->cmdidx
, addr
);
1200 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 0);
1201 if (res
!= ERROR_OK
)
1204 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1205 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_COMMAND
;
1206 h
->cmdbuf
[h
->cmdidx
++] = STLINK_SWIM_READBUF
;
1207 res
= stlink_usb_xfer(handle
, data
, len
);
1208 if (res
!= ERROR_OK
)
1215 static int stlink_usb_idcode(void *handle
, uint32_t *idcode
)
1218 struct stlink_usb_handle_s
*h
= handle
;
1220 assert(handle
!= NULL
);
1222 /* there is no swim read core id cmd */
1223 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1228 stlink_usb_init_buffer(handle
, h
->rx_ep
, 4);
1230 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1231 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READCOREID
;
1233 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1235 if (res
!= ERROR_OK
)
1238 *idcode
= le_to_h_u32(h
->databuf
);
1240 LOG_DEBUG("IDCODE: 0x%08" PRIX32
, *idcode
);
1245 static int stlink_usb_v2_read_debug_reg(void *handle
, uint32_t addr
, uint32_t *val
)
1247 struct stlink_usb_handle_s
*h
= handle
;
1250 assert(handle
!= NULL
);
1252 stlink_usb_init_buffer(handle
, h
->rx_ep
, 8);
1254 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1255 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READDEBUGREG
;
1256 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1259 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1260 if (res
!= ERROR_OK
)
1263 *val
= le_to_h_u32(h
->databuf
+ 4);
1267 static int stlink_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
1269 struct stlink_usb_handle_s
*h
= handle
;
1271 assert(handle
!= NULL
);
1273 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1275 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1276 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1277 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG
;
1279 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG
;
1280 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1282 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1285 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1289 static int stlink_usb_trace_read(void *handle
, uint8_t *buf
, size_t *size
)
1291 struct stlink_usb_handle_s
*h
= handle
;
1293 assert(handle
!= NULL
);
1295 if (h
->trace
.enabled
&& (h
->version
.flags
& STLINK_F_HAS_TRACE
)) {
1298 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1300 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1301 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GET_TRACE_NB
;
1303 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1304 if (res
!= ERROR_OK
)
1307 size_t bytes_avail
= le_to_h_u16(h
->databuf
);
1308 *size
= bytes_avail
< *size
? bytes_avail
: *size
- 1;
1311 res
= stlink_usb_read_trace(handle
, buf
, *size
);
1312 if (res
!= ERROR_OK
)
1321 static enum target_state
stlink_usb_v2_get_status(void *handle
)
1326 result
= stlink_usb_v2_read_debug_reg(handle
, DCB_DHCSR
, &status
);
1327 if (result
!= ERROR_OK
)
1328 return TARGET_UNKNOWN
;
1330 if (status
& S_HALT
)
1331 return TARGET_HALTED
;
1332 else if (status
& S_RESET_ST
)
1333 return TARGET_RESET
;
1335 return TARGET_RUNNING
;
1339 static enum target_state
stlink_usb_state(void *handle
)
1342 struct stlink_usb_handle_s
*h
= handle
;
1344 assert(handle
!= NULL
);
1346 if (h
->transport
== HL_TRANSPORT_SWIM
) {
1347 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1348 if (res
!= ERROR_OK
)
1349 return TARGET_UNKNOWN
;
1351 res
= stlink_swim_resync(handle
);
1352 if (res
!= ERROR_OK
)
1353 return TARGET_UNKNOWN
;
1358 if (h
->reconnect_pending
) {
1359 LOG_INFO("Previous state query failed, trying to reconnect");
1360 res
= stlink_usb_mode_enter(handle
, stlink_get_mode(h
->transport
));
1362 if (res
!= ERROR_OK
)
1363 return TARGET_UNKNOWN
;
1365 h
->reconnect_pending
= false;
1368 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1369 res
= stlink_usb_v2_get_status(handle
);
1370 if (res
== TARGET_UNKNOWN
)
1371 h
->reconnect_pending
= true;
1375 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1377 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1378 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_GETSTATUS
;
1380 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1382 if (res
!= ERROR_OK
)
1383 return TARGET_UNKNOWN
;
1385 if (h
->databuf
[0] == STLINK_CORE_RUNNING
)
1386 return TARGET_RUNNING
;
1387 if (h
->databuf
[0] == STLINK_CORE_HALTED
)
1388 return TARGET_HALTED
;
1390 h
->reconnect_pending
= true;
1392 return TARGET_UNKNOWN
;
1395 static int stlink_usb_assert_srst(void *handle
, int srst
)
1397 struct stlink_usb_handle_s
*h
= handle
;
1399 assert(handle
!= NULL
);
1401 if (h
->transport
== HL_TRANSPORT_SWIM
)
1402 return stlink_swim_assert_reset(handle
, srst
);
1404 if (h
->version
.stlink
== 1)
1405 return ERROR_COMMAND_NOTFOUND
;
1407 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1409 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1410 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_DRIVE_NRST
;
1411 h
->cmdbuf
[h
->cmdidx
++] = srst
;
1413 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1417 static void stlink_usb_trace_disable(void *handle
)
1420 struct stlink_usb_handle_s
*h
= handle
;
1422 assert(handle
!= NULL
);
1424 assert(h
->version
.flags
& STLINK_F_HAS_TRACE
);
1426 LOG_DEBUG("Tracing: disable");
1428 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1429 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1430 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX
;
1431 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1433 if (res
== ERROR_OK
)
1434 h
->trace
.enabled
= false;
1439 static int stlink_usb_trace_enable(void *handle
)
1442 struct stlink_usb_handle_s
*h
= handle
;
1444 assert(handle
!= NULL
);
1446 if (h
->version
.flags
& STLINK_F_HAS_TRACE
) {
1447 stlink_usb_init_buffer(handle
, h
->rx_ep
, 10);
1449 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1450 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_START_TRACE_RX
;
1451 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, (uint16_t)STLINK_TRACE_SIZE
);
1453 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, h
->trace
.source_hz
);
1456 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1458 if (res
== ERROR_OK
) {
1459 h
->trace
.enabled
= true;
1460 LOG_DEBUG("Tracing: recording at %" PRIu32
"Hz", h
->trace
.source_hz
);
1463 LOG_ERROR("Tracing is not supported by this version.");
1471 static int stlink_usb_reset(void *handle
)
1473 struct stlink_usb_handle_s
*h
= handle
;
1476 assert(handle
!= NULL
);
1478 if (h
->transport
== HL_TRANSPORT_SWIM
)
1479 return stlink_swim_generate_rst(handle
);
1481 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1483 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1485 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1486 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_RESETSYS
;
1488 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_RESETSYS
;
1490 retval
= stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1491 if (retval
!= ERROR_OK
)
1494 if (h
->trace
.enabled
) {
1495 stlink_usb_trace_disable(h
);
1496 return stlink_usb_trace_enable(h
);
1503 static int stlink_usb_run(void *handle
)
1506 struct stlink_usb_handle_s
*h
= handle
;
1508 assert(handle
!= NULL
);
1510 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1511 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_DEBUGEN
);
1516 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1518 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1519 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_RUNCORE
;
1521 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1525 static int stlink_usb_halt(void *handle
)
1528 struct stlink_usb_handle_s
*h
= handle
;
1530 assert(handle
!= NULL
);
1532 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1533 res
= stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1538 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1540 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1541 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_FORCEDEBUG
;
1543 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1547 static int stlink_usb_step(void *handle
)
1549 struct stlink_usb_handle_s
*h
= handle
;
1551 assert(handle
!= NULL
);
1553 if (h
->jtag_api
== STLINK_JTAG_API_V2
) {
1554 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
1555 * that the Cortex-M3 currently does. */
1556 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_MASKINTS
|C_DEBUGEN
);
1557 stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_STEP
|C_MASKINTS
|C_DEBUGEN
);
1558 return stlink_usb_write_debug_reg(handle
, DCB_DHCSR
, DBGKEY
|C_HALT
|C_DEBUGEN
);
1561 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1563 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1564 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_STEPCORE
;
1566 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1570 static int stlink_usb_read_regs(void *handle
)
1573 struct stlink_usb_handle_s
*h
= handle
;
1575 assert(handle
!= NULL
);
1577 stlink_usb_init_buffer(handle
, h
->rx_ep
, 84);
1579 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1580 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1581 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READALLREGS
;
1583 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READALLREGS
;
1585 res
= stlink_usb_xfer(handle
, h
->databuf
, 84);
1587 if (res
!= ERROR_OK
)
1594 static int stlink_usb_read_reg(void *handle
, int num
, uint32_t *val
)
1597 struct stlink_usb_handle_s
*h
= handle
;
1599 assert(handle
!= NULL
);
1601 stlink_usb_init_buffer(handle
, h
->rx_ep
, h
->jtag_api
== STLINK_JTAG_API_V1
? 4 : 8);
1603 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1604 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1605 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_READREG
;
1607 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READREG
;
1608 h
->cmdbuf
[h
->cmdidx
++] = num
;
1610 if (h
->jtag_api
== STLINK_JTAG_API_V1
) {
1611 res
= stlink_usb_xfer(handle
, h
->databuf
, 4);
1612 if (res
!= ERROR_OK
)
1614 *val
= le_to_h_u32(h
->databuf
);
1617 res
= stlink_cmd_allow_retry(handle
, h
->databuf
, 8);
1618 if (res
!= ERROR_OK
)
1620 *val
= le_to_h_u32(h
->databuf
+ 4);
1626 static int stlink_usb_write_reg(void *handle
, int num
, uint32_t val
)
1628 struct stlink_usb_handle_s
*h
= handle
;
1630 assert(handle
!= NULL
);
1632 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1634 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1635 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1636 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV1_WRITEREG
;
1638 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEREG
;
1639 h
->cmdbuf
[h
->cmdidx
++] = num
;
1640 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, val
);
1643 return stlink_cmd_allow_retry(handle
, h
->databuf
, 2);
1646 static int stlink_usb_get_rw_status(void *handle
)
1649 struct stlink_usb_handle_s
*h
= handle
;
1651 assert(handle
!= NULL
);
1653 if (h
->jtag_api
== STLINK_JTAG_API_V1
)
1656 stlink_usb_init_buffer(handle
, h
->rx_ep
, 2);
1658 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1659 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS
;
1661 res
= stlink_usb_xfer(handle
, h
->databuf
, 2);
1663 if (res
!= ERROR_OK
)
1666 return stlink_usb_error_check(h
);
1670 static int stlink_usb_read_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1674 uint16_t read_len
= len
;
1675 struct stlink_usb_handle_s
*h
= handle
;
1677 assert(handle
!= NULL
);
1679 /* max 8bit read/write is 64bytes */
1680 if (len
> STLINK_MAX_RW8
) {
1681 LOG_DEBUG("max buffer length exceeded");
1685 stlink_usb_init_buffer(handle
, h
->rx_ep
, read_len
);
1687 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1688 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_8BIT
;
1689 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1691 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1694 /* we need to fix read length for single bytes */
1698 res
= stlink_usb_xfer(handle
, h
->databuf
, read_len
);
1700 if (res
!= ERROR_OK
)
1703 memcpy(buffer
, h
->databuf
, len
);
1705 return stlink_usb_get_rw_status(handle
);
1709 static int stlink_usb_write_mem8(void *handle
, uint32_t addr
, uint16_t len
,
1710 const uint8_t *buffer
)
1713 struct stlink_usb_handle_s
*h
= handle
;
1715 assert(handle
!= NULL
);
1717 /* max 8bit read/write is 64bytes */
1718 if (len
> STLINK_MAX_RW8
) {
1719 LOG_DEBUG("max buffer length exceeded");
1723 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1725 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1726 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_8BIT
;
1727 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1729 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1732 res
= stlink_usb_xfer(handle
, buffer
, len
);
1734 if (res
!= ERROR_OK
)
1737 return stlink_usb_get_rw_status(handle
);
1741 static int stlink_usb_read_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1745 struct stlink_usb_handle_s
*h
= handle
;
1747 assert(handle
!= NULL
);
1749 /* only supported by stlink/v2 and for firmware >= 26 */
1750 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1751 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1752 return ERROR_COMMAND_NOTFOUND
;
1754 /* data must be a multiple of 2 and half-word aligned */
1755 if (len
% 2 || addr
% 2) {
1756 LOG_DEBUG("Invalid data alignment");
1757 return ERROR_TARGET_UNALIGNED_ACCESS
;
1760 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1762 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1763 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_READMEM_16BIT
;
1764 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1766 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1769 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1771 if (res
!= ERROR_OK
)
1774 memcpy(buffer
, h
->databuf
, len
);
1776 return stlink_usb_get_rw_status(handle
);
1780 static int stlink_usb_write_mem16(void *handle
, uint32_t addr
, uint16_t len
,
1781 const uint8_t *buffer
)
1784 struct stlink_usb_handle_s
*h
= handle
;
1786 assert(handle
!= NULL
);
1788 /* only supported by stlink/v2 and for firmware >= 26 */
1789 if (h
->jtag_api
== STLINK_JTAG_API_V1
||
1790 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26))
1791 return ERROR_COMMAND_NOTFOUND
;
1793 /* data must be a multiple of 2 and half-word aligned */
1794 if (len
% 2 || addr
% 2) {
1795 LOG_DEBUG("Invalid data alignment");
1796 return ERROR_TARGET_UNALIGNED_ACCESS
;
1799 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1801 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1802 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT
;
1803 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1805 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1808 res
= stlink_usb_xfer(handle
, buffer
, len
);
1810 if (res
!= ERROR_OK
)
1813 return stlink_usb_get_rw_status(handle
);
1817 static int stlink_usb_read_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1821 struct stlink_usb_handle_s
*h
= handle
;
1823 assert(handle
!= NULL
);
1825 /* data must be a multiple of 4 and word aligned */
1826 if (len
% 4 || addr
% 4) {
1827 LOG_DEBUG("Invalid data alignment");
1828 return ERROR_TARGET_UNALIGNED_ACCESS
;
1831 stlink_usb_init_buffer(handle
, h
->rx_ep
, len
);
1833 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1834 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_READMEM_32BIT
;
1835 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1837 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1840 res
= stlink_usb_xfer(handle
, h
->databuf
, len
);
1842 if (res
!= ERROR_OK
)
1845 memcpy(buffer
, h
->databuf
, len
);
1847 return stlink_usb_get_rw_status(handle
);
1851 static int stlink_usb_write_mem32(void *handle
, uint32_t addr
, uint16_t len
,
1852 const uint8_t *buffer
)
1855 struct stlink_usb_handle_s
*h
= handle
;
1857 assert(handle
!= NULL
);
1859 /* data must be a multiple of 4 and word aligned */
1860 if (len
% 4 || addr
% 4) {
1861 LOG_DEBUG("Invalid data alignment");
1862 return ERROR_TARGET_UNALIGNED_ACCESS
;
1865 stlink_usb_init_buffer(handle
, h
->tx_ep
, len
);
1867 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_COMMAND
;
1868 h
->cmdbuf
[h
->cmdidx
++] = STLINK_DEBUG_WRITEMEM_32BIT
;
1869 h_u32_to_le(h
->cmdbuf
+h
->cmdidx
, addr
);
1871 h_u16_to_le(h
->cmdbuf
+h
->cmdidx
, len
);
1874 res
= stlink_usb_xfer(handle
, buffer
, len
);
1876 if (res
!= ERROR_OK
)
1879 return stlink_usb_get_rw_status(handle
);
1882 static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block
, uint32_t address
)
1884 uint32_t max_tar_block
= (tar_autoincr_block
- ((tar_autoincr_block
- 1) & address
));
1885 if (max_tar_block
== 0)
1887 return max_tar_block
;
1890 static int stlink_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
1891 uint32_t count
, uint8_t *buffer
)
1893 int retval
= ERROR_OK
;
1894 uint32_t bytes_remaining
;
1896 struct stlink_usb_handle_s
*h
= handle
;
1898 /* calculate byte count */
1901 /* switch to 8 bit if stlink does not support 16 bit memory read */
1902 if (size
== 2 && (h
->jtag_api
== STLINK_JTAG_API_V1
||
1903 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
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
->jtag_api
== STLINK_JTAG_API_V1
||
1989 (h
->jtag_api
== STLINK_JTAG_API_V2
&& h
->version
.jtag
< 26)))
1994 bytes_remaining
= (size
!= 1) ? \
1995 stlink_max_block_size(h
->max_mem_packet
, addr
) : STLINK_MAX_RW8
;
1997 if (count
< bytes_remaining
)
1998 bytes_remaining
= count
;
2000 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2001 retval
= stlink_swim_writebytes(handle
, addr
, bytes_remaining
, buffer
);
2002 if (retval
!= ERROR_OK
)
2006 * all stlink support 8/32bit memory read/writes and only from
2007 * stlink V2J26 there is support for 16 bit memory read/write.
2008 * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
2013 /* When in jtag mode the stlink uses the auto-increment functionality.
2014 * However it expects us to pass the data correctly, this includes
2015 * alignment and any page boundaries. We already do this as part of the
2016 * adi_v5 implementation, but the stlink is a hla adapter and so this
2017 * needs implementing manually.
2018 * currently this only affects jtag mode, according to ST they do single
2019 * access in SWD mode - but this may change and so we do it for both modes */
2021 /* we first need to check for any unaligned bytes */
2022 if (addr
& (size
- 1)) {
2024 uint32_t head_bytes
= size
- (addr
& (size
- 1));
2025 retval
= stlink_usb_write_mem8(handle
, addr
, head_bytes
, buffer
);
2026 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2027 usleep((1<<retries
++) * 1000);
2030 if (retval
!= ERROR_OK
)
2032 buffer
+= head_bytes
;
2034 count
-= head_bytes
;
2035 bytes_remaining
-= head_bytes
;
2038 if (bytes_remaining
& (size
- 1))
2039 retval
= stlink_usb_write_mem(handle
, addr
, 1, bytes_remaining
, buffer
);
2041 retval
= stlink_usb_write_mem16(handle
, addr
, bytes_remaining
, buffer
);
2043 retval
= stlink_usb_write_mem32(handle
, addr
, bytes_remaining
, buffer
);
2046 retval
= stlink_usb_write_mem8(handle
, addr
, bytes_remaining
, buffer
);
2047 if (retval
== ERROR_WAIT
&& retries
< MAX_WAIT_RETRIES
) {
2048 usleep((1<<retries
++) * 1000);
2051 if (retval
!= ERROR_OK
)
2054 buffer
+= bytes_remaining
;
2055 addr
+= bytes_remaining
;
2056 count
-= bytes_remaining
;
2063 static int stlink_usb_override_target(const char *targetname
)
2065 return !strcmp(targetname
, "cortex_m");
2068 static int stlink_speed_swim(void *handle
, int khz
, bool query
)
2071 we dont care what the khz rate is
2072 we only have low and high speed...
2073 before changing speed the SWIM_CSR HS bit
2077 stlink_swim_speed(handle
, 0);
2079 stlink_swim_speed(handle
, 1);
2083 static int stlink_match_speed_map(const struct speed_map
*map
, unsigned int map_size
, int khz
, bool query
)
2086 int speed_index
= -1;
2087 int speed_diff
= INT_MAX
;
2090 for (i
= 0; i
< map_size
; i
++) {
2091 if (khz
== map
[i
].speed
) {
2095 int current_diff
= khz
- map
[i
].speed
;
2096 /* get abs value for comparison */
2097 current_diff
= (current_diff
> 0) ? current_diff
: -current_diff
;
2098 if ((current_diff
< speed_diff
) && khz
>= map
[i
].speed
) {
2099 speed_diff
= current_diff
;
2105 if (speed_index
== -1) {
2106 /* this will only be here if we cannot match the slow speed.
2107 * use the slowest speed we support.*/
2108 speed_index
= map_size
- 1;
2110 } else if (i
== map_size
)
2113 if (!match
&& query
) {
2114 LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
2115 khz
, map
[speed_index
].speed
);
2121 static int stlink_speed_swd(void *handle
, int khz
, bool query
)
2124 struct stlink_usb_handle_s
*h
= handle
;
2126 /* old firmware cannot change it */
2127 if (!(h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
))
2130 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_swd
,
2131 ARRAY_SIZE(stlink_khz_to_speed_map_swd
), khz
, query
);
2134 int result
= stlink_usb_set_swdclk(h
, stlink_khz_to_speed_map_swd
[speed_index
].speed_divisor
);
2135 if (result
!= ERROR_OK
) {
2136 LOG_ERROR("Unable to set adapter speed");
2141 return stlink_khz_to_speed_map_swd
[speed_index
].speed
;
2144 static int stlink_speed_jtag(void *handle
, int khz
, bool query
)
2147 struct stlink_usb_handle_s
*h
= handle
;
2149 /* old firmware cannot change it */
2150 if (!(h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
))
2153 speed_index
= stlink_match_speed_map(stlink_khz_to_speed_map_jtag
,
2154 ARRAY_SIZE(stlink_khz_to_speed_map_jtag
), khz
, query
);
2157 int result
= stlink_usb_set_jtagclk(h
, stlink_khz_to_speed_map_jtag
[speed_index
].speed_divisor
);
2158 if (result
!= ERROR_OK
) {
2159 LOG_ERROR("Unable to set adapter speed");
2164 return stlink_khz_to_speed_map_jtag
[speed_index
].speed
;
2167 void stlink_dump_speed_map(const struct speed_map
*map
, unsigned int map_size
)
2171 LOG_DEBUG("Supported clock speeds are:");
2172 for (i
= 0; i
< map_size
; i
++)
2173 LOG_DEBUG("%d kHz", map
[i
].speed
);
2176 static int stlink_speed(void *handle
, int khz
, bool query
)
2178 struct stlink_usb_handle_s
*h
= handle
;
2183 if (h
->transport
== HL_TRANSPORT_SWIM
)
2184 return stlink_speed_swim(handle
, khz
, query
);
2185 else if (h
->transport
== HL_TRANSPORT_SWD
)
2186 return stlink_speed_swd(handle
, khz
, query
);
2187 else if (h
->transport
== HL_TRANSPORT_JTAG
)
2188 return stlink_speed_jtag(handle
, khz
, query
);
2194 static int stlink_usb_close(void *handle
)
2198 enum stlink_mode emode
;
2199 struct stlink_usb_handle_s
*h
= handle
;
2202 res
= stlink_usb_current_mode(handle
, &mode
);
2205 /* do not exit if return code != ERROR_OK,
2206 it prevents us from closing jtag_libusb */
2208 if (res
== ERROR_OK
) {
2209 /* try to exit current mode */
2211 case STLINK_DEV_DFU_MODE
:
2212 emode
= STLINK_MODE_DFU
;
2214 case STLINK_DEV_DEBUG_MODE
:
2215 emode
= STLINK_MODE_DEBUG_SWD
;
2217 case STLINK_DEV_SWIM_MODE
:
2218 emode
= STLINK_MODE_DEBUG_SWIM
;
2220 case STLINK_DEV_BOOTLOADER_MODE
:
2221 case STLINK_DEV_MASS_MODE
:
2223 emode
= STLINK_MODE_UNKNOWN
;
2227 if (emode
!= STLINK_MODE_UNKNOWN
)
2228 stlink_usb_mode_leave(handle
, emode
);
2229 /* do not check return code, it prevent
2230 us from closing jtag_libusb */
2234 jtag_libusb_close(h
->fd
);
2242 static int stlink_usb_open(struct hl_interface_param_s
*param
, void **fd
)
2244 int err
, retry_count
= 1;
2245 struct stlink_usb_handle_s
*h
;
2246 enum stlink_jtag_api_version api
;
2248 LOG_DEBUG("stlink_usb_open");
2250 h
= calloc(1, sizeof(struct stlink_usb_handle_s
));
2253 LOG_DEBUG("malloc failed");
2257 h
->transport
= param
->transport
;
2259 for (unsigned i
= 0; param
->vid
[i
]; i
++) {
2260 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
2261 param
->transport
, param
->vid
[i
], param
->pid
[i
],
2262 param
->serial
? param
->serial
: "");
2266 On certain host USB configurations(e.g. MacBook Air)
2267 STLINKv2 dongle seems to have its FW in a funky state if,
2268 after plugging it in, you try to use openocd with it more
2269 then once (by launching and closing openocd). In cases like
2270 that initial attempt to read the FW info via
2271 stlink_usb_version will fail and the device has to be reset
2272 in order to become operational.
2275 if (jtag_libusb_open(param
->vid
, param
->pid
, param
->serial
, &h
->fd
) != ERROR_OK
) {
2276 LOG_ERROR("open failed");
2280 jtag_libusb_set_configuration(h
->fd
, 0);
2282 if (jtag_libusb_claim_interface(h
->fd
, 0) != ERROR_OK
) {
2283 LOG_DEBUG("claim interface failed");
2287 /* RX EP is common for all versions */
2288 h
->rx_ep
= STLINK_RX_EP
;
2291 if (jtag_libusb_get_pid(jtag_libusb_get_device(h
->fd
), &pid
) != ERROR_OK
) {
2292 LOG_DEBUG("libusb_get_pid failed");
2296 /* wrap version for first read */
2299 h
->version
.stlink
= 1;
2300 h
->tx_ep
= STLINK_TX_EP
;
2302 case STLINK_V2_1_PID
:
2303 case STLINK_V2_1_NO_MSD_PID
:
2304 h
->version
.stlink
= 2;
2305 h
->tx_ep
= STLINK_V2_1_TX_EP
;
2306 h
->trace_ep
= STLINK_V2_1_TRACE_EP
;
2309 /* fall through - we assume V2 to be the default version*/
2311 h
->version
.stlink
= 2;
2312 h
->tx_ep
= STLINK_TX_EP
;
2313 h
->trace_ep
= STLINK_TRACE_EP
;
2317 /* get the device version */
2318 err
= stlink_usb_version(h
);
2320 if (err
== ERROR_OK
) {
2322 } else if (h
->version
.stlink
== 1 ||
2324 LOG_ERROR("read version failed");
2327 err
= jtag_libusb_release_interface(h
->fd
, 0);
2328 if (err
!= ERROR_OK
) {
2329 LOG_ERROR("release interface failed");
2333 err
= jtag_libusb_reset_device(h
->fd
);
2334 if (err
!= ERROR_OK
) {
2335 LOG_ERROR("reset device failed");
2339 jtag_libusb_close(h
->fd
);
2341 Give the device one second to settle down and
2344 usleep(1 * 1000 * 1000);
2349 /* check if mode is supported */
2352 switch (h
->transport
) {
2353 case HL_TRANSPORT_SWD
:
2354 if (h
->version
.jtag_api_max
== STLINK_JTAG_API_V1
)
2357 case HL_TRANSPORT_JTAG
:
2358 if (h
->version
.jtag
== 0)
2361 case HL_TRANSPORT_SWIM
:
2362 if (h
->version
.swim
== 0)
2370 if (err
!= ERROR_OK
) {
2371 LOG_ERROR("mode (transport) not supported by device");
2375 api
= h
->version
.jtag_api_max
;
2377 LOG_INFO("using stlink api v%d", api
);
2379 /* set the used jtag api, this will default to the newest supported version */
2382 /* initialize the debug hardware */
2383 err
= stlink_usb_init_mode(h
, param
->connect_under_reset
);
2385 if (err
!= ERROR_OK
) {
2386 LOG_ERROR("init mode failed (unable to connect to the target)");
2390 if (h
->transport
== HL_TRANSPORT_SWIM
) {
2391 err
= stlink_swim_enter(h
);
2392 if (err
!= ERROR_OK
) {
2393 LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
2397 h
->max_mem_packet
= STLINK_DATA_SIZE
;
2401 if (h
->transport
== HL_TRANSPORT_JTAG
) {
2402 if (h
->version
.flags
& STLINK_F_HAS_JTAG_SET_FREQ
) {
2403 stlink_dump_speed_map(stlink_khz_to_speed_map_jtag
, ARRAY_SIZE(stlink_khz_to_speed_map_jtag
));
2404 stlink_speed(h
, param
->initial_interface_speed
, false);
2406 } else if (h
->transport
== HL_TRANSPORT_SWD
) {
2407 if (h
->version
.flags
& STLINK_F_HAS_SWD_SET_FREQ
) {
2408 stlink_dump_speed_map(stlink_khz_to_speed_map_swd
, ARRAY_SIZE(stlink_khz_to_speed_map_swd
));
2409 stlink_speed(h
, param
->initial_interface_speed
, false);
2413 /* get cpuid, so we can determine the max page size
2414 * start with a safe default */
2415 h
->max_mem_packet
= (1 << 10);
2418 err
= stlink_usb_read_mem32(h
, CPUID
, 4, buffer
);
2419 if (err
== ERROR_OK
) {
2420 uint32_t cpuid
= le_to_h_u32(buffer
);
2421 int i
= (cpuid
>> 4) & 0xf;
2422 if (i
== 4 || i
== 3) {
2423 /* Cortex-M3/M4 has 4096 bytes autoincrement range */
2424 h
->max_mem_packet
= (1 << 12);
2428 LOG_DEBUG("Using TAR autoincrement: %" PRIu32
, h
->max_mem_packet
);
2435 stlink_usb_close(h
);
2440 int stlink_config_trace(void *handle
, bool enabled
, enum tpiu_pin_protocol pin_protocol
,
2441 uint32_t port_size
, unsigned int *trace_freq
)
2443 struct stlink_usb_handle_s
*h
= handle
;
2445 if (enabled
&& (!(h
->version
.flags
& STLINK_F_HAS_TRACE
) ||
2446 pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
)) {
2447 LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
2452 stlink_usb_trace_disable(h
);
2456 if (*trace_freq
> STLINK_TRACE_MAX_HZ
) {
2457 LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
2458 STLINK_TRACE_MAX_HZ
);
2462 stlink_usb_trace_disable(h
);
2465 *trace_freq
= STLINK_TRACE_MAX_HZ
;
2466 h
->trace
.source_hz
= *trace_freq
;
2468 return stlink_usb_trace_enable(h
);
2472 struct hl_layout_api_s stlink_usb_layout_api
= {
2474 .open
= stlink_usb_open
,
2476 .close
= stlink_usb_close
,
2478 .idcode
= stlink_usb_idcode
,
2480 .state
= stlink_usb_state
,
2482 .reset
= stlink_usb_reset
,
2484 .assert_srst
= stlink_usb_assert_srst
,
2486 .run
= stlink_usb_run
,
2488 .halt
= stlink_usb_halt
,
2490 .step
= stlink_usb_step
,
2492 .read_regs
= stlink_usb_read_regs
,
2494 .read_reg
= stlink_usb_read_reg
,
2496 .write_reg
= stlink_usb_write_reg
,
2498 .read_mem
= stlink_usb_read_mem
,
2500 .write_mem
= stlink_usb_write_mem
,
2502 .write_debug_reg
= stlink_usb_write_debug_reg
,
2504 .override_target
= stlink_usb_override_target
,
2506 .speed
= stlink_speed
,
2508 .config_trace
= stlink_config_trace
,
2510 .poll_trace
= stlink_usb_trace_read
,